-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThunderAccess.cpp
227 lines (199 loc) · 7.28 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
221
222
223
224
225
226
227
/*
* 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 "Module.h"
#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 Structure to save the Thunder security token details
**/
typedef struct ThunderSecurity
{
std::string securityToken;
int tokenStatus;
bool tokenQueried;
}ThunderSecurityData;
ThunderSecurityData gSecurityData;
/**
* @brief ThunderAccessAAMP constructor
*/
ThunderAccessAAMP::ThunderAccessAAMP(std::string callsign, AampLogManager *logObj)
: remoteObject(NULL),
controllerObject(NULL),
pluginCallsign(callsign),
mLogObj(logObj)
{
AAMPLOG_INFO( "[ThunderAccessAAMP]Inside");
uint32_t status = Core::ERROR_NONE;
Core::SystemInfo::SetEnvironment(_T("THUNDER_ACCESS"), (_T(SERVER_DETAILS)));
if(!gSecurityData.tokenQueried)
{
unsigned char buffer[MAX_LENGTH] = {0};
gSecurityData.tokenStatus = GetSecurityToken(MAX_LENGTH,buffer);
if(gSecurityData.tokenStatus > 0){
AAMPLOG_INFO( "[ThunderAccessAAMP] : GetSecurityToken success");
string sToken = (char*)buffer;
gSecurityData.securityToken = "token=" + sToken;
}
gSecurityData.tokenQueried = true;
//AAMPLOG_WARN( "[ThunderAccessAAMP] securityToken : %s tokenStatus : %d tokenQueried : %s", gSecurityData.securityToken.c_str(), gSecurityData.tokenStatus, ((gSecurityData.tokenQueried)?"true":"false"));
}
if (NULL == controllerObject) {
/*Passing empty string instead of Controller callsign.This is assumed as controller plugin.*/
if(gSecurityData.tokenStatus > 0){
controllerObject = new JSONRPC::LinkType<Core::JSON::IElement>(_T(""), _T(""), false, gSecurityData.securityToken);
}
else{
controllerObject = new JSONRPC::LinkType<Core::JSON::IElement>(_T(""));
}
if (NULL == controllerObject) {
AAMPLOG_WARN( "[ThunderAccessAAMP] Controller object creation failed");
} else {
AAMPLOG_INFO( "[ThunderAccessAAMP] Controller object creation success");
}
}
if(gSecurityData.tokenStatus > 0){
remoteObject = new JSONRPC::LinkType<Core::JSON::IElement>(_T(pluginCallsign), _T(""), false, gSecurityData.securityToken);
}
else{
remoteObject = new JSONRPC::LinkType<Core::JSON::IElement>(_T(pluginCallsign), _T(""));
}
if (NULL == remoteObject) {
AAMPLOG_WARN( "[ThunderAccessAAMP] %s Client initialization failed", pluginCallsign.c_str());
} else {
AAMPLOG_INFO( "[ThunderAccessAAMP] %s Client initialization success", pluginCallsign.c_str());
}
}
/**
* @brief ThunderAccessAAMP destructor
*/
ThunderAccessAAMP::~ThunderAccessAAMP()
{
SAFE_DELETE(controllerObject);
SAFE_DELETE(remoteObject);
}
/**
* @brief ActivatePlugin
*/
bool ThunderAccessAAMP::ActivatePlugin()
{
bool ret = true;
JsonObject result;
JsonObject controlParam;
std::string response;
uint32_t status = Core::ERROR_NONE;
if (NULL != controllerObject) {
controlParam["callsign"] = pluginCallsign;
status = controllerObject->Invoke<JsonObject, JsonObject>(THUNDER_RPC_TIMEOUT, _T("activate"), controlParam, result);
if (Core::ERROR_NONE == status){
result.ToString(response);
AAMPLOG_INFO( "[ThunderAccessAAMP] %s plugin Activated. Response : %s ", pluginCallsign.c_str(), response.c_str());
}
else
{
AAMPLOG_WARN( "[ThunderAccessAAMP] %s plugin Activation failed with error status : %u ", pluginCallsign.c_str(), status);
ret = false;
}
} else {
AAMPLOG_WARN( "[ThunderAccessAAMP] Controller Object NULL ");
ret = false;
}
return ret;
}
/*To Do: Only JSON Object can be used as parameter now*/
/**
* @brief subscribeEvent
*/
bool ThunderAccessAAMP::SubscribeEvent (string eventName, std::function<void(const WPEFramework::Core::JSON::VariantContainer&)> functionHandler)
{
bool ret = true;
uint32_t status = Core::ERROR_NONE;
if (NULL != remoteObject) {
status = remoteObject->Subscribe<JsonObject>(THUNDER_RPC_TIMEOUT, _T(eventName), functionHandler);
if (Core::ERROR_NONE == status) {
AAMPLOG_INFO( "[ThunderAccessAAMP] Subscribed to : %s", eventName.c_str());
} else {
AAMPLOG_WARN( "[ThunderAccessAAMP] Subscription failed for : %s with error status %u", eventName.c_str(), status);
ret = false;
}
} else {
AAMPLOG_WARN( "[ThunderAccessAAMP] remoteObject not created for the plugin!");
ret = false;
}
return ret;
}
/*To Do: Only JSON Object can be used as parameter now*/
/**
* @brief unSubscribeEvent
*/
bool ThunderAccessAAMP::UnSubscribeEvent (string eventName)
{
bool ret = true;
if (NULL != remoteObject) {
remoteObject->Unsubscribe(THUNDER_RPC_TIMEOUT, _T(eventName));
AAMPLOG_INFO( "[ThunderAccessAAMP] UnSubscribed : %s event", eventName.c_str());
} else {
AAMPLOG_WARN( "[ThunderAccessAAMP] remoteObject not created for the plugin!");
ret = false;
}
return ret;
}
/**
* @brief invokeJSONRPC
* @note Invoke JSONRPC call for the plugin
*/
bool ThunderAccessAAMP::InvokeJSONRPC(std::string method, const JsonObject ¶m, JsonObject &result, const uint32_t waitTime)
{
bool ret = true;
std::string response;
uint32_t status = Core::ERROR_NONE;
if(NULL == remoteObject)
{
AAMPLOG_WARN( "[ThunderAccessAAMP] client not initialized! ");
return false;
}
JsonObject result_internal;
status = remoteObject->Invoke<JsonObject, JsonObject>(waitTime, _T(method), param, result_internal);
if (Core::ERROR_NONE == status)
{
if (result_internal["success"].Boolean()) {
result_internal.ToString(response);
AAMPLOG_TRACE( "[ThunderAccessAAMP] %s success! Response : %s", method.c_str() , response.c_str());
} else {
result_internal.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 with error status %u", method.c_str(), status);
ret = false;
}
result = result_internal;
return ret;
}