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 struct ubus_msghdr_buf
{
38 struct ubus_msghdr hdr
;
39 struct blob_attr
*data
;
42 typedef void (*ubus_lookup_handler_t
)(struct ubus_context
*ctx
,
43 struct ubus_object_data
*obj
,
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
,
59 typedef void (*ubus_connect_handler_t
)(struct ubus_context
*ctx
);
61 #define UBUS_OBJECT_TYPE(_name, _methods) \
65 .n_methods = ARRAY_SIZE(_methods), \
69 #define __UBUS_METHOD_NOARG(_name, _handler, _tags) \
71 .handler = _handler, \
74 #define __UBUS_METHOD(_name, _handler, _policy, _tags) \
75 __UBUS_METHOD_NOARG(_name, _handler, _tags), \
77 .n_policy = ARRAY_SIZE(_policy)
79 #define UBUS_METHOD(_name, _handler, _policy) \
80 { __UBUS_METHOD(_name, _handler, _policy, 0) }
82 #define UBUS_METHOD_TAG(_name, _handler, _policy, _tags)\
83 { __UBUS_METHOD(_name, _handler, _policy, _tags) }
85 #define UBUS_METHOD_MASK(_name, _handler, _policy, _mask) \
87 __UBUS_METHOD(_name, _handler, _policy, 0),\
91 #define UBUS_METHOD_NOARG(_name, _handler) \
92 { __UBUS_METHOD_NOARG(_name, _handler, 0) }
94 #define UBUS_METHOD_TAG_NOARG(_name, _handler, _tags) \
95 { __UBUS_METHOD_NOARG(_name, _handler, _tags) }
97 #define UBUS_TAG_STATUS BIT(0)
98 #define UBUS_TAG_ADMIN BIT(1)
99 #define UBUS_TAG_PRIVATE BIT(2)
103 ubus_handler_t handler
;
107 const struct blobmsg_policy
*policy
;
111 struct ubus_object_type
{
115 const struct ubus_method
*methods
;
126 struct ubus_object_type
*type
;
128 ubus_state_handler_t subscribe_cb
;
129 bool has_subscribers
;
131 const struct ubus_method
*methods
;
135 struct ubus_subscriber
{
136 struct ubus_object obj
;
139 ubus_remove_handler_t remove_cb
;
142 struct ubus_event_handler
{
143 struct ubus_object obj
;
145 ubus_event_handler_t cb
;
148 struct ubus_context
{
149 struct list_head requests
;
150 struct avl_tree objects
;
151 struct list_head pending
;
153 struct uloop_fd sock
;
154 struct uloop_timeout pending_timer
;
157 uint16_t request_seq
;
160 void (*connection_lost
)(struct ubus_context
*ctx
);
162 struct ubus_msghdr_buf msgbuf
;
163 uint32_t msgbuf_data_len
;
164 int msgbuf_reduction_counter
;
167 struct ubus_object_data
{
171 struct blob_attr
*signature
;
174 struct ubus_request_data
{
184 struct ubus_request
{
185 struct list_head list
;
187 struct list_head pending
;
197 ubus_data_handler_t raw_data_cb
;
198 ubus_data_handler_t data_cb
;
199 ubus_fd_handler_t fd_cb
;
200 ubus_complete_handler_t complete_cb
;
202 struct ubus_context
*ctx
;
206 struct ubus_notify_request
{
207 struct ubus_request req
;
209 ubus_notify_complete_handler_t status_cb
;
210 ubus_notify_complete_handler_t complete_cb
;
213 uint32_t id
[UBUS_MAX_NOTIFY_PEERS
+ 1];
216 struct ubus_auto_conn
{
217 struct ubus_context ctx
;
218 struct uloop_timeout timer
;
220 ubus_connect_handler_t cb
;
223 struct ubus_context
*ubus_connect(const char *path
);
224 int ubus_connect_ctx(struct ubus_context
*ctx
, const char *path
);
225 void ubus_auto_connect(struct ubus_auto_conn
*conn
);
226 int ubus_reconnect(struct ubus_context
*ctx
, const char *path
);
228 /* call this only for struct ubus_context pointers returned by ubus_connect() */
229 void ubus_free(struct ubus_context
*ctx
);
231 /* call this only for struct ubus_context pointers initialised by ubus_connect_ctx() */
232 void ubus_shutdown(struct ubus_context
*ctx
);
234 const char *ubus_strerror(int error
);
236 static inline void ubus_add_uloop(struct ubus_context
*ctx
)
238 uloop_fd_add(&ctx
->sock
, ULOOP_BLOCKING
| ULOOP_READ
);
241 /* call this for read events on ctx->sock.fd when not using uloop */
242 static inline void ubus_handle_event(struct ubus_context
*ctx
)
244 ctx
->sock
.cb(&ctx
->sock
, ULOOP_READ
);
247 /* ----------- raw request handling ----------- */
249 /* wait for a request to complete and return its status */
250 int ubus_complete_request(struct ubus_context
*ctx
, struct ubus_request
*req
,
253 /* complete a request asynchronously */
254 void ubus_complete_request_async(struct ubus_context
*ctx
,
255 struct ubus_request
*req
);
257 /* abort an asynchronous request */
258 void ubus_abort_request(struct ubus_context
*ctx
, struct ubus_request
*req
);
260 /* ----------- objects ----------- */
262 int ubus_lookup(struct ubus_context
*ctx
, const char *path
,
263 ubus_lookup_handler_t cb
, void *priv
);
265 int ubus_lookup_id(struct ubus_context
*ctx
, const char *path
, uint32_t *id
);
267 /* make an object visible to remote connections */
268 int ubus_add_object(struct ubus_context
*ctx
, struct ubus_object
*obj
);
270 /* remove the object from the ubus connection */
271 int ubus_remove_object(struct ubus_context
*ctx
, struct ubus_object
*obj
);
273 /* add a subscriber notifications from another object */
274 int ubus_register_subscriber(struct ubus_context
*ctx
, struct ubus_subscriber
*obj
);
277 ubus_unregister_subscriber(struct ubus_context
*ctx
, struct ubus_subscriber
*obj
)
279 return ubus_remove_object(ctx
, &obj
->obj
);
282 int ubus_subscribe(struct ubus_context
*ctx
, struct ubus_subscriber
*obj
, uint32_t id
);
283 int ubus_unsubscribe(struct ubus_context
*ctx
, struct ubus_subscriber
*obj
, uint32_t id
);
285 /* ----------- rpc ----------- */
287 /* invoke a method on a specific object */
288 int ubus_invoke(struct ubus_context
*ctx
, uint32_t obj
, const char *method
,
289 struct blob_attr
*msg
, ubus_data_handler_t cb
, void *priv
,
292 /* asynchronous version of ubus_invoke() */
293 int ubus_invoke_async(struct ubus_context
*ctx
, uint32_t obj
, const char *method
,
294 struct blob_attr
*msg
, struct ubus_request
*req
);
296 /* send a reply to an incoming object method call */
297 int ubus_send_reply(struct ubus_context
*ctx
, struct ubus_request_data
*req
,
298 struct blob_attr
*msg
);
300 static inline void ubus_defer_request(struct ubus_context
*ctx
,
301 struct ubus_request_data
*req
,
302 struct ubus_request_data
*new_req
)
304 memcpy(new_req
, req
, sizeof(*req
));
305 req
->deferred
= true;
308 static inline void ubus_request_set_fd(struct ubus_context
*ctx
,
309 struct ubus_request_data
*req
, int fd
)
314 void ubus_complete_deferred_request(struct ubus_context
*ctx
,
315 struct ubus_request_data
*req
, int ret
);
318 * send a notification to all subscribers of an object
319 * if timeout < 0, no reply is expected from subscribers
321 int ubus_notify(struct ubus_context
*ctx
, struct ubus_object
*obj
,
322 const char *type
, struct blob_attr
*msg
, int timeout
);
324 int ubus_notify_async(struct ubus_context
*ctx
, struct ubus_object
*obj
,
325 const char *type
, struct blob_attr
*msg
,
326 struct ubus_notify_request
*req
);
329 /* ----------- events ----------- */
331 int ubus_send_event(struct ubus_context
*ctx
, const char *id
,
332 struct blob_attr
*data
);
334 int ubus_register_event_handler(struct ubus_context
*ctx
,
335 struct ubus_event_handler
*ev
,
336 const char *pattern
);
338 static inline int ubus_unregister_event_handler(struct ubus_context
*ctx
,
339 struct ubus_event_handler
*ev
)
341 return ubus_remove_object(ctx
, &ev
->obj
);