2 * Copyright (C) 2011 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"
29 struct ubus_request_data
;
30 struct ubus_object_data
;
31 struct ubus_event_handler
;
32 struct ubus_watch_object
;
34 typedef void (*ubus_lookup_handler_t
)(struct ubus_context
*ctx
,
35 struct ubus_object_data
*obj
,
37 typedef int (*ubus_handler_t
)(struct ubus_context
*ctx
, struct ubus_object
*obj
,
38 struct ubus_request_data
*req
,
39 const char *method
, struct blob_attr
*msg
);
40 typedef void (*ubus_event_handler_t
)(struct ubus_context
*ctx
, struct ubus_event_handler
*ev
,
41 const char *type
, struct blob_attr
*msg
);
42 typedef void (*ubus_watch_handler_t
)(struct ubus_context
*ctx
, struct ubus_watch_object
*w
,
44 typedef void (*ubus_data_handler_t
)(struct ubus_request
*req
,
45 int type
, struct blob_attr
*msg
);
46 typedef void (*ubus_complete_handler_t
)(struct ubus_request
*req
, int ret
);
48 #define UBUS_OBJECT_TYPE(_name, _methods) \
52 .n_methods = ARRAY_SIZE(_methods), \
56 #define UBUS_METHOD(_name, _handler, _policy) \
59 .handler = _handler, \
61 .n_policy = ARRAY_SIZE(_policy) \
66 ubus_handler_t handler
;
68 const struct blobmsg_policy
*policy
;
72 struct ubus_object_type
{
76 const struct ubus_method
*methods
;
87 struct ubus_object_type
*type
;
89 const struct ubus_method
*methods
;
93 struct ubus_watch_object
{
94 struct ubus_object obj
;
96 ubus_watch_handler_t cb
;
99 struct ubus_event_handler
{
100 struct ubus_object obj
;
102 ubus_event_handler_t cb
;
105 struct ubus_context
{
106 struct list_head requests
;
107 struct avl_tree objects
;
108 struct list_head pending
;
110 struct uloop_fd sock
;
113 uint32_t request_seq
;
116 void (*connection_lost
)(struct ubus_context
*ctx
);
119 struct ubus_msghdr hdr
;
120 char data
[UBUS_MAX_MSGLEN
];
124 struct ubus_object_data
{
128 struct blob_attr
*signature
;
131 struct ubus_request_data
{
138 struct ubus_request
{
139 struct list_head list
;
141 struct list_head pending
;
150 ubus_data_handler_t raw_data_cb
;
151 ubus_data_handler_t data_cb
;
152 ubus_complete_handler_t complete_cb
;
154 struct ubus_context
*ctx
;
159 struct ubus_context
*ubus_connect(const char *path
);
160 int ubus_reconnect(struct ubus_context
*ctx
, const char *path
);
161 void ubus_free(struct ubus_context
*ctx
);
163 const char *ubus_strerror(int error
);
165 static inline void ubus_add_uloop(struct ubus_context
*ctx
)
167 uloop_fd_add(&ctx
->sock
, ULOOP_BLOCKING
| ULOOP_READ
);
170 /* call this for read events on ctx->sock.fd when not using uloop */
171 static inline void ubus_handle_event(struct ubus_context
*ctx
)
173 ctx
->sock
.cb(&ctx
->sock
, ULOOP_READ
);
176 /* ----------- raw request handling ----------- */
178 /* wait for a request to complete and return its status */
179 int ubus_complete_request(struct ubus_context
*ctx
, struct ubus_request
*req
,
182 /* complete a request asynchronously */
183 void ubus_complete_request_async(struct ubus_context
*ctx
,
184 struct ubus_request
*req
);
186 /* abort an asynchronous request */
187 void ubus_abort_request(struct ubus_context
*ctx
, struct ubus_request
*req
);
189 /* ----------- objects ----------- */
191 int ubus_lookup(struct ubus_context
*ctx
, const char *path
,
192 ubus_lookup_handler_t cb
, void *priv
);
194 int ubus_lookup_id(struct ubus_context
*ctx
, const char *path
, uint32_t *id
);
196 /* make an object visible to remote connections */
197 int ubus_add_object(struct ubus_context
*ctx
, struct ubus_object
*obj
);
199 /* remove the object from the ubus connection */
200 int ubus_remove_object(struct ubus_context
*ctx
, struct ubus_object
*obj
);
202 /* add an object for watching other object state changes */
203 int ubus_register_watch_object(struct ubus_context
*ctx
, struct ubus_watch_object
*obj
);
205 int ubus_watch_object_add(struct ubus_context
*ctx
, struct ubus_watch_object
*obj
, uint32_t id
);
207 int ubus_watch_object_remove(struct ubus_context
*ctx
, struct ubus_watch_object
*obj
, uint32_t id
);
209 /* ----------- rpc ----------- */
211 /* invoke a method on a specific object */
212 int ubus_invoke(struct ubus_context
*ctx
, uint32_t obj
, const char *method
,
213 struct blob_attr
*msg
, ubus_data_handler_t cb
, void *priv
,
216 /* asynchronous version of ubus_invoke() */
217 int ubus_invoke_async(struct ubus_context
*ctx
, uint32_t obj
, const char *method
,
218 struct blob_attr
*msg
, struct ubus_request
*req
);
220 /* send a reply to an incoming object method call */
221 int ubus_send_reply(struct ubus_context
*ctx
, struct ubus_request_data
*req
,
222 struct blob_attr
*msg
);
224 static inline void ubus_defer_request(struct ubus_context
*ctx
,
225 struct ubus_request_data
*req
,
226 struct ubus_request_data
*new_req
)
228 memcpy(new_req
, req
, sizeof(*req
));
229 req
->deferred
= true;
232 void ubus_complete_deferred_request(struct ubus_context
*ctx
,
233 struct ubus_request_data
*req
, int ret
);
235 /* ----------- events ----------- */
237 int ubus_send_event(struct ubus_context
*ctx
, const char *id
,
238 struct blob_attr
*data
);
240 int ubus_register_event_handler(struct ubus_context
*ctx
,
241 struct ubus_event_handler
*ev
,
242 const char *pattern
);
244 static inline int ubus_unregister_event_handler(struct ubus_context
*ctx
,
245 struct ubus_event_handler
*ev
)
247 return ubus_remove_object(ctx
, &ev
->obj
);