RDK Documentation (Open Sourced RDK Components)
hostIf_SNMPClient_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 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  * @file hostIf_SNMPClient_ReqHandler.cpp
22  * @brief The header file provides HostIf SNMP client request handler information APIs.
23  */
24 
26 #include "snmpAdapter.h"
27 #include "safec_lib.h"
28 
29 SNMPClientReqHandler* SNMPClientReqHandler::pInstance = NULL;
30 updateCallback SNMPClientReqHandler::mUpdateCallback = NULL;
31 
32 msgHandler* SNMPClientReqHandler::getInstance()
33 {
34  if(!pInstance)
35  pInstance = new SNMPClientReqHandler();
36 
37  return pInstance;
38 }
39 
40 
41 /**
42  * @brief This function is used to initialize all the current process and processor
43  * to '0' using memset.
44  *
45  * @return Returns the status of the operation.
46  *
47  * @retval true if initialization is successful.
48  * @retval false if initialization is not successful.
49  * @ingroup TR-181 HOSTIF_SNMPCLIENT_REQHANDLER_CLASSES
50  */
52 {
54  return true;
55 }
56 
57 /**
58  * @brief This function is used to close all the instances of SNMP.
59  *
60  * @return Returns the status of the operation.
61  *
62  * @retval true if it successfully close all the instances.
63  * @retval false if not able to close all the instances.
64  * @ingroup TR-181 HOSTIF_SNMPCLIENT_REQHANDLER_CLASSES
65  */
67 {
69  RDK_LOG(RDK_LOG_DEBUG,LOG_TR69HOSTIF,"[%s()] SNMP manager DeInitializing\n", __FUNCTION__);
70  return true;
71 }
72 
73 /**
74  * @brief This function is used to set the SNMP set.
75  *
76  * @param[out] stMsgData TR-181 Host interface message request.
77  *
78  * @return Returns the status of the operation.
79  *
80  * @retval OK if it is successful.
81  * @retval ERR_INTERNAL_ERROR if not able to set the SNMP command.
82  */
84 {
85  int ret = NOT_HANDLED;
86  const char *pSetting;
87  int instanceNumber;
88  hostIf_snmpAdapter::getLock();
89 
90  RDK_LOG(RDK_LOG_TRACE1,LOG_TR69HOSTIF,"[%s:%s:%d] Found string as %s\n", __FUNCTION__, __FILE__, __LINE__, stMsgData->paramName);
91 
92  if(strncasecmp(stMsgData->paramName,"Device.X_RDKCENTRAL-COM_DocsIf",strlen("Device.X_RDKCENTRAL-COM_DocsIf"))==0)
93  {
94  hostIf_snmpAdapter *pIfaceStatus = hostIf_snmpAdapter::getInstance(instanceNumber);
95  stMsgData->instanceNum = instanceNumber;
96  if(pIfaceStatus)
97  {
98  if(strcasecmp(stMsgData->paramName,"Device.X_RDKCENTRAL-COM_DocsIf.docsIfCmStatusTxPower")==0)
99  ret = pIfaceStatus->set_ValueToSNMPAdapter(stMsgData);
100  }
101  else
102  {
103  ret = NOK;
104  RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,"[%s:%s:%d] hostIf_snmpAdapter::getInstance is NULL for %s\n", __FUNCTION__, __FILE__, __LINE__, stMsgData->paramName);
105  }
106  }
107  else
108  {
109  ret = NOK;
110  stMsgData->faultCode = fcAttemptToSetaNonWritableParameter;
111  RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,"[%s] Failed, since Attempt To Set a NonWritable Parameter \"%s\"\n", __FUNCTION__, stMsgData->paramName);
112  }
113  hostIf_snmpAdapter::releaseLock();
114  return ret;
115 }
116 
117 /**
118  * @brief This function is used to set the SNMP get.
119  *
120  * @param[out] stMsgData TR-181 Host interface message request.
121  *
122  * @return Returns the status of the operation.
123  *
124  * @retval OK if it is gets the data successfully.
125  * @retval ERR_INTERNAL_ERROR if not able to get the data from the device.
126  * @ingroup TR-181 HOSTIF_DEVICECLIENT_REQHANDLER_CLASSES
127  */
129 {
130  int ret = NOT_HANDLED;
131  const char *pSetting;
132  int instanceNumber = 0;
133  hostIf_snmpAdapter::getLock();
134 
135  RDK_LOG(RDK_LOG_TRACE1,LOG_TR69HOSTIF,"[%s:%s:%d] Found string as %s\n", __FUNCTION__, __FILE__, __LINE__, stMsgData->paramName);
136  if(strncasecmp(stMsgData->paramName,"Device.X_RDKCENTRAL-COM_DocsIf",strlen("Device.X_RDKCENTRAL-COM_DocsIf"))==0 || strncasecmp(stMsgData->paramName,"Device.DeviceInfo.X_RDK_SNMP",strlen("Device.DeviceInfo.X_RDK_SNMP"))==0)
137  {
138  hostIf_snmpAdapter *pIfaceStatus = hostIf_snmpAdapter::getInstance(instanceNumber);
139  stMsgData->instanceNum = instanceNumber;
140 
141  if(pIfaceStatus)
142  ret = pIfaceStatus->get_ValueFromSNMPAdapter(stMsgData);
143  else
144  {
145  ret = NOK;
146  RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,"[%s:%s:%d] hostIf_snmpAdapter::getInstance is NULL for %s\n", __FUNCTION__, __FILE__, __LINE__, stMsgData->paramName);
147  }
148  }
149  else
150  {
151  stMsgData->faultCode = fcInvalidParameterName;
152  }
153  hostIf_snmpAdapter::releaseLock();
154  return ret;
155 }
156 
157 int SNMPClientReqHandler::handleGetAttributesMsg(HOSTIF_MsgData_t *stMsgData)
158 {
159  int ret = NOT_HANDLED;
160  int instanceNumber = 0;
161 
162  hostIf_snmpAdapter::getLock();
163  stMsgData->instanceNum = instanceNumber;
164  hostIf_snmpAdapter *pIface = hostIf_snmpAdapter::getInstance(instanceNumber);
165  if(pIface)
166  {
167  GHashTable* notifyhash = pIface->getNotifyHash();
168  if(notifyhash != NULL)
169  {
170  int* notifyvalue = (int*) g_hash_table_lookup(notifyhash,stMsgData->paramName);
171  put_int(stMsgData->paramValue, *notifyvalue);
172  stMsgData->paramtype = hostIf_IntegerType;
173  ret = OK;
174  }
175  else
176  {
177  ret = NOK;
178  RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,"[%s:%s:%d] Not able to get notifyhash %s\n", __FUNCTION__, __FILE__, __LINE__, stMsgData->paramName);
179  }
180  }
181  else
182  {
183  ret = NOK;
184  RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,"[%s:%s:%d] hostIf_snmpAdapter::getInstance is NULL for %s\n", __FUNCTION__, __FILE__, __LINE__, stMsgData->paramName);
185  }
186 
187  hostIf_snmpAdapter::releaseLock();
188  return ret;
189 }
190 
191 int SNMPClientReqHandler::handleSetAttributesMsg(HOSTIF_MsgData_t *stMsgData)
192 {
193  int ret = NOT_HANDLED;
194  int instanceNumber = 0;
195  const char *pSetting;
196  hostIf_snmpAdapter::getLock();
197  hostIf_snmpAdapter *pIface = hostIf_snmpAdapter::getInstance(instanceNumber);
198 
199  stMsgData->instanceNum = instanceNumber;
200  if(!pIface)
201  {
202  hostIf_snmpAdapter::releaseLock();
203  return NOK;
204  }
205  GHashTable* notifyhash = pIface->getNotifyHash();
206  if(notifyhash != NULL)
207  {
208  int *notifyValuePtr;
209  notifyValuePtr = (int*) malloc(1 * sizeof(int));
210 
211  // Inserting Notification parameter to Notify Hash Table,
212  // Note that neither keys nor values are copied when inserted into the GHashTable, so they must exist for the lifetime of the GHashTable
213  // There for allocating a memory for both Param name and param value. This should be freed whenever we disable Notification.
214  char *notifyKey;
215  notifyKey = (char*) malloc(sizeof(char)*strlen(stMsgData->paramName)+1);
216  if(NULL != notifyValuePtr)
217  {
218  *notifyValuePtr = 1;
219  errno_t rc = -1;
220  rc=strcpy_s(notifyKey,strlen(stMsgData->paramName)+1,stMsgData->paramName);
221  if(rc!=EOK)
222  {
223  ERR_CHK(rc);
224  }
225  g_hash_table_insert(notifyhash,notifyKey,notifyValuePtr);
226  ret = OK;
227  }
228  else
229  {
230  ret = NOK;
231  RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,"[%s:%s:%d] Not able to allocate Notify pointer %s\n", __FUNCTION__, __FILE__, __LINE__, stMsgData->paramName);
232  }
233  }
234  else
235  {
236  ret = NOK;
237  RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,"[%s:%s:%d] Not able to get notifyhash %s\n", __FUNCTION__, __FILE__, __LINE__, stMsgData->paramName);
238  }
239  hostIf_snmpAdapter::releaseLock();
240  return ret;
241 }
242 
243 void SNMPClientReqHandler::registerUpdateCallback(updateCallback cb)
244 {
245  mUpdateCallback = cb;
246 }
247 
248 /** @} */
249 /** @} */
SNMPClientReqHandler::handleGetMsg
virtual int handleGetMsg(HOSTIF_MsgData_t *stMsgData)
This function is used to set the SNMP get.
Definition: hostIf_SNMPClient_ReqHandler.cpp:128
_HostIf_MsgData_t::instanceNum
short instanceNum
Definition: hostIf_tr69ReqHandler.h:176
hostIf_SNMPClient_ReqHandler.h
The header file provides HostIf SNMP client request handler information APIs.
SNMPClientReqHandler::init
virtual bool init()
This function is used to initialize all the current process and processor to '0' using memset.
Definition: hostIf_SNMPClient_ReqHandler.cpp:51
hostIf_snmpAdapter
This class provides the interface for getting device information.
Definition: snmpAdapter.h:94
SNMPClientReqHandler::unInit
virtual bool unInit()
This function is used to close all the instances of SNMP.
Definition: hostIf_SNMPClient_ReqHandler.cpp:66
_HostIf_MsgData_t
Definition: hostIf_tr69ReqHandler.h:170
hostIf_snmpAdapter::get_ValueFromSNMPAdapter
int get_ValueFromSNMPAdapter(HOSTIF_MsgData_t *)
This function fetch the SNMP OID for the corresponding TR181 param, and run the snmpget command with ...
Definition: snmpAdapter.cpp:235
hostIf_snmpAdapter::init
static void init(void)
This function opens the RF_DocsIf_tr181_snmp map file, parse the TR181 parameter and its correspoindi...
Definition: snmpAdapter.cpp:102
hostIf_snmpAdapter::set_ValueToSNMPAdapter
int set_ValueToSNMPAdapter(HOSTIF_MsgData_t *)
This function fetch the SNMP OID for the corresponding TR181 param, and run the snmpset command with ...
Definition: snmpAdapter.cpp:309
snmpAdapter.h
The header file provides TR181 SNMP device RDK Central APIs.
_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_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
SNMPClientReqHandler::handleSetMsg
virtual int handleSetMsg(HOSTIF_MsgData_t *stMsgData)
This function is used to set the SNMP set.
Definition: hostIf_SNMPClient_ReqHandler.cpp:83
msgHandler
Definition: hostIf_msgHandler.h:103
hostIf_snmpAdapter::unInit
static void unInit(void)
This function clear the TR181 OID map container.
Definition: snmpAdapter.cpp:150
SNMPClientReqHandler
This class provides the interface for getting SNMP client request handler information.
Definition: hostIf_SNMPClient_ReqHandler.h:84
put_int
void put_int(char *ptr, int val)
This function converts the input data to integer type.
Definition: hostIf_utils.cpp:152