RDK Documentation (Open Sourced RDK Components)
hostIf_rbus_Dml_Provider.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 2019 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_msgHandler.cpp
22  * @brief The header file provides HostIf message handler information APIs.
23  */
24 
25 /**
26 * @defgroup tr69hostif
27 * @{
28 * @defgroup hostif
29 * @{
30 **/
31 
32 
33 /** @} */
34 /** @} */
35 
36 #include <stdlib.h>
37 #ifdef __cplusplus
38 extern "C"
39 {
40 #include "rbus.h"
41 #endif
42 #ifdef __cplusplus
43 }
44 #endif
45 
46 #include "hostIf_main.h"
47 #include "hostIf_utils.h"
48 #include "hostIf_tr69ReqHandler.h"
49 #include "hostIf_msgHandler.h"
50 #include "waldb.h"
51 //#include "rbus.h"
52 #include "exception.hpp"
53 #include "illegalArgumentException.hpp"
54 
55 
56 #define MAX_NUM_PARAMETERS 2048
57 #define MAX_PARAMETER_LENGTH 512
58 
59 static const char *EXCLUDE_PARAMLIST[] = { "Device.X_RDKCENTRAL-COM_T2.ReportProfiles", \
60  "Device.X_RDKCENTRAL-COM_T2.ReportProfilesMsgPack"
61  };
62 
63 #define EXCLUDE_LIST_SIZE (sizeof((EXCLUDE_PARAMLIST))/sizeof((EXCLUDE_PARAMLIST)[0]))
64 
65 rbusError_t TR_Dml_GetHandler(rbusHandle_t handle, rbusProperty_t property, rbusGetHandlerOptions_t* opts);
66 static rbusHandle_t rbusHandle = NULL;
67 
68 static bool isInExclusionList(const char* param) {
69  int i = 0 ;
70  bool isMatch = false ;
71  for (i = 0; i < EXCLUDE_LIST_SIZE ; i++) {
72  if (strncmp(EXCLUDE_PARAMLIST[i], param, MAX_PARAMETER_LENGTH) == 0) {
73  isMatch = true ;
74  break ;
75  }
76  }
77  return isMatch ;
78 }
79 
80 static void convert2hostIfDataType(char *ParamDataType, HostIf_ParamType_t* pParamType)
81 {
82  if(!strcmp(ParamDataType,"string"))
83  *pParamType = hostIf_StringType;
84  else if(!strcmp(ParamDataType,"unsignedInt"))
85  *pParamType = hostIf_UnsignedIntType;
86  else if(!strcmp(ParamDataType,"int"))
87  *pParamType = hostIf_IntegerType;
88  else if(!strcmp(ParamDataType,"unsignedLong"))
89  *pParamType = hostIf_UnsignedLongType;
90  else if(!strcmp(ParamDataType,"boolean"))
91  *pParamType = hostIf_BooleanType;
92  else if(!strcmp(ParamDataType,"hexBinary"))
93  *pParamType = hostIf_StringType;
94  else
95  *pParamType = hostIf_StringType;
96 }
97 
98 rbusError_t TR_Dml_GetHandler(rbusHandle_t handle, rbusProperty_t inProperty, rbusGetHandlerOptions_t* opts)
99 {
100  LOG_ENTRY_EXIT;
101 
102  rbusError_t rc = RBUS_ERROR_SUCCESS;
103 
104  const char *name = NULL;
105  rbusValue_t value;
106 
107  int status = NOK;
108  DataModelParam dmParam = {0};
109  void *dataBaseHandle = NULL;
110 
111  rbusValue_Init(&value);
112 
113  name = rbusProperty_GetName(inProperty);
114 
115  if(!name)
116  {
117  RDK_LOG (RDK_LOG_ERROR, LOG_TR69HOSTIF, "[%s][rbusdml] The parameter requested through rbus interface is invalid..!!! \n ", __FUNCTION__);
118  rc = RBUS_ERROR_INVALID_INPUT;
119  }
120  else
121  {
122  dataBaseHandle = getDataModelHandle();
123 
124  if (!dataBaseHandle)
125  {
126  RDK_LOG (RDK_LOG_ERROR, LOG_TR69HOSTIF, "[%s][rbusdml] Failed to initialized data base handle..!!! \n ", __FUNCTION__);
127  rc = RBUS_ERROR_DESTINATION_RESPONSE_FAILURE;
128  }
129  else
130  {
131  if (getParamInfoFromDataModel(dataBaseHandle, name, &dmParam) == 0)
132  {
133  RDK_LOG (RDK_LOG_ERROR, LOG_TR69HOSTIF, "[%s][rbusdml] Invalid Parameter [%d]. \n ", __FUNCTION__);
134  rc = RBUS_ERROR_INVALID_INPUT;
135  }
136  else
137  {
138  RDK_LOG (RDK_LOG_DEBUG, LOG_TR69HOSTIF, "[%s][rbusdml] Valid Parameter..! \n ", __FUNCTION__);
139 
140  HOSTIF_MsgData_t param = { 0 };
141  char *defaultValue = NULL;
142  param.reqType = HOSTIF_GET;
143  strncpy (param.paramName, name, TR69HOSTIFMGR_MAX_PARAM_LEN - 1);
144  convert2hostIfDataType(dmParam.dataType, &(param.paramtype));
145  if(dmParam.defaultValue)
146  {
147  defaultValue = strdup(dmParam.defaultValue);
148  }
149 
150  freeDataModelParam(dmParam);
151 
152  status = hostIf_GetMsgHandler(&param);
153 
154 
155  if(status == OK && param.faultCode == fcNoFault)
156  {
157  switch (param.paramtype)
158  {
159  case hostIf_IntegerType:
160  rbusValue_SetInt32(value, *((int *)param.paramValue));
161  break;
162  case hostIf_BooleanType:
163  rbusValue_SetBoolean(value, *((int *)param.paramValue));
164  break;
165  case hostIf_UnsignedIntType:
166  rbusValue_SetUInt32(value, *((unsigned int *)param.paramValue));
167  break;
168  case hostIf_UnsignedLongType:
169  rbusValue_SetUInt64(value, *((unsigned long long *)param.paramValue));
170  break;
171  case hostIf_StringType:
172  rbusValue_SetString(value, param.paramValue);
173  break;
174  default: // handle as string
175  rbusValue_SetString(value, param.paramValue);
176  break;
177  }
178  RDK_LOG (RDK_LOG_DEBUG, LOG_TR69HOSTIF, "[%s][rbusdml] Successfully return the get value for the Parameter [%s].\n", __FUNCTION__, name);
179  rbusProperty_SetValue(inProperty, value);
180  rc = RBUS_ERROR_SUCCESS;
181  }
182  else
183  {
184  if(defaultValue)
185  {
186  switch (param.paramtype)
187  {
188  case hostIf_IntegerType:
189  rbusValue_SetInt32(value, *((int *)defaultValue));
190  break;
191  case hostIf_BooleanType:
192  rbusValue_SetBoolean(value, *((int *)defaultValue));
193  break;
194  case hostIf_UnsignedIntType:
195  rbusValue_SetUInt32(value, *((unsigned int *)defaultValue));
196  break;
197  case hostIf_UnsignedLongType:
198  rbusValue_SetUInt64(value, *((unsigned long long *)defaultValue));
199  break;
200  case hostIf_StringType:
201  rbusValue_SetString(value, defaultValue);
202  break;
203  default: // handle as string
204  rbusValue_SetString(value, defaultValue);
205  break;
206  }
207  RDK_LOG (RDK_LOG_DEBUG, LOG_TR69HOSTIF, "[%s][rbusdml] Successfully return the get the default value for the Parameter [%s].\n", __FUNCTION__, name);
208  rbusProperty_SetValue(inProperty, value);
209  rc = RBUS_ERROR_SUCCESS;
210  }
211  else {
212  RDK_LOG (RDK_LOG_ERROR, LOG_TR69HOSTIF, "[%s][rbusdml] Get Parameter [%s] Invalid format to send across.\n", __FUNCTION__, name);
213  rc = RBUS_ERROR_BUS_ERROR;
214  }
215  }
216 
217  if(defaultValue)
218  {
219  free(defaultValue);
220  }
221  }
222  }
223  }
224 
225  rbusValue_Release(value);
226  return rc;
227 }
228 
229 
230 void init_rbus_dml_provider()
231 {
232 
233  LOG_ENTRY_EXIT;
234 
235  /* Check for rbus status */
236  rbusStatus_t rs;
237 
238  int max_retry = 6;
239  int retry_count = 0;
240 
241  while(true) {
242  rs = rbus_checkStatus();
243  if(rs == RBUS_ENABLED)
244  {
245  RDK_LOG (RDK_LOG_INFO, LOG_TR69HOSTIF, "[%s] [rbusdml] The rbus_checkStatus() is Enabled (%d). \n", __FUNCTION__, rs);
246  break;
247  }
248  else {
249  if(retry_count >= max_retry) {
250  RDK_LOG (RDK_LOG_ERROR, LOG_TR69HOSTIF, "[%s] [rbusdml] The rbus_checkStatus() is Not Enabled, exiting rbus support after max retry (%d). \n",__FUNCTION__, max_retry);
251  break;
252  }
253  RDK_LOG (RDK_LOG_ERROR, LOG_TR69HOSTIF, "[%s][rbusdml] The rbus_checkStatus() is Not Enabled, returned with status (%d), so retrying... \n", __FUNCTION__, rs);
254  sleep(5);
255  retry_count++;
256  }
257  }
258 
259  if(rs != RBUS_ENABLED) {
260  RDK_LOG (RDK_LOG_ERROR, LOG_TR69HOSTIF, "[%s][rbusdml] Failed to initialized, rbus_checkStatus() returned with status (%d). \n", __FUNCTION__, rs);
261  return;
262  }
263 
264  rbusError_t rc = RBUS_ERROR_SUCCESS;
265  rbusDataElement_t* dataElements = NULL;
266  int i = 0;
267  rc = rbus_open(&rbusHandle, "tr69hostif");
268  if(rc != RBUS_ERROR_SUCCESS)
269  {
270  RDK_LOG (RDK_LOG_ERROR, LOG_TR69HOSTIF, "consumer: rbus_open failed: %d\n", rc);
271  return ;
272  } //CID:161936 - checked return
273 
274  char **pParamNameList = (char**) calloc (MAX_NUM_PARAMETERS, sizeof(char*));
275  int num_of_params = 0;
276  int num_of_rbus_params = 0;
277  int rbus_param_counter = 0;
278 
279  try {
280 
281  /* Get the list of properties that are supported by Data Model Xml */
282  if(DB_SUCCESS != get_complete_param_list (pParamNameList, &num_of_params))
283  {
284  RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,"[%s][rbusdml] Failed to get complete parameter list. The number of parameter is :%d\n", __FUNCTION__, num_of_params);
285  }
286  else
287  {
288  RDK_LOG(RDK_LOG_INFO,LOG_TR69HOSTIF,"[%s:%d][rbusdml]Successfully get the complete parameter list, the Parameter counts is = %d\n", __FUNCTION__, __LINE__, num_of_params);
289 
290  num_of_rbus_params = num_of_params - EXCLUDE_LIST_SIZE ;
291  dataElements = (rbusDataElement_t*)calloc(num_of_rbus_params, sizeof(rbusDataElement_t));
292 
293  if(dataElements != NULL)
294  {
295  for(i=0; i< num_of_params; i++)
296  {
297  if(pParamNameList[i] != NULL) {
298  if(isInExclusionList(pParamNameList[i])) {
299  // Temporary solution to ignore externally managed TR181 parameters from rBus registration.
300  // This is to avoid recursive call of handlers.
301  RDK_LOG(RDK_LOG_DEBUG, LOG_TR69HOSTIF,"[%s] [rbusdml] Parameter %s is externally rbus registered, ignoring from datamodel list.\n", __FUNCTION__,pParamNameList[i]);
302  continue;
303  } else {
304  dataElements[rbus_param_counter].name = strdup(pParamNameList[i]);
305  if(strstr(pParamNameList[i],".{i}.")){
306  RDK_LOG(RDK_LOG_DEBUG, LOG_TR69HOSTIF,"[%s] [rbusdml] Parameter [%d] : [ %s ] registered as [RBUS_ELEMENT_TYPE_TABLE]\n", __FUNCTION__, i + 1,pParamNameList[i]);
307  dataElements[rbus_param_counter].type = RBUS_ELEMENT_TYPE_TABLE;
308  dataElements[rbus_param_counter].cbTable.getHandler = TR_Dml_GetHandler;
309  }
310  else {
311  RDK_LOG(RDK_LOG_DEBUG, LOG_TR69HOSTIF,"[%s] [rbusdml]Parameter [%d] : [ %s ] registered as [RBUS_ELEMENT_TYPE_PROPERTY]\n", __FUNCTION__, i + 1,pParamNameList[i]);
312  dataElements[rbus_param_counter].type = RBUS_ELEMENT_TYPE_PROPERTY;
313  dataElements[rbus_param_counter].cbTable.getHandler = TR_Dml_GetHandler;
314  }
315  rbus_param_counter++ ;
316  }
317  }
318  }
319 
320  rc = rbus_regDataElements(rbusHandle, rbus_param_counter, dataElements);
321 
322  if(rc != RBUS_ERROR_SUCCESS)
323  {
324  RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF, "[%s][rbusdml] rbusPropertyProvider_Register failed: %d\n", __FUNCTION__, rc);
325  free(dataElements);
326  rbus_close(rbusHandle);
327  }
328  else
329  {
330 
331  RDK_LOG(RDK_LOG_INFO,LOG_TR69HOSTIF,"[%s][rbusdml] rbus_regDataElements registered successfully.\n", __FUNCTION__);
332  free(dataElements);
333  }
334 
335  for (i=0; i< num_of_params; i++) {
336  if(pParamNameList[i]) free(pParamNameList[i]);
337  }
338 
339  }
340  }
341  }
342  catch (const std::exception e)
343  {
344  RDK_LOG(RDK_LOG_WARN,LOG_TR69HOSTIF,"[%s][rbusdml] Exception: %s\r\n",__FUNCTION__, e.what());
345  }
346  if(pParamNameList) free(pParamNameList); //CID:161937 - Resource leak
347 }
348 
349 
350 int setRbusStringParam(char *rbusParamName, char* rbusParamValue) {
351 
352  rbusValue_t value;
353  rbusSetOptions_t opts;
354  opts.commit = true;
355  int ret = RBUS_ERROR_SUCCESS;
356  if(!rbusHandle) {
357  RDK_LOG(RDK_LOG_INFO,LOG_TR69HOSTIF,"[%s][rbusdml] rbusHandle is NULL.\n", __FUNCTION__);
358  return NOK;
359  }
360 
361  rbusValue_Init(&value);
362  rbusValue_SetString(value, rbusParamValue);
363  //ret = rbus_set(rbusHandle, "Device.X_RDKCENTRAL-COM_T2.ReportProfiles", value, &opts);
364  ret = rbus_set(rbusHandle, rbusParamName, value, &opts);
365  if (ret != RBUS_ERROR_SUCCESS) {
366  RDK_LOG(RDK_LOG_INFO,LOG_TR69HOSTIF,"[%s][rbusdml] rbus_set Failed for [%s] with error [%d].\n", __FUNCTION__, rbusParamName, ret);
367  return NOT_HANDLED;
368  }
369  rbusValue_Release(value);
370 
371  return OK ;
372 
373 }
374 
375 int getRbusStringParam(char *rbusParamName, char **rbusParamValue) {
376 
377  rbusValue_t paramValue_t;
378  rbusValueType_t rbusValueType ;
379  int ret = RBUS_ERROR_SUCCESS ;
380  char *stringValue = NULL;
381  rbusSetOptions_t opts;
382  opts.commit = true;
383 
384  if(!rbusHandle) {
385  printf("rbusHandle is NULL\n");
386  return NOK;
387  }
388 
389  ret = rbus_get(rbusHandle, rbusParamName, &paramValue_t);
390  if(ret != RBUS_ERROR_SUCCESS) {
391  printf("Unable to get %s\n", rbusParamName);
392  return NOK;
393  }
394  stringValue = rbusValue_ToString(paramValue_t, NULL, 0);
395  if (stringValue != NULL)
396  *rbusParamValue = strdup(stringValue);
397 
398  rbusValue_Release(paramValue_t);
399  return OK ;
400 
401 }
__DataModelParam
Definition: waldb.h:39
_HostIf_MsgData_t::reqType
HostIf_ReqType_t reqType
Definition: hostIf_tr69ReqHandler.h:178
_HostIf_MsgData_t
Definition: hostIf_tr69ReqHandler.h:170
hostIf_main.h
hostIf_main API.
_HostIf_MsgData_t::paramtype
HostIf_ParamType_t paramtype
Definition: hostIf_tr69ReqHandler.h:177
RDK_LOG
#define RDK_LOG
Definition: rdk_debug.h:258
HostIf_ParamType_t
enum _HostIf_ParamType HostIf_ParamType_t
_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
hostIf_msgHandler.h
The header file provides HostIf message handler information APIs.