1 #include <libubox/avl.h>
2 #include <libubox/list.h>
3 #include <libubox/blobmsg.h>
4 #include <libubox/uloop.h>
7 #include "ubus_common.h"
13 struct ubus_request_data
;
14 struct ubus_object_data
;
16 typedef void (*ubus_lookup_handler_t
)(struct ubus_context
*ctx
,
17 struct ubus_object_data
*obj
,
19 typedef int (*ubus_handler_t
)(struct ubus_context
*ctx
, struct ubus_object
*obj
,
20 struct ubus_request_data
*req
,
21 const char *method
, struct blob_attr
*msg
);
22 typedef void (*ubus_data_handler_t
)(struct ubus_request
*req
,
23 int type
, struct blob_attr
*msg
);
24 typedef void (*ubus_complete_handler_t
)(struct ubus_request
*req
, int ret
);
27 #define UBUS_SIGNATURE(_type, _name) { .type = _type, .name = _name }
29 #define UBUS_METHOD_START(_name) UBUS_SIGNATURE(UBUS_SIGNATURE_METHOD, _name)
30 #define UBUS_METHOD_END() UBUS_SIGNATURE(UBUS_SIGNATURE_END, NULL)
32 #define UBUS_FIELD(_type, _name) UBUS_SIGNATURE(BLOBMSG_TYPE_ ## _type, _name)
34 #define UBUS_ARRAY(_name) UBUS_FIELD(ARRAY, _name)
35 #define UBUS_ARRAY_END() UBUS_SIGNATURE(UBUS_SIGNATURE_END, NULL)
37 #define UBUS_TABLE_START(_name) UBUS_FIELD(TABLE, _name)
38 #define UBUS_TABLE_END() UBUS_SIGNATURE(UBUS_SIGNATURE_END, NULL)
40 #define UBUS_OBJECT_TYPE(_name, _signature) \
44 .n_signature = ARRAY_SIZE(_signature), \
45 .signature = _signature \
48 struct ubus_signature
{
49 enum blobmsg_type type
;
53 struct ubus_object_type
{
57 const struct ubus_signature
*signature
;
62 ubus_handler_t handler
;
72 struct ubus_object_type
*type
;
74 const struct ubus_method
*methods
;
79 struct list_head requests
;
80 struct avl_tree objects
;
87 void (*connection_lost
)(struct ubus_context
*ctx
);
90 struct ubus_msghdr hdr
;
91 char data
[UBUS_MAX_MSGLEN
- sizeof(struct ubus_msghdr
)];
95 struct ubus_object_data
{
99 struct blob_attr
*signature
;
102 struct ubus_request_data
{
108 struct ubus_request
{
109 struct list_head list
;
111 struct list_head pending
;
120 ubus_data_handler_t raw_data_cb
;
121 ubus_data_handler_t data_cb
;
122 ubus_complete_handler_t complete_cb
;
124 struct ubus_context
*ctx
;
129 struct ubus_context
*ubus_connect(const char *path
);
130 void ubus_free(struct ubus_context
*ctx
);
132 const char *ubus_strerror(int error
);
134 static inline void ubus_add_uloop(struct ubus_context
*ctx
)
136 uloop_fd_add(&ctx
->sock
, ULOOP_EDGE_TRIGGER
| ULOOP_BLOCKING
| ULOOP_READ
);
139 /* ----------- raw request handling ----------- */
141 /* wait for a request to complete and return its status */
142 int ubus_complete_request(struct ubus_context
*ctx
, struct ubus_request
*req
);
144 /* complete a request asynchronously */
145 void ubus_complete_request_async(struct ubus_context
*ctx
,
146 struct ubus_request
*req
);
148 /* abort an asynchronous request */
149 void ubus_abort_request(struct ubus_context
*ctx
, struct ubus_request
*req
);
151 /* ----------- objects ----------- */
153 int ubus_lookup(struct ubus_context
*ctx
, const char *path
,
154 ubus_lookup_handler_t cb
, void *priv
);
156 int ubus_lookup_id(struct ubus_context
*ctx
, const char *path
, uint32_t *id
);
158 /* ----------- rpc ----------- */
160 /* invoke a method on a specific object */
161 int ubus_invoke(struct ubus_context
*ctx
, uint32_t obj
, const char *method
,
162 struct blob_attr
*msg
, ubus_data_handler_t cb
, void *priv
);
164 /* asynchronous version of ubus_invoke() */
165 void ubus_invoke_async(struct ubus_context
*ctx
, uint32_t obj
, const char *method
,
166 struct blob_attr
*msg
, struct ubus_request
*req
);
168 /* make an object visible to remote connections */
169 int ubus_publish(struct ubus_context
*ctx
, struct ubus_object
*obj
);
171 /* send a reply to an incoming object method call */
172 int ubus_send_reply(struct ubus_context
*ctx
, struct ubus_request_data
*req
,
173 struct blob_attr
*msg
);