RDK Documentation (Open Sourced RDK Components)
ThunderAccess.cpp
Go to the documentation of this file.
1 /*
2  * If not stated otherwise in this file or this component's license file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2018 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 /**
21  * @file ThunderAccess.cpp
22  * @brief wrapper class for accessing thunder plugins
23  */
24 #include "Module.h"
25 #include "priv_aamp.h"
26 #include <core/core.h>
27 #include <websocket/websocket.h>
28 #include <securityagent/SecurityTokenUtil.h>
29 #include <ThunderAccess.h>
30 
31 using namespace std;
32 using namespace WPEFramework;
33 
34 #define SERVER_DETAILS "127.0.0.1:9998"
35 #define MAX_LENGTH 1024
36 
37 /**
38  * @brief Structure to save the Thunder security token details
39  **/
40 typedef struct ThunderSecurity
41 {
42  std::string securityToken;
43  int tokenStatus;
44  bool tokenQueried;
46 
47 ThunderSecurityData gSecurityData;
48 
49 
50 /**
51  * @brief ThunderAccessAAMP constructor
52  */
54  : remoteObject(NULL),
55  controllerObject(NULL),
56  pluginCallsign(callsign),
57  mLogObj(logObj)
58 {
59  AAMPLOG_INFO( "[ThunderAccessAAMP]Inside");
60 
61  uint32_t status = Core::ERROR_NONE;
62 
63  Core::SystemInfo::SetEnvironment(_T("THUNDER_ACCESS"), (_T(SERVER_DETAILS)));
64 
65  if(!gSecurityData.tokenQueried)
66  {
67  unsigned char buffer[MAX_LENGTH] = {0};
68  gSecurityData.tokenStatus = GetSecurityToken(MAX_LENGTH,buffer);
69  if(gSecurityData.tokenStatus > 0){
70  AAMPLOG_INFO( "[ThunderAccessAAMP] : GetSecurityToken success");
71  string sToken = (char*)buffer;
72  gSecurityData.securityToken = "token=" + sToken;
73  }
74  gSecurityData.tokenQueried = true;
75 
76  //AAMPLOG_WARN( "[ThunderAccessAAMP] securityToken : %s tokenStatus : %d tokenQueried : %s", gSecurityData.securityToken.c_str(), gSecurityData.tokenStatus, ((gSecurityData.tokenQueried)?"true":"false"));
77  }
78 
79  if (NULL == controllerObject) {
80  /*Passing empty string instead of Controller callsign.This is assumed as controller plugin.*/
81  if(gSecurityData.tokenStatus > 0){
82  controllerObject = new JSONRPC::LinkType<Core::JSON::IElement>(_T(""), _T(""), false, gSecurityData.securityToken);
83  }
84  else{
85  controllerObject = new JSONRPC::LinkType<Core::JSON::IElement>(_T(""));
86  }
87 
88  if (NULL == controllerObject) {
89  AAMPLOG_WARN( "[ThunderAccessAAMP] Controller object creation failed");
90  } else {
91  AAMPLOG_INFO( "[ThunderAccessAAMP] Controller object creation success");
92  }
93  }
94 
95  if(gSecurityData.tokenStatus > 0){
96  remoteObject = new JSONRPC::LinkType<Core::JSON::IElement>(_T(pluginCallsign), _T(""), false, gSecurityData.securityToken);
97  }
98  else{
99  remoteObject = new JSONRPC::LinkType<Core::JSON::IElement>(_T(pluginCallsign), _T(""));
100  }
101  if (NULL == remoteObject) {
102  AAMPLOG_WARN( "[ThunderAccessAAMP] %s Client initialization failed", pluginCallsign.c_str());
103  } else {
104  AAMPLOG_INFO( "[ThunderAccessAAMP] %s Client initialization success", pluginCallsign.c_str());
105  }
106 }
107 
108 /**
109  * @brief ThunderAccessAAMP destructor
110  */
112 {
113  SAFE_DELETE(controllerObject);
114  SAFE_DELETE(remoteObject);
115 }
116 
117 /**
118  * @brief ActivatePlugin
119  */
121 {
122  bool ret = true;
123  JsonObject result;
124  JsonObject controlParam;
125  std::string response;
126  uint32_t status = Core::ERROR_NONE;
127 
128  if (NULL != controllerObject) {
129  controlParam["callsign"] = pluginCallsign;
130  status = controllerObject->Invoke<JsonObject, JsonObject>(THUNDER_RPC_TIMEOUT, _T("activate"), controlParam, result);
131  if (Core::ERROR_NONE == status){
132  result.ToString(response);
133  AAMPLOG_INFO( "[ThunderAccessAAMP] %s plugin Activated. Response : %s ", pluginCallsign.c_str(), response.c_str());
134  }
135  else
136  {
137  AAMPLOG_WARN( "[ThunderAccessAAMP] %s plugin Activation failed with error status : %u ", pluginCallsign.c_str(), status);
138  ret = false;
139  }
140  } else {
141  AAMPLOG_WARN( "[ThunderAccessAAMP] Controller Object NULL ");
142  ret = false;
143  }
144 
145  return ret;
146 }
147 
148 
149 /*To Do: Only JSON Object can be used as parameter now*/
150 /**
151  * @brief subscribeEvent
152  */
153 bool ThunderAccessAAMP::SubscribeEvent (string eventName, std::function<void(const WPEFramework::Core::JSON::VariantContainer&)> functionHandler)
154 {
155  bool ret = true;
156  uint32_t status = Core::ERROR_NONE;
157  if (NULL != remoteObject) {
158  status = remoteObject->Subscribe<JsonObject>(THUNDER_RPC_TIMEOUT, _T(eventName), functionHandler);
159  if (Core::ERROR_NONE == status) {
160  AAMPLOG_INFO( "[ThunderAccessAAMP] Subscribed to : %s", eventName.c_str());
161  } else {
162  AAMPLOG_WARN( "[ThunderAccessAAMP] Subscription failed for : %s with error status %u", eventName.c_str(), status);
163  ret = false;
164  }
165  } else {
166  AAMPLOG_WARN( "[ThunderAccessAAMP] remoteObject not created for the plugin!");
167  ret = false;
168  }
169  return ret;
170 }
171 
172 
173 /*To Do: Only JSON Object can be used as parameter now*/
174 
175 /**
176  * @brief unSubscribeEvent
177  */
178 bool ThunderAccessAAMP::UnSubscribeEvent (string eventName)
179 {
180  bool ret = true;
181  if (NULL != remoteObject) {
182  remoteObject->Unsubscribe(THUNDER_RPC_TIMEOUT, _T(eventName));
183  AAMPLOG_INFO( "[ThunderAccessAAMP] UnSubscribed : %s event", eventName.c_str());
184  } else {
185  AAMPLOG_WARN( "[ThunderAccessAAMP] remoteObject not created for the plugin!");
186  ret = false;
187  }
188  return ret;
189 }
190 
191 
192 /**
193  * @brief invokeJSONRPC
194  * @note Invoke JSONRPC call for the plugin
195  */
196 bool ThunderAccessAAMP::InvokeJSONRPC(std::string method, const JsonObject &param, JsonObject &result, const uint32_t waitTime)
197 {
198  bool ret = true;
199  std::string response;
200  uint32_t status = Core::ERROR_NONE;
201 
202  if(NULL == remoteObject)
203  {
204  AAMPLOG_WARN( "[ThunderAccessAAMP] client not initialized! ");
205  return false;
206  }
207 
208  JsonObject result_internal;
209  status = remoteObject->Invoke<JsonObject, JsonObject>(waitTime, _T(method), param, result_internal);
210  if (Core::ERROR_NONE == status)
211  {
212  if (result_internal["success"].Boolean()) {
213  result_internal.ToString(response);
214  AAMPLOG_TRACE( "[ThunderAccessAAMP] %s success! Response : %s", method.c_str() , response.c_str());
215  } else {
216  result_internal.ToString(response);
217  AAMPLOG_WARN( "[ThunderAccessAAMP] %s call failed! Response : %s", method.c_str() , response.c_str());
218  ret = false;
219  }
220  } else {
221  AAMPLOG_WARN( "[ThunderAccessAAMP] %s : invoke failed with error status %u", method.c_str(), status);
222  ret = false;
223  }
224 
225  result = result_internal;
226  return ret;
227 }
ThunderAccess.h
shim for dispatching UVE HDMI input playback
ThunderAccessAAMP::SubscribeEvent
bool SubscribeEvent(string eventName, std::function< void(const WPEFramework::Core::JSON::VariantContainer &)> functionHandler)
subscribeEvent
Definition: ThunderAccess.cpp:153
ThunderAccessAAMP::remoteObject
JSONRPC::LinkType< Core::JSON::IElement > * remoteObject
Definition: ThunderAccess.h:104
AampLogManager
AampLogManager Class.
Definition: AampLogManager.h:150
ThunderAccessAAMP::ThunderAccessAAMP
ThunderAccessAAMP(std::string callsign, AampLogManager *logObj=NULL)
ThunderAccessAAMP constructor.
Definition: ThunderAccess.cpp:53
ThunderAccessAAMP::UnSubscribeEvent
bool UnSubscribeEvent(string eventName)
unSubscribeEvent
Definition: ThunderAccess.cpp:178
ThunderAccessAAMP::InvokeJSONRPC
bool InvokeJSONRPC(std::string method, const JsonObject &param, JsonObject &result, const uint32_t waitTime=5000)
invokeJSONRPC
Definition: ThunderAccess.cpp:196
priv_aamp.h
Private functions and types used internally by AAMP.
ThunderSecurity
Structure to save the Thunder security token details.
Definition: ThunderAccess.cpp:40
AAMPLOG_TRACE
#define AAMPLOG_TRACE(FORMAT,...)
AAMP logging defines, this can be enabled through setLogLevel() as per the need.
Definition: AampLogManager.h:83
ThunderAccessAAMP::~ThunderAccessAAMP
~ThunderAccessAAMP()
ThunderAccessAAMP destructor.
Definition: ThunderAccess.cpp:111
ThunderAccessAAMP::ActivatePlugin
bool ActivatePlugin()
ActivatePlugin.
Definition: ThunderAccess.cpp:120
Module.h
Declaration of module name aamp.
ThunderSecurityData
struct ThunderSecurity ThunderSecurityData
Structure to save the Thunder security token details.