RDK Documentation (Open Sourced RDK Components)
webcfg_utils.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  * @file webcfg_utils.cpp
21  *
22  * @description This file describes the utility functions for webconfig module
23  *
24  */
25 
26 #include <webcfg_utils.h>
27 #include <stdlib.h>
28 #include "safec_lib.h"
29 #include "XrdkCentralComBSStore.h"
30 
31 static XBSStore *m_bsStore;
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #include <webcfg_generic.h>
36 
37 #define UNUSED(x) (void )(x)
38 #define RET_SUCCESS 100
39 #define RET_FAILURE 102
40 
41 static char fw_version[64] = {0};
42 static char model_name[64] = {0};
43 static char device_mac[64] = {0};
44 static char force_sync[256] = {0};
45 static char force_sync_transID[256] = {0};
46 
47 pthread_cond_t *get_global_sync_condition(void);
48 pthread_mutex_t *get_global_sync_mutex(void);
49 void initWebConfigMultipartTask(unsigned long status);
50 
51 #define TR181_CMD "tr181 -s %s -v %s "
52 
53 static int getKeyVal(char *key, char *value)
54 {
55  FILE *fp = fopen(DEV_DETAILS_FILE, "r");
56  int i,j;
57  int rval = ERR;
58  if (NULL != fp)
59  {
60  char appendkey[] = "=";
61  char str[512] = {'\0'};
62  char searchkey[64] = {'\0'};
63 
64  strncpy(searchkey, key, strlen(key));
65  strcat(searchkey, appendkey);
66  while(fscanf(fp,"%s", str) != EOF)
67  {
68  char *searchVal = NULL;
69  if(searchVal = strstr(str, searchkey))
70  {
71  searchVal = searchVal + strlen(searchkey);
72  strncpy(value, searchVal, (strlen(str) - strlen(searchkey))+1);
73  rval = SUCCESS;
74  }
75  }
76  fclose(fp);
77  for (i=j=0; i<strlen(value); i++)
78  {
79  if (value[i] != ':')
80  value[j++] = value[i];
81  }
82  value[j] = '\0';
83 
84  for(i=0; i<=strlen(value); i++) {
85  if(value[i]>=65&&value[i]<=90)
86  value[i]=value[i]+32;
87  }
88  }
89  if((strlen(value))<1)/*It means only key is populated in the file & value is yet to be*/
90  {
91  rval = ERR;
92  }
93  RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,"webcfg: [%s] %d key = %s, value = %s rval= %d\n\n", __FUNCTION__, __LINE__, key, value, rval);
94  return rval;
95 }
96 
97 char* get_deviceMAC(void)
98 {
99  if(0 == device_mac[0])
100  getKeyVal( (char*)"estb_mac", device_mac);
101  RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,"webcfg: [%s] %d %s \n", __FUNCTION__, __LINE__, device_mac);
102  return device_mac;
103 }
104 
105 int Get_Webconfig_URL( char *pString)
106 {
107  errno_t rc = -1;
108  HOSTIF_MsgData_t stRfcData = {0};
109 
110  rc = strcpy_s(stRfcData.paramName, strlen("Device.X_RDK_WebConfig.URL")+1, "Device.X_RDK_WebConfig.URL");
111  if(rc != EOK)
112  {
113  RDK_LOG(RDK_LOG_ERROR, LOG_TR69HOSTIF,"[%s:%d]webcfg: Failed to copy.\n", __FUNCTION__, __LINE__);
114  ERR_CHK(rc);
115  return rc;
116  }
117 
118  m_bsStore = XBSStore::getInstance();
119  if (fcNoFault != m_bsStore->getValue(&stRfcData) ) {
120  RDK_LOG(RDK_LOG_ERROR, LOG_TR69HOSTIF,"[%s:%d]webcfg: Failed to get RFC Value.\n", __FUNCTION__, __LINE__);
121  return rc;
122  }
123 
124  if(stRfcData.paramValue[0] == '\0') {
125  strcpy_s(pString,strlen(BLE_DETECTION_WEBCFG_ENDPOINT)+1,BLE_DETECTION_WEBCFG_ENDPOINT);
126  RDK_LOG(RDK_LOG_ERROR, LOG_TR69HOSTIF,"[%s:%d]webcfg: Empty [%s] Value, so using default URL [%s]\n", __FUNCTION__, __LINE__,stRfcData.paramName, pString );
127  return -1;
128  }
129 
130  RDK_LOG(RDK_LOG_ERROR, LOG_TR69HOSTIF,"[%s:%d]webcfg: The URL Value is \"%s\".\n", __FUNCTION__, __LINE__, stRfcData.paramValue);
131 
132  const char* url = stRfcData.paramValue;
133 
134  rc = strcpy_s(pString, strlen(url) + 1, url);
135  if(rc!=EOK) {
136  ERR_CHK(rc);
137  return -1;
138  }
139  else {
140  strncat(pString,(const char *)BLE_DETECTION_WEBCFG_SUFIX, strlen(BLE_DETECTION_WEBCFG_SUFIX)+1);
141  }
142 
143  if(strlen(pString) == 0) {
144  rc=strcpy_s(pString,strlen(BLE_DETECTION_WEBCFG_ENDPOINT)+1,BLE_DETECTION_WEBCFG_ENDPOINT);
145  }
146 
147  if(rc!=EOK) {
148  ERR_CHK(rc);
149  }
150 
151  RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,"[%s:%d] The \"Device.X_RDK_WebConfig.URL\" is [%s] \n", __FUNCTION__,__LINE__, pString);
152 
153  return 0;
154 }
155 
156 
157 char *getSerialNumber(void)
158 {
159  char *serialNumber = (char*)" ";
160  return strdup(serialNumber);
161 }
162 
163 char *getModelName(void)
164 {
165  char *device_model_name = NULL;
166  if(0 == model_name[0])
167  getKeyVal( (char *)"model_number", model_name);
168  device_model_name = strdup(model_name);
169  RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,"webcfg: [%s] %d %s \n", __FUNCTION__, __LINE__, device_model_name);
170  return device_model_name;
171 }
172 
173 char *getFirmwareVersion(void)
174 {
175  char *firmware_version = NULL;
176  if(0 == fw_version[0])
177  getKeyVal((char *)"imageVersion", fw_version);
178  firmware_version = strdup(fw_version);
179  RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,"webcfg: [%s] %d %s \n", __FUNCTION__, __LINE__, firmware_version);
180  return firmware_version;
181 }
182 
183 char *get_global_systemReadyTime(void)
184 {
185  return strdup(" ");
186 }
187 
188 char *getDeviceBootTime(void)
189 {
190  return strdup(" ");
191 }
192 
193 char *getProductClass(void)
194 {
195  return strdup(" ");
196 }
197 
198 char *getRebootReason(void)
199 {
200  return strdup(" ");
201 }
202 
203 void setValues(const param_t paramVal[], const unsigned int paramCount, const int setType, char *transactionId, money_trace_spans *timeSpan, WDMP_STATUS *retStatus, int *ccspStatus)
204 {
205  UNUSED(setType);
206  UNUSED(transactionId);
207  UNUSED(timeSpan);
208 
209  int ret = 0, count = 0, len = 0;
210  FILE *fp = NULL;
211  char *cmd = NULL;
212  for (count = 0; count < paramCount ; count++) {
213  len = strlen(TR181_CMD) + strlen(paramVal[count].name) + strlen(paramVal[count].value);
214  cmd = (char*)malloc(len);
215  if (cmd) {
216  memset(cmd,0,len);
217  sprintf(cmd, TR181_CMD, paramVal[count].name, paramVal[count].value);
218  RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,"webcfg: [%s] %d command to execute %s \n", __FUNCTION__, __LINE__, cmd);
219  fp = popen(cmd,"r");
220  free(cmd);
221  }
222  if(fp) {
223  if(-1 == fgetc(fp)) {
224  *ccspStatus = RET_SUCCESS;
225  *retStatus = WDMP_SUCCESS;
226  } else {
227  *ccspStatus = RET_FAILURE;
228  *retStatus = WDMP_FAILURE;
229  }
230  pclose(fp);
231  fp = NULL;
232  } else {
233  *ccspStatus = RET_FAILURE;
234  *retStatus = WDMP_FAILURE;
235  }
236  if(WDMP_SUCCESS != *retStatus)
237  break;
238  }
239 
240  return;
241 }
242 
243 int setForceSync(char* pString, char *transactionId,int *pStatus)
244 {
245  errno_t rc = -1;
246  memset(force_sync,0,sizeof(force_sync));
247  rc=strcpy_s(force_sync,sizeof(force_sync),pString);
248  if(rc!=EOK)
249  {
250  ERR_CHK(rc);
251  }
252  if((force_sync[0] !='\0') && (strlen(force_sync)>0)) {
253  if(strlen(force_sync_transID)>0) {
254  RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,"webcfg: [%s] %d Force sync is already in progress, Ignoring this request.\n",__FUNCTION__, __LINE__);
255  *pStatus = 1;
256  return 0;
257  } else {
258  pthread_mutex_lock (get_global_sync_mutex());
259 
260  if(transactionId !=NULL && (strlen(transactionId)>0)) {
261  rc=strcpy_s(force_sync_transID,sizeof(force_sync_transID), transactionId);
262  if(rc!=EOK)
263  {
264  ERR_CHK(rc);
265  }
266 
267  }
268  pthread_cond_signal(get_global_sync_condition());
269  pthread_mutex_unlock(get_global_sync_mutex());
270  }
271  } else {
272  memset(force_sync_transID,0,sizeof(force_sync_transID));
273  }
274  return 1;
275 }
276 
277 int getForceSync(char** pString, char **transactionId )
278 {
279  if((force_sync[0] != '\0') && strlen(force_sync)>0) {
280  *pString = strdup(force_sync);
281  *transactionId = strdup(force_sync_transID);
282  } else {
283  *pString = NULL;
284  *transactionId = NULL;
285  return 0;
286  }
287  return 1;
288 }
289 
290 }
291 #endif
XBSStore
Definition: XrdkCentralComBSStore.h:33
_HostIf_MsgData_t
Definition: hostIf_tr69ReqHandler.h:170
RDK_LOG
#define RDK_LOG
Definition: rdk_debug.h:258
_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
setValues
void setValues(const ParamVal paramVal[], const unsigned int paramCount, const WEBPA_SET_TYPE setType, money_trace_spans *timeSpan, WDMP_STATUS **retStatus, char *transaction_id)
setValues interface sets the parameter value.
Definition: webpa_parameter.cpp:95