From ed4b742f3facc04c127ed96731fa89dac340ee3e Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Sat, 5 Feb 2011 23:23:44 +0100 Subject: [PATCH] implement code for receiving events --- cli.c | 24 ++++++++++++++++++++++-- libubus.c | 26 ++++++++++++++++++++++++-- libubus.h | 12 +++++++++++- 3 files changed, 57 insertions(+), 5 deletions(-) diff --git a/cli.c b/cli.c index 22fa768..8a47cd0 100644 --- a/cli.c +++ b/cli.c @@ -20,10 +20,13 @@ static void receive_lookup(struct ubus_context *ctx, struct ubus_object_data *ob static void receive_data(struct ubus_request *req, int type, struct blob_attr *msg) { + char *str; if (!msg) return; - fprintf(stderr, "%s\n", blobmsg_format_json(msg, true)); + str = blobmsg_format_json(msg, true); + fprintf(stderr, "%s\n", str); + free(str); } @@ -39,12 +42,29 @@ static int usage(char *prog) return 1; } +static void receive_event(struct ubus_context *ctx, struct ubus_event_handler *ev, + const char *type, struct blob_attr *msg) +{ + char *str; + + if (msg) + str = blobmsg_format_json(msg, true); + else + str = ""; + + fprintf(stderr, "\"%s\":{ %s }\n", type, str); + free(str); +} + static int ubus_cli_listen(struct ubus_context *ctx, int argc, char **argv) { - static struct ubus_object listener; + static struct ubus_event_handler listener; const char *event; int ret = 0; + memset(&listener, 0, sizeof(listener)); + listener.cb = receive_event; + if (!argc) { event = "*"; ret = ubus_register_event_handler(ctx, &listener, NULL); diff --git a/libubus.c b/libubus.c index 223a6bb..66de478 100644 --- a/libubus.c +++ b/libubus.c @@ -307,7 +307,8 @@ static void ubus_process_invoke(struct ubus_context *ctx, struct ubus_msghdr *hd } for (method = 0; method < obj->n_methods; method++) - if (!strcmp(obj->methods[method].name, + if (!obj->methods[method].name || + !strcmp(obj->methods[method].name, blob_data(attrbuf[UBUS_ATTR_METHOD]))) goto found; @@ -643,13 +644,34 @@ int ubus_publish(struct ubus_context *ctx, struct ubus_object *obj) return __ubus_publish(ctx, obj); } -int ubus_register_event_handler(struct ubus_context *ctx, struct ubus_object *obj, +static int ubus_event_cb(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, + const char *method, struct blob_attr *msg) +{ + struct ubus_event_handler *ev; + + ev = container_of(obj, struct ubus_event_handler, obj); + ev->cb(ctx, ev, method, msg); + return 0; +} + +static const struct ubus_method event_method = { + .name = NULL, + .handler = ubus_event_cb, +}; + +int ubus_register_event_handler(struct ubus_context *ctx, + struct ubus_event_handler *ev, const char *pattern) { + struct ubus_object *obj = &ev->obj; struct blob_buf b2; int ret; if (!obj->id) { + obj->methods = &event_method; + obj->n_methods = 1; + if (!!obj->name ^ !!obj->type) return UBUS_STATUS_INVALID_ARGUMENT; diff --git a/libubus.h b/libubus.h index 48ee056..5fb565b 100644 --- a/libubus.h +++ b/libubus.h @@ -12,6 +12,7 @@ struct ubus_object; struct ubus_request; struct ubus_request_data; struct ubus_object_data; +struct ubus_event_handler; typedef void (*ubus_lookup_handler_t)(struct ubus_context *ctx, struct ubus_object_data *obj, @@ -19,6 +20,8 @@ typedef void (*ubus_lookup_handler_t)(struct ubus_context *ctx, typedef int (*ubus_handler_t)(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg); +typedef void (*ubus_event_handler_t)(struct ubus_context *ctx, struct ubus_event_handler *ev, + const char *type, struct blob_attr *msg); typedef void (*ubus_data_handler_t)(struct ubus_request *req, int type, struct blob_attr *msg); typedef void (*ubus_complete_handler_t)(struct ubus_request *req, int ret); @@ -75,6 +78,12 @@ struct ubus_object { int n_methods; }; +struct ubus_event_handler { + struct ubus_object obj; + + ubus_event_handler_t cb; +}; + struct ubus_context { struct list_head requests; struct avl_tree objects; @@ -173,5 +182,6 @@ int ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req, struct blob_attr *msg); /* ----------- events ----------- */ -int ubus_register_event_handler(struct ubus_context *ctx, struct ubus_object *obj, +int ubus_register_event_handler(struct ubus_context *ctx, + struct ubus_event_handler *ev, const char *pattern); -- 2.11.0