libubus: add ubus_unregister_subscriber wrapper
[project/ubus.git] / libubus-obj.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 void
18 ubus_process_unsubscribe(struct ubus_context *ctx, struct ubus_msghdr *hdr,
19                          struct ubus_object *obj, struct blob_attr **attrbuf)
20 {
21         struct ubus_subscriber *s;
22
23         if (!obj || !attrbuf[UBUS_ATTR_TARGET])
24                 return;
25
26         if (obj->methods != &watch_method)
27                 return;
28
29         s = container_of(obj, struct ubus_subscriber, obj);
30         s->remove_cb(ctx, s, blob_get_u32(attrbuf[UBUS_ATTR_TARGET]));
31 }
32
33 static void
34 ubus_process_notify(struct ubus_context *ctx, struct ubus_msghdr *hdr,
35                     struct ubus_object *obj, struct blob_attr **attrbuf)
36 {
37         if (!obj || !attrbuf[UBUS_ATTR_ACTIVE])
38                 return;
39
40         obj->has_subscribers = blob_get_u8(attrbuf[UBUS_ATTR_ACTIVE]);
41         if (obj->subscribe_cb)
42                 obj->subscribe_cb(ctx, obj);
43 }
44 static void
45 ubus_process_invoke(struct ubus_context *ctx, struct ubus_msghdr *hdr,
46                     struct ubus_object *obj, struct blob_attr **attrbuf)
47 {
48         struct ubus_request_data req = {};
49         int method;
50         int ret;
51         bool no_reply = false;
52
53         if (!obj) {
54                 ret = UBUS_STATUS_NOT_FOUND;
55                 goto send;
56         }
57
58         if (!attrbuf[UBUS_ATTR_METHOD]) {
59                 ret = UBUS_STATUS_INVALID_ARGUMENT;
60                 goto send;
61         }
62
63         if (attrbuf[UBUS_ATTR_NO_REPLY])
64                 no_reply = blob_get_int8(attrbuf[UBUS_ATTR_NO_REPLY]);
65
66         req.peer = hdr->peer;
67         req.seq = hdr->seq;
68         req.object = obj->id;
69
70         for (method = 0; method < obj->n_methods; method++)
71                 if (!obj->methods[method].name ||
72                     !strcmp(obj->methods[method].name,
73                             blob_data(attrbuf[UBUS_ATTR_METHOD])))
74                         goto found;
75
76         /* not found */
77         ret = UBUS_STATUS_METHOD_NOT_FOUND;
78         goto send;
79
80 found:
81         ret = obj->methods[method].handler(ctx, obj, &req,
82                                            blob_data(attrbuf[UBUS_ATTR_METHOD]),
83                                            attrbuf[UBUS_ATTR_DATA]);
84         if (req.deferred || no_reply)
85                 return;
86
87 send:
88         ubus_complete_deferred_request(ctx, &req, ret);
89 }
90
91 void __hidden ubus_process_obj_msg(struct ubus_context *ctx, struct ubus_msghdr *hdr)
92 {
93         void (*cb)(struct ubus_context *, struct ubus_msghdr *,
94                    struct ubus_object *, struct blob_attr **);
95         struct blob_attr **attrbuf;
96         struct ubus_object *obj;
97         uint32_t objid;
98
99         attrbuf = ubus_parse_msg(hdr->data);
100         if (!attrbuf[UBUS_ATTR_OBJID])
101                 return;
102
103         objid = blob_get_u32(attrbuf[UBUS_ATTR_OBJID]);
104         obj = avl_find_element(&ctx->objects, &objid, obj, avl);
105
106         switch (hdr->type) {
107         case UBUS_MSG_INVOKE:
108                 cb = ubus_process_invoke;
109                 break;
110         case UBUS_MSG_UNSUBSCRIBE:
111                 cb = ubus_process_unsubscribe;
112                 break;
113         case UBUS_MSG_NOTIFY:
114                 cb = ubus_process_notify;
115                 break;
116         default:
117                 return;
118         }
119         cb(ctx, hdr, obj, attrbuf);
120 }
121
122 static void ubus_add_object_cb(struct ubus_request *req, int type, struct blob_attr *msg)
123 {
124         struct ubus_object *obj = req->priv;
125         struct blob_attr **attrbuf = ubus_parse_msg(msg);
126
127         if (!attrbuf[UBUS_ATTR_OBJID])
128                 return;
129
130         obj->id = blob_get_u32(attrbuf[UBUS_ATTR_OBJID]);
131
132         if (attrbuf[UBUS_ATTR_OBJTYPE])
133                 obj->type->id = blob_get_u32(attrbuf[UBUS_ATTR_OBJTYPE]);
134
135         obj->avl.key = &obj->id;
136         avl_insert(&req->ctx->objects, &obj->avl);
137 }
138
139 static void ubus_push_method_data(const struct ubus_method *m)
140 {
141         void *mtbl;
142         int i;
143
144         mtbl = blobmsg_open_table(&b, m->name);
145
146         for (i = 0; i < m->n_policy; i++)
147                 blobmsg_add_u32(&b, m->policy[i].name, m->policy[i].type);
148
149         blobmsg_close_table(&b, mtbl);
150 }
151
152 static bool ubus_push_object_type(const struct ubus_object_type *type)
153 {
154         void *s;
155         int i;
156
157         s = blob_nest_start(&b, UBUS_ATTR_SIGNATURE);
158
159         for (i = 0; i < type->n_methods; i++)
160                 ubus_push_method_data(&type->methods[i]);
161
162         blob_nest_end(&b, s);
163
164         return true;
165 }
166
167 int ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj)
168 {
169         struct ubus_request req;
170         int ret;
171
172         blob_buf_init(&b, 0);
173
174         if (obj->name && obj->type) {
175                 blob_put_string(&b, UBUS_ATTR_OBJPATH, obj->name);
176
177                 if (obj->type->id)
178                         blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id);
179                 else if (!ubus_push_object_type(obj->type))
180                         return UBUS_STATUS_INVALID_ARGUMENT;
181         }
182
183         if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_ADD_OBJECT, 0) < 0)
184                 return UBUS_STATUS_INVALID_ARGUMENT;
185
186         req.raw_data_cb = ubus_add_object_cb;
187         req.priv = obj;
188         ret = ubus_complete_request(ctx, &req, 0);
189         if (ret)
190                 return ret;
191
192         if (!obj->id)
193                 return UBUS_STATUS_NO_DATA;
194
195         return 0;
196 }
197
198 static void ubus_remove_object_cb(struct ubus_request *req, int type, struct blob_attr *msg)
199 {
200         struct ubus_object *obj = req->priv;
201         struct blob_attr **attrbuf = ubus_parse_msg(msg);
202
203         if (!attrbuf[UBUS_ATTR_OBJID])
204                 return;
205
206         obj->id = 0;
207
208         if (attrbuf[UBUS_ATTR_OBJTYPE] && obj->type)
209                 obj->type->id = 0;
210
211         avl_delete(&req->ctx->objects, &obj->avl);
212 }
213
214 int ubus_remove_object(struct ubus_context *ctx, struct ubus_object *obj)
215 {
216         struct ubus_request req;
217         int ret;
218
219         blob_buf_init(&b, 0);
220         blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id);
221
222         if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_REMOVE_OBJECT, 0) < 0)
223                 return UBUS_STATUS_INVALID_ARGUMENT;
224
225         req.raw_data_cb = ubus_remove_object_cb;
226         req.priv = obj;
227         ret = ubus_complete_request(ctx, &req, 0);
228         if (ret)
229                 return ret;
230
231         if (obj->id)
232                 return UBUS_STATUS_NO_DATA;
233
234         return 0;
235 }