Skip to content

Commit

Permalink
Merge pull request #29 from sadhyama/webcfg_reboot
Browse files Browse the repository at this point in the history
Webcfg handling of Factory Reset and Reboot scenarios
  • Loading branch information
shilpa24balaji authored Aug 28, 2020
2 parents 1d7b4dc + bb12635 commit d9d3924
Show file tree
Hide file tree
Showing 17 changed files with 689 additions and 65 deletions.
57 changes: 54 additions & 3 deletions src/webcfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,18 +297,29 @@ 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'};
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);
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)
{
WEBCFG_FREE(db_root_string);
}
WEBCFG_FREE(transaction_uuid);
return 1;
}
else if(response_code == 200)
{
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");
Expand All @@ -321,18 +332,35 @@ int handlehttpResponse(long response_code, char *webConfigData, int retry_count,
}
else
{
WebcfgError("Failed to apply root webConfigData received from server\n");
WebcfgDebug("root webConfigData processed, check apply status events\n");
return 1;
}
}
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
contentLength = get_global_contentLen();
if((contentLength !=NULL) && (strcmp(contentLength, "0") == 0))
{
WebcfgInfo("webConfigData content length is 0\n");
refreshConfigVersionList(version, response_code);
WEBCFG_FREE(contentLength);
WEBCFG_FREE(transaction_uuid);
WEBCFG_FREE(webConfigData);
return 1;
}
}
}
else if(response_code == 204)
{
WebcfgInfo("No configuration available for this device. response_code:%ld\n", response_code);
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)
{
WEBCFG_FREE(db_root_string);
}
WEBCFG_FREE(transaction_uuid);
return 1;
}
Expand All @@ -346,13 +374,30 @@ 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);
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)
{
WEBCFG_FREE(db_root_string);
}
WEBCFG_FREE(transaction_uuid);
return 1;
}
first_digit = (int)(response_code / pow(10, (int)log10(response_code)));
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 root version when 404
refreshConfigVersionList(version, response_code);
}
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)
{
WEBCFG_FREE(db_root_string);
}
WEBCFG_FREE(transaction_uuid);
return 1;
}
Expand All @@ -362,6 +407,12 @@ int handlehttpResponse(long response_code, char *webConfigData, int retry_count,
if(retry_count == 3 && !err)
{
WebcfgDebug("3 curl retry attempts\n");
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)
{
WEBCFG_FREE(db_root_string);
}
WEBCFG_FREE(transaction_uuid);
return 0;
}
Expand Down
4 changes: 2 additions & 2 deletions src/webcfg_aker.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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);
Expand Down
84 changes: 70 additions & 14 deletions src/webcfg_db.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -461,9 +464,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));
Expand All @@ -473,10 +476,24 @@ void checkDBList(char *docname, uint32_t version)

webcfgdb->name = strdup(docname);
webcfgdb->version = version;
if( strcmp("root", webcfgdb->name) == 0)
{
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);
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
{
Expand All @@ -485,7 +502,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();
Expand All @@ -495,11 +512,23 @@ 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( strcmp("root", webcfgdb->name) == 0)
{
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);
WebcfgDebug("mutex_unlock if docname is webcfgdb name\n");
return WEBCFG_SUCCESS;
Expand Down Expand Up @@ -641,7 +670,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;
Expand Down Expand Up @@ -675,16 +704,27 @@ 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++;
}

if( 1 & objects_left )
{
if( (1 << 0) & objects_left )
{
WebcfgDebug("Skip optional root_string element\n");
objects_left &= ~(1 << 0);
}
}
else
{
Expand Down Expand Up @@ -815,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\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 );
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 );
}
}

}
Expand Down Expand Up @@ -874,7 +921,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);

}
}
Expand All @@ -898,14 +945,23 @@ 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++;
}

if( 1 & objects_left )
{
}
else if( (1 << 3) & objects_left ) {
WebcfgDebug("Skip optional root_string element\n");
objects_left &= ~(1 << 3);
}
else
{
errno = BD_OK;
Expand Down
6 changes: 4 additions & 2 deletions src/webcfg_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
Loading

0 comments on commit d9d3924

Please sign in to comment.