From 8efaf043b1a0292f0d7e2592ed33b4246f818f19 Mon Sep 17 00:00:00 2001 From: knair410 Date: Thu, 6 Aug 2020 17:29:40 +0530 Subject: [PATCH 01/21] Getter Function update in webcfg to fetch reboot reason --- src/webcfg_generic.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/webcfg_generic.h b/src/webcfg_generic.h index 957ec9b5..5f0c3c35 100644 --- a/src/webcfg_generic.h +++ b/src/webcfg_generic.h @@ -42,6 +42,7 @@ char * getModelName(); char * getConnClientParamName(); char * getFirmwareVersion(); char* get_deviceMAC(); +char * getreboot_reason(); /* Getter function to return systemReadyTime in UTC format */ char *get_global_systemReadyTime(); From 50f66f19c5318235f29699938c48c06df1094f43 Mon Sep 17 00:00:00 2001 From: Sadhyama Vengilat Date: Mon, 10 Aug 2020 21:29:19 +0530 Subject: [PATCH 02/21] Logic to derive root version string on bootup --- src/webcfg_generic.h | 2 +- src/webcfg_multipart.c | 101 +++++++++++++++++++++++++++++++++-------- tests/test_events.c | 6 +++ tests/test_multipart.c | 6 +++ tests/webcfgCli.c | 6 +++ 5 files changed, 102 insertions(+), 19 deletions(-) diff --git a/src/webcfg_generic.h b/src/webcfg_generic.h index 5f0c3c35..5d8f55b7 100644 --- a/src/webcfg_generic.h +++ b/src/webcfg_generic.h @@ -42,7 +42,7 @@ char * getModelName(); char * getConnClientParamName(); char * getFirmwareVersion(); char* get_deviceMAC(); -char * getreboot_reason(); +char * getRebootReason(); /* Getter function to return systemReadyTime in UTC format */ char *get_global_systemReadyTime(); diff --git a/src/webcfg_multipart.c b/src/webcfg_multipart.c index 34776c7b..dbae8c29 100644 --- a/src/webcfg_multipart.c +++ b/src/webcfg_multipart.c @@ -101,7 +101,7 @@ char* generate_trans_uuid(); WEBCFG_STATUS processMsgpackSubdoc(char *transaction_id); void loadInitURLFromFile(char **url); static void get_webCfg_interface(char **interface); -void get_root_version(uint32_t *rt_version); +void get_root_version(uint32_t *rt_version, int *subdoclist); char *replaceMacWord(const char *s, const char *macW, const char *deviceMACW); WEBCFG_STATUS checkAkerDoc(); /*----------------------------------------------------------------------------*/ @@ -997,7 +997,7 @@ void getConfigDocList(char *docList) } } -void get_root_version(uint32_t *rt_version) +void get_root_version(uint32_t *rt_version, int *subdoclist) { webconfig_db_data_t *temp = NULL; temp = get_global_db_node(); @@ -1010,6 +1010,72 @@ void get_root_version(uint32_t *rt_version) WebcfgDebug("rt_version %lu\n", (long)*rt_version); } temp= temp->next; + *subdoclist = *subdoclist+1; + } + WebcfgDebug("*subdoclist is %d\n", *subdoclist); +} + +void get_root_version_string(char *rootVersion, int status) +{ + FILE *fp = NULL; + char *reason = NULL; + uint32_t root_version = 0; + int subdocList = 0; + + fp = fopen(WEBCFG_DB_FILE,"rb"); + + reason = getRebootReason(); + if(reason != NULL) + { + WebcfgInfo("reboot reason is %s\n", reason); + if (fp == NULL) + { + if(strncmp(reason,"factory-reset",strlen("factory-reset"))==0) + { + strcpy(rootVersion, "NONE"); + } + else if(strncmp(reason,"Software_upgrade",strlen("Software_upgrade"))==0) + { + strcpy(rootVersion, "NONE-MIGRATION"); + } + else + { + strcpy(rootVersion, "NONE-REBOOT"); + } + } + else + { + if((strncmp(rootVersion,"POST-NONE",strlen("POST-NONE")==0)) && ((strncmp(reason,"Software_upgrade",strlen("Software_upgrade"))!=0) || (strncmp(reason,"factory-reset",strlen("factory-reset"))!=0))) + { + strcpy(rootVersion, "NONE-REBOOT"); + return; + } + else if(status == 404 && ((strcmp(rootVersion, "NONE") == 0) || (strcmp(rootVersion, "NONE-MIGRATION") == 0) || (strcmp(rootVersion, "NONE-REBOOT") == 0))) + { + strcpy(rootVersion, "POST-NONE"); + return; + } + + //check existing root version + get_root_version(&root_version, &subdocList); + WebcfgDebug("root_version %lu subdocList %d\n", (long)root_version, subdocList); + + //when subdocs are applied and reboot due to software migration/rollback, root reset to 0. + if(subdocList > 1 && strncmp(reason,"Software_upgrade",strlen("Software_upgrade"))==0) + { + strcpy(rootVersion, "0"); + return; + } + if(root_version) + { + WebcfgDebug("root_version is %lu\n", (long)root_version); + sprintf(rootVersion, "%lu", (long)root_version); + } + } + } + else + { + WebcfgError("Failed to get reboot reason\n"); } } @@ -1020,27 +1086,26 @@ This can be increased if required. */ void getConfigVersionList(char *versionsList) { char *versionsList_tmp = NULL; - uint32_t root_version = 0; + char root_str[32]={'\0'}; + int http_status = 404; //TODO: pass http status to handle 404 + + //initialize to default value "0". + sprintf(versionsList, "%s", "0"); - get_root_version(&root_version); - WebcfgDebug("root_version %lu\n", (long)root_version); + get_root_version_string(root_str, http_status); + if(strlen(root_str) >0) + { + WebcfgInfo("root_str is %s\n", root_str); + sprintf(versionsList, "%s", root_str); + } webconfig_db_data_t *temp = NULL; temp = get_global_db_node(); if(NULL != temp) { - if(root_version) - { - WebcfgDebug("Updating root_version to versionsList\n"); - sprintf(versionsList, "%lu", (long)root_version); - } - else - { - WebcfgDebug("Updating versionsList as NONE\n"); - sprintf(versionsList, "%s", "NONE"); - } - WebcfgDebug("versionsList is %s\n", versionsList); + sprintf(versionsList, "%s", root_str); + WebcfgInfo("versionsList is %s\n", versionsList); while (NULL != temp) { @@ -1055,7 +1120,7 @@ void getConfigVersionList(char *versionsList) } temp= temp->next; } - WebcfgDebug("Final versionsList is %s len %lu\n", versionsList, strlen(versionsList)); + WebcfgInfo("Final versionsList is %s len %lu\n", versionsList, strlen(versionsList)); } } @@ -1102,7 +1167,7 @@ void createCurlHeader( struct curl_slist *list, struct curl_slist **header_list, if(version_header !=NULL) { getConfigVersionList(version); - snprintf(version_header, MAX_BUF_SIZE, "IF-NONE-MATCH:%s", ((strlen(version)!=0) ? version : "NONE")); + snprintf(version_header, MAX_BUF_SIZE, "IF-NONE-MATCH:%s", ((strlen(version)!=0) ? version : "0")); WebcfgInfo("version_header formed %s\n", version_header); list = curl_slist_append(list, version_header); WEBCFG_FREE(version_header); diff --git a/tests/test_events.c b/tests/test_events.c index 3476f16f..0d958293 100644 --- a/tests/test_events.c +++ b/tests/test_events.c @@ -119,6 +119,12 @@ char * getFirmwareVersion() return fName; } +char * getRebootReason() +{ + char *reason = strdup("factory-reset"); + return reason; +} + void sendNotification(char *payload, char *source, char *destination) { WEBCFG_FREE(payload); diff --git a/tests/test_multipart.c b/tests/test_multipart.c index d4f1dd4b..987c87ce 100644 --- a/tests/test_multipart.c +++ b/tests/test_multipart.c @@ -104,6 +104,12 @@ char * getFirmwareVersion() return fName; } +char * getRebootReason() +{ + char *reason = strdup("Sotware_upgrade2"); + return reason; +} + void sendNotification(char *payload, char *source, char *destination) { WEBCFG_FREE(payload); diff --git a/tests/webcfgCli.c b/tests/webcfgCli.c index dca29ff7..22d5dece 100644 --- a/tests/webcfgCli.c +++ b/tests/webcfgCli.c @@ -103,6 +103,12 @@ char * getFirmwareVersion() return fName; } +char * getRebootReason() +{ + char *reason = strdup("Software_upgrade"); + return reason; +} + void sendNotification(char *payload, char *source, char *destination) { WEBCFG_FREE(payload); From c237e189b635f979ba6cd8170b2c7fa6b38701e7 Mon Sep 17 00:00:00 2001 From: Sadhyama Vengilat Date: Tue, 11 Aug 2020 20:27:33 +0530 Subject: [PATCH 03/21] Add root version string to DB and blob --- src/webcfg_db.c | 62 +++++++++++++++++++++++++++++++----------- src/webcfg_db.h | 6 ++-- src/webcfg_event.c | 6 ++-- src/webcfg_multipart.c | 33 +++++++++++++--------- src/webcfg_multipart.h | 2 ++ src/webcfg_pack.c | 38 +++++++++++++++++++++++--- tests/test_multipart.c | 3 +- 7 files changed, 111 insertions(+), 39 deletions(-) diff --git a/src/webcfg_db.c b/src/webcfg_db.c index 2b6ff6a4..da2257f0 100644 --- a/src/webcfg_db.c +++ b/src/webcfg_db.c @@ -461,9 +461,9 @@ WEBCFG_STATUS addToTmpList( multipart_t *mp) } -void checkDBList(char *docname, uint32_t version) +void checkDBList(char *docname, uint32_t version, char* rootstr) { - if(updateDBlist(docname, version) != WEBCFG_SUCCESS) + if(updateDBlist(docname, version, rootstr) != WEBCFG_SUCCESS) { webconfig_db_data_t * webcfgdb = NULL; webcfgdb = (webconfig_db_data_t *) malloc (sizeof(webconfig_db_data_t)); @@ -473,10 +473,14 @@ void checkDBList(char *docname, uint32_t version) webcfgdb->name = strdup(docname); webcfgdb->version = version; + if(rootstr !=NULL) + { + webcfgdb->root_string = strdup(rootstr); + } webcfgdb->next = NULL; addToDBList(webcfgdb); - WebcfgInfo("webcfgdb->name added to DB %s webcfgdb->version %lu\n",webcfgdb->name, (long)webcfgdb->version); + WebcfgInfo("webcfgdb->name added to DB %s webcfgdb->version %lu webcfgdb->root_string %s\n",webcfgdb->name, (long)webcfgdb->version, webcfgdb->root_string); } else { @@ -485,7 +489,7 @@ void checkDBList(char *docname, uint32_t version) } } -WEBCFG_STATUS updateDBlist(char *docname, uint32_t version) +WEBCFG_STATUS updateDBlist(char *docname, uint32_t version, char* rootstr) { webconfig_db_data_t *webcfgdb = NULL; webcfgdb = get_global_db_node(); @@ -495,11 +499,15 @@ WEBCFG_STATUS updateDBlist(char *docname, uint32_t version) { pthread_mutex_lock (&webconfig_db_mut); WebcfgDebug("mutex_lock in updateDBlist\n"); - WebcfgDebug("node is pointing to webcfgdb->name %s, docname %s, dblen %zu, doclen %zu \n",webcfgdb->name, docname, strlen(webcfgdb->name), strlen(docname)); + WebcfgDebug("node is pointing to webcfgdb->name %s, docname %s, dblen %zu, doclen %zu webcfgdb->root_string %s\n",webcfgdb->name, docname, strlen(webcfgdb->name), strlen(docname), webcfgdb->root_string); if( strcmp(docname, webcfgdb->name) == 0) { webcfgdb->version = version; - WebcfgDebug("webcfgdb %s is updated to version %lu\n", docname, (long)webcfgdb->version); + if(rootstr !=NULL) + { + webcfgdb->root_string = strdup(rootstr); + } + WebcfgDebug("webcfgdb %s is updated to version %lu webcfgdb->root_string %s\n", docname, (long)webcfgdb->version, webcfgdb->root_string); pthread_mutex_unlock (&webconfig_db_mut); WebcfgDebug("mutex_unlock if docname is webcfgdb name\n"); return WEBCFG_SUCCESS; @@ -641,7 +649,7 @@ void delete_tmp_doc_list() int process_webcfgdbparams( webconfig_db_data_t *e, msgpack_object_map *map ) { int left = map->size; - uint8_t objects_left = 0x02; + uint8_t objects_left = 0x03; msgpack_object_kv *p; p = map->ptr; @@ -675,22 +683,34 @@ int process_webcfgdbparams( webconfig_db_data_t *e, msgpack_object_map *map ) { e->name = strndup( p->val.via.str.ptr, p->val.via.str.size ); //WebcfgDebug("e->name is %s\n", e->name); - objects_left &= ~(1 << 0); + objects_left &= ~(1 << 2); //WebcfgDebug("objects_left after name %d\n", objects_left); } + else if( 0 == match(p, "root_string") ) + { + e->root_string = strndup( p->val.via.str.ptr, p->val.via.str.size ); + //WebcfgDebug("e->root_string is %s\n", e->root_string); + objects_left &= ~(1 << 0); + //WebcfgDebug("objects_left after root_string %d\n", objects_left); + } } } p++; } - + WebcfgInfo("objects_left is %d\n", objects_left); if( 1 & objects_left ) { + if( (1 << 0) & objects_left ) + { + WebcfgInfo("Skip optional root_string element\n"); + objects_left &= ~(1 << 0); + } } else { errno = WD_OK; } - + WebcfgInfo("End objects_left is %d\n", objects_left); return (0 == objects_left) ? 0 : -1; } @@ -755,7 +775,7 @@ void addToDBList(webconfig_db_data_t *webcfgdb) webcfgdb_data = webcfgdb; pthread_mutex_unlock (&webconfig_db_mut); success_doc_count++; - WebcfgInfo("Producer added webcfgdb->name %s, webcfg->version %lu, success_doc_count %d\n",webcfgdb->name, (long)webcfgdb->version, success_doc_count); + WebcfgInfo("Producer added webcfgdb->name %s, webcfg->version %lu, webcfgdb->root_string %s, success_doc_count %d\n",webcfgdb->name, (long)webcfgdb->version, webcfgdb->root_string, success_doc_count); } else { @@ -768,7 +788,7 @@ void addToDBList(webconfig_db_data_t *webcfgdb) temp->next = webcfgdb; pthread_mutex_unlock (&webconfig_db_mut); success_doc_count++; - WebcfgInfo("Producer added webcfgdb->name %s, webcfg->version %lu, success_doc_count %d\n",webcfgdb->name, (long)webcfgdb->version, success_doc_count); + WebcfgInfo("Producer added webcfgdb->name %s, webcfg->version %lu, webcfgdb->root_string %s, success_doc_count %d\n",webcfgdb->name, (long)webcfgdb->version, webcfgdb->root_string, success_doc_count); } } @@ -815,7 +835,7 @@ char * get_DB_BLOB_base64() { for(k = 0;k< bd->entries_count ; k++) { - WebcfgInfo("Blob bd->entries[%zu].name %s, version: %lu, status: %s, error_details: %s, error_code: %d\n", k, bd->entries[k].name, (long)bd->entries[k].version, bd->entries[k].status, bd->entries[k].error_details, bd->entries[k].error_code ); + WebcfgInfo("Blob bd->entries[%zu].name %s, version: %lu, status: %s, error_details: %s, error_code: %d root_string: %s\n", k, bd->entries[k].name, (long)bd->entries[k].version, bd->entries[k].status, bd->entries[k].error_details, bd->entries[k].error_code, bd->entries[k].root_string ); } } @@ -874,7 +894,7 @@ int process_webcfgdbblobparams( blob_data_t *e, msgpack_object_map *map ) e->error_code = (uint16_t) p->val.via.u64; //WebcfgDebug("e->version is %d\n", e->error_code); } - objects_left &= ~(1 << 3); + objects_left &= ~(1 << 4); } } @@ -898,19 +918,29 @@ int process_webcfgdbblobparams( blob_data_t *e, msgpack_object_map *map ) //WebcfgDebug("e->error_details is %s\n", e->error_details); objects_left &= ~(1 << 2); } + else if(0 == match(p, "root_string") ) + { + e->root_string = strndup( p->val.via.str.ptr, p->val.via.str.size ); + //WebcfgDebug("e->root_string is %s\n", e->root_string); + objects_left &= ~(1 << 3); + } } } p++; } - + WebcfgInfo("objects_left is %d\n", objects_left); if( 1 & objects_left ) { } + else if( (1 << 3) & objects_left ) { + WebcfgInfo("Skip optional root_string element\n"); + objects_left &= ~(1 << 3); + } else { errno = BD_OK; } - + WebcfgInfo("End objects_left is %d\n", objects_left); return (0 == objects_left) ? 0 : -1; } diff --git a/src/webcfg_db.h b/src/webcfg_db.h index 108486aa..e92785f8 100644 --- a/src/webcfg_db.h +++ b/src/webcfg_db.h @@ -50,6 +50,7 @@ typedef struct webconfig_tmp_data typedef struct webconfig_db_data{ char * name; uint32_t version; + char *root_string; struct webconfig_db_data *next; }webconfig_db_data_t; @@ -62,6 +63,7 @@ typedef struct blob{ typedef struct{ char * name; uint32_t version; + char *root_string; char * status; char * error_details; uint16_t error_code; @@ -130,9 +132,9 @@ void set_doc_fail( int value); char * get_DB_BLOB_base64(); -void checkDBList(char *docname, uint32_t version); +void checkDBList(char *docname, uint32_t version,char *rootstr); -WEBCFG_STATUS updateDBlist(char *docname, uint32_t version); +WEBCFG_STATUS updateDBlist(char *docname, uint32_t version,char *rootstr); int writebase64ToDBFile(char *base64_file_path, char *data); diff --git a/src/webcfg_event.c b/src/webcfg_event.c index 329bd5fd..6a466500 100644 --- a/src/webcfg_event.c +++ b/src/webcfg_event.c @@ -272,7 +272,7 @@ void* processSubdocEvents() //add to DB, update tmp list and notification based on success ack. sendSuccessNotification(subdoc_node, eventParam->subdoc_name, eventParam->version, eventParam->trans_id); WebcfgDebug("AddToDB subdoc_name %s version %lu\n", eventParam->subdoc_name, (long)eventParam->version); - checkDBList(eventParam->subdoc_name,eventParam->version); + checkDBList(eventParam->subdoc_name,eventParam->version, NULL); WebcfgDebug("checkRootUpdate\n"); if(checkRootUpdate() == WEBCFG_SUCCESS) { @@ -390,7 +390,7 @@ void* processSubdocEvents() WebcfgInfo("tmp version %lu same as event version %lu\n",(long)tmpVersion, (long)eventParam->version); sendSuccessNotification(subdoc_node, eventParam->subdoc_name, eventParam->version, eventParam->trans_id); WebcfgInfo("AddToDB subdoc_name %s version %lu\n", eventParam->subdoc_name, (long)eventParam->version); - checkDBList(eventParam->subdoc_name,eventParam->version); + checkDBList(eventParam->subdoc_name,eventParam->version, NULL); WebcfgDebug("checkRootUpdate\n"); if(checkRootUpdate() == WEBCFG_SUCCESS) { @@ -859,7 +859,7 @@ WEBCFG_STATUS retryMultipartSubdoc(webconfig_tmp_data_t *docNode, char *docName) addWebConfgNotifyMsg(gmp->entries[m].name_space, gmp->entries[m].etag, "success", "none", get_global_transID(), 0, "status", 0); WebcfgDebug("deleteFromTmpList as scalar doc is applied\n"); deleteFromTmpList(gmp->entries[m].name_space); - checkDBList(gmp->entries[m].name_space,gmp->entries[m].etag); + checkDBList(gmp->entries[m].name_space,gmp->entries[m].etag, NULL); WebcfgDebug("checkRootUpdate scalar doc case\n"); if(checkRootUpdate() == WEBCFG_SUCCESS) { diff --git a/src/webcfg_multipart.c b/src/webcfg_multipart.c index dbae8c29..1bdb2fee 100644 --- a/src/webcfg_multipart.c +++ b/src/webcfg_multipart.c @@ -87,6 +87,7 @@ void set_global_mp(multipart_t *new) { mp = new; } + /*----------------------------------------------------------------------------*/ /* Function Prototypes */ /*----------------------------------------------------------------------------*/ @@ -592,7 +593,7 @@ WEBCFG_STATUS processMsgpackSubdoc(char *transaction_id) addWebConfgNotifyMsg(mp->entries[m].name_space, mp->entries[m].etag, "success", "none", trans_id,0, "status",0); WebcfgDebug("deleteFromTmpList as doc is applied\n"); deleteFromTmpList(mp->entries[m].name_space); - checkDBList(mp->entries[m].name_space,mp->entries[m].etag); + checkDBList(mp->entries[m].name_space,mp->entries[m].etag, NULL); success_count++; } @@ -609,7 +610,7 @@ WEBCFG_STATUS processMsgpackSubdoc(char *transaction_id) } if(version != 0) { - checkDBList("root",version); + checkDBList("root",version, g_ETAG); success_count++; } @@ -725,7 +726,7 @@ WEBCFG_STATUS processMsgpackSubdoc(char *transaction_id) while(temp1 ) { - WebcfgInfo("DB wd->entries[%lu].name: %s, version: %lu\n", success_count-j, temp1->name, (long)temp1->version); + WebcfgInfo("DB wd->entries[%lu].name: %s, version: %lu root_string: %s\n", success_count-j, temp1->name, (long)temp1->version, temp1->root_string); j--; temp1 = temp1->next; } @@ -1015,7 +1016,7 @@ void get_root_version(uint32_t *rt_version, int *subdoclist) WebcfgDebug("*subdoclist is %d\n", *subdoclist); } -void get_root_version_string(char *rootVersion, int status) +void get_root_version_string(char *rootVersion, uint32_t *root_ver, int status) { FILE *fp = NULL; char *reason = NULL; @@ -1045,7 +1046,7 @@ void get_root_version_string(char *rootVersion, int status) } else { - if((strncmp(rootVersion,"POST-NONE",strlen("POST-NONE")==0)) && ((strncmp(reason,"Software_upgrade",strlen("Software_upgrade"))!=0) || (strncmp(reason,"factory-reset",strlen("factory-reset"))!=0))) + if((strncmp(rootVersion,"POST-NONE",strlen("POST-NONE"))==0) && ((strncmp(reason,"Software_upgrade",strlen("Software_upgrade"))!=0) || (strncmp(reason,"factory-reset",strlen("factory-reset"))!=0))) { strcpy(rootVersion, "NONE-REBOOT"); return; @@ -1057,12 +1058,14 @@ void get_root_version_string(char *rootVersion, int status) } //check existing root version + WebcfgInfo("check existing root version\n"); get_root_version(&root_version, &subdocList); WebcfgDebug("root_version %lu subdocList %d\n", (long)root_version, subdocList); //when subdocs are applied and reboot due to software migration/rollback, root reset to 0. if(subdocList > 1 && strncmp(reason,"Software_upgrade",strlen("Software_upgrade"))==0) { + WebcfgInfo("reboot due to software migration/rollback, root reset to 0\n"); strcpy(rootVersion, "0"); return; } @@ -1071,6 +1074,7 @@ void get_root_version_string(char *rootVersion, int status) WebcfgDebug("root_version is %lu\n", (long)root_version); sprintf(rootVersion, "%lu", (long)root_version); } + *root_ver = root_version; } } else @@ -1083,20 +1087,23 @@ void get_root_version_string(char *rootVersion, int status) e.g. IF-NONE-MATCH: 123,44317,66317,77317 where 123 is root version. Currently versionsList length is fixed to 512 which can support up to 45 docs. This can be increased if required. */ -void getConfigVersionList(char *versionsList) +void getConfigVersionList(char *versionsList, int http_status) { char *versionsList_tmp = NULL; - char root_str[32]={'\0'}; - int http_status = 404; //TODO: pass http status to handle 404 + char root_str[32]="0"; + uint32_t root_version = 0; + //int http_status = 404; //TODO: pass http status to handle 404 //initialize to default value "0". sprintf(versionsList, "%s", "0"); - get_root_version_string(root_str, http_status); + get_root_version_string(root_str, &root_version, http_status); + WebcfgInfo("root_str is %s\n", root_str); if(strlen(root_str) >0) { - WebcfgInfo("root_str is %s\n", root_str); sprintf(versionsList, "%s", root_str); + WebcfgInfo("update root_version %lu rootString %s to DB\n", (long)root_version, root_str); + checkDBList("root", root_version, root_str); } webconfig_db_data_t *temp = NULL; @@ -1120,7 +1127,7 @@ void getConfigVersionList(char *versionsList) } temp= temp->next; } - WebcfgInfo("Final versionsList is %s len %lu\n", versionsList, strlen(versionsList)); + WebcfgDebug("Final versionsList is %s len %lu\n", versionsList, strlen(versionsList)); } } @@ -1166,7 +1173,7 @@ void createCurlHeader( struct curl_slist *list, struct curl_slist **header_list, version_header = (char *) malloc(sizeof(char)*MAX_BUF_SIZE); if(version_header !=NULL) { - getConfigVersionList(version); + getConfigVersionList(version, 0); snprintf(version_header, MAX_BUF_SIZE, "IF-NONE-MATCH:%s", ((strlen(version)!=0) ? version : "0")); WebcfgInfo("version_header formed %s\n", version_header); list = curl_slist_append(list, version_header); @@ -1589,7 +1596,7 @@ void updateRootVersionToDB() } if(version != 0) { - checkDBList("root",version); + checkDBList("root",version, g_ETAG); } WebcfgDebug("The Etag is %lu\n",(long)version ); diff --git a/src/webcfg_multipart.h b/src/webcfg_multipart.h index e5c5ea89..314db588 100644 --- a/src/webcfg_multipart.h +++ b/src/webcfg_multipart.h @@ -55,4 +55,6 @@ void set_global_mp(multipart_t *new); void reqParam_destroy( int paramCnt, param_t *reqObj ); void failedDocsRetry(); WEBCFG_STATUS validate_request_param(param_t *reqParam, int paramCount); +char * get_global_rootString(void); +void set_global_rootString(char *rootStr); #endif diff --git a/src/webcfg_pack.c b/src/webcfg_pack.c index 742ff793..7cc4b1cd 100644 --- a/src/webcfg_pack.c +++ b/src/webcfg_pack.c @@ -125,7 +125,14 @@ ssize_t webcfgdb_blob_pack(webconfig_db_data_t *webcfgdb, webconfig_tmp_data_t * { while(db_data != NULL) //1 element { - msgpack_pack_map( &pk, 5); //name, version,status, error_details + if(db_data->root_string !=NULL) + { + msgpack_pack_map( &pk, 6); //name, version,status, error_details, root_string + } + else + { + msgpack_pack_map( &pk, 5); + } struct webcfg_token WEBCFG_MAP_BLOB_NAME; @@ -160,6 +167,14 @@ ssize_t webcfgdb_blob_pack(webconfig_db_data_t *webcfgdb, webconfig_tmp_data_t * __msgpack_pack_string( &pk, WEBCFG_MAP_BLOB_ERROR_CODE.name, WEBCFG_MAP_BLOB_ERROR_CODE.length); msgpack_pack_uint64(&pk,0); + if(db_data->root_string !=NULL) + { + struct webcfg_token WEBCFG_MAP_BLOB_ROOT_STRING; + + WEBCFG_MAP_BLOB_ROOT_STRING.name = "root_string"; + WEBCFG_MAP_BLOB_ROOT_STRING.length = strlen( "root_string" ); + __msgpack_pack_string_nvp( &pk, &WEBCFG_MAP_BLOB_ROOT_STRING, db_data->root_string ); + } db_data = db_data->next; } @@ -250,8 +265,15 @@ ssize_t webcfgdb_pack( webconfig_db_data_t *packData, void **data, size_t count webconfig_db_data_t *temp = packData; while(temp != NULL) //1 element - { - msgpack_pack_map( &pk, 2); //name, version + { + if(temp->root_string !=NULL) + { + msgpack_pack_map( &pk, 3); //name, version, root_string + } + else + { + msgpack_pack_map( &pk, 2); + } struct webcfg_token WEBCFG_MAP_NAME; @@ -266,7 +288,15 @@ ssize_t webcfgdb_pack( webconfig_db_data_t *packData, void **data, size_t count __msgpack_pack_string( &pk, WEBCFG_MAP_VERSION.name, WEBCFG_MAP_VERSION.length); //WebcfgDebug("The version is %ld\n",(long)temp->version); msgpack_pack_uint64(&pk,(uint32_t) temp->version); - + + if(temp->root_string !=NULL) + { + struct webcfg_token WEBCFG_MAP_ROOTSTRING; + + WEBCFG_MAP_ROOTSTRING.name = "root_string"; + WEBCFG_MAP_ROOTSTRING.length = strlen( "root_string" ); + __msgpack_pack_string_nvp( &pk, &WEBCFG_MAP_ROOTSTRING, temp->root_string ); + } temp = temp->next; } diff --git a/tests/test_multipart.c b/tests/test_multipart.c index 987c87ce..f8a12ac4 100644 --- a/tests/test_multipart.c +++ b/tests/test_multipart.c @@ -106,7 +106,7 @@ char * getFirmwareVersion() char * getRebootReason() { - char *reason = strdup("Sotware_upgrade2"); + char *reason = strdup("factory-reset"); return reason; } @@ -157,6 +157,7 @@ void test_multipart() initWebConfigNotifyTask(); processWebcfgEvents(); initEventHandlingTask(); + initWebConfigClient(); processWebconfgSync(status); } From 386c544bd3ba69b5898d71228c6f5c971fb3d524 Mon Sep 17 00:00:00 2001 From: Sadhyama Vengilat Date: Thu, 13 Aug 2020 10:46:25 +0530 Subject: [PATCH 04/21] Fix jenkins build issues on getRebootReason() --- src/webcfg_generic.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/webcfg_generic.c b/src/webcfg_generic.c index 36b49df9..9434f6d7 100644 --- a/src/webcfg_generic.c +++ b/src/webcfg_generic.c @@ -30,6 +30,7 @@ char *__attribute__((weak)) getDeviceBootTime(void); char *__attribute__((weak)) getSerialNumber(void); char *__attribute__((weak)) getProductClass(void); char *__attribute__((weak)) getModelName(void); +char *__attribute__((weak)) getRebootReason(void); char *__attribute__((weak)) getConnClientParamName(void); char *__attribute__((weak)) getFirmwareVersion(void); char *__attribute__((weak)) get_deviceMAC(void); @@ -68,6 +69,11 @@ char *getModelName(void) return NULL; } +char *getRebootReason(void) +{ + return NULL; +} + char *getConnClientParamName(void) { return NULL; From ef58bbc1b92b1ccfcf0f6acaa5f58bd1a33a3560 Mon Sep 17 00:00:00 2001 From: Sadhyama Vengilat Date: Thu, 13 Aug 2020 12:45:56 +0530 Subject: [PATCH 05/21] To handle 404 POST-NONE reboot --- src/webcfg.c | 6 ++++++ src/webcfg_db.c | 4 ---- src/webcfg_multipart.c | 3 ++- src/webcfg_multipart.h | 1 + 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/webcfg.c b/src/webcfg.c index 75f35f57..40a86223 100644 --- a/src/webcfg.c +++ b/src/webcfg.c @@ -294,6 +294,7 @@ int handlehttpResponse(long response_code, char *webConfigData, int retry_count, int first_digit=0; int msgpack_status=0; int err = 0; + char version[512]={'\0'}; if(response_code == 304) { @@ -350,6 +351,11 @@ int handlehttpResponse(long response_code, char *webConfigData, int retry_count, if((response_code !=403) && (first_digit == 4)) //4xx { WebcfgInfo("Action not supported. response_code:%ld\n", response_code); + if (response_code == 404) + { + //To set POST-NONE reboot version string when 404 + getConfigVersionList(version, response_code); + } WEBCFG_FREE(transaction_uuid); return 1; } diff --git a/src/webcfg_db.c b/src/webcfg_db.c index da2257f0..b836872f 100644 --- a/src/webcfg_db.c +++ b/src/webcfg_db.c @@ -697,7 +697,6 @@ int process_webcfgdbparams( webconfig_db_data_t *e, msgpack_object_map *map ) } p++; } - WebcfgInfo("objects_left is %d\n", objects_left); if( 1 & objects_left ) { if( (1 << 0) & objects_left ) @@ -710,7 +709,6 @@ int process_webcfgdbparams( webconfig_db_data_t *e, msgpack_object_map *map ) { errno = WD_OK; } - WebcfgInfo("End objects_left is %d\n", objects_left); return (0 == objects_left) ? 0 : -1; } @@ -928,7 +926,6 @@ int process_webcfgdbblobparams( blob_data_t *e, msgpack_object_map *map ) } p++; } - WebcfgInfo("objects_left is %d\n", objects_left); if( 1 & objects_left ) { } @@ -940,7 +937,6 @@ int process_webcfgdbblobparams( blob_data_t *e, msgpack_object_map *map ) { errno = BD_OK; } - WebcfgInfo("End objects_left is %d\n", objects_left); return (0 == objects_left) ? 0 : -1; } diff --git a/src/webcfg_multipart.c b/src/webcfg_multipart.c index 1bdb2fee..1e169851 100644 --- a/src/webcfg_multipart.c +++ b/src/webcfg_multipart.c @@ -1092,7 +1092,6 @@ void getConfigVersionList(char *versionsList, int http_status) char *versionsList_tmp = NULL; char root_str[32]="0"; uint32_t root_version = 0; - //int http_status = 404; //TODO: pass http status to handle 404 //initialize to default value "0". sprintf(versionsList, "%s", "0"); @@ -1104,6 +1103,8 @@ void getConfigVersionList(char *versionsList, int http_status) sprintf(versionsList, "%s", root_str); WebcfgInfo("update root_version %lu rootString %s to DB\n", (long)root_version, root_str); checkDBList("root", root_version, root_str); + WebcfgInfo("addNewDocEntry. get_successDocCount %d\n", get_successDocCount()); + addNewDocEntry(get_successDocCount()); } webconfig_db_data_t *temp = NULL; diff --git a/src/webcfg_multipart.h b/src/webcfg_multipart.h index 314db588..d11c1f4d 100644 --- a/src/webcfg_multipart.h +++ b/src/webcfg_multipart.h @@ -57,4 +57,5 @@ void failedDocsRetry(); WEBCFG_STATUS validate_request_param(param_t *reqParam, int paramCount); char * get_global_rootString(void); void set_global_rootString(char *rootStr); +void getConfigVersionList(char *versionsList, int http_status); #endif From 53b59b6aef3c61b9fcab1c4f89df6e8f324658f3 Mon Sep 17 00:00:00 2001 From: Sadhyama Vengilat Date: Fri, 14 Aug 2020 13:39:01 +0530 Subject: [PATCH 06/21] Root version string to 0 when http response 200 --- src/webcfg.c | 1 + src/webcfg_multipart.c | 54 ++++++++++++++++++++++++++++-------------- 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/src/webcfg.c b/src/webcfg.c index 40a86223..6b44f574 100644 --- a/src/webcfg.c +++ b/src/webcfg.c @@ -309,6 +309,7 @@ int handlehttpResponse(long response_code, char *webConfigData, int retry_count, if(webConfigData !=NULL) { WebcfgDebug("webConfigData fetched successfully\n"); + getConfigVersionList(version, response_code); WebcfgDebug("parseMultipartDocument\n"); msgpack_status = parseMultipartDocument(webConfigData, ct, dataSize, transaction_uuid); diff --git a/src/webcfg_multipart.c b/src/webcfg_multipart.c index 1e169851..44e82bb2 100644 --- a/src/webcfg_multipart.c +++ b/src/webcfg_multipart.c @@ -102,7 +102,7 @@ char* generate_trans_uuid(); WEBCFG_STATUS processMsgpackSubdoc(char *transaction_id); void loadInitURLFromFile(char **url); static void get_webCfg_interface(char **interface); -void get_root_version(uint32_t *rt_version, int *subdoclist); +void getRootVersionFromDB(uint32_t *rt_version, char *rt_string, int *subdoclist); char *replaceMacWord(const char *s, const char *macW, const char *deviceMACW); WEBCFG_STATUS checkAkerDoc(); /*----------------------------------------------------------------------------*/ @@ -998,7 +998,7 @@ void getConfigDocList(char *docList) } } -void get_root_version(uint32_t *rt_version, int *subdoclist) +void getRootVersionFromDB(uint32_t *rt_version, char *rt_string, int *subdoclist) { webconfig_db_data_t *temp = NULL; temp = get_global_db_node(); @@ -1008,7 +1008,11 @@ void get_root_version(uint32_t *rt_version, int *subdoclist) if( strcmp(temp->name, "root") == 0) { *rt_version = temp->version; - WebcfgDebug("rt_version %lu\n", (long)*rt_version); + if(temp->root_string !=NULL) + { + strcpy(rt_string, temp->root_string); + } + WebcfgInfo("rt_version %lu rt_string %s from DB list\n", (long)*rt_version, rt_string); } temp= temp->next; *subdoclist = *subdoclist+1; @@ -1020,7 +1024,8 @@ void get_root_version_string(char *rootVersion, uint32_t *root_ver, int status) { FILE *fp = NULL; char *reason = NULL; - uint32_t root_version = 0; + uint32_t db_root_version = 0; + char db_root_string[32]="0"; int subdocList = 0; fp = fopen(WEBCFG_DB_FILE,"rb"); @@ -1046,35 +1051,48 @@ void get_root_version_string(char *rootVersion, uint32_t *root_ver, int status) } else { - if((strncmp(rootVersion,"POST-NONE",strlen("POST-NONE"))==0) && ((strncmp(reason,"Software_upgrade",strlen("Software_upgrade"))!=0) || (strncmp(reason,"factory-reset",strlen("factory-reset"))!=0))) + //get existing root version from DB + getRootVersionFromDB(&db_root_version, db_root_string, &subdocList); + WebcfgDebug("db_root_version %lu db_root_string %s subdocList %d\n", (long)db_root_version, db_root_string, subdocList); + + if((strncmp(db_root_string,"POST-NONE",strlen("POST-NONE"))==0) && ((strncmp(reason,"Software_upgrade",strlen("Software_upgrade"))!=0) || (strncmp(reason,"factory-reset",strlen("factory-reset"))!=0))) { strcpy(rootVersion, "NONE-REBOOT"); return; } - else if(status == 404 && ((strcmp(rootVersion, "NONE") == 0) || (strcmp(rootVersion, "NONE-MIGRATION") == 0) || (strcmp(rootVersion, "NONE-REBOOT") == 0))) + else if(status == 404 && ((strcmp(db_root_string, "NONE") == 0) || (strcmp(db_root_string, "NONE-MIGRATION") == 0) || (strcmp(db_root_string, "NONE-REBOOT") == 0))) { strcpy(rootVersion, "POST-NONE"); return; } + //Reset reboot version string to "0" after initial migration and factory reset. + else if(status == 200 && ((strcmp(db_root_string, "NONE") == 0) || (strcmp(db_root_string, "NONE-MIGRATION") == 0) || (strcmp(db_root_string, "NONE-REBOOT") == 0))) + { + WebcfgInfo("Received config from server, root reset to 0\n"); + strcpy(rootVersion, "0"); + return; + } //check existing root version WebcfgInfo("check existing root version\n"); - get_root_version(&root_version, &subdocList); - WebcfgDebug("root_version %lu subdocList %d\n", (long)root_version, subdocList); - - //when subdocs are applied and reboot due to software migration/rollback, root reset to 0. - if(subdocList > 1 && strncmp(reason,"Software_upgrade",strlen("Software_upgrade"))==0) + if(db_root_version) { - WebcfgInfo("reboot due to software migration/rollback, root reset to 0\n"); - strcpy(rootVersion, "0"); - return; + //when subdocs are applied and reboot due to software migration/rollback, root reset to 0. + if(subdocList > 1 && strncmp(reason,"Software_upgrade",strlen("Software_upgrade"))==0) + { + WebcfgInfo("reboot due to software migration/rollback, root reset to 0\n"); + strcpy(rootVersion, "0"); + return; + } + WebcfgDebug("Update rootVersion with db_root_version %lu\n", (long)db_root_version); + sprintf(rootVersion, "%lu", (long)db_root_version); } - if(root_version) + else { - WebcfgDebug("root_version is %lu\n", (long)root_version); - sprintf(rootVersion, "%lu", (long)root_version); + WebcfgInfo("Update rootVersion with db_root_string %s\n", db_root_string); + sprintf(rootVersion, "%s", db_root_string); } - *root_ver = root_version; + *root_ver = db_root_version; } } else From c264e60e4ca5ec22d2105498bcce3ed2cb6f4a73 Mon Sep 17 00:00:00 2001 From: Sadhyama Vengilat Date: Mon, 17 Aug 2020 12:57:12 +0530 Subject: [PATCH 07/21] POST-NONE for 200 empty config, avoid root_string pack when non-zero root version --- src/webcfg.c | 7 ++--- src/webcfg_db.c | 10 +++++++- src/webcfg_multipart.c | 58 +++++++++++++++++++++++++----------------- 3 files changed, 48 insertions(+), 27 deletions(-) diff --git a/src/webcfg.c b/src/webcfg.c index 6b44f574..dc261163 100644 --- a/src/webcfg.c +++ b/src/webcfg.c @@ -309,7 +309,6 @@ int handlehttpResponse(long response_code, char *webConfigData, int retry_count, if(webConfigData !=NULL) { WebcfgDebug("webConfigData fetched successfully\n"); - getConfigVersionList(version, response_code); WebcfgDebug("parseMultipartDocument\n"); msgpack_status = parseMultipartDocument(webConfigData, ct, dataSize, transaction_uuid); @@ -326,7 +325,9 @@ int handlehttpResponse(long response_code, char *webConfigData, int retry_count, } else { - WebcfgError("webConfigData is empty, need to do curl retry to server\n"); + WebcfgInfo("webConfigData is empty\n"); + //After factory reset when server sends 200 with empty config, set POST-NONE root version + getConfigVersionList(version, response_code); } } else if(response_code == 204) @@ -354,7 +355,7 @@ int handlehttpResponse(long response_code, char *webConfigData, int retry_count, WebcfgInfo("Action not supported. response_code:%ld\n", response_code); if (response_code == 404) { - //To set POST-NONE reboot version string when 404 + //To set POST-NONE root version when 404 getConfigVersionList(version, response_code); } WEBCFG_FREE(transaction_uuid); diff --git a/src/webcfg_db.c b/src/webcfg_db.c index b836872f..b61724d0 100644 --- a/src/webcfg_db.c +++ b/src/webcfg_db.c @@ -477,6 +477,10 @@ void checkDBList(char *docname, uint32_t version, char* rootstr) { webcfgdb->root_string = strdup(rootstr); } + else + { + webcfgdb->root_string = NULL; + } webcfgdb->next = NULL; addToDBList(webcfgdb); @@ -507,7 +511,11 @@ WEBCFG_STATUS updateDBlist(char *docname, uint32_t version, char* rootstr) { webcfgdb->root_string = strdup(rootstr); } - WebcfgDebug("webcfgdb %s is updated to version %lu webcfgdb->root_string %s\n", docname, (long)webcfgdb->version, webcfgdb->root_string); + else + { + webcfgdb->root_string = NULL; + } + WebcfgInfo("webcfgdb %s is updated to version %lu webcfgdb->root_string %s\n", docname, (long)webcfgdb->version, webcfgdb->root_string); pthread_mutex_unlock (&webconfig_db_mut); WebcfgDebug("mutex_unlock if docname is webcfgdb name\n"); return WEBCFG_SUCCESS; diff --git a/src/webcfg_multipart.c b/src/webcfg_multipart.c index 44e82bb2..1e8522f8 100644 --- a/src/webcfg_multipart.c +++ b/src/webcfg_multipart.c @@ -610,7 +610,7 @@ WEBCFG_STATUS processMsgpackSubdoc(char *transaction_id) } if(version != 0) { - checkDBList("root",version, g_ETAG); + checkDBList("root",version, NULL); success_count++; } @@ -1020,7 +1020,7 @@ void getRootVersionFromDB(uint32_t *rt_version, char *rt_string, int *subdoclist WebcfgDebug("*subdoclist is %d\n", *subdoclist); } -void get_root_version_string(char *rootVersion, uint32_t *root_ver, int status) +void get_root_version_string(char **rootVersion, uint32_t *root_ver, int status) { FILE *fp = NULL; char *reason = NULL; @@ -1038,15 +1038,15 @@ void get_root_version_string(char *rootVersion, uint32_t *root_ver, int status) { if(strncmp(reason,"factory-reset",strlen("factory-reset"))==0) { - strcpy(rootVersion, "NONE"); + strcpy(*rootVersion, "NONE"); } else if(strncmp(reason,"Software_upgrade",strlen("Software_upgrade"))==0) { - strcpy(rootVersion, "NONE-MIGRATION"); + strcpy(*rootVersion, "NONE-MIGRATION"); } else { - strcpy(rootVersion, "NONE-REBOOT"); + strcpy(*rootVersion, "NONE-REBOOT"); } } else @@ -1055,21 +1055,22 @@ void get_root_version_string(char *rootVersion, uint32_t *root_ver, int status) getRootVersionFromDB(&db_root_version, db_root_string, &subdocList); WebcfgDebug("db_root_version %lu db_root_string %s subdocList %d\n", (long)db_root_version, db_root_string, subdocList); - if((strncmp(db_root_string,"POST-NONE",strlen("POST-NONE"))==0) && ((strncmp(reason,"Software_upgrade",strlen("Software_upgrade"))!=0) || (strncmp(reason,"factory-reset",strlen("factory-reset"))!=0))) + if((strncmp(db_root_string,"POST-NONE",strlen("POST-NONE"))==0) && ((strncmp(reason,"Software_upgrade",strlen("Software_upgrade"))!=0) && (strncmp(reason,"factory-reset",strlen("factory-reset"))!=0))) { - strcpy(rootVersion, "NONE-REBOOT"); + strcpy(*rootVersion, "NONE-REBOOT"); return; } else if(status == 404 && ((strcmp(db_root_string, "NONE") == 0) || (strcmp(db_root_string, "NONE-MIGRATION") == 0) || (strcmp(db_root_string, "NONE-REBOOT") == 0))) { - strcpy(rootVersion, "POST-NONE"); + strcpy(*rootVersion, "POST-NONE"); return; } //Reset reboot version string to "0" after initial migration and factory reset. - else if(status == 200 && ((strcmp(db_root_string, "NONE") == 0) || (strcmp(db_root_string, "NONE-MIGRATION") == 0) || (strcmp(db_root_string, "NONE-REBOOT") == 0))) + else if(status == 200 && (strcmp(db_root_string, "NONE") == 0)) { - WebcfgInfo("Received config from server, root reset to 0\n"); - strcpy(rootVersion, "0"); + WebcfgInfo("Received factory reset Ack from server, root reset to POST-NONE\n"); + strcpy(*rootVersion, "POST-NONE"); + *root_ver = 0; return; } @@ -1081,16 +1082,17 @@ void get_root_version_string(char *rootVersion, uint32_t *root_ver, int status) if(subdocList > 1 && strncmp(reason,"Software_upgrade",strlen("Software_upgrade"))==0) { WebcfgInfo("reboot due to software migration/rollback, root reset to 0\n"); - strcpy(rootVersion, "0"); + *rootVersion = NULL; + *root_ver = 0; return; } WebcfgDebug("Update rootVersion with db_root_version %lu\n", (long)db_root_version); - sprintf(rootVersion, "%lu", (long)db_root_version); + *rootVersion = NULL; } else { WebcfgInfo("Update rootVersion with db_root_string %s\n", db_root_string); - sprintf(rootVersion, "%s", db_root_string); + sprintf(*rootVersion, "%s", db_root_string); } *root_ver = db_root_version; } @@ -1108,29 +1110,39 @@ This can be increased if required. */ void getConfigVersionList(char *versionsList, int http_status) { char *versionsList_tmp = NULL; - char root_str[32]="0"; + char *root_str = NULL; uint32_t root_version = 0; + root_str = (char*) malloc(32); //initialize to default value "0". sprintf(versionsList, "%s", "0"); - get_root_version_string(root_str, &root_version, http_status); + get_root_version_string(&root_str, &root_version, http_status); WebcfgInfo("root_str is %s\n", root_str); - if(strlen(root_str) >0) + if(root_str !=NULL) { sprintf(versionsList, "%s", root_str); - WebcfgInfo("update root_version %lu rootString %s to DB\n", (long)root_version, root_str); - checkDBList("root", root_version, root_str); - WebcfgInfo("addNewDocEntry. get_successDocCount %d\n", get_successDocCount()); - addNewDocEntry(get_successDocCount()); } + WebcfgInfo("update root_version %lu rootString %s to DB\n", (long)root_version, root_str); + checkDBList("root", root_version, root_str); + WebcfgInfo("addNewDocEntry. get_successDocCount %d\n", get_successDocCount()); + addNewDocEntry(get_successDocCount()); webconfig_db_data_t *temp = NULL; temp = get_global_db_node(); if(NULL != temp) { - sprintf(versionsList, "%s", root_str); + if(root_str!=NULL && strlen(root_str) >0) + { + WebcfgInfo("update root_str %s to versionsList\n", root_str); + sprintf(versionsList, "%s", root_str); + } + else + { + WebcfgInfo("update root_version %lu to versionsList\n", (long)root_version); + sprintf(versionsList, "%lu", (long)root_version); + } WebcfgInfo("versionsList is %s\n", versionsList); while (NULL != temp) @@ -1615,7 +1627,7 @@ void updateRootVersionToDB() } if(version != 0) { - checkDBList("root",version, g_ETAG); + checkDBList("root",version, NULL); } WebcfgDebug("The Etag is %lu\n",(long)version ); From 79e7c70c6ff1cf92c14b66ff8c30cfea0f389371 Mon Sep 17 00:00:00 2001 From: Sadhyama Vengilat Date: Tue, 18 Aug 2020 10:11:10 +0530 Subject: [PATCH 08/21] Fix root string packing to DB when reboot after software upgrade --- src/webcfg_multipart.c | 73 +++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 36 deletions(-) diff --git a/src/webcfg_multipart.c b/src/webcfg_multipart.c index 1e8522f8..0dbf1ebe 100644 --- a/src/webcfg_multipart.c +++ b/src/webcfg_multipart.c @@ -102,7 +102,7 @@ char* generate_trans_uuid(); WEBCFG_STATUS processMsgpackSubdoc(char *transaction_id); void loadInitURLFromFile(char **url); static void get_webCfg_interface(char **interface); -void getRootVersionFromDB(uint32_t *rt_version, char *rt_string, int *subdoclist); +void getRootVersionFromDB(uint32_t *rt_version, char **rt_string, int *subdoclist); char *replaceMacWord(const char *s, const char *macW, const char *deviceMACW); WEBCFG_STATUS checkAkerDoc(); /*----------------------------------------------------------------------------*/ @@ -998,7 +998,7 @@ void getConfigDocList(char *docList) } } -void getRootVersionFromDB(uint32_t *rt_version, char *rt_string, int *subdoclist) +void getRootVersionFromDB(uint32_t *rt_version, char **rt_string, int *subdoclist) { webconfig_db_data_t *temp = NULL; temp = get_global_db_node(); @@ -1010,9 +1010,9 @@ void getRootVersionFromDB(uint32_t *rt_version, char *rt_string, int *subdoclist *rt_version = temp->version; if(temp->root_string !=NULL) { - strcpy(rt_string, temp->root_string); + *rt_string = strdup(temp->root_string); } - WebcfgInfo("rt_version %lu rt_string %s from DB list\n", (long)*rt_version, rt_string); + WebcfgInfo("rt_version %lu rt_string %s from DB list\n", (long)*rt_version, *rt_string); } temp= temp->next; *subdoclist = *subdoclist+1; @@ -1025,7 +1025,7 @@ void get_root_version_string(char **rootVersion, uint32_t *root_ver, int status) FILE *fp = NULL; char *reason = NULL; uint32_t db_root_version = 0; - char db_root_string[32]="0"; + char *db_root_string = NULL; int subdocList = 0; fp = fopen(WEBCFG_DB_FILE,"rb"); @@ -1038,40 +1038,44 @@ void get_root_version_string(char **rootVersion, uint32_t *root_ver, int status) { if(strncmp(reason,"factory-reset",strlen("factory-reset"))==0) { - strcpy(*rootVersion, "NONE"); + *rootVersion = strdup("NONE"); } else if(strncmp(reason,"Software_upgrade",strlen("Software_upgrade"))==0) { - strcpy(*rootVersion, "NONE-MIGRATION"); + *rootVersion = strdup("NONE-MIGRATION"); } else { - strcpy(*rootVersion, "NONE-REBOOT"); + *rootVersion = strdup("NONE-REBOOT"); } } else { //get existing root version from DB - getRootVersionFromDB(&db_root_version, db_root_string, &subdocList); + getRootVersionFromDB(&db_root_version, &db_root_string, &subdocList); WebcfgDebug("db_root_version %lu db_root_string %s subdocList %d\n", (long)db_root_version, db_root_string, subdocList); - if((strncmp(db_root_string,"POST-NONE",strlen("POST-NONE"))==0) && ((strncmp(reason,"Software_upgrade",strlen("Software_upgrade"))!=0) && (strncmp(reason,"factory-reset",strlen("factory-reset"))!=0))) + if(db_root_string !=NULL) { - strcpy(*rootVersion, "NONE-REBOOT"); - return; - } - else if(status == 404 && ((strcmp(db_root_string, "NONE") == 0) || (strcmp(db_root_string, "NONE-MIGRATION") == 0) || (strcmp(db_root_string, "NONE-REBOOT") == 0))) - { - strcpy(*rootVersion, "POST-NONE"); - return; - } - //Reset reboot version string to "0" after initial migration and factory reset. - else if(status == 200 && (strcmp(db_root_string, "NONE") == 0)) - { - WebcfgInfo("Received factory reset Ack from server, root reset to POST-NONE\n"); - strcpy(*rootVersion, "POST-NONE"); - *root_ver = 0; - return; + if((strcmp(db_root_string,"POST-NONE")==0) && ((strcmp(reason,"Software_upgrade")!=0) && (strcmp(reason,"factory-reset")!=0))) + { + *rootVersion = strdup("NONE-REBOOT"); + WEBCFG_FREE(db_root_string); + return; + } + else if(status == 404 && ((strcmp(db_root_string, "NONE") == 0) || (strcmp(db_root_string, "NONE-MIGRATION") == 0) || (strcmp(db_root_string, "NONE-REBOOT") == 0))) + { + *rootVersion = strdup("POST-NONE"); + WEBCFG_FREE(db_root_string); + return; + } + else if(status == 200 && (strcmp(db_root_string, "NONE") == 0)) + { + WebcfgInfo("Received factory reset Ack from server, set root to POST-NONE\n"); + *rootVersion = strdup("POST-NONE"); + WEBCFG_FREE(db_root_string); + return; + } } //check existing root version @@ -1082,17 +1086,19 @@ void get_root_version_string(char **rootVersion, uint32_t *root_ver, int status) if(subdocList > 1 && strncmp(reason,"Software_upgrade",strlen("Software_upgrade"))==0) { WebcfgInfo("reboot due to software migration/rollback, root reset to 0\n"); - *rootVersion = NULL; *root_ver = 0; return; } WebcfgDebug("Update rootVersion with db_root_version %lu\n", (long)db_root_version); - *rootVersion = NULL; } else { - WebcfgInfo("Update rootVersion with db_root_string %s\n", db_root_string); - sprintf(*rootVersion, "%s", db_root_string); + if(db_root_string !=NULL) + { + WebcfgInfo("Update rootVersion with db_root_string %s\n", db_root_string); + *rootVersion = strdup(db_root_string); + WEBCFG_FREE(db_root_string); + } } *root_ver = db_root_version; } @@ -1112,20 +1118,14 @@ void getConfigVersionList(char *versionsList, int http_status) char *versionsList_tmp = NULL; char *root_str = NULL; uint32_t root_version = 0; - root_str = (char*) malloc(32); //initialize to default value "0". sprintf(versionsList, "%s", "0"); get_root_version_string(&root_str, &root_version, http_status); - WebcfgInfo("root_str is %s\n", root_str); - if(root_str !=NULL) - { - sprintf(versionsList, "%s", root_str); - } WebcfgInfo("update root_version %lu rootString %s to DB\n", (long)root_version, root_str); checkDBList("root", root_version, root_str); - WebcfgInfo("addNewDocEntry. get_successDocCount %d\n", get_successDocCount()); + WebcfgDebug("addNewDocEntry. get_successDocCount %d\n", get_successDocCount()); addNewDocEntry(get_successDocCount()); webconfig_db_data_t *temp = NULL; @@ -1137,6 +1137,7 @@ void getConfigVersionList(char *versionsList, int http_status) { WebcfgInfo("update root_str %s to versionsList\n", root_str); sprintf(versionsList, "%s", root_str); + WEBCFG_FREE(root_str); } else { From b47fd3374c9ebaa54f0f43a43ac7ce050a7ae6eb Mon Sep 17 00:00:00 2001 From: Sadhyama Vengilat Date: Thu, 20 Aug 2020 02:56:34 +0530 Subject: [PATCH 09/21] Handle 200 empty response during factory reset NONE --- src/webcfg.c | 8 +++++++- src/webcfg_multipart.c | 25 +++++++++++++++++++++++++ src/webcfg_multipart.h | 1 + 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/webcfg.c b/src/webcfg.c index dc261163..010fd473 100644 --- a/src/webcfg.c +++ b/src/webcfg.c @@ -327,7 +327,13 @@ int handlehttpResponse(long response_code, char *webConfigData, int retry_count, { WebcfgInfo("webConfigData is empty\n"); //After factory reset when server sends 200 with empty config, set POST-NONE root version - getConfigVersionList(version, response_code); + if(strcmp(get_global_contentLen(), "0") == 0) + { + WebcfgInfo("webConfigData content length is 0\n"); + getConfigVersionList(version, response_code); + memset(get_global_contentLen(), 0, 32); + return 1; + } } } else if(response_code == 204) diff --git a/src/webcfg_multipart.c b/src/webcfg_multipart.c index 0dbf1ebe..bad0a01b 100644 --- a/src/webcfg_multipart.c +++ b/src/webcfg_multipart.c @@ -35,6 +35,7 @@ /*----------------------------------------------------------------------------*/ #define MAX_HEADER_LEN 4096 #define ETAG_HEADER "Etag:" +#define CONTENT_LENGTH_HEADER "Content-Length:" #define CURL_TIMEOUT_SEC 25L #define CA_CERT_PATH "/etc/ssl/certs/ca-certificates.crt" #define WEBPA_READ_HEADER "/etc/parodus/parodus_read_file.sh" @@ -61,6 +62,7 @@ static char g_bootTime[64]={'\0'}; static char g_productClass[64]={'\0'}; static char g_ModelName[64]={'\0'}; static char g_transID[64]={'\0'}; +static char g_contentLen[32]={'\0'}; multipart_t *mp = NULL; pthread_mutex_t multipart_t_mut =PTHREAD_MUTEX_INITIALIZER; static int eventFlag = 0; @@ -88,6 +90,10 @@ void set_global_mp(multipart_t *new) mp = new; } +char * get_global_contentLen(void) +{ + return g_contentLen; +} /*----------------------------------------------------------------------------*/ /* Function Prototypes */ /*----------------------------------------------------------------------------*/ @@ -799,8 +805,10 @@ size_t headr_callback(char *buffer, size_t size, size_t nitems) char* header_value = NULL; char* final_header = NULL; char header_str[64] = {'\0'}; + size_t content_len = 0; etag_len = strlen(ETAG_HEADER); + content_len = strlen(CONTENT_LENGTH_HEADER); if( nitems > etag_len ) { if( strncasecmp(ETAG_HEADER, buffer, etag_len) == 0 ) @@ -818,6 +826,23 @@ size_t headr_callback(char *buffer, size_t size, size_t nitems) } } } + + if( strncasecmp(CONTENT_LENGTH_HEADER, buffer, content_len) == 0 ) + { + header_value = strtok(buffer, ":"); + while( header_value != NULL ) + { + header_value = strtok(NULL, ":"); + if(header_value !=NULL) + { + strncpy(header_str, header_value, sizeof(header_str)-1); + stripspaces(header_str, &final_header); + + strncpy(g_contentLen, final_header, sizeof(g_contentLen)-1); + } + } + WebcfgInfo("g_contentLen is %s\n", g_contentLen); + } } WebcfgInfo("header_callback size %zu\n", size); return nitems; diff --git a/src/webcfg_multipart.h b/src/webcfg_multipart.h index d11c1f4d..f9721b26 100644 --- a/src/webcfg_multipart.h +++ b/src/webcfg_multipart.h @@ -58,4 +58,5 @@ WEBCFG_STATUS validate_request_param(param_t *reqParam, int paramCount); char * get_global_rootString(void); void set_global_rootString(char *rootStr); void getConfigVersionList(char *versionsList, int http_status); +char * get_global_contentLen(void); #endif From a4a1c524d4a7caa6e45f0f6fb814ed67eeb1c7fe Mon Sep 17 00:00:00 2001 From: Sadhyama Vengilat Date: Thu, 20 Aug 2020 14:45:56 +0530 Subject: [PATCH 10/21] Fix crash when 200 empty response with valid content-type multipart/mixed --- src/webcfg.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/webcfg.c b/src/webcfg.c index 010fd473..9e1601a8 100644 --- a/src/webcfg.c +++ b/src/webcfg.c @@ -306,7 +306,7 @@ int handlehttpResponse(long response_code, char *webConfigData, int retry_count, { WebcfgInfo("webConfig is not in sync with cloud. response_code:%ld\n", response_code); - if(webConfigData !=NULL) + if(webConfigData !=NULL && (strlen(webConfigData)>0)) { WebcfgDebug("webConfigData fetched successfully\n"); WebcfgDebug("parseMultipartDocument\n"); From c770b97f5968e5313c7bee5cd526a986d47f4d2c Mon Sep 17 00:00:00 2001 From: Sadhyama Vengilat Date: Thu, 20 Aug 2020 18:37:38 +0530 Subject: [PATCH 11/21] root version logging improvements --- src/webcfg_db.c | 12 +++++++----- src/webcfg_multipart.c | 10 +++++----- src/webcfg_multipart.h | 2 -- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/webcfg_db.c b/src/webcfg_db.c index b61724d0..8121876b 100644 --- a/src/webcfg_db.c +++ b/src/webcfg_db.c @@ -515,7 +515,7 @@ WEBCFG_STATUS updateDBlist(char *docname, uint32_t version, char* rootstr) { webcfgdb->root_string = NULL; } - WebcfgInfo("webcfgdb %s is updated to version %lu webcfgdb->root_string %s\n", docname, (long)webcfgdb->version, webcfgdb->root_string); + WebcfgDebug("webcfgdb %s is updated to version %lu webcfgdb->root_string %s\n", docname, (long)webcfgdb->version, webcfgdb->root_string); pthread_mutex_unlock (&webconfig_db_mut); WebcfgDebug("mutex_unlock if docname is webcfgdb name\n"); return WEBCFG_SUCCESS; @@ -709,7 +709,7 @@ int process_webcfgdbparams( webconfig_db_data_t *e, msgpack_object_map *map ) { if( (1 << 0) & objects_left ) { - WebcfgInfo("Skip optional root_string element\n"); + WebcfgDebug("Skip optional root_string element\n"); objects_left &= ~(1 << 0); } } @@ -717,6 +717,7 @@ int process_webcfgdbparams( webconfig_db_data_t *e, msgpack_object_map *map ) { errno = WD_OK; } + return (0 == objects_left) ? 0 : -1; } @@ -781,7 +782,7 @@ void addToDBList(webconfig_db_data_t *webcfgdb) webcfgdb_data = webcfgdb; pthread_mutex_unlock (&webconfig_db_mut); success_doc_count++; - WebcfgInfo("Producer added webcfgdb->name %s, webcfg->version %lu, webcfgdb->root_string %s, success_doc_count %d\n",webcfgdb->name, (long)webcfgdb->version, webcfgdb->root_string, success_doc_count); + WebcfgInfo("Producer added webcfgdb->name %s, webcfg->version %lu, success_doc_count %d\n",webcfgdb->name, (long)webcfgdb->version, success_doc_count); } else { @@ -794,7 +795,7 @@ void addToDBList(webconfig_db_data_t *webcfgdb) temp->next = webcfgdb; pthread_mutex_unlock (&webconfig_db_mut); success_doc_count++; - WebcfgInfo("Producer added webcfgdb->name %s, webcfg->version %lu, webcfgdb->root_string %s, success_doc_count %d\n",webcfgdb->name, (long)webcfgdb->version, webcfgdb->root_string, success_doc_count); + WebcfgInfo("Producer added webcfgdb->name %s, webcfg->version %lu, success_doc_count %d\n",webcfgdb->name, (long)webcfgdb->version, success_doc_count); } } @@ -938,13 +939,14 @@ int process_webcfgdbblobparams( blob_data_t *e, msgpack_object_map *map ) { } else if( (1 << 3) & objects_left ) { - WebcfgInfo("Skip optional root_string element\n"); + WebcfgDebug("Skip optional root_string element\n"); objects_left &= ~(1 << 3); } else { errno = BD_OK; } + return (0 == objects_left) ? 0 : -1; } diff --git a/src/webcfg_multipart.c b/src/webcfg_multipart.c index bad0a01b..5922b0f9 100644 --- a/src/webcfg_multipart.c +++ b/src/webcfg_multipart.c @@ -732,7 +732,7 @@ WEBCFG_STATUS processMsgpackSubdoc(char *transaction_id) while(temp1 ) { - WebcfgInfo("DB wd->entries[%lu].name: %s, version: %lu root_string: %s\n", success_count-j, temp1->name, (long)temp1->version, temp1->root_string); + WebcfgInfo("DB wd->entries[%lu].name: %s, version: %lu\n", success_count-j, temp1->name, (long)temp1->version); j--; temp1 = temp1->next; } @@ -1037,7 +1037,7 @@ void getRootVersionFromDB(uint32_t *rt_version, char **rt_string, int *subdoclis { *rt_string = strdup(temp->root_string); } - WebcfgInfo("rt_version %lu rt_string %s from DB list\n", (long)*rt_version, *rt_string); + WebcfgDebug("rt_version %lu rt_string %s from DB list\n", (long)*rt_version, *rt_string); } temp= temp->next; *subdoclist = *subdoclist+1; @@ -1148,7 +1148,7 @@ void getConfigVersionList(char *versionsList, int http_status) sprintf(versionsList, "%s", "0"); get_root_version_string(&root_str, &root_version, http_status); - WebcfgInfo("update root_version %lu rootString %s to DB\n", (long)root_version, root_str); + WebcfgDebug("update root_version %lu rootString %s to DB\n", (long)root_version, root_str); checkDBList("root", root_version, root_str); WebcfgDebug("addNewDocEntry. get_successDocCount %d\n", get_successDocCount()); addNewDocEntry(get_successDocCount()); @@ -1160,13 +1160,13 @@ void getConfigVersionList(char *versionsList, int http_status) { if(root_str!=NULL && strlen(root_str) >0) { - WebcfgInfo("update root_str %s to versionsList\n", root_str); + WebcfgDebug("update root_str %s to versionsList\n", root_str); sprintf(versionsList, "%s", root_str); WEBCFG_FREE(root_str); } else { - WebcfgInfo("update root_version %lu to versionsList\n", (long)root_version); + WebcfgDebug("update root_version %lu to versionsList\n", (long)root_version); sprintf(versionsList, "%lu", (long)root_version); } WebcfgInfo("versionsList is %s\n", versionsList); diff --git a/src/webcfg_multipart.h b/src/webcfg_multipart.h index f9721b26..7ef71598 100644 --- a/src/webcfg_multipart.h +++ b/src/webcfg_multipart.h @@ -55,8 +55,6 @@ void set_global_mp(multipart_t *new); void reqParam_destroy( int paramCnt, param_t *reqObj ); void failedDocsRetry(); WEBCFG_STATUS validate_request_param(param_t *reqParam, int paramCount); -char * get_global_rootString(void); -void set_global_rootString(char *rootStr); void getConfigVersionList(char *versionsList, int http_status); char * get_global_contentLen(void); #endif From b2d78b722758b689e23dde35fe0ef69895db56d2 Mon Sep 17 00:00:00 2001 From: Sadhyama Vengilat Date: Thu, 20 Aug 2020 19:37:26 +0530 Subject: [PATCH 12/21] Logging improvement on processMsgpackSubdoc --- src/webcfg.c | 2 +- src/webcfg_multipart.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/webcfg.c b/src/webcfg.c index 9e1601a8..292edf75 100644 --- a/src/webcfg.c +++ b/src/webcfg.c @@ -319,7 +319,7 @@ int handlehttpResponse(long response_code, char *webConfigData, int retry_count, } else { - WebcfgError("Failed to apply root webConfigData received from server\n"); + WebcfgInfo("root webConfigData processed, waiting for apply status events\n"); return 1; } } diff --git a/src/webcfg_multipart.c b/src/webcfg_multipart.c index 5922b0f9..de6431c6 100644 --- a/src/webcfg_multipart.c +++ b/src/webcfg_multipart.c @@ -438,7 +438,7 @@ WEBCFG_STATUS parseMultipartDocument(void *config_data, char *ct , size_t data_s } else { - WebcfgError("processMsgpackSubdoc failed,as all the docs are not applied\n"); + WebcfgDebug("processMsgpackSubdoc done,docs are sent for apply\n"); } return WEBCFG_FAILURE; } From fb7b467fdab48ef78f7d77112257e4dcbd915fad Mon Sep 17 00:00:00 2001 From: Sadhyama Vengilat Date: Mon, 24 Aug 2020 01:42:41 +0530 Subject: [PATCH 13/21] Sync notification event for root doc status --- src/webcfg.c | 38 ++++++++++++++++++++++++++++++++++++++ src/webcfg_aker.c | 4 ++-- src/webcfg_event.c | 16 ++++++++-------- src/webcfg_multipart.c | 7 +++---- src/webcfg_multipart.h | 1 + src/webcfg_notify.c | 37 +++++++++++++++++++++++++++++++------ src/webcfg_notify.h | 3 ++- 7 files changed, 85 insertions(+), 21 deletions(-) diff --git a/src/webcfg.c b/src/webcfg.c index 292edf75..88326aa5 100644 --- a/src/webcfg.c +++ b/src/webcfg.c @@ -295,10 +295,20 @@ int handlehttpResponse(long response_code, char *webConfigData, int retry_count, int msgpack_status=0; int err = 0; char version[512]={'\0'}; + uint32_t db_root_version = 0; + char *db_root_string = NULL; + int subdocList = 0; if(response_code == 304) { WebcfgInfo("webConfig is in sync with cloud. response_code:%ld\n", response_code); + getRootVersionFromDB(&db_root_version, &db_root_string, &subdocList); + WebcfgInfo("db_root_version %lu db_root_string %s\n", (long)db_root_version, db_root_string); + addWebConfgNotifyMsg(NULL, db_root_version, NULL, NULL, transaction_uuid, 0, "status", 0, db_root_string, response_code); + if(db_root_string !=NULL) + { + WEBCFG_FREE(db_root_string); + } WEBCFG_FREE(transaction_uuid); return 1; } @@ -339,6 +349,13 @@ int handlehttpResponse(long response_code, char *webConfigData, int retry_count, else if(response_code == 204) { WebcfgInfo("No configuration available for this device. response_code:%ld\n", response_code); + getRootVersionFromDB(&db_root_version, &db_root_string, &subdocList); + WebcfgInfo("db_root_version %lu db_root_string %s\n", (long)db_root_version, db_root_string); + addWebConfgNotifyMsg(NULL, db_root_version, NULL, NULL, transaction_uuid, 0, "status", 0, db_root_string, response_code); + if(db_root_string !=NULL) + { + WEBCFG_FREE(db_root_string); + } WEBCFG_FREE(transaction_uuid); return 1; } @@ -352,6 +369,13 @@ int handlehttpResponse(long response_code, char *webConfigData, int retry_count, else if(response_code == 429) { WebcfgInfo("No action required from client. response_code:%ld\n", response_code); + getRootVersionFromDB(&db_root_version, &db_root_string, &subdocList); + WebcfgInfo("db_root_version %lu db_root_string %s\n", (long)db_root_version, db_root_string); + addWebConfgNotifyMsg(NULL, db_root_version, NULL, NULL, transaction_uuid, 0, "status", 0, db_root_string, response_code); + if(db_root_string !=NULL) + { + WEBCFG_FREE(db_root_string); + } WEBCFG_FREE(transaction_uuid); return 1; } @@ -364,6 +388,13 @@ int handlehttpResponse(long response_code, char *webConfigData, int retry_count, //To set POST-NONE root version when 404 getConfigVersionList(version, response_code); } + getRootVersionFromDB(&db_root_version, &db_root_string, &subdocList); + WebcfgInfo("db_root_version %lu db_root_string %s\n", (long)db_root_version, db_root_string); + addWebConfgNotifyMsg(NULL, db_root_version, NULL, NULL, transaction_uuid, 0, "status", 0, db_root_string, response_code); + if(db_root_string !=NULL) + { + WEBCFG_FREE(db_root_string); + } WEBCFG_FREE(transaction_uuid); return 1; } @@ -373,6 +404,13 @@ int handlehttpResponse(long response_code, char *webConfigData, int retry_count, if(retry_count == 3 && !err) { WebcfgDebug("3 curl retry attempts\n"); + getRootVersionFromDB(&db_root_version, &db_root_string, &subdocList); + WebcfgInfo("db_root_version %lu db_root_string %s\n", (long)db_root_version, db_root_string); + addWebConfgNotifyMsg(NULL, db_root_version, NULL, NULL, transaction_uuid, 0, "status", 0, db_root_string, response_code); + if(db_root_string !=NULL) + { + WEBCFG_FREE(db_root_string); + } WEBCFG_FREE(transaction_uuid); return 0; } diff --git a/src/webcfg_aker.c b/src/webcfg_aker.c index 44f443a1..36daab9e 100644 --- a/src/webcfg_aker.c +++ b/src/webcfg_aker.c @@ -222,7 +222,7 @@ void updateAkerMaxRetry(webconfig_tmp_data_t *temp, char *docname) if( strcmp(docname, temp->name) == 0) { updateTmpList(temp, temp->name, temp->version, "failed", "aker_service_unavailable", 0, 0, 0); - addWebConfgNotifyMsg(temp->name, temp->version, "failed", "aker_service_unavailable", get_global_transID(),0, "status", 0); + addWebConfgNotifyMsg(temp->name, temp->version, "failed", "aker_service_unavailable", get_global_transID(),0, "status", 0, NULL, 200); return; } } @@ -314,7 +314,7 @@ AKER_STATUS processAkerSubdoc(webconfig_tmp_data_t *docNode, int akerIndex) { //Invalid aker request updateTmpList(docNode, gmp->entries[m].name_space, gmp->entries[m].etag, "failed", "doc_rejected", 0, 0, 0); - addWebConfgNotifyMsg(gmp->entries[m].name_space, gmp->entries[m].etag, "failed", "doc_rejected", get_global_transID(),0, "status", 0); + addWebConfgNotifyMsg(gmp->entries[m].name_space, gmp->entries[m].etag, "failed", "doc_rejected", get_global_transID(),0, "status", 0, NULL, 200); rv = AKER_FAILURE; } reqParam_destroy(paramCount, reqParam); diff --git a/src/webcfg_event.c b/src/webcfg_event.c index 6a466500..a83837b3 100644 --- a/src/webcfg_event.c +++ b/src/webcfg_event.c @@ -300,7 +300,7 @@ void* processSubdocEvents() WebcfgDebug("err_details : %s, err_code : %lu\n", err_details, (long) eventParam->err_code); updateTmpList(subdoc_node, eventParam->subdoc_name, eventParam->version, "failed", err_details, eventParam->err_code, eventParam->trans_id, 0); WebcfgDebug("get_global_transID is %s\n", get_global_transID()); - addWebConfgNotifyMsg(eventParam->subdoc_name, eventParam->version, "failed", err_details, get_global_transID(),eventParam->timeout, "status", eventParam->err_code); + addWebConfgNotifyMsg(eventParam->subdoc_name, eventParam->version, "failed", err_details, get_global_transID(),eventParam->timeout, "status", eventParam->err_code, NULL, 200); } else { @@ -322,7 +322,7 @@ void* processSubdocEvents() docVersion = getDocVersionFromTmpList(subdoc_node, eventParam->subdoc_name); } WebcfgDebug("docVersion %lu\n", (long) docVersion); - addWebConfgNotifyMsg(eventParam->subdoc_name, docVersion, "pending", "timer_expired", get_global_transID(),eventParam->timeout, "status", 0); + addWebConfgNotifyMsg(eventParam->subdoc_name, docVersion, "pending", "timer_expired", get_global_transID(),eventParam->timeout, "status", 0, NULL, 200); WebcfgDebug("retryMultipartSubdoc for EXPIRE case\n"); rs = retryMultipartSubdoc(subdoc_node, eventParam->subdoc_name); if(rs == WEBCFG_SUCCESS) @@ -343,7 +343,7 @@ void* processSubdocEvents() if((getDocVersionFromTmpList(subdoc_node, eventParam->subdoc_name))== eventParam->version) { startWebcfgTimer(doctimer_node, eventParam->subdoc_name, eventParam->trans_id, eventParam->timeout); - addWebConfgNotifyMsg(eventParam->subdoc_name, eventParam->version, "pending", NULL, get_global_transID(),eventParam->timeout, "ack", 0); + addWebConfgNotifyMsg(eventParam->subdoc_name, eventParam->version, "pending", NULL, get_global_transID(),eventParam->timeout, "ack", 0, NULL, 200); } else { @@ -555,7 +555,7 @@ void createTimerExpiryEvent(char *docName, uint16_t transid) void sendSuccessNotification(webconfig_tmp_data_t *subdoc_node, char *name, uint32_t version, uint16_t txid) { updateTmpList(subdoc_node, name, version, "success", "none", 0, txid, 0); - addWebConfgNotifyMsg(name, version, "success", "none", get_global_transID(),0, "status",0); + addWebConfgNotifyMsg(name, version, "success", "none", get_global_transID(),0, "status",0, NULL, 200); deleteFromTmpList(name); } @@ -856,7 +856,7 @@ WEBCFG_STATUS retryMultipartSubdoc(webconfig_tmp_data_t *docNode, char *docName) WebcfgDebug("For scalar docs, update trans_id as 0\n"); updateTmpList(docNode, gmp->entries[m].name_space, gmp->entries[m].etag, "success", "none", 0, 0, 0); //send scalar success notification, delete tmp, updateDB - addWebConfgNotifyMsg(gmp->entries[m].name_space, gmp->entries[m].etag, "success", "none", get_global_transID(), 0, "status", 0); + addWebConfgNotifyMsg(gmp->entries[m].name_space, gmp->entries[m].etag, "success", "none", get_global_transID(), 0, "status", 0, NULL, 200); WebcfgDebug("deleteFromTmpList as scalar doc is applied\n"); deleteFromTmpList(gmp->entries[m].name_space); checkDBList(gmp->entries[m].name_space,gmp->entries[m].etag, NULL); @@ -885,7 +885,7 @@ WEBCFG_STATUS retryMultipartSubdoc(webconfig_tmp_data_t *docNode, char *docName) snprintf(result,MAX_VALUE_LEN,"failed_retrying:%s", errDetails); WebcfgDebug("The result is %s\n",result); updateTmpList(docNode, gmp->entries[m].name_space, gmp->entries[m].etag, "pending", result, ccspStatus, 0, 1); - addWebConfgNotifyMsg(gmp->entries[m].name_space, gmp->entries[m].etag, "pending", result, get_global_transID(), 0,"status",ccspStatus); + addWebConfgNotifyMsg(gmp->entries[m].name_space, gmp->entries[m].etag, "pending", result, get_global_transID(), 0,"status",ccspStatus, NULL, 200); set_doc_fail(1); WebcfgDebug("the retry flag value is %d\n", get_doc_fail()); } @@ -894,7 +894,7 @@ WEBCFG_STATUS retryMultipartSubdoc(webconfig_tmp_data_t *docNode, char *docName) snprintf(result,MAX_VALUE_LEN,"doc_rejected:%s", errDetails); WebcfgDebug("The result is %s\n",result); updateTmpList(docNode, gmp->entries[m].name_space, gmp->entries[m].etag, "failed", result, ccspStatus, 0, 0); - addWebConfgNotifyMsg(gmp->entries[m].name_space, gmp->entries[m].etag, "failed", result, get_global_transID(), 0, "status", ccspStatus); + addWebConfgNotifyMsg(gmp->entries[m].name_space, gmp->entries[m].etag, "failed", result, get_global_transID(), 0, "status", ccspStatus, NULL, 200); } } reqParam_destroy(paramCount, reqParam); @@ -948,7 +948,7 @@ WEBCFG_STATUS checkAndUpdateTmpRetryCount(webconfig_tmp_data_t *temp, char *docn { WebcfgInfo("Apply retry_count %d has reached max limit for doc %s\n", temp->retry_count, docname); //send max retry notification to cloud. - addWebConfgNotifyMsg(temp->name, temp->version, "failed", "max_retry_reached", get_global_transID(),0,"status",0); + addWebConfgNotifyMsg(temp->name, temp->version, "failed", "max_retry_reached", get_global_transID(),0,"status",0, NULL, 200); WebcfgDebug("update max_retry_reached to tmp list: ccsp error code %hu\n", temp->error_code); updateTmpList(temp, temp->name, temp->version, "failed", "max_retry_reached", temp->error_code, 0, 1); return WEBCFG_FAILURE; diff --git a/src/webcfg_multipart.c b/src/webcfg_multipart.c index de6431c6..1d03d868 100644 --- a/src/webcfg_multipart.c +++ b/src/webcfg_multipart.c @@ -108,7 +108,6 @@ char* generate_trans_uuid(); WEBCFG_STATUS processMsgpackSubdoc(char *transaction_id); void loadInitURLFromFile(char **url); static void get_webCfg_interface(char **interface); -void getRootVersionFromDB(uint32_t *rt_version, char **rt_string, int *subdoclist); char *replaceMacWord(const char *s, const char *macW, const char *deviceMACW); WEBCFG_STATUS checkAkerDoc(); /*----------------------------------------------------------------------------*/ @@ -596,7 +595,7 @@ WEBCFG_STATUS processMsgpackSubdoc(char *transaction_id) updateTmpList(subdoc_node, mp->entries[m].name_space, mp->entries[m].etag, "success", "none", 0, 0, 0); //send success notification to cloud WebcfgDebug("send notify for mp->entries[m].name_space %s\n", mp->entries[m].name_space); - addWebConfgNotifyMsg(mp->entries[m].name_space, mp->entries[m].etag, "success", "none", trans_id,0, "status",0); + addWebConfgNotifyMsg(mp->entries[m].name_space, mp->entries[m].etag, "success", "none", trans_id,0, "status",0, NULL, 200); WebcfgDebug("deleteFromTmpList as doc is applied\n"); deleteFromTmpList(mp->entries[m].name_space); checkDBList(mp->entries[m].name_space,mp->entries[m].etag, NULL); @@ -645,7 +644,7 @@ WEBCFG_STATUS processMsgpackSubdoc(char *transaction_id) snprintf(result,MAX_VALUE_LEN,"crash_retrying:%s", errDetails); WebcfgDebug("The result is %s\n",result); updateTmpList(subdoc_node, mp->entries[m].name_space, mp->entries[m].etag, "failed", result, ccspStatus, 0, 1); - addWebConfgNotifyMsg(mp->entries[m].name_space, mp->entries[m].etag, "failed", result, trans_id,0,"status",ccspStatus); + addWebConfgNotifyMsg(mp->entries[m].name_space, mp->entries[m].etag, "failed", result, trans_id,0,"status",ccspStatus, NULL, 200); set_doc_fail(1); WebcfgDebug("the retry flag value is %d\n", get_doc_fail()); } @@ -654,7 +653,7 @@ WEBCFG_STATUS processMsgpackSubdoc(char *transaction_id) snprintf(result,MAX_VALUE_LEN,"doc_rejected:%s", errDetails); WebcfgDebug("The result is %s\n",result); updateTmpList(subdoc_node, mp->entries[m].name_space, mp->entries[m].etag, "failed", result, ccspStatus, 0, 0); - addWebConfgNotifyMsg(mp->entries[m].name_space, mp->entries[m].etag, "failed", result, trans_id,0, "status", ccspStatus); + addWebConfgNotifyMsg(mp->entries[m].name_space, mp->entries[m].etag, "failed", result, trans_id,0, "status", ccspStatus, NULL, 200); } //print_tmp_doc_list(mp->entries_count); } diff --git a/src/webcfg_multipart.h b/src/webcfg_multipart.h index 7ef71598..31008ea3 100644 --- a/src/webcfg_multipart.h +++ b/src/webcfg_multipart.h @@ -57,4 +57,5 @@ void failedDocsRetry(); WEBCFG_STATUS validate_request_param(param_t *reqParam, int paramCount); void getConfigVersionList(char *versionsList, int http_status); char * get_global_contentLen(void); +void getRootVersionFromDB(uint32_t *rt_version, char **rt_string, int *subdoclist); #endif diff --git a/src/webcfg_notify.c b/src/webcfg_notify.c index b1d55775..747cc557 100644 --- a/src/webcfg_notify.c +++ b/src/webcfg_notify.c @@ -68,7 +68,7 @@ void initWebConfigNotifyTask() } -void addWebConfgNotifyMsg(char *docname, uint32_t version, char *status, char *error_details, char *transaction_uuid, uint32_t timeout, char *type, uint16_t error_code) +void addWebConfgNotifyMsg(char *docname, uint32_t version, char *status, char *error_details, char *transaction_uuid, uint32_t timeout, char *type, uint16_t error_code, char *root_string, long response_code) { notify_params_t *args = NULL; @@ -95,7 +95,16 @@ void addWebConfgNotifyMsg(char *docname, uint32_t version, char *status, char *e args->error_details = strdup(error_details); } - snprintf(versionStr, sizeof(versionStr), "%lu", (long)version); + if(version ==0 && root_string !=NULL) + { + WebcfgInfo("Update root_string %s to notify struct\n", root_string); + strcpy(versionStr, root_string); + } + else + { + snprintf(versionStr, sizeof(versionStr), "%lu", (long)version); + } + WebcfgInfo("versionStr is %s\n", versionStr); if(strlen(versionStr) > 0) { @@ -114,7 +123,9 @@ void addWebConfgNotifyMsg(char *docname, uint32_t version, char *status, char *e args->error_code = error_code; - WebcfgDebug("args->name:%s,args->application_status:%s,args->timeout:%lu,args->error_details:%s,args->version:%s,args->transaction_uuid:%s,args->type:%s,args->error_code:%lu\n",args->name,args->application_status, (long)args->timeout, args->error_details, args->version, args->transaction_uuid, args->type, (long)args->error_code ); + args->response_code = response_code; + + WebcfgInfo("args->name:%s,args->application_status:%s,args->timeout:%lu,args->error_details:%s,args->version:%s,args->transaction_uuid:%s,args->type:%s,args->error_code:%lu,args->response_code:%lu\n",args->name,args->application_status, (long)args->timeout, args->error_details, args->version, args->transaction_uuid, args->type, (long)args->error_code,args->response_code ); args->next=NULL; @@ -183,7 +194,10 @@ void* processWebConfgNotification() if(msg) { - cJSON_AddStringToObject(notifyPayload,"namespace", (NULL != msg->name && (strlen(msg->name)!=0)) ? msg->name : "unknown"); + if(msg->name !=NULL) + { + cJSON_AddStringToObject(notifyPayload,"namespace", (NULL != msg->name && (strlen(msg->name)!=0)) ? msg->name : "unknown"); + } if(msg->application_status !=NULL) { cJSON_AddStringToObject(notifyPayload,"application_status", msg->application_status); @@ -202,15 +216,26 @@ void* processWebConfgNotification() { cJSON_AddStringToObject(notifyPayload,"error_details", (NULL != msg->error_details) ? msg->error_details : "unknown"); } + if( msg->response_code != 200 ) + { + cJSON_AddNumberToObject(notifyPayload,"http_status_code", msg->response_code); + } cJSON_AddStringToObject(notifyPayload,"transaction_uuid", (NULL != msg->transaction_uuid && (strlen(msg->transaction_uuid)!=0)) ? msg->transaction_uuid : "unknown"); - cJSON_AddStringToObject(notifyPayload,"version", (NULL != msg->version && (strlen(msg->version)!=0)) ? msg->version : "NONE"); + cJSON_AddStringToObject(notifyPayload,"version", (NULL != msg->version && (strlen(msg->version)!=0)) ? msg->version : "0"); } stringifiedNotifyPayload = cJSON_PrintUnformatted(notifyPayload); cJSON_Delete(notifyPayload); } - snprintf(dest,sizeof(dest),"event:subdoc-report/%s/%s/%s",msg->name,device_id,msg->type); + if(msg->response_code == 200) + { + snprintf(dest,sizeof(dest),"event:subdoc-report/%s/%s/%s",msg->name,device_id,msg->type); + } + else + { + snprintf(dest,sizeof(dest),"event:rootdoc-report/%s/%s",device_id,msg->type); + } WebcfgInfo("dest is %s\n", dest); if (stringifiedNotifyPayload != NULL && strlen(device_id) != 0) diff --git a/src/webcfg_notify.h b/src/webcfg_notify.h index 9a7daf36..c658e949 100644 --- a/src/webcfg_notify.h +++ b/src/webcfg_notify.h @@ -32,10 +32,11 @@ typedef struct _notify_params char * type; uint32_t timeout; uint16_t error_code; + long response_code; struct _notify_params *next; } notify_params_t; void initWebConfigNotifyTask(); pthread_t get_global_notify_threadid(); -void addWebConfgNotifyMsg(char *docname, uint32_t version, char *status, char *error_details, char *transaction_uuid, uint32_t timeout,char* type, uint16_t error_code); +void addWebConfgNotifyMsg(char *docname, uint32_t version, char *status, char *error_details, char *transaction_uuid, uint32_t timeout,char* type, uint16_t error_code, char *root_string, long response_code); #endif From 00c5a6e80b0679701416f3c3b1742c796c65126d Mon Sep 17 00:00:00 2001 From: Sadhyama Vengilat Date: Mon, 24 Aug 2020 20:36:33 +0530 Subject: [PATCH 14/21] Avoid getRebootReason multiple dbus query, Modify contentLength reset --- src/webcfg.c | 11 ++++------- src/webcfg_db.c | 4 ++-- src/webcfg_multipart.c | 40 +++++++++++++++++++++++++--------------- src/webcfg_notify.c | 4 +--- 4 files changed, 32 insertions(+), 27 deletions(-) diff --git a/src/webcfg.c b/src/webcfg.c index 88326aa5..14d68ca9 100644 --- a/src/webcfg.c +++ b/src/webcfg.c @@ -298,12 +298,12 @@ int handlehttpResponse(long response_code, char *webConfigData, int retry_count, uint32_t db_root_version = 0; char *db_root_string = NULL; int subdocList = 0; + char *contentLength = NULL; if(response_code == 304) { WebcfgInfo("webConfig is in sync with cloud. response_code:%ld\n", response_code); getRootVersionFromDB(&db_root_version, &db_root_string, &subdocList); - WebcfgInfo("db_root_version %lu db_root_string %s\n", (long)db_root_version, db_root_string); addWebConfgNotifyMsg(NULL, db_root_version, NULL, NULL, transaction_uuid, 0, "status", 0, db_root_string, response_code); if(db_root_string !=NULL) { @@ -337,11 +337,12 @@ int handlehttpResponse(long response_code, char *webConfigData, int retry_count, { WebcfgInfo("webConfigData is empty\n"); //After factory reset when server sends 200 with empty config, set POST-NONE root version - if(strcmp(get_global_contentLen(), "0") == 0) + contentLength = get_global_contentLen(); + if((contentLength !=NULL) && (strcmp(contentLength, "0") == 0)) { WebcfgInfo("webConfigData content length is 0\n"); getConfigVersionList(version, response_code); - memset(get_global_contentLen(), 0, 32); + WEBCFG_FREE(contentLength); return 1; } } @@ -350,7 +351,6 @@ int handlehttpResponse(long response_code, char *webConfigData, int retry_count, { WebcfgInfo("No configuration available for this device. response_code:%ld\n", response_code); getRootVersionFromDB(&db_root_version, &db_root_string, &subdocList); - WebcfgInfo("db_root_version %lu db_root_string %s\n", (long)db_root_version, db_root_string); addWebConfgNotifyMsg(NULL, db_root_version, NULL, NULL, transaction_uuid, 0, "status", 0, db_root_string, response_code); if(db_root_string !=NULL) { @@ -370,7 +370,6 @@ int handlehttpResponse(long response_code, char *webConfigData, int retry_count, { WebcfgInfo("No action required from client. response_code:%ld\n", response_code); getRootVersionFromDB(&db_root_version, &db_root_string, &subdocList); - WebcfgInfo("db_root_version %lu db_root_string %s\n", (long)db_root_version, db_root_string); addWebConfgNotifyMsg(NULL, db_root_version, NULL, NULL, transaction_uuid, 0, "status", 0, db_root_string, response_code); if(db_root_string !=NULL) { @@ -389,7 +388,6 @@ int handlehttpResponse(long response_code, char *webConfigData, int retry_count, getConfigVersionList(version, response_code); } getRootVersionFromDB(&db_root_version, &db_root_string, &subdocList); - WebcfgInfo("db_root_version %lu db_root_string %s\n", (long)db_root_version, db_root_string); addWebConfgNotifyMsg(NULL, db_root_version, NULL, NULL, transaction_uuid, 0, "status", 0, db_root_string, response_code); if(db_root_string !=NULL) { @@ -405,7 +403,6 @@ int handlehttpResponse(long response_code, char *webConfigData, int retry_count, { WebcfgDebug("3 curl retry attempts\n"); getRootVersionFromDB(&db_root_version, &db_root_string, &subdocList); - WebcfgInfo("db_root_version %lu db_root_string %s\n", (long)db_root_version, db_root_string); addWebConfgNotifyMsg(NULL, db_root_version, NULL, NULL, transaction_uuid, 0, "status", 0, db_root_string, response_code); if(db_root_string !=NULL) { diff --git a/src/webcfg_db.c b/src/webcfg_db.c index 8121876b..ff72e5e1 100644 --- a/src/webcfg_db.c +++ b/src/webcfg_db.c @@ -144,8 +144,8 @@ WEBCFG_STATUS addNewDocEntry(size_t count) WebcfgDebug("DB docs count %ld\n", (size_t)count); webcfgdbPackSize = webcfgdb_pack(webcfgdb_data, &data, count); - WebcfgInfo("size of webcfgdbPackSize %ld\n", webcfgdbPackSize); - WebcfgInfo("writeToDBFile %s\n", WEBCFG_DB_FILE); + WebcfgDebug("size of webcfgdbPackSize %ld\n", webcfgdbPackSize); + WebcfgDebug("writeToDBFile %s\n", WEBCFG_DB_FILE); writeToDBFile(WEBCFG_DB_FILE,(char *)data,webcfgdbPackSize); if(data) { diff --git a/src/webcfg_multipart.c b/src/webcfg_multipart.c index 1d03d868..98e007ab 100644 --- a/src/webcfg_multipart.c +++ b/src/webcfg_multipart.c @@ -61,8 +61,9 @@ static char g_FirmwareVersion[64]={'\0'}; static char g_bootTime[64]={'\0'}; static char g_productClass[64]={'\0'}; static char g_ModelName[64]={'\0'}; +static char g_RebootReason[64]={'\0'}; static char g_transID[64]={'\0'}; -static char g_contentLen[32]={'\0'}; +static char * g_contentLen = NULL; multipart_t *mp = NULL; pthread_mutex_t multipart_t_mut =PTHREAD_MUTEX_INITIALIZER; static int eventFlag = 0; @@ -836,8 +837,7 @@ size_t headr_callback(char *buffer, size_t size, size_t nitems) { strncpy(header_str, header_value, sizeof(header_str)-1); stripspaces(header_str, &final_header); - - strncpy(g_contentLen, final_header, sizeof(g_contentLen)-1); + g_contentLen = strdup(final_header); } } WebcfgInfo("g_contentLen is %s\n", g_contentLen); @@ -1054,17 +1054,31 @@ void get_root_version_string(char **rootVersion, uint32_t *root_ver, int status) fp = fopen(WEBCFG_DB_FILE,"rb"); - reason = getRebootReason(); - if(reason != NULL) + if(strlen(g_RebootReason) == 0) + { + reason = getRebootReason(); + if(reason !=NULL) + { + strncpy(g_RebootReason, reason, sizeof(g_RebootReason)-1); + WebcfgInfo("g_RebootReason fetched is %s\n", g_RebootReason); + WEBCFG_FREE(reason); + } + else + { + WebcfgError("Failed to get reboot reason\n"); + } + } + + if(strlen(g_RebootReason)> 0) { - WebcfgInfo("reboot reason is %s\n", reason); + WebcfgInfo("reboot reason is %s\n", g_RebootReason); if (fp == NULL) { - if(strncmp(reason,"factory-reset",strlen("factory-reset"))==0) + if(strncmp(g_RebootReason,"factory-reset",strlen("factory-reset"))==0) { *rootVersion = strdup("NONE"); } - else if(strncmp(reason,"Software_upgrade",strlen("Software_upgrade"))==0) + else if(strncmp(g_RebootReason,"Software_upgrade",strlen("Software_upgrade"))==0) { *rootVersion = strdup("NONE-MIGRATION"); } @@ -1081,7 +1095,7 @@ void get_root_version_string(char **rootVersion, uint32_t *root_ver, int status) if(db_root_string !=NULL) { - if((strcmp(db_root_string,"POST-NONE")==0) && ((strcmp(reason,"Software_upgrade")!=0) && (strcmp(reason,"factory-reset")!=0))) + if((strcmp(db_root_string,"POST-NONE")==0) && ((strcmp(g_RebootReason,"Software_upgrade")!=0) && (strcmp(g_RebootReason,"factory-reset")!=0))) { *rootVersion = strdup("NONE-REBOOT"); WEBCFG_FREE(db_root_string); @@ -1107,7 +1121,7 @@ void get_root_version_string(char **rootVersion, uint32_t *root_ver, int status) if(db_root_version) { //when subdocs are applied and reboot due to software migration/rollback, root reset to 0. - if(subdocList > 1 && strncmp(reason,"Software_upgrade",strlen("Software_upgrade"))==0) + if(subdocList > 1 && strcmp(g_RebootReason,"Software_upgrade")==0) { WebcfgInfo("reboot due to software migration/rollback, root reset to 0\n"); *root_ver = 0; @@ -1119,7 +1133,7 @@ void get_root_version_string(char **rootVersion, uint32_t *root_ver, int status) { if(db_root_string !=NULL) { - WebcfgInfo("Update rootVersion with db_root_string %s\n", db_root_string); + WebcfgDebug("Update rootVersion with db_root_string %s\n", db_root_string); *rootVersion = strdup(db_root_string); WEBCFG_FREE(db_root_string); } @@ -1127,10 +1141,6 @@ void get_root_version_string(char **rootVersion, uint32_t *root_ver, int status) *root_ver = db_root_version; } } - else - { - WebcfgError("Failed to get reboot reason\n"); - } } /* Traverse through db list to get versions of all docs with root. diff --git a/src/webcfg_notify.c b/src/webcfg_notify.c index 747cc557..3d13ba67 100644 --- a/src/webcfg_notify.c +++ b/src/webcfg_notify.c @@ -97,14 +97,12 @@ void addWebConfgNotifyMsg(char *docname, uint32_t version, char *status, char *e if(version ==0 && root_string !=NULL) { - WebcfgInfo("Update root_string %s to notify struct\n", root_string); strcpy(versionStr, root_string); } else { snprintf(versionStr, sizeof(versionStr), "%lu", (long)version); } - WebcfgInfo("versionStr is %s\n", versionStr); if(strlen(versionStr) > 0) { @@ -125,7 +123,7 @@ void addWebConfgNotifyMsg(char *docname, uint32_t version, char *status, char *e args->response_code = response_code; - WebcfgInfo("args->name:%s,args->application_status:%s,args->timeout:%lu,args->error_details:%s,args->version:%s,args->transaction_uuid:%s,args->type:%s,args->error_code:%lu,args->response_code:%lu\n",args->name,args->application_status, (long)args->timeout, args->error_details, args->version, args->transaction_uuid, args->type, (long)args->error_code,args->response_code ); + WebcfgDebug("args->name:%s,args->application_status:%s,args->timeout:%lu,args->error_details:%s,args->version:%s,args->transaction_uuid:%s,args->type:%s,args->error_code:%lu,args->response_code:%lu\n",args->name,args->application_status, (long)args->timeout, args->error_details, args->version, args->transaction_uuid, args->type, (long)args->error_code,args->response_code ); args->next=NULL; From d6dfa68f3325495e2a3d58d326b6c0f48342bfd2 Mon Sep 17 00:00:00 2001 From: Sadhyama Vengilat Date: Tue, 25 Aug 2020 17:38:48 +0530 Subject: [PATCH 15/21] Fix valgrind leaks --- src/webcfg.c | 2 ++ src/webcfg_db.c | 30 ++++++++++++++++++------------ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/webcfg.c b/src/webcfg.c index 14d68ca9..a19e5959 100644 --- a/src/webcfg.c +++ b/src/webcfg.c @@ -343,6 +343,8 @@ int handlehttpResponse(long response_code, char *webConfigData, int retry_count, WebcfgInfo("webConfigData content length is 0\n"); getConfigVersionList(version, response_code); WEBCFG_FREE(contentLength); + WEBCFG_FREE(transaction_uuid); + WEBCFG_FREE(webConfigData); return 1; } } diff --git a/src/webcfg_db.c b/src/webcfg_db.c index ff72e5e1..cd7e928d 100644 --- a/src/webcfg_db.c +++ b/src/webcfg_db.c @@ -263,6 +263,9 @@ void webcfgdbblob_destroy( blob_struct_t *bd ) if( NULL != bd->entries[i].error_details ) { free( bd->entries[i].error_details ); } + if( NULL != bd->entries[i].root_string ) { + free( bd->entries[i].root_string ); + } } if( NULL != bd->entries ) { free( bd->entries ); @@ -473,13 +476,12 @@ void checkDBList(char *docname, uint32_t version, char* rootstr) webcfgdb->name = strdup(docname); webcfgdb->version = version; - if(rootstr !=NULL) - { - webcfgdb->root_string = strdup(rootstr); - } - else + if( strcmp("root", webcfgdb->name) == 0) { - webcfgdb->root_string = NULL; + if(rootstr !=NULL) + { + webcfgdb->root_string = strdup(rootstr); + } } webcfgdb->next = NULL; @@ -507,13 +509,17 @@ WEBCFG_STATUS updateDBlist(char *docname, uint32_t version, char* rootstr) if( strcmp(docname, webcfgdb->name) == 0) { webcfgdb->version = version; - if(rootstr !=NULL) + if( strcmp("root", webcfgdb->name) == 0) { - webcfgdb->root_string = strdup(rootstr); - } - else - { - webcfgdb->root_string = NULL; + if(webcfgdb->root_string !=NULL) + { + WEBCFG_FREE(webcfgdb->root_string); + webcfgdb->root_string = NULL; + } + if(rootstr!=NULL) + { + webcfgdb->root_string = strdup(rootstr); + } } WebcfgDebug("webcfgdb %s is updated to version %lu webcfgdb->root_string %s\n", docname, (long)webcfgdb->version, webcfgdb->root_string); pthread_mutex_unlock (&webconfig_db_mut); From 863a8a91a367621557b2f5aa13fbfbca38f6d1ca Mon Sep 17 00:00:00 2001 From: Sadhyama Vengilat Date: Tue, 25 Aug 2020 20:11:53 +0530 Subject: [PATCH 16/21] Reduce debug logs --- src/webcfg.c | 2 +- src/webcfg_multipart.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/webcfg.c b/src/webcfg.c index a19e5959..a2df56b5 100644 --- a/src/webcfg.c +++ b/src/webcfg.c @@ -329,7 +329,7 @@ int handlehttpResponse(long response_code, char *webConfigData, int retry_count, } else { - WebcfgInfo("root webConfigData processed, waiting for apply status events\n"); + WebcfgInfo("root webConfigData processed, check apply status events\n"); return 1; } } diff --git a/src/webcfg_multipart.c b/src/webcfg_multipart.c index 98e007ab..f2a5a44f 100644 --- a/src/webcfg_multipart.c +++ b/src/webcfg_multipart.c @@ -840,7 +840,7 @@ size_t headr_callback(char *buffer, size_t size, size_t nitems) g_contentLen = strdup(final_header); } } - WebcfgInfo("g_contentLen is %s\n", g_contentLen); + WebcfgDebug("g_contentLen is %s\n", g_contentLen); } } WebcfgInfo("header_callback size %zu\n", size); @@ -1060,7 +1060,7 @@ void get_root_version_string(char **rootVersion, uint32_t *root_ver, int status) if(reason !=NULL) { strncpy(g_RebootReason, reason, sizeof(g_RebootReason)-1); - WebcfgInfo("g_RebootReason fetched is %s\n", g_RebootReason); + WebcfgDebug("g_RebootReason fetched is %s\n", g_RebootReason); WEBCFG_FREE(reason); } else From 743d7500ba0eeb56597faec1da3e3b11746ef0c2 Mon Sep 17 00:00:00 2001 From: Sadhyama Vengilat Date: Thu, 27 Aug 2020 12:36:39 +0530 Subject: [PATCH 17/21] Change to snprintf --- src/webcfg_multipart.c | 4 ++-- src/webcfg_notify.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/webcfg_multipart.c b/src/webcfg_multipart.c index f2a5a44f..e77520a7 100644 --- a/src/webcfg_multipart.c +++ b/src/webcfg_multipart.c @@ -1154,7 +1154,7 @@ void getConfigVersionList(char *versionsList, int http_status) uint32_t root_version = 0; //initialize to default value "0". - sprintf(versionsList, "%s", "0"); + snprintf(versionsList, 512, "%s", "0"); get_root_version_string(&root_str, &root_version, http_status); WebcfgDebug("update root_version %lu rootString %s to DB\n", (long)root_version, root_str); @@ -1170,7 +1170,7 @@ void getConfigVersionList(char *versionsList, int http_status) if(root_str!=NULL && strlen(root_str) >0) { WebcfgDebug("update root_str %s to versionsList\n", root_str); - sprintf(versionsList, "%s", root_str); + snprintf(versionsList, 512, "%s", root_str); WEBCFG_FREE(root_str); } else diff --git a/src/webcfg_notify.c b/src/webcfg_notify.c index 3d13ba67..63d569ea 100644 --- a/src/webcfg_notify.c +++ b/src/webcfg_notify.c @@ -97,7 +97,7 @@ void addWebConfgNotifyMsg(char *docname, uint32_t version, char *status, char *e if(version ==0 && root_string !=NULL) { - strcpy(versionStr, root_string); + snprintf(versionStr, sizeof(versionStr), "%s", root_string); } else { From f13c6c8eb5daad4de2679c9a56f92eda27193aa6 Mon Sep 17 00:00:00 2001 From: Sadhyama Vengilat Date: Thu, 27 Aug 2020 21:03:52 +0530 Subject: [PATCH 18/21] Added unit tests --- src/webcfg_multipart.c | 7 +- src/webcfg_multipart.h | 1 + tests/CMakeLists.txt | 9 + tests/test_root.c | 411 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 425 insertions(+), 3 deletions(-) create mode 100644 tests/test_root.c diff --git a/src/webcfg_multipart.c b/src/webcfg_multipart.c index e77520a7..e377a7ea 100644 --- a/src/webcfg_multipart.c +++ b/src/webcfg_multipart.c @@ -61,7 +61,7 @@ static char g_FirmwareVersion[64]={'\0'}; static char g_bootTime[64]={'\0'}; static char g_productClass[64]={'\0'}; static char g_ModelName[64]={'\0'}; -static char g_RebootReason[64]={'\0'}; +char g_RebootReason[64]={'\0'}; static char g_transID[64]={'\0'}; static char * g_contentLen = NULL; multipart_t *mp = NULL; @@ -1052,8 +1052,6 @@ void get_root_version_string(char **rootVersion, uint32_t *root_ver, int status) char *db_root_string = NULL; int subdocList = 0; - fp = fopen(WEBCFG_DB_FILE,"rb"); - if(strlen(g_RebootReason) == 0) { reason = getRebootReason(); @@ -1072,6 +1070,8 @@ void get_root_version_string(char **rootVersion, uint32_t *root_ver, int status) if(strlen(g_RebootReason)> 0) { WebcfgInfo("reboot reason is %s\n", g_RebootReason); + + fp = fopen(WEBCFG_DB_FILE,"rb"); if (fp == NULL) { if(strncmp(g_RebootReason,"factory-reset",strlen("factory-reset"))==0) @@ -1089,6 +1089,7 @@ void get_root_version_string(char **rootVersion, uint32_t *root_ver, int status) } else { + fclose(fp); //get existing root version from DB getRootVersionFromDB(&db_root_version, &db_root_string, &subdocList); WebcfgDebug("db_root_version %lu db_root_string %s subdocList %d\n", (long)db_root_version, db_root_string, subdocList); diff --git a/src/webcfg_multipart.h b/src/webcfg_multipart.h index 31008ea3..b5851cf3 100644 --- a/src/webcfg_multipart.h +++ b/src/webcfg_multipart.h @@ -58,4 +58,5 @@ WEBCFG_STATUS validate_request_param(param_t *reqParam, int paramCount); void getConfigVersionList(char *versionsList, int http_status); char * get_global_contentLen(void); void getRootVersionFromDB(uint32_t *rt_version, char **rt_string, int *subdoclist); +void get_root_version_string(char **rootVersion, uint32_t *root_ver, int status); #endif diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3dc44092..4ca8188a 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -63,6 +63,15 @@ target_link_libraries (test_events -lcunit -lmsgpackc -lcurl -lpthread -lm -luu target_link_libraries (test_events gcov -Wl,--no-as-needed ) +#------------------------------------------------------------------------------- +# test_root +#------------------------------------------------------------------------------- +add_test(NAME test_root COMMAND ${MEMORY_CHECK} ./test_root) +add_executable(test_root test_root.c ../src/webcfg_param.c ../src/webcfg_multipart.c ../src/webcfg_helpers.c ../src/webcfg.c ../src/webcfg_auth.c ../src/webcfg_notify.c ../src/webcfg_db.c ../src/webcfg_pack.c ../src/webcfg_blob.c ../src/webcfg_event.c ../src/webcfg_generic.c ../src/webcfg_client.c ../src/webcfg_aker.c) +target_link_libraries (test_root -lcunit -lmsgpackc -lcurl -lpthread -lm -luuid -ltrower-base64 -lwdmp-c -lcimplog -lcjson -lwrp-c -llibparodus -lnanomsg) + +target_link_libraries (test_root gcov -Wl,--no-as-needed ) + # Code coverage add_custom_target(coverage diff --git a/tests/test_root.c b/tests/test_root.c new file mode 100644 index 00000000..9827e487 --- /dev/null +++ b/tests/test_root.c @@ -0,0 +1,411 @@ + /** + * Copyright 2019 Comcast Cable Communications Management, LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +#include +#include +#include +#include +#include +#include "../src/webcfg_param.h" +#include "../src/webcfg.h" +#include "../src/webcfg_multipart.h" +#include "../src/webcfg_helpers.h" +#include "../src/webcfg_db.h" +#include "../src/webcfg_notify.h" +#include +#include +#include +#include "../src/webcfg_generic.h" +#include "../src/webcfg_event.h" +#define FILE_URL "/tmp/webcfg_url" + +#define UNUSED(x) (void )(x) + +char *url = NULL; +char *interface = NULL; +char device_mac[32] = {'\0'}; +char *reason = NULL; +extern char g_RebootReason; + +char* get_deviceMAC() +{ + strcpy(device_mac, "b42xxxxxxxxx"); + return device_mac; +} +void setValues(const param_t paramVal[], const unsigned int paramCount, const int setType, char *transactionId, money_trace_spans *timeSpan, WDMP_STATUS *retStatus, int *ccspStatus) +{ + UNUSED(paramVal); + UNUSED(paramCount); + UNUSED(setType); + UNUSED(transactionId); + UNUSED(timeSpan); + UNUSED(ccspStatus); + *retStatus = WDMP_SUCCESS; + return; +} + +void setAttributes(param_t *attArr, const unsigned int paramCount, money_trace_spans *timeSpan, WDMP_STATUS *retStatus) +{ + UNUSED(attArr); + UNUSED(paramCount); + UNUSED(timeSpan); + *retStatus = WDMP_SUCCESS; + return; +} + +int getForceSync(char** pString, char **transactionId) +{ + UNUSED(pString); + UNUSED(transactionId); + return 0; +} +int setForceSync(char* pString, char *transactionId,int *session_status) +{ + UNUSED(pString); + UNUSED(transactionId); + UNUSED(session_status); + return 0; +} + +char * getParameterValue(char *paramName) +{ + UNUSED(paramName); + return NULL; +} + +char * getSerialNumber() +{ + char *sNum = strdup("1234"); + return sNum; +} + +char * getDeviceBootTime() +{ + char *bTime = strdup("152200345"); + return bTime; +} + +char * getProductClass() +{ + char *pClass = strdup("Product"); + return pClass; +} + +char * getModelName() +{ + char *mName = strdup("Model"); + return mName; +} + +char * getConnClientParamName() +{ + char *pName = strdup("ConnClientParamName"); + return pName; +} +char * getFirmwareVersion() +{ + char *fName = strdup("Firmware.bin"); + return fName; +} + +void setRebootReason(char *r_reason) +{ + if (r_reason) + reason = strdup(r_reason); + //return reason; +} + +char * getRebootReason() +{ + //char *reason = strdup("factory-reset"); + return reason; +} + +void sendNotification(char *payload, char *source, char *destination) +{ + WEBCFG_FREE(payload); + WEBCFG_FREE(source); + UNUSED(destination); + return; +} + +char *get_global_systemReadyTime() +{ + char *sTime = strdup("158000123"); + return sTime; +} + +int Get_Webconfig_URL( char *pString) +{ + char *webConfigURL =NULL; + loadInitURLFromFile(&webConfigURL); + pString = webConfigURL; + printf("The value of pString is %s\n",pString); + return 0; +} + +int Set_Webconfig_URL( char *pString) +{ + printf("Set_Webconfig_URL pString %s\n", pString); + return 0; +} + +int registerWebcfgEvent(WebConfigEventCallback webcfgEventCB) +{ + UNUSED(webcfgEventCB); + return 0; +} + +void test_FRNONE() +{ + char *root_str = NULL; + uint32_t root_version = 0; + int http_status = 0; + FILE *fp = NULL; + + fp = fopen("/tmp/webconfig_db.bin","rb"); + if(fp !=NULL) + { + system("rm -rf /tmp/webconfig_db.bin"); + fclose(fp); + } + setRebootReason("factory-reset"); + + get_root_version_string(&root_str, &root_version, http_status); + CU_ASSERT_FATAL( NULL != root_str ); + CU_ASSERT_STRING_EQUAL( "NONE", root_str ); +} + +void test_MIGRATION() +{ + char *root_str = NULL; + uint32_t root_version = 0; + int http_status = 0; + g_RebootReason ='\0'; + + setRebootReason("Software_upgrade"); + get_root_version_string(&root_str, &root_version, http_status); + CU_ASSERT_FATAL( NULL != root_str ); + CU_ASSERT_STRING_EQUAL( "NONE-MIGRATION", root_str ); +} + +void test_NONE_REBOOT() +{ + char *root_str = NULL; + uint32_t root_version = 0; + int http_status = 0; + g_RebootReason ='\0'; + + setRebootReason("unknown"); + get_root_version_string(&root_str, &root_version, http_status); + CU_ASSERT_FATAL( NULL != root_str ); + CU_ASSERT_STRING_EQUAL( "NONE-REBOOT", root_str ); +} + +void test_ROOTNULL() +{ + char *root_str = NULL; + uint32_t root_version = 0; + int http_status = 0; + g_RebootReason ='\0'; + reason = NULL; + + setRebootReason(NULL); + get_root_version_string(&root_str, &root_version, http_status); + CU_ASSERT_FATAL( NULL == root_str ); +} + +void test_POSTNONE() +{ + char *root_str = NULL; + uint32_t root_version = 0; + int http_status = 0; + FILE *fp = NULL; + + root_str = strdup("POST-NONE"); + setRebootReason("unknown"); + checkDBList("root", root_version, root_str); + addNewDocEntry(1); + + get_root_version_string(&root_str, &root_version, http_status); + CU_ASSERT_FATAL( NULL != root_str ); + CU_ASSERT_STRING_EQUAL( "NONE-REBOOT", root_str ); + fp = fopen("/tmp/webconfig_db.bin","rb"); + if(fp !=NULL) + { + system("rm -rf /tmp/webconfig_db.bin"); + fclose(fp); + } +} + +void test_FR_404() +{ + char *root_str = NULL; + uint32_t root_version = 0; + int http_status = 404; + FILE *fp = NULL; + + root_str = strdup("NONE"); + setRebootReason("factory-reset"); + checkDBList("root", root_version, root_str); + addNewDocEntry(1); + + get_root_version_string(&root_str, &root_version, http_status); + CU_ASSERT_FATAL( NULL != root_str ); + CU_ASSERT_STRING_EQUAL( "POST-NONE", root_str ); + fp = fopen("/tmp/webconfig_db.bin","rb"); + if(fp !=NULL) + { + system("rm -rf /tmp/webconfig_db.bin"); + fclose(fp); + } +} + +void test_FR_200() +{ + char *root_str = NULL; + uint32_t root_version = 0; + int http_status = 200; + FILE *fp = NULL; + + root_str = strdup("NONE"); + setRebootReason("factory-reset"); + checkDBList("root", root_version, root_str); + addNewDocEntry(1); + + get_root_version_string(&root_str, &root_version, http_status); + CU_ASSERT_FATAL( NULL != root_str ); + CU_ASSERT_STRING_EQUAL( "POST-NONE", root_str ); + fp = fopen("/tmp/webconfig_db.bin","rb"); + if(fp !=NULL) + { + system("rm -rf /tmp/webconfig_db.bin"); + fclose(fp); + } +} + +void test_FW_ROLLBACK() +{ + char *root_str = NULL; + uint32_t root_version = 1234; + int http_status = 0; + g_RebootReason ='\0'; + FILE *fp = NULL; + + setRebootReason("Software_upgrade"); + checkDBList("root", root_version, root_str); + checkDBList("test", 123, NULL); + addNewDocEntry(2); + + get_root_version_string(&root_str, &root_version, http_status); + CU_ASSERT_FATAL( NULL == root_str ); + CU_ASSERT_EQUAL( 0, root_version ); + fp = fopen("/tmp/webconfig_db.bin","rb"); + if(fp !=NULL) + { + system("rm -rf /tmp/webconfig_db.bin"); + fclose(fp); + } +} + +void test_zerorootVersion() +{ + char *root_str = "POST-NONE"; + uint32_t root_version = 0; + int http_status = 0; + g_RebootReason ='\0'; + FILE *fp = NULL; + + setRebootReason("Software_upgrade"); + checkDBList("root", root_version, root_str); + checkDBList("test", 1234, NULL); + addNewDocEntry(2); + + get_root_version_string(&root_str, &root_version, http_status); + CU_ASSERT_FATAL( NULL != root_str ); + CU_ASSERT_STRING_EQUAL( "POST-NONE", root_str ); + fp = fopen("/tmp/webconfig_db.bin","rb"); + if(fp !=NULL) + { + system("rm -rf /tmp/webconfig_db.bin"); + fclose(fp); + } +} + +void test_rootVersion() +{ + char *root_str = NULL; + uint32_t root_version = 44444; + int http_status = 0; + g_RebootReason ='\0'; + FILE *fp = NULL; + + setRebootReason("unknown"); + checkDBList("root", root_version, root_str); + checkDBList("test", 1234, NULL); + addNewDocEntry(2); + + get_root_version_string(&root_str, &root_version, http_status); + CU_ASSERT_FATAL( NULL == root_str ); + CU_ASSERT_EQUAL( 44444, root_version ); + fp = fopen("/tmp/webconfig_db.bin","rb"); + if(fp !=NULL) + { + system("rm -rf /tmp/webconfig_db.bin"); + fclose(fp); + } +} + +void add_suites( CU_pSuite *suite ) +{ + *suite = CU_add_suite( "tests", NULL, NULL ); + CU_add_test( *suite, "Factory reset NONE\n", test_FRNONE); + CU_add_test( *suite, "Software upgrade\n", test_MIGRATION); + CU_add_test( *suite, "Unknown reboot\n", test_NONE_REBOOT); + CU_add_test( *suite, "No reboot reason\n", test_ROOTNULL); + CU_add_test( *suite, "Unknown Reboot with DB\n", test_POSTNONE); + CU_add_test( *suite, "Factory reset 404\n", test_FR_404); + CU_add_test( *suite, "Factory reset 200\n", test_FR_200); + CU_add_test( *suite, "Migration/rollback\n", test_FW_ROLLBACK); + CU_add_test( *suite, "zero root DB\n", test_zerorootVersion); + CU_add_test( *suite, "Non zero root DB\n", test_rootVersion); +} + +/*----------------------------------------------------------------------------*/ +/* External Functions */ +/*----------------------------------------------------------------------------*/ +int main( ) +{ + unsigned rv = 1; + CU_pSuite suite = NULL; + if( CUE_SUCCESS == CU_initialize_registry() ) { + add_suites( &suite ); + + if( NULL != suite ) { + CU_basic_set_mode( CU_BRM_VERBOSE ); + CU_basic_run_tests(); + printf( "\n" ); + CU_basic_show_failures( CU_get_failure_list() ); + printf( "\n\n" ); + rv = CU_get_number_of_tests_failed(); + } + + CU_cleanup_registry(); + + } + return rv; +} + From a396ccaef4ad1638d65cede1ac9fb4cb2b276626 Mon Sep 17 00:00:00 2001 From: Sadhyama Vengilat Date: Thu, 27 Aug 2020 21:57:43 +0530 Subject: [PATCH 19/21] To fix linking issue --- tests/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index d3345635..f06e0b56 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -75,7 +75,7 @@ target_link_libraries (test_metadata gcov -Wl,--no-as-needed ) # test_root #------------------------------------------------------------------------------- add_test(NAME test_root COMMAND ${MEMORY_CHECK} ./test_root) -add_executable(test_root test_root.c ../src/webcfg_param.c ../src/webcfg_multipart.c ../src/webcfg_helpers.c ../src/webcfg.c ../src/webcfg_auth.c ../src/webcfg_notify.c ../src/webcfg_db.c ../src/webcfg_pack.c ../src/webcfg_blob.c ../src/webcfg_event.c ../src/webcfg_generic.c ../src/webcfg_client.c ../src/webcfg_aker.c) +add_executable(test_root test_root.c ../src/webcfg_param.c ../src/webcfg_multipart.c ../src/webcfg_helpers.c ../src/webcfg.c ../src/webcfg_auth.c ../src/webcfg_notify.c ../src/webcfg_db.c ../src/webcfg_pack.c ../src/webcfg_blob.c ../src/webcfg_event.c ../src/webcfg_generic.c ../src/webcfg_client.c ../src/webcfg_aker.c ../src/webcfg_metadata.c) target_link_libraries (test_root -lcunit -lmsgpackc -lcurl -lpthread -lm -luuid -ltrower-base64 -lwdmp-c -lcimplog -lcjson -lwrp-c -llibparodus -lnanomsg) target_link_libraries (test_root gcov -Wl,--no-as-needed ) From f0aee5513f6545d5834e7e9a08983079789903de Mon Sep 17 00:00:00 2001 From: Sadhyama Vengilat Date: Fri, 28 Aug 2020 13:35:11 +0530 Subject: [PATCH 20/21] Rename root DB functions --- src/webcfg.c | 14 ++-- src/webcfg_multipart.c | 12 ++-- src/webcfg_multipart.h | 6 +- tests/test_root.c | 157 ++++------------------------------------- 4 files changed, 31 insertions(+), 158 deletions(-) diff --git a/src/webcfg.c b/src/webcfg.c index adf20ac2..60b8b3ce 100644 --- a/src/webcfg.c +++ b/src/webcfg.c @@ -306,7 +306,7 @@ int handlehttpResponse(long response_code, char *webConfigData, int retry_count, if(response_code == 304) { WebcfgInfo("webConfig is in sync with cloud. response_code:%ld\n", response_code); - getRootVersionFromDB(&db_root_version, &db_root_string, &subdocList); + getRootDocVersionFromDBCache(&db_root_version, &db_root_string, &subdocList); addWebConfgNotifyMsg(NULL, db_root_version, NULL, NULL, transaction_uuid, 0, "status", 0, db_root_string, response_code); if(db_root_string !=NULL) { @@ -344,7 +344,7 @@ int handlehttpResponse(long response_code, char *webConfigData, int retry_count, if((contentLength !=NULL) && (strcmp(contentLength, "0") == 0)) { WebcfgInfo("webConfigData content length is 0\n"); - getConfigVersionList(version, response_code); + refreshConfigVersionList(version, response_code); WEBCFG_FREE(contentLength); WEBCFG_FREE(transaction_uuid); WEBCFG_FREE(webConfigData); @@ -355,7 +355,7 @@ int handlehttpResponse(long response_code, char *webConfigData, int retry_count, else if(response_code == 204) { WebcfgInfo("No configuration available for this device. response_code:%ld\n", response_code); - getRootVersionFromDB(&db_root_version, &db_root_string, &subdocList); + getRootDocVersionFromDBCache(&db_root_version, &db_root_string, &subdocList); addWebConfgNotifyMsg(NULL, db_root_version, NULL, NULL, transaction_uuid, 0, "status", 0, db_root_string, response_code); if(db_root_string !=NULL) { @@ -374,7 +374,7 @@ int handlehttpResponse(long response_code, char *webConfigData, int retry_count, else if(response_code == 429) { WebcfgInfo("No action required from client. response_code:%ld\n", response_code); - getRootVersionFromDB(&db_root_version, &db_root_string, &subdocList); + getRootDocVersionFromDBCache(&db_root_version, &db_root_string, &subdocList); addWebConfgNotifyMsg(NULL, db_root_version, NULL, NULL, transaction_uuid, 0, "status", 0, db_root_string, response_code); if(db_root_string !=NULL) { @@ -390,9 +390,9 @@ int handlehttpResponse(long response_code, char *webConfigData, int retry_count, if (response_code == 404) { //To set POST-NONE root version when 404 - getConfigVersionList(version, response_code); + refreshConfigVersionList(version, response_code); } - getRootVersionFromDB(&db_root_version, &db_root_string, &subdocList); + getRootDocVersionFromDBCache(&db_root_version, &db_root_string, &subdocList); addWebConfgNotifyMsg(NULL, db_root_version, NULL, NULL, transaction_uuid, 0, "status", 0, db_root_string, response_code); if(db_root_string !=NULL) { @@ -407,7 +407,7 @@ int handlehttpResponse(long response_code, char *webConfigData, int retry_count, if(retry_count == 3 && !err) { WebcfgDebug("3 curl retry attempts\n"); - getRootVersionFromDB(&db_root_version, &db_root_string, &subdocList); + getRootDocVersionFromDBCache(&db_root_version, &db_root_string, &subdocList); addWebConfgNotifyMsg(NULL, db_root_version, NULL, NULL, transaction_uuid, 0, "status", 0, db_root_string, response_code); if(db_root_string !=NULL) { diff --git a/src/webcfg_multipart.c b/src/webcfg_multipart.c index c879b3ad..97ca4c76 100644 --- a/src/webcfg_multipart.c +++ b/src/webcfg_multipart.c @@ -1033,7 +1033,7 @@ void getConfigDocList(char *docList) } } -void getRootVersionFromDB(uint32_t *rt_version, char **rt_string, int *subdoclist) +void getRootDocVersionFromDBCache(uint32_t *rt_version, char **rt_string, int *subdoclist) { webconfig_db_data_t *temp = NULL; temp = get_global_db_node(); @@ -1055,7 +1055,7 @@ void getRootVersionFromDB(uint32_t *rt_version, char **rt_string, int *subdoclis WebcfgDebug("*subdoclist is %d\n", *subdoclist); } -void get_root_version_string(char **rootVersion, uint32_t *root_ver, int status) +void derive_root_doc_version_string(char **rootVersion, uint32_t *root_ver, int status) { FILE *fp = NULL; char *reason = NULL; @@ -1102,7 +1102,7 @@ void get_root_version_string(char **rootVersion, uint32_t *root_ver, int status) { fclose(fp); //get existing root version from DB - getRootVersionFromDB(&db_root_version, &db_root_string, &subdocList); + getRootDocVersionFromDBCache(&db_root_version, &db_root_string, &subdocList); WebcfgDebug("db_root_version %lu db_root_string %s subdocList %d\n", (long)db_root_version, db_root_string, subdocList); if(db_root_string !=NULL) @@ -1159,7 +1159,7 @@ void get_root_version_string(char **rootVersion, uint32_t *root_ver, int status) e.g. IF-NONE-MATCH: 123,44317,66317,77317 where 123 is root version. Currently versionsList length is fixed to 512 which can support up to 45 docs. This can be increased if required. */ -void getConfigVersionList(char *versionsList, int http_status) +void refreshConfigVersionList(char *versionsList, int http_status) { char *versionsList_tmp = NULL; char *root_str = NULL; @@ -1168,7 +1168,7 @@ void getConfigVersionList(char *versionsList, int http_status) //initialize to default value "0". snprintf(versionsList, 512, "%s", "0"); - get_root_version_string(&root_str, &root_version, http_status); + derive_root_doc_version_string(&root_str, &root_version, http_status); WebcfgDebug("update root_version %lu rootString %s to DB\n", (long)root_version, root_str); checkDBList("root", root_version, root_str); WebcfgDebug("addNewDocEntry. get_successDocCount %d\n", get_successDocCount()); @@ -1256,7 +1256,7 @@ void createCurlHeader( struct curl_slist *list, struct curl_slist **header_list, version_header = (char *) malloc(sizeof(char)*MAX_BUF_SIZE); if(version_header !=NULL) { - getConfigVersionList(version, 0); + refreshConfigVersionList(version, 0); snprintf(version_header, MAX_BUF_SIZE, "IF-NONE-MATCH:%s", ((strlen(version)!=0) ? version : "0")); WebcfgInfo("version_header formed %s\n", version_header); list = curl_slist_append(list, version_header); diff --git a/src/webcfg_multipart.h b/src/webcfg_multipart.h index b5851cf3..58531a2f 100644 --- a/src/webcfg_multipart.h +++ b/src/webcfg_multipart.h @@ -55,8 +55,8 @@ void set_global_mp(multipart_t *new); void reqParam_destroy( int paramCnt, param_t *reqObj ); void failedDocsRetry(); WEBCFG_STATUS validate_request_param(param_t *reqParam, int paramCount); -void getConfigVersionList(char *versionsList, int http_status); +void refreshConfigVersionList(char *versionsList, int http_status); char * get_global_contentLen(void); -void getRootVersionFromDB(uint32_t *rt_version, char **rt_string, int *subdoclist); -void get_root_version_string(char **rootVersion, uint32_t *root_ver, int status); +void getRootDocVersionFromDBCache(uint32_t *rt_version, char **rt_string, int *subdoclist); +void derive_root_doc_version_string(char **rootVersion, uint32_t *root_ver, int status); #endif diff --git a/tests/test_root.c b/tests/test_root.c index 9827e487..eddd3ff0 100644 --- a/tests/test_root.c +++ b/tests/test_root.c @@ -17,158 +17,27 @@ #include #include #include +#include #include #include -#include "../src/webcfg_param.h" -#include "../src/webcfg.h" #include "../src/webcfg_multipart.h" -#include "../src/webcfg_helpers.h" #include "../src/webcfg_db.h" -#include "../src/webcfg_notify.h" -#include -#include -#include #include "../src/webcfg_generic.h" -#include "../src/webcfg_event.h" -#define FILE_URL "/tmp/webcfg_url" -#define UNUSED(x) (void )(x) - -char *url = NULL; -char *interface = NULL; -char device_mac[32] = {'\0'}; char *reason = NULL; extern char g_RebootReason; -char* get_deviceMAC() -{ - strcpy(device_mac, "b42xxxxxxxxx"); - return device_mac; -} -void setValues(const param_t paramVal[], const unsigned int paramCount, const int setType, char *transactionId, money_trace_spans *timeSpan, WDMP_STATUS *retStatus, int *ccspStatus) -{ - UNUSED(paramVal); - UNUSED(paramCount); - UNUSED(setType); - UNUSED(transactionId); - UNUSED(timeSpan); - UNUSED(ccspStatus); - *retStatus = WDMP_SUCCESS; - return; -} - -void setAttributes(param_t *attArr, const unsigned int paramCount, money_trace_spans *timeSpan, WDMP_STATUS *retStatus) -{ - UNUSED(attArr); - UNUSED(paramCount); - UNUSED(timeSpan); - *retStatus = WDMP_SUCCESS; - return; -} - -int getForceSync(char** pString, char **transactionId) -{ - UNUSED(pString); - UNUSED(transactionId); - return 0; -} -int setForceSync(char* pString, char *transactionId,int *session_status) -{ - UNUSED(pString); - UNUSED(transactionId); - UNUSED(session_status); - return 0; -} - -char * getParameterValue(char *paramName) -{ - UNUSED(paramName); - return NULL; -} - -char * getSerialNumber() -{ - char *sNum = strdup("1234"); - return sNum; -} - -char * getDeviceBootTime() -{ - char *bTime = strdup("152200345"); - return bTime; -} - -char * getProductClass() -{ - char *pClass = strdup("Product"); - return pClass; -} - -char * getModelName() -{ - char *mName = strdup("Model"); - return mName; -} - -char * getConnClientParamName() -{ - char *pName = strdup("ConnClientParamName"); - return pName; -} -char * getFirmwareVersion() -{ - char *fName = strdup("Firmware.bin"); - return fName; -} - void setRebootReason(char *r_reason) { if (r_reason) reason = strdup(r_reason); - //return reason; } char * getRebootReason() { - //char *reason = strdup("factory-reset"); return reason; } -void sendNotification(char *payload, char *source, char *destination) -{ - WEBCFG_FREE(payload); - WEBCFG_FREE(source); - UNUSED(destination); - return; -} - -char *get_global_systemReadyTime() -{ - char *sTime = strdup("158000123"); - return sTime; -} - -int Get_Webconfig_URL( char *pString) -{ - char *webConfigURL =NULL; - loadInitURLFromFile(&webConfigURL); - pString = webConfigURL; - printf("The value of pString is %s\n",pString); - return 0; -} - -int Set_Webconfig_URL( char *pString) -{ - printf("Set_Webconfig_URL pString %s\n", pString); - return 0; -} - -int registerWebcfgEvent(WebConfigEventCallback webcfgEventCB) -{ - UNUSED(webcfgEventCB); - return 0; -} - void test_FRNONE() { char *root_str = NULL; @@ -184,9 +53,10 @@ void test_FRNONE() } setRebootReason("factory-reset"); - get_root_version_string(&root_str, &root_version, http_status); + derive_root_doc_version_string(&root_str, &root_version, http_status); CU_ASSERT_FATAL( NULL != root_str ); CU_ASSERT_STRING_EQUAL( "NONE", root_str ); + WEBCFG_FREE(reason); } void test_MIGRATION() @@ -197,9 +67,10 @@ void test_MIGRATION() g_RebootReason ='\0'; setRebootReason("Software_upgrade"); - get_root_version_string(&root_str, &root_version, http_status); + derive_root_doc_version_string(&root_str, &root_version, http_status); CU_ASSERT_FATAL( NULL != root_str ); CU_ASSERT_STRING_EQUAL( "NONE-MIGRATION", root_str ); + WEBCFG_FREE(reason); } void test_NONE_REBOOT() @@ -210,9 +81,10 @@ void test_NONE_REBOOT() g_RebootReason ='\0'; setRebootReason("unknown"); - get_root_version_string(&root_str, &root_version, http_status); + derive_root_doc_version_string(&root_str, &root_version, http_status); CU_ASSERT_FATAL( NULL != root_str ); CU_ASSERT_STRING_EQUAL( "NONE-REBOOT", root_str ); + WEBCFG_FREE(reason); } void test_ROOTNULL() @@ -224,8 +96,9 @@ void test_ROOTNULL() reason = NULL; setRebootReason(NULL); - get_root_version_string(&root_str, &root_version, http_status); + derive_root_doc_version_string(&root_str, &root_version, http_status); CU_ASSERT_FATAL( NULL == root_str ); + WEBCFG_FREE(reason); } void test_POSTNONE() @@ -240,7 +113,7 @@ void test_POSTNONE() checkDBList("root", root_version, root_str); addNewDocEntry(1); - get_root_version_string(&root_str, &root_version, http_status); + derive_root_doc_version_string(&root_str, &root_version, http_status); CU_ASSERT_FATAL( NULL != root_str ); CU_ASSERT_STRING_EQUAL( "NONE-REBOOT", root_str ); fp = fopen("/tmp/webconfig_db.bin","rb"); @@ -263,7 +136,7 @@ void test_FR_404() checkDBList("root", root_version, root_str); addNewDocEntry(1); - get_root_version_string(&root_str, &root_version, http_status); + derive_root_doc_version_string(&root_str, &root_version, http_status); CU_ASSERT_FATAL( NULL != root_str ); CU_ASSERT_STRING_EQUAL( "POST-NONE", root_str ); fp = fopen("/tmp/webconfig_db.bin","rb"); @@ -286,7 +159,7 @@ void test_FR_200() checkDBList("root", root_version, root_str); addNewDocEntry(1); - get_root_version_string(&root_str, &root_version, http_status); + derive_root_doc_version_string(&root_str, &root_version, http_status); CU_ASSERT_FATAL( NULL != root_str ); CU_ASSERT_STRING_EQUAL( "POST-NONE", root_str ); fp = fopen("/tmp/webconfig_db.bin","rb"); @@ -310,7 +183,7 @@ void test_FW_ROLLBACK() checkDBList("test", 123, NULL); addNewDocEntry(2); - get_root_version_string(&root_str, &root_version, http_status); + derive_root_doc_version_string(&root_str, &root_version, http_status); CU_ASSERT_FATAL( NULL == root_str ); CU_ASSERT_EQUAL( 0, root_version ); fp = fopen("/tmp/webconfig_db.bin","rb"); @@ -334,7 +207,7 @@ void test_zerorootVersion() checkDBList("test", 1234, NULL); addNewDocEntry(2); - get_root_version_string(&root_str, &root_version, http_status); + derive_root_doc_version_string(&root_str, &root_version, http_status); CU_ASSERT_FATAL( NULL != root_str ); CU_ASSERT_STRING_EQUAL( "POST-NONE", root_str ); fp = fopen("/tmp/webconfig_db.bin","rb"); @@ -358,7 +231,7 @@ void test_rootVersion() checkDBList("test", 1234, NULL); addNewDocEntry(2); - get_root_version_string(&root_str, &root_version, http_status); + derive_root_doc_version_string(&root_str, &root_version, http_status); CU_ASSERT_FATAL( NULL == root_str ); CU_ASSERT_EQUAL( 44444, root_version ); fp = fopen("/tmp/webconfig_db.bin","rb"); From bb126350b79225befc1fcb9ed87202bb52c0de82 Mon Sep 17 00:00:00 2001 From: Sadhyama Vengilat Date: Fri, 28 Aug 2020 15:57:19 +0530 Subject: [PATCH 21/21] To avoid NULL logging for root string --- src/webcfg.c | 2 +- src/webcfg_db.c | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/webcfg.c b/src/webcfg.c index 60b8b3ce..0f700d2c 100644 --- a/src/webcfg.c +++ b/src/webcfg.c @@ -332,7 +332,7 @@ int handlehttpResponse(long response_code, char *webConfigData, int retry_count, } else { - WebcfgInfo("root webConfigData processed, check apply status events\n"); + WebcfgDebug("root webConfigData processed, check apply status events\n"); return 1; } } diff --git a/src/webcfg_db.c b/src/webcfg_db.c index cd7e928d..b03f90de 100644 --- a/src/webcfg_db.c +++ b/src/webcfg_db.c @@ -486,7 +486,14 @@ void checkDBList(char *docname, uint32_t version, char* rootstr) webcfgdb->next = NULL; addToDBList(webcfgdb); - WebcfgInfo("webcfgdb->name added to DB %s webcfgdb->version %lu webcfgdb->root_string %s\n",webcfgdb->name, (long)webcfgdb->version, webcfgdb->root_string); + if(webcfgdb->root_string !=NULL) + { + WebcfgInfo("webcfgdb->name added to DB %s webcfgdb->version %lu webcfgdb->root_string %s\n",webcfgdb->name, (long)webcfgdb->version, webcfgdb->root_string); + } + else + { + WebcfgInfo("webcfgdb->name added to DB %s webcfgdb->version %lu\n",webcfgdb->name, (long)webcfgdb->version); + } } else { @@ -848,7 +855,14 @@ char * get_DB_BLOB_base64() { for(k = 0;k< bd->entries_count ; k++) { - WebcfgInfo("Blob bd->entries[%zu].name %s, version: %lu, status: %s, error_details: %s, error_code: %d root_string: %s\n", k, bd->entries[k].name, (long)bd->entries[k].version, bd->entries[k].status, bd->entries[k].error_details, bd->entries[k].error_code, bd->entries[k].root_string ); + if(bd->entries[k].root_string !=NULL) + { + WebcfgInfo("Blob bd->entries[%zu].name %s, version: %lu, status: %s, error_details: %s, error_code: %d root_string: %s\n", k, bd->entries[k].name, (long)bd->entries[k].version, bd->entries[k].status, bd->entries[k].error_details, bd->entries[k].error_code, bd->entries[k].root_string ); + } + else + { + WebcfgInfo("Blob bd->entries[%zu].name %s, version: %lu, status: %s, error_details: %s, error_code: %d\n", k, bd->entries[k].name, (long)bd->entries[k].version, bd->entries[k].status, bd->entries[k].error_details, bd->entries[k].error_code ); + } } }