implement code for receiving events
This commit is contained in:
parent
f87a9dc69e
commit
ed4b742f3f
3 changed files with 57 additions and 5 deletions
24
cli.c
24
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);
|
||||
|
|
26
libubus.c
26
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;
|
||||
|
||||
|
|
12
libubus.h
12
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);
|
||||
|
|
Loading…
Add table
Reference in a new issue