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