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);
}
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);
}
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;
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;
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,
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);
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;
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);