fix and optimize download RPC

This commit is contained in:
zribiahmed 2013-02-11 13:49:27 +00:00
parent 6a7fe0bd59
commit 9a8b4b4e89
7 changed files with 68 additions and 124 deletions

View file

@ -595,7 +595,8 @@ void load_download(mxml_node_t *tree,struct cwmp *cwmp)
} }
} }
list_add (&(download_request->list), ilist->prev); list_add (&(download_request->list), ilist->prev);
count_download_queue++; if(download_request->scheduled_time != 0)
count_download_queue++;
} }
void load_transfer_complete(mxml_node_t *tree,struct cwmp *cwmp) void load_transfer_complete(mxml_node_t *tree,struct cwmp *cwmp)

View file

@ -57,13 +57,13 @@ inline void external_free_list_value_change()
parameter_container_delete_all(&external_list_value_change); parameter_container_delete_all(&external_list_value_change);
} }
void external_downloadResp (char *fault_code) void external_downloadFaultResp (char *fault_code)
{ {
FREE(external_MethodFault); FREE(external_MethodFault);
external_MethodFault = fault_code ? strdup(fault_code) : NULL; external_MethodFault = fault_code ? strdup(fault_code) : NULL;
} }
void external_fetch_downloadResp (char **fault) void external_fetch_downloadFaultResp (char **fault)
{ {
*fault = external_MethodFault; *fault = external_MethodFault;
external_MethodFault = NULL; external_MethodFault = NULL;
@ -412,9 +412,8 @@ int external_simple(char *arg)
return 0; return 0;
} }
int external_download(char *url, char *size, char *type, char *user, char *pass, time_t scheduled_time) int external_download(char *url, char *size, char *type, char *user, char *pass)
{ {
char delay[256];
CWMP_LOG(INFO,"executing download url '%s'", url); CWMP_LOG(INFO,"executing download url '%s'", url);
if ((uproc.pid = fork()) == -1) if ((uproc.pid = fork()) == -1)
@ -445,9 +444,6 @@ int external_download(char *url, char *size, char *type, char *user, char *pass,
argv[i++] = "--pass"; argv[i++] = "--pass";
argv[i++] = pass; argv[i++] = pass;
} }
sprintf(delay,"%ld",scheduled_time);
argv[i++] = "--delay";
argv[i++] = delay;
argv[i++] = NULL; argv[i++] = NULL;
execvp(argv[0], (char **) argv); execvp(argv[0], (char **) argv);

View file

@ -26,8 +26,8 @@ extern pthread_mutex_t external_mutex_value_change;
extern struct list_head external_list_value_change; extern struct list_head external_list_value_change;
extern struct list_head external_list_parameter; extern struct list_head external_list_parameter;
void external_downloadResp (char *fault_code); void external_downloadFaultResp (char *fault_code);
void external_fetch_downloadResp (char **fault_code); void external_fetch_downloadFaultResp (char **fault_code);
void external_setParamValRespStatus (char *status); void external_setParamValRespStatus (char *status);
void external_fetch_setParamValRespStatus (char **status); void external_fetch_setParamValRespStatus (char **status);
void external_setParamAttrResp (char *status, char *fault); void external_setParamAttrResp (char *status, char *fault);
@ -42,7 +42,7 @@ int external_get_action_execute();
int external_set_action_write(char *action, char *name, char *value, char *change); int external_set_action_write(char *action, char *name, char *value, char *change);
int external_set_action_execute(char *action); int external_set_action_execute(char *action);
int external_simple(char *arg); int external_simple(char *arg);
int external_download(char *url, char *size, char *type, char *user, char *pass, time_t scheduled_time); int external_download(char *url, char *size, char *type, char *user, char *pass);
int external_apply_download(char *type); int external_apply_download(char *type);
void external_add_list_paramameter(char *param_name, char *param_data, char *param_type, char *fault_code); void external_add_list_paramameter(char *param_name, char *param_data, char *param_type, char *fault_code);
void external_free_list_parameter(); void external_free_list_parameter();

View file

@ -19,9 +19,8 @@ DEFINE_boolean 'force' false 'force getting values for certain parameters' 'f'
DEFINE_string 'url' '' 'file to download [download only]' 'u' DEFINE_string 'url' '' 'file to download [download only]' 'u'
DEFINE_string 'size' '' 'size of file to download [download only]' 's' DEFINE_string 'size' '' 'size of file to download [download only]' 's'
DEFINE_string 'type' '' 'type of file to download [download only]' 't' DEFINE_string 'type' '' 'type of file to download [download only]' 't'
DEFINE_string 'user' '' 'username for downloading file [download only]' 'a' DEFINE_string 'user' '' 'username for downloading file [download only]' 'U'
DEFINE_string 'pass' '' 'password for downloading file [download only]' 'p' DEFINE_string 'pass' '' 'password for downloading file [download only]' 'P'
DEFINE_string 'delay' '' 'scheduled_time for downloading file [download only]' 'y'
FLAGS_HELP=`cat << EOF FLAGS_HELP=`cat << EOF
USAGE: $0 [flags] command [parameter] [values] USAGE: $0 [flags] command [parameter] [values]
@ -441,10 +440,6 @@ if [ "$action" = "download" ]; then
rm /tmp/firmware_upgrade_image_last_valid 2> /dev/null rm /tmp/firmware_upgrade_image_last_valid 2> /dev/null
mv /tmp/firmware_upgrade_image /tmp/firmware_upgrade_image_last_valid 2> /dev/null mv /tmp/firmware_upgrade_image /tmp/firmware_upgrade_image_last_valid 2> /dev/null
freecwmp_fault_output "" "$FAULT_CPE_NO_FAULT" freecwmp_fault_output "" "$FAULT_CPE_NO_FAULT"
if [ "${FLAGS_delay}" = "0" ];then
echo "/bin/sh /usr/sbin/freecwmp apply download --type ${FLAGS_type}" > /tmp/end_session.sh
ubus ${UBUS_SOCKET:+-s $UBUS_SOCKET} call tr069 command '{ "command": "action_end_session" }' 2> /dev/null
fi
fi fi
else else
let fault_code=$fault_code+$FAULT_CPE_DOWNLOAD_FAIL_FILE_CORRUPTED let fault_code=$fault_code+$FAULT_CPE_DOWNLOAD_FAIL_FILE_CORRUPTED
@ -454,17 +449,9 @@ if [ "$action" = "download" ]; then
elif [ "${FLAGS_type}" = "2" ];then elif [ "${FLAGS_type}" = "2" ];then
mv /tmp/freecwmp_download /tmp/web_content.ipk 2> /dev/null mv /tmp/freecwmp_download /tmp/web_content.ipk 2> /dev/null
freecwmp_fault_output "" "$FAULT_CPE_NO_FAULT" freecwmp_fault_output "" "$FAULT_CPE_NO_FAULT"
if [ "${FLAGS_delay}" = "0" ];then
echo "/bin/sh /usr/sbin/freecwmp apply download --type ${FLAGS_type}" > /tmp/end_session.sh
ubus ${UBUS_SOCKET:+-s $UBUS_SOCKET} call tr069 command '{ "command": "action_end_session" }' 2> /dev/null
fi
elif [ "${FLAGS_type}" = "3" ];then elif [ "${FLAGS_type}" = "3" ];then
mv /tmp/freecwmp_download /tmp/vendor_configuration_file.cfg 2> /dev/null mv /tmp/freecwmp_download /tmp/vendor_configuration_file.cfg 2> /dev/null
freecwmp_fault_output "" "$FAULT_CPE_NO_FAULT" freecwmp_fault_output "" "$FAULT_CPE_NO_FAULT"
if [ "${FLAGS_delay}" = "0" ];then
echo "/bin/sh /usr/sbin/freecwmp apply download --type ${FLAGS_type}" > /tmp/end_session.sh
ubus ${UBUS_SOCKET:+-s $UBUS_SOCKET} call tr069 command '{ "command": "action_end_session" }' 2> /dev/null
fi
else else
let fault_code=$fault_code+$FAULT_CPE_DOWNLOAD_FAILURE let fault_code=$fault_code+$FAULT_CPE_DOWNLOAD_FAILURE
freecwmp_fault_output "" "$fault_code" freecwmp_fault_output "" "$fault_code"

View file

@ -94,7 +94,7 @@ freecwmp_fault_output() {
ubus ${UBUS_SOCKET:+-s $UBUS_SOCKET} call tr069 SetParameterAttributes '{"success": "'$success'", "fault_code": "'$fault_code'"}' 2> /dev/null ubus ${UBUS_SOCKET:+-s $UBUS_SOCKET} call tr069 SetParameterAttributes '{"success": "'$success'", "fault_code": "'$fault_code'"}' 2> /dev/null
;; ;;
*download) *download)
ubus ${UBUS_SOCKET:+-s $UBUS_SOCKET} call tr069 download '{"fault_code": "'$fault_code'"}' 2> /dev/null ubus ${UBUS_SOCKET:+-s $UBUS_SOCKET} call tr069 downloadFault '{"fault_code": "'$fault_code'"}' 2> /dev/null
;; ;;
esac esac
} }
@ -430,7 +430,7 @@ freecwmp_execute_functions()
freecwmp_check_image() freecwmp_check_image()
{ {
. /etc/functions.sh; include /lib/upgrade; platform_check_image /tmp/freecwmp_download . /etc/functions.sh; include /lib/upgrade; platform_check_image /tmp/firmware_upgrade_image
return $? return $?
} }

