2 * Copyright (C) 2011-2012 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 typedef void (*ubus_lookup_handler_t
)(struct ubus_context
*ctx
,
38 struct ubus_object_data
*obj
,
40 typedef int (*ubus_handler_t
)(struct ubus_context
*ctx
, struct ubus_object
*obj
,
41 struct ubus_request_data
*req
,
42 const char *method
, struct blob_attr
*msg
);
43 typedef void (*ubus_state_handler_t
)(struct ubus_context
*ctx
, struct ubus_object
*obj
);
44 typedef void (*ubus_remove_handler_t
)(struct ubus_context
*ctx
,
45 struct ubus_subscriber
*obj
, uint32_t id
);
46 typedef void (*ubus_event_handler_t
)(struct ubus_context
*ctx
, struct ubus_event_handler
*ev
,
47 const char *type
, struct blob_attr
*msg
);
48 typedef void (*ubus_data_handler_t
)(struct ubus_request
*req
,
49 int type
, struct blob_attr
*msg
);
50 typedef void (*ubus_complete_handler_t
)(struct ubus_request
*req
, int ret
);
51 typedef void (*ubus_notify_complete_handler_t
)(struct ubus_notify_request
*req
,
54 #define UBUS_OBJECT_TYPE(_name, _methods) \
58 .n_methods = ARRAY_SIZE(_methods), \
62 #define UBUS_METHOD(_name, _handler, _policy) \
65 .handler = _handler, \
67 .n_policy = ARRAY_SIZE(_policy) \
72 ubus_handler_t handler
;
74 const struct blobmsg_policy
*policy
;
78 struct ubus_object_type
{
82 const struct ubus_method
*methods
;
93 struct ubus_object_type
*type
;
95 ubus_state_handler_t subscribe_cb
;
98 const struct ubus_method
*methods
;
102 struct ubus_subscriber
{
103 struct ubus_object obj
;
106 ubus_remove_handler_t remove_cb
;
109 struct ubus_event_handler
{
110 struct ubus_object obj
;
112 ubus_event_handler_t cb
;
115 struct ubus_context
{
116 struct list_head requests
;
117 struct avl_tree objects
;
118 struct list_head pending
;
120 struct uloop_fd sock
;
123 uint16_t request_seq
;
126 void (*connection_lost
)(struct ubus_context
*ctx
);
129 struct ubus_msghdr hdr
;
130 char data
[UBUS_MAX_MSGLEN
];
134 struct ubus_object_data
{
138 struct blob_attr
*signature
;
141 struct ubus_request_data
{
148 struct ubus_request
{
149 struct list_head list
;
151 struct list_head pending
;
161 ubus_data_handler_t raw_data_cb
;
162 ubus_data_handler_t data_cb
;
163 ubus_complete_handler_t complete_cb
;
165 struct ubus_context
*ctx
;
169 struct ubus_notify_request
{
170 struct ubus_request req
;
172 ubus_notify_complete_handler_t status_cb
;
173 ubus_notify_complete_handler_t complete_cb
;
176 uint32_t id
[UBUS_MAX_NOTIFY_PEERS
+ 1];
179 struct ubus_context
*ubus_connect(const char *path
);
180 int ubus_reconnect(struct ubus_context
*ctx
, const char *path
);
181 void ubus_free(struct ubus_context
*ctx
);
183 const char *ubus_strerror(int error
);
185 static inline void ubus_add_uloop(struct ubus_context
*ctx
)
187 uloop_fd_add(&ctx
->sock
, ULOOP_BLOCKING
| ULOOP_READ
);
190 /* call this for read events on ctx->sock.fd when not using uloop */
191 static inline void ubus_handle_event(struct ubus_context
*ctx
)
193 ctx
->sock
.cb(&ctx
->sock
, ULOOP_READ
);
196 /* ----------- raw request handling ----------- */
198 /* wait for a request to complete and return its status */
199 int ubus_complete_request(struct ubus_context
*ctx
, struct ubus_request
*req
,
202 /* complete a request asynchronously */
203 void ubus_complete_request_async(struct ubus_context
*ctx
,
204 struct ubus_request
*req
);
206 /* abort an asynchronous request */
207 void ubus_abort_request(struct ubus_context
*ctx
, struct ubus_request
*req
);
209 /* ----------- objects ----------- */
211 int ubus_lookup(struct ubus_context
*ctx
, const char *path
,
212 ubus_lookup_handler_t cb
, void *priv
);
214 int ubus_lookup_id(struct ubus_context
*ctx
, const char *path
, uint32_t *id
);
216 /* make an object visible to remote connections */
217 int ubus_add_object(struct ubus_context
*ctx
, struct ubus_object
*obj
);
219 /* remove the object from the ubus connection */
220 int ubus_remove_object(struct ubus_context
*ctx
, struct ubus_object
*obj
);
222 /* add a subscriber notifications from another object */
223 int ubus_register_subscriber(struct ubus_context
*ctx
, struct ubus_subscriber
*obj
);
224 int ubus_subscribe(struct ubus_context
*ctx
, struct ubus_subscriber
*obj
, uint32_t id
);
225 int ubus_unsubscribe(struct ubus_context
*ctx
, struct ubus_subscriber
*obj
, uint32_t id
);
227 /* ----------- rpc ----------- */
229 /* invoke a method on a specific object */
230 int ubus_invoke(struct ubus_context
*ctx
, uint32_t obj
, const char *method
,
231 struct blob_attr
*msg
, ubus_data_handler_t cb
, void *priv
,
234 /* asynchronous version of ubus_invoke() */
235 int ubus_invoke_async(struct ubus_context
*ctx
, uint32_t obj
, const char *method
,
236 struct blob_attr
*msg
, struct ubus_request
*req
);
238 /* send a reply to an incoming object method call */
239 int ubus_send_reply(struct ubus_context
*ctx
, struct ubus_request_data
*req
,
240 struct blob_attr
*msg
);
242 static inline void ubus_defer_request(struct ubus_context
*ctx
,
243 struct ubus_request_data
*req
,
244 struct ubus_request_data
*new_req
)
246 memcpy(new_req
, req
, sizeof(*req
));
247 req
->deferred
= true;
250 void ubus_complete_deferred_request(struct ubus_context
*ctx
,
251 struct ubus_request_data
*req
, int ret
);
254 * send a notification to all subscribers of an object
255 * if timeout < 0, no reply is expected from subscribers
257 int ubus_notify(struct ubus_context
*ctx
, struct ubus_object
*obj
,
258 const char *type
, struct blob_attr
*msg
, int timeout
);
260 int ubus_notify_async(struct ubus_context
*ctx
, struct ubus_object
*obj
,
261 const char *type
, struct blob_attr
*msg
,
262 struct ubus_notify_request
*req
);
265 /* ----------- events ----------- */
267 int ubus_send_event(struct ubus_context
*ctx
, const char *id
,
268 struct blob_attr
*data
);
270 int ubus_register_event_handler(struct ubus_context
*ctx
,
271 struct ubus_event_handler
*ev
,
272 const char *pattern
);
274 static inline int ubus_unregister_event_handler(struct ubus_context
*ctx
,
275 struct ubus_event_handler
*ev
)
277 return ubus_remove_object(ctx
, &ev
->obj
);