diff --git a/src/Download.c b/src/Download.c index 136c70c..8d4f096 100644 --- a/src/Download.c +++ b/src/Download.c @@ -431,7 +431,10 @@ int cwmp_launch_download(struct cwmp *cwmp, struct session *session, struct down res = curl_easy_perform(curl); curl_easy_cleanup(curl); } - fclose(fp); + if(fp != NULL) + { + fclose(fp); + } } if(res != CURLE_OK) diff --git a/src/Makefile b/src/Makefile index 8bf5f82..97af4f6 100644 --- a/src/Makefile +++ b/src/Makefile @@ -13,7 +13,7 @@ LIBS=-lc SHLIB_FILE=libcwmp.$(SHLIB_EXT).$(CWMP_VERSION) PCFLAGS+=-Iinc -DCWMP_VERSION=\"$(CWMP_VERSION)\" -DWITH_COOKIES -DWITH_CWMP_DEBUG -DWITH_DEV_DEBUG -DACS_MULTI=1 -PLDFLAGS+=-DCWMP_VERSION=\"$(CWMP_VERSION)\" -DWITH_COOKIES -DWITH_CWMP_DEBUG -DWITH_DEV_DEBUG -luci -lexpat -lpthread -lssl -lcurl -lubox -lubus +PLDFLAGS+=-DCWMP_VERSION=\"$(CWMP_VERSION)\" -DWITH_COOKIES -DWITH_CWMP_DEBUG -DWITH_DEV_DEBUG -luci -lmicroxml -lpthread -lssl -lcurl -lubox -lubus ifneq ($(CWMP_REVISION)_,_) ifneq ($(CWMP_REVISION),exported) diff --git a/src/backupSession.c b/src/backupSession.c index 4a22c79..38029dd 100644 --- a/src/backupSession.c +++ b/src/backupSession.c @@ -18,13 +18,22 @@ Author contact information: #include #include #include -#include -#include "soapH.h" +#include +#include +#include #include "cwmp.h" #include "backupSession.h" -static int backup_session_insert (char *element, char *value, char *parent, char *parent_name, struct attribute *attrs, int level); -static int backup_session_delete (char *element, struct attribute *attrs,int level, char *parent_name); +# define CWMP_BACKUP_SESSION "" + +pthread_mutex_t mutex_backup_session; +extern struct list_head list_schedule_inform; +extern struct list_head list_download; +extern const struct EVENT_CONST_STRUCT EVENT_CONST [COUNT_EVENT]; +extern struct FAULT_CPE FAULT_CPE_ARRAY [FAULT_CPE_ARRAY_SIZE]; +extern int count_download_queue; + +int cwmp_load_saved_session(struct cwmp *cwmp, char **acsurl, enum backup_loading load); void backup_session_insert_rpc(char *name, char *commandKey, int status); void backup_session_delete_rpc(char *name, char *commandKey, int status); void backup_session_insert_event(char *name, char *commandKey, int id, int rpc_status); @@ -36,1359 +45,869 @@ void backup_session_move_inform_to_inform_queue (); void backup_session_insert_schedule_time(time_t schedule_time,char *commandKey); void backup_session_delete_schedule_time(time_t schedule_time,char *commandKey); void backup_session_delete_scheduled_inform(); -void backup_session_insert_download(char *commandKey, char *url, time_t id); -void backup_session_delete_download(char *commandKey, char *url, time_t id); -void backup_session_insert_parameter_download(char *name, char *value, char *url, int id, char *commandKey); - -struct event_container *cwmp_add_event_container (struct cwmp *cwmp, int event_idx, char *command_key); -struct _cwmp1__TransferComplete *cwmp_set_data_rpc_acs_transferComplete (); - -static pthread_mutex_t mutex_backup_session; -extern struct list_head list_schedule_inform; -extern struct list_head list_download; -extern const struct EVENT_CONST_STRUCT EVENT_CONST [COUNT_EVENT]; -extern struct FAULT_CPE FAULT_CPE_ARRAY [FAULT_CPE_ARRAY_SIZE]; -static struct cwmp *save_cwmp; -static struct event_container *event_container_save = NULL; -static char *acs_url = NULL; -static char value[256]; -static char *parent = NULL; -static char *rpc_name = NULL; -static char *rpc_commandkey = NULL; -static char *parameter_name = NULL; -static struct _cwmp1__TransferComplete *p_soap_cwmp1__TransferComplete; -static enum backup_loading init_option; -static struct download *download_request = NULL; -extern int count_download_queue; - -long int get_xml_file_size(FILE *pFile) -{ - long int size; - - if(pFile!=NULL) - { - fseek (pFile, 0, SEEK_END); - - size = ftell (pFile); - rewind(pFile); - return size; - } - return 0; -} +void backup_session_insert_download(char *commandKey, char *url, time_t schedule_time); +void backup_session_delete_download(char *commandKey, char *url, time_t schedule_time); +void backup_session_insert_parameter_download(char *name, char *value, char *url, time_t schedule_time, char *commandKey); -void start_bkp_hndl(void *data, const char *el, const char **attr) -{ - char *event_name; - char *event_commandKey; - char *event_id; - int event_idx; - int idx; - int i; - struct schedule_inform *schedule_inform; - struct list_head *ilist; - char *scheduled_commandKey; - time_t scheduled_time; - - if(init_option == ALL) - { - if(strcmp(el,"rpc") == 0) - { - for(i = 0; attr[i] ; i+=2) - { - if(strcmp(attr[i],"name") == 0) - { - rpc_name = strdup(attr[i + 1]); - } - if(strcmp(attr[i],"commandKey") == 0) - { - rpc_commandkey = strdup(attr[i + 1]); - } - } - if(strcmp(rpc_name,"TransferComplete") == 0) - { - parent = strdup(el); - p_soap_cwmp1__TransferComplete = cwmp_set_data_rpc_acs_transferComplete (); - } - } - if(strcmp(el,"event") == 0) - { - parent = strdup(el); - for(i = 0; attr[i] ; i+=2) - { - if(strcmp(attr[i],"name") == 0) - { - event_name = strdup(attr[i+1]); - } - if(strcmp(attr[i],"commandKey") == 0) - { - event_commandKey = strdup(attr[i+1]); - } - if(strcmp(attr[i],"id") == 0) - { - event_id = strdup(attr[i+1]); - } - } - for (i = 0; i< COUNT_EVENT; i++) - { - if(strcmp(event_name,EVENT_CONST [i].CODE) == 0) - { - event_idx = i; - break; - } - } - if ((save_cwmp->env.boot != CWMP_START_BOOT) || (EVENT_CONST[event_idx].RETRY & EVENT_RETRY_AFTER_REBOOT)) - { - event_container_save = cwmp_add_event_container (save_cwmp, event_idx, event_commandKey); - if(event_container_save != NULL) - { - event_container_save->id = atoi(event_id); - } - } - if(event_id != NULL) - { - free(event_id); - event_id = NULL; - } - if(event_commandKey != NULL) - { - free(event_commandKey); - event_commandKey = NULL; - } - if(event_name != NULL) - { - free(event_name); - event_name = NULL; - } - } - if(strcmp(el,"parameter") == 0) - { - for(i = 0; attr[i] ; i+=2) - { - if(strcmp(attr[i],"name") == 0) - { - parameter_name = strdup(attr[i + 1]); - } - } - } - if(strcmp(el,"scheduledTime") == 0) - { - for(i = 0; attr[i] ; i+=2) - { - if(strcmp(attr[i],"commandKey") == 0) - { - scheduled_commandKey = strdup(attr[i + 1]); - } - if(strcmp(attr[i],"time") == 0) - { - scheduled_time = atol(attr[i + 1]); - } - } - 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 = scheduled_commandKey; - schedule_inform->scheduled_time = scheduled_time; - list_add (&(schedule_inform->list), ilist->prev); - } - } - if(strcmp(el,"download") == 0) - { - parent = strdup(el); - download_request = calloc(1,sizeof(struct download)); - for(i = 0; attr[i] ; i+=2) - { - if(strcmp(attr[i],"commandKey") == 0) - { - download_request->CommandKey = strdup(attr[i + 1]); - } - if(strcmp(attr[i],"url") == 0) - { - download_request->URL = strdup(attr[i + 1]); - } - if(strcmp(attr[i],"time") == 0) - { - download_request->scheduled_time = atol(attr[i + 1]); - } - } - } - } -} - -void end_bkp_hndl(void *data, const char *el) -{ - int i,fault_idx; - struct session *session; - struct list_head *ilist; - struct download *idownload_request; - - if(init_option == ALL || init_option == ACS) - { - if(strcmp(el,"url") == 0) - { - acs_url = strdup(value); - } - } - if(init_option == ALL) - { - if(strcmp(el,"rpc") == 0) - { - if(strcmp(rpc_name,"TransferComplete") == 0) - { - if(rpc_commandkey != NULL) - { - p_soap_cwmp1__TransferComplete->CommandKey = strdup(rpc_commandkey); - } - else - { - p_soap_cwmp1__TransferComplete->CommandKey = strdup(""); - } - cwmp_root_cause_TransferComplete (save_cwmp); - } - if(parent != NULL) - { - free(parent); - parent = NULL; - } - if(rpc_name != NULL) - { - free(rpc_name); - rpc_name = NULL; - } - } - if(strcmp(el,"event") == 0) - { - if(parent != NULL) - { - free(parent); - parent = NULL; - } - } - if(strcmp(el,"download") == 0) - { - 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); - count_download_queue++; - if(parent != NULL) - { - free(parent); - parent = NULL; - } - } - if(strcmp(el,"parameter") == 0) - { - if(parent != NULL && strcmp(parent,"rpc") == 0) - { - if(strcmp(rpc_name,"TransferComplete") == 0) - { - if(p_soap_cwmp1__TransferComplete != NULL) - { - if(strcmp(parameter_name,"StartTime") == 0) - { - p_soap_cwmp1__TransferComplete->StartTime = atol(value); - } - if(strcmp(parameter_name,"CompleteTime") == 0) - { - p_soap_cwmp1__TransferComplete->CompleteTime = atol(value); - } - if(strcmp(parameter_name,"FaultCode") == 0) - { - p_soap_cwmp1__TransferComplete->FaultStruct = calloc(1,sizeof(struct cwmp1__FaultStruct)); - p_soap_cwmp1__TransferComplete->FaultStruct->FaultCode = strdup(value); - for(i = 0; i < FAULT_CPE_ARRAY_SIZE; i++) - { - if((FAULT_CPE_ARRAY[i].CODE != NULL)&&(strcmp(FAULT_CPE_ARRAY[i].CODE, value) == 0)) - { - fault_idx = i; - break; - } - } - p_soap_cwmp1__TransferComplete->FaultStruct->FaultString = strdup(FAULT_CPE_ARRAY[i].DESCRIPTION); - } - } - } - } - if(parent != NULL && strcmp(parent,"download") == 0) - { - if(download_request != NULL) - { - if(strcmp(parameter_name,"FileType") == 0) - { - download_request->FileType = strdup(value); - } - if(strcmp(parameter_name,"Username") == 0) - { - download_request->Username = strdup(value); - } - if(strcmp(parameter_name,"Password") == 0) - { - download_request->Password = strdup(value); - } - } - } - if(parent != NULL && strcmp(parent,"event") == 0) - { - if(event_container_save != NULL) - { - cwmp_add_parameter_container (save_cwmp,event_container_save, parameter_name, NULL, NULL); - } - } - if(parameter_name != NULL) - { - free(parameter_name); - parameter_name = NULL; - } - } - } -} - -void char_bkp_hndl(void *data, const char *content,int length) -{ - char tmp[512]; - int i = 0,n = 0; - - if(length!=0) - { - strncpy(tmp, content, length); - tmp[length] = '\0'; - while(istatus != NULL) - { - sprintf(attrib,"name=\"%s\" status=\"%s\"", attrs->name, attrs->status); - } - else - { - if(attrs->commandKey != NULL) - { - sprintf(attrib,"name=\"%s\" commandKey=\"%s\"", attrs->name, attrs->commandKey); - } - else - { - sprintf(attrib,"name=\"%s\"", attrs->name); - } - } - } - else if (strcmp(element,"event") == 0) - { - sprintf(attrib,"name=\"%s\" id=\"%li\" commandKey=\"%s\"", attrs->name, attrs->id, attrs->commandKey); - } - else if (strcmp(element,"parameter") == 0) - { - sprintf(attrib,"name=\"%s\"", attrs->name); - } - else if (strcmp(element,"scheduledTime") == 0) - { - sprintf(attrib,"commandKey=\"%s\" time=\"%li\"", attrs->commandKey, attrs->id); - } - else if (strcmp(element,"download") == 0) - { - sprintf(attrib,"commandKey=\"%s\" url=\"%s\" time=\"%li\"", attrs->commandKey, attrs->url, attrs->id); - } - } - - if(strlen(indent) != 0) - { - if(value != NULL) - { - if(strlen(attrib) != 0) - { - sprintf(start,"%s<%s %s>%s",indent,element,attrib,value); - } - else - { - sprintf(start,"%s<%s>%s",indent,element,value); - } - sprintf(end,"\n",element); - } - else - { - if(strlen(attrib) != 0) - { - sprintf(start,"%s<%s %s>\n",indent,element,attrib); - } - else - { - sprintf(start,"%s<%s>\n",indent,element); - } - sprintf(end,"%s\n",indent,element); - } - } - else - { - if(strlen(attrib) != 0) - { - sprintf(start,"<%s %s>\n",element,attrib); - } - else - { - sprintf(start,"<%s>\n",element); - } - sprintf(end,"",element); - } - - if(parent != NULL) - { - if(parent_name != NULL) - { - if(strcmp(parent,"event") == 0) - { - sprintf(parent_tag,"<%s name=\"%s\" id=\"%li\" commandKey=\"%s\"", parent, parent_name, attrs->id, attrs->commandKey); - } - else if(strcmp(parent,"rpc") == 0) - { - if(attrs->commandKey != NULL) - { - sprintf(parent_tag,"<%s name=\"%s\" commandKey=\"%s\"", parent, parent_name, attrs->commandKey); - } - else - { - sprintf(parent_tag,"<%s name=\"%s\"", parent, parent_name); - } - } - else - { - sprintf(parent_tag,"<%s name=\"%s\"", parent, parent_name); - } - } - else - { - if(strcmp(parent,"download") == 0) - { - if(attrs->commandKey != NULL) - { - sprintf(parent_tag,"<%s commandKey=\"%s\" url=\"%s\" time=\"%li\"", parent, attrs->commandKey, attrs->url, attrs->id); - } - else - { - sprintf(parent_tag,"<%s commandKey=\"\" url=\"%s\" time=\"%li\"", parent, attrs->url, attrs->id); - } - } - else - { - sprintf(parent_tag,"<%s", parent); - } - } - } - /** search element if exist **/ - while(!feof(pFile)) - { - fgets(line,sizeof(line),pFile); - if ((strcmp(element,"parameter") != 0) && - (strstr(line,start) != NULL)) - { - fclose(pFile); - fclose(tmpFile); - remove("tmp.xml"); - pthread_mutex_unlock (&mutex_backup_session); - return CWMP_OK; - } - if ((strcmp(element,"parameter") == 0) && - (parent != NULL) && (strcmp(parent,"event") == 0)&& - (strstr(line,parent_tag) != NULL)) - { - while(strstr(line,"") == NULL) - { - fgets(line,sizeof(line),pFile); - if (strstr(line,start) != NULL) - { - fclose(pFile); - fclose(tmpFile); - remove("tmp.xml"); - pthread_mutex_unlock (&mutex_backup_session); - return CWMP_OK; - } - } - } - if ((strcmp(element,"parameter") == 0) && - (parent != NULL) && (strcmp(parent,"rpc") == 0) && - (strstr(line,parent_tag) != NULL)) - { - while(strstr(line,"") == NULL) - { - fgets(line,sizeof(line),pFile); - if (strstr(line,start) != NULL) - { - fclose(pFile); - fclose(tmpFile); - remove("tmp.xml"); - pthread_mutex_unlock (&mutex_backup_session); - return CWMP_OK; - } - } - } - if ((strcmp(element,"parameter") == 0) && - (parent != NULL) && (strcmp(parent,"download") == 0) && - (strstr(line,parent_tag) != NULL)) - { - while(strstr(line,"") == NULL) - { - fgets(line,sizeof(line),pFile); - if (strstr(line,start) != NULL) - { - fclose(pFile); - fclose(tmpFile); - remove("tmp.xml"); - pthread_mutex_unlock (&mutex_backup_session); - return CWMP_OK; - } - } - } - } - - if((feof(pFile))&&(strcmp(element,"cwmp") == 0)) - { - fputs(start,tmpFile); - fputs(end,tmpFile); - goto end; - } - rewind(pFile); - while(!feof(pFile)) - { - fgets(line,sizeof(line),pFile); - fputs(line,tmpFile); - if(strstr(line,parent_tag) != NULL) - { - if((strcmp(element,"event") == 0)&&(strstr(line,attrs->status) == NULL)) - { - continue; - } - break; - } - } - - fputs(start,tmpFile); - fputs(end,tmpFile); - while(!feof(pFile)) - { - fgets(line,sizeof(line),pFile); - fputs(line,tmpFile); - } -end: - fclose(pFile); - fclose(tmpFile); - rename("tmp.xml", CWMP_BKP_FILE); - pthread_mutex_unlock (&mutex_backup_session); - return CWMP_OK; -} - -static int backup_session_delete (char *element, struct attribute *attrs,int level, char *parent_name) -{ - char line[256]; - char start[256]; - char end[256]; - char attrib[256]; - char id[16]; - int i; - FILE *pFile; - FILE *tmpFile; - bool found = FALSE; - - pthread_mutex_lock (&mutex_backup_session); - attrib[0] = 0; - start[0] = 0; - end[0] = 0; - - pFile = fopen(CWMP_BKP_FILE,"r"); - tmpFile = fopen("tmp.xml","w"); - if(pFile == NULL || tmpFile == NULL) - { - CWMP_LOG(ERROR,"Unable to write in %s file",CWMP_BKP_FILE); - pthread_mutex_unlock (&mutex_backup_session); - exit(EXIT_FAILURE); - } - - if(attrs != NULL) - { - if(strcmp(element,"rpc") == 0) - { - if(attrs->status != NULL) - { - sprintf(attrib,"name=\"%s\" status=\"%s\"", attrs->name, attrs->status); - } - else - { - if(attrs->commandKey != NULL) - { - sprintf(attrib,"name=\"%s\" commandKey=\"%s\"", attrs->name, attrs->commandKey); - } - else - { - sprintf(attrib,"name=\"%s\"", attrs->name); - } - } - } - else if (strcmp(element,"event") == 0) - { - sprintf(attrib,"name=\"%s\" id=\"%li\" commandKey=\"%s\"", attrs->name, attrs->id, attrs->commandKey); - } - else if (strcmp(element,"parameter") == 0) - { - sprintf(attrib,"name=\"%s\"", attrs->name); - sprintf(id,"%li",attrs->id); - } - else if (strcmp(element,"scheduledTime") == 0) - { - sprintf(attrib,"commandKey=\"%s\" time=\"%li\"", attrs->commandKey, attrs->id); - } - else if (strcmp(element,"download") == 0) - { - sprintf(attrib,"commandKey=\"%s\" url=\"%s\" time=\"%li\"", attrs->commandKey, attrs->url, attrs->id); - } - } - - if(strlen(attrib) != 0) - { - sprintf(start,"<%s %s>",element,attrib); - } - else - { - sprintf(start,"<%s>",element); - } - sprintf(end,"\n",element); - - /** search element if exist **/ - - while(!feof(pFile)) - { - fgets(line,sizeof(line),pFile); - if (strstr(line,start) != NULL) - { - found = TRUE; - break; - } - } - if((found == FALSE) && (feof(pFile))) - { - fclose(pFile); - fclose(tmpFile); - remove("tmp.xml"); - pthread_mutex_unlock (&mutex_backup_session); - return CWMP_OK; - } - else - { - found = FALSE; - rewind(pFile); - } - - while(!feof(pFile)) - { - fgets(line,sizeof(line),pFile); - if ( - (strcmp(element,"parameter") == 0) && - (strstr(line,parent_name) != NULL) && - ((attrs != NULL)&&((attrs->id == -1)||((attrs->id != -1) && (strstr(line,id) != NULL)))) - ) - { - found = TRUE; - } - if((strcmp(element,"parameter") != 0)||((strcmp(element,"parameter") == 0)&&(found == TRUE))) - { - if((strstr(line,start) != NULL)&&(strstr(line,end) == NULL)) - { - break; - } - else if((strstr(line,start) != NULL)&&(strstr(line,end) != NULL)) - { - goto end; - } - } - fputs(line,tmpFile); - } - - if(feof(pFile)) - { - pthread_mutex_unlock (&mutex_backup_session); - return CWMP_OK; - } - - while(!feof(pFile)&&(strstr(line,end) == NULL)) - { - fgets(line,sizeof(line),pFile); - } -end: - while(!feof(pFile)) - { - fgets(line,sizeof(line),pFile); - fputs(line,tmpFile); - } - fclose(pFile); - fclose(tmpFile); - rename("tmp.xml", CWMP_BKP_FILE); - pthread_mutex_unlock (&mutex_backup_session); - return CWMP_OK; -} - -void backup_session_insert_event(char *name, char *commandKey, int id, int rpc_status) -{ - struct attribute *attrs; - - attrs = calloc(1,sizeof(struct attribute)); - if(attrs != NULL) - { - attrs->id = id; - attrs->name = strdup(name); - if(rpc_status == RPC_QUEUE) - { - attrs->status = strdup("queue"); - } - else if(rpc_status == RPC_SEND) - { - attrs->status = strdup("send"); - } - if(commandKey != NULL) - { - attrs->commandKey = strdup(commandKey); - } - else - { - attrs->commandKey = NULL; - } - } - backup_session_insert("event",NULL,"rpc",NULL,attrs,3); - if(attrs->name != NULL) - { - free(attrs->name); - } - if(attrs->status != NULL) - { - free(attrs->status); - } - if(attrs->commandKey != NULL) - { - free(attrs->commandKey); - } - free(attrs); -} - -void backup_session_insert_parameter(char *name, char *value, char *parent,char *parent_name, int id, char *commandKey) -{ - struct attribute *attrs; - int level; - attrs = calloc(1,sizeof(struct attribute)); - if(attrs != NULL) - { - attrs->id = id; - attrs->name = strdup(name); - if(commandKey != NULL) - { - attrs->commandKey = strdup(commandKey); - } - } - if(strcmp(parent,"rpc") == 0) - { - level = 3; - } - else if(strcmp(parent,"event") == 0) - { - level = 4; - } - backup_session_insert("parameter",value,parent,parent_name,attrs,level); - if(attrs->name != NULL) - { - free(attrs->name); - } - if(attrs->commandKey != NULL) - { - free(attrs->commandKey); - } - free(attrs); -} void backup_session_insert_rpc(char *name, char *commandKey, int status) { - struct attribute *attrs; + FILE *fp; + mxml_node_t *tree, *b; + char *bkp_content; - attrs = calloc(1,sizeof(struct attribute)); - if(attrs != NULL) - { - attrs->id = -1; - attrs->name = strdup(name); - if(status == RPC_QUEUE) - { - attrs->status = strdup("queue"); - } - else if(status == RPC_SEND) - { - attrs->status = strdup("send"); - } - else - { - attrs->status = NULL; - } - if(commandKey != NULL) - { - attrs->commandKey = strdup(commandKey); - } - else - { - attrs->commandKey = NULL; - } - } - backup_session_insert("rpc",NULL,"cwmp",NULL,attrs,2); - if(attrs->name != NULL) - { - free(attrs->name); - } - if(attrs->status != NULL) - { - free(attrs->status); - } - if(attrs->commandKey != NULL) - { - free(attrs->commandKey); - } - free(attrs); -} + pthread_mutex_lock (&mutex_backup_session); + fp = fopen(CWMP_BKP_FILE, "r"); + tree = mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK); + fclose(fp); -void backup_session_insert_acs(char *value) -{ - backup_session_delete ("url", NULL, 3, NULL); - backup_session_insert("url", value, "acs", NULL, NULL, 3); -} + if(strcmp(name,"Inform") == 0) + { + if(status == RPC_QUEUE) + { + b = mxmlFindElement(tree, tree, "rpc", "status", "queue", MXML_DESCEND); + } + if(status == RPC_SEND) + { + b = mxmlFindElement(tree, tree, "rpc", "status", "send", MXML_DESCEND); + } + if(b) + { + goto end; + } + } + b = mxmlFindElement(tree, tree, "cwmp", NULL, NULL, MXML_DESCEND); -void backup_session_delete_event(char *name,char *commandKey, int id, int rpc_status) -{ - struct attribute *attrs; + b = mxmlNewElement(tree, "rpc"); - attrs = calloc(1,sizeof(struct attribute)); - if(attrs != NULL) - { - attrs->id = id; - attrs->name = strdup(name); - if(rpc_status == RPC_QUEUE) - { - attrs->status = strdup("queue"); - } - else if(rpc_status == RPC_SEND) - { - attrs->status = strdup("send"); - } - if(commandKey != NULL) - { - attrs->commandKey = strdup(commandKey); - } - else - { - attrs->commandKey = NULL; - } - } - backup_session_delete ("event", attrs, 3, NULL); - if(attrs->name != NULL) - { - free(attrs->name); - } - if(attrs->status != NULL) - { - free(attrs->status); - } - if(attrs->commandKey != NULL) - { - free(attrs->commandKey); - } - free(attrs); + mxmlElementSetAttr(b, "name", name); + if(status == RPC_QUEUE) + { + mxmlElementSetAttr(b, "status", "queue"); + } + else if(status == RPC_SEND) + { + mxmlElementSetAttr(b, "status", "send"); + } + + if(commandKey != NULL) + { + mxmlElementSetAttr(b, "commandKey", commandKey); + } + bkp_content = mxmlSaveAllocString(tree, MXML_NO_CALLBACK); + fp = fopen(CWMP_BKP_FILE, "w"); + fprintf(fp,"%s",bkp_content); + fclose(fp); +end: + mxmlDelete(tree); + pthread_mutex_unlock (&mutex_backup_session); } void backup_session_delete_rpc(char *name, char *commandKey, int status) { - struct attribute *attrs; + FILE *fp; + mxml_node_t *tree, *b; + char *bkp_content; - attrs = calloc(1,sizeof(struct attribute)); - if(attrs != NULL) - { - attrs->id = -1; - attrs->name = strdup(name); - if(status == RPC_QUEUE) - { - attrs->status = strdup("queue"); - } - else if(status == RPC_SEND) - { - attrs->status = strdup("send"); - } - else - { - attrs->status = NULL; - } - if(commandKey != NULL) - { - attrs->commandKey = strdup(commandKey); - } - else - { - attrs->commandKey = NULL; - } - } - backup_session_delete("rpc",attrs,2, NULL); - if(attrs->name != NULL) - { - free(attrs->name); - } - if(attrs->status != NULL) - { - free(attrs->status); - } - if(attrs->commandKey != NULL) - { - free(attrs->commandKey); - } - free(attrs); + pthread_mutex_lock (&mutex_backup_session); + fp = fopen(CWMP_BKP_FILE, "r"); + tree = mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK); + fclose(fp); + + if(status == RPC_QUEUE) + { + b = mxmlFindElement(tree, tree, "rpc", "status", "queue", MXML_DESCEND); + } + else if(status == RPC_SEND) + { + b = mxmlFindElement(tree, tree, "rpc", "status", "send", MXML_DESCEND); + } + else + { + if(commandKey != NULL) + { + b = mxmlFindElement(tree, tree, "rpc", "commandKey", commandKey, MXML_DESCEND); + } + } + if(b) + { + mxmlRemove(b); + } + bkp_content = mxmlSaveAllocString(tree, MXML_NO_CALLBACK); + fp = fopen(CWMP_BKP_FILE, "w"); + fprintf(fp,"%s",bkp_content); + fclose(fp); + mxmlDelete(tree); + pthread_mutex_unlock (&mutex_backup_session); +} + +void backup_session_insert_event(char *name, char *commandKey, int id, int rpc_status) +{ + FILE *fp; + mxml_node_t *tree, *b, *c; + char tmp[4]; + char *bkp_content; + + pthread_mutex_lock (&mutex_backup_session); + fp = fopen(CWMP_BKP_FILE, "r"); + tree = mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK); + fclose(fp); + + if(rpc_status == RPC_QUEUE) + { + b = mxmlFindElement(tree, tree, "rpc", "status", "queue", MXML_DESCEND); + } + else if(rpc_status == RPC_SEND) + { + b = mxmlFindElement(tree, tree, "rpc", "status", "send", MXML_DESCEND); + } + + sprintf(tmp,"%d",id); + c = mxmlFindElement(b, b, "event", "id", tmp, MXML_DESCEND); + if(c) + { + goto end; + } + + b = mxmlNewElement(b, "event"); + + mxmlElementSetAttr(b, "name", name); + + mxmlElementSetAttr(b, "id", tmp); + + if(commandKey != NULL) + { + mxmlElementSetAttr(b, "commandKey", commandKey); + } + bkp_content = mxmlSaveAllocString(tree, MXML_NO_CALLBACK); + fp = fopen(CWMP_BKP_FILE, "w"); + fprintf(fp,"%s",bkp_content); + fclose(fp); +end: + mxmlDelete(tree); + pthread_mutex_unlock (&mutex_backup_session); +} + +void backup_session_delete_event(char *name,char *commandKey, int id, int rpc_status) +{ + FILE *fp; + mxml_node_t *tree, *b; + char tmp[4]; + char *bkp_content; + + pthread_mutex_lock (&mutex_backup_session); + fp = fopen(CWMP_BKP_FILE, "r"); + tree = mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK); + fclose(fp); + + if(rpc_status == RPC_QUEUE) + { + b = mxmlFindElement(tree, tree, "rpc", "status", "queue", MXML_DESCEND); + } + else if(rpc_status == RPC_SEND) + { + b = mxmlFindElement(tree, tree, "rpc", "status", "send", MXML_DESCEND); + } + if(b) + { + sprintf(tmp,"%d",id); + b = mxmlFindElement(b, b, "event", "id", tmp, MXML_DESCEND); + if(b) + { + mxmlRemove(b); + } + } + bkp_content = mxmlSaveAllocString(tree, MXML_NO_CALLBACK); + fp = fopen(CWMP_BKP_FILE, "w"); + fprintf(fp,"%s",bkp_content); + fclose(fp); + mxmlDelete(tree); + pthread_mutex_unlock (&mutex_backup_session); +} + +void backup_session_insert_parameter(char *name, char *value, char *parent, char *parent_name, int id, char *commandKey) +{ + FILE *fp; + mxml_node_t *tree, *b, *c; + char tmp[4]; + char *bkp_content; + + pthread_mutex_lock (&mutex_backup_session); + fp = fopen(CWMP_BKP_FILE, "r"); + tree = mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK); + fclose(fp); + + if(id==-1) + { + b = mxmlFindElement(tree, tree, parent, "name", parent_name, MXML_DESCEND); + } + else + { + sprintf(tmp,"%d",id); + b = mxmlFindElement(tree, tree, parent, "id", tmp, MXML_DESCEND); + } + if(b) + { + c = mxmlFindElement(b, b, "parameter", "name", name, MXML_DESCEND); + if(c) + { + goto end; + } + b = mxmlNewElement(b, "parameter"); + + mxmlElementSetAttr(b, "name", name); + + if(value != NULL) + { + b = mxmlNewText(b, 0, value); + } + bkp_content = mxmlSaveAllocString(tree, MXML_NO_CALLBACK); + fp = fopen(CWMP_BKP_FILE, "w"); + fprintf(fp,"%s",bkp_content); + fclose(fp); + } +end: + mxmlDelete(tree); + pthread_mutex_unlock (&mutex_backup_session); +} + +void backup_session_insert_acs(char *value) +{ + FILE *fp; + mxml_node_t *tree, *b; + char *bkp_content; + + pthread_mutex_lock (&mutex_backup_session); + fp = fopen(CWMP_BKP_FILE, "r"); + tree = mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK); + fclose(fp); + + b = mxmlFindElement(tree, tree, "acs", NULL, NULL, MXML_DESCEND); + if(!b) + { + b = mxmlNewElement(tree, "acs"); + } + + b = mxmlFindElement(tree, tree, "url", NULL, NULL, MXML_DESCEND); + if(b) + { + mxmlRemove(b); + } + + b = mxmlFindElement(tree, tree, "acs", NULL, NULL, MXML_DESCEND); + + b = mxmlNewElement(b, "url"); + + b = mxmlNewText(b, 0, value); + + bkp_content = mxmlSaveAllocString(tree, MXML_NO_CALLBACK); + fp = fopen(CWMP_BKP_FILE, "w"); + fprintf(fp,"%s",bkp_content); + fclose(fp); + mxmlDelete(tree); + pthread_mutex_unlock (&mutex_backup_session); } void backup_session_move_inform_to_inform_send () { - char line[256]; - char start[256]; - FILE *pFile; - FILE *tmpFile; + FILE *fp; + mxml_node_t *tree, *b; + char *bkp_content; - pthread_mutex_lock (&mutex_backup_session); - start[0] = 0; + pthread_mutex_lock (&mutex_backup_session); + fp = fopen(CWMP_BKP_FILE, "r"); + tree = mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK); + fclose(fp); - pFile = fopen(CWMP_BKP_FILE,"r"); - tmpFile = fopen("tmp.xml","w"); - if(pFile == NULL || tmpFile == NULL) - { - CWMP_LOG(ERROR,"Unable to write in %s file",CWMP_BKP_FILE); - pthread_mutex_unlock (&mutex_backup_session); - exit(EXIT_FAILURE); - } + b = mxmlFindElement(tree, tree, "rpc", "status", "queue", MXML_DESCEND); + if(b) + { + mxmlElementSetAttr(b, "status", "send"); + bkp_content = mxmlSaveAllocString(tree, MXML_NO_CALLBACK); + fp = fopen(CWMP_BKP_FILE, "w"); + fprintf(fp,"%s",bkp_content); + fclose(fp); + } - sprintf(start,"\t\n"); - - while(!feof(pFile)) - { - fgets(line,sizeof(line),pFile); - if(strcmp(line,"\t\n") == 0) - { - break; - } - fputs(line,tmpFile); - } - if(!feof(pFile)) - { - fputs(start,tmpFile); - } - while(!feof(pFile)) - { - fgets(line,sizeof(line),pFile); - fputs(line,tmpFile); - } - fclose(pFile); - fclose(tmpFile); - rename("tmp.xml", CWMP_BKP_FILE); - pthread_mutex_unlock (&mutex_backup_session); + mxmlDelete(tree); + pthread_mutex_unlock (&mutex_backup_session); } void backup_session_move_inform_to_inform_queue () { - char line[256]; - char *buffer; - FILE *pFile; - FILE *tmpFile; - long int max; + FILE *fp; + mxml_node_t *tree, *b; + char *bkp_content; - pthread_mutex_lock (&mutex_backup_session); + pthread_mutex_lock (&mutex_backup_session); + fp = fopen(CWMP_BKP_FILE, "r"); + tree = mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK); + fclose(fp); - - pFile = fopen(CWMP_BKP_FILE,"r"); - - max = get_xml_file_size(pFile); - buffer = calloc(1,max); - - tmpFile = fopen("tmp.xml","w"); - if(pFile == NULL || tmpFile == NULL) - { - CWMP_LOG(ERROR,"Unable to write in %s file",CWMP_BKP_FILE); - exit(EXIT_FAILURE); - } - - while(!feof(pFile)) - { - fgets(line,sizeof(line),pFile); - if(strcmp(line,"\t\n") == 0) - { - break; - } - fputs(line,tmpFile); - } - while(!feof(pFile)) - { - fgets(line,sizeof(line),pFile); - if(strcmp(line,"\t\n") != 0) - { - strcat (buffer,line); - } - else - { - break; - } - } - while(!feof(pFile)) - { - fgets(line,sizeof(line),pFile); - fputs(line,tmpFile); - } - fclose(pFile); - fclose(tmpFile); - rename("tmp.xml", CWMP_BKP_FILE); - pthread_mutex_unlock (&mutex_backup_session); - - if(strlen(buffer) != 0) - { - backup_session_insert_rpc("Inform",NULL,RPC_QUEUE); - } - else - { - return; - } - - pthread_mutex_lock (&mutex_backup_session); - pFile = fopen(CWMP_BKP_FILE,"r"); - tmpFile = fopen("tmp.xml","w"); - if(pFile == NULL || tmpFile == NULL) - { - CWMP_LOG(ERROR,"Unable to write in %s file",CWMP_BKP_FILE); - pthread_mutex_unlock (&mutex_backup_session); - exit(EXIT_FAILURE); - } - - while(!feof(pFile)) - { - fgets(line,sizeof(line),pFile); - fputs(line,tmpFile); - if(strcmp(line,"\t\n") == 0) - { - break; - } - } - if(!feof(pFile)) - { - fputs(buffer,tmpFile); - } - while(!feof(pFile)) - { - fgets(line,sizeof(line),pFile); - fputs(line,tmpFile); - } - fclose(pFile); - fclose(tmpFile); - rename("tmp.xml", CWMP_BKP_FILE); - if(buffer != NULL) - { - free(buffer); - } - pthread_mutex_unlock (&mutex_backup_session); + b = mxmlFindElement(tree, tree, "rpc", "status", "send", MXML_DESCEND); + if(b) + { + mxmlElementSetAttr(b, "status", "queue"); + bkp_content = mxmlSaveAllocString(tree, MXML_NO_CALLBACK); + fp = fopen(CWMP_BKP_FILE, "w"); + fprintf(fp,"%s",bkp_content); + fclose(fp); + } + mxmlDelete(tree); + pthread_mutex_unlock (&mutex_backup_session); } void backup_session_insert_schedule_time(time_t schedule_time,char *commandKey) { - struct attribute *attrs; + FILE *fp; + mxml_node_t *tree, *b; + char time[64]; + char *bkp_content; - attrs = calloc(1,sizeof(struct attribute)); - if(attrs != NULL) - { - attrs->id = schedule_time; - if(commandKey != NULL) - { - attrs->commandKey = strdup(commandKey); - } - else - { - attrs->commandKey = NULL; - } - } - backup_session_insert("scheduledInform",NULL,"cwmp",NULL,NULL,2); - backup_session_insert("scheduledTime", NULL, "scheduledInform", NULL, attrs, 3); - if(attrs->commandKey != NULL) - { - free(attrs->commandKey); - } - free(attrs); + pthread_mutex_lock (&mutex_backup_session); + fp = fopen(CWMP_BKP_FILE, "r"); + tree = mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK); + fclose(fp); + + b = mxmlFindElement(tree, tree, "scheduledInform", NULL, NULL, MXML_DESCEND); + if(!b) + { + b = mxmlNewElement(tree, "scheduledInform"); + } + + b = mxmlNewElement(b, "scheduledTime"); + + if(commandKey != NULL) + { + mxmlElementSetAttr(b, "commandKey", commandKey); + } + else + { + mxmlElementSetAttr(b, "commandKey", ""); + } + sprintf(time,"%li",schedule_time); + mxmlElementSetAttr(b, "time", time); + + bkp_content = mxmlSaveAllocString(tree, MXML_NO_CALLBACK); + fp = fopen(CWMP_BKP_FILE, "w"); + fprintf(fp,"%s",bkp_content); + fclose(fp); + mxmlDelete(tree); + pthread_mutex_unlock (&mutex_backup_session); } void backup_session_delete_schedule_time(time_t schedule_time,char *commandKey) { - struct attribute *attrs; + FILE *fp; + mxml_node_t *tree, *b; + char time[64]; + char *bkp_content; - attrs = calloc(1,sizeof(struct attribute)); - if(attrs != NULL) - { - attrs->id = schedule_time; - if(commandKey != NULL) - { - attrs->commandKey = strdup(commandKey); - } - else - { - attrs->commandKey = NULL; - } - } - backup_session_delete("scheduledTime", attrs, 3, NULL); - if(attrs->commandKey != NULL) - { - free(attrs->commandKey); - } - free(attrs); + pthread_mutex_lock (&mutex_backup_session); + fp = fopen(CWMP_BKP_FILE, "r"); + tree = mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK); + fclose(fp); + + sprintf(time,"%li",schedule_time); + b = mxmlFindElement(tree, tree, "scheduledTime", "time", time, MXML_DESCEND); + + if(b) + { + mxmlRemove(b); + } + + bkp_content = mxmlSaveAllocString(tree, MXML_NO_CALLBACK); + fp = fopen(CWMP_BKP_FILE, "w"); + fprintf(fp,"%s",bkp_content); + fclose(fp); + mxmlDelete(tree); + pthread_mutex_unlock (&mutex_backup_session); } void backup_session_delete_scheduled_inform() { - backup_session_delete("scheduledInform",NULL,2,NULL); + FILE *fp; + mxml_node_t *tree, *b; + char *bkp_content; + + fp = fopen(CWMP_BKP_FILE, "r"); + tree = mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK); + fclose(fp); + + b = mxmlFindElement(tree, tree, "scheduledInform", NULL, NULL, MXML_DESCEND); + + if(b) + { + mxmlRemove(b); + } + + bkp_content = mxmlSaveAllocString(tree, MXML_NO_CALLBACK); + fp = fopen(CWMP_BKP_FILE, "w"); + fprintf(fp,"%s",bkp_content); + fclose(fp); + mxmlDelete(tree); + pthread_mutex_unlock (&mutex_backup_session); } -void backup_session_insert_download(char *commandKey, char *url, time_t id) +void backup_session_insert_download(char *commandKey, char *url, time_t schedule_time) { - struct attribute *attrs; + FILE *fp; + mxml_node_t *tree, *b; + char time[64]; + char *bkp_content; - attrs = calloc(1,sizeof(struct attribute)); - if(attrs != NULL) - { - attrs->id = id; - if(url != NULL) - { - attrs->url = strdup(url); - } - else - { - attrs->url = NULL; - } - if(commandKey != NULL) - { - attrs->commandKey = strdup(commandKey); - } - else - { - attrs->commandKey = NULL; - } - } - backup_session_insert("download",NULL,"cwmp",NULL,attrs,2); - if(attrs->url != NULL) - { - free(attrs->url); - } - if(attrs->commandKey != NULL) - { - free(attrs->commandKey); - } - free(attrs); + pthread_mutex_lock (&mutex_backup_session); + fp = fopen(CWMP_BKP_FILE, "r"); + tree = mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK); + fclose(fp); + + b = mxmlFindElement(tree, tree, "cwmp", NULL, NULL, MXML_DESCEND); + + b = mxmlNewElement(tree, "download"); + + if(commandKey != NULL) + { + mxmlElementSetAttr(b, "commandKey", commandKey); + } + else + { + mxmlElementSetAttr(b, "commandKey", ""); + } + + mxmlElementSetAttr(b, "url", url); + + sprintf(time,"%li",schedule_time); + mxmlElementSetAttr(b, "time", time); + + bkp_content = mxmlSaveAllocString(tree, MXML_NO_CALLBACK); + fp = fopen(CWMP_BKP_FILE, "w"); + fprintf(fp,"%s",bkp_content); + fclose(fp); + mxmlDelete(tree); + pthread_mutex_unlock (&mutex_backup_session); } -void backup_session_delete_download(char *commandKey, char *url, time_t id) +void backup_session_delete_download(char *commandKey, char *url, time_t schedule_time) { - struct attribute *attrs; + FILE *fp; + mxml_node_t *tree, *b; + char time[64]; + char *bkp_content; - attrs = calloc(1,sizeof(struct attribute)); - if(attrs != NULL) - { - attrs->id = id; - if(url != NULL) - { - attrs->url = strdup(url); - } - else - { - attrs->url = NULL; - } - if(commandKey != NULL) - { - attrs->commandKey = strdup(commandKey); - } - else - { - attrs->commandKey = NULL; - } - } - backup_session_delete("download",attrs,2, NULL); - if(attrs->url != NULL) - { - free(attrs->url); - } - if(attrs->commandKey != NULL) - { - free(attrs->commandKey); - } - free(attrs); + pthread_mutex_lock (&mutex_backup_session); + fp = fopen(CWMP_BKP_FILE, "r"); + tree = mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK); + fclose(fp); + + sprintf(time,"%li",schedule_time); + b = mxmlFindElement(tree, tree, "download", "time", time, MXML_DESCEND); + + if(b) + { + mxmlRemove(b); + } + + bkp_content = mxmlSaveAllocString(tree, MXML_NO_CALLBACK); + fp = fopen(CWMP_BKP_FILE, "w"); + fprintf(fp,"%s",bkp_content); + fclose(fp); + mxmlDelete(tree); + pthread_mutex_unlock (&mutex_backup_session); } -void backup_session_insert_parameter_download(char *name, char *value, char *url, int id, char *commandKey) +void backup_session_insert_parameter_download(char *name, char *value, char *url, time_t schedule_time, char *commandKey) { - struct attribute *attrs; + FILE *fp; + mxml_node_t *tree, *b; + char time[64]; + char *bkp_content; - attrs = calloc(1,sizeof(struct attribute)); - if(attrs != NULL) - { - attrs->id = id; - attrs->name = strdup(name); - if(commandKey != NULL) - { - attrs->commandKey = strdup(commandKey); - } - if(url != NULL) - { - attrs->url = strdup(url); - } - } - backup_session_insert("parameter",value,"download",NULL,attrs,3); - if(attrs->name != NULL) - { - free(attrs->name); - } - if(attrs->commandKey != NULL) - { - free(attrs->commandKey); - } - if(attrs->url != NULL) - { - free(attrs->url); - } - free(attrs); + pthread_mutex_lock (&mutex_backup_session); + fp = fopen(CWMP_BKP_FILE, "r"); + tree = mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK); + fclose(fp); + + sprintf(time, "%li", schedule_time); + b = mxmlFindElement(tree, tree, "download", "time", time, MXML_DESCEND); + + if(b) + { + b = mxmlNewElement(b, "parameter"); + + mxmlElementSetAttr(b, "name", name); + + mxmlNewText(b, 0, value); + + bkp_content = mxmlSaveAllocString(tree, MXML_NO_CALLBACK); + fp = fopen(CWMP_BKP_FILE, "w"); + fprintf(fp,"%s",bkp_content); + fclose(fp); + } + + mxmlDelete(tree); + pthread_mutex_unlock (&mutex_backup_session); +} + +int cwmp_load_saved_session(struct cwmp *cwmp, char **acsurl, enum backup_loading load) +{ + int i,j,fault_idx; + char *name = NULL, *commandKey = NULL, *status = NULL; + FILE *pFile; + mxml_node_t *tree, *n, *b, *c; + mxml_attr_t *attr; + struct download *download_request = NULL; + struct event_container *event_container_save = NULL; + struct _cwmp1__TransferComplete *p_soap_cwmp1__TransferComplete = NULL; + struct list_head *ilist = NULL; + struct download *idownload_request = NULL; + char *event_name = NULL; + char *event_commandKey = NULL; + char *event_id = NULL; + int event_idx; + struct schedule_inform *schedule_inform = NULL; + char *scheduled_commandKey = NULL; + time_t scheduled_time; + + pthread_mutex_lock (&mutex_backup_session); + pFile = fopen(CWMP_BKP_FILE,"r+"); + + if(pFile == NULL) + { + pFile = fopen(CWMP_BKP_FILE,"w"); + if(pFile == NULL) + { + CWMP_LOG(ERROR,"Unable to create %s file",CWMP_BKP_FILE); + pthread_mutex_unlock (&mutex_backup_session); + return CWMP_MEM_ERR; + } + fprintf(pFile,"%s",CWMP_BACKUP_SESSION); + fclose(pFile); + pthread_mutex_unlock (&mutex_backup_session); + return CWMP_OK; + } + fclose(pFile); + pthread_mutex_unlock (&mutex_backup_session); + backup_session_move_inform_to_inform_queue (); + pthread_mutex_lock (&mutex_backup_session); + pFile = fopen(CWMP_BKP_FILE, "r"); + tree = mxmlLoadFile(NULL, pFile, MXML_NO_CALLBACK); + fclose(pFile); + + n = tree; + + if (!n) + { + CWMP_LOG(ERROR,"Backup file %s was empty",CWMP_BKP_FILE); + return CWMP_GEN_ERR; + } + + b = n; + + while (b != NULL) + { + if(b->type == MXML_ELEMENT) + { + if(load == ALL || load == ACS) + { + if ((b != NULL) && (strcmp(b->value.element.name, "url") == 0)) + { + if(b->child != NULL) + { + b = mxmlWalkNext(b, n, MXML_DESCEND); + if (b->type == MXML_TEXT) + { + if(acsurl != NULL) + { + if(b->value.text.string != NULL) + { + *acsurl = strdup(b->value.text.string); + } + else + { + *acsurl = NULL; + } + } + } + } + else + { + if(acsurl != NULL) + { + *acsurl = NULL; + } + } + if(load == ACS) + { + break; + } + } + } + if(load == ALL) + { + if ((b != NULL) && (b->type == MXML_ELEMENT) && (strcmp(b->value.element.name, "download") == 0)) + { + download_request = calloc(1,sizeof(struct download)); + for (i = b->value.element.num_attrs, attr = b->value.element.attrs;i > 0;i --, attr ++) + { + if(strcmp(attr->name,"commandKey") == 0) + { + download_request->CommandKey = strdup(attr->value); + } + if(strcmp(attr->name,"url") == 0) + { + download_request->URL = strdup(attr->value); + } + if(strcmp(attr->name,"time") == 0) + { + download_request->scheduled_time = atol(attr->value); + } + } + while ((b != NULL)&&(b = mxmlWalkNext(b, n, MXML_DESCEND)) && (strcmp(b->parent->value.element.name,"download") == 0)) + { + if (b->type == MXML_ELEMENT) + { + for (i = b->value.element.num_attrs, attr = b->value.element.attrs;i > 0;i --, attr ++) + { + if(strcmp(attr->name,"name") == 0) + { + if((attr->value!= NULL) && (strcmp(attr->value,"FileType") == 0)) + { + b = mxmlWalkNext(b, n, MXML_DESCEND); + if (b->type == MXML_TEXT) + { + download_request->FileType = strdup(b->value.text.string); + } + break; + } + else if ((attr->value!= NULL) && (strcmp(attr->value,"Username") == 0)) + { + b = mxmlWalkNext(b, n, MXML_DESCEND); + if (b->type == MXML_TEXT) + { + download_request->Username = strdup(b->value.text.string); + } + break; + } + else if ((attr->value!= NULL) && (strcmp(attr->value,"Password") == 0)) + { + b = mxmlWalkNext(b, n, MXML_DESCEND); + if (b->type == MXML_TEXT) + { + download_request->Password = strdup(b->value.text.string); + } + break; + } + } + } + } + } + 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); + count_download_queue++; + } + if ((b != NULL) && (b->type == MXML_ELEMENT) && (strcmp(b->value.element.name, "rpc") == 0)) + { + for (i = b->value.element.num_attrs, attr = b->value.element.attrs;i > 0;i --, attr ++) + { + if(strcmp(attr->name,"name") == 0) + { + name = strdup(attr->value); + } + if(strcmp(attr->name,"commandKey") == 0) + { + commandKey = strdup(attr->value); + } + if(strcmp(attr->name,"status") == 0) + { + status = strdup(attr->value); + } + } + if(strcmp(name,"TransferComplete") == 0) + { + p_soap_cwmp1__TransferComplete = cwmp_set_data_rpc_acs_transferComplete (); + p_soap_cwmp1__TransferComplete->CommandKey = strdup(commandKey); + while ((b != NULL)&&(b = mxmlWalkNext(b, n, MXML_DESCEND))&&(strcmp(b->parent->value.element.name,"rpc") == 0)&&(strcmp(name,"TransferComplete") == 0)) + { + if ((b->type == MXML_ELEMENT) && (strcmp(b->value.element.name,"parameter") == 0)) + { + for (i = b->value.element.num_attrs, attr = b->value.element.attrs;i > 0;i --, attr ++) + { + if(strcmp(attr->name,"name") == 0) + { + if(strcmp(attr->value,"FaultCode") == 0) + { + b = mxmlWalkNext(b, n, MXML_DESCEND); + if (b->type == MXML_TEXT) + { + p_soap_cwmp1__TransferComplete->FaultStruct = calloc(1,sizeof(struct cwmp1__FaultStruct)); + p_soap_cwmp1__TransferComplete->FaultStruct->FaultCode = strdup(b->value.text.string); + for(j = 0; j < FAULT_CPE_ARRAY_SIZE; j++) + { + if((FAULT_CPE_ARRAY[j].CODE != NULL)&&(strcmp(FAULT_CPE_ARRAY[j].CODE, b->value.text.string) == 0)) + { + fault_idx = j; + break; + } + } + p_soap_cwmp1__TransferComplete->FaultStruct->FaultString = strdup(FAULT_CPE_ARRAY[fault_idx].DESCRIPTION); + } + break; + } + else if(strcmp(attr->value,"StartTime") == 0) + { + b = mxmlWalkNext(b, n, MXML_DESCEND); + if (b->type == MXML_TEXT) + { + p_soap_cwmp1__TransferComplete->StartTime = atol(b->value.text.string); + } + break; + } + else if(strcmp(attr->value,"CompleteTime") == 0) + { + b = mxmlWalkNext(b, n, MXML_DESCEND); + if (b->type == MXML_TEXT) + { + p_soap_cwmp1__TransferComplete->CompleteTime = atol(b->value.text.string); + } + break; + } + } + } + } + } + cwmp_root_cause_TransferComplete (cwmp); + } + if((strcmp(name,"Inform") == 0) && (strcmp(status,"queue") == 0)) + { + while ((b != NULL) && (b = mxmlWalkNext(b, n, MXML_DESCEND)) && (b->type == MXML_ELEMENT) && (strcmp(b->value.element.name, "event") == 0) && (strcmp(b->parent->value.element.name, "rpc") == 0)) + { + for (i = b->value.element.num_attrs, attr = b->value.element.attrs;i > 0;i --, attr ++) + { + if(strcmp(attr->name,"name") == 0) + { + event_name = strdup(attr->value); + } + if(strcmp(attr->name,"commandKey") == 0) + { + event_commandKey = strdup(attr->value); + } + if(strcmp(attr->name,"id") == 0) + { + event_id = strdup(attr->value); + } + } + for (j = 0; j< COUNT_EVENT; j++) + { + if(strcmp(event_name,EVENT_CONST[j].CODE) == 0) + { + event_idx = j; + break; + } + } + if ((cwmp->env.boot != CWMP_START_BOOT) || (EVENT_CONST[event_idx].RETRY & EVENT_RETRY_AFTER_REBOOT)) + { + event_container_save = cwmp_add_event_container (cwmp, event_idx, event_commandKey); + if(event_container_save != NULL) + { + event_container_save->id = atoi(event_id); + } + } + while ((b != NULL)&&(b = mxmlWalkNext(b, n, MXML_DESCEND))&&(strcmp(b->parent->value.element.name,"event") == 0)) + { + if ((b->type == MXML_ELEMENT) && (strcmp(b->value.element.name,"parameter") == 0)) + { + for (i = b->value.element.num_attrs, attr = b->value.element.attrs;i > 0;i --, attr ++) + { + if(strcmp(attr->name,"name") == 0) + { + if(event_container_save != NULL) + { + cwmp_add_parameter_container (cwmp,event_container_save, attr->value, NULL, NULL); + } + } + } + } + } + if ((b != NULL)&&(b->type == MXML_ELEMENT)&&(strcmp(b->value.element.name,"event") == 0)) + { + b = mxmlWalkPrev(b, n, MXML_DESCEND); + } + if(event_id != NULL) + { + free(event_id); + event_id = NULL; + } + if(event_commandKey != NULL) + { + free(event_commandKey); + event_commandKey = NULL; + } + if(event_name != NULL) + { + free(event_name); + event_name = NULL; + } + } + } + if(name != NULL) + { + free(name); + name = NULL; + } + if(status != NULL) + { + free(status); + status = NULL; + } + if(commandKey != NULL) + { + free(commandKey); + commandKey = NULL; + } + } + if ((b != NULL) && (b->type == MXML_ELEMENT) && (strcmp(b->value.element.name, "scheduledTime") == 0)) + { + for (i = b->value.element.num_attrs, attr = b->value.element.attrs;i > 0;i --, attr ++) + { + if(strcmp(attr->name,"commandKey") == 0) + { + scheduled_commandKey = strdup(attr->value); + } + if(strcmp(attr->name,"time") == 0) + { + scheduled_time = atol(attr->value); + } + } + 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 = scheduled_commandKey; + schedule_inform->scheduled_time = scheduled_time; + list_add (&(schedule_inform->list), ilist->prev); + } + } + } + } + b = mxmlWalkNext(b, n, MXML_DESCEND); + } + + mxmlDelete(tree); + pthread_mutex_unlock (&mutex_backup_session); + return CWMP_OK; } diff --git a/src/cwmp.c b/src/cwmp.c index f21482a..9faf8c7 100644 --- a/src/cwmp.c +++ b/src/cwmp.c @@ -27,6 +27,7 @@ struct cwmp cwmp_main; static pthread_mutex_t thread_sync_mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t thread_sync_cond = PTHREAD_COND_INITIALIZER; static bool thread_sync_wait = TRUE; +extern pthread_mutex_t mutex_backup_session; struct rpc_acs *cwmp_add_session_rpc_acs_inform (struct session *session); struct rpc_cpe *cwmp_soap_receive_rpc_cpe (struct cwmp *cwmp,struct session *session); @@ -611,6 +612,8 @@ int main(int argc, char **argv) pthread_t download_thread; pthread_t ubus_thread; + pthread_mutex_init(&mutex_backup_session, NULL); + if (error = cwmp_init(argc, argv, cwmp)) { return error;