-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmartControl.cpp
More file actions
308 lines (256 loc) · 9.62 KB
/
SmartControl.cpp
File metadata and controls
308 lines (256 loc) · 9.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include <algorithm>
#include <regex>
#if defined(SECURITY_TOKEN_ENABLED) && ((SECURITY_TOKEN_ENABLED == 0) || (SECURITY_TOKEN_ENABLED == false))
#define GetSecurityToken(a, b) 0
#define GetToken(a, b, c) 0
#else
#include <WPEFramework/securityagent/securityagent.h>
#include <WPEFramework/securityagent/SecurityTokenUtil.h>
#endif
#include "SmartControl.h"
#include "UtilsJsonRpc.h"
#include <fstream>
const string WPEFramework::Plugin::SmartControl::SERVICE_NAME = "org.rdk.SmarControl";
const string WPEFramework::Plugin::SmartControl::METHOD_SMARTCONTROL_SET_VOLUME_LEVEL = "ds_setVolumeLevel";
const string WPEFramework::Plugin::SmartControl::METHOD_SMARTCONTROL_GET_VOLUME_LEVEL = "ds_getvolumeLevel";
using namespace std;
#define API_VERSION_NUMBER_MAJOR 1
#define API_VERSION_NUMBER_MINOR 0
#define API_VERSION_NUMBER_PATCH 0
#define SERVER_DETAILS "127.0.0.1:9998"
#define DISPLAYSETTINGS_CALLSIGN_VER ".1"
#define DISPLAYSETTINGS_CALLSIGN "org.rdk.DisplaySettings"
#define BLUETOOTH_CALLSIGN_VER ".1"
#define BLUETOOTH_CALLSIGN "org.rdk.Bluetooth"
#define SECURITY_TOKEN_LEN_MAX 1024
#define THUNDER_RPC_TIMEOUT 2000
#define VOLUME_THRESHOLD_LEVEL 4
namespace WPEFramework
{
namespace
{
static Plugin::Metadata<Plugin::SmartControl> metadata(
// Version (Major, Minor, Patch)
API_VERSION_NUMBER_MAJOR, API_VERSION_NUMBER_MINOR, API_VERSION_NUMBER_PATCH,
// Preconditions
{},
// Terminations
{},
// Controls
{});
}
namespace Plugin
{
SERVICE_REGISTRATION(SmartControl, API_VERSION_NUMBER_MAJOR, API_VERSION_NUMBER_MINOR, API_VERSION_NUMBER_PATCH);
SmartControl *SmartControl::_instance = nullptr;
SmartControl::SmartControl()
: PluginHost::JSONRPC(),
m_isInitialized(false),
{
SmartControl::_instance = this;
Register(METHOD_SMARTCONTROL_SET_VOLUME_LEVEL, &SmartControl::ds_setVolumeLevel, this);
Register(METHOD_SMARTCONTROL_GET_VOLUME_LEVEL, &SmartControl::ds_getVolumeLevel, this);
}
SmartControl::~SmartControl()
{
if (nullptr != m_DisplaySettingsPluginObj)
{
delete m_DisplaySettingsPluginObj;
m_DisplaySettingsPluginObj = nullptr;
}
if (nullptr != m_BluetoothPluginObj)
{
delete m_BluetoothPluginObj;
m_BluetoothPluginObj = nullptr;
}
Unregister(METHOD_SMARTCONTROL_SET_VOLUME_LEVEL);
Unregister(METHOD_SMARTCONTROL_GET_VOLUME_LEVEL);
}
// Thunder plugins communication
void SmartControl::getThunderPlugins()
{
if (nullptr == m_DisplaySettingsPluginObj)
{
string token;
// TODO: use interfaces and remove token
auto security = m_CurrentService->QueryInterfaceByCallsign<PluginHost::IAuthenticate>("SecurityAgent");
if (nullptr != security)
{
string payload = "http://localhost";
if (security->CreateToken(static_cast<uint16_t>(payload.length()),
reinterpret_cast<const uint8_t *>(payload.c_str()),
token) == Core::ERROR_NONE)
{
LOGINFO("got security token\n");
}
else
{
LOGERR("failed to get security token\n");
}
security->Release();
}
else
{
LOGERR("No security agent\n");
}
string query = "token=" + token;
Core::SystemInfo::SetEnvironment(_T("THUNDER_ACCESS"), (_T(SERVER_DETAILS)));
m_DisplaySettingsPluginObj = new WPEFramework::JSONRPC::LinkType<Core::JSON::IElement>(_T(DISPLAYSETTINGS_CALLSIGN_VER), (_T("SmartControl")), false, query);
if (nullptr == m_DisplaySettingsPluginObj)
{
LOGERR("JSONRPC: %s: initialization failed", DISPLAYSETTINGS_CALLSIGN_VER);
}
else
{
LOGINFO("JSONRPC: %s: initialization ok", DISPLAYSETTINGS_CALLSIGN_VER);
}
m_BluetoothPluginObj = new WPEFramework::JSONRPC::LinkType<Core::JSON::IElement>(_T(BLUETOOTH_CALLSIGN_VER), (_T("SmartControl")), false, query);
if (nullptr == m_BluetoothPluginObj)
{
LOGERR("JSONRPC: %s: initialization failed", BLUETOOTH_CALLSIGN_VER);
}
else
{
LOGINFO("JSONRPC: %s: initialization ok", BLUETOOTH_CALLSIGN_VER);
}
}
}
const string SmartControl::Initialize(PluginHost::IShell *service)
{
if (!m_isInitialized)
{
getThunderPlugins();
m_CurrentService = service;
//event subscribe
if (nullptr != m_DisplaySettingsPluginObj)
{
m_DisplaySettingsPluginObj-->Subscribe<JsonObject>(1000, "volumeLevelChanged", &SmartControl::onVolumeLevelChangedHandler, this);
}
if (nullptr != m_BluetoothPluginObj)
{
m_BluetoothPluginObj-->Subscribe<JsonObject>(1000, "onDeviceFound", &SmartControl::onDeviceFoundHandler, this);
}
m_isInitialized = true;
}
return (string());
}
void SmartControl::Deinitialize(PluginHost::IShell * /* service */)
{
SmartControl::_instance = nullptr;
if(m_isInitialized)
{
m_isInitialized = false;
m_CurrentService = nullptr;
}
}
string SmartControl::Information() const
{
return (string("{\"service\": \"") + SERVICE_NAME + string("\"}"));
}
bool SmartControl::ds_getVolumeLevel(JsonObject &Result)
{
bool result = false;
JsonObject params;
uint32_t Vlevel;
LOGINFO("Entering..!!!");
getThunderPlugins();
if (nullptr == m_DisplaySettingsPluginObj)
{
LOGERR("m_DisplaySettingsPluginObj not yet instantiated");
return result;
}
uint32_t ret = m_DisplaySettingsPluginObj->Invoke<JsonObject, JsonObject>(THUNDER_RPC_TIMEOUT, _T("getVolumeLevel"), params, Result);
if (Core::ERROR_NONE == ret)
{
if (Result["success"].Boolean())
{
Vlevel = Result["volumeLevel"].Number();
LOGINFO("ds_getVolumeLevel value=%u", Vlevel);
result = true;
}
else
{
ret = Core::ERROR_GENERAL;
LOGERR("ds_getVolumeLevel call failed");
}
}
else
{
LOGERR("ds_getVolumeLevel failed E[%u]", ret);
}
return result;
}
bool SmartControl::ds_setVolumeLevel(const JsonObject ¶meters, JsonObject &Result)
{
bool result = false;
if ((parameters.HasLabel("volumeLevel")) && (parameters.HasLabel("audioPort")))
{
getThunderPlugins();
if (nullptr == m_DisplaySettingsPluginObj)
{
LOGERR("m_DisplaySettingsPluginObj not yet instantiated");
return false;
}
uint32_t ret = m_DisplaySettingsPluginObj->Invoke<JsonObject, JsonObject>(THUNDER_RPC_TIMEOUT, _T("setVolumeLevel"), parameters, Result);
if (Core::ERROR_NONE == ret)
{
if (Result["success"].Boolean())
{
result = true;
}
else
{
ret = Core::ERROR_GENERAL;
LOGERR("ds_setVolumeLevel call failed");
}
}
else
{
LOGERR("ds_setVolumeLevel failed E[%u]", ret);
}
}
return result;
}
bool ThresholdLevelCheck()
{
bool result = false;
JsonObject Internal;
JsonObject InternalResponse;
if (ds_getVolumeLevel(Internal) )
{
if (Internal["volumelevel"].Number() <= VOLUME_THRESHOLD_LEVEL )
{
Internal["volumelevel"] = VOLUME_THRESHOLD_LEVEL;
Internal["audioPort"] = "SPEAKER0";
//Set the Volumelevel in threshold level
if (ds_setVolumeLevel(Internal,InternalResponse))
result = true;
}
else
{
LOGERR("Unable to set to Threshold level")
}
}
return result;
}
void onVolumeLevelChangedHandler(const JsonObject ¶meters)
{
string message;
JsonObject InternalResponse;
parameters.ToString(message);
LOGINFO("[VolumeLevel Event], [%s]", message.c_str());
if ( parameters["volumelevel"].Number() == VOLUME_THRESHOLD_LEVEL )
{
//update the database
LOGINFO("Current Volume Level : %d", InternalResponse["volumelevel"].Number());
}
}
void onDeviceOnHandler(const JsonObject ¶meters)
{
string message;
parameters.ToString(message);
LOGINFO("[OnDevice Event], [%s]", message.c_str());
ThresholdLevelCheck();
}
} // namespace Plugin
} // namespace WPEFramework