From b671efa938b4de26f5f7ab45ac52998073e9c26e Mon Sep 17 00:00:00 2001 From: Suvendhu Hansa Date: Mon, 7 Oct 2024 19:14:56 +0530 Subject: [PATCH] Convert diagnostics to async operation --- src/common.h | 1 - src/diagnostic.c | 23 +++++++++++------------ src/session.c | 8 -------- src/ubus_utils.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/ubus_utils.h | 3 +++ 5 files changed, 59 insertions(+), 21 deletions(-) diff --git a/src/common.h b/src/common.h index bd6ca99..f5668ee 100644 --- a/src/common.h +++ b/src/common.h @@ -197,7 +197,6 @@ typedef struct cwmp { bool prev_periodic_enable; bool prev_heartbeat_enable; bool heart_session; - bool diag_session; bool throttle_session; int prev_periodic_interval; int prev_heartbeat_interval; diff --git a/src/diagnostic.c b/src/diagnostic.c index 2d84e15..f257d4d 100644 --- a/src/diagnostic.c +++ b/src/diagnostic.c @@ -255,6 +255,16 @@ bool set_diagnostic_parameter_structure_value(char *parameter_name, char *value) } +static void diagnostics_complete_cb(struct ubus_request *req, int ret) +{ + struct session_timer_event *periodic_inform_event = calloc(1, sizeof(struct session_timer_event)); + periodic_inform_event->session_timer_evt.cb = cwmp_schedule_session_with_event; + periodic_inform_event->event = EVENT_IDX_8DIAGNOSTICS_COMPLETE; + trigger_cwmp_session_timer_with_event(&periodic_inform_event->session_timer_evt); + + FREE(req); +} + static int cwmp_diagnostics_operate(const char *command, const char *command_key, struct diagnostic_input diagnostics[], int number_inputs) { struct blob_buf b = {0}; @@ -279,7 +289,7 @@ static int cwmp_diagnostics_operate(const char *command, const char *command_key blobmsg_close_table(&b, tbl); } - int e = icwmp_ubus_invoke(BBFDM_OBJECT_NAME, "operate", b.head, NULL, NULL); + int e = icwmp_ubus_invoke_async(BBFDM_OBJECT_NAME, "operate", b.head, NULL, diagnostics_complete_cb); blob_buf_free(&b); return e; @@ -291,7 +301,6 @@ int cwmp_wifi_neighboring__diagnostics(void) return -1; CWMP_LOG(INFO, "WiFi neighboring diagnostic is successfully executed"); - cwmp_ctx.diag_session = true; return 0; } @@ -301,7 +310,6 @@ int cwmp_packet_capture_diagnostics(void) return -1; CWMP_LOG(INFO, "packet capture diagnostic is successfully executed"); - cwmp_ctx.diag_session = true; return 0; } @@ -311,7 +319,6 @@ int cwmp_selftest_diagnostics(void) return -1; CWMP_LOG(INFO, "self test diagnostic is successfully executed"); - cwmp_ctx.diag_session = true; return 0; } @@ -321,7 +328,6 @@ int cwmp_ip_layer_capacity_diagnostics(void) return -1; CWMP_LOG(INFO, "IP layer capacity diagnostic is successfully executed"); - cwmp_ctx.diag_session = true; return 0; } @@ -331,7 +337,6 @@ int cwmp_download_diagnostics(void) return -1; CWMP_LOG(INFO, "Download diagnostic is successfully executed"); - cwmp_ctx.diag_session = true; return 0; } @@ -341,7 +346,6 @@ int cwmp_upload_diagnostics(void) return -1; CWMP_LOG(INFO, "Upload diagnostic is successfully executed"); - cwmp_ctx.diag_session = true; return 0; } @@ -351,7 +355,6 @@ int cwmp_ip_ping_diagnostics(void) return -1; CWMP_LOG(INFO, "IPPing diagnostic is successfully executed"); - cwmp_ctx.diag_session = true; return 0; } @@ -361,7 +364,6 @@ int cwmp_nslookup_diagnostics(void) return -1; CWMP_LOG(INFO, "Nslookup diagnostic is successfully executed"); - cwmp_ctx.diag_session = true; return 0; } @@ -371,7 +373,6 @@ int cwmp_traceroute_diagnostics(void) return -1; CWMP_LOG(INFO, "Trace Route diagnostic is successfully executed"); - cwmp_ctx.diag_session = true; return 0; } @@ -381,7 +382,6 @@ int cwmp_udp_echo_diagnostics(void) return -1; CWMP_LOG(INFO, "UDPEcho diagnostic is successfully executed"); - cwmp_ctx.diag_session = true; return 0; } @@ -391,6 +391,5 @@ int cwmp_serverselection_diagnostics(void) return -1; CWMP_LOG(INFO, "Server Selection diagnostic is successfully executed"); - cwmp_ctx.diag_session = true; return 0; } diff --git a/src/session.c b/src/session.c index b3207d8..983a5b0 100644 --- a/src/session.c +++ b/src/session.c @@ -724,14 +724,6 @@ int run_session_end_func(void) cwmp_selftest_diagnostics(); } - if (cwmp_ctx.diag_session) { - struct session_timer_event *periodic_inform_event = calloc(1, sizeof(struct session_timer_event)); - periodic_inform_event->session_timer_evt.cb = cwmp_schedule_session_with_event; - periodic_inform_event->event = EVENT_IDX_8DIAGNOSTICS_COMPLETE; - trigger_cwmp_session_timer_with_event(&periodic_inform_event->session_timer_evt); - cwmp_ctx.diag_session = false; - } - if (end_session_flag & END_SESSION_DOWNLOAD) { CWMP_LOG(INFO, "Apply Downaload Calls"); apply_downloads(); diff --git a/src/ubus_utils.c b/src/ubus_utils.c index 4d772e5..b6d0b88 100644 --- a/src/ubus_utils.c +++ b/src/ubus_utils.c @@ -470,6 +470,51 @@ int icwmp_ubus_invoke(const char *obj, const char *method, struct blob_attr *msg return rc; } +int icwmp_ubus_invoke_async(const char *obj, const char *method, struct blob_attr *msg, + icwmp_ubus_cb data_callback, icwmp_ubus_async_cb complete_callback) +{ + uint32_t id; + int fault = UBUS_STATUS_OK; + struct ubus_request *req; + + if (ubus_ctx == NULL) { + CWMP_LOG(ERROR, "Failed to connect with ubus err: %d", errno); + return -1; + } + + fault = ubus_lookup_id(ubus_ctx, obj, &id); + if (fault) { + CWMP_LOG(ERROR, "failed to lookup object: %s", obj); + return -1; + } + + req = (struct ubus_request *)malloc(sizeof(struct ubus_request)); + if (req == NULL) { + CWMP_LOG(ERROR, "failed to allocate memory for ubus request"); + return -1; + } + + memset(req, 0, sizeof(struct ubus_request)); + + fault = ubus_invoke_async(ubus_ctx, id, method, msg, req); + if (fault) { + CWMP_LOG(ERROR, "ubus async call failed"); + FREE(req); + return -1; + } + + if (data_callback) { + req->data_cb = data_callback; + } + + if (complete_callback) { + req->complete_cb = complete_callback; + } + + ubus_complete_request_async(ubus_ctx, req); + return 0; +} + int initiate_autonomous_complpolicy(void) { cwmp_ctx.ev = (struct ubus_event_handler *)malloc(sizeof(struct ubus_event_handler)); diff --git a/src/ubus_utils.h b/src/ubus_utils.h index ef52aaa..d28b1a2 100644 --- a/src/ubus_utils.h +++ b/src/ubus_utils.h @@ -15,9 +15,12 @@ #include "common.h" typedef void (*icwmp_ubus_cb)(struct ubus_request *req, int type, struct blob_attr *msg); +typedef void (*icwmp_ubus_async_cb)(struct ubus_request *req, int ret); void bb_add_string(struct blob_buf *bb, const char *name, const char *value); int icwmp_ubus_invoke(const char *obj, const char *method, struct blob_attr *msg, icwmp_ubus_cb icwmp_callback, void *callback_arg); +int icwmp_ubus_invoke_async(const char *obj, const char *method, struct blob_attr *msg, + icwmp_ubus_cb data_callback, icwmp_ubus_async_cb complete_callback); int icwmp_uloop_ubus_register(void); void icwmp_uloop_ubus_exit(void); int initiate_autonomous_complpolicy(void);