forked from rdkcentral/rdkservices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXCast.cpp
424 lines (388 loc) · 13.2 KB
/
XCast.cpp
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/**
* If not stated otherwise in this file or this component's LICENSE
* file the following copyright and licenses apply:
*
* Copyright 2020 RDK Management
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
#include "XCast.h"
#include "tracing/Logging.h"
#include "utils.h"
#ifdef RFC_ENABLED
#include "rfcapi.h"
#endif //RFC_ENABLED
#include <syscall.h>
#include <cstring>
#include "RtXcastConnector.h"
using namespace std;
#if defined(HAS_PERSISTENT_IN_HDD)
#define XCAST_SETTING_ENABLED_FILE "/tmp/mnt/diska3/persistent/ds/xcastData"
#else
#define XCAST_SETTING_ENABLED_FILE "/opt/persistent/ds/xcastData"
#endif
#define XCAST_SETTING_ENABLED "xcastEnabled"
// Events
// com.comcast.xcast_1
#define EVT_ON_LAUNCH_REQUEST "onApplicationLaunchRequest"
#define EVT_ON_HIDE_REQUEST "onApplicationHideRequest"
#define EVT_ON_RESUME_REQUEST "onApplicationResumeRequest"
#define EVT_ON_STOP_REQUEST "onApplicationStopRequest"
#define EVT_ON_STATE_REQUEST "onApplicationStateRequest"
//Methods
#define METHOD_ON_APPLICATION_STATE_CHANGED "onApplicationStateChanged"
#define METHOD_GET_API_VERSION_NUMBER "getApiVersionNumber"
#define METHOD_SET_ENABLED "setEnabled"
#define METHOD_GET_ENABLED "getEnabled"
#define LOCATE_CAST_FIRST_TIMEOUT_IN_MILLIS 5000 //5 seconds
#define LOCATE_CAST_SECOND_TIMEOUT_IN_MILLIS 15000 //15 seconds
#define LOCATE_CAST_THIRD_TIMEOUT_IN_MILLIS 30000 //30 seconds
#define LOCATE_CAST_FINAL_TIMEOUT_IN_MILLIS 60000 //60 seconds
namespace WPEFramework {
namespace Plugin {
SERVICE_REGISTRATION(XCast, 1, 0);
static RtXcastConnector * _rtConnector = RtXcastConnector::getInstance();
static int locateCastObjectRetryCount = 0;
bool XCast::isCastEnabled = false;
bool XCast::m_xcastEnableSettings = false;
IARM_Bus_PWRMgr_PowerState_t XCast::m_powerState = IARM_BUS_PWRMGR_POWERSTATE_STANDBY;
XCast::XCast() : AbstractPlugin()
, m_apiVersionNumber(1)
{
InitializeIARM();
m_xcastEnableSettings = XCast::checkXcastSettingsStatus();
LOGINFO("XcastService::m_xcastEnableSettings :%d ",m_xcastEnableSettings);
XCast::checkRFCServiceStatus();
if(XCast::isCastEnabled)
{
registerMethod(METHOD_GET_API_VERSION_NUMBER, &XCast::getApiVersionNumber, this);
registerMethod(METHOD_ON_APPLICATION_STATE_CHANGED , &XCast::applicationStateChanged, this);
registerMethod(METHOD_SET_ENABLED, &XCast::setEnabled, this);
registerMethod(METHOD_GET_ENABLED, &XCast::getEnabled, this);
m_locateCastTimer.connect( bind( &XCast::onLocateCastTimer, this ));
m_locateCastTimer.setSingleShot(true);
}
}
XCast::~XCast()
{
Unregister(METHOD_GET_API_VERSION_NUMBER);
Unregister(METHOD_ON_APPLICATION_STATE_CHANGED);
DeinitializeIARM();
if ( m_locateCastTimer.isActive())
{
m_locateCastTimer.stop();
}
}
const void XCast::InitializeIARM()
{
LOGINFO();
if (Utils::IARM::init())
{
IARM_Result_t res;
IARM_CHECK( IARM_Bus_RegisterEventHandler(IARM_BUS_PWRMGR_NAME,IARM_BUS_PWRMGR_EVENT_MODECHANGED, powerModeChange) );
IARM_Bus_PWRMgr_GetPowerState_Param_t param;
res = IARM_Bus_Call(IARM_BUS_PWRMGR_NAME, IARM_BUS_PWRMGR_API_GetPowerState,
(void *)¶m, sizeof(param));
if (res == IARM_RESULT_SUCCESS)
{
m_powerState = param.curState;
}
LOGINFO("XcastService::m_powerState:%d ",m_powerState);
}
}
void XCast::DeinitializeIARM()
{
LOGINFO();
if (Utils::IARM::isConnected())
{
IARM_Result_t res;
IARM_CHECK( IARM_Bus_UnRegisterEventHandler(IARM_BUS_PWRMGR_NAME,IARM_BUS_PWRMGR_EVENT_MODECHANGED) );
}
}
void XCast::powerModeChange(const char *owner, IARM_EventId_t eventId, void *data, size_t len)
{
LOGINFO();
if (strcmp(owner, IARM_BUS_PWRMGR_NAME) == 0) {
if (eventId == IARM_BUS_PWRMGR_EVENT_MODECHANGED ) {
IARM_Bus_PWRMgr_EventData_t *param = (IARM_Bus_PWRMgr_EventData_t *)data;
LOGINFO("Event IARM_BUS_PWRMGR_EVENT_MODECHANGED: State Changed %d -- > %d\r",
param->data.state.curState, param->data.state.newState);
m_powerState = param->data.state.newState;
if(m_powerState == IARM_BUS_PWRMGR_POWERSTATE_ON && m_xcastEnableSettings)
_rtConnector->enableCastService(true);
else
_rtConnector->enableCastService(false);
}
}
}
const string XCast::Initialize(PluginHost::IShell* /* service */)
{
LOGINFO("Activate plugin.");
_rtConnector = RtXcastConnector::getInstance();
_rtConnector->setService(this);
if (XCast::isCastEnabled)
{
//TODO add rt intialization.
if( _rtConnector->initialize())
{
//We give few seconds delay before the timer is fired.
m_locateCastTimer.start(LOCATE_CAST_FIRST_TIMEOUT_IN_MILLIS);
}
}
else
{
LOGINFO(" Cast service is disabled. Not initializing");
}
// On success return empty, to indicate there is no error text.
return (string());
}
void XCast::Deinitialize(PluginHost::IShell* /* service */)
{
if( XCast::isCastEnabled){
_rtConnector->shutdown();
}
}
string XCast::Information() const
{
// No additional info to report.
return (string());
}
uint32_t XCast::getApiVersionNumber(const JsonObject& parameters, JsonObject& response)
{
LOGINFOMETHOD();
response["version"] = m_apiVersionNumber;
returnResponse(true);
}
uint32_t XCast::applicationStateChanged(const JsonObject& parameters, JsonObject& response)
{
LOGINFO("XcastService::ApplicationStateChanged () ");
string app,id,state,error;
getStringParameter("applicationName",app);
getStringParameter("state", state);
if (parameters.HasLabel("applicationId"))
{
getStringParameter("applicationId", id);
}
if (parameters.HasLabel("error"))
{
getStringParameter("error", error);
}
if(!app.empty() && !state.empty())
{
if (app == "NetflixApp")
app = "Netflix";
LOGINFO("XcastService::ApplicationStateChanged ARGS = %s : %s : %s : %s ", app.c_str(), id.c_str() , state.c_str() , error.c_str());
_rtConnector->applicationStateChanged(app, state, id, error);
returnResponse(true);
}//app && state not empty
else{
returnResponse(false);
}
}
uint32_t XCast::setEnabled(const JsonObject& parameters, JsonObject& response)
{
LOGINFO("XcastService::setEnabled ");
bool enabled = false;
if (parameters.HasLabel("enabled"))
{
getBoolParameter("enabled", enabled);
}
else
{
returnResponse(false);
}
//persist value
m_xcastEnableSettings = enabled;
persistEnabledSettings(enabled);
//apply settings
if (enabled && m_powerState == IARM_BUS_PWRMGR_POWERSTATE_ON)
_rtConnector->enableCastService(true);
else
_rtConnector->enableCastService(false);
returnResponse(true);
}
uint32_t XCast::getEnabled(const JsonObject& parameters, JsonObject& response)
{
LOGINFO("XcastService::getEnabled ");
response["enabled"] = m_xcastEnableSettings;
returnResponse(true);
}
void XCast::persistEnabledSettings(bool enableStatus)
{
Core::File file;
file = XCAST_SETTING_ENABLED_FILE;
file.Open(false);
if (!file.IsOpen())
file.Create();
JsonObject enableSetting;
enableSetting.IElement::FromFile(file);
file.Destroy();
file.Create();
enableSetting[XCAST_SETTING_ENABLED] = enableStatus;
enableSetting.IElement::ToFile(file);
file.Close();
return;
}
//Timer Functions
void XCast::onLocateCastTimer()
{
int status = _rtConnector->connectToRemoteService();
if(status != 0)
{
locateCastObjectRetryCount++;
if(locateCastObjectRetryCount == 1)
{
LOGINFO("Retry after 5 sec...");
m_locateCastTimer.start(LOCATE_CAST_FIRST_TIMEOUT_IN_MILLIS);
}
if(locateCastObjectRetryCount == 2)
{
LOGINFO("Retry after 15 sec...");
m_locateCastTimer.start(LOCATE_CAST_SECOND_TIMEOUT_IN_MILLIS);
}
if(locateCastObjectRetryCount == 3)
{
LOGINFO("Retry after 30 sec...");
m_locateCastTimer.start(LOCATE_CAST_THIRD_TIMEOUT_IN_MILLIS);
}
if(locateCastObjectRetryCount == 4)
{
LOGINFO("Retry after 60 sec...");
m_locateCastTimer.start(LOCATE_CAST_FINAL_TIMEOUT_IN_MILLIS);
}
return ;
}// err != RT_OK
locateCastObjectRetryCount = 0;
m_locateCastTimer.stop();
if (m_xcastEnableSettings && m_powerState == IARM_BUS_PWRMGR_POWERSTATE_ON)
_rtConnector->enableCastService(true);
else
_rtConnector->enableCastService(false);
LOGINFO("XCast::onLocateCastTimer : Timer still active ? %d ",m_locateCastTimer.isActive());
}
void XCast::onRtServiceDisconnected()
{
LOGINFO("RT communication failure. Reconnecting.. ");
m_locateCastTimer.start(LOCATE_CAST_FIRST_TIMEOUT_IN_MILLIS);
}
void XCast::onXcastApplicationLaunchRequest(string appName, string parameter)
{
//TODO
LOGINFO ("XcastService::onXcastApplicationLaunchRequest ");
JsonObject params;
JsonObject urlParam;
if (appName == "NetflixApp")
urlParam["pluginUrl"]=parameter;
else
urlParam["url"]=parameter;
params["applicationName"]= appName;
params["parameters"]= urlParam;
sendNotify(EVT_ON_LAUNCH_REQUEST, params);
}
void XCast::onXcastApplicationStopRequest(string appName, string appID)
{
//TODO
LOGINFO("XcastService::onXcastApplicationStopRequest ");
JsonObject params;
params["applicationName"] = appName;
params["applicationId"]= appID;
sendNotify(EVT_ON_STOP_REQUEST, params);
}
void XCast::onXcastApplicationHideRequest(string appName, string appID)
{
LOGINFO("XcastService::onXcastApplicationHideRequest : ");
if (appName.compare("Netflix") == 0 )
appName = "NetflixApp";
JsonObject params;
params["applicationName"] = appName;
params["applicationId"]= appID;
sendNotify(EVT_ON_HIDE_REQUEST, params);
}
void XCast::onXcastApplicationStateRequest(string appName, string appID)
{
LOGINFO("XcastService::onXcastApplicationStateRequest: ");
if (appName.compare("Netflix") == 0 )
appName = "NetflixApp";
JsonObject params;
params["applicationName"] = appName;
params["applicationId"]= appID;
sendNotify(EVT_ON_STATE_REQUEST , params);
}
void XCast::onXcastApplicationResumeRequest(string appName, string appID)
{
LOGINFO("XcastService::onXcastApplicationResumeRequest ");
if (appName.compare("Netflix") == 0 )
appName = "NetflixApp";
JsonObject params;
params["applicationName"] = appName;
params["applicationId"]= appID;
sendNotify(EVT_ON_RESUME_REQUEST, params);
}
bool XCast::checkRFCServiceStatus()
{
LOGINFO();
#ifdef RFC_ENABLED
RFC_ParamData_t param;
WDMP_STATUS wdmpStatus = getRFCParameter(const_cast<char *>("Xcast"), "Device.DeviceInfo.X_RDKCENTRAL-COM_RFC.Feature.XDial.Enable", ¶m);
if (wdmpStatus == WDMP_SUCCESS || wdmpStatus == WDMP_ERR_DEFAULT_VALUE)
{
if( param.type == WDMP_BOOLEAN )
{
if(strncasecmp(param.value,"true",4) == 0 )
XCast::isCastEnabled = true;
}
}
LOGINFO(" Is cast enabled ? %d , call value %d ", isCastEnabled, wdmpStatus);
#else
XCast::isCastEnabled = true;;
#endif //RFC_ENABLED
return XCast::isCastEnabled;
}
bool XCast::checkXcastSettingsStatus()
{
LOGINFO();
Core::File file;
JsonObject parameters;
bool xcastEnableStatus = false;
file = XCAST_SETTING_ENABLED_FILE;
file.Open(false);
if (!file.IsOpen())
{
LOGINFO("XcastService::persistance file not present create with default setting true");
file.Create();
JsonObject parameters;
parameters[XCAST_SETTING_ENABLED] = true;
xcastEnableStatus = true;
parameters.IElement::ToFile(file);
}
else
{
parameters.IElement::FromFile(file);
if( parameters.HasLabel(XCAST_SETTING_ENABLED))
{
getBoolParameter(XCAST_SETTING_ENABLED, xcastEnableStatus);
LOGINFO("XcastService:: xcastEnableStatus :%d",xcastEnableStatus);
}
else
{
LOGINFO("XcastService:: XCAST_SETTING_ENABLED not present create with default setting true");
parameters[XCAST_SETTING_ENABLED] = true;
xcastEnableStatus = true;
parameters.IElement::ToFile(file);
}
}
file.Close();
return xcastEnableStatus;
}
} // namespace Plugin
} // namespace WPEFramework