2 * Copyright (C) 2011-2014 Felix Fietkau <nbd@openwrt.org>
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
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.
21 #include <libubox/avl.h>
22 #include <libubox/list.h>
23 #include <libubox/blobmsg.h>
24 #include <libubox/uloop.h>
27 #include "ubus_common.h"
29 #define UBUS_MAX_NOTIFY_PEERS 16
35 struct ubus_request_data;
36 struct ubus_object_data;
37 struct ubus_event_handler;
38 struct ubus_subscriber;
39 struct ubus_notify_request;
41 struct ubus_msghdr_buf {
42 struct ubus_msghdr hdr;
43 struct blob_attr *data;
46 typedef void (*ubus_lookup_handler_t)(struct ubus_context *ctx,
47 struct ubus_object_data *obj,
49 typedef int (*ubus_handler_t)(struct ubus_context *ctx, struct ubus_object *obj,
50 struct ubus_request_data *req,
51 const char *method, struct blob_attr *msg);
52 typedef void (*ubus_state_handler_t)(struct ubus_context *ctx, struct ubus_object *obj);
53 typedef void (*ubus_remove_handler_t)(struct ubus_context *ctx,
54 struct ubus_subscriber *obj, uint32_t id);
55 typedef void (*ubus_event_handler_t)(struct ubus_context *ctx, struct ubus_event_handler *ev,
56 const char *type, struct blob_attr *msg);
57 typedef void (*ubus_data_handler_t)(struct ubus_request *req,
58 int type, struct blob_attr *msg);
59 typedef void (*ubus_fd_handler_t)(struct ubus_request *req, int fd);
60 typedef void (*ubus_complete_handler_t)(struct ubus_request *req, int ret);
61 typedef void (*ubus_notify_complete_handler_t)(struct ubus_notify_request *req,
63 typedef void (*ubus_notify_data_handler_t)(struct ubus_notify_request *req,
64 int type, struct blob_attr *msg);
65 typedef void (*ubus_connect_handler_t)(struct ubus_context *ctx);
67 #define UBUS_OBJECT_TYPE(_name, _methods) \
71 .n_methods = ARRAY_SIZE(_methods), \
75 #define __UBUS_METHOD_NOARG(_name, _handler, _tags) \
77 .handler = _handler, \
80 #define __UBUS_METHOD(_name, _handler, _policy, _tags) \
81 __UBUS_METHOD_NOARG(_name, _handler, _tags), \
83 .n_policy = ARRAY_SIZE(_policy)
85 #define UBUS_METHOD(_name, _handler, _policy) \
86 { __UBUS_METHOD(_name, _handler, _policy, 0) }
88 #define UBUS_METHOD_TAG(_name, _handler, _policy, _tags)\
89 { __UBUS_METHOD(_name, _handler, _policy, _tags) }
91 #define UBUS_METHOD_MASK(_name, _handler, _policy, _mask) \
93 __UBUS_METHOD(_name, _handler, _policy, 0),\
97 #define UBUS_METHOD_NOARG(_name, _handler) \
98 { __UBUS_METHOD_NOARG(_name, _handler, 0) }
100 #define UBUS_METHOD_TAG_NOARG(_name, _handler, _tags) \
101 { __UBUS_METHOD_NOARG(_name, _handler, _tags) }
103 #define UBUS_TAG_STATUS BIT(0)
104 #define UBUS_TAG_ADMIN BIT(1)
105 #define UBUS_TAG_PRIVATE BIT(2)
109 ubus_handler_t handler;
113 const struct blobmsg_policy *policy;
117 struct ubus_object_type {
121 const struct ubus_method *methods;
132 struct ubus_object_type *type;
134 ubus_state_handler_t subscribe_cb;
135 bool has_subscribers;
137 const struct ubus_method *methods;
141 struct ubus_subscriber {
142 struct ubus_object obj;
145 ubus_remove_handler_t remove_cb;
148 struct ubus_event_handler {
149 struct ubus_object obj;
151 ubus_event_handler_t cb;
154 struct ubus_context {
155 struct list_head requests;
156 struct avl_tree objects;
157 struct list_head pending;
159 struct uloop_fd sock;
160 struct uloop_timeout pending_timer;
163 uint16_t request_seq;
167 void (*connection_lost)(struct ubus_context *ctx);
168 void (*monitor_cb)(struct ubus_context *ctx, uint32_t seq, struct blob_attr *data);
170 struct ubus_msghdr_buf msgbuf;
171 uint32_t msgbuf_data_len;
172 int msgbuf_reduction_counter;
175 struct ubus_object_data {
179 struct blob_attr *signature;
182 struct ubus_acl_key {
188 struct ubus_request_data {
193 struct ubus_acl_key acl;
198 int req_fd; /* fd received from the initial request */
201 struct ubus_request {
202 struct list_head list;
204 struct list_head pending;
214 ubus_data_handler_t raw_data_cb;
215 ubus_data_handler_t data_cb;
216 ubus_fd_handler_t fd_cb;
217 ubus_complete_handler_t complete_cb;
221 struct ubus_context *ctx;
225 struct ubus_notify_request {
226 struct ubus_request req;
228 ubus_notify_complete_handler_t status_cb;
229 ubus_notify_complete_handler_t complete_cb;
230 ubus_notify_data_handler_t data_cb;
233 uint32_t id[UBUS_MAX_NOTIFY_PEERS + 1];
236 struct ubus_auto_conn {
237 struct ubus_context ctx;
238 struct uloop_timeout timer;
240 ubus_connect_handler_t cb;
243 struct ubus_context *ubus_connect(const char *path);
244 int ubus_connect_ctx(struct ubus_context *ctx, const char *path);
245 void ubus_auto_connect(struct ubus_auto_conn *conn);
246 int ubus_reconnect(struct ubus_context *ctx, const char *path);
248 /* call this only for struct ubus_context pointers returned by ubus_connect() */
249 void ubus_free(struct ubus_context *ctx);
251 /* call this only for struct ubus_context pointers initialised by ubus_connect_ctx() */
252 void ubus_shutdown(struct ubus_context *ctx);
254 static inline void ubus_auto_shutdown(struct ubus_auto_conn *conn)
256 uloop_timeout_cancel(&conn->timer);
257 ubus_shutdown(&conn->ctx);
260 const char *ubus_strerror(int error);
262 static inline void ubus_add_uloop(struct ubus_context *ctx)
264 uloop_fd_add(&ctx->sock, ULOOP_BLOCKING | ULOOP_READ);
267 /* call this for read events on ctx->sock.fd when not using uloop */
268 static inline void ubus_handle_event(struct ubus_context *ctx)
270 ctx->sock.cb(&ctx->sock, ULOOP_READ);
273 /* ----------- raw request handling ----------- */
275 /* wait for a request to complete and return its status */
276 int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req,
279 /* complete a request asynchronously */
280 void ubus_complete_request_async(struct ubus_context *ctx,
281 struct ubus_request *req);
283 /* abort an asynchronous request */
284 void ubus_abort_request(struct ubus_context *ctx, struct ubus_request *req);
286 /* ----------- objects ----------- */
288 int ubus_lookup(struct ubus_context *ctx, const char *path,
289 ubus_lookup_handler_t cb, void *priv);
291 int ubus_lookup_id(struct ubus_context *ctx, const char *path, uint32_t *id);
293 /* make an object visible to remote connections */
294 int ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj);
296 /* remove the object from the ubus connection */
297 int ubus_remove_object(struct ubus_context *ctx, struct ubus_object *obj);
299 /* add a subscriber notifications from another object */
300 int ubus_register_subscriber(struct ubus_context *ctx, struct ubus_subscriber *obj);
303 ubus_unregister_subscriber(struct ubus_context *ctx, struct ubus_subscriber *obj)
305 return ubus_remove_object(ctx, &obj->obj);
308 int ubus_subscribe(struct ubus_context *ctx, struct ubus_subscriber *obj, uint32_t id);
309 int ubus_unsubscribe(struct ubus_context *ctx, struct ubus_subscriber *obj, uint32_t id);
311 int __ubus_monitor(struct ubus_context *ctx, const char *type);
313 static inline int ubus_monitor_start(struct ubus_context *ctx)
315 return __ubus_monitor(ctx, "add");
318 static inline int ubus_monitor_stop(struct ubus_context *ctx)
320 return __ubus_monitor(ctx, "remove");
324 /* ----------- acl ----------- */
327 struct ubus_acl_key key;
329 struct blob_attr *acl;
332 extern struct avl_tree acl_objects;
333 int ubus_register_acl(struct ubus_context *ctx);
335 #define acl_for_each(o, m) \
336 if ((m)->object && (m)->user && (m)->group) \
337 avl_for_element_range(avl_find_ge_element(&acl_objects, m, o, avl), avl_find_le_element(&acl_objects, m, o, avl), o, avl)
339 /* ----------- rpc ----------- */
341 /* invoke a method on a specific object */
342 int ubus_invoke_fd(struct ubus_context *ctx, uint32_t obj, const char *method,
343 struct blob_attr *msg, ubus_data_handler_t cb, void *priv,
344 int timeout, int fd);
346 ubus_invoke(struct ubus_context *ctx, uint32_t obj, const char *method,
347 struct blob_attr *msg, ubus_data_handler_t cb, void *priv,
350 return ubus_invoke_fd(ctx, obj, method, msg, cb, priv, timeout, -1);
353 /* asynchronous version of ubus_invoke() */
354 int ubus_invoke_async_fd(struct ubus_context *ctx, uint32_t obj, const char *method,
355 struct blob_attr *msg, struct ubus_request *req, int fd);
357 ubus_invoke_async(struct ubus_context *ctx, uint32_t obj, const char *method,
358 struct blob_attr *msg, struct ubus_request *req)
360 return ubus_invoke_async_fd(ctx, obj, method, msg, req, -1);
363 /* send a reply to an incoming object method call */
364 int ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req,
365 struct blob_attr *msg);
367 static inline void ubus_defer_request(struct ubus_context *ctx,
368 struct ubus_request_data *req,
369 struct ubus_request_data *new_req)
372 memcpy(new_req, req, sizeof(*req));
373 req->deferred = true;
376 static inline void ubus_request_set_fd(struct ubus_context *ctx,
377 struct ubus_request_data *req, int fd)
383 static inline int ubus_request_get_caller_fd(struct ubus_request_data *req)
385 int fd = req->req_fd;
391 void ubus_complete_deferred_request(struct ubus_context *ctx,
392 struct ubus_request_data *req, int ret);
395 * send a notification to all subscribers of an object
396 * if timeout < 0, no reply is expected from subscribers
398 int ubus_notify(struct ubus_context *ctx, struct ubus_object *obj,
399 const char *type, struct blob_attr *msg, int timeout);
401 int ubus_notify_async(struct ubus_context *ctx, struct ubus_object *obj,
402 const char *type, struct blob_attr *msg,
403 struct ubus_notify_request *req);
406 /* ----------- events ----------- */
408 int ubus_send_event(struct ubus_context *ctx, const char *id,
409 struct blob_attr *data);
411 int ubus_register_event_handler(struct ubus_context *ctx,
412 struct ubus_event_handler *ev,
413 const char *pattern);
415 static inline int ubus_unregister_event_handler(struct ubus_context *ctx,
416 struct ubus_event_handler *ev)
418 return ubus_remove_object(ctx, &ev->obj);