diff --git a/src/session.c b/src/session.c index 6c4a807..6548e3a 100644 --- a/src/session.c +++ b/src/session.c @@ -34,6 +34,7 @@ #include "cwmp_du_state.h" #include "cwmp_http.h" #include "uci_utils.h" +#include "ubus_utils.h" static struct rpc *cwmp_add_session_rpc_acs_head(int type); static int cwmp_session_rpc_destructor(struct rpc *rpc); @@ -407,6 +408,9 @@ void start_cwmp_session(void) CWMP_LOG(INFO, "Start session"); + // Send session start event + icwmp_ubus_send_event("icwmp.session", "{\"status\":\"start\"}"); + get_uci_path_value(NULL, "cwmp.cpe.exec_download", exec_download, BUF_SIZE_256); if (CWMP_STRCMP(exec_download, "1") == 0) { CWMP_LOG(INFO, "Firmware downloaded and applied successfully"); @@ -689,6 +693,9 @@ int run_session_end_func(void) { CWMP_LOG(INFO, "Handling end session with: (%u)", end_session_flag); + // Send session end event + icwmp_ubus_send_event("icwmp.session", "{\"status\":\"end\"}"); + if (end_session_flag & END_SESSION_RESTART_SERVICES) { CWMP_LOG(INFO, "Restart modified services"); icwmp_restart_services(RELOAD_END_SESSION, true, true); diff --git a/src/ubus_utils.c b/src/ubus_utils.c index 4cff52f..ffc1554 100644 --- a/src/ubus_utils.c +++ b/src/ubus_utils.c @@ -458,6 +458,37 @@ void icwmp_uloop_ubus_exit() } } +void icwmp_ubus_send_event(const char *event, const char *data) +{ + if (CWMP_STRLEN(event) == 0) { + CWMP_LOG(ERROR, "No event name found"); + return; + } + + if (ubus_ctx == NULL) { + CWMP_LOG(ERROR, "Failed to connect with ubus err: %d", errno); + return; + } + + struct blob_buf b = {0}; + blob_buf_init(&b, 0); + + if (CWMP_STRLEN(data)) { + if (blobmsg_add_json_from_string(&b, data) != true) { + CWMP_LOG(ERROR, "Failed to parse event data: %s", data); + blob_buf_free(&b); + return; + } + } + + if (ubus_send_event(ubus_ctx, event, b.head) != UBUS_STATUS_OK) { + CWMP_LOG(ERROR, "Failed to send event: %s", event); + } + + blob_buf_free(&b); + return; +} + int icwmp_ubus_invoke(const char *obj, const char *method, struct blob_attr *msg, icwmp_ubus_cb icwmp_callback, void *callback_arg) { uint32_t id; diff --git a/src/ubus_utils.h b/src/ubus_utils.h index a69721a..6bd5bc5 100644 --- a/src/ubus_utils.h +++ b/src/ubus_utils.h @@ -12,11 +12,13 @@ #define __ICWMP_UBUS_UTILS_H__ #include +#include #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); +void icwmp_ubus_send_event(const char *event, const char *data); 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_timeout(const char *obj, const char *method, struct blob_attr *msg,