|
| 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 "priv_aamp.h" |
| 25 | +#include <core/core.h> |
| 26 | +#include <websocket/websocket.h> |
| 27 | +#include <securityagent/SecurityTokenUtil.h> |
| 28 | +#include <ThunderAccess.h> |
| 29 | + |
| 30 | +using namespace std; |
| 31 | +using namespace WPEFramework; |
| 32 | + |
| 33 | +#define SERVER_DETAILS "127.0.0.1:9998" |
| 34 | +#define MAX_LENGTH 1024 |
| 35 | + |
| 36 | +/** |
| 37 | + * @brief ThunderAccessAAMP constructor |
| 38 | + * @note Security tocken accusition, controller object creation |
| 39 | + * @param NA |
| 40 | + * @retval NA |
| 41 | + * @retval NA |
| 42 | + */ |
| 43 | +ThunderAccessAAMP::ThunderAccessAAMP(std::string callsign) |
| 44 | + : remoteObject(NULL), |
| 45 | + controllerObject(NULL), |
| 46 | + query(""), |
| 47 | + pluginCallsign(callsign) |
| 48 | +{ |
| 49 | + AAMPLOG_INFO( "[ThunderAccessAAMP]Inside %s ", __FUNCTION__ ); |
| 50 | + |
| 51 | + int ret = 0; |
| 52 | + uint32_t status = Core::ERROR_NONE; |
| 53 | + unsigned char buffer[MAX_LENGTH] = {0}; |
| 54 | + |
| 55 | + Core::SystemInfo::SetEnvironment(_T("THUNDER_ACCESS"), (_T(SERVER_DETAILS))); |
| 56 | + |
| 57 | + ret = GetSecurityToken(MAX_LENGTH,buffer); |
| 58 | + if(ret > 0){ |
| 59 | + AAMPLOG_INFO( "[ThunderAccessAAMP] %s : GetSecurityToken success", __FUNCTION__ ); |
| 60 | + } |
| 61 | + else{ |
| 62 | + AAMPLOG_WARN( "[ThunderAccessAAMP] %s : GetSecurityToken failed", __FUNCTION__ ); |
| 63 | + } |
| 64 | + |
| 65 | + if (NULL == controllerObject) { |
| 66 | + string sToken = (char*)buffer; |
| 67 | + query = "token=" + sToken; |
| 68 | + |
| 69 | + /*Passing empty string instead of Controller callsign.This is assumed as controller plugin.*/ |
| 70 | + //controllerObject = new JSONRPC::LinkType<Core::JSON::IElement>(_T(""), _T(""), false, query); |
| 71 | + /*To Do: Need to switch to above line once auth token is required*/ |
| 72 | + controllerObject = new JSONRPC::LinkType<Core::JSON::IElement>(_T("")); |
| 73 | + if (NULL == controllerObject) { |
| 74 | + AAMPLOG_WARN( "[ThunderAccessAAMP] %s : Controller object creation failed", __FUNCTION__ ); |
| 75 | + } else { |
| 76 | + AAMPLOG_INFO( "[ThunderAccessAAMP] %s : Controller object creation success", __FUNCTION__ ); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + //remoteObject = new JSONRPC::LinkType<Core::JSON::IElement>(_T(pluginCallsign), _T(""), false, query); |
| 81 | + /*To Do: Need to switch to above line once auth token is required*/ |
| 82 | + remoteObject = new JSONRPC::LinkType<Core::JSON::IElement>(_T(pluginCallsign), _T("")); |
| 83 | + if (NULL == remoteObject) { |
| 84 | + AAMPLOG_WARN( "[ThunderAccessAAMP] %s : %s Client initialization failed", __FUNCTION__, pluginCallsign.c_str()); |
| 85 | + ret = false; |
| 86 | + } else { |
| 87 | + AAMPLOG_INFO( "[ThunderAccessAAMP] %s : %s Client initialization success", __FUNCTION__, pluginCallsign.c_str()); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * @brief ThunderAccessAAMP destructor |
| 93 | + * @note clean up |
| 94 | + * @param NA |
| 95 | + * @retval NA |
| 96 | + * @retval NA |
| 97 | + */ |
| 98 | +ThunderAccessAAMP::~ThunderAccessAAMP() |
| 99 | +{ |
| 100 | + if(NULL != controllerObject) |
| 101 | + { |
| 102 | + delete controllerObject; |
| 103 | + controllerObject = NULL; |
| 104 | + } |
| 105 | + if(NULL != remoteObject) |
| 106 | + { |
| 107 | + delete remoteObject; |
| 108 | + remoteObject = NULL; |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * @brief ActivatePlugin |
| 114 | + * @note Plugin activation and Remote object creation |
| 115 | + * @param Plugin Callsign |
| 116 | + * @retval true on success |
| 117 | + * @retval false on failure |
| 118 | + */ |
| 119 | +bool ThunderAccessAAMP::ActivatePlugin() |
| 120 | +{ |
| 121 | + bool ret = true; |
| 122 | + JsonObject result; |
| 123 | + JsonObject controlParam; |
| 124 | + std::string response; |
| 125 | + |
| 126 | + if (NULL != controllerObject) { |
| 127 | + controlParam["callsign"] = pluginCallsign; |
| 128 | + if (Core::ERROR_NONE == controllerObject->Invoke<JsonObject, JsonObject>(1000, _T("activate"), controlParam, result)){ |
| 129 | + result.ToString(response); |
| 130 | + AAMPLOG_INFO( "[ThunderAccessAAMP] %s plugin Activated. Response : %s ", pluginCallsign.c_str(), response.c_str()); |
| 131 | + } |
| 132 | + } else { |
| 133 | + AAMPLOG_WARN( "[ThunderAccessAAMP] %s : Controller Object NULL ", __FUNCTION__); |
| 134 | + ret = false; |
| 135 | + } |
| 136 | + |
| 137 | + return ret; |
| 138 | +} |
| 139 | + |
| 140 | +/** |
| 141 | + * @brief subscribeEvent |
| 142 | + * @note Subscribe event data for the specific plugin |
| 143 | + * @param Event name, Event handler |
| 144 | + * @retval true on success |
| 145 | + * @retval false on failure |
| 146 | + */ |
| 147 | +/*To Do: Only JSON Object can be used as parameter now*/ |
| 148 | +bool ThunderAccessAAMP::SubscribeEvent (string eventName, std::function<void(const WPEFramework::Core::JSON::VariantContainer&)> functionHandler) |
| 149 | +{ |
| 150 | + bool ret = true; |
| 151 | + if (NULL != remoteObject) { |
| 152 | + if (remoteObject->Subscribe<JsonObject>(1000, _T(eventName), functionHandler) == Core::ERROR_NONE) { |
| 153 | + AAMPLOG_INFO( "[ThunderAccessAAMP] %s : Subscribed to : %s", __FUNCTION__, eventName.c_str()); |
| 154 | + } else { |
| 155 | + AAMPLOG_WARN( "[ThunderAccessAAMP] %s : Failed to Subscribe notification handler for : %s", __FUNCTION__, eventName.c_str()); |
| 156 | + } |
| 157 | + } else { |
| 158 | + AAMPLOG_WARN( "[ThunderAccessAAMP] %s : remoteObject not created for the plugin!", __FUNCTION__ ); |
| 159 | + ret = false; |
| 160 | + } |
| 161 | + return ret; |
| 162 | +} |
| 163 | + |
| 164 | +/** |
| 165 | + * @brief unSubscribeEvent |
| 166 | + * @note unSubscribe event data for the specific plugin |
| 167 | + * @param Event name |
| 168 | + * @retval true on success |
| 169 | + * @retval false on failure |
| 170 | + */ |
| 171 | +/*To Do: Only JSON Object can be used as parameter now*/ |
| 172 | +bool ThunderAccessAAMP::UnSubscribeEvent (string eventName) |
| 173 | +{ |
| 174 | + bool ret = true; |
| 175 | + if (NULL != remoteObject) { |
| 176 | + remoteObject->Unsubscribe(1000, _T(eventName)); |
| 177 | + AAMPLOG_INFO( "[ThunderAccessAAMP] %s : UnSubscribed : %s event", __FUNCTION__, eventName.c_str()); |
| 178 | + } else { |
| 179 | + AAMPLOG_WARN( "[ThunderAccessAAMP] %s : remoteObject not created for the plugin!", __FUNCTION__ ); |
| 180 | + ret = false; |
| 181 | + } |
| 182 | + return ret; |
| 183 | +} |
| 184 | + |
| 185 | +/** |
| 186 | + * @brief invokeJSONRPC |
| 187 | + * @note Invoke JSONRPC call for the plugin |
| 188 | + * @param Plugin method, reference to input pram and result |
| 189 | + * @retval true on success |
| 190 | + * @retval false on failure |
| 191 | + */ |
| 192 | +bool ThunderAccessAAMP::InvokeJSONRPC(std::string method, const JsonObject ¶m, JsonObject &result) |
| 193 | +{ |
| 194 | + bool ret = true; |
| 195 | + std::string response; |
| 196 | + if(NULL == remoteObject) |
| 197 | + { |
| 198 | + AAMPLOG_WARN( "[ThunderAccessAAMP] %s : client not initialized! ", __FUNCTION__ ); |
| 199 | + return false; |
| 200 | + } |
| 201 | + if (Core::ERROR_NONE == remoteObject->Invoke<JsonObject, JsonObject>(1000, _T(method), param, result)) |
| 202 | + { |
| 203 | + if (result["success"].Boolean()) { |
| 204 | + result.ToString(response); |
| 205 | + AAMPLOG_INFO( "[ThunderAccessAAMP] %s success! Response : %s", method.c_str() , response.c_str()); |
| 206 | + } |
| 207 | + else |
| 208 | + { |
| 209 | + result.ToString(response); |
| 210 | + AAMPLOG_WARN( "[ThunderAccessAAMP] %s call failed! Response : %s", method.c_str() , response.c_str()); |
| 211 | + ret = false; |
| 212 | + } |
| 213 | + } |
| 214 | + else |
| 215 | + { |
| 216 | + AAMPLOG_WARN( "[ThunderAccessAAMP] %s : invoke failed! ", method.c_str()); |
| 217 | + ret = false; |
| 218 | + } |
| 219 | + return ret; |
| 220 | +} |
0 commit comments