RDK Documentation (Open Sourced RDK Components)
hostIf_TimeClient_ReqHandler.cpp
Go to the documentation of this file.
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 2016 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 hostIf_TimeClient_ReqHandler.cpp
22  * @brief The header file provides HostIf time client request handler information APIs.
23  */
24 
25 /**
26 * @defgroup tr69hostif
27 * @{
28 * @defgroup hostif
29 * @{
30 **/
31 //#define HAVE_VALUE_CHANGE_EVENT
32 #include "hostIf_main.h"
33 #include "hostIf_utils.h"
35 #include "safec_lib.h"
36 
37 #define NTPSTATUS_NAME_FILE "/tmp/ntp_status"
38 #define NTPENABLED_FILE "/opt/.ntpEnabled"
39 
40 TimeClientReqHandler* TimeClientReqHandler::pInstance = NULL;
41 updateCallback TimeClientReqHandler::mUpdateCallback = NULL;
42 msgHandler* TimeClientReqHandler::getInstance()
43 {
44 
45  if(!pInstance)
46  pInstance = new TimeClientReqHandler();
47  return pInstance;
48 }
49 
50 /**
51  * @brief This function is use to initialize. Currently not implemented.
52  *
53  * @return Returns the status of the operation.
54  *
55  * @retval true if initialization is successfully .
56  * @retval false if initialization is not successful.
57  * @ingroup TR-069HOSTIF_TIMECLIENT_REQHANDLER_CLASSES
58  */
60 {
61  RDK_LOG(RDK_LOG_TRACE1,LOG_TR69HOSTIF,"[%s:%s] Entering..\n", __FUNCTION__, __FILE__);
62  RDK_LOG(RDK_LOG_TRACE1,LOG_TR69HOSTIF,"[%s:%s] Exiting..\n", __FUNCTION__, __FILE__);
63  return true;
64 }
65 
66 /**
67  * @brief This function is used to close all the instances of time client.
68  *
69  * @return Returns the status of the operation.
70  *
71  * @retval true if it is successfully close all the instances.
72  * @retval false if not able to close all the instances.
73  * @ingroup TR-069HOSTIF_TIMECLIENT_REQHANDLER_CLASSES
74  */
76 {
77  RDK_LOG(RDK_LOG_TRACE1,LOG_TR69HOSTIF,"[%s:%s] Entering..\n", __FUNCTION__, __FILE__);
78 
79  hostIf_Time::closeAllInstances();
80 
81  RDK_LOG(RDK_LOG_TRACE1,LOG_TR69HOSTIF,"[%s:%s] Exiting..\n", __FUNCTION__, __FILE__);
82  return true;
83 }
84 
85 /**
86  * @brief This function is use to handle the set message request of time client interface
87  * and set the attributes of the time client component.
88  *
89  * @param[out] stMsgData TR-069 Host interface message request.
90  *
91  * @return Returns the status of the operation.
92  *
93  * @retval OK if it is successfully.
94  * @retval ERR_INTERNAL_ERROR if not able to set the data to the device.
95  * @ingroup TR-069HOSTIF_TIMECLIENT_REQHANDLER_CLASSES
96  */
98 {
99  int ret = NOT_HANDLED;
100  const char *pSetting;
101  int instanceNumber = 0; //CID:83448 - UNINIT
102  RDK_LOG(RDK_LOG_TRACE1,LOG_TR69HOSTIF,"[%s:%s:%d] Found string as %s\n", __FUNCTION__, __FILE__, __LINE__, stMsgData->paramName);
103  if(strncasecmp(stMsgData->paramName,"Device.Time",strlen("Device.Time"))==0)
104  {
105  stMsgData->instanceNum = 0;
106  hostIf_Time::getLock();
107 
108  hostIf_Time *pIface = hostIf_Time::getInstance(instanceNumber);
109 
110  if(!pIface)
111  {
112  hostIf_Time::releaseLock();
113  return NOK;
114  }
115 
116  if (stMsgData->bsUpdate != HOSTIF_NONE)
117  {
118  if ( (stMsgData->bsUpdate == HOSTIF_SRC_RFC && stMsgData->requestor == HOSTIF_SRC_RFC) ||
119  (stMsgData->bsUpdate == HOSTIF_SRC_ALL && (stMsgData->requestor == HOSTIF_SRC_RFC || stMsgData->requestor == HOSTIF_SRC_WEBPA)) )
120  {
121  ret = pIface->set_xRDKCentralComBootstrap(stMsgData);
122  }
123  else
124  RDK_LOG(RDK_LOG_DEBUG,LOG_TR69HOSTIF,"[%s()] Not setting the bootstrap param:%s [bsUpdate=%d, requestor=%d]\n", __FUNCTION__, stMsgData->paramName, stMsgData->bsUpdate, stMsgData->requestor);
125 
126  }
127  else if (strcasecmp(stMsgData->paramName,"Device.Time.Enable") == 0)
128  {
129  ret = pIface->set_Device_Time_Enable(stMsgData);
130  }
131  else
132  {
133  RDK_LOG(RDK_LOG_TRACE1,LOG_TR69HOSTIF,"[%s:%s:%d] parameter : \'%s\' Not handled \n", __FUNCTION__, __FILE__, __LINE__, stMsgData->paramName);
134  stMsgData->faultCode = fcInvalidParameterName;
135  ret = NOK;
136  }
137  hostIf_Time::releaseLock();
138  }
139  return ret;
140 }
141 
142 /**
143  * @brief This function use to handle the get message request of time client interface
144  * and get the attributes of the time client component "LocalTimeZone" and "CurrentLocalTime".
145  *
146  * @param[out] stMsgData TR-069 Host interface message request.
147  *
148  * @return Returns the status of the operation.
149  *
150  * @retval OK if it is successfully.
151  * @retval ERR_INTERNAL_ERROR if not able to get data from the device.
152  * @ingroup TR-069HOSTIF_TIMECLIENT_REQHANDLER_CLASSES
153  */
155 {
156  int ret = NOT_HANDLED;
157  const char *pSetting;
158  int instanceNumber = 0; //CID:87240 - UNINIT
159  RDK_LOG(RDK_LOG_TRACE1,LOG_TR69HOSTIF,"[%s:%s:%d] Found string as %s\n", __FUNCTION__, __FILE__, __LINE__, stMsgData->paramName);
160 
161  if(strncasecmp(stMsgData->paramName,"Device.Time",strlen("Device.Time"))==0)
162  {
163  stMsgData->instanceNum = 0;
164  hostIf_Time::getLock();
165  hostIf_Time *pIface = hostIf_Time::getInstance(instanceNumber);
166 
167  if(!pIface)
168  {
169  hostIf_Time::releaseLock();
170  return NOK;
171  }
172 
173  if (stMsgData->bsUpdate != HOSTIF_NONE)
174  {
175  ret = pIface->get_xRDKCentralComBootstrap(stMsgData);
176  }
177  else if (strcasecmp(stMsgData->paramName,"Device.Time.LocalTimeZone") == 0)
178  {
179  ret = pIface->get_Device_Time_LocalTimeZone(stMsgData);
180  }
181  else if (strcasecmp(stMsgData->paramName,"Device.Time.CurrentLocalTime") == 0)
182  {
183  ret = pIface->get_Device_Time_CurrentLocalTime(stMsgData);
184  }
185  else if (strcasecmp(stMsgData->paramName,"Device.Time.Status") == 0)
186  {
187  FILE *fp = NULL;
188  char ntpStatus[64] = {'\0'};
189 
190  fp = fopen(NTPSTATUS_NAME_FILE,"r");
191  if(fp == NULL) {
192  RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,"[%s:%s:%d] Failed to open NTP Status file\n.!",__FUNCTION__, __FILE__, __LINE__);
193  hostIf_Time::releaseLock();
194  return NOK;
195  }
196  if(fgets(ntpStatus, 64,fp)!=NULL) {
197  // Remove new line char if any in model name
198  int len = strlen(ntpStatus);
199  if(ntpStatus[len-1] == '\n') ntpStatus[len-1] = '\0';
200  RDK_LOG(RDK_LOG_DEBUG,LOG_TR69HOSTIF,"[%s:%s:%d] ntpStatus = %s.\n",__FUNCTION__, __FILE__, __LINE__,ntpStatus);
201  strncpy((char *)stMsgData->paramValue, ntpStatus,sizeof(stMsgData->paramValue)-1);
202  stMsgData->paramValue[sizeof(stMsgData->paramValue)-1] = '\0';
203  stMsgData->paramLen = strlen(ntpStatus);
204  RDK_LOG(RDK_LOG_DEBUG,LOG_TR69HOSTIF,"[%s:%s:%d] paramValue: %s stMsgData->paramLen: %d \n", __FUNCTION__, __FILE__, __LINE__, stMsgData->paramValue,stMsgData->paramLen);
205  ret = OK;
206  } else {
207  RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,"%s(): Failed to read ntp status.\n", __FUNCTION__);
208  }
209  fclose(fp);
210  }
211  else if (strcasecmp(stMsgData->paramName,"Device.Time.Enable") == 0)
212  {
213  bool enable = false;
214  ifstream ifp(NTPENABLED_FILE);
215  if(ifp.is_open())
216  {
217  enable=true;
218  RDK_LOG (RDK_LOG_DEBUG, LOG_TR69HOSTIF, "[%s] %s exists, NTP sync enabled\n", __FUNCTION__, NTPENABLED_FILE);
219 
220  }
221  else
222  {
223  enable=false;
224  RDK_LOG(RDK_LOG_DEBUG,LOG_TR69HOSTIF,"[%s] File does not exist, NTP sync disabled\n", __FUNCTION__);
225  }
226 
227  put_boolean(stMsgData->paramValue,enable);
228  stMsgData->paramtype = hostIf_BooleanType;
229  stMsgData->paramLen = sizeof(bool);
230  ret = OK;
231  }
232  else
233  {
234  RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,"[%s:%s:%d] parameter : \'%s\' Not handled \n", __FUNCTION__, __FILE__, __LINE__, stMsgData->paramName);
235  stMsgData->faultCode = fcInvalidParameterName;
236  ret = NOK;
237  }
238  hostIf_Time::releaseLock();
239  }
240  else
241  {
242  RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,"[%s:%s:%d] parameter : \'%s\' Not handled \n", __FUNCTION__, __FILE__, __LINE__, stMsgData->paramName);
243  stMsgData->faultCode = fcInvalidParameterName;
244  ret = NOK;
245  }
246  return ret;
247 }
248 
249 void TimeClientReqHandler::registerUpdateCallback(updateCallback cb)
250 {
251  mUpdateCallback = cb;
252 }
253 
254 int TimeClientReqHandler::handleGetAttributesMsg(HOSTIF_MsgData_t *stMsgData)
255 {
256  int ret = NOT_HANDLED;
257  int instanceNumber =0;
258 
259  hostIf_Time::getLock();
260  hostIf_Time *pIface = hostIf_Time::getInstance(instanceNumber);
261  stMsgData->instanceNum = instanceNumber;
262  if(!pIface)
263  {
264  hostIf_Time::releaseLock();
265  return NOK;
266  }
267  GHashTable* notifyhash = pIface->getNotifyHash();
268  if(notifyhash != NULL)
269  {
270  int *notifyValuePtr;
271  notifyValuePtr = (int*) malloc(1 * sizeof(int));
272 
273  // Inserting Notification parameter to Notify Hash Table,
274  // Note that neither keys nor values are copied when inserted into the GHashTable, so they must exist for the lifetime of the GHashTable
275  // There for allocating a memory for both Param name and param value. This should be freed whenever we disable Notification.
276  char *notifyKey = NULL;
277  notifyKey = (char*) calloc(sizeof(char),strlen(stMsgData->paramName)+1);
278  if((NULL != notifyValuePtr) && (NULL != notifyKey))
279  {
280  *notifyValuePtr = 1;
281  errno_t rc = -1;
282  rc=strcpy_s(notifyKey,strlen(stMsgData->paramName)+1,stMsgData->paramName);
283  if(rc!=EOK)
284  {
285  ERR_CHK(rc);
286  }
287  g_hash_table_insert(notifyhash,notifyKey,notifyValuePtr);
288  ret = OK;
289  }
290  else
291  {
292  ret = NOK;
293  RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,"[%s:%s:%d] Not able to allocate Notify pointer %s\n", __FUNCTION__, __FILE__, __LINE__, stMsgData->paramName);
294  }
295  free(notifyKey); //CID:88420 - Resource leak
296  free(notifyValuePtr);
297  }
298  else
299  {
300  ret = NOK;
301  RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,"[%s:%s:%d] Not able to get notifyhash %s\n", __FUNCTION__, __FILE__, __LINE__, stMsgData->paramName);
302  }
303  hostIf_Time::releaseLock();
304  return ret;
305 }
306 
307 int TimeClientReqHandler::handleSetAttributesMsg(HOSTIF_MsgData_t *stMsgData)
308 {
309  int ret = NOT_HANDLED;
310  int instanceNumber = 0;
311 
312  hostIf_Time::getLock();
313  hostIf_Time *pIface = hostIf_Time::getInstance(instanceNumber);
314  stMsgData->instanceNum = instanceNumber;
315  if(!pIface)
316  {
317  hostIf_Time::releaseLock();
318  return NOK;
319  }
320  GHashTable* notifyhash = pIface->getNotifyHash();
321  if(notifyhash != NULL)
322  {
323  int *notifyValuePtr;
324  notifyValuePtr = (int*) malloc(1 * sizeof(int));
325 
326  // Inserting Notification parameter to Notify Hash Table,
327  // Note that neither keys nor values are copied when inserted into the GHashTable, so they must exist for the lifetime of the GHashTable
328  // There for allocating a memory for both Param name and param value. This should be freed whenever we disable Notification.
329  char *notifyKey = NULL;
330  notifyKey = (char*) calloc(sizeof(char),strlen(stMsgData->paramName)+1);
331  if((NULL != notifyValuePtr) && (NULL != notifyKey))
332  {
333  *notifyValuePtr = 1;
334  errno_t rc = -1;
335  rc=strcpy_s(notifyKey,strlen(stMsgData->paramName)+1,stMsgData->paramName);
336  if(rc!=EOK)
337  {
338  ERR_CHK(rc);
339  }
340  g_hash_table_insert(notifyhash,notifyKey,notifyValuePtr);
341  ret = OK;
342  }
343  else
344  {
345  ret = NOK;
346  RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,"[%s:%s:%d] Not able to allocate Notify pointer %s\n", __FUNCTION__, __FILE__, __LINE__, stMsgData->paramName);
347  }
348  free(notifyKey); //CID:89484 - Resource leak
349  free(notifyValuePtr);
350  }
351  else
352  {
353  ret = NOK;
354  RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,"[%s:%s:%d] EthernetClientReqHandler Not able to get notifyhash %s\n", __FUNCTION__, __FILE__, __LINE__, stMsgData->paramName);
355  }
356  hostIf_Time::releaseLock();
357  return ret;
358 }
359 void TimeClientReqHandler::checkForUpdates()
360 {
361  HOSTIF_MsgData_t msgData;
362  bool bChanged;
363  int instanceNumber;
364  GHashTable* notifyhash = NULL;
365 
366  hostIf_Time::getLock();
367 
368 #ifdef HAVE_VALUE_CHANGE_EVENT
369  instanceNumber = 0;
370  hostIf_Time *pIface = hostIf_Time::getInstance(instanceNumber);
371  if(NULL != pIface)
372  {
373  notifyhash = pIface->getNotifyHash();
374  }
375  else
376  {
377  RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,"[%s:%s] Unable to get Time Instance\n", __FUNCTION__, __FILE__);
378  }
379 
380 // Iterate through Ghash Table
381  if(NULL != notifyhash)
382  {
383  GHashTableIter notifyHashIterator;
384  gpointer paramName;
385  gpointer notifyEnable;
386  const char *pSetting;
387 
388  g_hash_table_iter_init (&notifyHashIterator, notifyhash);
389  while (g_hash_table_iter_next (&notifyHashIterator, &paramName, &notifyEnable))
390  {
391  int* isNotifyEnabled = (int *)notifyEnable;
392  instanceNumber = 0;
393  if(matchComponent((const char*)paramName,"Device.Time.Interface",&pSetting,instanceNumber))
394  {
395  if(!instanceNumber)
396  { // Time settings not found in Notify Hash Table
397  hostIf_Time::releaseLock();
398  continue;
399  }
400  hostIf_Time *pTimeIface = hostIf_Time::getInstance(instanceNumber);
401  if(pTimeIface)
402  {
403  if (strcasecmp(pSetting,"Enable") == 0)
404  {
405  pTimeIface->get_Device_Time_LocalTimeZone(&msgData,&bChanged);
406  if(mUpdateCallback && (*isNotifyEnabled == 1))
407  {
408  mUpdateCallback(IARM_BUS_TR69HOSTIFMGR_EVENT_VALUECHANGED,(const char*)paramName, msgData.paramValue, msgData.paramtype);
409  }
410 
411  }
412  }
413  }
414  }
415 
416  }
417 #endif /*HAVE_VALUE_CHANGE_EVENT */
418 
419  hostIf_Time::releaseLock();
420 }
421 
422 
423 /** @} */
424 /** @} */
_HostIf_MsgData_t::instanceNum
short instanceNum
Definition: hostIf_tr69ReqHandler.h:176
_HostIf_MsgData_t::bsUpdate
HostIf_Source_Type_t bsUpdate
Definition: hostIf_tr69ReqHandler.h:181
hostIf_Time::get_Device_Time_LocalTimeZone
int get_Device_Time_LocalTimeZone(HOSTIF_MsgData_t *, bool *pChanged=NULL)
Get the local time zone definition.
Definition: Device_Time.cpp:163
_HostIf_MsgData_t
Definition: hostIf_tr69ReqHandler.h:170
TimeClientReqHandler::init
virtual bool init()
This function is use to initialize. Currently not implemented.
Definition: hostIf_TimeClient_ReqHandler.cpp:59
put_boolean
void put_boolean(char *ptr, bool val)
This function converts the input data to Boolean type.
Definition: hostIf_utils.cpp:191
TimeClientReqHandler::handleGetMsg
virtual int handleGetMsg(HOSTIF_MsgData_t *stMsgData)
This function use to handle the get message request of time client interface and get the attributes o...
Definition: hostIf_TimeClient_ReqHandler.cpp:154
hostIf_main.h
hostIf_main API.
hostIf_Time::get_xRDKCentralComBootstrap
int get_xRDKCentralComBootstrap(HOSTIF_MsgData_t *)
Get the bootstrap parameters.
Definition: Device_Time.cpp:263
TimeClientReqHandler
This class provides the interface for getting TimeClient request handler information.
Definition: hostIf_TimeClient_ReqHandler.h:55
_HostIf_MsgData_t::paramtype
HostIf_ParamType_t paramtype
Definition: hostIf_tr69ReqHandler.h:177
TimeClientReqHandler::handleSetMsg
virtual int handleSetMsg(HOSTIF_MsgData_t *stMsgData)
This function is use to handle the set message request of time client interface and set the attribute...
Definition: hostIf_TimeClient_ReqHandler.cpp:97
RDK_LOG
#define RDK_LOG
Definition: rdk_debug.h:258
_HostIf_MsgData_t::faultCode
faultCode_t faultCode
Definition: hostIf_tr69ReqHandler.h:179
hostIf_Time::set_Device_Time_Enable
int set_Device_Time_Enable(HOSTIF_MsgData_t *)
Set the status of the time client.
Definition: Device_Time.cpp:273
hostIf_Time::get_Device_Time_CurrentLocalTime
int get_Device_Time_CurrentLocalTime(HOSTIF_MsgData_t *, bool *pChanged=NULL)
Get the current dates & time.
Definition: Device_Time.cpp:221
_HostIf_MsgData_t::paramName
char paramName[(4 *1024)]
Definition: hostIf_tr69ReqHandler.h:171
_HostIf_MsgData_t::paramValue
char paramValue[(4 *1024)]
Definition: hostIf_tr69ReqHandler.h:172
hostIf_Time
Get the status of the time client.
Definition: Device_Time.h:134
msgHandler
Definition: hostIf_msgHandler.h:103
_HostIf_MsgData_t::requestor
HostIf_Source_Type_t requestor
Definition: hostIf_tr69ReqHandler.h:180
hostIf_TimeClient_ReqHandler.h
The header file provides HostIf time client request handler information APIs.
IARM_BUS_TR69HOSTIFMGR_EVENT_VALUECHANGED
@ IARM_BUS_TR69HOSTIFMGR_EVENT_VALUECHANGED
Definition: hostIf_tr69ReqHandler.h:189
TimeClientReqHandler::unInit
virtual bool unInit()
This function is used to close all the instances of time client.
Definition: hostIf_TimeClient_ReqHandler.cpp:75
_HostIf_MsgData_t::paramLen
short paramLen
Definition: hostIf_tr69ReqHandler.h:175
hostIf_Time::set_xRDKCentralComBootstrap
int set_xRDKCentralComBootstrap(HOSTIF_MsgData_t *)
Set the bootstrap parameters.
Definition: Device_Time.cpp:268