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