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