libubus: reset ctx->sock.error when doing ubus reconnect
[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, _tags) \
70 .name = _name, \
71 .handler = _handler, \
72 .tags = _tags
73
74 #define __UBUS_METHOD(_name, _handler, _policy, _tags) \
75 __UBUS_METHOD_NOARG(_name, _handler, _tags), \
76 .policy = _policy, \
77 .n_policy = ARRAY_SIZE(_policy)
78
79 #define UBUS_METHOD(_name, _handler, _policy) \
80 { __UBUS_METHOD(_name, _handler, _policy, 0) }
81
82 #define UBUS_METHOD_TAG(_name, _handler, _policy, _tags)\
83 { __UBUS_METHOD(_name, _handler, _policy, _tags) }
84
85 #define UBUS_METHOD_MASK(_name, _handler, _policy, _mask) \
86 { \
87 __UBUS_METHOD(_name, _handler, _policy, 0),\
88 .mask = _mask \
89 }
90
91 #define UBUS_METHOD_NOARG(_name, _handler) \
92 { __UBUS_METHOD_NOARG(_name, _handler, 0) }
93
94 #define UBUS_METHOD_TAG_NOARG(_name, _handler, _tags) \
95 { __UBUS_METHOD_NOARG(_name, _handler, _tags) }
96
97 #define UBUS_TAG_STATUS BIT(0)
98 #define UBUS_TAG_ADMIN BIT(1)
99 #define UBUS_TAG_PRIVATE BIT(2)
100
101 struct ubus_method {
102 const char *name;
103 ubus_handler_t handler;
104
105 unsigned long mask;
106 unsigned long tags;
107 const struct blobmsg_policy *policy;
108 int n_policy;
109 };
110
111 struct ubus_object_type {
112 const char *name;
113 uint32_t id;
114
115 const struct ubus_method *methods;
116 int n_methods;
117 };
118
119 struct ubus_object {
120 struct avl_node avl;
121
122 const char *name;
123 uint32_t id;
124
125 const char *path;
126 struct ubus_object_type *type;
127
128 ubus_state_handler_t subscribe_cb;
129 bool has_subscribers;
130
131 const struct ubus_method *methods;
132 int n_methods;
133 };
134
135 struct ubus_subscriber {
136 struct ubus_object obj;
137
138 ubus_handler_t cb;
139 ubus_remove_handler_t remove_cb;
140 };
141
142 struct ubus_event_handler {
143 struct ubus_object obj;
144
145 ubus_event_handler_t cb;
146 };
147
148 struct ubus_context {
149 struct list_head requests;
150 struct avl_tree objects;
151 struct list_head pending;
152
153 struct uloop_fd sock;
154 struct uloop_timeout pending_timer;
155
156 uint32_t local_id;
157 uint16_t request_seq;
158 bool cancel_poll;
159 int stack_depth;
160
161 void (*connection_lost)(struct ubus_context *ctx);
162 void (*monitor_cb)(struct ubus_context *ctx, uint32_t seq, struct blob_attr *data);
163
164 struct ubus_msghdr_buf msgbuf;
165 uint32_t msgbuf_data_len;
166 int msgbuf_reduction_counter;
167 };
168
169 struct ubus_object_data {
170 uint32_t id;
171 uint32_t type_id;
172 const char *path;
173 struct blob_attr *signature;
174 };
175
176 struct ubus_acl_key {
177 const char *user;
178 const char *group;
179 const char *object;
180 };
181
182 struct ubus_request_data {
183 uint32_t object;
184 uint32_t peer;
185 uint16_t seq;
186
187 struct ubus_acl_key acl;
188
189 /* internal use */
190 bool deferred;
191 int fd;
192 int req_fd; /* fd received from the initial request */
193 };
194
195 struct ubus_request {
196 struct list_head list;
197
198 struct list_head pending;
199 int status_code;
200 bool status_msg;
201 bool blocked;
202 bool cancelled;
203 bool notify;
204
205 uint32_t peer;
206 uint16_t seq;
207
208 ubus_data_handler_t raw_data_cb;
209 ubus_data_handler_t data_cb;
210 ubus_fd_handler_t fd_cb;
211 ubus_complete_handler_t complete_cb;
212
213 int fd;
214
215 struct ubus_context *ctx;
216 void *priv;
217 };
218
219 struct ubus_notify_request {
220 struct ubus_request req;
221
222 ubus_notify_complete_handler_t status_cb;
223 ubus_notify_complete_handler_t complete_cb;
224
225 uint32_t pending;
226 uint32_t id[UBUS_MAX_NOTIFY_PEERS + 1];
227 };
228
229 struct ubus_auto_conn {
230 struct ubus_context ctx;
231 struct uloop_timeout timer;
232 const char *path;
233 ubus_connect_handler_t cb;
234 };
235
236 struct ubus_context *ubus_connect(const char *path);
237 int ubus_connect_ctx(struct ubus_context *ctx, const char *path);
238 void ubus_auto_connect(struct ubus_auto_conn *conn);
239 int ubus_reconnect(struct ubus_context *ctx, const char *path);
240
241 /* call this only for struct ubus_context pointers returned by ubus_connect() */
242 void ubus_free(struct ubus_context *ctx);
243
244 /* call this only for struct ubus_context pointers initialised by ubus_connect_ctx() */
245 void ubus_shutdown(struct ubus_context *ctx);
246
247 static inline void ubus_auto_shutdown(struct ubus_auto_conn *conn)
248 {
249 uloop_timeout_cancel(&conn->timer);
250 ubus_shutdown(&conn->ctx);
251 }
252
253 const char *ubus_strerror(int error);
254
255 static inline void ubus_add_uloop(struct ubus_context *ctx)
256 {
257 uloop_fd_add(&ctx->sock, ULOOP_BLOCKING | ULOOP_READ);
258 }
259
260 /* call this for read events on ctx->sock.fd when not using uloop */
261 static inline void ubus_handle_event(struct ubus_context *ctx)
262 {
263 ctx->sock.cb(&ctx->sock, ULOOP_READ);
264 }
265
266 /* ----------- raw request handling ----------- */
267
268 /* wait for a request to complete and return its status */
269 int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req,
270 int timeout);
271
272 /* complete a request asynchronously */
273 void ubus_complete_request_async(struct ubus_context *ctx,
274 struct ubus_request *req);
275
276 /* abort an asynchronous request */
277 void ubus_abort_request(struct ubus_context *ctx, struct ubus_request *req);
278
279 /* ----------- objects ----------- */
280
281 int ubus_lookup(struct ubus_context *ctx, const char *path,
282 ubus_lookup_handler_t cb, void *priv);
283
284 int ubus_lookup_id(struct ubus_context *ctx, const char *path, uint32_t *id);
285
286 /* make an object visible to remote connections */
287 int ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj);
288
289 /* remove the object from the ubus connection */
290 int ubus_remove_object(struct ubus_context *ctx, struct ubus_object *obj);
291
292 /* add a subscriber notifications from another object */
293 int ubus_register_subscriber(struct ubus_context *ctx, struct ubus_subscriber *obj);
294
295 static inline int
296 ubus_unregister_subscriber(struct ubus_context *ctx, struct ubus_subscriber *obj)
297 {
298 return ubus_remove_object(ctx, &obj->obj);
299 }
300
301 int ubus_subscribe(struct ubus_context *ctx, struct ubus_subscriber *obj, uint32_t id);
302 int ubus_unsubscribe(struct ubus_context *ctx, struct ubus_subscriber *obj, uint32_t id);
303
304 int __ubus_monitor(struct ubus_context *ctx, const char *type);
305
306 static inline int ubus_monitor_start(struct ubus_context *ctx)
307 {
308 return __ubus_monitor(ctx, "add");
309 }
310
311 static inline int ubus_monitor_stop(struct ubus_context *ctx)
312 {
313 return __ubus_monitor(ctx, "remove");
314 }
315
316
317 /* ----------- acl ----------- */
318
319 struct acl_object {
320 struct ubus_acl_key key;
321 struct avl_node avl;
322 struct blob_attr *acl;
323 };
324
325 extern struct avl_tree acl_objects;
326 int ubus_register_acl(struct ubus_context *ctx);
327
328 #define acl_for_each(o, m) \
329 if ((m)->object && (m)->user && (m)->group) \
330 avl_for_element_range(avl_find_ge_element(&acl_objects, m, o, avl), avl_find_le_element(&acl_objects, m, o, avl), o, avl)
331
332 /* ----------- rpc ----------- */
333
334 /* invoke a method on a specific object */
335 int ubus_invoke_fd(struct ubus_context *ctx, uint32_t obj, const char *method,
336 struct blob_attr *msg, ubus_data_handler_t cb, void *priv,
337 int timeout, int fd);
338 static inline int
339 ubus_invoke(struct ubus_context *ctx, uint32_t obj, const char *method,
340 struct blob_attr *msg, ubus_data_handler_t cb, void *priv,
341 int timeout)
342 {
343 return ubus_invoke_fd(ctx, obj, method, msg, cb, priv, timeout, -1);
344 }
345
346 /* asynchronous version of ubus_invoke() */
347 int ubus_invoke_async_fd(struct ubus_context *ctx, uint32_t obj, const char *method,
348 struct blob_attr *msg, struct ubus_request *req, int fd);
349 static inline int
350 ubus_invoke_async(struct ubus_context *ctx, uint32_t obj, const char *method,
351 struct blob_attr *msg, struct ubus_request *req)
352 {
353 return ubus_invoke_async_fd(ctx, obj, method, msg, req, -1);
354 }
355
356 /* send a reply to an incoming object method call */
357 int ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req,
358 struct blob_attr *msg);
359
360 static inline void ubus_defer_request(struct ubus_context *ctx,
361 struct ubus_request_data *req,
362 struct ubus_request_data *new_req)
363 {
364 (void) ctx;
365 memcpy(new_req, req, sizeof(*req));
366 req->deferred = true;
367 }
368
369 static inline void ubus_request_set_fd(struct ubus_context *ctx,
370 struct ubus_request_data *req, int fd)
371 {
372 (void) ctx;
373 req->fd = fd;
374 }
375
376 static inline int ubus_request_get_caller_fd(struct ubus_request_data *req)
377 {
378 int fd = req->req_fd;
379 req->req_fd = -1;
380
381 return fd;
382 }
383
384 void ubus_complete_deferred_request(struct ubus_context *ctx,
385 struct ubus_request_data *req, int ret);
386
387 /*
388 * send a notification to all subscribers of an object
389 * if timeout < 0, no reply is expected from subscribers
390 */
391 int ubus_notify(struct ubus_context *ctx, struct ubus_object *obj,
392 const char *type, struct blob_attr *msg, int timeout);
393
394 int ubus_notify_async(struct ubus_context *ctx, struct ubus_object *obj,
395 const char *type, struct blob_attr *msg,
396 struct ubus_notify_request *req);
397
398
399 /* ----------- events ----------- */
400
401 int ubus_send_event(struct ubus_context *ctx, const char *id,
402 struct blob_attr *data);
403
404 int ubus_register_event_handler(struct ubus_context *ctx,
405 struct ubus_event_handler *ev,
406 const char *pattern);
407
408 static inline int ubus_unregister_event_handler(struct ubus_context *ctx,
409 struct ubus_event_handler *ev)
410 {
411 return ubus_remove_object(ctx, &ev->obj);
412 }
413
414 #endif