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