View file

@ -62,27 +62,27 @@ static enum download_fault {
__DOWNLOAD_MAX __DOWNLOAD_MAX
}; };
static const struct blobmsg_policy download_policy[] = { static const struct blobmsg_policy download_fault_policy[] = {
[DOWNLOAD_FAULT] = { .name = "fault_code", .type = BLOBMSG_TYPE_STRING }, [DOWNLOAD_FAULT] = { .name = "fault_code", .type = BLOBMSG_TYPE_STRING },
}; };
static int static int
freecwmpd_handle_download(struct ubus_context *ctx, struct ubus_object *obj, freecwmpd_handle_downloadFault(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct ubus_request_data *req, const char *method,
struct blob_attr *msg) struct blob_attr *msg)
{ {
int tmp; int tmp;
struct blob_attr *tb[__DOWNLOAD_MAX]; struct blob_attr *tb[__DOWNLOAD_MAX];
blobmsg_parse(download_policy, ARRAY_SIZE(download_policy), tb, blobmsg_parse(download_fault_policy, ARRAY_SIZE(download_fault_policy), tb,
blob_data(msg), blob_len(msg)); blob_data(msg), blob_len(msg));
if (!tb[DOWNLOAD_FAULT]) if (!tb[DOWNLOAD_FAULT])
return UBUS_STATUS_INVALID_ARGUMENT; return UBUS_STATUS_INVALID_ARGUMENT;
CWMP_LOG(INFO,"triggered ubus download %s", blobmsg_data(tb[DOWNLOAD_FAULT])); CWMP_LOG(INFO,"triggered ubus downloadFault %s", blobmsg_data(tb[DOWNLOAD_FAULT]));
external_downloadResp (blobmsg_data(tb[DOWNLOAD_FAULT])); external_downloadFaultResp (blobmsg_data(tb[DOWNLOAD_FAULT]));
return 0; return 0;
} }
@ -432,7 +432,7 @@ freecwmpd_handle_delObject(struct ubus_context *ctx, struct ubus_object *obj,
static const struct ubus_method freecwmp_methods[] = { static const struct ubus_method freecwmp_methods[] = {
UBUS_METHOD("notify", freecwmpd_handle_notify, notify_policy), UBUS_METHOD("notify", freecwmpd_handle_notify, notify_policy),
UBUS_METHOD("download", freecwmpd_handle_download, download_policy), UBUS_METHOD("downloadFault", freecwmpd_handle_downloadFault, download_fault_policy),
UBUS_METHOD("GetParameterValues", freecwmpd_handle_getParamValues, getParamValues_policy), UBUS_METHOD("GetParameterValues", freecwmpd_handle_getParamValues, getParamValues_policy),
UBUS_METHOD("SetParameterValuesFault", freecwmpd_handle_setParamValuesFault, setParamValuesFault_policy), UBUS_METHOD("SetParameterValuesFault", freecwmpd_handle_setParamValuesFault, setParamValuesFault_policy),
UBUS_METHOD("SetParameterValuesStatus", freecwmpd_handle_setParamValuesStatus, setParamValuesStatus_policy), UBUS_METHOD("SetParameterValuesStatus", freecwmpd_handle_setParamValuesStatus, setParamValuesStatus_policy),

140
src/xml.c
View file

@ -1770,19 +1770,12 @@ int cwmp_launch_download(struct download *pdownload, struct transfer_complete **
download_startTime = mix_get_time(); download_startTime = mix_get_time();
if(pdownload->scheduled_time != 0) bkp_session_delete_download(pdownload);
{ bkp_session_save();
bkp_session_delete_download(pdownload);
bkp_session_save();
}
sprintf(file_size,"%d",pdownload->file_size); sprintf(file_size,"%d",pdownload->file_size);
if(external_download(pdownload->url, file_size, pdownload->file_type, pdownload->username, pdownload->password, pdownload->scheduled_time)) external_download(pdownload->url, file_size, pdownload->file_type, pdownload->username, pdownload->password);
{ external_fetch_downloadFaultResp(&fault_code);
error = FAULT_CPE_DOWNLOAD_FAILURE;
}
external_fetch_downloadResp(&fault_code);
if(fault_code != NULL) if(fault_code != NULL)
{ {
@ -1810,7 +1803,7 @@ int cwmp_launch_download(struct download *pdownload, struct transfer_complete **
p->command_key = strdup(pdownload->command_key); p->command_key = strdup(pdownload->command_key);
p->start_time = download_startTime; p->start_time = download_startTime;
p->complete_time = mix_get_time(); p->complete_time = mix_get_time();
if(error) if(error != FAULT_CPE_NO_FAULT)
{ {
p->fault_code = error; p->fault_code = error;
} }
@ -1839,7 +1832,10 @@ void *thread_cwmp_rpc_cpe_download (void *v)
{ {
pdownload = list_entry(list_download.next,struct download, list); pdownload = list_entry(list_download.next,struct download, list);
current_time = time(NULL); current_time = time(NULL);
timeout = current_time - pdownload->scheduled_time; if(pdownload->scheduled_time != 0)
timeout = current_time - pdownload->scheduled_time;
else
timeout = 0;
if((timeout >= 0)&&(timeout > time_of_grace)) if((timeout >= 0)&&(timeout > time_of_grace))
{ {
pthread_mutex_lock (&mutex_download); pthread_mutex_lock (&mutex_download);
@ -1859,7 +1855,8 @@ void *thread_cwmp_rpc_cpe_download (void *v)
cwmp_root_cause_TransferComplete (cwmp,ptransfer_complete); cwmp_root_cause_TransferComplete (cwmp,ptransfer_complete);
} }
list_del (&(pdownload->list)); list_del (&(pdownload->list));
count_download_queue--; if(pdownload->scheduled_time != 0)
count_download_queue--;
cwmp_free_download_request(pdownload); cwmp_free_download_request(pdownload);
pthread_mutex_unlock (&mutex_download); pthread_mutex_unlock (&mutex_download);
} }
@ -1873,7 +1870,7 @@ void *thread_cwmp_rpc_cpe_download (void *v)
cwmp_root_cause_TransferComplete (cwmp,ptransfer_complete); cwmp_root_cause_TransferComplete (cwmp,ptransfer_complete);
} }
external_apply_download(pdownload->file_type); external_apply_download(pdownload->file_type);
external_fetch_downloadResp(&fault_code); external_fetch_downloadFaultResp(&fault_code);
if(fault_code != NULL) if(fault_code != NULL)
{ {
if(strcmp(fault_code,"0") != 0) if(strcmp(fault_code,"0") != 0)
@ -1898,7 +1895,8 @@ void *thread_cwmp_rpc_cpe_download (void *v)
pthread_cond_signal (&(cwmp->threshold_session_send)); pthread_cond_signal (&(cwmp->threshold_session_send));
pthread_mutex_lock (&mutex_download); pthread_mutex_lock (&mutex_download);
list_del (&(pdownload->list)); list_del (&(pdownload->list));
count_download_queue--; if(pdownload->scheduled_time != 0)
count_download_queue--;
cwmp_free_download_request(pdownload); cwmp_free_download_request(pdownload);
pthread_mutex_unlock (&mutex_download); pthread_mutex_unlock (&mutex_download);
continue; continue;
@ -1951,7 +1949,8 @@ int cwmp_scheduledDownload_remove_all()
download = list_entry(list_download.next,struct download, list); download = list_entry(list_download.next,struct download, list);
list_del (&(download->list)); list_del (&(download->list));
bkp_session_delete_download(download); bkp_session_delete_download(download);
count_download_queue--; if(download->scheduled_time != 0)
count_download_queue--;
cwmp_free_download_request(download); cwmp_free_download_request(download);
} }
pthread_mutex_unlock (&mutex_download); pthread_mutex_unlock (&mutex_download);
@ -2109,86 +2108,47 @@ int cwmp_handle_rpc_cpe_download(struct session *session, struct rpc *rpc)
if(error == FAULT_CPE_NO_FAULT) if(error == FAULT_CPE_NO_FAULT)
{ {
if(download_delay == 0) pthread_mutex_lock (&mutex_download);
{ if(download_delay != 0)
bkp_session_insert_download(download);
bkp_session_save();
error = cwmp_launch_download(download,&ptransfer_complete);
if(error != FAULT_CPE_NO_FAULT)
{
if(cwmp_root_cause_TransferComplete (&cwmp_main, ptransfer_complete) != CWMP_OK)
{
cwmp_free_download_request(download);
return -1;
}
}
// else
// {
// external_apply_download(download->file_type);
// external_fetch_downloadResp(&fault_code);
// if(fault_code != NULL)
// {
// if(strcmp(fault_code,"0") != 0)
// {
// for(i=0;i<__FAULT_CPE_MAX;i++)
// {
// if(strcmp(FAULT_CPE_ARRAY[i].CODE,fault_code) == 0)
// {
// error = i;
// break;
// }
// }
// }
// free(fault_code);
// bkp_session_delete_transfer_complete(ptransfer_complete);
// ptransfer_complete->fault_code = atoi(fault_code);
// bkp_session_insert_transfer_complete(ptransfer_complete);
// bkp_session_save();
// cwmp_root_cause_TransferComplete (&cwmp_main,ptransfer_complete);
// }
// }
cwmp_free_download_request(download);
}
else
{
pthread_mutex_lock (&mutex_download);
scheduled_time = time(NULL) + download_delay; scheduled_time = time(NULL) + download_delay;
list_for_each(ilist,&(list_download)) list_for_each(ilist,&(list_download))
{
idownload = list_entry(ilist,struct download, list);
if (idownload->scheduled_time == scheduled_time)
{ {
idownload = list_entry(ilist,struct download, list); pthread_mutex_unlock (&mutex_download);
if (idownload->scheduled_time == scheduled_time) return 0;
{
pthread_mutex_unlock (&mutex_download);
return 0;
}
if (idownload->scheduled_time > scheduled_time)
{
cond_signal = true;
break;
}
} }
list_add (&(download->list), ilist->prev); if (idownload->scheduled_time > scheduled_time)
{
cond_signal = true;
break;
}
}
list_add (&(download->list), ilist->prev);
if(download_delay != 0)
{
count_download_queue++; count_download_queue++;
download->scheduled_time = scheduled_time; download->scheduled_time = scheduled_time;
bkp_session_insert_download(download); }
bkp_session_save(); bkp_session_insert_download(download);
CWMP_LOG(INFO,"Download will start in %us",download_delay); bkp_session_save();
if (cond_signal) CWMP_LOG(INFO,"Download will start in %us",download_delay);
if (cond_signal)
{
pthread_cond_signal(&threshold_download);
}
pthread_mutex_unlock (&mutex_download);
if (!thread_download_is_working)
{
thread_download_is_working = true;
error = pthread_create(&download_thread, NULL, &thread_cwmp_rpc_cpe_download, (void *)&cwmp_main);
if (error<0)
{ {
pthread_cond_signal(&threshold_download); CWMP_LOG(ERROR,"Error when creating the download thread!");
} thread_download_is_working = false;
pthread_mutex_unlock (&mutex_download); return -1;
if (!thread_download_is_working)
{
thread_download_is_working = true;
error = pthread_create(&download_thread, NULL, &thread_cwmp_rpc_cpe_download, (void *)&cwmp_main);
if (error<0)
{
CWMP_LOG(ERROR,"Error when creating the download thread!");
thread_download_is_working = false;
return -1;
}
} }
} }
} }