35 #include "hostIf_utils.h"
36 #include "safec_lib.h"
39 #if defined (RDK_DEVICE_CISCO_XI4) || defined (RDK_DEVICE_EMU)
40 #define INTERFACE_ETH "eth0"
42 #define INTERFACE_ETH "eth1"
45 static bool gAcsConnStatus =
false;
46 static bool gGatewayConnStatus =
false;
47 #ifndef NEW_HTTP_SERVER_DISABLE
48 static bool legacyRFC =
false;
51 const char* ntp_time_received_file=
"/tmp/timeReceivedNTP";
52 const char *webpa_start_tm_file =
"/tmp/webpa/start_time";
54 EntryExitLogger::EntryExitLogger (
const char* func,
const char* file) :
55 func (func), file (file)
57 RDK_LOG (RDK_LOG_TRACE1, LOG_TR69HOSTIF,
"Entry: %s [%s]\n", func, file);
60 EntryExitLogger::~EntryExitLogger ()
62 RDK_LOG (RDK_LOG_TRACE1, LOG_TR69HOSTIF,
"Exit: %s [%s]\n", func, file);
67 const char * getStringFromEnum(
EnumStringMapper stbMapperArray[],
int stbMapperArraySize,
int enumCode )
70 for ( i = 0; i != stbMapperArraySize; i++ )
72 if ( stbMapperArray[i].enumCode == enumCode )
74 return stbMapperArray[i].enumString;
81 int getEnumFromString(
EnumStringMapper stbMapperArray[],
int stbMapperArraySize,
const char * inputStr )
84 for ( i = 0; i != stbMapperArraySize; i++ )
86 if (!strncasecmp(stbMapperArray[i].enumString, inputStr, strlen(inputStr)))
88 return stbMapperArray[i].enumCode;
95 #define MAX_NUM_LEN 10
96 bool matchComponent(
const char* pParam,
const char *pKey,
const char **pSetting,
int &instanceNo)
99 int str_len = strlen(pKey);
100 ret = (strncasecmp(pParam,pKey,str_len)==0?
true:
false);
106 if((pParam[str_len] ==
'.') &&
107 (tmp_ptr = strchr(pParam+str_len+1,
'.')) &&
108 (tmp_len = tmp_ptr - (pParam + str_len + 1)) < MAX_NUM_LEN)
110 char tmp_buff[MAX_NUM_LEN];
111 memset(tmp_buff,0,MAX_NUM_LEN);
112 strncpy(tmp_buff,pParam+str_len+1,tmp_len);
113 instanceNo = atoi(tmp_buff);
114 *pSetting = (tmp_ptr + 1);
124 std::string int_to_string(
int d)
127 memset(tmp_buff,0,10);
128 sprintf(tmp_buff,
"%d",d);
129 return std::string(tmp_buff);
132 std::string uint_to_string(uint d)
134 std::stringstream ss;
139 std::string ulong_to_string(
unsigned long d)
141 std::stringstream ss;
146 int get_int(
const char* ptr)
148 int *ret = (
int *)ptr;
154 int *tmp = (
int *)ptr;
158 uint get_uint(
char *ptr)
160 uint *ret = (uint *)ptr;
164 void put_uint(
char *ptr, uint val)
166 uint *tmp = (uint *)ptr;
172 int get_ulong(
const char* ptr)
174 unsigned long *ret = (
unsigned long *)ptr;
178 void put_ulong(
char *ptr,
unsigned long val)
180 unsigned long *tmp = (
unsigned long *)ptr;
185 bool get_boolean(
const char *ptr)
187 bool *ret = (
bool *)ptr;
193 bool *tmp = (
bool *)ptr;
197 std::string bool_to_string(
bool value)
201 }
else if (value ==
false) {
207 int string_to_int(
const char *value)
210 long ret = strtol(value, &end, 10);
214 uint string_to_uint(
const char *value)
217 unsigned long ret = strtoul(value, &end, 10);
221 unsigned long string_to_ulong(
const char *value)
224 unsigned long ret = strtoul(value, &end, 10);
228 bool string_to_bool(
const char *value)
230 bool ret = (strcmp(value,
"true") == 0) ?
true :
false;
237 case hostIf_StringType:
239 case hostIf_IntegerType:
240 return int_to_string(get_int(stMsgData->
paramValue));
241 case hostIf_UnsignedIntType:
242 return uint_to_string(get_uint(stMsgData->
paramValue));
243 case hostIf_BooleanType:
244 return bool_to_string(get_boolean(stMsgData->
paramValue));
245 case hostIf_UnsignedLongType:
246 return ulong_to_string(get_ulong(stMsgData->
paramValue));
247 case hostIf_DateTimeType:
258 memset(stMsgData->
paramValue, 0, TR69HOSTIFMGR_MAX_PARAM_LEN);
261 case hostIf_StringType:
268 stMsgData->
paramLen = strlen(value.c_str());
271 case hostIf_IntegerType:
274 case hostIf_UnsignedIntType:
275 put_uint(stMsgData->
paramValue, string_to_uint(value.c_str()));
277 case hostIf_BooleanType:
280 case hostIf_UnsignedLongType:
281 put_ulong(stMsgData->
paramValue, string_to_ulong(value.c_str()));
283 case hostIf_DateTimeType:
290 void setResetState( eSTBResetState rebootFlag)
292 gResetState = rebootFlag;
295 eSTBResetState getResetState(
void )
300 void triggerResetScript()
303 char scriptbuff[100] = {
'\0'};
305 switch (gResetState) {
308 sprintf(scriptbuff,
"%s %s/%s",
"sh", SCR_PATH,
"coldfactory-reset.sh");
309 RDK_LOG(RDK_LOG_INFO,LOG_TR69HOSTIF,
"[%s] Executing : %s \n",__FUNCTION__, scriptbuff);
312 ret = system(scriptbuff);
313 if (WEXITSTATUS(ret) != 0 )
315 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"[%s] Failed to execute : %s. \n",__FUNCTION__, scriptbuff);
318 RDK_LOG(RDK_LOG_INFO,LOG_TR69HOSTIF,
"[%s] Successfully executed %s Reset. \n",__FUNCTION__, scriptbuff);
325 sprintf(scriptbuff,
"%s %s/%s",
"sh", SCR_PATH,
"factory-reset.sh");
327 RDK_LOG(RDK_LOG_INFO,LOG_TR69HOSTIF,
"[%s] Executing : %s \n",__FUNCTION__, scriptbuff);
330 ret = system(scriptbuff);
331 if (WEXITSTATUS(ret) != 0 )
333 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"[%s] Failed to execute : %s. \n",__FUNCTION__, scriptbuff);
336 RDK_LOG(RDK_LOG_INFO,LOG_TR69HOSTIF,
"[%s] Successfully executed %s Reset. \n",__FUNCTION__, scriptbuff);
342 sprintf(scriptbuff,
"%s %s/%s",
"sh", SCR_PATH,
"warehouse-reset.sh");
343 RDK_LOG(RDK_LOG_INFO,LOG_TR69HOSTIF,
"[%s] Executing : %s \n",__FUNCTION__, scriptbuff);
346 ret = system(scriptbuff);
347 if (WEXITSTATUS(ret) != 0 )
349 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"[%s] Failed to execute: %s. \n",__FUNCTION__, scriptbuff);
352 RDK_LOG(RDK_LOG_INFO,LOG_TR69HOSTIF,
"[%s] Successfully executed %s Reset. \n",__FUNCTION__, scriptbuff);
357 sprintf(scriptbuff,
"%s %s/%s",
"sh", SCR_PATH,
"customer-reset.sh");
358 RDK_LOG(RDK_LOG_INFO,LOG_TR69HOSTIF,
"[%s] Executing : %s \n",__FUNCTION__, scriptbuff);
361 ret = system(scriptbuff);
362 if (WEXITSTATUS(ret) != 0 )
364 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"[%s] Failed to execute: %s. \n",__FUNCTION__, scriptbuff);
367 RDK_LOG(RDK_LOG_INFO,LOG_TR69HOSTIF,
"[%s] Successfully executed %s Reset. \n",__FUNCTION__, scriptbuff);
372 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"[%s] Invalid input for reset \n",__FUNCTION__);
379 void set_ACSStatus(
bool enabled)
381 gAcsConnStatus = enabled;
386 return gAcsConnStatus;
389 void set_GatewayConnStatus(
bool enabled)
391 gGatewayConnStatus = enabled;
394 bool get_GatewayConnStatus()
396 return gGatewayConnStatus;
406 char* value = getenv (name);
407 return value ? value : defaultValue;
410 int read_command_output (
char* cmd,
char* resultBuff,
int length)
412 FILE* fp = popen (cmd,
"r");
415 RDK_LOG (RDK_LOG_ERROR, LOG_TR69HOSTIF,
"[%s]: cannot run command [%s]\n", __FUNCTION__, cmd);
418 if (fgets (resultBuff, length, fp) == NULL)
420 RDK_LOG (RDK_LOG_ERROR, LOG_TR69HOSTIF,
"[%s]: cannot read output from command [%s]\n", __FUNCTION__, cmd);
426 RDK_LOG (RDK_LOG_DEBUG, LOG_TR69HOSTIF,
"[%s]: command [%s] returned [%s]\n", __FUNCTION__, cmd, resultBuff);
438 char buffer[BUFF_LENGTH_1024];
440 memset(buffer, 0,
sizeof(buffer));
441 consoleString.clear();
442 stream = popen(cmd,
"r");
443 if (stream == NULL) {
448 while (!feof(stream))
450 if (fgets(buffer, BUFF_LENGTH_1024, stream) != NULL)
452 consoleString.append(buffer);
460 #ifndef NEW_HTTP_SERVER_DISABLE
468 clock_gettime(CLOCK_REALTIME, timer);
472 long timeValDiff(
struct timespec *starttime,
struct timespec *finishtime)
475 msec=(finishtime->tv_sec-starttime->tv_sec)*1000;
476 msec+=(finishtime->tv_nsec-starttime->tv_nsec)/1000000;
480 void setLegacyRFCEnabled(
bool value)
485 bool legacyRFCEnabled()
496 if (strcasecmp(bsUpdate,
"allUpdate") == 0)
497 return HOSTIF_SRC_ALL;
498 else if (strcasecmp(bsUpdate,
"rfcUpdate") == 0)
499 return HOSTIF_SRC_RFC;
500 else if (strcasecmp(bsUpdate,
"default") == 0)
501 return HOSTIF_SRC_DEFAULT;
507 return ((access(
"/tmp/webpa/start_time", F_OK) == 0)?
true:
false);
510 bool isNtpTimeFilePresent()
512 return ((access(ntp_time_received_file, F_OK) == 0)?
true:
false);
515 unsigned long get_system_manageble_ntp_time()
517 unsigned long ret = 0;
518 FILE* fp = fopen(ntp_time_received_file,
"r");
527 if((getline(&line, &len, fp)) != -1) {
528 RDK_LOG (RDK_LOG_ERROR, LOG_TR69HOSTIF,
"[%s] Date : \"%s\".\n", line);
537 s_time = strptime(line,
"%a %b %d %H:%M:%S %Z %Y", &tm);
539 if (NULL != s_time ) {
541 ret = (
unsigned long)epoch;
544 RDK_LOG (RDK_LOG_ERROR, LOG_TR69HOSTIF,
"[%s] Failed to parse NTP Date Format from \'%f\' file.\n ", __FUNCTION__, ntp_time_received_file);
546 if (line) free(line);
550 unsigned long get_device_manageble_time()
552 unsigned long epoch_time = 0;
557 if(epoch_time || counter++ >=5 )
559 if ((fptr = fopen(webpa_start_tm_file,
"r")) != NULL) {
560 fscanf(fptr,
"%ld", &epoch_time);
561 RDK_LOG (RDK_LOG_INFO, LOG_TR69HOSTIF,
"Device Manageble Time =%ld\n", epoch_time);
567 while(epoch_time == 0);
573 std::string sToken =
"";
574 char pSecurityOutput[256] = {0};
576 FILE *pSecurity = popen(
"/usr/bin/WPEFrameworkSecurityUtility",
"r");
578 if (fgets(pSecurityOutput, 256, pSecurity) != NULL) {
579 cJSON* root = cJSON_Parse(pSecurityOutput);
581 cJSON *res = cJSON_GetObjectItem(root,
"success");
582 if(cJSON_IsTrue(res) == 1) {
583 cJSON* token = cJSON_GetObjectItem(root,
"token");
584 if (token != NULL && token->type == cJSON_String && token->valuestring != NULL) {
585 RDK_LOG (RDK_LOG_INFO, LOG_TR69HOSTIF,
"%s: Security Token retrieved successfully\n", __FUNCTION__);
586 sToken = token->valuestring;
590 RDK_LOG (RDK_LOG_ERROR, LOG_TR69HOSTIF,
"%s: Security Token retrieval failed!\n", __FUNCTION__);
595 RDK_LOG (RDK_LOG_ERROR, LOG_TR69HOSTIF,
"[%s] json parse error\n", __FUNCTION__);
596 if (NULL != pSecurity)
602 RDK_LOG (RDK_LOG_ERROR, LOG_TR69HOSTIF,
"%s: Failed to open security utility\n", __FUNCTION__);
604 if (NULL != pSecurity)