Improve event handling

This commit is contained in:
Amin Ben Romdhane 2024-10-07 11:04:20 +02:00
parent 46796a5a47
commit 9dc981b081
2 changed files with 42 additions and 7 deletions

View file

@ -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) {

View file

@ -14,6 +14,11 @@
#include "get_helper.h"
#include <libubus.h>
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);