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.
17 #include <libubox/avl.h>
18 #include <libubox/list.h>
19 #include <libubox/blobmsg.h>
20 #include <libubox/uloop.h>
23 #include "ubus_common.h"
25 #define UBUS_MAX_NOTIFY_PEERS 16
31 struct ubus_request_data;
32 struct ubus_object_data;
33 struct ubus_event_handler;
34 struct ubus_subscriber;
35 struct ubus_notify_request;
37 static inline struct blob_attr *
38 ubus_msghdr_data(struct ubus_msghdr *hdr)
40 return (struct blob_attr *) (hdr + 1);
43 typedef void (*ubus_lookup_handler_t)(struct ubus_context *ctx,
44 struct ubus_object_data *obj,
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,
60 typedef void (*ubus_connect_handler_t)(struct ubus_context *ctx);
62 #define UBUS_OBJECT_TYPE(_name, _methods) \
66 .n_methods = ARRAY_SIZE(_methods), \
70 #define __UBUS_METHOD_NOARG(_name, _handler) \
74 #define __UBUS_METHOD(_name, _handler, _policy) \
75 __UBUS_METHOD_NOARG(_name, _handler), \
77 .n_policy = ARRAY_SIZE(_policy)
79 #define UBUS_METHOD(_name, _handler, _policy) \
80 { __UBUS_METHOD(_name, _handler, _policy) }
82 #define UBUS_METHOD_MASK(_name, _handler, _policy, _mask) \
84 __UBUS_METHOD(_name, _handler, _policy),\
88 #define UBUS_METHOD_NOARG(_name, _handler) \
89 { __UBUS_METHOD_NOARG(_name, _handler) }
93 ubus_handler_t handler;
96 const struct blobmsg_policy *policy;
100 struct ubus_object_type {
104 const struct ubus_method *methods;
115 struct ubus_object_type *type;
117 ubus_state_handler_t subscribe_cb;
118 bool has_subscribers;
120 const struct ubus_method *methods;
124 struct ubus_subscriber {
125 struct ubus_object obj;
128 ubus_remove_handler_t remove_cb;
131 struct ubus_event_handler {
132 struct ubus_object obj;
134 ubus_event_handler_t cb;
137 struct ubus_context {
138 struct list_head requests;
139 struct avl_tree objects;
140 struct list_head pending;
142 struct uloop_fd sock;
143 struct uloop_timeout pending_timer;
146 uint16_t request_seq;
149 void (*connection_lost)(struct ubus_context *ctx);
152 struct ubus_msghdr hdr;
153 char data[UBUS_MAX_MSGLEN];
157 struct ubus_object_data {
161 struct blob_attr *signature;
164 struct ubus_request_data {
174 struct ubus_request {
175 struct list_head list;
177 struct list_head pending;
187 ubus_data_handler_t raw_data_cb;
188 ubus_data_handler_t data_cb;
189 ubus_fd_handler_t fd_cb;
190 ubus_complete_handler_t complete_cb;
192 struct ubus_context *ctx;
196 struct ubus_notify_request {
197 struct ubus_request req;
199 ubus_notify_complete_handler_t status_cb;
200 ubus_notify_complete_handler_t complete_cb;
203 uint32_t id[UBUS_MAX_NOTIFY_PEERS + 1];
206 struct ubus_auto_conn {
207 struct ubus_context ctx;
208 struct uloop_timeout timer;
210 ubus_connect_handler_t cb;
213 struct ubus_context *ubus_connect(const char *path);
214 void ubus_auto_connect(struct ubus_auto_conn *conn);
215 int ubus_reconnect(struct ubus_context *ctx, const char *path);
216 void ubus_free(struct ubus_context *ctx);
218 const char *ubus_strerror(int error);
220 static inline void ubus_add_uloop(struct ubus_context *ctx)
222 uloop_fd_add(&ctx->sock, ULOOP_BLOCKING | ULOOP_READ);
225 /* call this for read events on ctx->sock.fd when not using uloop */
226 static inline void ubus_handle_event(struct ubus_context *ctx)
228 ctx->sock.cb(&ctx->sock, ULOOP_READ);
231 /* ----------- raw request handling ----------- */
233 /* wait for a request to complete and return its status */
234 int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req,
237 /* complete a request asynchronously */
238 void ubus_complete_request_async(struct ubus_context *ctx,
239 struct ubus_request *req);
241 /* abort an asynchronous request */
242 void ubus_abort_request(struct ubus_context *ctx, struct ubus_request *req);
244 /* ----------- objects ----------- */
246 int ubus_lookup(struct ubus_context *ctx, const char *path,
247 ubus_lookup_handler_t cb, void *priv);
249 int ubus_lookup_id(struct ubus_context *ctx, const char *path, uint32_t *id);
251 /* make an object visible to remote connections */
252 int ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj);
254 /* remove the object from the ubus connection */
255 int ubus_remove_object(struct ubus_context *ctx, struct ubus_object *obj);
257 /* add a subscriber notifications from another object */
258 int ubus_register_subscriber(struct ubus_context *ctx, struct ubus_subscriber *obj);
261 ubus_unregister_subscriber(struct ubus_context *ctx, struct ubus_subscriber *obj)
263 return ubus_remove_object(ctx, &obj->obj);
266 int ubus_subscribe(struct ubus_context *ctx, struct ubus_subscriber *obj, uint32_t id);
267 int ubus_unsubscribe(struct ubus_context *ctx, struct ubus_subscriber *obj, uint32_t id);
269 /* ----------- rpc ----------- */
271 /* invoke a method on a specific object */
272 int ubus_invoke(struct ubus_context *ctx, uint32_t obj, const char *method,
273 struct blob_attr *msg, ubus_data_handler_t cb, void *priv,
276 /* asynchronous version of ubus_invoke() */
277 int ubus_invoke_async(struct ubus_context *ctx, uint32_t obj, const char *method,
278 struct blob_attr *msg, struct ubus_request *req);
280 /* send a reply to an incoming object method call */
281 int ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req,
282 struct blob_attr *msg);
284 static inline void ubus_defer_request(struct ubus_context *ctx,
285 struct ubus_request_data *req,
286 struct ubus_request_data *new_req)
288 memcpy(new_req, req, sizeof(*req));
289 req->deferred = true;
292 static inline void ubus_request_set_fd(struct ubus_context *ctx,
293 struct ubus_request_data *req, int fd)
298 void ubus_complete_deferred_request(struct ubus_context *ctx,
299 struct ubus_request_data *req, int ret);
302 * send a notification to all subscribers of an object
303 * if timeout < 0, no reply is expected from subscribers
305 int ubus_notify(struct ubus_context *ctx, struct ubus_object *obj,
306 const char *type, struct blob_attr *msg, int timeout);
308 int ubus_notify_async(struct ubus_context *ctx, struct ubus_object *obj,
309 const char *type, struct blob_attr *msg,
310 struct ubus_notify_request *req);
313 /* ----------- events ----------- */
315 int ubus_send_event(struct ubus_context *ctx, const char *id,
316 struct blob_attr *data);
318 int ubus_register_event_handler(struct ubus_context *ctx,
319 struct ubus_event_handler *ev,
320 const char *pattern);
322 static inline int ubus_unregister_event_handler(struct ubus_context *ctx,
323 struct ubus_event_handler *ev)
325 return ubus_remove_object(ctx, &ev->obj);