2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License version 2.1
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
18 #include <libubox/blobmsg_json.h>
23 struct list_head list;
29 struct watch_subscribe {
30 struct uloop_timeout t;
34 static struct ubus_event_handler watch_event;
35 static struct ubus_subscriber watch_subscribe;
36 static LIST_HEAD(watch_objects);
38 static void watch_subscribe_cb(struct ubus_context *ctx, struct ubus_event_handler *ev,
39 const char *type, struct blob_attr *msg)
41 static const struct blobmsg_policy policy = {
42 "path", BLOBMSG_TYPE_STRING
44 struct watch_object *o;
45 struct blob_attr *attr;
48 DEBUG(3, "ubus event %s\n", type);
49 if (strcmp(type, "ubus.object.add") != 0)
52 blobmsg_parse(&policy, 1, &attr, blob_data(msg), blob_len(msg));
56 path = blobmsg_data(attr);
57 DEBUG(3, "ubus path %s\n", path);
59 list_for_each_entry(o, &watch_objects, list) {
62 if (strcmp(o->name, path))
64 if (ubus_lookup_id(ctx, path, &id))
66 if (!ubus_subscribe(ctx, &watch_subscribe, id))
68 ERROR("failed to subscribe %d\n", id);
73 watch_add(const char *_name, void *id)
75 int len = strlen(_name);
77 struct watch_object *o = calloc_a(sizeof(*o), &name, len + 1);
82 list_add(&o->list, &watch_objects);
88 struct watch_object *t, *n;
90 list_for_each_entry_safe(t, n, &watch_objects, list) {
99 watch_notify_cb(struct ubus_context *ctx, struct ubus_object *obj,
100 struct ubus_request_data *req, const char *method,
101 struct blob_attr *msg)
106 str = blobmsg_format_json(msg, true);
107 DEBUG(3, "Received ubus notify '%s': %s\n", method, str);
111 trigger_event(method, msg);
116 watch_ubus(struct ubus_context *ctx)
118 watch_event.cb = watch_subscribe_cb;
119 watch_subscribe.cb = watch_notify_cb;
120 if (ubus_register_event_handler(ctx, &watch_event, "ubus.object.add"))
121 ERROR("failed to add ubus event handler\n");
122 if (ubus_register_subscriber(ctx, &watch_subscribe))
123 ERROR("failed to register ubus subscriber\n");