From 84e632371c913f5d28fdf5a7d8a052df1c029091 Mon Sep 17 00:00:00 2001 From: Omar Kallel Date: Tue, 17 Jan 2023 10:18:51 +0100 Subject: [PATCH] Ticket refs #9261: icwmp: apply new xml functions for backupsession --- src/autonomous_complpolicy.c | 16 +- src/backupSession.c | 1683 +++++++----------- src/backupSession.h | 31 +- src/common.h | 18 + src/cwmp.c | 9 + src/cwmp_du_state.c | 14 +- src/cwmp_event.c | 4 +- src/download.c | 80 +- src/event.c | 4 +- src/rpc.c | 31 +- src/sched_inform.c | 2 +- src/upload.c | 12 +- src/xml.c | 253 ++- src/xml.h | 72 +- test/cmocka/icwmp_backup_session_unit_test.c | 15 +- 15 files changed, 1101 insertions(+), 1143 deletions(-) diff --git a/src/autonomous_complpolicy.c b/src/autonomous_complpolicy.c index 6312980..5cb97c6 100644 --- a/src/autonomous_complpolicy.c +++ b/src/autonomous_complpolicy.c @@ -149,7 +149,11 @@ static void send_du_state_change_notif(struct blob_attr *msg) free_autonomous_du_state_change_complete_data(data); return; } - + if ((cwmp_main->auto_cdu_id < 0) || (cwmp_main->auto_cdu_id >= MAX_INT_ID)) { + cwmp_main->auto_cdu_id = 0; + } + cwmp_main->auto_cdu_id++; + data->id = cwmp_main->auto_cdu_id; bkp_session_insert_autonomous_du_state_change(data); bkp_session_save(); @@ -251,7 +255,11 @@ static void send_transfer_complete_notif(struct blob_attr *msg) free_autonomous_transfer_complete_data(data); return; } - + if ((cwmp_main->auto_tc_id < 0) || (cwmp_main->auto_tc_id >= MAX_INT_ID)) { + cwmp_main->auto_tc_id = 0; + } + cwmp_main->auto_tc_id++; + data->id = cwmp_main->auto_tc_id; bkp_session_insert_autonomous_transfer_complete(data); bkp_session_save(); @@ -337,7 +345,7 @@ int cwmp_rpc_acs_destroy_data_autonomous_du_state_change_complete(struct rpc *rp { auto_du_state_change_compl *p = (auto_du_state_change_compl *)rpc->extra_data; if (p) { - bkp_session_delete_autonomous_du_state_change(p); + bkp_session_delete_element("autonomous_du_state_change_complete", p->id); free_autonomous_du_state_change_complete_data(p); } @@ -348,7 +356,7 @@ int cwmp_rpc_acs_destroy_data_autonomous_transfer_complete(struct rpc *rpc) { auto_transfer_complete *p = (auto_transfer_complete *)rpc->extra_data; if (p) { - bkp_session_delete_autonomous_transfer_complete(p); + bkp_session_delete_element("autonomous_transfer_complete", p->id); free_autonomous_transfer_complete_data(p); } diff --git a/src/backupSession.c b/src/backupSession.c index ec6af37..e1e78a8 100644 --- a/src/backupSession.c +++ b/src/backupSession.c @@ -26,154 +26,13 @@ static mxml_node_t *bkp_tree = NULL; -enum backup_attributes_types +int cwmp_init_backup_session(char **ret, enum backup_loading load) { - BKP_STRING, - BKP_INTEGER, - BKP_BOOL, - BKP_TIME, -}; - -struct backup_attributes_name_type { - char *name; - enum backup_attributes_types bkp_type; -}; - -struct backup_attributes_name_type bkp_attrs_names[] = { { "command_key", BKP_STRING }, - { "url", BKP_STRING }, - { "file_type", BKP_STRING }, - { "username", BKP_STRING }, - { "password", BKP_STRING }, - { "windowmode1", BKP_STRING }, - { "usermessage1", BKP_STRING }, - { "windowmode2", BKP_STRING }, - { "usermessage2", BKP_STRING }, - { "start_time", BKP_STRING }, - { "complete_time", BKP_STRING }, - { "uuid", BKP_STRING }, - { "version", BKP_STRING }, - { "du_ref", BKP_STRING }, - { "current_state", BKP_STRING }, - { "execution_unit_ref", BKP_STRING }, - { "old_software_version", BKP_STRING }, - { "executionenvref", BKP_STRING }, - { "index", BKP_INTEGER }, - { "id", BKP_INTEGER }, - { "file_size", BKP_INTEGER }, - { "maxretrie1", BKP_INTEGER }, - { "maxretrie2", BKP_INTEGER }, - { "type", BKP_INTEGER }, - { "fault", BKP_INTEGER }, - { "fault_code", BKP_INTEGER }, - { "resolved", BKP_BOOL }, - { "time", BKP_TIME }, - { "fault_string", BKP_STRING }, - { "operation", BKP_STRING }, - { "windowstart1", BKP_TIME }, - { "windowend1", BKP_TIME }, - { "windowstart2", BKP_TIME }, - { "windowend2", BKP_TIME } }; -struct backup_attributes { - char **command_key; - char **url; - char **file_type; - char **username; - char **password; - char **windowmode1; - char **usermessage1; - char **windowmode2; - char **usermessage2; - char **start_time; - char **complete_time; - char **uuid; - char **version; - char **du_ref; - char **current_state; - char **execution_unit_ref; - char **old_software_version; - char **executionenvref; - int *index; - int *id; - int *file_size; - int *maxretrie1; - int *maxretrie2; - int *type; - int *fault; - int *fault_code; - bool *resolved; - time_t *time; - char **fault_string; - char **operation; - time_t *windowstart1; - time_t *windowend1; - time_t *windowstart2; - time_t *windowend2; -}; - -int get_bkp_attribute_index_type(const char *name) -{ - unsigned int i; - if (name == NULL) - return -1; - size_t total_size = sizeof(bkp_attrs_names) / sizeof(struct backup_attributes_name_type); - for (i = 0; i < total_size; i++) { - if (strcmp(name, bkp_attrs_names[i].name) == 0) - return i; - } - return -1; -} - -void load_specific_backup_attributes(mxml_node_t *tree, struct backup_attributes *bkp_attrs) -{ - mxml_node_t *b = tree, *c; - int idx; - void **ptr; - - b = mxmlWalkNext(b, tree, MXML_DESCEND); - while (b) { - if (mxmlGetType(b) == MXML_ELEMENT) { - idx = get_bkp_attribute_index_type(mxmlGetElement(b)); - if (idx == -1) { - b = mxmlWalkNext(b, tree, MXML_NO_DESCEND); - continue; - } - c = mxmlWalkNext(b, b, MXML_DESCEND); - if (c && mxmlGetType(c) == MXML_OPAQUE) { - const char *opaque = mxmlGetOpaque(c); - if (opaque != NULL) { - char **str; - int *intgr; - bool *bol; - time_t *time; - - ptr = (void **)((char *)bkp_attrs + idx * sizeof(char *)); - if (ptr == NULL || *ptr == NULL) { - b = mxmlWalkNext(b, tree, MXML_NO_DESCEND); - continue; - } - switch (bkp_attrs_names[idx].bkp_type) { - case BKP_STRING: - str = (char **)(*ptr); - *str = strdup(opaque); - break; - case BKP_INTEGER: - intgr = (int *)(*ptr); - *intgr = atoi(opaque); - break; - case BKP_BOOL: - bol = (bool *)(*ptr); - *bol = opaque; - break; - case BKP_TIME: - time = (time_t *)(*ptr); - *time = atol(opaque); - break; - } - } - } - } - b = mxmlWalkNext(b, tree, MXML_NO_DESCEND); - } + int error; + if (bkp_session_check_file()) + return 0; + error = cwmp_load_saved_session(ret, load); + return error; } void bkp_tree_clean(void) @@ -194,919 +53,6 @@ void bkp_session_save() sync(); } -mxml_node_t *bkp_session_insert(mxml_node_t *tree, char *name, char *value) -{ - mxml_node_t *b; - if (tree == NULL || name == NULL) { - CWMP_LOG(ERROR, "backup %s: tree or name is null: %p %p", __FUNCTION__, tree, name); - return NULL; - } - b = mxmlNewElement(tree, name); - if (b == NULL) - return NULL; - - if (value != NULL) - mxmlNewOpaque(b, value); - - return b; -} -/* - * The order of key array filling should be the same of insertion function - */ -mxml_node_t *bkp_session_node_found(mxml_node_t *tree, char *name, struct search_keywords *keys, int size) -{ - mxml_node_t *b = tree, *c, *d; - struct search_keywords; - int i = 0; - if (!tree) - return NULL; - b = mxmlFindElement(b, b, name, NULL, NULL, MXML_DESCEND_FIRST); - while (b) { - c = mxmlGetFirstChild(b); - if (c) { - i = 0; - while (c && i < size) { - if (mxmlGetType(c) == MXML_ELEMENT && strcmp(keys[i].name, (char *) mxmlGetElement(c)) == 0) { - d = c; - d = mxmlWalkNext(d, c, MXML_DESCEND); - if ((keys[i].value == NULL) || (d && mxmlGetType(d) == MXML_OPAQUE && CWMP_STRCMP(keys[i].value, mxmlGetOpaque(d)) == 0)) - i++; - } - c = mxmlWalkNext(c, b, MXML_NO_DESCEND); - } - } - if (i == size) - break; - - b = mxmlWalkNext(b, tree, MXML_NO_DESCEND); - } - return b; -} - -mxml_node_t *bkp_session_insert_event(int index, char *command_key, int id) -{ - struct search_keywords keys[1]; - char event_id[32]; - char event_idx[32]; - mxml_node_t *b; - - snprintf(event_id, sizeof(event_id), "%d", id); - snprintf(event_idx, sizeof(event_idx), "%d", index); - keys[0].name = "id"; - keys[0].value = event_id; - b = bkp_session_node_found(bkp_tree, "cwmp_event", keys, 1); - if (!b) { - b = bkp_session_insert(bkp_tree, "cwmp_event", NULL); - bkp_session_insert(b, "index", event_idx); - bkp_session_insert(b, "id", event_id); - bkp_session_insert(b, "command_key", command_key ? command_key : ""); - } - return b; -} - -void bkp_session_delete_event(int id) -{ - struct search_keywords keys[1]; - char event_id[32]; - mxml_node_t *b; - - snprintf(event_id, sizeof(event_id), "%d", id); - keys[0].name = "id"; - keys[0].value = event_id; - b = bkp_session_node_found(bkp_tree, "cwmp_event", keys, 1); - if (b) - mxmlDelete(b); -} - -void bkp_session_insert_parameter(mxml_node_t *b, char *name) -{ - bkp_session_insert(b, "parameter", name); -} - -void bkp_session_simple_insert(char *parent, char *child, char *value) -{ - mxml_node_t *b = bkp_tree; - - if (parent == NULL || child == NULL) { - CWMP_LOG(ERROR, "backup %s: parent or child is null %p %p", __FUNCTION__, parent, child); - return; - } - b = mxmlFindElement(b, b, parent, NULL, NULL, MXML_DESCEND); - if (b) - mxmlDelete(b); - b = bkp_session_insert(bkp_tree, parent, NULL); - bkp_session_insert(b, child, value); -} - -void bkp_session_simple_insert_in_parent(char *parent, char *child, char *value) -{ - mxml_node_t *n, *b = bkp_tree; - - if (parent == NULL || child == NULL) { - CWMP_LOG(ERROR, "backup %s: parent or child is null %p %p", __FUNCTION__, parent, child); - return; - } - n = mxmlFindElement(b, b, parent, NULL, NULL, MXML_DESCEND); - if (!n) - n = bkp_session_insert(bkp_tree, parent, NULL); - b = mxmlFindElement(n, n, child, NULL, NULL, MXML_DESCEND); - if (b) - mxmlDelete(b); - bkp_session_insert(n, child, value); -} - -void bkp_session_insert_schedule_inform(time_t time, char *command_key) -{ - char schedule_time[128]; - mxml_node_t *b; - - snprintf(schedule_time, sizeof(schedule_time), "%lld", (long long int)time); - struct search_keywords sched_inf_insert_keys[2] = { { "command_key", command_key }, { "time", schedule_time } }; - b = bkp_session_node_found(bkp_tree, "schedule_inform", sched_inf_insert_keys, 2); - if (!b) { - b = bkp_session_insert(bkp_tree, "schedule_inform", NULL); - bkp_session_insert(b, "command_key", command_key ? command_key : ""); - bkp_session_insert(b, "time", schedule_time); - } -} - -void bkp_session_delete_schedule_inform(time_t time, char *command_key) -{ - char schedule_time[128]; - mxml_node_t *b; - - snprintf(schedule_time, sizeof(schedule_time), "%lld", (long long int)time); - struct search_keywords sched_inf_del_keys[2] = { { "command_key", command_key }, { "time", schedule_time } }; - b = bkp_session_node_found(bkp_tree, "schedule_inform", sched_inf_del_keys, 2); - if (b) - mxmlDelete(b); -} - -void bkp_session_insert_download(struct download *pdownload) -{ - char schedule_time[128]; - char file_size[128]; - mxml_node_t *b; - - snprintf(schedule_time, sizeof(schedule_time), "%lld", (long long int)pdownload->scheduled_time); - snprintf(file_size, sizeof(file_size), "%d", pdownload->file_size); - struct search_keywords download_insert_keys[7] = { { "url", pdownload->url }, { "command_key", pdownload->command_key }, { "file_type", pdownload->file_type }, { "username", pdownload->username }, { "password", pdownload->password }, { "file_size", file_size }, { "time", schedule_time } }; - - b = bkp_session_node_found(bkp_tree, "download", download_insert_keys, 7); - if (!b) { - b = bkp_session_insert(bkp_tree, "download", NULL); - bkp_session_insert(b, "url", pdownload->url ? pdownload->url : ""); - bkp_session_insert(b, "command_key", pdownload->command_key ? pdownload->command_key : ""); - bkp_session_insert(b, "file_type", pdownload->file_type ? pdownload->file_type : ""); - bkp_session_insert(b, "username", pdownload->username ? pdownload->username : ""); - bkp_session_insert(b, "password", pdownload->password ? pdownload->password : ""); - bkp_session_insert(b, "file_size", file_size); - bkp_session_insert(b, "time", schedule_time); - } -} - -void bkp_session_insert_schedule_download(struct download *pschedule_download) -{ - char delay[4][128]; - int i; - char file_size[128]; - char maxretrie[2][128]; - mxml_node_t *b; - - snprintf(file_size, sizeof(file_size), "%d", pschedule_download->file_size); - for (i = 0; i < 2; i++) { - snprintf(delay[2 * i], sizeof(delay[i]), "%lld", (long long int)pschedule_download->timewindowstruct[i].windowstart); - snprintf(delay[2 * i + 1], sizeof(delay[i]), "%lld", (long long int)pschedule_download->timewindowstruct[i].windowend); - snprintf(maxretrie[i], sizeof(maxretrie[i]), "%d", pschedule_download->timewindowstruct[i].maxretries); - } - struct search_keywords sched_download_insert_keys[16] = { { "url", pschedule_download->url }, - { "command_key", pschedule_download->command_key }, - { "file_type", pschedule_download->file_type }, - { "username", pschedule_download->username }, - { "password", pschedule_download->password }, - { "file_size", file_size }, - { "windowstart1", delay[0] }, - { "windowend1", delay[1] }, - { "windowmode1", pschedule_download->timewindowstruct[0].windowmode }, - { "usermessage1", pschedule_download->timewindowstruct[0].usermessage }, - { "maxretrie1", maxretrie[0] }, - { "windowstart2", delay[2] }, - { "windowend2", delay[3] }, - { "windowmode2", pschedule_download->timewindowstruct[1].windowmode }, - { "usermessage2", pschedule_download->timewindowstruct[1].usermessage }, - { "maxretrie2", maxretrie[1] } }; - - b = bkp_session_node_found(bkp_tree, "schedule_download", sched_download_insert_keys, 16); - if (!b) { - b = bkp_session_insert(bkp_tree, "schedule_download", NULL); - bkp_session_insert(b, "url", pschedule_download->url); - bkp_session_insert(b, "command_key", pschedule_download->command_key); - bkp_session_insert(b, "file_type", pschedule_download->file_type); - bkp_session_insert(b, "username", pschedule_download->username); - bkp_session_insert(b, "password", pschedule_download->password); - bkp_session_insert(b, "file_size", file_size); - bkp_session_insert(b, "windowstart1", delay[0]); - bkp_session_insert(b, "windowend1", delay[1]); - bkp_session_insert(b, "windowmode1", pschedule_download->timewindowstruct[0].windowmode); - bkp_session_insert(b, "usermessage1", pschedule_download->timewindowstruct[0].usermessage); - bkp_session_insert(b, "maxretrie1", maxretrie[0]); - bkp_session_insert(b, "windowstart2", delay[2]); - bkp_session_insert(b, "windowend2", delay[3]); - bkp_session_insert(b, "windowmode2", pschedule_download->timewindowstruct[1].windowmode); - bkp_session_insert(b, "usermessage2", pschedule_download->timewindowstruct[1].usermessage); - bkp_session_insert(b, "maxretrie2", maxretrie[1]); - } -} - -void bkp_session_insert_change_du_state(struct change_du_state *pchange_du_state) -{ - struct operations *p = NULL; - char schedule_time[128]; - mxml_node_t *b, *n; - - snprintf(schedule_time, sizeof(schedule_time), "%lld", (long long int)pchange_du_state->timeout); - b = bkp_session_insert(bkp_tree, "change_du_state", NULL); - bkp_session_insert(b, "command_key", pchange_du_state->command_key); - bkp_session_insert(b, "time", schedule_time); - list_for_each_entry (p, &(pchange_du_state->list_operation), list) { - if (p->type == DU_INSTALL) { - n = bkp_session_insert(b, "install", NULL); - bkp_session_insert(n, "url", p->url); - bkp_session_insert(n, "uuid", p->uuid); - bkp_session_insert(n, "username", p->username); - bkp_session_insert(n, "password", p->password); - bkp_session_insert(n, "executionenvref", p->executionenvref); - } else if (p->type == DU_UPDATE) { - n = bkp_session_insert(b, "update", NULL); - bkp_session_insert(n, "uuid", p->uuid); - bkp_session_insert(n, "version", p->version); - bkp_session_insert(n, "url", p->url); - bkp_session_insert(n, "username", p->username); - bkp_session_insert(n, "password", p->password); - } else if (p->type == DU_UNINSTALL) { - n = bkp_session_insert(b, "uninstall", NULL); - bkp_session_insert(n, "uuid", p->uuid); - bkp_session_insert(n, "version", p->version); - bkp_session_insert(n, "executionenvref", p->executionenvref); - } - } -} - -void bkp_session_delete_change_du_state(struct change_du_state *pchange_du_state) -{ - char schedule_time[128]; - mxml_node_t *b; - - snprintf(schedule_time, sizeof(schedule_time), "%lld", (long long int)pchange_du_state->timeout); - struct search_keywords cds_del_keys[2] = { { "command_key", pchange_du_state->command_key }, { "time", schedule_time } }; - b = bkp_session_node_found(bkp_tree, "change_du_state", cds_del_keys, 2); - if (b) - mxmlDelete(b); -} - -void bkp_session_insert_upload(struct upload *pupload) -{ - char schedule_time[128]; - mxml_node_t *b; - - snprintf(schedule_time, sizeof(schedule_time), "%lld", (long long int)pupload->scheduled_time); - struct search_keywords upload_insert_keys[6] = { { "url", pupload->url }, { "command_key", pupload->command_key }, { "username", pupload->username }, { "password", pupload->password }, { "time", schedule_time }, { "file_type", pupload->file_type } }; - - b = bkp_session_node_found(bkp_tree, "upload", upload_insert_keys, 6); - if (!b) { - b = bkp_session_insert(bkp_tree, "upload", NULL); - bkp_session_insert(b, "url", pupload->url); - bkp_session_insert(b, "command_key", pupload->command_key); - bkp_session_insert(b, "file_type", pupload->file_type); - bkp_session_insert(b, "username", pupload->username); - bkp_session_insert(b, "password", pupload->password); - bkp_session_insert(b, "time", schedule_time); - } -} -void bkp_session_delete_download(struct download *pdownload) -{ - char schedule_time[128]; - char file_size[128]; - mxml_node_t *b; - - snprintf(schedule_time, sizeof(schedule_time), "%lld", (long long int)pdownload->scheduled_time); - snprintf(file_size, sizeof(file_size), "%d", pdownload->file_size); - struct search_keywords download_del_keys[7] = { { "url", pdownload->url }, { "command_key", pdownload->command_key }, { "file_type", pdownload->file_type }, { "username", pdownload->username }, { "password", pdownload->password }, { "file_size", file_size }, { "time", schedule_time } }; - - b = bkp_session_node_found(bkp_tree, "download", download_del_keys, 7); - if (b) - mxmlDelete(b); -} - -void bkp_session_delete_schedule_download(struct download *pschedule_download_delete) -{ - char delay[4][128]; - char file_size[128]; - char maxretrie[2][128]; - int i; - mxml_node_t *b; - - snprintf(file_size, sizeof(file_size), "%d", pschedule_download_delete->file_size); - for (i = 0; i < 2; i++) { - snprintf(delay[2 * i], sizeof(delay[i]), "%lld", (long long int)pschedule_download_delete->timewindowstruct[i].windowstart); - snprintf(delay[2 * i + 1], sizeof(delay[i]), "%lld", (long long int)pschedule_download_delete->timewindowstruct[i].windowend); - snprintf(maxretrie[i], sizeof(maxretrie[i]), "%d", pschedule_download_delete->timewindowstruct[i].maxretries); - } - struct search_keywords sched_download_del_keys[16] = { { "url", pschedule_download_delete->url }, - { "command_key", pschedule_download_delete->command_key }, - { "file_type", pschedule_download_delete->file_type }, - { "username", pschedule_download_delete->username }, - { "password", pschedule_download_delete->password }, - { "file_size", file_size }, - { "windowstart1", delay[0] }, - { "windowend1", delay[1] }, - { "windowmode1", pschedule_download_delete->timewindowstruct[0].windowmode }, - { "usermessage1", pschedule_download_delete->timewindowstruct[0].usermessage }, - { "maxretrie1", maxretrie[0] }, - { "windowstart2", delay[2] }, - { "windowend2", delay[3] }, - { "windowmode2", pschedule_download_delete->timewindowstruct[1].windowmode }, - { "usermessage2", pschedule_download_delete->timewindowstruct[1].usermessage }, - { "maxretrie2", maxretrie[1] } }; - - b = bkp_session_node_found(bkp_tree, "schedule_download", sched_download_del_keys, 16); - - if (b) - mxmlDelete(b); -} -void bkp_session_delete_upload(struct upload *pupload) -{ - char schedule_time[128]; - mxml_node_t *b; - - snprintf(schedule_time, sizeof(schedule_time), "%lld", (long long int)pupload->scheduled_time); - struct search_keywords upload_del_keys[6] = { { "url", pupload->url }, { "command_key", pupload->command_key }, { "file_type", pupload->file_type }, { "username", pupload->username }, { "password", pupload->password }, { "time", schedule_time } }; - b = bkp_session_node_found(bkp_tree, "upload", upload_del_keys, 6); - if (b) - mxmlDelete(b); -} - -void bkp_session_insert_autonomous_du_state_change(auto_du_state_change_compl *data) -{ - char resolved[8], fault_code[8]; - mxml_node_t *b; - - if (data == NULL) - return; - - snprintf(resolved, sizeof(resolved), "%d", data->resolved); - snprintf(fault_code, sizeof(fault_code), "%d", data->fault_code); - - struct search_keywords keys[9] = { - { "uuid", data->uuid }, - { "version", data->ver ? data->ver : "" }, - { "current_state", data->current_state ? data->current_state : "" }, - { "resolved", resolved }, - { "start_time", data->start_time ? data->start_time : "" }, - { "complete_time", data->complete_time ? data->complete_time : "" }, - { "fault_code", fault_code }, - { "fault_string", data->fault_string ? data->fault_string : "" }, - { "operation", data->operation ? data->operation : "" } - }; - - b = bkp_session_node_found(bkp_tree, "autonomous_du_state_change_complete", keys, 9); - if (!b) { - b = bkp_session_insert(bkp_tree, "autonomous_du_state_change_complete", NULL); - bkp_session_insert(b, "uuid", data->uuid); - bkp_session_insert(b, "version", data->ver ? data->ver : ""); - bkp_session_insert(b, "current_state", data->current_state ? data->current_state : ""); - bkp_session_insert(b, "resolved", resolved); - bkp_session_insert(b, "start_time", data->start_time ? data->start_time : ""); - bkp_session_insert(b, "complete_time", data->complete_time ? data->complete_time : ""); - bkp_session_insert(b, "fault_code", fault_code); - bkp_session_insert(b, "fault_string", data->fault_string ? data->fault_string : ""); - bkp_session_insert(b, "operation", data->operation ? data->operation : ""); - } -} - -void bkp_session_delete_autonomous_du_state_change(auto_du_state_change_compl *data) -{ - char resolved[8], fault_code[8]; - mxml_node_t *b; - - if (data == NULL) - return; - - snprintf(resolved, sizeof(resolved), "%d", data->resolved); - snprintf(fault_code, sizeof(fault_code), "%d", data->fault_code); - - struct search_keywords keys[9] = { - { "uuid", data->uuid }, - { "version", data->ver ? data->ver : "" }, - { "current_state", data->current_state ? data->current_state : "" }, - { "resolved", resolved }, - { "start_time", data->start_time ? data->start_time : "" }, - { "complete_time", data->complete_time ? data->complete_time : "" }, - { "fault_code", fault_code }, - { "fault_string", data->fault_string ? data->fault_string : "" }, - { "operation", data->operation ? data->operation : "" } - }; - - b = bkp_session_node_found(bkp_tree, "autonomous_du_state_change_complete", keys, 9); - if (!b) { - mxmlDelete(b); - } -} - -void bkp_session_insert_autonomous_transfer_complete(auto_transfer_complete *data) -{ - char is_download[8], fault_code[8], file_size[8]; - mxml_node_t *b; - - if (data == NULL) - return; - - snprintf(is_download, sizeof(is_download), "%d", data->is_download); - snprintf(file_size, sizeof(is_download), "%d", data->file_size); - snprintf(fault_code, sizeof(fault_code), "%d", data->fault_code); - - struct search_keywords keys[9] = { - { "announceurl", data->announce_url ? data->announce_url : "" }, - { "transferurl", data->transfer_url ? data->transfer_url : "" }, - { "isdownload", is_download }, - { "filetype", data->file_type }, - { "filesize", file_size }, - { "start_time", data->start_time ? data->start_time : "" }, - { "complete_time", data->complete_time ? data->complete_time : "" }, - { "fault_code", fault_code }, - { "fault_string", data->fault_string ? data->fault_string : "" } - }; - - b = bkp_session_node_found(bkp_tree, "autonomous_transfer_complete", keys, 9); - if (!b) { - b = bkp_session_insert(bkp_tree, "autonomous_transfer_complete", NULL); - bkp_session_insert(b, "announceurl", data->announce_url ? data->announce_url : ""); - bkp_session_insert(b, "transferurl", data->transfer_url ? data->transfer_url : ""); - bkp_session_insert(b, "isdownload", is_download); - bkp_session_insert(b, "filetype", data->file_type); - bkp_session_insert(b, "filesize", file_size); - bkp_session_insert(b, "start_time", data->start_time ? data->start_time : ""); - bkp_session_insert(b, "complete_time", data->complete_time ? data->complete_time : ""); - bkp_session_insert(b, "fault_code", fault_code); - bkp_session_insert(b, "fault_string", data->fault_string ? data->fault_string : ""); - } -} - -void bkp_session_delete_autonomous_transfer_complete(auto_transfer_complete *data) -{ - if (data == NULL) - return; - - char is_download[8], fault_code[8], file_size[8]; - snprintf(is_download, sizeof(is_download), "%d", data->is_download); - snprintf(file_size, sizeof(file_size), "%d", data->file_size); - snprintf(fault_code, sizeof(fault_code), "%d", data->fault_code); - - struct search_keywords keys[9] = { - { "announceurl", data->announce_url ? data->announce_url : "" }, - { "transferurl", data->transfer_url ? data->transfer_url : "" }, - { "isdownload", is_download }, - { "filetype", data->file_type }, - { "filesize", file_size }, - { "start_time", data->start_time ? data->start_time : "" }, - { "complete_time", data->complete_time ? data->complete_time : "" }, - { "fault_code", fault_code }, - { "fault_string", data->fault_string ? data->fault_string : "" } - }; - - mxml_node_t *b = bkp_session_node_found(bkp_tree, "autonomous_transfer_complete", keys, 9); - if (!b) { - mxmlDelete(b); - } -} - -void bkp_session_insert_du_state_change_complete(struct du_state_change_complete *pdu_state_change_complete) -{ - char schedule_time[128], resolved[8], fault_code[8]; - struct opresult *p = NULL; - mxml_node_t *b; - - snprintf(schedule_time, sizeof(schedule_time), "%lld", (long long int)pdu_state_change_complete->timeout); - b = bkp_session_insert(bkp_tree, "du_state_change_complete", NULL); - bkp_session_insert(b, "command_key", pdu_state_change_complete->command_key); - bkp_session_insert(b, "time", schedule_time); - list_for_each_entry (p, &(pdu_state_change_complete->list_opresult), list) { - mxml_node_t *n; - n = bkp_session_insert(b, "opresult", NULL); - snprintf(resolved, sizeof(resolved), "%d", p->resolved); - snprintf(fault_code, sizeof(fault_code), "%d", p->fault); - bkp_session_insert(n, "uuid", p->uuid); - bkp_session_insert(n, "du_ref", p->du_ref); - bkp_session_insert(n, "version", p->version); - bkp_session_insert(n, "current_state", p->current_state); - bkp_session_insert(n, "resolved", resolved); - bkp_session_insert(n, "execution_unit_ref", p->execution_unit_ref); - bkp_session_insert(n, "start_time", p->start_time); - bkp_session_insert(n, "complete_time", p->complete_time); - bkp_session_insert(n, "fault", fault_code); - } -} - -void bkp_session_delete_du_state_change_complete(struct du_state_change_complete *pdu_state_change_complete) -{ - mxml_node_t *b; - char schedule_time[128]; - - snprintf(schedule_time, sizeof(schedule_time), "%lld", (long long int)pdu_state_change_complete->timeout); - struct search_keywords cds_complete_keys[2] = { { "command_key", pdu_state_change_complete->command_key }, { "time", schedule_time } }; - - b = bkp_session_node_found(bkp_tree, "du_state_change_complete", cds_complete_keys, 2); - if (b) - mxmlDelete(b); -} -void bkp_session_insert_transfer_complete(struct transfer_complete *ptransfer_complete) -{ - struct search_keywords keys[5]; - char fault_code[16]; - char type[16]; - mxml_node_t *b; - - snprintf(fault_code, sizeof(fault_code), "%d", ptransfer_complete->fault_code); - keys[0].name = "command_key"; - keys[0].value = ptransfer_complete->command_key; - keys[1].name = "start_time"; - keys[1].value = ptransfer_complete->start_time; - keys[2].name = "complete_time"; - keys[2].value = ptransfer_complete->complete_time; - keys[3].name = "fault_code"; - keys[3].value = fault_code; - keys[4].name = "type"; - snprintf(type, sizeof(type), "%d", ptransfer_complete->type); - keys[4].value = type; - b = bkp_session_node_found(bkp_tree, "transfer_complete", keys, 5); - if (!b) { - b = bkp_session_insert(bkp_tree, "transfer_complete", NULL); - bkp_session_insert(b, "command_key", ptransfer_complete->command_key); - bkp_session_insert(b, "start_time", ptransfer_complete->start_time); - bkp_session_insert(b, "complete_time", ptransfer_complete->complete_time); - bkp_session_insert(b, "old_software_version", ptransfer_complete->old_software_version); - bkp_session_insert(b, "fault_code", fault_code); - bkp_session_insert(b, "type", type); - } -} - -void bkp_session_delete_transfer_complete(struct transfer_complete *ptransfer_complete) -{ - char fault_code[16]; - char type[16]; - mxml_node_t *b; - - snprintf(fault_code, sizeof(fault_code), "%d", ptransfer_complete->fault_code); - snprintf(type, sizeof(type), "%d", ptransfer_complete->type); - struct search_keywords trans_comp_del_keys[5] = { { "command_key", ptransfer_complete->command_key }, { "start_time", ptransfer_complete->start_time }, { "complete_time", ptransfer_complete->complete_time }, { "fault_code", fault_code }, { "type", type } }; - - b = bkp_session_node_found(bkp_tree, "transfer_complete", trans_comp_del_keys, 5); - if (b) - mxmlDelete(b); -} - -int save_acs_bkp_config() -{ - struct config *conf; - - conf = &(cwmp_main->conf); - bkp_session_simple_insert("acs", "url", conf->acsurl); - bkp_session_save(); - return CWMP_OK; -} - -/* - * Load backup session - */ -char *load_child_value(mxml_node_t *tree, char *sub_name) -{ - char *value = NULL; - mxml_node_t *b = tree; - - if (b) { - b = mxmlFindElement(b, b, sub_name, NULL, NULL, MXML_DESCEND); - if (b) { - b = mxmlWalkNext(b, tree, MXML_DESCEND); - if (b && mxmlGetType(b) == MXML_OPAQUE) { - const char *opaque = mxmlGetOpaque(b); - if (opaque != NULL) { - value = strdup(opaque); - } - } - } - } - return value; -} - -void load_queue_event(mxml_node_t *tree) -{ - char *command_key = NULL; - mxml_node_t *b = tree, *c; - int idx = -1, id = -1; - struct event_container *event_container_save = NULL; - - struct backup_attributes bkp_attrs = { .index = &idx, .id = &id, .command_key = &command_key }; - load_specific_backup_attributes(tree, &bkp_attrs); - - b = mxmlWalkNext(b, tree, MXML_DESCEND); - - while (b) { - if (mxmlGetType(b) == MXML_ELEMENT) { - const char *element = mxmlGetElement(b); - if (element == NULL) { - b = mxmlWalkNext(b, tree, MXML_NO_DESCEND); - continue; - } - if (strcmp(element, "command_key") == 0) { - // cppcheck-suppress knownConditionTrueFalse - /* - * idx value can be modified when loading the backup with the function load_specific_backup_attributes - */ - if (idx != -1) { - if (EVENT_CONST[idx].RETRY & EVENT_RETRY_AFTER_REBOOT) { - event_container_save = cwmp_add_event_container(idx, ((command_key != NULL) ? command_key : "")); - if (event_container_save != NULL) { - event_container_save->id = id; - } - } - } - FREE(command_key); - } else if (strcmp(element, "parameter") == 0) { - c = mxmlWalkNext(b, b, MXML_DESCEND); - if (c && mxmlGetType(c) == MXML_OPAQUE) { - const char *op = mxmlGetOpaque(c); - if (op != NULL) { - if (event_container_save != NULL) { - add_dm_parameter_to_list(&(event_container_save->head_dm_parameter), (char *)op, NULL, NULL, 0, false); - } - } - } - } - } - b = mxmlWalkNext(b, tree, MXML_NO_DESCEND); - } -} - -void load_schedule_inform(mxml_node_t *tree) -{ - char *command_key = NULL; - time_t scheduled_time = 0; - struct schedule_inform *schedule_inform = NULL; - struct list_head *ilist = NULL; - - struct backup_attributes bkp_attrs = { .command_key = &command_key, .time = &scheduled_time }; - load_specific_backup_attributes(tree, &bkp_attrs); - - list_for_each (ilist, &(list_schedule_inform)) { - schedule_inform = list_entry(ilist, struct schedule_inform, list); - if (schedule_inform->scheduled_time > scheduled_time) { - break; - } - } - schedule_inform = calloc(1, sizeof(struct schedule_inform)); - if (schedule_inform != NULL) { - schedule_inform->commandKey = command_key; - schedule_inform->scheduled_time = scheduled_time; - list_add(&(schedule_inform->list), ilist->prev); - } -} - -void load_download(mxml_node_t *tree) -{ - struct download *download_request = NULL; - struct list_head *ilist = NULL; - struct download *idownload_request = NULL; - - if (tree == NULL) { - CWMP_LOG(ERROR, "backup %s: tree is null", __FUNCTION__); - return; - } - download_request = calloc(1, sizeof(struct download)); - if (download_request == NULL) { - CWMP_LOG(ERROR, "backup %s: download_request is null", __FUNCTION__); - return; - } - struct backup_attributes bkp_attrs = { .url = &download_request->url, - .command_key = &download_request->command_key, - .file_type = &download_request->file_type, - .username = &download_request->username, - .password = &download_request->password, - .file_size = &download_request->file_size, - .time = &download_request->scheduled_time }; - load_specific_backup_attributes(tree, &bkp_attrs); - download_request->handler_timer.cb = cwmp_start_download; - - list_for_each (ilist, &(list_download)) { - idownload_request = list_entry(ilist, struct download, list); - if (idownload_request->scheduled_time > download_request->scheduled_time) { - break; - } - } - list_add(&(download_request->list), ilist->prev); - if (download_request->scheduled_time != 0) - count_download_queue++; - cwmp_set_end_session(END_SESSION_DOWNLOAD); -} - -void load_schedule_download(mxml_node_t *tree) -{ - struct download *download_request = NULL; - struct list_head *ilist = NULL; - struct download *idownload_request = NULL; - - if (tree == NULL) { - CWMP_LOG(ERROR, "backup %s: tree is null", __FUNCTION__); - return; - } - download_request = calloc(1, sizeof(struct download)); - if (download_request == NULL) { - CWMP_LOG(ERROR, "backup %s: download_request is null", __FUNCTION__); - return; - } - - struct backup_attributes bkp_attrs = { - .url = &download_request->url, - .command_key = &download_request->command_key, - .file_type = &download_request->file_type, - .username = &download_request->username, - .password = &download_request->password, - .file_size = &download_request->file_size, - .windowstart1 = &download_request->timewindowstruct[0].windowstart, - .windowend1 = &download_request->timewindowstruct[0].windowend, - .windowmode1 = &download_request->timewindowstruct[0].windowmode, - .usermessage1 = &download_request->timewindowstruct[0].usermessage, - .maxretrie1 = &download_request->timewindowstruct[0].maxretries, - .windowstart2 = &download_request->timewindowstruct[1].windowstart, - .windowend2 = &download_request->timewindowstruct[1].windowend, - .windowmode2 = &download_request->timewindowstruct[1].windowmode, - .usermessage2 = &download_request->timewindowstruct[1].usermessage, - .maxretrie2 = &download_request->timewindowstruct[1].maxretries, - }; - load_specific_backup_attributes(tree, &bkp_attrs); - - list_for_each (ilist, &(list_schedule_download)) { - idownload_request = list_entry(ilist, struct download, list); - if (idownload_request->timewindowstruct[0].windowstart > download_request->timewindowstruct[0].windowstart) { - break; - } - } - list_add(&(download_request->list), ilist->prev); - if (download_request->timewindowstruct[0].windowstart != 0) - count_download_queue++; -} - -void load_upload(mxml_node_t *tree) -{ - struct upload *upload_request = NULL; - struct list_head *ilist = NULL; - struct upload *iupload_request = NULL; - - if (tree == NULL) { - CWMP_LOG(ERROR, "backup %s: tree is null", __FUNCTION__); - return; - } - upload_request = calloc(1, sizeof(struct upload)); - if (upload_request == NULL) { - CWMP_LOG(ERROR, "backup %s: download_request is null", __FUNCTION__); - return; - } - - struct backup_attributes bkp_attrs = { .url = &upload_request->url, .command_key = &upload_request->command_key, .file_type = &upload_request->file_type, .username = &upload_request->username, .password = &upload_request->password, .time = &upload_request->scheduled_time }; - load_specific_backup_attributes(tree, &bkp_attrs); - - list_for_each (ilist, &(list_upload)) { - iupload_request = list_entry(ilist, struct upload, list); - if (iupload_request->scheduled_time > upload_request->scheduled_time) { - break; - } - } - list_add(&(upload_request->list), ilist->prev); - if (upload_request->scheduled_time != 0) - count_upload_queue++; -} - -void load_change_du_state(mxml_node_t *tree) -{ - if (tree == NULL) { - CWMP_LOG(ERROR, "backup %s: tree is null", __FUNCTION__); - return; - } - mxml_node_t *b = tree; - struct change_du_state *change_du_state_request = NULL; - struct operations *elem; - - change_du_state_request = calloc(1, sizeof(struct change_du_state)); - INIT_LIST_HEAD(&(change_du_state_request->list_operation)); - - struct backup_attributes bkp_attrs = { .command_key = &change_du_state_request->command_key, .time = &change_du_state_request->timeout }; - load_specific_backup_attributes(tree, &bkp_attrs); - - b = mxmlWalkNext(b, tree, MXML_DESCEND); - - while (b) { - if (mxmlGetType(b) == MXML_ELEMENT) { - const char *element = mxmlGetElement(b); - if (strcmp(element, "update") == 0) { - elem = (operations *)calloc(1, sizeof(operations)); - elem->type = DU_UPDATE; - list_add_tail(&(elem->list), &(change_du_state_request->list_operation)); - struct backup_attributes update_bkp_attrs = { .uuid = &elem->uuid, .version = &elem->version, .url = &elem->url, .username = &elem->username, .password = &elem->password }; - load_specific_backup_attributes(b, &update_bkp_attrs); - } else if (strcmp(element, "install") == 0) { - elem = (operations *)calloc(1, sizeof(operations)); - elem->type = DU_INSTALL; - list_add_tail(&(elem->list), &(change_du_state_request->list_operation)); - - struct backup_attributes install_bkp_attrs = { .uuid = &elem->uuid, .executionenvref = &elem->executionenvref, .url = &elem->url, .username = &elem->username, .password = &elem->password }; - load_specific_backup_attributes(b, &install_bkp_attrs); - } else if (strcmp(element, "uninstall") == 0) { - elem = (operations *)calloc(1, sizeof(operations)); - elem->type = DU_UNINSTALL; - list_add_tail(&(elem->list), &(change_du_state_request->list_operation)); - struct backup_attributes uninstall_bkp_attrs = { .uuid = &elem->uuid, .version = &elem->version, .executionenvref = &elem->executionenvref }; - load_specific_backup_attributes(b, &uninstall_bkp_attrs); - } - } - b = mxmlWalkNext(b, tree, MXML_NO_DESCEND); - } - list_add_tail(&(change_du_state_request->list_operation), &(list_change_du_state)); -} - -void load_du_state_change_complete(mxml_node_t *tree) -{ - mxml_node_t *b = tree; - struct du_state_change_complete *du_state_change_complete_request = NULL; - struct opresult *elem; - - du_state_change_complete_request = calloc(1, sizeof(struct du_state_change_complete)); - INIT_LIST_HEAD(&(du_state_change_complete_request->list_opresult)); - - struct backup_attributes bkp_attrs = { .command_key = &du_state_change_complete_request->command_key, .time = &du_state_change_complete_request->timeout }; - load_specific_backup_attributes(tree, &bkp_attrs); - - b = mxmlWalkNext(b, tree, MXML_DESCEND); - - while (b) { - if (mxmlGetType(b) == MXML_ELEMENT) { - if (strcmp(mxmlGetElement(b), "opresult") == 0) { - elem = (opresult *)calloc(1, sizeof(opresult)); - list_add_tail(&(elem->list), &(du_state_change_complete_request->list_opresult)); - - struct backup_attributes opresult_bkp_attrs = { .uuid = &elem->uuid, - .version = &elem->version, - .du_ref = &elem->du_ref, - .current_state = &elem->current_state, - .resolved = &elem->resolved, - .start_time = &elem->start_time, - .complete_time = &elem->complete_time, - .fault = &elem->fault, - .execution_unit_ref = &elem->execution_unit_ref }; - load_specific_backup_attributes(b, &opresult_bkp_attrs); - } - } - b = mxmlWalkNext(b, tree, MXML_NO_DESCEND); - } - cwmp_root_cause_changedustate_complete(du_state_change_complete_request); -} - -void load_transfer_complete(mxml_node_t *tree) -{ - struct transfer_complete *ptransfer_complete; - - ptransfer_complete = calloc(1, sizeof(struct transfer_complete)); - - struct backup_attributes bkp_attrs = { .command_key = &ptransfer_complete->command_key, - .start_time = &ptransfer_complete->start_time, - .complete_time = &ptransfer_complete->complete_time, - .old_software_version = &ptransfer_complete->old_software_version, - .fault_code = &ptransfer_complete->fault_code, - .type = &ptransfer_complete->type }; - load_specific_backup_attributes(tree, &bkp_attrs); - - cwmp_root_cause_transfer_complete(ptransfer_complete); - sotfware_version_value_change(ptransfer_complete); -} - -void load_autonomous_du_state_change_complete(mxml_node_t *tree) -{ - auto_du_state_change_compl *p; - - p = calloc(1, sizeof(auto_du_state_change_compl)); - - struct backup_attributes bkp_attrs = { .uuid = &p->uuid, - .version = &p->ver, - .current_state = &p->current_state, - .resolved = &p->resolved, - .start_time = &p->start_time, - .complete_time = &p->complete_time, - .fault_code = &p->fault_code, - .fault_string = &p->fault_string, - .operation = &p->operation }; - load_specific_backup_attributes(tree, &bkp_attrs); - - cwmp_root_cause_autonomous_cdu_complete(p); -} - void bkp_session_create_file() { FILE *pFile; @@ -1145,13 +91,597 @@ int bkp_session_check_file() return 0; } -int cwmp_init_backup_session(char **ret, enum backup_loading load) +int save_acs_bkp_config() { - int error; - if (bkp_session_check_file()) - return 0; - error = cwmp_load_saved_session(ret, load); - return error; + struct config *conf; + + conf = &(cwmp_main->conf); + bkp_session_simple_insert("acs", "URL", conf->acsurl); + bkp_session_save(); + return CWMP_OK; +} + +mxml_node_t *bkp_session_node_found(mxml_node_t *tree, char *name, struct search_keywords *keys, int size) +{ + mxml_node_t *b = tree, *c, *d; + struct search_keywords; + int i = 0; + if (!tree) + return NULL; + b = mxmlFindElement(b, b, name, NULL, NULL, MXML_DESCEND_FIRST); + while (b) { + c = mxmlGetFirstChild(b); + if (c) { + i = 0; + while (c && i < size) { + if (mxmlGetType(c) == MXML_ELEMENT && strcmp(keys[i].name, (char *) mxmlGetElement(c)) == 0) { + d = c; + d = mxmlWalkNext(d, c, MXML_DESCEND); + if ((keys[i].value == NULL) || (d && mxmlGetType(d) == MXML_OPAQUE && CWMP_STRCMP(keys[i].value, mxmlGetOpaque(d)) == 0)) + i++; + } + c = mxmlWalkNext(c, b, MXML_NO_DESCEND); + } + } + if (i == size) + break; + + b = mxmlWalkNext(b, tree, MXML_NO_DESCEND); + } + return b; +} + +mxml_node_t *get_bkp_session_node_by_id(mxml_node_t *tree, char *name, int id) +{ + struct search_keywords keys[1]; + char bkp_id[32]; + + snprintf(bkp_id, sizeof(bkp_id), "%d", id); + keys[0].name = "id"; + keys[0].value = bkp_id; + + return bkp_session_node_found(tree, name, keys, 1); +} + +mxml_node_t *get_bkp_session_node_by_key(mxml_node_t *tree, char *name, char *key_name, char *key_value) +{ + struct search_keywords keys[1]; + + keys[0].name = key_name; + keys[0].value = key_value; + + return bkp_session_node_found(tree, name, keys, 1); +} +/* + * Insert Backup Session + */ +mxml_node_t *bkp_session_insert(mxml_node_t *tree, char *name, char *value) +{ + mxml_node_t *b; + if (tree == NULL || name == NULL) { + CWMP_LOG(ERROR, "backup %s: tree or name is null: %p %p", __FUNCTION__, tree, name); + return NULL; + } + b = mxmlNewElement(tree, name); + if (b == NULL) + return NULL; + + if (value != NULL) + mxmlNewOpaque(b, value); + + return b; +} + +void bkp_session_simple_insert(char *parent, char *child, char *value) +{ + mxml_node_t *b = bkp_tree; + + if (parent == NULL || child == NULL) { + CWMP_LOG(ERROR, "backup %s: parent or child is null %p %p", __FUNCTION__, parent, child); + return; + } + b = mxmlFindElement(b, b, parent, NULL, NULL, MXML_DESCEND); + if (b) + mxmlDelete(b); + b = bkp_session_insert(bkp_tree, parent, NULL); + bkp_session_insert(b, child, value); +} + +void bkp_session_simple_insert_in_parent(char *parent, char *child, char *value) +{ + mxml_node_t *n, *b = bkp_tree; + + if (parent == NULL || child == NULL) { + CWMP_LOG(ERROR, "backup %s: parent or child is null %p %p", __FUNCTION__, parent, child); + return; + } + n = mxmlFindElement(b, b, parent, NULL, NULL, MXML_DESCEND); + if (!n) + n = bkp_session_insert(bkp_tree, parent, NULL); + b = mxmlFindElement(n, n, child, NULL, NULL, MXML_DESCEND); + if (b) + mxmlDelete(b); + bkp_session_insert(n, child, value); +} + +mxml_node_t *bkp_session_insert_event(int index, char *command_key, int id) +{ + char event_idx[32]; + mxml_node_t *b; + + snprintf(event_idx, sizeof(event_idx), "%d", index); + b = get_bkp_session_node_by_id(bkp_tree, "cwmp_event", id); + if (!b) { + struct xml_data_struct bkp_xml_event = {0}; + bkp_xml_event.command_key = command_key ? &command_key : NULL; + bkp_xml_event.id = &id; + bkp_xml_event.index = &index; + int fault = build_xml_node_data(BKP_EVT_BUILD, bkp_tree, &bkp_xml_event); + if (fault != CWMP_OK) + return NULL; + } + b = get_bkp_session_node_by_id(bkp_tree, "cwmp_event", id); + return b; +} + +void bkp_session_insert_schedule_inform(int id, time_t time, char *command_key) +{ + mxml_node_t *b; + + b = get_bkp_session_node_by_id(bkp_tree, "schedule_inform", id); + if (!b) { + struct xml_data_struct bkp_xml_sched_inform = {0}; + bkp_xml_sched_inform.command_key = command_key ? &command_key : NULL; + bkp_xml_sched_inform.time = (int *)&time; + bkp_xml_sched_inform.id = &id; + build_xml_node_data(BKP_SCHEDULE_INFORM_BUILD, bkp_tree, &bkp_xml_sched_inform); + } +} + +void bkp_session_insert_download(struct download *pdownload) +{ + mxml_node_t *b; + + b = get_bkp_session_node_by_id(bkp_tree, "download", pdownload->id); + if (!b) { + struct xml_data_struct bkp_xml_download = {0}; + bkp_xml_download.url = pdownload->url ? &pdownload->url : NULL; + bkp_xml_download.command_key = pdownload->command_key ? &pdownload->command_key : NULL; + bkp_xml_download.file_type = pdownload->file_type ? &pdownload->file_type : NULL; + bkp_xml_download.username = pdownload->username ? &pdownload->username : NULL; + bkp_xml_download.password = pdownload->password ? &pdownload->password : NULL; + bkp_xml_download.file_size = &pdownload->file_size; + bkp_xml_download.time = (int *)&pdownload->scheduled_time; + bkp_xml_download.id = &pdownload->id; + build_xml_node_data(BKP_DOWNLOAD_BUILD, bkp_tree, &bkp_xml_download); + } +} + +void bkp_session_insert_schedule_download(struct download *pschedule_download) +{ + mxml_node_t *b; + + b = get_bkp_session_node_by_id(bkp_tree, "schedule_download", pschedule_download->id); + if (!b) { + struct xml_data_struct bkp_xml_sched_download = {0}; + bkp_xml_sched_download.url = pschedule_download->url ? &pschedule_download->url : NULL; + bkp_xml_sched_download.command_key = pschedule_download->command_key ? &pschedule_download->command_key : NULL; + bkp_xml_sched_download.file_type = pschedule_download->file_type ? &pschedule_download->file_type : NULL; + bkp_xml_sched_download.username = pschedule_download->username ? &pschedule_download->username : NULL; + bkp_xml_sched_download.password = pschedule_download->password ? &pschedule_download->password : NULL; + bkp_xml_sched_download.file_size = &pschedule_download->file_size; + + bkp_xml_sched_download.window_start1 = &pschedule_download->timewindowstruct[0].windowstart; + bkp_xml_sched_download.window_start2 = &pschedule_download->timewindowstruct[1].windowstart; + bkp_xml_sched_download.window_end1 = &pschedule_download->timewindowstruct[0].windowend; + bkp_xml_sched_download.window_end2 = &pschedule_download->timewindowstruct[1].windowend; + bkp_xml_sched_download.user_message1 = &pschedule_download->timewindowstruct[0].usermessage; + bkp_xml_sched_download.user_message2 = &pschedule_download->timewindowstruct[1].usermessage; + bkp_xml_sched_download.window_mode1 = &pschedule_download->timewindowstruct[0].windowmode; + bkp_xml_sched_download.window_mode2 = &pschedule_download->timewindowstruct[1].windowmode; + bkp_xml_sched_download.max_retries1 = &pschedule_download->timewindowstruct[0].maxretries; + bkp_xml_sched_download.max_retries2 = &pschedule_download->timewindowstruct[1].maxretries; + bkp_xml_sched_download.id = &pschedule_download->id; + build_xml_node_data(BKP_SCHED_DOWNLOAD_BUILD, bkp_tree, &bkp_xml_sched_download); + } +} + +void bkp_session_insert_change_du_state(struct change_du_state *pchange_du_state) +{ + LIST_HEAD(cdu_operations_xml_list); + struct xml_data_struct bkp_xml_cdu = {0}; + bkp_xml_cdu.time = (int *)&pchange_du_state->timeout; + bkp_xml_cdu.command_key = &pchange_du_state->command_key; + bkp_xml_cdu.id = &pchange_du_state->id; + cdu_operations_list_to_xml_data_list(&(pchange_du_state->list_operation), &cdu_operations_xml_list); + bkp_xml_cdu.data_list = &cdu_operations_xml_list; + build_xml_node_data(BKP_CDU_BUILD, bkp_tree, &bkp_xml_cdu); +} + +void bkp_session_insert_upload(struct upload *pupload) +{ + mxml_node_t *b; + + b = get_bkp_session_node_by_id(bkp_tree, "upload", pupload->id); + if (!b) { + struct xml_data_struct bkp_xml_upload = {0}; + bkp_xml_upload.command_key = &pupload->command_key; + bkp_xml_upload.file_type = &pupload->file_type; + bkp_xml_upload.username = &pupload->username; + bkp_xml_upload.password = &pupload->password; + bkp_xml_upload.time = (int *)&pupload->scheduled_time; + bkp_xml_upload.url = &pupload->url; + bkp_xml_upload.id = &pupload->id; + build_xml_node_data(BKP_UPLOAD_BUILD, bkp_tree, &bkp_xml_upload); + } +} + +void bkp_session_insert_autonomous_du_state_change(auto_du_state_change_compl *data) +{ + mxml_node_t *b; + + if (data == NULL) + return; + + b = get_bkp_session_node_by_id(bkp_tree, "autonomous_du_state_change_complete", data->id); + if (!b) { + struct xml_data_struct bkp_xml_auto_cdu = {0}; + bkp_xml_auto_cdu.uuid = &data->uuid; + bkp_xml_auto_cdu.version = data->ver ? &data->ver : NULL; + bkp_xml_auto_cdu.current_state = data->current_state ? &data->current_state : NULL; + bkp_xml_auto_cdu.resolved = &data->resolved; + bkp_xml_auto_cdu.start_time = data->start_time ? &data->start_time : NULL; + bkp_xml_auto_cdu.complete_time = data->complete_time ? &data->complete_time : NULL; + bkp_xml_auto_cdu.fault_code = &data->fault_code; + bkp_xml_auto_cdu.fault_string = data->fault_string ? &data->fault_string : NULL; + bkp_xml_auto_cdu.operation = data->operation ? &data->operation : NULL; + bkp_xml_auto_cdu.id = &data->id; + build_xml_node_data(BKP_AUTO_CDU_BUILD, bkp_tree, &bkp_xml_auto_cdu); + } +} + +void bkp_session_insert_autonomous_transfer_complete(auto_transfer_complete *data) +{ + mxml_node_t *b; + + if (data == NULL) + return; + + b = get_bkp_session_node_by_id(bkp_tree, "autonomous_transfer_complete", data->id); + if (!b) { + struct xml_data_struct bkp_xml_auto_tc = {0}; + bkp_xml_auto_tc.announce_url = data->announce_url ? &data->announce_url : NULL; + bkp_xml_auto_tc.transfer_url = data->transfer_url ? &data->transfer_url : NULL; + bkp_xml_auto_tc.is_download = &data->is_download; + bkp_xml_auto_tc.file_type = data->file_type ? &data->file_type : NULL; + bkp_xml_auto_tc.file_size = &data->file_size; + bkp_xml_auto_tc.start_time = data->start_time ? &data->start_time : NULL; + bkp_xml_auto_tc.complete_time = data->complete_time ? &data->complete_time : NULL; + bkp_xml_auto_tc.fault_code = &data->fault_code; + bkp_xml_auto_tc.fault_string = data->fault_string ? &data->fault_string : NULL; + bkp_xml_auto_tc.id = &data->id; + build_xml_node_data(BKP_AUTO_TRANSFER_COMPLETE_BUILD, bkp_tree, &bkp_xml_auto_tc); + } +} + +void bkp_session_insert_du_state_change_complete(struct du_state_change_complete *pdu_state_change_complete) +{ + struct xml_data_struct bkp_xml_auto_cdu_complete = {0}; + LIST_HEAD(opresult_xml_data_list); + cdu_operations_result_list_to_xml_data_list(&(pdu_state_change_complete->list_opresult), &opresult_xml_data_list); + bkp_xml_auto_cdu_complete.command_key = &pdu_state_change_complete->command_key; + bkp_xml_auto_cdu_complete.time = (int *)&pdu_state_change_complete->timeout; + bkp_xml_auto_cdu_complete.data_list = &opresult_xml_data_list; + bkp_xml_auto_cdu_complete.id = &pdu_state_change_complete->id; + build_xml_node_data(BKP_CDU_COMPLETE_BUILD, bkp_tree, &bkp_xml_auto_cdu_complete); +} + +void bkp_session_insert_transfer_complete(struct transfer_complete *ptransfer_complete) +{ + mxml_node_t *b; + + b = get_bkp_session_node_by_key(bkp_tree, "transfer_complete", "start_time", ptransfer_complete->start_time); + if (!b) { + struct xml_data_struct bkp_xml_tc = {0}; + + bkp_xml_tc.command_key = ptransfer_complete->command_key ? &ptransfer_complete->command_key : NULL; + bkp_xml_tc.start_time = ptransfer_complete->start_time ? &ptransfer_complete->start_time : NULL; + bkp_xml_tc.complete_time = ptransfer_complete->complete_time ? &ptransfer_complete->complete_time : NULL; + bkp_xml_tc.old_software_version = ptransfer_complete->old_software_version ? &ptransfer_complete->old_software_version : NULL; + bkp_xml_tc.fault_code = &ptransfer_complete->fault_code; + bkp_xml_tc.type = &ptransfer_complete->type; + build_xml_node_data(BKP_TRANSFER_COMPLETE_BUILD, bkp_tree, &bkp_xml_tc); + } +} + +/* + * Load backup session + */ +char *load_child_value(mxml_node_t *tree, char *sub_name) +{ + char *value = NULL; + mxml_node_t *b = tree; + + if (b) { + b = mxmlFindElement(b, b, sub_name, NULL, NULL, MXML_DESCEND); + if (b) { + b = mxmlWalkNext(b, tree, MXML_DESCEND); + if (b && mxmlGetType(b) == MXML_OPAQUE) { + const char *opaque = mxmlGetOpaque(b); + if (opaque != NULL) { + value = strdup(opaque); + } + } + } + } + return value; +} + +void load_queue_event(mxml_node_t *tree) +{ + int idx = -1, id = -1; + + struct xml_data_struct bkp_xml_evt = {0}; + bkp_xml_evt.index = &idx; + bkp_xml_evt.id = &id; + load_xml_node_data(BKP_EVT_LOAD, tree, &bkp_xml_evt); +} + +void load_schedule_inform(mxml_node_t *tree) +{ + char *command_key = NULL; + time_t scheduled_time = 0; + struct schedule_inform *schedule_inform = NULL; + struct list_head *ilist = NULL; + + struct xml_data_struct bkp_xml_schedule_inform = {0}; + bkp_xml_schedule_inform.command_key = &command_key; + bkp_xml_schedule_inform.time = (int *)&scheduled_time; + load_xml_node_data(BKP_SCHEDULE_INFORM, tree, &bkp_xml_schedule_inform); + + list_for_each (ilist, &(list_schedule_inform)) { + schedule_inform = list_entry(ilist, struct schedule_inform, list); + if (schedule_inform->scheduled_time > scheduled_time) { + break; + } + } + schedule_inform = calloc(1, sizeof(struct schedule_inform)); + if (schedule_inform != NULL) { + schedule_inform->commandKey = command_key; + schedule_inform->scheduled_time = scheduled_time; + list_add(&(schedule_inform->list), ilist->prev); + } +} + +void load_download(mxml_node_t *tree) +{ + struct download *download_request = NULL; + struct list_head *ilist = NULL; + struct download *idownload_request = NULL; + + if (tree == NULL) { + CWMP_LOG(ERROR, "backup %s: tree is null", __FUNCTION__); + return; + } + download_request = calloc(1, sizeof(struct download)); + if (download_request == NULL) { + CWMP_LOG(ERROR, "backup %s: download_request is null", __FUNCTION__); + return; + } + + struct xml_data_struct bkp_xml_download = {0}; + bkp_xml_download.command_key = &download_request->command_key; + bkp_xml_download.url = &download_request->url; + bkp_xml_download.file_type = &download_request->file_type; + bkp_xml_download.username = &download_request->username; + bkp_xml_download.password = &download_request->password; + bkp_xml_download.file_size = &download_request->file_size; + bkp_xml_download.time = (int *)&download_request->scheduled_time; + bkp_xml_download.id = &download_request->id; + load_xml_node_data(BKP_DOWNLOAD, tree, &bkp_xml_download); + + download_request->handler_timer.cb = cwmp_start_download; + + list_for_each (ilist, &(list_download)) { + idownload_request = list_entry(ilist, struct download, list); + if (idownload_request->scheduled_time > download_request->scheduled_time) { + break; + } + } + list_add(&(download_request->list), ilist->prev); + if (download_request->scheduled_time != 0) + count_download_queue++; + cwmp_set_end_session(END_SESSION_DOWNLOAD); +} + +void load_schedule_download(mxml_node_t *tree) +{ + struct download *download_request = NULL; + struct list_head *ilist = NULL; + struct download *idownload_request = NULL; + + if (tree == NULL) { + CWMP_LOG(ERROR, "backup %s: tree is null", __FUNCTION__); + return; + } + download_request = calloc(1, sizeof(struct download)); + if (download_request == NULL) { + CWMP_LOG(ERROR, "backup %s: download_request is null", __FUNCTION__); + return; + } + + struct xml_data_struct bkp_xml_sched_download = {0}; + bkp_xml_sched_download.command_key = &download_request->command_key; + bkp_xml_sched_download.url = &download_request->url; + bkp_xml_sched_download.file_type = &download_request->file_type; + bkp_xml_sched_download.username = &download_request->username; + bkp_xml_sched_download.password = &download_request->password; + bkp_xml_sched_download.file_size = &download_request->file_size; + + bkp_xml_sched_download.window_start1 = &download_request->timewindowstruct[0].windowstart; + bkp_xml_sched_download.window_start2 = &download_request->timewindowstruct[1].windowstart; + bkp_xml_sched_download.window_end1 = &download_request->timewindowstruct[0].windowend; + bkp_xml_sched_download.window_end2 = &download_request->timewindowstruct[1].windowend; + bkp_xml_sched_download.window_mode1 = &download_request->timewindowstruct[0].windowmode; + bkp_xml_sched_download.window_mode2 = &download_request->timewindowstruct[1].windowmode; + bkp_xml_sched_download.user_message1 = &download_request->timewindowstruct[0].usermessage; + bkp_xml_sched_download.user_message2 = &download_request->timewindowstruct[1].usermessage; + bkp_xml_sched_download.max_retries1 = &download_request->timewindowstruct[0].maxretries; + bkp_xml_sched_download.max_retries2 = &download_request->timewindowstruct[1].maxretries; + bkp_xml_sched_download.id = &download_request->id; + load_xml_node_data(BKP_SCHED_DOWNLOAD, tree, &bkp_xml_sched_download); + + list_for_each (ilist, &(list_schedule_download)) { + idownload_request = list_entry(ilist, struct download, list); + if (idownload_request->timewindowstruct[0].windowstart > download_request->timewindowstruct[0].windowstart) { + break; + } + } + list_add(&(download_request->list), ilist->prev); + if (download_request->timewindowstruct[0].windowstart != 0) + count_download_queue++; + cwmp_set_end_session(END_SESSION_SCHEDULE_DOWNLOAD); +} + +void load_upload(mxml_node_t *tree) +{ + struct upload *upload_request = NULL; + struct list_head *ilist = NULL; + struct upload *iupload_request = NULL; + + if (tree == NULL) { + CWMP_LOG(ERROR, "backup %s: tree is null", __FUNCTION__); + return; + } + upload_request = calloc(1, sizeof(struct upload)); + if (upload_request == NULL) { + CWMP_LOG(ERROR, "backup %s: download_request is null", __FUNCTION__); + return; + } + + struct xml_data_struct bkp_xml_upload = {0}; + bkp_xml_upload.url = &upload_request->url; + bkp_xml_upload.command_key = &upload_request->command_key; + bkp_xml_upload.file_type = &upload_request->file_type; + bkp_xml_upload.username = &upload_request->username; + bkp_xml_upload.password = &upload_request->password; + bkp_xml_upload.time = (int *)&upload_request->scheduled_time; + bkp_xml_upload.id = &upload_request->id; + load_xml_node_data(BKP_UPLOAD, tree, &bkp_xml_upload); + + list_for_each (ilist, &(list_upload)) { + iupload_request = list_entry(ilist, struct upload, list); + if (iupload_request->scheduled_time > upload_request->scheduled_time) { + break; + } + } + list_add(&(upload_request->list), ilist->prev); + if (upload_request->scheduled_time != 0) + count_upload_queue++; + cwmp_set_end_session(END_SESSION_UPLOAD); +} + +void load_change_du_state(mxml_node_t *tree) +{ + if (tree == NULL) { + CWMP_LOG(ERROR, "backup %s: tree is null", __FUNCTION__); + return; + } + + struct change_du_state *change_du_state_request = NULL; + change_du_state_request = calloc(1, sizeof(struct change_du_state)); + INIT_LIST_HEAD(&(change_du_state_request->list_operation)); + + struct xml_data_struct bkp_xml_cdu = {0}; + bkp_xml_cdu.command_key = &change_du_state_request->command_key; + bkp_xml_cdu.time = (int *)&change_du_state_request->timeout; + bkp_xml_cdu.cdu = change_du_state_request; + bkp_xml_cdu.id = &change_du_state_request->id; + load_xml_node_data(BKP_CDU, tree, &bkp_xml_cdu); + + list_add_tail(&(change_du_state_request->list_operation), &(list_change_du_state)); + cwmp_set_end_session(END_SESSION_CDU); +} + +void load_du_state_change_complete(mxml_node_t *tree) +{ + struct du_state_change_complete *du_state_change_complete_request = NULL; + + du_state_change_complete_request = calloc(1, sizeof(struct du_state_change_complete)); + INIT_LIST_HEAD(&(du_state_change_complete_request->list_opresult)); + + struct xml_data_struct bkp_xml_cdu_complete = {0}; + bkp_xml_cdu_complete.command_key = &du_state_change_complete_request->command_key; + bkp_xml_cdu_complete.time = (int *)&du_state_change_complete_request->timeout; + bkp_xml_cdu_complete.id = &du_state_change_complete_request->id; + bkp_xml_cdu_complete.cdu_complete = du_state_change_complete_request; + load_xml_node_data(BKP_CDU_COMPLETE, tree, &bkp_xml_cdu_complete); + + cwmp_root_cause_changedustate_complete(du_state_change_complete_request); +} + +void load_transfer_complete(mxml_node_t *tree) +{ + struct transfer_complete *ptransfer_complete; + + ptransfer_complete = calloc(1, sizeof(struct transfer_complete)); + + struct xml_data_struct bkp_xml_transfer_complete = {0}; + bkp_xml_transfer_complete.command_key = &ptransfer_complete->command_key; + bkp_xml_transfer_complete.start_time = &ptransfer_complete->start_time; + bkp_xml_transfer_complete.complete_time = &ptransfer_complete->complete_time; + bkp_xml_transfer_complete.old_software_version = &ptransfer_complete->old_software_version; + bkp_xml_transfer_complete.fault_code = &ptransfer_complete->fault_code; + bkp_xml_transfer_complete.type = &ptransfer_complete->type; + + load_xml_node_data(BKP_TRANSFER_COMPLETE, tree, &bkp_xml_transfer_complete); + + cwmp_root_cause_transfer_complete(ptransfer_complete); + sotfware_version_value_change(ptransfer_complete); +} + +void load_autonomous_du_state_change_complete(mxml_node_t *tree) +{ + auto_du_state_change_compl *p; + + p = calloc(1, sizeof(auto_du_state_change_compl)); + + struct xml_data_struct bkp_xml_auto_change_complete = {0}; + bkp_xml_auto_change_complete.uuid = &p->uuid; + bkp_xml_auto_change_complete.version = &p->ver; + bkp_xml_auto_change_complete.current_state = &p->current_state; + bkp_xml_auto_change_complete.resolved = &p->resolved; + bkp_xml_auto_change_complete.start_time = &p->start_time; + bkp_xml_auto_change_complete.complete_time = &p->complete_time; + bkp_xml_auto_change_complete.fault_code = &p->fault_code; + bkp_xml_auto_change_complete.fault_string = &p->fault_string; + bkp_xml_auto_change_complete.operation = &p->operation; + bkp_xml_auto_change_complete.id = &p->id; + load_xml_node_data(BKP_AUTO_CDU, tree, &bkp_xml_auto_change_complete); + + cwmp_root_cause_autonomous_cdu_complete(p); +} + +void load_autonomous_transfer_complete(mxml_node_t *tree) +{ + auto_transfer_complete *p; + + p = calloc(1, sizeof(auto_transfer_complete)); + + struct xml_data_struct bkp_xml_auto_transfer_complete = {0}; + bkp_xml_auto_transfer_complete.announce_url = &p->announce_url; + bkp_xml_auto_transfer_complete.transfer_url = &p->transfer_url; + bkp_xml_auto_transfer_complete.is_download = &p->is_download; + bkp_xml_auto_transfer_complete.file_type = &p->file_type; + bkp_xml_auto_transfer_complete.file_size = &p->file_size; + bkp_xml_auto_transfer_complete.start_time = &p->start_time; + bkp_xml_auto_transfer_complete.complete_time = &p->complete_time; + bkp_xml_auto_transfer_complete.fault_code = &p->fault_code; + bkp_xml_auto_transfer_complete.fault_string = &p->fault_string; + bkp_xml_auto_transfer_complete.id = &p->id; + load_xml_node_data(BKP_AUTO_TRANSFER_COMPLETE, tree, &bkp_xml_auto_transfer_complete); + + cwmp_root_cause_autonomous_transfer_complete(p); } int cwmp_load_saved_session(char **ret, enum backup_loading load) @@ -1165,7 +695,7 @@ int cwmp_load_saved_session(char **ret, enum backup_loading load) const char *elem_name = mxmlGetElement(b); if (load == ACS) { if (ntype == MXML_ELEMENT && strcmp(elem_name, "acs") == 0) { - *ret = load_child_value(b, "url"); + *ret = load_child_value(b, "URL"); break; } } @@ -1206,6 +736,8 @@ int cwmp_load_saved_session(char **ret, enum backup_loading load) load_schedule_download(b); } else if (ntype == MXML_ELEMENT && strcmp(elem_name, "autonomous_du_state_change_complete") == 0) { load_autonomous_du_state_change_complete(b); + } else if (ntype == MXML_ELEMENT && strcmp(elem_name, "autonomous_transfer_complete") == 0) { + load_autonomous_transfer_complete(b); } } b = mxmlWalkNext(b, bkp_tree, MXML_NO_DESCEND); @@ -1213,3 +745,20 @@ int cwmp_load_saved_session(char **ret, enum backup_loading load) return CWMP_OK; } + +/* + * Delete Backup Session + */ +void bkp_session_delete_element(char *element_name, int id) +{ + mxml_node_t *b = get_bkp_session_node_by_id(bkp_tree, element_name, id); + if (b) + mxmlDelete(b); +} + +void bkp_session_delete_element_by_key(char *element_name, char *key_name, char *key_value) +{ + mxml_node_t *b = get_bkp_session_node_by_key(bkp_tree, element_name, key_name, key_value); + if (b) + mxmlDelete(b); +} diff --git a/src/backupSession.h b/src/backupSession.h index 8a7f515..e8a2af4 100644 --- a/src/backupSession.h +++ b/src/backupSession.h @@ -38,32 +38,25 @@ struct search_keywords { }; int cwmp_init_backup_session(char **ret, enum backup_loading load); +int bkp_session_check_file(); void bkp_session_save(); int cwmp_load_saved_session(char **acsurl, enum backup_loading load); -mxml_node_t *bkp_session_insert_event(int index, char *command_key, int id); -void bkp_session_delete_event(int id); -void bkp_session_simple_insert_in_parent(char *parent, char *child, char *value); -void bkp_session_insert_parameter(mxml_node_t *b, char *name); -void bkp_session_simple_insert(char *parent, char *child, char *value); -void bkp_session_insert_schedule_inform(time_t schedule_time, char *command_key); -void bkp_session_delete_schedule_inform(time_t schedule_time, char *command_key); -void bkp_session_insert_download(struct download *pdownload); -void bkp_session_delete_download(struct download *pdownload); -void bkp_session_insert_upload(struct upload *pupload); -void bkp_session_delete_upload(struct upload *pupload); -void bkp_session_insert_change_du_state(struct change_du_state *pchange_du_state); -void bkp_session_delete_change_du_state(struct change_du_state *pchange_du_state); -void bkp_session_insert_transfer_complete(struct transfer_complete *ptransfer_complete); -void bkp_session_delete_transfer_complete(struct transfer_complete *ptransfer_complete); int save_acs_bkp_config(); +mxml_node_t *bkp_session_insert(mxml_node_t *tree, char *name, char *value); +void bkp_session_simple_insert_in_parent(char *parent, char *child, char *value); +void bkp_session_simple_insert(char *parent, char *child, char *value); +mxml_node_t *bkp_session_insert_event(int index, char *command_key, int id); +void bkp_session_insert_schedule_inform(int id, time_t schedule_time, char *command_key); +void bkp_session_insert_download(struct download *pdownload); +void bkp_session_insert_upload(struct upload *pupload); +void bkp_session_insert_change_du_state(struct change_du_state *pchange_du_state); +void bkp_session_insert_transfer_complete(struct transfer_complete *ptransfer_complete); void bkp_session_insert_schedule_download(struct download *pschedule_download); -void bkp_session_delete_du_state_change_complete(struct du_state_change_complete *pdu_state_change_complete); -void bkp_session_delete_schedule_download(struct download *pschedule_download); void bkp_session_insert_du_state_change_complete(struct du_state_change_complete *pdu_state_change_complete); void bkp_session_insert_autonomous_du_state_change(auto_du_state_change_compl *data); -void bkp_session_delete_autonomous_du_state_change(auto_du_state_change_compl *data); void bkp_session_insert_autonomous_transfer_complete(auto_transfer_complete *data); -void bkp_session_delete_autonomous_transfer_complete(auto_transfer_complete *data); +void bkp_session_delete_element(char *element_name, int id); +void bkp_session_delete_element_by_key(char *element_name, char *key_name, char *key_value); void bkp_tree_clean(void); #endif /* _BACKUPSESSION_H__ */ diff --git a/src/common.h b/src/common.h index 6848d01..6283326 100644 --- a/src/common.h +++ b/src/common.h @@ -167,6 +167,15 @@ typedef struct cwmp { time_t prev_heartbeat_time; unsigned int cwmp_id; int event_id; + int sched_inform_id; + int download_id; + int sched_download_id; + int cdu_id; + int upload_id; + int auto_cdu_id; + int auto_tc_id; + int cdu_complete_id; + int tc_id; int cr_socket_desc; int cwmp_period; long int heart_session_interval; @@ -408,6 +417,7 @@ typedef struct schedule_inform { struct uloop_timeout handler_timer ; time_t scheduled_time; char *commandKey; + int id; } schedule_inform; typedef struct timewindow { @@ -429,6 +439,7 @@ typedef struct download { char *username; char *password; struct timewindow timewindowstruct[2]; + int id; } download; typedef struct timeinterval { @@ -443,6 +454,7 @@ typedef struct change_du_state { time_t timeout; char *command_key; struct list_head list_operation; + int id; } change_du_state; typedef struct du_operational_uuid { @@ -472,6 +484,7 @@ typedef struct upload { char *username; char *password; int f_instance; + int id; } upload; typedef struct transfer_complete { @@ -480,7 +493,9 @@ typedef struct transfer_complete { char *start_time; char *complete_time; char *old_software_version; + char *file_type; int type; + int id; } transfer_complete; typedef struct autonomous_du_state_change_complete { @@ -493,6 +508,7 @@ typedef struct autonomous_du_state_change_complete { int fault_code; char *fault_string; char *operation; + int id; } auto_du_state_change_compl; typedef struct autonomous_transfer_complete { @@ -506,6 +522,7 @@ typedef struct autonomous_transfer_complete { int fault_code; bool is_download; int file_size; + int id; } auto_transfer_complete; @@ -513,6 +530,7 @@ typedef struct du_state_change_complete { char *command_key; time_t timeout; struct list_head list_opresult; + int id; } du_state_change_complete; typedef struct opresult { diff --git a/src/cwmp.c b/src/cwmp.c index 1cb18ce..83067e2 100644 --- a/src/cwmp.c +++ b/src/cwmp.c @@ -258,6 +258,15 @@ static int cwmp_init() INIT_LIST_HEAD(&du_uuid_list); cwmp_main->start_time = time(NULL); cwmp_main->event_id = 0; + cwmp_main->sched_inform_id = 0; + cwmp_main->download_id = 0; + cwmp_main->sched_download_id = 0; + cwmp_main->cdu_id = 0; + cwmp_main->upload_id = 0; + cwmp_main->auto_cdu_id = 0; + cwmp_main->auto_tc_id = 0; + cwmp_main->cdu_complete_id = 0; + cwmp_main->tc_id = 0; cwmp_main->cwmp_period = 0; cwmp_main->cwmp_periodic_time = 0; cwmp_main->cwmp_periodic_enable = false; diff --git a/src/cwmp_du_state.c b/src/cwmp_du_state.c index 7b0691d..6c1bd56 100644 --- a/src/cwmp_du_state.c +++ b/src/cwmp_du_state.c @@ -419,6 +419,11 @@ int change_du_state_fault(struct change_du_state *pchange_du_state, struct du_st res->complete_time = strdup(res->start_time); res->fault = error; } + if ((cwmp_main->cdu_complete_id < 0) || (cwmp_main->cdu_complete_id >= MAX_INT_ID)) { + cwmp_main->cdu_complete_id = 0; + } + cwmp_main->cdu_complete_id++; + (*pdu_state_change_complete)->id = cwmp_main->cdu_complete_id; bkp_session_insert_du_state_change_complete(*pdu_state_change_complete); bkp_session_save(); //cwmp_root_cause_changedustate_complete(*pdu_state_change_complete); @@ -595,8 +600,13 @@ void change_du_state_execute(struct uloop_timeout *utimeout) break; } } - bkp_session_delete_change_du_state(pchange_du_state); + bkp_session_delete_element("change_du_state", pchange_du_state->id); bkp_session_save(); + if ((cwmp_main->cdu_complete_id < 0) || (cwmp_main->cdu_complete_id >= MAX_INT_ID)) { + cwmp_main->cdu_complete_id = 0; + } + cwmp_main->cdu_complete_id++; + pdu_state_change_complete->id = cwmp_main->cdu_complete_id; bkp_session_insert_du_state_change_complete(pdu_state_change_complete); bkp_session_save(); //cwmp_root_cause_changedustate_complete(pdu_state_change_complete); @@ -616,7 +626,7 @@ int cwmp_rpc_acs_destroy_data_du_state_change_complete(struct rpc *rpc) if (rpc->extra_data != NULL) { struct du_state_change_complete *p; p = (struct du_state_change_complete *)rpc->extra_data; - bkp_session_delete_du_state_change_complete(p); + bkp_session_delete_element("du_state_change_complete", p->id); bkp_session_save(); FREE(p->command_key); } diff --git a/src/cwmp_event.c b/src/cwmp_event.c index 7ed4116..c522025 100644 --- a/src/cwmp_event.c +++ b/src/cwmp_event.c @@ -72,7 +72,7 @@ int cwmp_remove_all_session_events() events_ptr = events_ptr->next; continue; } - bkp_session_delete_event(event_container->id); + bkp_session_delete_element("cwmp_event", event_container->id); free(event_container->command_key); cwmp_free_all_dm_parameter_list(&(event_container->head_dm_parameter)); list_del(&(event_container->list)); @@ -91,7 +91,7 @@ int remove_single_event(int event_code) if (event_container->next_session) continue; if (event_container->code == event_code) { - bkp_session_delete_event(event_container->id); + bkp_session_delete_element("cwmp_event",event_container->id); if (event_container->command_key) free(event_container->command_key); cwmp_free_all_dm_parameter_list(&(event_container->head_dm_parameter)); diff --git a/src/download.c b/src/download.c index ed62a05..bb3017f 100644 --- a/src/download.c +++ b/src/download.c @@ -374,7 +374,7 @@ int cwmp_launch_download(struct download *pdownload, char *download_file_name, e download_startTime = get_time(time(NULL)); - ltype == TYPE_DOWNLOAD ? bkp_session_delete_download(pdownload) : bkp_session_delete_schedule_download(pdownload); + bkp_session_delete_element((ltype == TYPE_DOWNLOAD) ? "download" : "schedule_download", pdownload->id); bkp_session_save(); if (flashsize < pdownload->file_size) { @@ -449,6 +449,7 @@ end_download: p->start_time = strdup(download_startTime); p->complete_time = strdup(get_time(time(NULL))); p->type = ltype; + p->file_type = strdup(pdownload->file_type); if (error != FAULT_CPE_NO_FAULT) { p->fault_code = error; } @@ -475,6 +476,13 @@ int apply_downloaded_file(struct download *pdownload, char *download_file_name, if (pdownload->file_type[0] == '1') { ptransfer_complete->old_software_version = cwmp_main->deviceid.softwareversion; } + if (ptransfer_complete->id <= 0) { + if ((cwmp_main->tc_id < 0) || (cwmp_main->tc_id >= MAX_INT_ID)) { + cwmp_main->tc_id = 0; + } + cwmp_main->tc_id++; + ptransfer_complete->id = cwmp_main->tc_id; + } bkp_session_insert_transfer_complete(ptransfer_complete); bkp_session_save(); if (strcmp(pdownload->file_type, FIRMWARE_UPGRADE_IMAGE_FILE_TYPE) == 0) { @@ -534,14 +542,21 @@ int apply_downloaded_file(struct download *pdownload, char *download_file_name, if (pdownload->file_type[0] == '3') { CWMP_LOG(INFO, "Download and apply new vendor config file is done successfully"); //cwmp_root_cause_transfer_complete(ptransfer_complete); - bkp_session_delete_transfer_complete(ptransfer_complete); + bkp_session_delete_element_by_key("transfer_complete", "start_time", ptransfer_complete->start_time); } return FAULT_CPE_NO_FAULT; } if (error != FAULT_CPE_NO_FAULT) { - bkp_session_delete_transfer_complete(ptransfer_complete); + bkp_session_delete_element_by_key("transfer_complete", "start_time", ptransfer_complete->start_time); ptransfer_complete->fault_code = error; } + if (ptransfer_complete->id <= 0) { + if ((cwmp_main->tc_id < 0) || (cwmp_main->tc_id >= MAX_INT_ID)) { + cwmp_main->tc_id = 0; + } + cwmp_main->tc_id++; + ptransfer_complete->id = cwmp_main->tc_id; + } bkp_session_insert_transfer_complete(ptransfer_complete); bkp_session_save(); //cwmp_root_cause_transfer_complete(ptransfer_complete); @@ -558,6 +573,13 @@ struct transfer_complete *set_download_error_transfer_complete(struct download * ptransfer_complete->complete_time = strdup(ptransfer_complete->start_time ? ptransfer_complete->start_time : ""); ptransfer_complete->fault_code = ltype == TYPE_DOWNLOAD ? FAULT_CPE_DOWNLOAD_FAILURE : FAULT_CPE_DOWNLOAD_FAIL_WITHIN_TIME_WINDOW; ptransfer_complete->type = ltype; + if (ptransfer_complete->id <= 0) { + if ((cwmp_main->tc_id < 0) || (cwmp_main->tc_id >= MAX_INT_ID)) { + cwmp_main->tc_id = 0; + } + cwmp_main->tc_id++; + ptransfer_complete->id = cwmp_main->tc_id; + } bkp_session_insert_transfer_complete(ptransfer_complete); cwmp_root_cause_transfer_complete(ptransfer_complete); } @@ -623,7 +645,7 @@ int cwmp_scheduledDownload_remove_all() struct download *download; download = list_entry(list_download.next, struct download, list); list_del(&(download->list)); - bkp_session_delete_download(download); + bkp_session_delete_element("schedule_download", download->id); if (download->scheduled_time != 0) count_download_queue--; cwmp_free_download_request(download); @@ -638,7 +660,7 @@ int cwmp_scheduled_Download_remove_all() struct download *schedule_download; schedule_download = list_entry(list_schedule_download.next, struct download, list); list_del(&(schedule_download->list)); - bkp_session_delete_schedule_download(schedule_download); + bkp_session_delete_element("schedule_download", schedule_download->id); if (schedule_download->timewindowstruct[0].windowstart != 0) count_download_queue--; cwmp_free_schedule_download_request(schedule_download); @@ -651,12 +673,14 @@ int cwmp_rpc_acs_destroy_data_transfer_complete(struct rpc *rpc) { if (rpc && rpc->extra_data != NULL) { struct transfer_complete *p = (struct transfer_complete *)rpc->extra_data; - bkp_session_delete_transfer_complete(p); + bkp_session_delete_element_by_key("transfer_complete", "start_time", p->start_time); + bkp_session_save(); FREE(p->command_key); FREE(p->start_time); FREE(p->complete_time); FREE(p->old_software_version); + FREE(p->file_type); } if (rpc) FREE(rpc->extra_data); @@ -676,24 +700,38 @@ void cwmp_start_download(struct uloop_timeout *timeout) sleep(3); if (error != FAULT_CPE_NO_FAULT) { CWMP_LOG(ERROR, "Error while downloading the file: %s", pdownload->url); + if (ptransfer_complete->id <= 0) { + if ((cwmp_main->tc_id < 0) || (cwmp_main->tc_id >= MAX_INT_ID)) { + cwmp_main->tc_id = 0; + } + cwmp_main->tc_id++; + ptransfer_complete->id = cwmp_main->tc_id; + } bkp_session_insert_transfer_complete(ptransfer_complete); bkp_session_save(); //cwmp_root_cause_transfer_complete(ptransfer_complete); - bkp_session_delete_transfer_complete(ptransfer_complete); + //bkp_session_delete_transfer_complete(ptransfer_complete); } else { error = apply_downloaded_file(pdownload, download_file_name, ptransfer_complete); if (error != FAULT_CPE_NO_FAULT) { CWMP_LOG(ERROR, "Error while applying the downloaded file: %s", download_file_name); + if (ptransfer_complete->id <= 0) { + if ((cwmp_main->tc_id < 0) || (cwmp_main->tc_id >= MAX_INT_ID)) { + cwmp_main->tc_id = 0; + } + cwmp_main->tc_id++; + ptransfer_complete->id = cwmp_main->tc_id; + } bkp_session_insert_transfer_complete(ptransfer_complete); bkp_session_save(); //cwmp_root_cause_transfer_complete(ptransfer_complete); - bkp_session_delete_transfer_complete(ptransfer_complete); + //bkp_session_delete_transfer_complete(ptransfer_complete); } } if (error == FAULT_CPE_NO_FAULT && pdownload->file_type[0] == '3') { //cwmp_root_cause_transfer_complete(ptransfer_complete); - bkp_session_delete_download(pdownload); - bkp_session_delete_transfer_complete(ptransfer_complete); + bkp_session_delete_element_by_key("transfer_complete", "start_time", ptransfer_complete->start_time); + bkp_session_delete_element("download", pdownload->id); bkp_session_save(); } list_del(&(pdownload->list)); @@ -768,8 +806,8 @@ void cwmp_start_schedule_download(struct uloop_timeout *timeout) } if (error == FAULT_CPE_NO_FAULT && sched_download->file_type[0] == '3') { //cwmp_root_cause_transfer_complete(ptransfer_complete); - bkp_session_delete_download(sched_download); - bkp_session_delete_transfer_complete(ptransfer_complete); + bkp_session_delete_element("schedule_download", sched_download->id); + bkp_session_delete_element_by_key("transfer_complete", "start_time", ptransfer_complete->start_time); bkp_session_save(); } } else { @@ -785,10 +823,17 @@ void cwmp_start_schedule_download(struct uloop_timeout *timeout) ptransfer_complete->complete_time = strdup(get_time(now)); ptransfer_complete->type = TYPE_DOWNLOAD; ptransfer_complete->fault_code = FAULT_CPE_INTERNAL_ERROR; + if (ptransfer_complete->id <= 0) { + if ((cwmp_main->tc_id < 0) || (cwmp_main->tc_id >= MAX_INT_ID)) { + cwmp_main->tc_id = 0; + } + cwmp_main->tc_id++; + ptransfer_complete->id = cwmp_main->tc_id; + } bkp_session_insert_transfer_complete(ptransfer_complete); bkp_session_save(); //cwmp_root_cause_transfer_complete(ptransfer_complete); - bkp_session_delete_transfer_complete(ptransfer_complete); + bkp_session_delete_element_by_key("transfer_complete", "start_time", ptransfer_complete->start_time); } return; @@ -799,10 +844,17 @@ retry: sched_download->timewindowstruct[window_index].maxretries--; return; } else { + if (ptransfer_complete->id <= 0) { + if ((cwmp_main->tc_id < 0) || (cwmp_main->tc_id >= MAX_INT_ID)) { + cwmp_main->tc_id = 0; + } + cwmp_main->tc_id++; + ptransfer_complete->id = cwmp_main->tc_id; + } bkp_session_insert_transfer_complete(ptransfer_complete); bkp_session_save(); //cwmp_root_cause_transfer_complete(ptransfer_complete); - bkp_session_delete_transfer_complete(ptransfer_complete); + bkp_session_delete_element_by_key("transfer_complete", "start_time", ptransfer_complete->start_time); bkp_session_save(); } list_del(&(sched_download->list)); diff --git a/src/event.c b/src/event.c index d98252b..73c8c62 100644 --- a/src/event.c +++ b/src/event.c @@ -45,7 +45,7 @@ const struct EVENT_CONST_STRUCT EVENT_CONST[] = {[EVENT_IDX_0BOOTSTRAP] = { "0 B [EVENT_IDX_M_Upload] = { "M Upload", EVENT_RETRY_AFTER_TRANSMIT_FAIL | EVENT_RETRY_AFTER_REBOOT }, [EVENT_IDX_M_ChangeDUState] = { "M ChangeDUState", EVENT_RETRY_AFTER_TRANSMIT_FAIL | EVENT_RETRY_AFTER_REBOOT } }; -void cwmp_save_event_container(struct event_container *event_container) //to be moved to backupsession +void cwmp_save_event_container(struct event_container *event_container) { if (event_container == NULL) { CWMP_LOG(ERROR, "event %s: event_container is null", __FUNCTION__); @@ -60,7 +60,7 @@ void cwmp_save_event_container(struct event_container *event_container) //to be list_for_each (ilist, &(event_container->head_dm_parameter)) { struct cwmp_dm_parameter *dm_parameter; dm_parameter = list_entry(ilist, struct cwmp_dm_parameter, list); - bkp_session_insert_parameter(b, dm_parameter->name); + bkp_session_insert(b, "Parameter", dm_parameter->name); } bkp_session_save(); } diff --git a/src/rpc.c b/src/rpc.c index 8109e0a..76455d0 100755 --- a/src/rpc.c +++ b/src/rpc.c @@ -1428,7 +1428,7 @@ int cancel_transfer(char *key) list_for_each_safe (ilist, q, &(list_download)) { struct download *pdownload = list_entry(ilist, struct download, list); if (key && pdownload->command_key && strcmp(pdownload->command_key, key) == 0) { - bkp_session_delete_download(pdownload); + bkp_session_delete_element("download", pdownload->id); bkp_session_save(); list_del(&(pdownload->list)); if (pdownload->scheduled_time != 0) @@ -1441,7 +1441,7 @@ int cancel_transfer(char *key) list_for_each_safe (ilist, q, &(list_upload)) { struct upload *pupload = list_entry(ilist, struct upload, list); if (key && pupload->command_key && strcmp(pupload->command_key, key) == 0) { - bkp_session_delete_upload(pupload); + bkp_session_delete_element("upload", pupload->id); bkp_session_save(); list_del(&(pupload->list)); if (pupload->scheduled_time != 0) @@ -1557,8 +1557,13 @@ int cwmp_handle_rpc_cpe_schedule_inform(struct rpc *rpc) schedule_inform->handler_timer.cb = cwmp_start_schedule_inform; schedule_inform->commandKey = CWMP_STRDUP(command_key); schedule_inform->scheduled_time = scheduled_time; + if ((cwmp_main->sched_inform_id < 0) || (cwmp_main->sched_inform_id >= MAX_INT_ID)) { + cwmp_main->sched_inform_id = 0; + } + cwmp_main->sched_inform_id++; + schedule_inform->id = cwmp_main->sched_inform_id; list_add(&(schedule_inform->list), ilist->prev); - bkp_session_insert_schedule_inform(schedule_inform->scheduled_time, schedule_inform->commandKey); + bkp_session_insert_schedule_inform(schedule_inform->id, schedule_inform->scheduled_time, schedule_inform->commandKey); bkp_session_save(); FREE(command_key); @@ -1626,6 +1631,11 @@ int cwmp_handle_rpc_cpe_change_du_state(struct rpc *rpc) change_du_state->handler_timer.cb = change_du_state_execute; list_add_tail(&(change_du_state->list), &(list_change_du_state)); + if ((cwmp_main->cdu_id < 0) || (cwmp_main->cdu_id >= MAX_INT_ID)) { + cwmp_main->cdu_id = 0; + } + cwmp_main->cdu_id++; + change_du_state->id = cwmp_main->cdu_id; bkp_session_insert_change_du_state(change_du_state); bkp_session_save(); cwmp_set_end_session(END_SESSION_CDU); @@ -1733,6 +1743,11 @@ int cwmp_handle_rpc_cpe_download(struct rpc *rpc) download->scheduled_time = scheduled_time; } download->handler_timer.cb = cwmp_start_download; + if ((cwmp_main->download_id < 0) || (cwmp_main->download_id >= MAX_INT_ID)) { + cwmp_main->download_id = 0; + } + cwmp_main->download_id++; + download->id = cwmp_main->download_id; bkp_session_insert_download(download); bkp_session_save(); if (download_delay != 0) { @@ -1859,6 +1874,11 @@ int cwmp_handle_rpc_cpe_schedule_download(struct rpc *rpc) schedule_download->timewindowstruct[i].windowend = time(NULL) + schedule_download_delay[i * 2 + 1]; } schedule_download->handler_timer.cb = cwmp_start_schedule_download; + if ((cwmp_main->sched_download_id < 0) || (cwmp_main->sched_download_id >= MAX_INT_ID)) { + cwmp_main->sched_download_id = 0; + } + cwmp_main->sched_download_id++; + schedule_download->id = cwmp_main->sched_download_id; bkp_session_insert_schedule_download(schedule_download); bkp_session_save(); if (schedule_download_delay[0] != 0) { @@ -1976,6 +1996,11 @@ int cwmp_handle_rpc_cpe_upload(struct rpc *rpc) count_upload_queue++; upload->scheduled_time = scheduled_time; } + if ((cwmp_main->upload_id < 0) || (cwmp_main->upload_id >= MAX_INT_ID)) { + cwmp_main->upload_id = 0; + } + cwmp_main->upload_id++; + upload->id = cwmp_main->upload_id; bkp_session_insert_upload(upload); bkp_session_save(); upload->handler_timer.cb = cwmp_start_upload; diff --git a/src/sched_inform.c b/src/sched_inform.c index 6712c6a..bd376cd 100644 --- a/src/sched_inform.c +++ b/src/sched_inform.c @@ -24,7 +24,7 @@ int remove_schedule_inform(struct schedule_inform *schedule_inform) { if (schedule_inform != NULL) { list_del(&(schedule_inform->list)); - bkp_session_delete_schedule_inform(schedule_inform->scheduled_time, schedule_inform->commandKey ? schedule_inform->commandKey : ""); + bkp_session_delete_element("schedule_inform", schedule_inform->id); FREE(schedule_inform->commandKey); free(schedule_inform); } diff --git a/src/upload.c b/src/upload.c index 6a5e742..2950758 100644 --- a/src/upload.c +++ b/src/upload.c @@ -197,7 +197,7 @@ int cwmp_launch_upload(struct upload *pupload, struct transfer_complete **ptrans char *name = NULL; upload_startTime = get_time(time(NULL)); char file_path[128] = {'\0'}; - bkp_session_delete_upload(pupload); + bkp_session_delete_element("upload", pupload->id); bkp_session_save(); if (pupload->file_type[0] == '1') { @@ -302,7 +302,7 @@ int cwmp_scheduledUpload_remove_all() struct upload *upload; upload = list_entry(list_upload.next, struct upload, list); list_del(&(upload->list)); - bkp_session_delete_upload(upload); + bkp_session_delete_element("upload", upload->id); if (upload->scheduled_time != 0) count_upload_queue--; cwmp_free_upload_request(upload); @@ -324,7 +324,13 @@ void cwmp_start_upload(struct uloop_timeout *timeout) if (error != FAULT_CPE_NO_FAULT) { CWMP_LOG(ERROR, "Error while uploading the file: %s", pupload->url); } - + if (ptransfer_complete->id <= 0) { + if ((cwmp_main->tc_id < 0) || (cwmp_main->tc_id >= MAX_INT_ID)) { + cwmp_main->tc_id = 0; + } + cwmp_main->tc_id++; + ptransfer_complete->id = cwmp_main->tc_id; + } bkp_session_insert_transfer_complete(ptransfer_complete); bkp_session_save(); list_del(&(pupload->list)); diff --git a/src/xml.c b/src/xml.c index ea50875..4f2fbb1 100644 --- a/src/xml.c +++ b/src/xml.c @@ -18,6 +18,7 @@ #include "cwmp_zlib.h" #include "common.h" #include "event.h" +#include "cwmp_event.h" #include "datamodel_interface.h" static const char *soap_env_url = "http://schemas.xmlsoap.org/soap/envelope/"; @@ -104,6 +105,40 @@ struct xml_node_data xml_nodes_data[] = { [SOAP_CDU_OPTS_REF] = {XML_SINGLE, 0, NULL, {{"UUID", XML_STRING, 0, NULL}, {"DeploymentUnitRef", XML_STRING, 0, NULL}, {"Version", XML_STRING, 0, NULL}, {"CurrentState", XML_STRING, 0, NULL}, {"StartTime", XML_STRING, 0, NULL}, {"CompleteTime", XML_STRING, 0, NULL}, {"FaultStruct", XML_REC, SOAP_CWMP_FAULT, NULL}}}, [SOAP_ACDU_OPTS_REF] = {XML_SINGLE, 0, NULL, {{"UUID", XML_STRING, 0, NULL}, {"Version", XML_STRING, 0, NULL}, {"CurrentState", XML_STRING, 0, NULL}, {"Resolved", XML_BOOL, 0, NULL}, {"StartTime", XML_STRING, 0, NULL}, {"CompleteTime", XML_STRING, 0, NULL}, {"FaultStruct", XML_REC, SOAP_CWMP_FAULT, NULL}, {"OperationPerformed", XML_STRING, 0, NULL}}}, + /* + * XML Backup Session + */ + [BKP_EVT_LOAD] = {XML_SINGLE, 0, NULL, {{"id", XML_INTEGER, 0, NULL}, {"index", XML_INTEGER, 0, NULL}, {"CommandKey", XML_FUNC, XML_SWITCH, load_backup_event_command_key}, {"parameter", XML_REC, BKP_EVT_PARAM_REF, NULL}}}, + [BKP_EVT_PARAM_REF] = {XML_LIST, BKP_EVT_SINGLE_PARAM, "parameter", {}}, + [BKP_EVT_SINGLE_PARAM] = {XML_SINGLE, 0, NULL, {{"string", XML_FUNC, 0, load_backup_event_parameter}}}, + [BKP_EVT_BUILD] = {XML_SINGLE, 0, NULL, {{"cwmp_event", XML_REC, BKP_EVT_BUILD_REF, NULL}}}, + [BKP_EVT_BUILD_REF] = {XML_SINGLE, 0, NULL, {{"id", XML_INTEGER, 0, NULL}, {"index", XML_INTEGER, 0, NULL}, {"CommandKey", XML_STRING, XML_SWITCH, NULL}}}, + [BKP_SCHEDULE_INFORM_BUILD] = {XML_SINGLE, 0, NULL, {{"schedule_inform", XML_REC, BKP_SCHEDULE_INFORM, NULL}}}, + [BKP_SCHEDULE_INFORM] = {XML_SINGLE, 0, NULL, {{"id", XML_INTEGER, 0, NULL}, {"CommandKey", XML_STRING, XML_SWITCH, NULL}, {"time", XML_INTEGER, 0, NULL}}}, + [BKP_DOWNLOAD_BUILD] = {XML_SINGLE, 0, NULL, {{"download", XML_REC, BKP_DOWNLOAD, NULL}}}, + [BKP_DOWNLOAD] = {XML_SINGLE, 0, NULL, {{"id", XML_INTEGER, 0, NULL}, {"URL", XML_STRING, XML_SWITCH, NULL}, {"CommandKey", XML_STRING, XML_SWITCH, NULL}, {"FileType", XML_STRING, XML_SWITCH, NULL}, {"Username", XML_STRING, XML_SWITCH, NULL}, {"Password", XML_STRING, XML_SWITCH, NULL}, {"FileSize", XML_INTEGER, XML_SWITCH, NULL}, {"time", XML_INTEGER, 0, NULL}}}, + [BKP_SCHED_DOWNLOAD_BUILD] = {XML_SINGLE, 0, NULL, {{"schedule_download", XML_REC, BKP_SCHED_DOWNLOAD, NULL}}}, + [BKP_SCHED_DOWNLOAD] = {XML_SINGLE, 0, NULL, {{"id", XML_INTEGER, 0, NULL}, {"URL", XML_STRING, XML_SWITCH, NULL}, {"CommandKey", XML_STRING, XML_SWITCH, NULL}, {"FileType", XML_STRING, XML_SWITCH, NULL}, {"Username", XML_STRING, XML_SWITCH, NULL}, {"Password", XML_STRING, XML_SWITCH, NULL}, {"FileSize", XML_INTEGER, XML_SWITCH, NULL}, {"windowstart1", XML_INTEGER, 0, NULL}, {"windowstart2", XML_INTEGER, 0, NULL}, {"windowend1", XML_INTEGER, 0, NULL}, {"windowend2", XML_INTEGER, 0, NULL}, {"windowmode1", XML_STRING, 0, NULL}, {"windowmode2", XML_STRING, 0, NULL}, {"usermessage1", XML_STRING, 0, NULL}, {"usermessage2", XML_STRING, 0, NULL}, {"maxretrie1", XML_INTEGER, 0, NULL}, {"maxretrie2", XML_INTEGER, 0, NULL}}}, + [BKP_UPLOAD_BUILD] = {XML_SINGLE, 0, NULL, {{"upload", XML_REC, BKP_UPLOAD, NULL}}}, + [BKP_UPLOAD] = {XML_SINGLE, 0, NULL, {{"id", XML_INTEGER, 0, NULL}, {"URL", XML_STRING, XML_SWITCH, NULL}, {"CommandKey", XML_STRING, XML_SWITCH, NULL}, {"FileType", XML_STRING, XML_SWITCH, NULL}, {"Username", XML_STRING, XML_SWITCH, NULL}, {"Password", XML_STRING, XML_SWITCH, NULL}, {"time", XML_INTEGER, 0, NULL}}}, + [BKP_CDU_BUILD] = {XML_SINGLE, 0, NULL, {{"change_du_state", XML_REC, BKP_CDU_BUILD_REF, NULL}}}, + [BKP_CDU_BUILD_REF] = {XML_SINGLE, 0, NULL, {{"id", XML_INTEGER, 0, NULL}, {"CommandKey", XML_STRING, XML_SWITCH, NULL}, {"time", XML_INTEGER, 0, NULL}, {NULL, XML_REC, BKP_CDU_OPS_REF, NULL}}}, + [BKP_CDU_OPS_REF] = {XML_LIST, BKP_CDU_OPTION, NULL, {}}, + [BKP_CDU_OPTION] = {XML_SINGLE, 0, NULL, {{NULL, XML_FUNC, 0, build_backup_cdu_option}}}, + [BKP_CDU] = {XML_SINGLE, 0, NULL, {{"id", XML_INTEGER, 0, NULL}, {"CommandKey", XML_STRING, XML_SWITCH, NULL}, {"time", XML_INTEGER, 0, NULL}, {"update", XML_FUNC, 0, load_cdu_backup_operation}, {"install", XML_FUNC, 0, load_cdu_backup_operation}, {"uninstall", XML_FUNC, 0, load_cdu_backup_operation}}}, + [BKP_CDU_UPDATE] = {XML_SINGLE, 0, NULL, {{"UUID", XML_STRING, XML_SWITCH, NULL}, {"Version", XML_STRING, XML_SWITCH, NULL}, {"URL", XML_STRING, 0, NULL}, {"Username", XML_STRING, 0, NULL}, {"Password", XML_INTEGER, 0, NULL}}}, + [BKP_CDU_INSTALL] = {XML_SINGLE, 0, NULL, {{"UUID", XML_STRING, XML_SWITCH, NULL}, {"ExecutionEnvRef", XML_STRING, XML_SWITCH, NULL}, {"URL", XML_STRING, XML_SWITCH, NULL}, {"Username", XML_STRING, XML_SWITCH, NULL}, {"Password", XML_INTEGER, XML_SWITCH, NULL}}}, + [BKP_CDU_UNINSTALL] = {XML_SINGLE, 0, NULL, {{"UUID", XML_STRING, XML_SWITCH, NULL}, {"ExecutionEnvRef", XML_STRING, XML_SWITCH, NULL}, {"Version", XML_STRING, XML_SWITCH, NULL}}}, + [BKP_CDU_COMPLETE_BUILD] = {XML_SINGLE, 0, NULL, {{"du_state_change_complete", XML_REC, BKP_CDU_COMPLETE, NULL}}}, + [BKP_CDU_COMPLETE] = {XML_SINGLE, 0, NULL, {{"id", XML_INTEGER, 0, NULL}, {"CommandKey", XML_STRING, XML_SWITCH, NULL}, {"time", XML_INTEGER, 0, NULL}, {"opresult", XML_FUNC, 0, load_cdu_complete_backup_operation}}}, + [BKP_CDU_COMPLETE_OPRES] = {XML_SINGLE, 0, NULL, {{"UUID", XML_STRING, XML_SWITCH, NULL}, {"execution_unit_ref", XML_STRING, 0, NULL}, {"Version", XML_STRING, XML_SWITCH, NULL}, {"CurrentState", XML_STRING, XML_SWITCH, NULL}, {"Resolved", XML_STRING, XML_SWITCH, NULL}, {"StartTime", XML_STRING, XML_SWITCH, NULL}, {"CompleteTime", XML_STRING, XML_SWITCH, NULL}, {"FaultCode", XML_INTEGER, XML_SWITCH, NULL}}}, + [BKP_TRANSFER_COMPLETE_BUILD] = {XML_SINGLE, 0, NULL, {{"transfer_complete", XML_REC, BKP_TRANSFER_COMPLETE, NULL}}}, + [BKP_TRANSFER_COMPLETE] = {XML_SINGLE, 0, NULL, {{"CommandKey", XML_STRING, XML_SWITCH, NULL}, {"StartTime", XML_STRING, XML_SWITCH, NULL}, {"CompleteTime", XML_STRING, XML_SWITCH, NULL}, {"old_software_version", XML_STRING, 0, NULL}, {"FaultCode", XML_INTEGER, XML_SWITCH, NULL}, {"type", XML_LINTEGER, 0, NULL}}}, + [BKP_AUTO_CDU_BUILD] = {XML_SINGLE, 0, NULL, {{"autonomous_du_state_change_complete", XML_REC, BKP_AUTO_CDU, NULL}}}, + [BKP_AUTO_CDU] = {XML_SINGLE, 0, NULL, {{"id", XML_INTEGER, 0, NULL}, {"UUID", XML_STRING, XML_SWITCH, NULL}, {"Version", XML_STRING, XML_SWITCH, NULL}, {"CurrentState", XML_STRING, XML_SWITCH, NULL}, {"StartTime", XML_STRING, XML_SWITCH, NULL}, {"CompleteTime", XML_STRING, XML_SWITCH, NULL}, {"operation", XML_STRING, 0, NULL}, {"FaultCode", XML_INTEGER, XML_SWITCH, NULL}, {"FaultString", XML_STRING, XML_SWITCH, NULL}}}, + [BKP_AUTO_TRANSFER_COMPLETE_BUILD] = {XML_SINGLE, 0, NULL, {{"autonomous_transfer_complete", XML_REC, BKP_AUTO_TRANSFER_COMPLETE, NULL}}}, + [BKP_AUTO_TRANSFER_COMPLETE] = {XML_SINGLE, 0, NULL, {{"id", XML_INTEGER, 0, NULL}, {"AnnounceURL", XML_STRING, XML_SWITCH, NULL}, {"TransferURL", XML_STRING, XML_SWITCH, NULL}, {"IsDownload", XML_BOOL, 0, NULL}, {"StartTime", XML_STRING, XML_SWITCH, NULL}, {"CompleteTime", XML_STRING, XML_SWITCH, NULL}, {"FileType", XML_STRING, XML_SWITCH, NULL}, {"FileSize", XML_INTEGER, XML_SWITCH, NULL}, {"FaultCode", XML_INTEGER, XML_SWITCH, NULL}, {"FaultString", XML_STRING, XML_SWITCH, NULL}}}, + /* * XML node attributes */ @@ -112,6 +147,8 @@ struct xml_node_data xml_nodes_data[] = { [GET_RPC_ATTR] = {XML_SINGLE, 0, NULL, {{"xsi:type", XML_STRING, 0, NULL}, {"soap_enc:arrayType", XML_FUNC, 0, get_soap_enc_array_type}}} }; +struct xml_switch xml_nodes_names_switches[] = {{"URL", "url"}, {"UUID", "uuid"}, {"IsDownload", "isdownload"}, {"AnnounceURL", "announceurl"}, {"TransferURL", "transferurl"}, {"ExecutionEnvRef", "executionenvref"}, {"Resolved", "resolved"}, {"CurrentState", "uuid"}, {"FileType", "file_type"}, {"CommandKey", "command_key"}, {"Username", "username"}, {"Version", "version"}, {"Password", "password"}, {"StartTime", "start_time"}, {"CompleteTime", "complete_time"}, {"FileSize", "file_size"}, {"FaultCode", "fault_code"}, {"FaultString", "fault_string"}}; + char* xml_tags_names[] = { "ParameterList", "Name", @@ -131,6 +168,7 @@ char* xml_tags_names[] = { "UUID", "ExecutionEnvRef", "DeploymentUnitRef", + "execution_unit_ref", "CurrentState", "Version", "OperationPerformed", @@ -148,20 +186,38 @@ char* xml_tags_names[] = { "SerialNumber", "CurrentTime", "ProductClass", + "windowmode1", + "windowmode2", + "usermessage1", + "usermessage2", + "operation", + "old_software_version", + "parameter", "xsi:type", "soap_enc:arrayType", "TargetFileName", + "index", + "id", + "BKP_ID", + "time", "FileSize", "Notification", "MaxRetries", + "maxretrie1", + "maxretrie2", "Status", "InstanceNumber", "FaultCode", "MaxEnvelopes", "RetryCount", + "type", "DelaySeconds", "WindowStart", "WindowEnd", + "windowstart1", + "windowstart2", + "windowend1", + "windowend2", "NextLevel", "NotificationChange", "Writable", @@ -216,6 +272,35 @@ void cwmp_free_all_xml_data_list(struct list_head *list) } } +int load_backup_event_command_key(mxml_node_t *b __attribute__((unused)), struct xml_data_struct *xml_attrs) +{ + mxml_node_t *c = mxmlWalkNext(b, b, MXML_DESCEND); + if (!c || mxmlGetType(c) != MXML_OPAQUE) + return FAULT_CPE_INVALID_ARGUMENTS; + const char *command_key = mxmlGetOpaque(c); + if (xml_attrs->index && *(xml_attrs->index) > -1) { + if (EVENT_CONST[*(xml_attrs->index)].RETRY & EVENT_RETRY_AFTER_REBOOT) { + xml_attrs->event_save = cwmp_add_event_container(*(xml_attrs->index), ((command_key != NULL) ? (char *)command_key : "")); + if (xml_attrs->event_save != NULL) + xml_attrs->event_save->id = *(xml_attrs->id); + } + } + return FAULT_CPE_NO_FAULT; +} + +int load_backup_event_parameter(mxml_node_t *b, struct xml_data_struct *xml_attrs) +{ + mxml_node_t *c = mxmlWalkNext(b, b, MXML_DESCEND); + if (c && mxmlGetType(c) == MXML_OPAQUE) { + const char *op = mxmlGetOpaque(c); + if (op != NULL) { + if (xml_attrs->event_save != NULL) + add_dm_parameter_to_list(&xml_attrs->event_save->head_dm_parameter, (char *)op, NULL, NULL, 0, false); + } + } + return FAULT_CPE_NO_FAULT; +} + int load_upload_filetype(mxml_node_t *b, struct xml_data_struct *xml_attrs) { if (b == NULL) @@ -338,6 +423,56 @@ int load_change_du_state_operation(mxml_node_t *b, struct xml_data_struct *xml_a return FAULT_CPE_NO_FAULT; } +int load_cdu_backup_operation(mxml_node_t *b, struct xml_data_struct *xml_attrs) +{ + int fault = FAULT_CPE_NO_FAULT; + if (b == NULL) + return FAULT_CPE_INTERNAL_ERROR; + + struct operations *operat = (operations *)calloc(1, sizeof(operations)); + list_add_tail(&(operat->list), &(xml_attrs->cdu->list_operation)); + const char *element = mxmlGetElement(b); + + struct xml_data_struct bkp_xml_cdu_backup = {0}; + bkp_xml_cdu_backup.uuid = &operat->uuid; + bkp_xml_cdu_backup.exec_env_ref = &operat->executionenvref; + bkp_xml_cdu_backup.version = &operat->version; + bkp_xml_cdu_backup.url = &operat->url; + bkp_xml_cdu_backup.username = &operat->username; + bkp_xml_cdu_backup.password = &operat->password; + if (strcmp(element, "update") == 0) { + operat->type = DU_UPDATE; + fault = load_xml_node_data(BKP_CDU_UPDATE, b, &bkp_xml_cdu_backup); + } else if (strcmp(element, "install") == 0) { + operat->type = DU_INSTALL; + fault = load_xml_node_data(BKP_CDU_INSTALL, b, &bkp_xml_cdu_backup); + } else if (strcmp(element, "uninstall") == 0) { + operat->type = DU_UNINSTALL; + fault = load_xml_node_data(BKP_CDU_UNINSTALL, b, &bkp_xml_cdu_backup); + } + return fault; +} + +int load_cdu_complete_backup_operation(mxml_node_t *b, struct xml_data_struct *xml_attrs) +{ + if (b == NULL) + return FAULT_CPE_INTERNAL_ERROR; + struct opresult *elem = (opresult *)calloc(1, sizeof(opresult)); + list_add_tail(&(elem->list), &(xml_attrs->cdu_complete->list_opresult)); + struct xml_data_struct opresult_bkp = {0}; + opresult_bkp.uuid = &elem->uuid; + opresult_bkp.version = &elem->version; + opresult_bkp.du_ref = &elem->du_ref; + opresult_bkp.current_state = &elem->current_state; + opresult_bkp.resolved = &elem->resolved; + opresult_bkp.start_time = &elem->start_time; + opresult_bkp.complete_time = &elem->complete_time; + opresult_bkp.fault_code = &elem->fault; + opresult_bkp.exec_unit_ref = &elem->execution_unit_ref; + int fault = load_xml_node_data(BKP_CDU_COMPLETE_OPRES, b, &opresult_bkp); + return fault; +} + int build_inform_env_header(mxml_node_t *b, struct xml_data_struct *xml_attrs) { if (b == NULL || xml_attrs == NULL) @@ -474,6 +609,21 @@ int build_parameter_structure(mxml_node_t *param_list, struct xml_data_struct *x return FAULT_CPE_NO_FAULT; } +int build_backup_cdu_option(mxml_node_t *cdu, struct xml_data_struct *xml_attrs) +{ + mxml_node_t *cdu_opt = NULL; + if (*(xml_attrs->cdu_type) == DU_INSTALL) { + cdu_opt = mxmlNewElement(cdu, "install"); + return build_xml_node_data(BKP_CDU_INSTALL, cdu_opt, xml_attrs); + } else if (*(xml_attrs->cdu_type) == DU_UPDATE) { + cdu_opt = mxmlNewElement(cdu, "update"); + return build_xml_node_data(BKP_CDU_UPDATE, cdu_opt, xml_attrs); + } else if (*(xml_attrs->cdu_type) == DU_UNINSTALL) { + cdu_opt = mxmlNewElement(cdu, "uninstall"); + return build_xml_node_data(BKP_CDU_UNINSTALL, cdu_opt, xml_attrs); + } + return FAULT_CPE_INTERNAL_ERROR; +} int get_soap_enc_array_type(mxml_node_t *node __attribute__((unused)), struct xml_data_struct *xml_attrs) { if (xml_attrs->soap_enc_array_type == NULL) @@ -504,6 +654,36 @@ int get_xml_type(int node_ref, int soap_idx) return xml_nodes_data[node_ref].xml_tags[soap_idx].tag_type; } +char *get_xml_node_name_switch(char *node_name) +{ + unsigned int i; + if (node_name == NULL) + return NULL; + + size_t total_size = sizeof(xml_nodes_names_switches) / sizeof(struct xml_switch); + for (i = 0; i < total_size; i++) + { + if (strcmp(node_name, xml_nodes_names_switches[i].node_name) == 0) + return xml_nodes_names_switches[i].switch_node_name; + } + return NULL; +} + +char *get_xml_node_name_by_switch_name(char *switch_node_name) +{ + unsigned int i; + if (switch_node_name == NULL) + return NULL; + + size_t total_size = sizeof(xml_nodes_names_switches) / sizeof(struct xml_switch); + for (i = 0; i < total_size; i++) + { + if (strcmp(switch_node_name, xml_nodes_names_switches[i].switch_node_name) == 0) + return xml_nodes_names_switches[i].node_name; + } + return NULL; +} + int get_xml_tag_index(const char *name) { unsigned int i; @@ -614,6 +794,20 @@ bool validate_xml_node_opaque_value(char *node_name, char *opaque, struct xml_ta return true; } +bool check_node_is_switch_by_node_name(int node_ref, char *node_name) +{ + unsigned int i; + if (node_name == NULL) + return false; + size_t total_size = sizeof(xml_nodes_data[node_ref].xml_tags) / sizeof(struct xml_tag); + for (i = 0; i < total_size; i++) + { + if (strcmp(xml_nodes_data[node_ref].xml_tags[i].tag_name, node_name) == 0 && xml_nodes_data[node_ref].xml_tags[i].rec_ref == XML_SWITCH) + return true; + } + return false; +} + int load_single_xml_node_data(int node_ref, mxml_node_t *node, struct xml_data_struct *xml_attrs) { mxml_node_t *b = node; @@ -621,12 +815,16 @@ int load_single_xml_node_data(int node_ref, mxml_node_t *node, struct xml_data_s void **ptr = NULL; int error = FAULT_CPE_NO_FAULT; while (b) { - const char *node_name = mxmlGetElement(b); + const char *xml_node_name = mxmlGetElement(b); mxml_type_t node_type = mxmlGetType(b); mxml_node_t *firstchild = mxmlGetFirstChild(b); + char *node_name = get_xml_node_name_by_switch_name((char *)xml_node_name); + if (!check_node_is_switch_by_node_name(node_ref, node_name)) + node_name = (char *)xml_node_name; + if (node_type == MXML_ELEMENT) { - soap_idx = get_xml_soap_tag_index(node_ref, node_name); + soap_idx = get_xml_soap_tag_index(node_ref, (char *)node_name); if (soap_idx == -1) { b = mxmlWalkNext(b, node, MXML_DESCEND); continue; @@ -646,7 +844,7 @@ int load_single_xml_node_data(int node_ref, mxml_node_t *node, struct xml_data_s b = mxmlWalkNext(b, node, MXML_DESCEND); continue; } - idx = get_xml_tag_index(node_name); + idx = get_xml_tag_index((char *)node_name); // cppcheck-suppress knownConditionTrueFalse /* @@ -784,20 +982,36 @@ void xml_data_list_to_cdu_operations_list(struct list_head *xml_data_list, struc } } +void cdu_operations_result_list_to_xml_data_list(struct list_head *du_op_res_list, struct list_head *xml_data_list) +{ + struct opresult *du_op_res_data = NULL; + list_for_each_entry (du_op_res_data, du_op_res_list, list) { + struct xml_list_data *xml_data = calloc(1, sizeof(struct xml_list_data)); + list_add_tail(&xml_data->list, xml_data_list); + xml_data->uuid = strdup(du_op_res_data->uuid ? du_op_res_data->uuid : ""); + xml_data->du_ref = strdup(du_op_res_data->du_ref ? du_op_res_data->du_ref : ""); + xml_data->version = strdup(du_op_res_data->version ? du_op_res_data->version : ""); + xml_data->current_state = strdup(du_op_res_data->current_state ? du_op_res_data->current_state : ""); + xml_data->start_time = strdup(du_op_res_data->start_time ? du_op_res_data->start_time : ""); + xml_data->complete_time = strdup(du_op_res_data->complete_time ? du_op_res_data->complete_time : ""); + xml_data->fault_code = du_op_res_data->fault ? atoi(FAULT_CPE_ARRAY[du_op_res_data->fault].CODE) : 0; + xml_data->fault_string = du_op_res_data->fault ? strdup(FAULT_CPE_ARRAY[du_op_res_data->fault].DESCRIPTION) : strdup(""); + } +} + void cdu_operations_list_to_xml_data_list(struct list_head *du_op_list, struct list_head *xml_data_list) { - struct opresult *du_opt_data = NULL; + struct operations *du_opt_data = NULL; list_for_each_entry (du_opt_data, du_op_list, list) { struct xml_list_data *xml_data = calloc(1, sizeof(struct xml_list_data)); list_add_tail(&xml_data->list, xml_data_list); xml_data->uuid = strdup(du_opt_data->uuid ? du_opt_data->uuid : ""); - xml_data->du_ref = strdup(du_opt_data->du_ref ? du_opt_data->du_ref : ""); + xml_data->du_ref = strdup(du_opt_data->url ? du_opt_data->url : ""); + xml_data->username = strdup(du_opt_data->username ? du_opt_data->username : ""); + xml_data->password = strdup(du_opt_data->password ? du_opt_data->password : ""); + xml_data->execution_env_ref = strdup(du_opt_data->executionenvref ? du_opt_data->executionenvref : ""); xml_data->version = strdup(du_opt_data->version ? du_opt_data->version : ""); - xml_data->current_state = strdup(du_opt_data->current_state ? du_opt_data->current_state : ""); - xml_data->start_time = strdup(du_opt_data->start_time ? du_opt_data->start_time : ""); - xml_data->complete_time = strdup(du_opt_data->complete_time ? du_opt_data->complete_time : ""); - xml_data->fault_code = du_opt_data->fault ? atoi(FAULT_CPE_ARRAY[du_opt_data->fault].CODE) : 0; - xml_data->fault_string = du_opt_data->fault ? strdup(FAULT_CPE_ARRAY[du_opt_data->fault].DESCRIPTION) : strdup(""); + xml_data->cdu_type = du_opt_data->type; } } @@ -829,23 +1043,23 @@ void get_xml_data_value_by_name(int type, int idx, struct xml_data_struct *xml_a switch(type) { case XML_STRING: str = (char **)(*ptr); - *data_value = icwmp_strdup(*str ? *str : ""); + *data_value = icwmp_strdup((str && *str) ? *str : ""); break; case XML_INTEGER: intgr = (int *)(*ptr); - icwmp_asprintf(data_value, "%d", *intgr); + icwmp_asprintf(data_value, "%d", intgr ? *intgr : 0); break; case XML_LINTEGER: lint = (long int *)(*ptr); - icwmp_asprintf(data_value, "%ld", *lint); + icwmp_asprintf(data_value, "%ld", lint ? *lint : 0); break; case XML_BOOL: bol = (bool *)(*ptr); - *data_value = icwmp_strdup(bol ? "1" : "0"); + *data_value = icwmp_strdup((bol && *bol) ? "1" : "0"); break; case XML_TIME: time = (time_t *)(*ptr); - icwmp_asprintf(data_value, "%ld", *time); + icwmp_asprintf(data_value, "%ld", time ? *time : 0); break; case XML_NODE: *data_value = *ptr; @@ -884,12 +1098,17 @@ int build_single_xml_node_data(int node_ref, mxml_node_t *node, struct xml_data_ int total_size = get_xml_tags_array_total_size(node_ref); for(i =0; i < total_size; i++) { if (xml_nodes_data[node_ref].xml_tags[i].tag_name != NULL) { - n = mxmlNewElement(node, xml_nodes_data[node_ref].xml_tags[i].tag_name); + char *node_name = NULL; + if (xml_nodes_data[node_ref].xml_tags[i].rec_ref == XML_SWITCH) + node_name = get_xml_node_name_switch(xml_nodes_data[node_ref].xml_tags[i].tag_name); + else + node_name = xml_nodes_data[node_ref].xml_tags[i].tag_name; + n = mxmlNewElement(node, node_name); if (!n) return FAULT_CPE_INTERNAL_ERROR; } - if (xml_nodes_data[node_ref].xml_tags[i].rec_ref >= ATTR_PARAM_STRUCT) { + if ((xml_nodes_data[node_ref].xml_tags[i].rec_ref >= ATTR_PARAM_STRUCT) && (xml_nodes_data[node_ref].xml_tags[i].rec_ref < XML_SWITCH)) { set_node_attributes(xml_nodes_data[node_ref].xml_tags[i].rec_ref, n, xml_attrs); if (xml_nodes_data[node_ref].xml_tags[i].tag_type == XML_REC) continue; diff --git a/src/xml.h b/src/xml.h index af67f61..fce9617 100644 --- a/src/xml.h +++ b/src/xml.h @@ -81,9 +81,44 @@ enum soap_methods { SOAP_CDU_RESULTS_REF, SOAP_ACDU_OPTS_REF, SOAP_CDU_OPTS_REF, + + BKP_EVT_LOAD, + BKP_EVT_PARAM_REF, + BKP_EVT_SINGLE_PARAM, + BKP_EVT_BUILD, + BKP_EVT_BUILD_REF, + BKP_SCHEDULE_INFORM_BUILD, + BKP_SCHEDULE_INFORM, + BKP_DOWNLOAD_BUILD, + BKP_DOWNLOAD, + BKP_SCHED_DOWNLOAD_BUILD, + BKP_SCHED_DOWNLOAD, + BKP_UPLOAD_BUILD, + BKP_UPLOAD, + BKP_CDU_BUILD, + BKP_CDU_BUILD_REF, + BKP_CDU_OPS_REF, + BKP_CDU_OPTION, + BKP_CDU, + BKP_CDU_UPDATE, + BKP_CDU_INSTALL, + BKP_CDU_UNINSTALL, + BKP_CDU_COMPLETE_BUILD, + BKP_CDU_COMPLETE, + BKP_CDU_COMPLETE_OPRES, + BKP_TRANSFER_COMPLETE_BUILD, + BKP_TRANSFER_COMPLETE, + BKP_AUTO_CDU_BUILD, + BKP_AUTO_CDU, + BKP_AUTO_TRANSFER_COMPLETE_BUILD, + BKP_AUTO_TRANSFER_COMPLETE, + ATTR_PARAM_STRUCT, ATTR_SOAP_ENV, GET_RPC_ATTR, + + XML_SWITCH, + SOAP_MAX }; @@ -111,6 +146,11 @@ enum validation_types { VALIDATE_INT_RANGE }; +struct xml_switch { + char *node_name; + char *switch_node_name; +}; + struct xml_tag_validation { char *tag_name; int validation_type; @@ -137,6 +177,7 @@ struct xml_data_struct { char **uuid; char **exec_env_ref; char **du_ref; + char **exec_unit_ref; char **current_state; char **version; char **operation; @@ -154,29 +195,50 @@ struct xml_data_struct { char **serial_number; char **current_time; char **product_class; + char **window_mode1; + char **window_mode2; + char **user_message1; + char **user_message2; + char **op; + char **old_software_version; + char **parameter; char **xsi_type; char **soap_enc_array_type; char **target_file_name; + int *index; + int *id; + int *bkp_id; + int *time; int *file_size; int *notification; int *scheddown_max_retries; + int *max_retries1; + int *max_retries2; int *status; int *instance; int *fault_code; int *max_envelopes; int *retry_count; + int *type; long int *delay_seconds; long int *window_start; long int *window_end; + time_t *window_start1; + time_t *window_start2; + time_t *window_end1; + time_t *window_end2; bool *next_level; bool *notification_change; bool *writable; bool *is_download; + struct change_du_state *cdu; + struct du_state_change_complete *cdu_complete; mxml_node_t **xml_env; struct list_head *data_list; char **xcwmp; int *amd_version; + struct event_container *event_save; unsigned int *session_timeout; int *cdu_type; int *counter; @@ -230,7 +292,7 @@ struct xml_node_data { int node_ms; int tag_node_ref; char *tag_list_name; - struct xml_tag xml_tags[10]; + struct xml_tag xml_tags[20]; }; #define MXML_DELETE(X) \ @@ -263,17 +325,25 @@ mxml_node_t * build_top_body_soap_request(mxml_node_t *node, char *method); void dm_parameter_list_to_xml_data_list(struct list_head *dm_parameter_list, struct list_head *xml_data_list); void xml_data_list_to_dm_parameter_list(struct list_head *xml_data_list, struct list_head *dm_parameter_list); void xml_data_list_to_cdu_operations_list(struct list_head *xml_data_list, struct list_head *du_op_list); +void cdu_operations_result_list_to_xml_data_list(struct list_head *du_op_res_list, struct list_head *xml_data_list); void cdu_operations_list_to_xml_data_list(struct list_head *du_op_list, struct list_head *xml_data_list); void event_container_list_to_xml_data_list(struct list_head *event_container, struct list_head *xml_data_list); void cwmp_param_fault_list_to_xml_data_list(struct list_head *param_fault_list, struct list_head *xml_data_list); void cwmp_free_all_xml_data_list(struct list_head *list); int load_upload_filetype(mxml_node_t *b, struct xml_data_struct *xml_attrs); +int load_backup_event_command_key(mxml_node_t *b, struct xml_data_struct *xml_attrs); +int load_backup_event_parameter(mxml_node_t *b, struct xml_data_struct *xml_attrs); int load_get_rpc_method_acs_resp_string(mxml_node_t *b, struct xml_data_struct *xml_attrs); int load_download_filetype(mxml_node_t *b, struct xml_data_struct *xml_attrs); int load_sched_download_window_mode(mxml_node_t *b, struct xml_data_struct *xml_attrs); int load_change_du_state_operation(mxml_node_t *b, struct xml_data_struct *xml_attrs); +int load_cdu_backup_operation(mxml_node_t *b, struct xml_data_struct *xml_attrs); +int load_cdu_complete_backup_operation(mxml_node_t *b, struct xml_data_struct *xml_attrs); int build_inform_events(mxml_node_t *b, struct xml_data_struct *xml_attrs); int build_inform_env_header(mxml_node_t *b, struct xml_data_struct *xml_attrs); int build_parameter_structure(mxml_node_t *param_list, struct xml_data_struct *xml_attrs); +int build_backup_cdu_option(mxml_node_t *cdu, struct xml_data_struct *xml_attrs); int get_soap_enc_array_type(mxml_node_t *node, struct xml_data_struct *xml_attrs); +char *get_xml_node_name_switch(char *node_name); +char *get_xml_node_name_by_switch_name(char *switch_node_name); #endif diff --git a/test/cmocka/icwmp_backup_session_unit_test.c b/test/cmocka/icwmp_backup_session_unit_test.c index 02e52ae..2fd0427 100644 --- a/test/cmocka/icwmp_backup_session_unit_test.c +++ b/test/cmocka/icwmp_backup_session_unit_test.c @@ -64,7 +64,7 @@ static void cwmp_backup_session_unit_test(void **state) mxml_node_t *bkp_event1 = NULL, *bkp_event2 = NULL, *event_tree1 = NULL, *event_tree2; // Case of one event - bkp_event1 = bkp_session_insert_event(EVENT_IDX_4VALUE_CHANGE, "4 VALUE CHANGE", 0); + bkp_event1 = bkp_session_insert_event(EVENT_IDX_4VALUE_CHANGE, "VALUE_CHANGE", 0); bkp_session_save(); pFile = fopen(CWMP_BKP_FILE, "r"); backup_tree = mxmlLoadFile(NULL, pFile, MXML_OPAQUE_CALLBACK); @@ -80,7 +80,7 @@ static void cwmp_backup_session_unit_test(void **state) assert_int_equal(atoi(mxmlGetOpaque(mxmlGetFirstChild(n))), 0); n = mxmlFindElement(event_tree1, event_tree1, "command_key", NULL, NULL, MXML_DESCEND); assert_non_null(n); - assert_string_equal(mxmlGetOpaque(mxmlGetFirstChild(n)), "4 VALUE CHANGE"); + assert_string_equal(mxmlGetOpaque(mxmlGetFirstChild(n)), "VALUE_CHANGE"); MXML_DELETE(bkp_event1); bkp_session_save(); MXML_DELETE(backup_tree); @@ -137,6 +137,7 @@ static void cwmp_backup_session_unit_test(void **state) download->password = icwmp_strdup("iopsys"); download->username = icwmp_strdup("iopsys"); download->url = icwmp_strdup("http://192.168.1.160:8080/openacs/acs"); + download->id = 1; bkp_session_insert_download(download); bkp_session_save(); pFile = fopen(CWMP_BKP_FILE, "r"); @@ -161,9 +162,6 @@ static void cwmp_backup_session_unit_test(void **state) n = mxmlFindElement(download_tree, download_tree, "password", NULL, NULL, MXML_DESCEND); assert_non_null(n); assert_string_equal(mxmlGetOpaque(mxmlGetFirstChild(n)), "iopsys"); - n = mxmlFindElement(download_tree, download_tree, "file_size", NULL, NULL, MXML_DESCEND); - assert_non_null(n); - assert_string_equal(mxmlGetOpaque(mxmlGetFirstChild(n)), "0"); n = mxmlFindElement(download_tree, download_tree, "time", NULL, NULL, MXML_DESCEND); assert_non_null(n); MXML_DELETE(backup_tree); @@ -171,7 +169,7 @@ static void cwmp_backup_session_unit_test(void **state) /* * Delete download */ - bkp_session_delete_download(download); + bkp_session_delete_element("download", download->id); bkp_session_save(); pFile = fopen(CWMP_BKP_FILE, "r"); backup_tree = mxmlLoadFile(NULL, pFile, MXML_OPAQUE_CALLBACK); @@ -182,7 +180,7 @@ static void cwmp_backup_session_unit_test(void **state) MXML_DELETE(backup_tree); /* - * Insert TransferComplete bkp_session_delete_transfer_complete + * Insert TransferComplete bkp_session_insert_transfer_complete */ struct transfer_complete *p; p = icwmp_calloc(1, sizeof(struct transfer_complete)); @@ -192,6 +190,7 @@ static void cwmp_backup_session_unit_test(void **state) p->old_software_version = icwmp_strdup("iopsys_img_old"); p->type = TYPE_DOWNLOAD; p->fault_code = FAULT_CPE_NO_FAULT; + p->id = 1; bkp_session_insert_transfer_complete(p); bkp_session_save(); pFile = fopen(CWMP_BKP_FILE, "r"); @@ -219,7 +218,7 @@ static void cwmp_backup_session_unit_test(void **state) /* * Delete TransferComplete */ - bkp_session_delete_transfer_complete(p); + bkp_session_delete_element_by_key("transfer_complete", "start_time", p->start_time); bkp_session_save(); pFile = fopen(CWMP_BKP_FILE, "r"); backup_tree = mxmlLoadFile(NULL, pFile, MXML_OPAQUE_CALLBACK);