From 9dc981b08113276c2172e8ad5e4aa523fb6ad9aa 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 | 4 ++-- libbbfdm-ubus/events.c | 45 +++++++++++++++++++++++++++++++++++++----- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/libbbfdm-api/dmubus.c b/libbbfdm-api/dmubus.c index 3dc037fd..87aea20c 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_add(bbfdm_task_callback_t callback, const void *arg1, const void *arg2, int timeout_sec) { - +int bbfdm_task_add(bbfdm_task_callback_t callback, const void *arg1, const void *arg2, int timeout_sec) +{ bbfdm_task_data_t *task; if (timeout_sec < 0) { diff --git a/libbbfdm-ubus/events.c b/libbbfdm-ubus/events.c index 2cc9ab9e..b39924a2 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, const void *arg2) +{ + struct event_args *e_args = (struct event_args *)arg1; + + 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) { @@ -74,15 +101,23 @@ static void bbfdm_event_handler(struct ubus_context *ctx, struct ubus_event_hand goto end; cancel_instance_refresh_timer(ctx); + register_instance_refresh_timer(ctx, 0); - char method_name[256] = {0}; + size_t blob_data_len = blob_len(bbf_ctx.bb.head); - 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); + if (blob_data_len) { + struct event_args *e_args = (struct event_args *)calloc(1, sizeof(struct event_args)); + if (!e_args) + goto end; - ubus_send_event(ctx, method_name, bbf_ctx.bb.head); - BBF_INFO("Event[%s], for [%s] sent", method_name, dm_path); + 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); - register_instance_refresh_timer(ctx, 2000); + 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_add(event_callback, e_args, NULL, 5); + } end: bbf_cleanup(&bbf_ctx);