From 5aa1298df5fa75c8ad6888768d1bf53089302b02 Mon Sep 17 00:00:00 2001 From: Amin Ben Romdhane Date: Mon, 7 Oct 2024 11:04:20 +0200 Subject: [PATCH] Improve event handling --- libbbfdm-api/dmubus.c | 6 ++--- libbbfdm-api/dmubus.h | 8 +++---- libbbfdm-ubus/events.c | 50 ++++++++++++++++++++++++++++++++++++------ libbbfdm/device.c | 2 +- libbbfdm/deviceinfo.c | 4 ++-- libbbfdm/deviceinfo.h | 2 +- 6 files changed, 54 insertions(+), 18 deletions(-) diff --git a/libbbfdm-api/dmubus.c b/libbbfdm-api/dmubus.c index 21b34e72..1abdb879 100644 --- a/libbbfdm-api/dmubus.c +++ b/libbbfdm-api/dmubus.c @@ -165,8 +165,8 @@ static void _bbfdm_task_callback(struct uloop_timeout *t) free(task); } -int bbfdm_task_schedule(bbfdm_task_callback_t callback, const void *arg1, const void *arg2, int timeout_sec) { - +int bbfdm_task_schedule(bbfdm_task_callback_t callback, const void *arg1, void *arg2, int timeout_sec) +{ bbfdm_task_data_t *task; if (timeout_sec < 0) { @@ -209,7 +209,7 @@ static void _bbfdm_task_finish_callback(struct uloop_process *p, int ret) } -int bbfdm_task_fork(bbfdm_task_callback_t taskcb, bbfdm_task_callback_t finishcb, const void *arg1, const void *arg2) +int bbfdm_task_fork(bbfdm_task_callback_t taskcb, bbfdm_task_callback_t finishcb, const void *arg1, void *arg2) { pid_t child; bbfdm_task_data_t *task; diff --git a/libbbfdm-api/dmubus.h b/libbbfdm-api/dmubus.h index 2003c848..cf5da981 100644 --- a/libbbfdm-api/dmubus.h +++ b/libbbfdm-api/dmubus.h @@ -31,7 +31,7 @@ struct dmubus_ev_subtask { }; // bbfdm task related functions -typedef void (*bbfdm_task_callback_t)(const void *arg1, const void *arg2); +typedef void (*bbfdm_task_callback_t)(const void *arg1, void *arg2); typedef struct bbfdm_task_data { struct uloop_process process; // Used for forked task @@ -39,12 +39,12 @@ typedef struct bbfdm_task_data { bbfdm_task_callback_t taskcb; bbfdm_task_callback_t finishcb; // Used for forked task const void *arg1; - const void *arg2; + void *arg2; } bbfdm_task_data_t; -int bbfdm_task_schedule(bbfdm_task_callback_t callback, const void *arg1, const void *arg2, int timeout); +int bbfdm_task_schedule(bbfdm_task_callback_t callback, const void *arg1, void *arg2, int timeout); -int bbfdm_task_fork(bbfdm_task_callback_t taskcb, bbfdm_task_callback_t finishcb, const void *arg1, const void *arg2); +int bbfdm_task_fork(bbfdm_task_callback_t taskcb, bbfdm_task_callback_t finishcb, const void *arg1, void *arg2); typedef void (*CB_FUNC_PTR)(struct ubus_context *ctx, struct ubus_event_handler *ev, const char *type, struct blob_attr *msg); diff --git a/libbbfdm-ubus/events.c b/libbbfdm-ubus/events.c index b498f5f7..3d66af4a 100644 --- a/libbbfdm-ubus/events.c +++ b/libbbfdm-ubus/events.c @@ -14,6 +14,11 @@ #include "get_helper.h" #include +struct event_args { + struct blob_attr *blob_data; + char method_name[256]; +}; + static char *get_events_dm_path(struct list_head *ev_list, const char *event) { struct ev_handler_node *iter = NULL; @@ -29,6 +34,28 @@ static char *get_events_dm_path(struct list_head *ev_list, const char *event) return NULL; } +void event_callback(const void *arg1, void *arg2) +{ + struct event_args *e_args = (struct event_args *)arg2; + + if (!e_args || !e_args->blob_data || !DM_STRLEN(e_args->method_name)) + return; + + struct ubus_context *ctx = ubus_connect(NULL); + if (ctx == NULL) { + BBF_ERR("Can't create UBUS context for event"); + return; + } + + ubus_send_event(ctx, e_args->method_name, e_args->blob_data); + BBF_INFO("Event[%s] sent", e_args->method_name); + + ubus_free(ctx); + + free(e_args->blob_data); + free(e_args); +} + static void bbfdm_event_handler(struct ubus_context *ctx, struct ubus_event_handler *ev, const char *type, struct blob_attr *msg) { @@ -73,15 +100,24 @@ static void bbfdm_event_handler(struct ubus_context *ctx, struct ubus_event_hand if (ret) goto end; - char method_name[256] = {0}; - - snprintf(method_name, sizeof(method_name), "%s.%s", DM_STRLEN(u->config.out_root_obj) ? u->config.out_root_obj : u->config.out_name, BBF_EVENT_NAME); - - ubus_send_event(ctx, method_name, bbf_ctx.bb.head); - BBF_INFO("Event[%s], for [%s] sent", method_name, dm_path); - bbfdm_schedule_instance_refresh_timer(ctx, 2); + size_t blob_data_len = blob_len(bbf_ctx.bb.head); + + if (blob_data_len) { + struct event_args *e_args = (struct event_args *)calloc(1, sizeof(struct event_args)); + if (!e_args) + goto end; + + snprintf(e_args->method_name, sizeof(e_args->method_name), "%s.%s", DM_STRLEN(u->config.out_root_obj) ? u->config.out_root_obj : u->config.out_name, BBF_EVENT_NAME); + + e_args->blob_data = (struct blob_attr *)calloc(1, blob_data_len); + + memcpy(e_args->blob_data, bbf_ctx.bb.head, blob_data_len); + + bbfdm_task_schedule(event_callback, NULL, e_args, 6); + } + end: bbf_cleanup(&bbf_ctx); FREE(str); diff --git a/libbbfdm/device.c b/libbbfdm/device.c index 43cfcfa1..cea21fe2 100644 --- a/libbbfdm/device.c +++ b/libbbfdm/device.c @@ -26,7 +26,7 @@ static int get_Device_RootDataModelVersion(char *refparam, struct dmctx *ctx, vo return 0; } -static void _exec_factoryreset(const void *arg1, const void *arg2) +static void _exec_factoryreset(const void *arg1, void *arg2) { sleep(2); dmubus_call_set("rpc-sys", "factory", UBUS_ARGS{0}, 0); diff --git a/libbbfdm/deviceinfo.c b/libbbfdm/deviceinfo.c index c28f2d73..8914b0e5 100644 --- a/libbbfdm/deviceinfo.c +++ b/libbbfdm/deviceinfo.c @@ -1692,9 +1692,9 @@ static int get_operate_args_DeviceInfoFirmwareImage_Activate(char *refparam, str return 0; } -void _exec_reboot(const void *arg1, const void *arg2) +void _exec_reboot(const void *arg1, void *arg2) { - sleep(3); // Wait for reboot to happen + sleep(3); dmubus_call_set("rpc-sys", "reboot", UBUS_ARGS{0}, 0); sleep(5); // Wait for reboot to happen BBF_ERR("Reboot call failed with rpc-sys, trying again with system"); diff --git a/libbbfdm/deviceinfo.h b/libbbfdm/deviceinfo.h index c2e39d2d..90b12834 100644 --- a/libbbfdm/deviceinfo.h +++ b/libbbfdm/deviceinfo.h @@ -26,5 +26,5 @@ extern DMLEAF tDeviceInfoProcessorParams[]; extern DMLEAF tDeviceInfoSupportedDataModelParams[]; extern DMLEAF tDeviceInfoFirmwareImageParams[]; -void _exec_reboot(const void *arg1, const void *arg2); +void _exec_reboot(const void *arg1, void *arg2); #endif