diff --git a/src/download.c b/src/download.c index 2dda9b2..c0f73bf 100644 --- a/src/download.c +++ b/src/download.c @@ -111,7 +111,10 @@ char *download_file_task_function(char *task) int download_file_in_subprocess(const char *file_path, const char *url, const char *username, const char *password) { - subprocess_start(download_file_task_function); + if (CWMP_OK != subprocess_start(download_file_task_function)) { + CWMP_LOG(ERROR, "Failed to spawn subprocess to start download"); + return 500; + } if (url == NULL) { CWMP_LOG(ERROR, "download %s: url is null"); diff --git a/src/http.c b/src/http.c index 4618fce..99a9590 100644 --- a/src/http.c +++ b/src/http.c @@ -263,14 +263,36 @@ error: return -1; } +static void inform_status_check_cb(struct ubus_request *req, int type __attribute__((unused)), struct blob_attr *msg) +{ + if (msg == NULL) { + CWMP_LOG(ERROR, "inform ubus call resp msg is null"); + return; + } + + int *status = (int *)req->priv; + const struct blobmsg_policy p[2] = { { "status", BLOBMSG_TYPE_INT32 }, { "info", BLOBMSG_TYPE_STRING } }; + struct blob_attr *tb[2] = { NULL, NULL }; + blobmsg_parse(p, 2, tb, blobmsg_data(msg), blobmsg_len(msg)); + + *status = tb[0] ? blobmsg_get_u32(tb[0]) : -1; +} + static void http_success_cr(void) { CWMP_LOG(INFO, "Connection Request triggering ..."); + int status = -1, retry = 0, rc = -1; struct blob_buf b = { 0 }; memset(&b, 0, sizeof(struct blob_buf)); blob_buf_init(&b, 0); - icwmp_ubus_invoke("tr069", "inform", b.head, NULL, NULL); + while ((rc < 0 || status != 1) && retry < 5) { + rc = icwmp_ubus_invoke("tr069", "inform", b.head, inform_status_check_cb, &status); + retry = retry + 1; + } + blob_buf_free(&b); + if (rc < 0 || status != 1) + CWMP_LOG(ERROR, "Failed to send Inform message after 5 retry"); } static void http_cr_new_client(int client, bool service_available) @@ -495,8 +517,7 @@ http_end: fclose(fp); } close(client); - } - else { + } else { CWMP_LOG(INFO, "Receive Connection Request: Return 401 Unauthorized"); if (fp) { fputs("HTTP/1.1 401 Unauthorized\r\n", fp); diff --git a/src/session.c b/src/session.c index 4e005d9..70cc3fd 100644 --- a/src/session.c +++ b/src/session.c @@ -316,21 +316,34 @@ void rpc_exit() FREE(cwmp_main->session->rpc_cpe); } +static void schedule_session_retry(void) +{ + cwmp_main->retry_count_session++; + int t = cwmp_get_retry_interval(0); + CWMP_LOG(INFO, "Retry session, retry count = %d, retry in %ds", cwmp_main->retry_count_session, t); + cwmp_uci_reinit(); + cwmp_config_load(); + trigger_periodic_notify_check(); + cwmp_uci_exit(); + + if (!cwmp_main->session->session_status.is_heartbeat) { + set_cwmp_session_status(SESSION_FAILURE, t); + uloop_timeout_set(&retry_session_timer, 1000 * t); + } else { + uloop_timeout_cancel(&heartbeat_session_timer); + uloop_timeout_set(&heartbeat_session_timer, 1000 * t); + } +} + void start_cwmp_session() { - int t, error; + int error; char *exec_download = NULL; uloop_timeout_cancel(&check_notify_timer); if (cwmp_session_init() != CWMP_OK) { CWMP_LOG(ERROR, "Not able to init a CWMP session"); - t = cwmp_get_retry_interval(0); - CWMP_LOG(INFO, "Retry session, retry count = %d, retry in %ds", cwmp_main->retry_count_session, t); - cwmp_uci_reinit(); - set_cwmp_session_status(SESSION_FAILURE, t); - cwmp_config_load(); - trigger_periodic_notify_check(); - cwmp_uci_exit(); + schedule_session_retry(); return; } @@ -343,6 +356,8 @@ void start_cwmp_session() if (is_ipv6_status_changed()) { if (icwmp_check_http_connection() != CWMP_OK || cwmp_stop) { CWMP_LOG(INFO, "Failed to check http connection"); + if (!cwmp_stop) + schedule_session_retry(); return; } } @@ -397,9 +412,11 @@ void start_cwmp_session() } if (cwmp_main->session->error == CWMP_RETRY_SESSION && (!list_empty(&(cwmp_main->session->events)) || (list_empty(&(cwmp_main->session->events)) && cwmp_main->cwmp_cr_event == 0))) { //CWMP Retry session + cwmp_uci_reinit(); cwmp_config_load(); + cwmp_uci_exit(); cwmp_main->retry_count_session++; - t = cwmp_get_retry_interval(0); + int t = cwmp_get_retry_interval(0); CWMP_LOG(INFO, "Retry session, retry count = %d, retry in %ds", cwmp_main->retry_count_session, t); if (!cwmp_main->session->session_status.is_heartbeat) { set_cwmp_session_status(SESSION_FAILURE, t); diff --git a/src/subprocess.c b/src/subprocess.c index 2db07c1..7c7363e 100644 --- a/src/subprocess.c +++ b/src/subprocess.c @@ -94,7 +94,7 @@ int subprocess_start(task_function task_fun) struct blob_buf bbuf; memset(&bbuf, 0, sizeof(struct blob_buf)); blob_buf_init(&bbuf, 0); - blobmsg_add_string(&bbuf, "ret", to_child); + blobmsg_add_string(&bbuf, "ret", to_child ? to_child : "500"); char *to_child_json = blobmsg_format_json(bbuf.head, true); write(pipefd2[1], to_child_json, strlen(to_child_json)+1); FREE(to_child); diff --git a/src/ubus_utils.c b/src/ubus_utils.c index 605c6da..7e51b59 100644 --- a/src/ubus_utils.c +++ b/src/ubus_utils.c @@ -269,7 +269,7 @@ static int icwmp_inform_event(struct blob_buf *bb, char *event) int event_code = cwmp_get_int_event_code(event); if (event_code != -1) { if (cwmp_main->session->session_status.last_status == SESSION_RUNNING) { - blobmsg_add_u32(bb, "status", -1); + blobmsg_add_u32(bb, "status", 1); blobmsg_add_string(bb, "info", "Session already running, event will be sent at the end of the session"); } else { blobmsg_add_u32(bb, "status", 1);