RDK Documentation (Open Sourced RDK Components)
hostIf_DHCPv4Client_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  * @brief Provides implementation for DHCPv4 Client Request Handler Interface
21  *
22  * @author vejuturu@cisco.com
23  */
24 //#define HAVE_VALUE_CHANGE_EVENT
25 
26 /**
27  * @file hostIf_DHCPv4Client_ReqHandler.cpp
28  * @brief The header file provides HostIf DHCPv4 client request handler information APIs.
29  */
30 #include "hostIf_main.h"
31 #include "hostIf_utils.h"
32 #include "Device_DHCPv4_Client.h"
34 #include "safec_lib.h"
35 
36 DHCPv4ClientReqHandler* DHCPv4ClientReqHandler::pInstance = NULL;
37 
38 updateCallback DHCPv4ClientReqHandler::mUpdateCallback = NULL;
39 int DHCPv4ClientReqHandler::curNumOfDHCPv4Clients = 0;
40 
41 msgHandler* DHCPv4ClientReqHandler::getInstance()
42 {
43  hostIf_DHCPv4Client::getLock();
44  if(!pInstance)
45  pInstance = new DHCPv4ClientReqHandler();
46  hostIf_DHCPv4Client::releaseLock();
47 
48  return pInstance;
49 }
50 
51 void DHCPv4ClientReqHandler::reset()
52 {
53  hostIf_DHCPv4Client::getLock();
54  curNumOfDHCPv4Clients = 0;
55  hostIf_DHCPv4Client::releaseLock();
56 }
57 
58 /**
59  * @brief This function is used to initialize. Currently not implemented.
60  *
61  * @return Returns the status of the operation.
62  *
63  * @retval true if initialization is successful.
64  * @retval false if initialization is not successful.
65  * @ingroup TR-069HOSTIF_DHCPV4CLIENT_REQHANDLER_CLASSES
66  */
68 {
69  return true;
70 }
71 
72 /**
73  * @brief This function is used to close all the instances of DHCPv4 client.
74  *
75  * @return Returns the status of the operation.
76  *
77  * @retval true if it successfully close all the instances.
78  * @retval false if not able to close all the instances.
79  * @ingroup TR-069HOSTIF_DHCPV4CLIENT_REQHANDLER_CLASSES
80  */
82 {
83  RDK_LOG(RDK_LOG_DEBUG,LOG_TR69HOSTIF,"[%s()] DHCPv4 manager DeInitializing\n", __FUNCTION__);
84  hostIf_DHCPv4Client::closeAllInstances();
85 
86  return true;
87 }
88 
89 /**
90  * @brief This function is used to handle the set message request of DHCPv4 Client.
91  * Currently not implemented.
92  *
93  * @param[out] stMsgData TR-069 Host interface message request.
94  *
95  * @return Returns the status of the operation.
96  *
97  * @retval OK if it is successful.
98  * @retval ERR_INTERNAL_ERROR if not able to set the data to the device.
99  * @ingroup TR-069HOSTIF_DHCPV4CLIENT_REQHANDLER_CLASSES
100  */
102 {
103  int ret = NOT_HANDLED;
104 
105  RDK_LOG(RDK_LOG_INFO,LOG_TR69HOSTIF,"[%s:%s] Found string as %s. Set command not supported.\n",
106  __FUNCTION__, __FILE__, stMsgData->paramName);
107  stMsgData->faultCode = fcAttemptToSetaNonWritableParameter;
108  return ret;
109 }
110 
111 /**
112  * @brief This function is used to handle the get message request of DHCPv4 Client information
113  * such as "Interface", "IPRouters" and "DNSServers".
114  *
115  * @param[out] stMsgData TR-069 Host interface message request.
116  *
117  * @return Returns the status of the operation.
118  *
119  * @retval OK if it gets the data successfully.
120  * @retval ERR_INTERNAL_ERROR if not able to get the data from the device.
121  * @ingroup TR-069HOSTIF_DHCPV4CLIENT_REQHANDLER_CLASSES
122  */
124 {
125  int ret = NOT_HANDLED;
126  const char *pSetting;
127  int instanceNumber = 0;
128  hostIf_DHCPv4Client::getLock();
129 
130  RDK_LOG(RDK_LOG_TRACE1,LOG_TR69HOSTIF,"[%s:%s:%d] Found string as %s\n", __FUNCTION__, __FILE__, __LINE__, stMsgData->paramName);
131  if(strcasecmp(stMsgData->paramName, "Device.DHCPv4.ClientNumberOfEntries") == 0)
132  {
133  ret = hostIf_DHCPv4Client::get_Device_DHCPv4_ClientNumberOfEntries(stMsgData);
134  }
135  else if(matchComponent(stMsgData->paramName,"Device.DHCPv4.Client",&pSetting,instanceNumber))
136  {
137  hostIf_DHCPv4Client *pDHCPv4Client = hostIf_DHCPv4Client::getInstance(instanceNumber);
138  stMsgData->instanceNum = instanceNumber;
139  if(pDHCPv4Client)
140  {
141  if(strcasecmp(pSetting,"Interface") == 0)
142  {
143  ret = pDHCPv4Client->get_Device_DHCPv4_Client_InterfaceReference(stMsgData);
144  }
145  else if(strcasecmp(pSetting,"IPRouters") == 0)
146  {
147  ret = pDHCPv4Client->get_Device_DHCPv4_Client_IPRouters(stMsgData);
148  }
149  else if(strcasecmp(pSetting,"DNSServers") == 0)
150  {
151  ret = pDHCPv4Client->get_Device_DHCPv4_Client_DnsServer(stMsgData);
152  }
153  else
154  {
155  RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,"[%s:%d] Parameter : \'%s\' is Not Supported \n", __FUNCTION__, __LINE__, stMsgData->paramName);
156  stMsgData->faultCode = fcInvalidParameterName;
157  ret=NOK;
158  }
159  }
160  else
161  {
162  ret = NOK;
163  RDK_LOG(RDK_LOG_INFO,LOG_TR69HOSTIF,"[%s:%s:%d] hostIf_DHCPv4Client::getInstance returned NULL for instance %d,\n",
164  __FUNCTION__, __FILE__, __LINE__, instanceNumber);
165  }
166  }
167  else
168  {
169  RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,"[%s:%d] Parameter : \'%s\' is Not Supported \n", __FUNCTION__, __LINE__, stMsgData->paramName);
170  stMsgData->faultCode = fcInvalidParameterName;
171  ret=NOK;
172  }
173 
174  hostIf_DHCPv4Client::releaseLock();
175  return ret;
176 }
177 int DHCPv4ClientReqHandler::handleGetAttributesMsg(HOSTIF_MsgData_t *stMsgData)
178 {
179  int ret = NOT_HANDLED;
180  int instanceNumber = 0;
181 
182  hostIf_DHCPv4Client::getLock();
183  hostIf_DHCPv4Client *pIface = hostIf_DHCPv4Client::getInstance(instanceNumber);
184  stMsgData->instanceNum = instanceNumber;
185  if(!pIface)
186  {
187  hostIf_DHCPv4Client::releaseLock();
188  return NOK;
189  }
190 
191  GHashTable* notifyhash = pIface->getNotifyHash();
192  if(notifyhash != NULL)
193  {
194  int* notifyvalue = (int*) g_hash_table_lookup(notifyhash,stMsgData->paramName);
195  put_int(stMsgData->paramValue, *notifyvalue);
196  stMsgData->paramtype = hostIf_IntegerType;
197  ret = OK;
198  }
199  else
200  {
201  ret = NOK;
202  }
203  hostIf_DHCPv4Client::releaseLock();
204  return ret;
205 }
206 
207 int DHCPv4ClientReqHandler::handleSetAttributesMsg(HOSTIF_MsgData_t *stMsgData)
208 {
209  int ret = NOT_HANDLED;
210  int instanceNumber = 0;
211 
212  hostIf_DHCPv4Client::getLock();
213  hostIf_DHCPv4Client *pIface = hostIf_DHCPv4Client::getInstance(instanceNumber);
214  stMsgData->instanceNum = instanceNumber;
215  if(!pIface)
216  {
217  hostIf_DHCPv4Client::releaseLock();
218  return NOK;
219  }
220  GHashTable* notifyhash = pIface->getNotifyHash();
221  if(notifyhash != NULL)
222  {
223  int *notifyValuePtr;
224  notifyValuePtr = (int*) malloc(1 * sizeof(int));
225 
226  // Inserting Notification parameter to Notify Hash Table,
227  // Note that neither keys nor values are copied when inserted into the GHashTable, so they must exist for the lifetime of the GHashTable
228  // There for allocating a memory for both Param name and param value. This should be freed whenever we disable Notification.
229  char *notifyKey;
230  notifyKey = (char*) malloc(sizeof(char)*strlen(stMsgData->paramName)+1);
231  if(NULL != notifyValuePtr)
232  {
233  *notifyValuePtr = 1;
234  errno_t rc = -1;
235  rc=strcpy_s(notifyKey,strlen(stMsgData->paramName)+1,stMsgData->paramName);
236  if(rc!=EOK)
237  {
238  ERR_CHK(rc);
239  }
240  g_hash_table_insert(notifyhash,notifyKey,notifyValuePtr);
241  ret = OK;
242  }
243  else
244  {
245  ret = NOK;
246  RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,"[%s:%s:%d] EthernetClientReqHandler Not able to allocate Notify pointer %s\n", __FUNCTION__, __FILE__, __LINE__, stMsgData->paramName);
247  }
248  }
249  else
250  {
251  ret = NOK;
252  RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,"[%s:%s:%d] EthernetClientReqHandler Not able to get notifyhash %s\n", __FUNCTION__, __FILE__, __LINE__, stMsgData->paramName);
253  }
254  hostIf_DHCPv4Client::releaseLock();
255  return ret;
256 }
257 
258 void DHCPv4ClientReqHandler::registerUpdateCallback(updateCallback cb)
259 {
260  mUpdateCallback = cb;
261 }
262 
263 void DHCPv4ClientReqHandler::checkForUpdates()
264 {
265  HOSTIF_MsgData_t msgData;
266  bool bChanged;
267  GList *elem;
268  int index = 1;
269  char tmp_buff[TR69HOSTIFMGR_MAX_PARAM_LEN];
270  hostIf_DHCPv4Client::getLock();
271  int instanceNumber = 0;
272  GHashTable* notifyhash = NULL;
273 
274  memset(&msgData,0,sizeof(msgData));
275  memset(tmp_buff,0,TR69HOSTIFMGR_MAX_PARAM_LEN);
276  RDK_LOG(RDK_LOG_DEBUG,LOG_TR69HOSTIF,"[%s:%s] Entering..\n", __FILE__, __FUNCTION__);
277 
278 
279  if(hostIf_DHCPv4Client::get_Device_DHCPv4_ClientNumberOfEntries(&msgData) == OK)
280  {
281  int tmpNoDev = get_int(msgData.paramValue);
282  char tmp[TR69HOSTIFMGR_MAX_PARAM_LEN] = "";
283  sprintf(tmp_buff,"Device.DHCPv4.Client");
284  while(curNumOfDHCPv4Clients > tmpNoDev)
285  {
286  sprintf(tmp,"%s.%d.",tmp_buff,tmpNoDev);
287  if(mUpdateCallback) mUpdateCallback(IARM_BUS_TR69HOSTIFMGR_EVENT_REMOVE,tmp, NULL, hostIf_IntegerType);
288  RDK_LOG(RDK_LOG_DEBUG,LOG_TR69HOSTIF,"[%s:%s] ClientNumberOfEntries Removed Event..\n", __FILE__, __FUNCTION__);
289 
290  tmpNoDev++;
291  }
292  while(curNumOfDHCPv4Clients < tmpNoDev)
293  {
294  sprintf(tmp,"%s.",tmp_buff);
295  if(mUpdateCallback) mUpdateCallback(IARM_BUS_TR69HOSTIFMGR_EVENT_ADD,tmp, NULL, hostIf_IntegerType);
296  RDK_LOG(RDK_LOG_DEBUG,LOG_TR69HOSTIF,"[%s:%s] ClientNumberOfEntries Added Event..\n", __FILE__, __FUNCTION__);
297 
298  tmpNoDev--;
299  }
300  curNumOfDHCPv4Clients = get_int(msgData.paramValue);
301  }
302 #ifdef HAVE_VALUE_CHANGE_EVENT
303  instanceNumber = 0;
304  hostIf_DHCPv4Client *pDHCPv4Client = hostIf_DHCPv4Client::getInstance(instanceNumber);
305  if(NULL != pDHCPv4Client)
306  {
307  notifyhash = pDHCPv4Client->getNotifyHash();
308  }
309  else
310  {
311  RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,"[%s:%s] Unable to get DHCPV4Client Instance\n", __FUNCTION__, __FILE__);
312  }
313 
314  // Iterate through Ghash Table
315  if(NULL != notifyhash)
316  {
317  GHashTableIter notifyHashIterator;
318  gpointer paramName;
319  gpointer notifyEnable;
320  bool bChanged;
321 
322  g_hash_table_iter_init (&notifyHashIterator, notifyhash);
323  while (g_hash_table_iter_next (&notifyHashIterator, &paramName, &notifyEnable))
324  {
325  int* isNotifyEnabled = (int *)notifyEnable;
326  instanceNumber = 0;
327  if(matchComponent((const char*)paramName,"Device.DHCPv4.Client",&pSetting,instanceNumber))
328  {
329  hostIf_DHCPv4Client *pIface = hostIf_DHCPv4Client::getInstance(instanceNumber);
330  if(pIface)
331  {
332  if (strcasecmp(pSetting,"Interface") == 0)
333  {
334  memset(&msgData,0,sizeof(msgData));
335  bChanged = false;
336  pIface->get_Device_DHCPv4_Client_InterfaceReference(&msgData,&bChanged);
337  if(bChanged)
338  {
339  if(mUpdateCallback && (*isNotifyEnabled == 1))
340  {
341  mUpdateCallback(IARM_BUS_TR69HOSTIFMGR_EVENT_VALUECHANGED,(const char*)paramName, msgData.paramValue, msgData.paramtype);
342  }
343  }
344  }
345  if (strcasecmp(pSetting,"IPRouters") == 0)
346  {
347  memset(&msgData,0,sizeof(msgData));
348  bChanged = false;
349  pIface->get_Device_DHCPv4_Client_IPRouters(&msgData,&bChanged);
350  if(bChanged)
351  {
352  if(mUpdateCallback && (*isNotifyEnabled == 1))
353  {
354  mUpdateCallback(IARM_BUS_TR69HOSTIFMGR_EVENT_VALUECHANGED,(const char*)paramName, msgData.paramValue, msgData.paramtype);
355  }
356  }
357  }
358  if (strcasecmp(pSetting,"DNSServers") == 0)
359  {
360  memset(&msgData,0,sizeof(msgData));
361  bChanged = false;
362  pIface->get_Device_DHCPv4_Client_DnsServer(&msgData,&bChanged);
363  if(bChanged)
364  {
365  if(mUpdateCallback && (*isNotifyEnabled == 1))
366  {
367  mUpdateCallback(IARM_BUS_TR69HOSTIFMGR_EVENT_VALUECHANGED,(const char*)paramName, msgData.paramValue, msgData.paramtype);
368  }
369  }
370  }
371  }
372  }
373  }
374  }
375 #endif
376 
377  hostIf_DHCPv4Client::releaseLock();
378 
379 }
DHCPv4ClientReqHandler::unInit
virtual bool unInit()
This function is used to close all the instances of DHCPv4 client.
Definition: hostIf_DHCPv4Client_ReqHandler.cpp:81
IARM_BUS_TR69HOSTIFMGR_EVENT_REMOVE
@ IARM_BUS_TR69HOSTIFMGR_EVENT_REMOVE
Definition: hostIf_tr69ReqHandler.h:188
_HostIf_MsgData_t::instanceNum
short instanceNum
Definition: hostIf_tr69ReqHandler.h:176
IARM_BUS_TR69HOSTIFMGR_EVENT_ADD
@ IARM_BUS_TR69HOSTIFMGR_EVENT_ADD
Definition: hostIf_tr69ReqHandler.h:187
_HostIf_MsgData_t
Definition: hostIf_tr69ReqHandler.h:170
hostIf_DHCPv4Client
This class provides the interface for getting device DHCPv4 Client information.
Definition: Device_DHCPv4_Client.h:151
hostIf_main.h
hostIf_main API.
Device_DHCPv4_Client.h
The header file provides TR069 device DHCPv4 Client information APIs.
hostIf_DHCPv4Client::get_Device_DHCPv4_Client_DnsServer
int get_Device_DHCPv4_Client_DnsServer(HOSTIF_MsgData_t *stMsgData, bool *pChanged=NULL)
This gets the DNS server information used by the DHCP(Dynamic Host Configuration Protocol) v4 client ...
Definition: Device_DHCPv4_Client.cpp:200
_HostIf_MsgData_t::paramtype
HostIf_ParamType_t paramtype
Definition: hostIf_tr69ReqHandler.h:177
RDK_LOG
#define RDK_LOG
Definition: rdk_debug.h:258
_HostIf_MsgData_t::faultCode
faultCode_t faultCode
Definition: hostIf_tr69ReqHandler.h:179
hostIf_DHCPv4Client_ReqHandler.h
host Interface for DHCPV4 Client Request Handler.
_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
DHCPv4ClientReqHandler::handleSetMsg
virtual int handleSetMsg(HOSTIF_MsgData_t *stMsgData)
This function is used to handle the set message request of DHCPv4 Client. Currently not implemented.
Definition: hostIf_DHCPv4Client_ReqHandler.cpp:101
hostIf_DHCPv4Client::get_Device_DHCPv4_Client_InterfaceReference
int get_Device_DHCPv4_Client_InterfaceReference(HOSTIF_MsgData_t *stMsgData, bool *pChanged=NULL)
This gets the interface information used by the DHCP(Dynamic Host Configuration Protocol) v4 client d...
Definition: Device_DHCPv4_Client.cpp:167
DHCPv4ClientReqHandler
This class provides the interface for request handler operations for the DHCPV4 Client.
Definition: hostIf_DHCPv4Client_ReqHandler.h:60
hostIf_DHCPv4Client::get_Device_DHCPv4_Client_IPRouters
int get_Device_DHCPv4_Client_IPRouters(HOSTIF_MsgData_t *stMsgData, bool *pChanged=NULL)
This gets the IP Routers information used by the DHCP(Dynamic Host Configuration Protocol) v4 client ...
Definition: Device_DHCPv4_Client.cpp:233
msgHandler
Definition: hostIf_msgHandler.h:103
DHCPv4ClientReqHandler::init
virtual bool init()
This function is used to initialize. Currently not implemented.
Definition: hostIf_DHCPv4Client_ReqHandler.cpp:67
put_int
void put_int(char *ptr, int val)
This function converts the input data to integer type.
Definition: hostIf_utils.cpp:152
DHCPv4ClientReqHandler::handleGetMsg
virtual int handleGetMsg(HOSTIF_MsgData_t *stMsgData)
This function is used to handle the get message request of DHCPv4 Client information such as "Interfa...
Definition: hostIf_DHCPv4Client_ReqHandler.cpp:123
IARM_BUS_TR69HOSTIFMGR_EVENT_VALUECHANGED
@ IARM_BUS_TR69HOSTIFMGR_EVENT_VALUECHANGED
Definition: hostIf_tr69ReqHandler.h:189