libubus: refactor code, move request handling to libubus-req.c
[project/ubus.git] / libubus.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 <sys/types.h>
15 #include <sys/socket.h>
16 #include <unistd.h>
17
18 #include <libubox/blob.h>
19 #include <libubox/blobmsg.h>
20
21 #include "libubus.h"
22 #include "libubus-internal.h"
23 #include "ubusmsg.h"
24
25 const char *__ubus_strerror[__UBUS_STATUS_LAST] = {
26         [UBUS_STATUS_OK] = "Success",
27         [UBUS_STATUS_INVALID_COMMAND] = "Invalid command",
28         [UBUS_STATUS_INVALID_ARGUMENT] = "Invalid argument",
29         [UBUS_STATUS_METHOD_NOT_FOUND] = "Method not found",
30         [UBUS_STATUS_NOT_FOUND] = "Not found",
31         [UBUS_STATUS_NO_DATA] = "No response",
32         [UBUS_STATUS_PERMISSION_DENIED] = "Permission denied",
33         [UBUS_STATUS_TIMEOUT] = "Request timed out",
34         [UBUS_STATUS_NOT_SUPPORTED] = "Operation not supported",
35         [UBUS_STATUS_UNKNOWN_ERROR] = "Unknown error",
36         [UBUS_STATUS_CONNECTION_FAILED] = "Connection failed",
37 };
38
39 struct blob_buf b __hidden = {};
40
41 struct ubus_pending_msg {
42         struct list_head list;
43         struct ubus_msghdr hdr;
44 };
45
46 static int ubus_cmp_id(const void *k1, const void *k2, void *ptr)
47 {
48         const uint32_t *id1 = k1, *id2 = k2;
49
50         if (*id1 < *id2)
51                 return -1;
52         else
53                 return *id1 > *id2;
54 }
55
56 const char *ubus_strerror(int error)
57 {
58         static char err[32];
59
60         if (error < 0 || error >= __UBUS_STATUS_LAST)
61                 goto out;
62
63         if (!__ubus_strerror[error])
64                 goto out;
65
66         return __ubus_strerror[error];
67
68 out:
69         sprintf(err, "Unknown error: %d", error);
70         return err;
71 }
72
73 static void
74 ubus_queue_msg(struct ubus_context *ctx, struct ubus_msghdr *hdr)
75 {
76         struct ubus_pending_msg *pending;
77
78         pending = calloc(1, sizeof(*pending) + blob_raw_len(hdr->data));
79         if (!pending)
80                 return;
81
82         memcpy(&pending->hdr, hdr, sizeof(*hdr) + blob_raw_len(hdr->data));
83         list_add(&pending->list, &ctx->pending);
84 }
85
86 void __hidden
87 ubus_process_msg(struct ubus_context *ctx, struct ubus_msghdr *hdr)
88 {
89
90         switch(hdr->type) {
91         case UBUS_MSG_STATUS:
92         case UBUS_MSG_DATA:
93                 ubus_process_req_msg(ctx, hdr);
94                 break;
95
96         case UBUS_MSG_INVOKE:
97         case UBUS_MSG_UNSUBSCRIBE:
98         case UBUS_MSG_NOTIFY:
99                 if (ctx->stack_depth > 2) {
100                         ubus_queue_msg(ctx, hdr);
101                         break;
102                 }
103
104                 ubus_process_obj_msg(ctx, hdr);
105                 break;
106         }
107 }
108
109 void __hidden ubus_process_pending_msg(struct ubus_context *ctx)
110 {
111         struct ubus_pending_msg *pending, *tmp;
112
113         list_for_each_entry_safe(pending, tmp, &ctx->pending, list) {
114                 list_del(&pending->list);
115                 ubus_process_msg(ctx, &pending->hdr);
116                 free(pending);
117                 if (ctx->stack_depth > 2)
118                         break;
119         }
120 }
121
122 struct ubus_lookup_request {
123         struct ubus_request req;
124         ubus_lookup_handler_t cb;
125 };
126
127 static void ubus_lookup_cb(struct ubus_request *ureq, int type, struct blob_attr *msg)
128 {
129         struct ubus_lookup_request *req;
130         struct ubus_object_data obj;
131         struct blob_attr **attr;
132
133         req = container_of(ureq, struct ubus_lookup_request, req);
134         attr = ubus_parse_msg(msg);
135
136         if (!attr[UBUS_ATTR_OBJID] || !attr[UBUS_ATTR_OBJPATH] ||
137             !attr[UBUS_ATTR_OBJTYPE])
138                 return;
139
140         memset(&obj, 0, sizeof(obj));
141         obj.id = blob_get_u32(attr[UBUS_ATTR_OBJID]);
142         obj.path = blob_data(attr[UBUS_ATTR_OBJPATH]);
143         obj.type_id = blob_get_u32(attr[UBUS_ATTR_OBJTYPE]);
144         obj.signature = attr[UBUS_ATTR_SIGNATURE];
145         req->cb(ureq->ctx, &obj, ureq->priv);
146 }
147
148 int __hidden ubus_start_request(struct ubus_context *ctx, struct ubus_request *req,
149                                 struct blob_attr *msg, int cmd, uint32_t peer)
150 {
151         memset(req, 0, sizeof(*req));
152
153         if (msg && blob_pad_len(msg) > UBUS_MAX_MSGLEN)
154                 return -1;
155
156         INIT_LIST_HEAD(&req->list);
157         INIT_LIST_HEAD(&req->pending);
158         req->ctx = ctx;
159         req->peer = peer;
160         req->seq = ++ctx->request_seq;
161         return ubus_send_msg(ctx, req->seq, msg, cmd, peer);
162 }
163
164 int ubus_lookup(struct ubus_context *ctx, const char *path,
165                 ubus_lookup_handler_t cb, void *priv)
166 {
167         struct ubus_lookup_request lookup;
168
169         blob_buf_init(&b, 0);
170         if (path)
171                 blob_put_string(&b, UBUS_ATTR_OBJPATH, path);
172
173         if (ubus_start_request(ctx, &lookup.req, b.head, UBUS_MSG_LOOKUP, 0) < 0)
174                 return UBUS_STATUS_INVALID_ARGUMENT;
175
176         lookup.req.raw_data_cb = ubus_lookup_cb;
177         lookup.req.priv = priv;
178         lookup.cb = cb;
179         return ubus_complete_request(ctx, &lookup.req, 0);
180 }
181
182 static void ubus_lookup_id_cb(struct ubus_request *req, int type, struct blob_attr *msg)
183 {
184         struct blob_attr **attr;
185         uint32_t *id = req->priv;
186
187         attr = ubus_parse_msg(msg);
188
189         if (!attr[UBUS_ATTR_OBJID])
190                 return;
191
192         *id = blob_get_u32(attr[UBUS_ATTR_OBJID]);
193 }
194
195 int ubus_lookup_id(struct ubus_context *ctx, const char *path, uint32_t *id)
196 {
197         struct ubus_request req;
198
199         blob_buf_init(&b, 0);
200         if (path)
201                 blob_put_string(&b, UBUS_ATTR_OBJPATH, path);
202
203         if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_LOOKUP, 0) < 0)
204                 return UBUS_STATUS_INVALID_ARGUMENT;
205
206         req.raw_data_cb = ubus_lookup_id_cb;
207         req.priv = id;
208
209         return ubus_complete_request(ctx, &req, 0);
210 }
211
212 static int ubus_event_cb(struct ubus_context *ctx, struct ubus_object *obj,
213                          struct ubus_request_data *req,
214                          const char *method, struct blob_attr *msg)
215 {
216         struct ubus_event_handler *ev;
217
218         ev = container_of(obj, struct ubus_event_handler, obj);
219         ev->cb(ctx, ev, method, msg);
220         return 0;
221 }
222
223 static const struct ubus_method event_method = {
224         .name = NULL,
225         .handler = ubus_event_cb,
226 };
227
228 int ubus_register_event_handler(struct ubus_context *ctx,
229                                 struct ubus_event_handler *ev,
230                                 const char *pattern)
231 {
232         struct ubus_object *obj = &ev->obj;
233         struct blob_buf b2;
234         int ret;
235
236         if (!obj->id) {
237                 obj->methods = &event_method;
238                 obj->n_methods = 1;
239
240                 if (!!obj->name ^ !!obj->type)
241                         return UBUS_STATUS_INVALID_ARGUMENT;
242
243                 ret = ubus_add_object(ctx, obj);
244                 if (ret)
245                         return ret;
246         }
247
248         /* use a second buffer, ubus_invoke() overwrites the primary one */
249         memset(&b2, 0, sizeof(b2));
250         blob_buf_init(&b2, 0);
251         blobmsg_add_u32(&b2, "object", obj->id);
252         if (pattern)
253                 blobmsg_add_string(&b2, "pattern", pattern);
254
255         return ubus_invoke(ctx, UBUS_SYSTEM_OBJECT_EVENT, "register", b2.head,
256                           NULL, NULL, 0);
257 }
258
259 int ubus_send_event(struct ubus_context *ctx, const char *id,
260                     struct blob_attr *data)
261 {
262         struct ubus_request req;
263         void *s;
264
265         blob_buf_init(&b, 0);
266         blob_put_int32(&b, UBUS_ATTR_OBJID, UBUS_SYSTEM_OBJECT_EVENT);
267         blob_put_string(&b, UBUS_ATTR_METHOD, "send");
268         s = blob_nest_start(&b, UBUS_ATTR_DATA);
269         blobmsg_add_string(&b, "id", id);
270         blobmsg_add_field(&b, BLOBMSG_TYPE_TABLE, "data", blob_data(data), blob_len(data));
271         blob_nest_end(&b, s);
272
273         if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_INVOKE, UBUS_SYSTEM_OBJECT_EVENT) < 0)
274                 return UBUS_STATUS_INVALID_ARGUMENT;
275
276         return ubus_complete_request(ctx, &req, 0);
277 }
278
279 static void ubus_default_connection_lost(struct ubus_context *ctx)
280 {
281         if (ctx->sock.registered)
282                 uloop_end();
283 }
284
285 struct ubus_context *ubus_connect(const char *path)
286 {
287         struct ubus_context *ctx;
288
289         ctx = calloc(1, sizeof(*ctx));
290         if (!ctx)
291                 return NULL;
292
293         ctx->sock.fd = -1;
294         ctx->sock.cb = ubus_handle_data;
295         ctx->connection_lost = ubus_default_connection_lost;
296
297         INIT_LIST_HEAD(&ctx->requests);
298         INIT_LIST_HEAD(&ctx->pending);
299         avl_init(&ctx->objects, ubus_cmp_id, false, NULL);
300         if (ubus_reconnect(ctx, path)) {
301                 free(ctx);
302                 ctx = NULL;
303         }
304
305         return ctx;
306 }
307
308 void ubus_free(struct ubus_context *ctx)
309 {
310         close(ctx->sock.fd);
311         free(ctx);
312 }