a63ce31a7ca0d3b4a13473f4cce3a6702c884e32
[project/ubus.git] / libubus.h
1 /*
2  * Copyright (C) 2011-2014 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 #ifndef __LIBUBUS_H
15 #define __LIBUBUS_H
16
17 #include <libubox/avl.h>
18 #include <libubox/list.h>
19 #include <libubox/blobmsg.h>
20 #include <libubox/uloop.h>
21 #include <stdint.h>
22 #include "ubusmsg.h"
23 #include "ubus_common.h"
24
25 #define UBUS_MAX_NOTIFY_PEERS   16
26
27 struct ubus_context;
28 struct ubus_msg_src;
29 struct ubus_object;
30 struct ubus_request;
31 struct ubus_request_data;
32 struct ubus_object_data;
33 struct ubus_event_handler;
34 struct ubus_subscriber;
35 struct ubus_notify_request;
36
37 struct ubus_msghdr_buf {
38         struct ubus_msghdr hdr;
39         struct blob_attr *data;
40 };
41
42 static inline struct blob_attr *
43 ubus_msghdr_data(struct ubus_msghdr *hdr)
44 {
45         struct ubus_msghdr_buf *hdrbuf = container_of(hdr, typeof(*hdrbuf), hdr);
46         return hdrbuf->data;
47 }
48
49 typedef void (*ubus_lookup_handler_t)(struct ubus_context *ctx,
50                                       struct ubus_object_data *obj,
51                                       void *priv);
52 typedef int (*ubus_handler_t)(struct ubus_context *ctx, struct ubus_object *obj,
53                               struct ubus_request_data *req,
54                               const char *method, struct blob_attr *msg);
55 typedef void (*ubus_state_handler_t)(struct ubus_context *ctx, struct ubus_object *obj);
56 typedef void (*ubus_remove_handler_t)(struct ubus_context *ctx,
57                                       struct ubus_subscriber *obj, uint32_t id);
58 typedef void (*ubus_event_handler_t)(struct ubus_context *ctx, struct ubus_event_handler *ev,
59                                      const char *type, struct blob_attr *msg);
60 typedef void (*ubus_data_handler_t)(struct ubus_request *req,
61                                     int type, struct blob_attr *msg);
62 typedef void (*ubus_fd_handler_t)(struct ubus_request *req, int fd);
63 typedef void (*ubus_complete_handler_t)(struct ubus_request *req, int ret);
64 typedef void (*ubus_notify_complete_handler_t)(struct ubus_notify_request *req,
65                                                int idx, int ret);
66 typedef void (*ubus_connect_handler_t)(struct ubus_context *ctx);
67
68 #define UBUS_OBJECT_TYPE(_name, _methods)               \
69         {                                               \
70                 .name = _name,                          \
71                 .id = 0,                                \
72                 .n_methods = ARRAY_SIZE(_methods),      \
73                 .methods = _methods                     \
74         }
75
76 #define __UBUS_METHOD_NOARG(_name, _handler)            \
77         .name = _name,                                  \
78         .handler = _handler
79
80 #define __UBUS_METHOD(_name, _handler, _policy)         \
81         __UBUS_METHOD_NOARG(_name, _handler),           \
82         .policy = _policy,                              \
83         .n_policy = ARRAY_SIZE(_policy)
84
85 #define UBUS_METHOD(_name, _handler, _policy)           \
86         { __UBUS_METHOD(_name, _handler, _policy) }
87
88 #define UBUS_METHOD_MASK(_name, _handler, _policy, _mask) \
89         {                                               \
90                 __UBUS_METHOD(_name, _handler, _policy),\
91                 .mask = _mask                           \
92         }
93
94 #define UBUS_METHOD_NOARG(_name, _handler)              \
95         { __UBUS_METHOD_NOARG(_name, _handler) }
96
97 struct ubus_method {
98         const char *name;
99         ubus_handler_t handler;
100
101         unsigned long mask;
102         const struct blobmsg_policy *policy;
103         int n_policy;
104 };
105
106 struct ubus_object_type {
107         const char *name;
108         uint32_t id;
109
110         const struct ubus_method *methods;
111         int n_methods;
112 };
113
114 struct ubus_object {
115         struct avl_node avl;
116
117         const char *name;
118         uint32_t id;
119
120         const char *path;
121         struct ubus_object_type *type;
122
123         ubus_state_handler_t subscribe_cb;
124         bool has_subscribers;
125
126         const struct ubus_method *methods;
127         int n_methods;
128 };
129
130 struct ubus_subscriber {
131         struct ubus_object obj;
132
133         ubus_handler_t cb;
134         ubus_remove_handler_t remove_cb;
135 };
136
137 struct ubus_event_handler {
138         struct ubus_object obj;
139
140         ubus_event_handler_t cb;
141 };
142
143 struct ubus_context {
144         struct list_head requests;
145         struct avl_tree objects;
146         struct list_head pending;
147
148         struct uloop_fd sock;
149         struct uloop_timeout pending_timer;
150
151         uint32_t local_id;
152         uint16_t request_seq;
153         int stack_depth;
154
155         void (*connection_lost)(struct ubus_context *ctx);
156
157         struct ubus_msghdr_buf msgbuf;
158         uint32_t msgbuf_data_len;
159         int msgbuf_reduction_counter;
160 };
161
162 struct ubus_object_data {
163         uint32_t id;
164         uint32_t type_id;
165         const char *path;
166         struct blob_attr *signature;
167 };
168
169 struct ubus_request_data {
170         uint32_t object;
171         uint32_t peer;
172         uint16_t seq;
173
174         /* internal use */
175         bool deferred;
176         int fd;
177 };
178
179 struct ubus_request {
180         struct list_head list;
181
182         struct list_head pending;
183         int status_code;
184         bool status_msg;
185         bool blocked;
186         bool cancelled;
187         bool notify;
188
189         uint32_t peer;
190         uint16_t seq;
191
192         ubus_data_handler_t raw_data_cb;
193         ubus_data_handler_t data_cb;
194         ubus_fd_handler_t fd_cb;
195         ubus_complete_handler_t complete_cb;
196
197         struct ubus_context *ctx;
198         void *priv;
199 };
200
201 struct ubus_notify_request {
202         struct ubus_request req;
203
204         ubus_notify_complete_handler_t status_cb;
205         ubus_notify_complete_handler_t complete_cb;
206
207         uint32_t pending;
208         uint32_t id[UBUS_MAX_NOTIFY_PEERS + 1];
209 };
210
211 struct ubus_auto_conn {
212         struct ubus_context ctx;
213         struct uloop_timeout timer;
214         const char *path;
215         ubus_connect_handler_t cb;
216 };
217
218 struct ubus_context *ubus_connect(const char *path);
219 void ubus_auto_connect(struct ubus_auto_conn *conn);
220 int ubus_reconnect(struct ubus_context *ctx, const char *path);
221 void ubus_free(struct ubus_context *ctx);
222
223 const char *ubus_strerror(int error);
224
225 static inline void ubus_add_uloop(struct ubus_context *ctx)
226 {
227         uloop_fd_add(&ctx->sock, ULOOP_BLOCKING | ULOOP_READ);
228 }
229
230 /* call this for read events on ctx->sock.fd when not using uloop */
231 static inline void ubus_handle_event(struct ubus_context *ctx)
232 {
233         ctx->sock.cb(&ctx->sock, ULOOP_READ);
234 }
235
236 /* ----------- raw request handling ----------- */
237
238 /* wait for a request to complete and return its status */
239 int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req,
240                           int timeout);
241
242 /* complete a request asynchronously */
243 void ubus_complete_request_async(struct ubus_context *ctx,
244                                  struct ubus_request *req);
245
246 /* abort an asynchronous request */
247 void ubus_abort_request(struct ubus_context *ctx, struct ubus_request *req);
248
249 /* ----------- objects ----------- */
250
251 int ubus_lookup(struct ubus_context *ctx, const char *path,
252                 ubus_lookup_handler_t cb, void *priv);
253
254 int ubus_lookup_id(struct ubus_context *ctx, const char *path, uint32_t *id);
255
256 /* make an object visible to remote connections */
257 int ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj);
258
259 /* remove the object from the ubus connection */
260 int ubus_remove_object(struct ubus_context *ctx, struct ubus_object *obj);
261
262 /* add a subscriber notifications from another object */
263 int ubus_register_subscriber(struct ubus_context *ctx, struct ubus_subscriber *obj);
264
265 static inline int
266 ubus_unregister_subscriber(struct ubus_context *ctx, struct ubus_subscriber *obj)
267 {
268         return ubus_remove_object(ctx, &obj->obj);
269 }
270
271 int ubus_subscribe(struct ubus_context *ctx, struct ubus_subscriber *obj, uint32_t id);
272 int ubus_unsubscribe(struct ubus_context *ctx, struct ubus_subscriber *obj, uint32_t id);
273
274 /* ----------- rpc ----------- */
275
276 /* invoke a method on a specific object */
277 int ubus_invoke(struct ubus_context *ctx, uint32_t obj, const char *method,
278                 struct blob_attr *msg, ubus_data_handler_t cb, void *priv,
279                 int timeout);
280
281 /* asynchronous version of ubus_invoke() */
282 int ubus_invoke_async(struct ubus_context *ctx, uint32_t obj, const char *method,
283                       struct blob_attr *msg, struct ubus_request *req);
284
285 /* send a reply to an incoming object method call */
286 int ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req,
287                     struct blob_attr *msg);
288
289 static inline void ubus_defer_request(struct ubus_context *ctx,
290                                       struct ubus_request_data *req,
291                                       struct ubus_request_data *new_req)
292 {
293     memcpy(new_req, req, sizeof(*req));
294     req->deferred = true;
295 }
296
297 static inline void ubus_request_set_fd(struct ubus_context *ctx,
298                                        struct ubus_request_data *req, int fd)
299 {
300     req->fd = fd;
301 }
302
303 void ubus_complete_deferred_request(struct ubus_context *ctx,
304                                     struct ubus_request_data *req, int ret);
305
306 /*
307  * send a notification to all subscribers of an object
308  * if timeout < 0, no reply is expected from subscribers
309  */
310 int ubus_notify(struct ubus_context *ctx, struct ubus_object *obj,
311                 const char *type, struct blob_attr *msg, int timeout);
312
313 int ubus_notify_async(struct ubus_context *ctx, struct ubus_object *obj,
314                       const char *type, struct blob_attr *msg,
315                       struct ubus_notify_request *req);
316
317
318 /* ----------- events ----------- */
319
320 int ubus_send_event(struct ubus_context *ctx, const char *id,
321                     struct blob_attr *data);
322
323 int ubus_register_event_handler(struct ubus_context *ctx,
324                                 struct ubus_event_handler *ev,
325                                 const char *pattern);
326
327 static inline int ubus_unregister_event_handler(struct ubus_context *ctx,
328                                                 struct ubus_event_handler *ev)
329 {
330     return ubus_remove_object(ctx, &ev->obj);
331 }
332
333 #endif