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 static struct ubus_event_handler watch_event
;
30 static struct ubus_subscriber watch_subscribe
;
31 static LIST_HEAD(watch_objects
);
33 static void watch_subscribe_cb(struct ubus_context
*ctx
, struct ubus_event_handler
*ev
,
34 const char *type
, struct blob_attr
*msg
)
36 static const struct blobmsg_policy policy
= {
37 "path", BLOBMSG_TYPE_STRING
39 struct watch_object
*o
;
40 struct blob_attr
*attr
;
43 DEBUG(3, "ubus event %s\n", type
);
44 if (strcmp(type
, "ubus.object.add") != 0)
47 blobmsg_parse(&policy
, 1, &attr
, blob_data(msg
), blob_len(msg
));
51 path
= blobmsg_data(attr
);
52 DEBUG(3, "ubus path %s\n", path
);
54 list_for_each_entry(o
, &watch_objects
, list
) {
57 if (strcmp(o
->name
, path
))
59 if (ubus_lookup_id(ctx
, path
, &id
))
61 if (!ubus_subscribe(ctx
, &watch_subscribe
, id
))
63 ERROR("failed to subscribe %d\n", id
);
68 watch_add(const char *_name
, void *id
)
70 int len
= strlen(_name
);
72 struct watch_object
*o
= calloc_a(sizeof(*o
), &name
, len
+ 1);
77 list_add(&o
->list
, &watch_objects
);
83 struct watch_object
*t
, *n
;
85 list_for_each_entry_safe(t
, n
, &watch_objects
, list
) {
94 watch_notify_cb(struct ubus_context
*ctx
, struct ubus_object
*obj
,
95 struct ubus_request_data
*req
, const char *method
,
96 struct blob_attr
*msg
)
101 str
= blobmsg_format_json(msg
, true);
102 DEBUG(3, "Received ubus notify '%s': %s\n", method
, str
);
106 trigger_event(method
, msg
);
111 watch_ubus(struct ubus_context
*ctx
)
113 watch_event
.cb
= watch_subscribe_cb
;
114 watch_subscribe
.cb
= watch_notify_cb
;
115 if (ubus_register_subscriber(ctx
, &watch_subscribe
))
116 ERROR("failed to register ubus subscriber\n");
117 if (ubus_register_event_handler(ctx
, &watch_event
, "ubus.object.add"))
118 ERROR("failed to add ubus event handler\n");