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 static inline struct blob_attr
*
43 ubus_msghdr_data(struct ubus_msghdr
*hdr
)
45 struct ubus_msghdr_buf
*hdrbuf
= container_of(hdr
, typeof(*hdrbuf
), hdr
);
49 typedef void (*ubus_lookup_handler_t
)(struct ubus_context
*ctx
,
50 struct ubus_object_data
*obj
,
52 typedef int (*ubus_handler_t
)(struct ubus_context
*ctx
, struct ubus_object
*obj
,
53 struct ubus_request_data
*req
,
54 const char *method
, struct blob_attr
*msg
);
55 typedef void (*ubus_state_handler_t
)(struct ubus_context
*ctx
, struct ubus_object
*obj
);
56 typedef void (*ubus_remove_handler_t
)(struct ubus_context
*ctx
,
57 struct ubus_subscriber
*obj
, uint32_t id
);
58 typedef void (*ubus_event_handler_t
)(struct ubus_context
*ctx
, struct ubus_event_handler
*ev
,
59 const char *type
, struct blob_attr
*msg
);
60 typedef void (*ubus_data_handler_t
)(struct ubus_request
*req
,
61 int type
, struct blob_attr
*msg
);
62 typedef void (*ubus_fd_handler_t
)(struct ubus_request
*req
, int fd
);
63 typedef void (*ubus_complete_handler_t
)(struct ubus_request
*req
, int ret
);
64 typedef void (*ubus_notify_complete_handler_t
)(struct ubus_notify_request
*req
,
66 typedef void (*ubus_connect_handler_t
)(struct ubus_context
*ctx
);
68 #define UBUS_OBJECT_TYPE(_name, _methods) \
72 .n_methods = ARRAY_SIZE(_methods), \
76 #define __UBUS_METHOD_NOARG(_name, _handler) \
80 #define __UBUS_METHOD(_name, _handler, _policy) \
81 __UBUS_METHOD_NOARG(_name, _handler), \
83 .n_policy = ARRAY_SIZE(_policy)
85 #define UBUS_METHOD(_name, _handler, _policy) \
86 { __UBUS_METHOD(_name, _handler, _policy) }
88 #define UBUS_METHOD_MASK(_name, _handler, _policy, _mask) \
90 __UBUS_METHOD(_name, _handler, _policy),\
94 #define UBUS_METHOD_NOARG(_name, _handler) \
95 { __UBUS_METHOD_NOARG(_name, _handler) }
99 ubus_handler_t handler
;
102 const struct blobmsg_policy
*policy
;
106 struct ubus_object_type
{
110 const struct ubus_method
*methods
;
121 struct ubus_object_type
*type
;
123 ubus_state_handler_t subscribe_cb
;
124 bool has_subscribers
;
126 const struct ubus_method
*methods
;
130 struct ubus_subscriber
{
131 struct ubus_object obj
;
134 ubus_remove_handler_t remove_cb
;
137 struct ubus_event_handler
{
138 struct ubus_object obj
;
140 ubus_event_handler_t cb
;
143 struct ubus_context
{
144 struct list_head requests
;
145 struct avl_tree objects
;
146 struct list_head pending
;
148 struct uloop_fd sock
;
149 struct uloop_timeout pending_timer
;
152 uint16_t request_seq
;
155 void (*connection_lost
)(struct ubus_context
*ctx
);
157 struct ubus_msghdr_buf msgbuf
;
160 struct ubus_object_data
{
164 struct blob_attr
*signature
;
167 struct ubus_request_data
{
177 struct ubus_request
{
178 struct list_head list
;
180 struct list_head pending
;
190 ubus_data_handler_t raw_data_cb
;
191 ubus_data_handler_t data_cb
;
192 ubus_fd_handler_t fd_cb
;
193 ubus_complete_handler_t complete_cb
;
195 struct ubus_context
*ctx
;
199 struct ubus_notify_request
{
200 struct ubus_request req
;
202 ubus_notify_complete_handler_t status_cb
;
203 ubus_notify_complete_handler_t complete_cb
;
206 uint32_t id
[UBUS_MAX_NOTIFY_PEERS
+ 1];
209 struct ubus_auto_conn
{
210 struct ubus_context ctx
;
211 struct uloop_timeout timer
;
213 ubus_connect_handler_t cb
;
216 struct ubus_context
*ubus_connect(const char *path
);
217 void ubus_auto_connect(struct ubus_auto_conn
*conn
);
218 int ubus_reconnect(struct ubus_context
*ctx
, const char *path
);
219 void ubus_free(struct ubus_context
*ctx
);
221 const char *ubus_strerror(int error
);
223 static inline void ubus_add_uloop(struct ubus_context
*ctx
)
225 uloop_fd_add(&ctx
->sock
, ULOOP_BLOCKING
| ULOOP_READ
);
228 /* call this for read events on ctx->sock.fd when not using uloop */
229 static inline void ubus_handle_event(struct ubus_context
*ctx
)
231 ctx
->sock
.cb(&ctx
->sock
, ULOOP_READ
);
234 /* ----------- raw request handling ----------- */
236 /* wait for a request to complete and return its status */
237 int ubus_complete_request(struct ubus_context
*ctx
, struct ubus_request
*req
,
240 /* complete a request asynchronously */
241 void ubus_complete_request_async(struct ubus_context
*ctx
,
242 struct ubus_request
*req
);
244 /* abort an asynchronous request */
245 void ubus_abort_request(struct ubus_context
*ctx
, struct ubus_request
*req
);
247 /* ----------- objects ----------- */
249 int ubus_lookup(struct ubus_context
*ctx
, const char *path
,
250 ubus_lookup_handler_t cb
, void *priv
);
252 int ubus_lookup_id(struct ubus_context
*ctx
, const char *path
, uint32_t *id
);
254 /* make an object visible to remote connections */
255 int ubus_add_object(struct ubus_context
*ctx
, struct ubus_object
*obj
);
257 /* remove the object from the ubus connection */
258 int ubus_remove_object(struct ubus_context
*ctx
, struct ubus_object
*obj
);
260 /* add a subscriber notifications from another object */
261 int ubus_register_subscriber(struct ubus_context
*ctx
, struct ubus_subscriber
*obj
);
264 ubus_unregister_subscriber(struct ubus_context
*ctx
, struct ubus_subscriber
*obj
)
266 return ubus_remove_object(ctx
, &obj
->obj
);
269 int ubus_subscribe(struct ubus_context
*ctx
, struct ubus_subscriber
*obj
, uint32_t id
);
270 int ubus_unsubscribe(struct ubus_context
*ctx
, struct ubus_subscriber
*obj
, uint32_t id
);
272 /* ----------- rpc ----------- */
274 /* invoke a method on a specific object */
275 int ubus_invoke(struct ubus_context
*ctx
, uint32_t obj
, const char *method
,
276 struct blob_attr
*msg
, ubus_data_handler_t cb
, void *priv
,
279 /* asynchronous version of ubus_invoke() */
280 int ubus_invoke_async(struct ubus_context
*ctx
, uint32_t obj
, const char *method
,
281 struct blob_attr
*msg
, struct ubus_request
*req
);
283 /* send a reply to an incoming object method call */
284 int ubus_send_reply(struct ubus_context
*ctx
, struct ubus_request_data
*req
,
285 struct blob_attr
*msg
);
287 static inline void ubus_defer_request(struct ubus_context
*ctx
,
288 struct ubus_request_data
*req
,
289 struct ubus_request_data
*new_req
)
291 memcpy(new_req
, req
, sizeof(*req
));
292 req
->deferred
= true;
295 static inline void ubus_request_set_fd(struct ubus_context
*ctx
,
296 struct ubus_request_data
*req
, int fd
)
301 void ubus_complete_deferred_request(struct ubus_context
*ctx
,
302 struct ubus_request_data
*req
, int ret
);
305 * send a notification to all subscribers of an object
306 * if timeout < 0, no reply is expected from subscribers
308 int ubus_notify(struct ubus_context
*ctx
, struct ubus_object
*obj
,
309 const char *type
, struct blob_attr
*msg
, int timeout
);
311 int ubus_notify_async(struct ubus_context
*ctx
, struct ubus_object
*obj
,
312 const char *type
, struct blob_attr
*msg
,
313 struct ubus_notify_request
*req
);
316 /* ----------- events ----------- */
318 int ubus_send_event(struct ubus_context
*ctx
, const char *id
,
319 struct blob_attr
*data
);
321 int ubus_register_event_handler(struct ubus_context
*ctx
,
322 struct ubus_event_handler
*ev
,
323 const char *pattern
);
325 static inline int ubus_unregister_event_handler(struct ubus_context
*ctx
,
326 struct ubus_event_handler
*ev
)
328 return ubus_remove_object(ctx
, &ev
->obj
);