33 #include "webconfig_lite.h"
34 #include <curl/curl.h>
36 #include <uuid/uuid.h>
41 #include "safec_lib.h"
47 #include <wdmp_internal.h>
56 #define CURL_TIMEOUT_SEC 25L
57 #define CA_CERT_PATH "/etc/ssl/certs"
58 #define MAX_BUF_SIZE 256
59 #define MAX_HEADER_LEN 4096
60 #define MAX_PARAMETERNAME_LENGTH 512
61 #define WEBPA_READ_HEADER "/etc/parodus/parodus_read_file.sh"
62 #define WEBPA_CREATE_HEADER "/etc/parodus/parodus_create_file.sh"
63 #define BACKOFF_SLEEP_DELAY_SEC 20
64 #define BACKOFF_SLEEP_DELAY_5_SEC 5
65 #define ETAG_HEADER "ETag:"
66 #define BLE_DETECTION_WEBCONFIG_ENDPOINT "https://cpe-config.xdp.comcast.net/api/v1/device/{mac}/config/ble"
67 #define BLE_DETECTION_WEBCONFIG_SUFIX "/ble"
68 #define LOG_TR69HOSTIF "LOG.RDK.TR69HOSTIF"
69 #define CURL_FILE "/tmp/adzvfchig-res.mch"
70 #define CONFIG_VERSION_FILE "/opt/persistent/webconfig_Version"
71 #define MAX_UUID_SIZE 64
85 char * application_status;
86 int application_details;
87 char * request_timestamp;
89 char * transaction_uuid;
102 static char serialNum[64]= {
'\0'};
103 char webpa_auth_token[4096]= {
'\0'};
104 static char g_ETAG[64]= {
'\0'};
105 static char deviceMac[64] = {
'\0'};
106 static char *webconfigEndPoint=NULL;
111 static int processJsonDocument(
char *jsonData,
int *retStatus,
char **docVersion);
112 static int validateConfigFormat(cJSON *json,
char **eTag);
113 static int requestWebConfigData(
char **configData,
long *code,
char **transaction_id);
114 static void createCurlheader(
struct curl_slist *list,
struct curl_slist **header_list,
char ** trans_uuid);
115 static int parseJsonData(
char* jsonData,
char **version);
116 static size_t write_callback_fn(
void *buffer,
size_t size,
size_t nmemb,
struct token_data *data);
117 static void getAuthToken();
118 static void createNewAuthToken(
char *newToken,
size_t len,
char *hw_mac,
char* hw_serial_number);
119 static int handleHttpResponse(
long response_code,
char *webConfigData,
int retry_count,
char* transaction_uuid);
120 static char* generate_trans_uuid();
121 static void macToLowerCase(
char macValue[]);
123 static void addWebConfigNotifyMsg(
char *url,
long status_code,
char *application_status,
int application_details,
char *request_timestamp,
char *version,
char *transaction_uuid);
125 static char *replaceMacWord(
const char *s,
const char *macW,
const char *deviceMACW);
126 static void processWebconfigSync();
127 static size_t header_callback(
char *buffer,
size_t size,
size_t nitems);
128 static void stripSpaces(
char *str,
char **final_str);
134 void parse_set_request(cJSON *request)
136 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:Inside parse set Request \n");
137 cJSON *reqParamObj = NULL,*paramArray = NULL;
138 size_t paramCount, i;
140 paramArray = cJSON_GetObjectItem(request,
"parameters");
142 paramCount = cJSON_GetArraySize(paramArray);
145 for (i = 0; i < paramCount; i++)
147 reqParamObj = cJSON_GetArrayItem(paramArray, i);
149 if(cJSON_GetObjectItem(reqParamObj,
"name") != NULL)
151 tr181data.name = cJSON_GetObjectItem(reqParamObj,
"name")->valuestring;
152 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:The Name = %s\n",tr181data.name.c_str());
156 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:The Name value is NULL \n");
159 if (cJSON_GetObjectItem(reqParamObj,
"value") != NULL )
161 if(cJSON_GetObjectItem(reqParamObj,
"value")->valuestring != NULL)
163 tr181data.value = cJSON_GetObjectItem(reqParamObj,
"value")->valuestring;
164 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:The param value =%s \n",tr181data.value.c_str());
168 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:Parameter value field is not a string \n");
172 if (cJSON_GetObjectItem(reqParamObj,
"dataType") != NULL)
174 tr181data.datatype = cJSON_GetObjectItem(reqParamObj,
"dataType")->valueint;
175 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:The data Type = %d \n", tr181data.datatype);
182 void * initWebConfigTask(
void *)
184 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite: [%s] %d **** Entering webconfig task thread ****** \n", __FUNCTION__, __LINE__);
186 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:**** calling processWebconfigSync ****** \n");
188 processWebconfigSync();
190 if(webconfigEndPoint != NULL)
192 WAL_FREE(webconfigEndPoint);
194 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:**** End of webconfig task thread ****** \n");
195 pthread_detach(pthread_self());
198 static void processWebconfigSync()
202 char *webConfigData = NULL;
205 char *transaction_uuid =NULL;
207 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:========= Start of processWebconfigSync ============= \n");
212 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:retry_count has reached max limit. Exiting.\n");
216 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:Checking for MAC address in Device file Cache \n");
217 ret = getKeyValue(
"estb_mac", deviceMac);
220 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:***** calling requestWebConfigData ***** \n");
221 configRet = requestWebConfigData(&webConfigData, &res_code, &transaction_uuid);
222 if(configRet == SUCCESS)
224 rv = handleHttpResponse(res_code, webConfigData, retry_count, transaction_uuid);
228 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:Failed to get webConfigData from cloud\n");
230 if(webConfigData != NULL)
232 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:=========Delete webconfig data =============\n");
233 WAL_FREE(webConfigData);
235 if(transaction_uuid != NULL)
237 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:=========transaction_uuid =============\n");
238 WAL_FREE(transaction_uuid);
242 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:No retries are required. Exiting..\n");
246 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:requestWebConfigData BACKOFF_SLEEP_DELAY_SEC is %d seconds\n", BACKOFF_SLEEP_DELAY_SEC);
250 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite: MAC address not populated yet in Device file Cache BACKOFF_SLEEP_DELAY_SEC is %d seconds\n", BACKOFF_SLEEP_DELAY_SEC);
252 sleep(BACKOFF_SLEEP_DELAY_SEC);
254 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:Webconfig retry_count is %d\n", retry_count);
257 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:========= End of processWebconfigSync =============\n");
261 static int handleHttpResponse(
long response_code,
char *webConfigData,
int retry_count,
char* transaction_uuid)
266 char configVersion[MAX_BUF_SIZE] = {
'\0'};
267 char *newDocVersion = NULL;
269 char currentTime[32];
270 current_time = time(NULL);
271 snprintf(currentTime,
sizeof(currentTime),
"%d",(
int)current_time);
274 FILE *ConfigFilePtr = NULL;
276 if(ConfigFilePtr = fopen(CONFIG_VERSION_FILE,
"r"))
279 fread(configVersion, MAX_BUF_SIZE, 1, ConfigFilePtr);
280 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"The config version file exists with value =%s \n",configVersion);
281 fclose(ConfigFilePtr);
284 if(response_code == 304)
286 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:webConfig is in sync with cloud. response_code:%d\n", response_code);
287 addWebConfigNotifyMsg(webconfigEndPoint, response_code, NULL, 0, currentTime , configVersion, transaction_uuid);
290 else if(response_code == 200)
292 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:webConfig is not in sync with cloud. response_code:%d\n", response_code);
294 if(webConfigData !=NULL)
296 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:webConfigData fetched successfully\n");
297 json_status = processJsonDocument(webConfigData, &setRet, &newDocVersion);
298 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:setRet after process Json is %d\n", setRet);
299 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:newDocVersion is %s\n", newDocVersion);
300 if(json_status == SUCCESS)
302 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:processJsonDocument success\n");
303 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:The transcation UUID =%s \n",transaction_uuid);
304 addWebConfigNotifyMsg(webconfigEndPoint, response_code,
"success", setRet, currentTime , newDocVersion, transaction_uuid);
308 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:Failure in processJsonDocument\n");
309 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:Configuration settings version %s FAILED\n", newDocVersion );
310 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:Sending Webconfig apply Failure Notification\n");
311 addWebConfigNotifyMsg(webconfigEndPoint, response_code,
"failed", setRet, currentTime , newDocVersion, transaction_uuid);
313 if(newDocVersion != NULL)
315 WAL_FREE(newDocVersion);
321 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:webConfigData is empty, need to retry\n");
324 else if(response_code == 204)
326 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:No configuration available for this device. response_code:%d\n", response_code);
327 addWebConfigNotifyMsg(webconfigEndPoint, response_code, NULL, 0, currentTime , configVersion, transaction_uuid);
330 else if(response_code == 403)
332 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:Token is expired, fetch new token. response_code:%d\n", response_code);
333 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:The device mac =%s \n",deviceMac);
334 createNewAuthToken(webpa_auth_token,
sizeof(webpa_auth_token), deviceMac, serialNum );
335 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:createNewAuthToken done in 403 case\n");
338 else if(response_code == 404)
340 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:Action not supported. response_code:%d\n", response_code);
341 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite: Something went wrong on the device side re try again \n");
346 else if(response_code == 429)
348 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:No action required from client. response_code:%d\n", response_code);
351 first_digit = (int)(response_code / pow(10, (
int)log10(response_code)));
352 if((response_code !=403) && (first_digit == 4))
354 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:Action not supported. response_code:%d\n", response_code);
355 addWebConfigNotifyMsg(webconfigEndPoint, response_code, NULL, 0, currentTime , configVersion, transaction_uuid);
360 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:Error code returned, need to retry. response_code:%d\n", response_code);
361 if(retry_count >= 25 )
363 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:Sending Notification after %d retry attempts\n",retry_count);
364 addWebConfigNotifyMsg(webconfigEndPoint, response_code, NULL, 0, currentTime , configVersion, transaction_uuid);
378 static int requestWebConfigData(
char **configData,
long *code,
char **transaction_id)
380 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite: **** inside requestWebConfigData***** \n");
384 struct curl_slist *list = NULL;
385 struct curl_slist *headers_list = NULL;
387 long response_code = 0;
389 char *transID = NULL;
395 void * dataVal = NULL;
396 curl = curl_easy_init();
400 data.data = (
char *) malloc(
sizeof(
char) * 1);
401 if(NULL == data.data)
403 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:Failed to allocate memory.\n");
407 createCurlheader(list, &headers_list, &transID);
410 *transaction_id = strdup(transID);
414 char endPoint[128] = {
'\0'};
416 RFC_ParamData_t param = {0};
417 WDMP_STATUS status = getRFCParameter((
char*)
"webcfg",
"Device.X_RDK_WebConfig.URL", ¶m);
419 if (status == WDMP_SUCCESS) {
420 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webcfg: name = %s, type = %d, value = %s\n", param.name, param.type, param.value);
421 rc=strcpy_s(endPoint,
sizeof(param.value) ,param.value);
424 rc=strcat_s(endPoint,strlen(BLE_DETECTION_WEBCFG_ENDPOINT),BLE_DETECTION_WEBCFG_ENDPOINT);
428 webconfigEndPoint = replaceMacWord(endPoint, c, deviceMac);
432 if(status != WDMP_SUCCESS || rc!=EOK)
434 webconfigEndPoint = replaceMacWord(BLE_DETECTION_WEBCONFIG_ENDPOINT, c, deviceMac);
438 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:webConfigURL is %s \n", webconfigEndPoint);
439 curl_easy_setopt(curl, CURLOPT_URL, webconfigEndPoint );
440 curl_easy_setopt(curl, CURLOPT_TIMEOUT, CURL_TIMEOUT_SEC);
444 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback_fn);
445 curl_easy_setopt(curl, CURLOPT_WRITEDATA, dataVal);
447 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers_list);
449 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:Set CURLOPT_HEADERFUNCTION option\n");
454 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:curl Ip resolve option set as default mode\n");
455 curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_WHATEVER);
456 curl_easy_setopt(curl, CURLOPT_CAPATH, CA_CERT_PATH);
458 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
460 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L);
462 curl_easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
464 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
466 res = curl_easy_perform(curl);
467 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
468 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:webConfig curl response %d http_code %d\n", res, response_code);
469 *code = response_code;
470 time_res = curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total);
473 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:curl response Time: %.1f seconds\n", total);
475 curl_slist_free_all(headers_list);
478 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
482 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:checking content type\n");
483 content_res = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);
484 if(!content_res && ct)
486 if(strcmp(ct,
"application/json") !=0)
488 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:Invalid Content-Type\n");
490 else if(response_code == 200)
492 *configData = strdup(data.data);
493 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:configData received from cloud is %s\n", *configData);
498 curl_easy_cleanup(curl);
503 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:curl init failure\n");
514 static size_t write_callback_fn(
void *buffer,
size_t size,
size_t nmemb,
struct token_data *data)
516 size_t index = data->size;
517 size_t n = (size * nmemb);
519 errno_t safec_rc = -1;
521 data->size += (size * nmemb);
523 tmp = (
char *)realloc(data->data, data->size + 1);
529 WAL_FREE(data->data);
531 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:Failed to allocate memory for data\n");
535 safec_rc=memcpy_s((data->data + index),
sizeof(data->data)-index, buffer, n);
540 data->data[data->size] =
'\0';
547 static size_t header_callback(
char *buffer,
size_t size,
size_t nitems)
550 char* header_value = NULL;
551 char* final_header = NULL;
552 char header_str[64] = {
'\0'};
555 etag_len = strlen(ETAG_HEADER);
556 if( nitems > etag_len )
558 if( strncasecmp(ETAG_HEADER, buffer, etag_len) == 0 )
560 header_value = strtok(buffer,
":");
561 while( header_value != NULL )
563 header_value = strtok(NULL,
":");
564 if(header_value !=NULL)
566 strncpy(header_str, header_value,
sizeof(header_str)-1);
567 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:header_str is %s\n", header_str);
568 stripSpaces(header_str, &final_header);
570 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:final_header is %s len %lu\n", final_header, strlen(final_header));
571 strncpy(g_ETAG, final_header,
sizeof(g_ETAG)-1);
580 static void stripSpaces(
char *str,
char **final_str)
584 for(i=0; str[i]!=
'\0'; ++i)
601 static int processJsonDocument(
char *jsonData,
int *retStatus,
char **docVersion)
604 char *version = NULL;
605 std:: string command;
608 parseStatus = parseJsonData(jsonData, &version);
609 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:After parseJsonData version is %s\n", version);
612 *docVersion = strdup(version);
613 FILE *ConfigFilePtr = NULL;
615 if(ConfigFilePtr = fopen(CONFIG_VERSION_FILE,
"w"))
617 fwrite(version, strlen(version), 1, ConfigFilePtr);
618 fclose(ConfigFilePtr);
622 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite: FAILED opening </opt/persistent/webconfig_Version> configVersion file \n");
624 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:docVersion is %s\n", *docVersion);
629 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite: ****webconfig set Request **** \n");
630 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:The param name =%s \n",tr181data.name.c_str());
631 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:The param value =%s \n",tr181data.value.c_str());
632 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:The param dataType =%d \n",tr181data.datatype);
633 DATA_TYPE data_type = (DATA_TYPE)tr181data.datatype;
635 WDMP_STATUS rfcStatus = setRFCParameter((
char*)
"webconfig",tr181data.name.c_str() , tr181data.value.c_str(), data_type);
637 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:The setRFCParameter return = %s \n",rfcStatus);
639 return (WDMP_SUCCESS == rfcStatus)? SUCCESS : ERR;
643 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:parseJsonData failed. parseStatus is %d\n", parseStatus);
648 static int parseJsonData(
char* jsonData,
char **version)
653 char *configVersion= NULL;
655 if((jsonData !=NULL) && (strlen(jsonData)>0))
657 json = cJSON_Parse(jsonData);
661 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:WebConfig Parse successi \n");
662 isValid = validateConfigFormat(json, &configVersion);
663 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:configVersion is %s\n", configVersion);
664 if(configVersion !=NULL)
666 *version = strdup(configVersion);
667 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:version copied from configVersion is %s\n", *version);
668 WAL_FREE(configVersion);
672 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:validateConfigFormat failed\n");
675 parse_set_request(json);
681 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:****Error parsing Json data **** \n");
686 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:jsonData is empty\n");
691 static int validateConfigFormat(cJSON *json,
char **eTag)
693 cJSON *versionObj =NULL;
694 cJSON *paramArray = NULL;
696 char *jsonversion=NULL;
698 versionObj = cJSON_GetObjectItem( json,
"version" );
699 if(versionObj !=NULL)
701 if(cJSON_GetObjectItem( json,
"version" )->type == cJSON_String)
703 jsonversion = cJSON_GetObjectItem( json,
"version" )->valuestring;
704 if(jsonversion !=NULL)
709 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:jsonversion :%s len %lu\n", jsonversion, strlen(jsonversion));
710 if(strncmp(jsonversion, g_ETAG, strlen(g_ETAG)) == 0)
712 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:Config Version and ETAG header are matching\n");
713 *eTag = strdup(jsonversion);
714 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:eTag is %s\n", *eTag);
716 paramArray = cJSON_GetObjectItem( json,
"parameters" );
717 if( paramArray != NULL )
719 itemSize = cJSON_GetArraySize( json );
722 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:Config document format is valid\n");
727 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:config contains fields other than version and parameters\n");
733 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:Invalid config json, parameters field is not present\n");
739 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:Invalid config json, version & ETAG are not same\n");
745 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:Failed to fetch ETAG header from config response\n");
753 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:Invalid config json, version field is not present\n");
766 static void createCurlheader(
struct curl_slist *list,
struct curl_slist **header_list,
char ** trans_uuid)
769 char currentTime[32];
770 char *transaction_uuid = NULL;
771 char current_version[MAX_BUF_SIZE] = {0};
772 FILE *ConfigFilePtr = NULL;
773 char auth_header[MAX_HEADER_LEN] = {0};
774 char version_header[MAX_BUF_SIZE] = {0};
775 char schema_header[MAX_BUF_SIZE] = {0};
776 char currentTime_header[MAX_BUF_SIZE] = {0};
777 char uuid_header[MAX_BUF_SIZE] = {0};
779 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:Start of createCurlheader\n");
784 snprintf(auth_header, MAX_HEADER_LEN,
"Authorization:Bearer %s", (0 < strlen(webpa_auth_token) ? webpa_auth_token : NULL));
785 list = curl_slist_append(list, auth_header);
788 if(ConfigFilePtr = fopen(CONFIG_VERSION_FILE,
"r"))
790 fread(current_version, MAX_BUF_SIZE, 1, ConfigFilePtr);
791 if(strlen(current_version)!=0)
793 snprintf(version_header, MAX_BUF_SIZE,
"IF-NONE-MATCH:%s", current_version);
797 snprintf(version_header, MAX_BUF_SIZE,
"IF-NONE-MATCH:%s",
"NONE");
799 fclose(ConfigFilePtr);
803 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite: FAILED opening </opt/persistent/webconfig_Version> configVersion file \n");
804 snprintf(version_header, MAX_BUF_SIZE,
"IF-NONE-MATCH:%s",
"NONE");
806 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:version_header formed %s\n", version_header);
807 list = curl_slist_append(list, version_header);
811 snprintf(schema_header, MAX_BUF_SIZE,
"Schema-Version: %s",
"v1.0");
812 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:schema_header formed %s\n", schema_header);
813 list = curl_slist_append(list, schema_header);
816 memset(currentTime, 0,
sizeof(currentTime));
817 current_time = time(NULL);
818 snprintf(currentTime,
sizeof(currentTime),
"%d",(
int)current_time);
820 snprintf(currentTime_header, MAX_BUF_SIZE,
"X-System-Current-Time: %s", currentTime);
821 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:currentTime_header formed %s\n", currentTime_header);
822 list = curl_slist_append(list, currentTime_header);
823 if(transaction_uuid == NULL)
825 transaction_uuid = generate_trans_uuid();
828 if(transaction_uuid !=NULL)
831 snprintf(uuid_header, MAX_BUF_SIZE,
"Transaction-ID: %s", transaction_uuid);
832 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:uuid_header formed %s\n", uuid_header);
833 list = curl_slist_append(list, uuid_header);
834 *trans_uuid = strdup(transaction_uuid);
835 WAL_FREE(transaction_uuid);
839 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:Failed to generate transaction_uuid\n");
844 static char* generate_trans_uuid()
846 char *transID = NULL;
847 uuid_t transaction_Id;
848 char *trans_id = NULL;
849 trans_id = (
char *)malloc(MAX_UUID_SIZE);
850 uuid_generate_random(transaction_Id);
851 uuid_unparse(transaction_Id, trans_id);
860 static void execute_token_script(
char *token,
char *name,
size_t len,
char *mac,
char *serNum)
862 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite: ****Inside execute token script**** \n");
863 FILE* out = NULL, *file = NULL;
864 char command[MAX_BUF_SIZE] = {
'\0'};
867 file = fopen(name,
"r");
870 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:Runn the commannd \n");
871 snprintf(command,
sizeof(command),
"%s %s %s",name,serNum,mac);
872 out = popen(command,
"r");
875 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:Read the token \n");
876 fgets(token, len, out);
877 RDK_LOG(RDK_LOG_DEBUG, LOG_TR69HOSTIF,
"webconfig_lite:Token =%s \n",token);
884 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"File %s open error\n", name);
894 static void createNewAuthToken(
char *newToken,
size_t len,
char *hw_mac,
char* hw_serial_number)
896 int token_retry_count=0;
900 if(( access( CURL_FILE, F_OK ) != -1 ))
903 char output[12] = {
'\0'};
904 execute_token_script(output,WEBPA_CREATE_HEADER,
sizeof(output),hw_mac,hw_serial_number);
905 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:***** inside create New Auth Token ***** \n");
906 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:**** the outout string =%s and lenngth =%d *** \n",output,strlen(output));
907 if (strlen(output)>0 && strcmp(output,
"SUCCESS")==0)
910 execute_token_script(newToken,WEBPA_READ_HEADER,len,hw_mac,hw_serial_number);
914 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:Failed to create new token\n");
922 if(token_retry_count >60)
924 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:token_retry_count has reached max limit. Exiting.\n");
928 sleep(BACKOFF_SLEEP_DELAY_5_SEC);
930 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:Webconfig token_retry_count is %d\n", token_retry_count);
942 static void getAuthToken()
945 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:****Inside of getAuthToken ***** \n");
946 char output[4069] = {
'\0'};
947 memset (webpa_auth_token, 0,
sizeof(webpa_auth_token));
949 if( strlen(WEBPA_READ_HEADER) !=0 && strlen(WEBPA_CREATE_HEADER) !=0)
951 execute_token_script(output, WEBPA_READ_HEADER,
sizeof(output), deviceMac, serialNum);
952 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:The OutPut =%s \n",output);
953 if ((strlen(output) == 0))
955 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:Unable to get auth token\n");
957 else if(strcmp(output,
"ERROR")==0)
959 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:Failed to read token from %s. Proceeding to create new token.\n",WEBPA_READ_HEADER);
961 createNewAuthToken(webpa_auth_token,
sizeof(webpa_auth_token), deviceMac, serialNum );
965 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:update webpa_auth_token in success case\n");
966 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:The Aut Token =%s \n",output);
968 strncpy(webpa_auth_token, output, (
sizeof(webpa_auth_token)-1));
974 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:Both read and write file are NULL \n");
979 static void addWebConfigNotifyMsg(
char *url,
long status_code,
char *application_status,
int application_details,
char *request_timestamp,
char *version,
char *transaction_uuid)
989 args->url = strdup(url);
991 args->status_code = status_code;
992 if(application_status != NULL)
994 args->application_status = strdup(application_status);
996 args->application_details = application_details;
997 if(request_timestamp != NULL)
999 args->request_timestamp = strdup(request_timestamp);
1003 args->version = strdup(version);
1005 if(transaction_uuid != NULL)
1007 args->transaction_uuid = strdup(transaction_uuid);
1009 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:args->url:%s args->status_code:%d \
1010 args->application_status:%s args->application_details:%d args->request_timestamp:%s\
1011 args->version:%s args->transaction_uuid:%s\n", args->url, args->status_code,\
1012 args->application_status, args->application_details, args->request_timestamp,\
1013 args->version, args->transaction_uuid );
1015 processWebConfigNotification(args);
1022 char device_id[32] = {
'\0' };
1023 cJSON *notifyPayload = NULL;
1024 char * stringifiedNotifyPayload = NULL;
1025 char dest[512] = {
'\0'};
1026 char source[MAX_BUF_SIZE] = {0};
1027 cJSON * reports, *one_report;
1029 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite: Entering processWebConfigNotification\n");
1032 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:Processing msg\n");
1033 snprintf(device_id,
sizeof(device_id),
"mac:%s", deviceMac);
1034 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:webconfig Device_id %s\n", device_id);
1036 notifyPayload = cJSON_CreateObject();
1038 if(notifyPayload != NULL)
1040 cJSON_AddStringToObject(notifyPayload,
"device_id", device_id);
1042 cJSON_AddItemToObject(notifyPayload,
"reports", reports = cJSON_CreateArray());
1043 cJSON_AddItemToArray(reports, one_report = cJSON_CreateObject());
1044 cJSON_AddStringToObject(one_report,
"url", (NULL != n_param->url) ? n_param->url :
"unknown");
1045 cJSON_AddNumberToObject(one_report,
"http_status_code", n_param->status_code);
1046 if(n_param->status_code == 200)
1048 cJSON_AddStringToObject(one_report,
"document_application_status", (NULL != n_param->application_status) ? n_param->application_status :
"unknown");
1049 cJSON_AddNumberToObject(one_report,
"document_application_details", n_param->application_details);
1051 cJSON_AddNumberToObject(one_report,
"request_timestamp", (NULL != n_param->request_timestamp) ? atoi(n_param->request_timestamp) : 0);
1052 cJSON_AddStringToObject(one_report,
"version", (NULL != n_param->version && (strlen(n_param->version)!=0)) ? n_param->version :
"NONE");
1053 cJSON_AddStringToObject(one_report,
"transaction_uuid", (NULL != n_param->transaction_uuid && (strlen(n_param->transaction_uuid)!=0)) ? n_param->transaction_uuid :
"unknown");
1054 stringifiedNotifyPayload = cJSON_PrintUnformatted(notifyPayload);
1055 cJSON_Delete(notifyPayload);
1058 snprintf(dest,
sizeof(dest),
"event:config-version-report/%s",device_id);
1059 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:dest is %s\n", dest);
1061 if (stringifiedNotifyPayload != NULL && strlen(device_id) != 0)
1063 strncpy(source, device_id,
sizeof(device_id));
1064 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:source is %s\n", source);
1065 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite:stringifiedNotifyPayload is %s\n", stringifiedNotifyPayload);
1070 free_notify_params_struct(n_param);
1075 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"webconfig_lite: n_param is NULL\n");
1084 if(param->url != NULL)
1086 WAL_FREE(param->url);
1088 if(param->application_status != NULL)
1090 WAL_FREE(param->application_status);
1092 if(param->request_timestamp != NULL)
1094 WAL_FREE(param->request_timestamp);
1096 if(param->version != NULL)
1098 WAL_FREE(param->version);
1100 if(param->transaction_uuid != NULL)
1102 WAL_FREE(param->transaction_uuid);
1108 static char *replaceMacWord(
const char *s,
const char *macW,
const char *deviceMACW)
1113 int deviceMACWlen = strlen(deviceMACW);
1114 int macWlen = strlen(macW);
1116 for (i = 0; s[i] !=
'\0'; i++)
1118 if (strstr(&s[i], macW) == &s[i])
1126 result = (
char *)malloc(i + cnt * (deviceMACWlen - macWlen) + 1);
1127 temp_var=(i + cnt * (deviceMACWlen - macWlen) + 1);
1131 if (strstr(s, macW) == s)
1134 rc=strcpy_s(&result[i],(temp_var - i), deviceMACW);