RDK Documentation (Open Sourced RDK Components)
hostIf_NotificationHandler.cpp
1 /*
2  * If not stated otherwise in this file or this component's Licenses.txt file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2017 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  * @defgroup tr69hostif
22  * @{
23  * @defgroup hostif
24  * @{
25  **/
26 
27 #include "hostIf_main.h"
28 #include "hostIf_NotificationHandler.h"
29 #include "libpd.h"
30 #include "hostIf_tr69ReqHandler.h"
31 #include "webpa_notification.h"
32 #include "Device_DeviceInfo.h"
33 #include "cJSON.h"
34 
35 #define NOTIFY_PARAM_VALUE_CHANGE ""
36 NotificationHandler* NotificationHandler::pInstance = NULL;
37 parodusNotificationCallback NotificationHandler::notifyUpdateCallback = NULL;
38 GAsyncQueue* NotificationHandler::notificationQueue = NULL;
39 
40 static void addToJsonObject (cJSON* json, const char* key, const char* value, const WAL_DATA_TYPE type);
41 static void converttoWalType(HostIf_ParamType_t paramType,WAL_DATA_TYPE* pwalType);
42 
43 typedef struct _notify_params
44 {
45  char * delay;
46  char * time;
47  char * status;
48  char * download_status;
49  char * system_ready_time;
51 
52 
53 //Constructor
54 NotificationHandler::NotificationHandler()
55 {
56  // Initialize notificationQueue
57  notificationQueue = g_async_queue_new();
58 
59 }
60 //Destructor
61 NotificationHandler::~NotificationHandler()
62 {
63  g_async_queue_unref(notificationQueue);
64 }
65 
66 NotificationHandler* NotificationHandler::getInstance()
67 {
68  if(!pInstance)
69  pInstance = new NotificationHandler();
70  return pInstance;
71 }
72 
73 GAsyncQueue* NotificationHandler::GetNotificationQueue()
74 {
75  return notificationQueue;
76 }
77 
78 void NotificationHandler::registerUpdateCallback(parodusNotificationCallback cb)
79 {
80  notifyUpdateCallback = cb;
81 }
82 
83 void NotificationHandler::pushNotification(const char* destination, const char* payload)
84 {
85  NotifyData* notifyDataPtr = NULL;
86  Notify_Data* notify_data = NULL;
87  ParamNotify* paramNotify = NULL;
88 
89  if (NULL == notificationQueue || NULL == notifyUpdateCallback)
90  {
91  RDK_LOG (RDK_LOG_ERROR, LOG_TR69HOSTIF, "%s: notificationQueue = %p, notifyUpdateCallback %p \n",
92  __FUNCTION__, notificationQueue, notifyUpdateCallback);
93  }
94  else if (NULL == destination || NULL == payload)
95  {
96  RDK_LOG (RDK_LOG_ERROR, LOG_TR69HOSTIF, "%s: Input error: destination %s payload %s \n", __FUNCTION__, destination, payload);
97  }
98  else if (NULL == (notifyDataPtr = (NotifyData *) calloc(1, sizeof(NotifyData))))
99  {
100  RDK_LOG (RDK_LOG_ERROR, LOG_TR69HOSTIF, "%s: Could not allocate notifyDataPtr\n", __FUNCTION__);
101  }
102  else if (NULL == (notify_data = (Notify_Data*) calloc(1, sizeof(Notify_Data))))
103  {
104  RDK_LOG (RDK_LOG_ERROR, LOG_TR69HOSTIF, "%s: Could not allocate notify_data\n", __FUNCTION__);
105  free (notifyDataPtr);
106  }
107  else if (NULL == (paramNotify = (ParamNotify *) calloc(1, sizeof(ParamNotify))))
108  {
109  RDK_LOG (RDK_LOG_ERROR, LOG_TR69HOSTIF, "%s: Could not allocate paramNotify\n", __FUNCTION__);
110  free (notify_data);
111  free (notifyDataPtr);
112  }
113  else
114  {
115  paramNotify->notifyDest = destination ? strdup (destination) : NULL;
116  paramNotify->notifyPayload = (char*) payload;
117 
118  notify_data->notify = paramNotify;
119 
120  notifyDataPtr->type = PARAM_VALUE_CHANGE_NOTIFY;
121  notifyDataPtr->data = notify_data;
122 
123  // Add the notification to queue and call Webpa Callback
124  g_async_queue_push(notificationQueue, notifyDataPtr);
125  (*notifyUpdateCallback)();
126  }
127 }
128 
129 void NotificationHandler::pushValueChangeNotification(IARM_Bus_tr69HostIfMgr_EventData_t& event)
130 {
131  cJSON* notifyPayload = cJSON_CreateObject ();
132  cJSON_AddStringToObject(notifyPayload, "device_id", getNotifySource ()); // Device ID and Notification source are same
133 
134  WAL_DATA_TYPE pwalType;
135  converttoWalType(event.paramtype, &pwalType);
136 
137  cJSON_AddNumberToObject(notifyPayload, "datatype", pwalType);
138  cJSON_AddStringToObject(notifyPayload, "paramName", event.paramName);
139  cJSON_AddStringToObject(notifyPayload, "notificationType", "VALUE_CHANGE_NOTIFICATION");
140  addToJsonObject (notifyPayload, "paramValue", event.paramValue, pwalType);
141 
142  char* payload = cJSON_PrintUnformatted (notifyPayload);
143  cJSON_Delete (notifyPayload);
144  RDK_LOG (RDK_LOG_DEBUG, LOG_PARODUS_IF, "%s: Generated payload: %s\n", __FUNCTION__, payload);
145 
146  NotificationHandler::getInstance()->pushNotification("event:VALUE_CHANGE_NOTIFICATION", payload);
147 }
148 
149 void NotificationHandler::pushKeyValueNotification(const char* destination, const char* key, const char* value)
150 {
151  cJSON* notifyPayload = cJSON_CreateObject ();
152  cJSON_AddStringToObject(notifyPayload, "device_id", getNotifySource ()); // Device ID and Notification source are same
153 
154  cJSON_AddStringToObject(notifyPayload, key, value);
155 
156  char* payload = cJSON_PrintUnformatted (notifyPayload);
157  cJSON_Delete (notifyPayload);
158  RDK_LOG (RDK_LOG_DEBUG, LOG_PARODUS_IF, "%s: Generated payload: %s\n", __FUNCTION__, payload);
159 
160  NotificationHandler::getInstance()->pushNotification(destination, payload);
161 }
162 
163 
164 void addToJsonObject (cJSON* json, const char* key, const char* value, const WAL_DATA_TYPE type)
165 {
166  switch (type)
167  {
168  case WAL_STRING:
169  {
170  cJSON_AddStringToObject(json, key, value);
171  break;
172  }
173  case WAL_INT:
174  {
175  const int* paramNewValue = (const int*) ((value));
176  cJSON_AddNumberToObject(json, key, *paramNewValue);
177  break;
178  }
179  case WAL_UINT:
180  {
181  const unsigned int* paramNewValue = (const unsigned int*) ((value));
182  cJSON_AddNumberToObject(json, key, *paramNewValue);
183  break;
184  }
185  case WAL_BOOLEAN:
186  {
187  const bool* paramNewValue = (const bool*) ((value));
188  cJSON_AddBoolToObject(json, key, *paramNewValue);
189  break;
190  }
191  case WAL_ULONG:
192  {
193  const unsigned long *paramNewValue = (const unsigned long *) ((value));
194  cJSON_AddNumberToObject(json, key, *paramNewValue);
195  break;
196  }
197  default:
198  {
199  cJSON_AddStringToObject(json, key, value);
200  break;
201  }
202  }
203 }
204 
205 static void converttoWalType(HostIf_ParamType_t paramType,WAL_DATA_TYPE* pwalType)
206 {
207  RDK_LOG(RDK_LOG_DEBUG,LOG_TR69HOSTIF,"Inside converttoWalType \n");
208  switch(paramType)
209  {
210  case hostIf_StringType:
211  *pwalType = WAL_STRING;
212  break;
213  case hostIf_UnsignedIntType:
214  *pwalType = WAL_UINT;
215  break;
216  case hostIf_IntegerType:
217  *pwalType = WAL_INT;
218  break;
219  case hostIf_BooleanType:
220  *pwalType = WAL_BOOLEAN;
221  break;
222  case hostIf_UnsignedLongType:
223  *pwalType = WAL_ULONG;
224  break;
225  case hostIf_DateTimeType:
226  *pwalType = WAL_DATETIME;
227  break;
228  default:
229  *pwalType = WAL_STRING;
230  break;
231  }
232 }
233 
234 void NotificationHandler::push_device_mgmt_notifications(char* delay, char* time, char* download_status, char* status, char* system_ready_time)
235 {
236  int sendStatus = -1;
237  cJSON *notifyPayload = NULL;
238  unsigned long bootTime = 0;
239 
240  LOG_ENTRY_EXIT;
241 
242  notifyPayload = cJSON_CreateObject();
243 
244  if(notifyPayload != NULL)
245  {
246  cJSON_AddStringToObject(notifyPayload,"device_id", getNotifySource ());
247  if(status !=NULL)
248  {
249  cJSON_AddStringToObject(notifyPayload,"status", status);
250  }
251  if(time !=NULL)
252  {
253  cJSON_AddStringToObject(notifyPayload,"start-time", time);
254  }
255 
256  if(download_status !=NULL)
257  {
258  cJSON_AddStringToObject(notifyPayload,"download-status", (strcmp(download_status, "true") == 0)?"success":"failure");
259  }
260 
261  if ((status !=NULL) && (strcmp(status, "reboot-pending") == 0))
262  {
263  HOSTIF_MsgData_t stBootUpData = {0};
264  hostIf_DeviceInfo::getInstance(0)->get_X_RDKCENTRAL_COM_BootTime(&stBootUpData);
265  bootTime = get_ulong(stBootUpData.paramValue);
266  cJSON_AddNumberToObject(notifyPayload,"boot-time", bootTime);
267 
268  char* lastRebootReason = NULL;
269  HOSTIF_MsgData_t stMgsData = {0};
270  hostIf_DeviceInfo::getInstance(0)->get_X_RDKCENTRAL_COM_LastRebootReason(&stMgsData);
271  lastRebootReason = stMgsData.paramValue;
272  RDK_LOG (RDK_LOG_DEBUG, LOG_PARODUS_IF, "LastRebootReason is %s\n", lastRebootReason);
273 
274  if(lastRebootReason !=NULL)
275  {
276  cJSON_AddStringToObject(notifyPayload,"reboot-reason", lastRebootReason);
277  }
278 
279  if(delay !=NULL)
280  {
281  cJSON_AddNumberToObject(notifyPayload,"delay", atoi(delay));
282  }
283  }
284 
285  if ((status !=NULL) && (strcmp(status, "fully-manageable") == 0) && (system_ready_time != NULL))
286  {
287  HOSTIF_MsgData_t stBootUpData = {0};
288  hostIf_DeviceInfo::getInstance(0)->get_X_RDKCENTRAL_COM_BootTime(&stBootUpData);
289  bootTime = get_ulong(stBootUpData.paramValue);
290  cJSON_AddNumberToObject(notifyPayload,"boot-time", bootTime);
291  cJSON_AddNumberToObject(notifyPayload,"system-ready-time", atoi(system_ready_time));
292  }
293 
294  char* payload = cJSON_PrintUnformatted (notifyPayload);
295  cJSON_Delete(notifyPayload);
296 
297  const char *destination = "event:device-status";
298  NotificationHandler::getInstance()->pushNotification(destination, payload);
299  }
300 }
301 
302 /** @} */
303 /** @} */
libpd.h
NotificationHandler
Definition: hostIf_NotificationHandler.h:37
_tr69HostIfMgr_EventData_t
Definition: hostIf_tr69ReqHandler.h:195
_HostIf_MsgData_t
Definition: hostIf_tr69ReqHandler.h:170
ParamNotify
Structure to return Parameter info in Notification callback.
Definition: webpa_adapter.h:183
hostIf_main.h
hostIf_main API.
getNotifySource
char * getNotifySource()
Get the Notification source.
Definition: webpa_notification.cpp:66
_notify_params
Definition: hostIf_NotificationHandler.cpp:43
NotifyData
Definition: webpa_adapter.h:208
RDK_LOG
#define RDK_LOG
Definition: rdk_debug.h:258
HostIf_ParamType_t
enum _HostIf_ParamType HostIf_ParamType_t
Device_DeviceInfo.h
The header file provides TR69 device information APIs.
WAL_DATA_TYPE
WAL_DATA_TYPE
WebPA Data types.
Definition: webpa_adapter.h:112
_HostIf_MsgData_t::paramValue
char paramValue[(4 *1024)]
Definition: hostIf_tr69ReqHandler.h:172
Notify_Data
Definition: webpa_adapter.h:201