ccb4d9533b1444110f130bc65b87153815482790
[project/ubus.git] / libubus-sub.c
1 /*
2  * Copyright (C) 2011-2012 Felix Fietkau <nbd@openwrt.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 2.1
6  * as published by the Free Software Foundation
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #include "libubus.h"
15 #include "libubus-internal.h"
16
17 static int ubus_subscriber_cb(struct ubus_context *ctx, struct ubus_object *obj,
18                          struct ubus_request_data *req,
19                          const char *method, struct blob_attr *msg)
20 {
21         struct ubus_subscriber *s;
22
23         s = container_of(obj, struct ubus_subscriber, obj);
24         s->cb(ctx, obj, req, method, msg);
25         return 0;
26 }
27
28 static const struct ubus_method watch_method = {
29         .name = NULL,
30         .handler = ubus_subscriber_cb,
31 };
32
33 int ubus_register_subscriber(struct ubus_context *ctx, struct ubus_subscriber *s)
34 {
35         struct ubus_object *obj = &s->obj;
36
37         obj->methods = &watch_method;
38         obj->n_methods = 1;
39
40         return ubus_add_object(ctx, obj);
41 }
42
43 static int
44 __ubus_subscribe_request(struct ubus_context *ctx, struct ubus_object *obj, uint32_t id, int type)
45 {
46         struct ubus_request req;
47
48         blob_buf_init(&b, 0);
49         blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id);
50         blob_put_int32(&b, UBUS_ATTR_TARGET, id);
51
52         if (ubus_start_request(ctx, &req, b.head, type, 0) < 0)
53                 return UBUS_STATUS_INVALID_ARGUMENT;
54
55         return ubus_complete_request(ctx, &req, 0);
56
57 }
58
59 int ubus_subscribe(struct ubus_context *ctx, struct ubus_subscriber *obj, uint32_t id)
60 {
61         return __ubus_subscribe_request(ctx, &obj->obj, id, UBUS_MSG_SUBSCRIBE);
62 }
63
64 int ubus_unsubscribe(struct ubus_context *ctx, struct ubus_subscriber *obj, uint32_t id)
65 {
66         return __ubus_subscribe_request(ctx, &obj->obj, id, UBUS_MSG_UNSUBSCRIBE);
67 }
68
69 void __hidden ubus_process_unsubscribe(struct ubus_context *ctx, struct ubus_msghdr *hdr)
70 {
71         struct ubus_subscriber *s;
72         struct blob_attr **attrbuf;
73         struct ubus_object *obj;
74         uint32_t objid;
75
76         attrbuf = ubus_parse_msg(hdr->data);
77         if (!attrbuf[UBUS_ATTR_OBJID] || !attrbuf[UBUS_ATTR_TARGET])
78                 return;
79
80         objid = blob_get_u32(attrbuf[UBUS_ATTR_OBJID]);
81         obj = avl_find_element(&ctx->objects, &objid, obj, avl);
82         if (!obj)
83                 return;
84
85         if (obj->methods != &watch_method)
86                 return;
87
88         s = container_of(obj, struct ubus_subscriber, obj);
89         s->remove_cb(ctx, s, blob_get_u32(attrbuf[UBUS_ATTR_TARGET]));
90 }
91
92 void __hidden ubus_process_notify(struct ubus_context *ctx, struct ubus_msghdr *hdr)
93 {
94         struct blob_attr **attrbuf;
95         struct ubus_object *obj;
96         uint32_t objid;
97
98         attrbuf = ubus_parse_msg(hdr->data);
99         if (!attrbuf[UBUS_ATTR_OBJID] || !attrbuf[UBUS_ATTR_ACTIVE])
100                 return;
101
102         objid = blob_get_u32(attrbuf[UBUS_ATTR_OBJID]);
103         obj = avl_find_element(&ctx->objects, &objid, obj, avl);
104         if (!obj)
105                 return;
106
107         obj->has_subscribers = blob_get_u8(attrbuf[UBUS_ATTR_ACTIVE]);
108         if (obj->subscribe_cb)
109                 obj->subscribe_cb(ctx, obj);
110 }