forked from rdkcmf/rdk-aamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThunderAccess.cpp
220 lines (203 loc) · 7.26 KB
/
ThunderAccess.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
/*
* If not stated otherwise in this file or this component's license file the
* following copyright and licenses apply:
*
* Copyright 2018 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.
*/
/**
* @file ThunderAccess.cpp
* @brief wrapper class for accessing thunder plugins
*/
#include "priv_aamp.h"
#include <core/core.h>
#include <websocket/websocket.h>
#include <securityagent/SecurityTokenUtil.h>
#include <ThunderAccess.h>
using namespace std;
using namespace WPEFramework;
#define SERVER_DETAILS "127.0.0.1:9998"
#define MAX_LENGTH 1024
/**
* @brief ThunderAccessAAMP constructor
* @note Security tocken accusition, controller object creation
* @param NA
* @retval NA
* @retval NA
*/
ThunderAccessAAMP::ThunderAccessAAMP(std::string callsign)
: remoteObject(NULL),
controllerObject(NULL),
query(""),
pluginCallsign(callsign)
{
AAMPLOG_INFO( "[ThunderAccessAAMP]Inside %s ", __FUNCTION__ );
int ret = 0;
uint32_t status = Core::ERROR_NONE;
unsigned char buffer[MAX_LENGTH] = {0};
Core::SystemInfo::SetEnvironment(_T("THUNDER_ACCESS"), (_T(SERVER_DETAILS)));
ret = GetSecurityToken(MAX_LENGTH,buffer);
if(ret > 0){
AAMPLOG_INFO( "[ThunderAccessAAMP] %s : GetSecurityToken success", __FUNCTION__ );
}
else{
AAMPLOG_WARN( "[ThunderAccessAAMP] %s : GetSecurityToken failed", __FUNCTION__ );
}
if (NULL == controllerObject) {
string sToken = (char*)buffer;
query = "token=" + sToken;
/*Passing empty string instead of Controller callsign.This is assumed as controller plugin.*/
//controllerObject = new JSONRPC::LinkType<Core::JSON::IElement>(_T(""), _T(""), false, query);
/*To Do: Need to switch to above line once auth token is required*/
controllerObject = new JSONRPC::LinkType<Core::JSON::IElement>(_T(""));
if (NULL == controllerObject) {
AAMPLOG_WARN( "[ThunderAccessAAMP] %s : Controller object creation failed", __FUNCTION__ );
} else {
AAMPLOG_INFO( "[ThunderAccessAAMP] %s : Controller object creation success", __FUNCTION__ );
}
}
//remoteObject = new JSONRPC::LinkType<Core::JSON::IElement>(_T(pluginCallsign), _T(""), false, query);
/*To Do: Need to switch to above line once auth token is required*/
remoteObject = new JSONRPC::LinkType<Core::JSON::IElement>(_T(pluginCallsign), _T(""));
if (NULL == remoteObject) {
AAMPLOG_WARN( "[ThunderAccessAAMP] %s : %s Client initialization failed", __FUNCTION__, pluginCallsign.c_str());
ret = false;
} else {
AAMPLOG_INFO( "[ThunderAccessAAMP] %s : %s Client initialization success", __FUNCTION__, pluginCallsign.c_str());
}
}
/**
* @brief ThunderAccessAAMP destructor
* @note clean up
* @param NA
* @retval NA
* @retval NA
*/
ThunderAccessAAMP::~ThunderAccessAAMP()
{
if(NULL != controllerObject)
{
delete controllerObject;
controllerObject = NULL;
}
if(NULL != remoteObject)
{
delete remoteObject;
remoteObject = NULL;
}
}
/**
* @brief ActivatePlugin
* @note Plugin activation and Remote object creation
* @param Plugin Callsign
* @retval true on success
* @retval false on failure
*/
bool ThunderAccessAAMP::ActivatePlugin()
{
bool ret = true;
JsonObject result;
JsonObject controlParam;
std::string response;
if (NULL != controllerObject) {
controlParam["callsign"] = pluginCallsign;
if (Core::ERROR_NONE == controllerObject->Invoke<JsonObject, JsonObject>(1000, _T("activate"), controlParam, result)){
result.ToString(response);
AAMPLOG_INFO( "[ThunderAccessAAMP] %s plugin Activated. Response : %s ", pluginCallsign.c_str(), response.c_str());
}
} else {
AAMPLOG_WARN( "[ThunderAccessAAMP] %s : Controller Object NULL ", __FUNCTION__);
ret = false;
}
return ret;
}
/**
* @brief subscribeEvent
* @note Subscribe event data for the specific plugin
* @param Event name, Event handler
* @retval true on success
* @retval false on failure
*/
/*To Do: Only JSON Object can be used as parameter now*/
bool ThunderAccessAAMP::SubscribeEvent (string eventName, std::function<void(const WPEFramework::Core::JSON::VariantContainer&)> functionHandler)
{
bool ret = true;
if (NULL != remoteObject) {
if (remoteObject->Subscribe<JsonObject>(1000, _T(eventName), functionHandler) == Core::ERROR_NONE) {
AAMPLOG_INFO( "[ThunderAccessAAMP] %s : Subscribed to : %s", __FUNCTION__, eventName.c_str());
} else {
AAMPLOG_WARN( "[ThunderAccessAAMP] %s : Failed to Subscribe notification handler for : %s", __FUNCTION__, eventName.c_str());
}
} else {
AAMPLOG_WARN( "[ThunderAccessAAMP] %s : remoteObject not created for the plugin!", __FUNCTION__ );
ret = false;
}
return ret;
}
/**
* @brief unSubscribeEvent
* @note unSubscribe event data for the specific plugin
* @param Event name
* @retval true on success
* @retval false on failure
*/
/*To Do: Only JSON Object can be used as parameter now*/
bool ThunderAccessAAMP::UnSubscribeEvent (string eventName)
{
bool ret = true;
if (NULL != remoteObject) {
remoteObject->Unsubscribe(1000, _T(eventName));
AAMPLOG_INFO( "[ThunderAccessAAMP] %s : UnSubscribed : %s event", __FUNCTION__, eventName.c_str());
} else {
AAMPLOG_WARN( "[ThunderAccessAAMP] %s : remoteObject not created for the plugin!", __FUNCTION__ );
ret = false;
}
return ret;
}
/**
* @brief invokeJSONRPC
* @note Invoke JSONRPC call for the plugin
* @param Plugin method, reference to input pram and result
* @retval true on success
* @retval false on failure
*/
bool ThunderAccessAAMP::InvokeJSONRPC(std::string method, const JsonObject ¶m, JsonObject &result)
{
bool ret = true;
std::string response;
if(NULL == remoteObject)
{
AAMPLOG_WARN( "[ThunderAccessAAMP] %s : client not initialized! ", __FUNCTION__ );
return false;
}
if (Core::ERROR_NONE == remoteObject->Invoke<JsonObject, JsonObject>(1000, _T(method), param, result))
{
if (result["success"].Boolean()) {
result.ToString(response);
AAMPLOG_INFO( "[ThunderAccessAAMP] %s success! Response : %s", method.c_str() , response.c_str());
}
else
{
result.ToString(response);
AAMPLOG_WARN( "[ThunderAccessAAMP] %s call failed! Response : %s", method.c_str() , response.c_str());
ret = false;
}
}
else
{
AAMPLOG_WARN( "[ThunderAccessAAMP] %s : invoke failed! ", method.c_str());
ret = false;
}
return ret;
}