add support for timeouts on synchronous requests
[project/ubus.git] / libubus.h
1 #include <libubox/avl.h>
2 #include <libubox/list.h>
3 #include <libubox/blobmsg.h>
4 #include <libubox/uloop.h>
5 #include <stdint.h>
6 #include "ubusmsg.h"
7 #include "ubus_common.h"
8
9 struct ubus_context;
10 struct ubus_msg_src;
11 struct ubus_object;
12 struct ubus_request;
13 struct ubus_request_data;
14 struct ubus_object_data;
15 struct ubus_event_handler;
16
17 typedef void (*ubus_lookup_handler_t)(struct ubus_context *ctx,
18 struct ubus_object_data *obj,
19 void *priv);
20 typedef int (*ubus_handler_t)(struct ubus_context *ctx, struct ubus_object *obj,
21 struct ubus_request_data *req,
22 const char *method, struct blob_attr *msg);
23 typedef void (*ubus_event_handler_t)(struct ubus_context *ctx, struct ubus_event_handler *ev,
24 const char *type, struct blob_attr *msg);
25 typedef void (*ubus_data_handler_t)(struct ubus_request *req,
26 int type, struct blob_attr *msg);
27 typedef void (*ubus_complete_handler_t)(struct ubus_request *req, int ret);
28
29
30 #define UBUS_SIGNATURE(_type, _name) { .type = _type, .name = _name }
31
32 #define UBUS_METHOD_START(_name) UBUS_SIGNATURE(UBUS_SIGNATURE_METHOD, _name)
33 #define UBUS_METHOD_END() UBUS_SIGNATURE(UBUS_SIGNATURE_END, NULL)
34
35 #define UBUS_FIELD(_type, _name) UBUS_SIGNATURE(BLOBMSG_TYPE_ ## _type, _name)
36
37 #define UBUS_ARRAY(_name) UBUS_FIELD(ARRAY, _name)
38 #define UBUS_ARRAY_END() UBUS_SIGNATURE(UBUS_SIGNATURE_END, NULL)
39
40 #define UBUS_TABLE_START(_name) UBUS_FIELD(TABLE, _name)
41 #define UBUS_TABLE_END() UBUS_SIGNATURE(UBUS_SIGNATURE_END, NULL)
42
43 #define UBUS_OBJECT_TYPE(_name, _signature) \
44 { \
45 .name = _name, \
46 .id = 0, \
47 .n_signature = ARRAY_SIZE(_signature), \
48 .signature = _signature \
49 }
50
51 struct ubus_signature {
52 enum blobmsg_type type;
53 const char *name;
54 };
55
56 struct ubus_object_type {
57 const char *name;
58 uint32_t id;
59 int n_signature;
60 const struct ubus_signature *signature;
61 };
62
63 struct ubus_method {
64 const char *name;
65 ubus_handler_t handler;
66 };
67
68 struct ubus_object {
69 struct avl_node avl;
70
71 const char *name;
72 uint32_t id;
73
74 const char *path;
75 struct ubus_object_type *type;
76
77 const struct ubus_method *methods;
78 int n_methods;
79 };
80
81 struct ubus_event_handler {
82 struct ubus_object obj;
83
84 ubus_event_handler_t cb;
85 };
86
87 struct ubus_context {
88 struct list_head requests;
89 struct avl_tree objects;
90
91 struct uloop_fd sock;
92
93 uint32_t local_id;
94 uint32_t request_seq;
95
96 void (*connection_lost)(struct ubus_context *ctx);
97
98 struct {
99 struct ubus_msghdr hdr;
100 char data[UBUS_MAX_MSGLEN];
101 } msgbuf;
102 };
103
104 struct ubus_object_data {
105 uint32_t id;
106 uint32_t type_id;
107 const char *path;
108 struct blob_attr *signature;
109 };
110
111 struct ubus_request_data {
112 uint32_t object;
113 uint32_t peer;
114 uint32_t seq;
115 };
116
117 struct ubus_request {
118 struct list_head list;
119
120 struct list_head pending;
121 bool status_msg;
122 int status_code;
123 bool blocked;
124 bool cancelled;
125
126 uint32_t peer;
127 uint32_t seq;
128
129 ubus_data_handler_t raw_data_cb;
130 ubus_data_handler_t data_cb;
131 ubus_complete_handler_t complete_cb;
132
133 struct ubus_context *ctx;
134 void *priv;
135 };
136
137
138 struct ubus_context *ubus_connect(const char *path);
139 void ubus_free(struct ubus_context *ctx);
140
141 const char *ubus_strerror(int error);
142
143 static inline void ubus_add_uloop(struct ubus_context *ctx)
144 {
145 uloop_fd_add(&ctx->sock, ULOOP_EDGE_TRIGGER | ULOOP_BLOCKING | ULOOP_READ);
146 }
147
148 /* call this for read events on ctx->sock.fd when not using uloop */
149 static inline void ubus_handle_event(struct ubus_context *ctx)
150 {
151 ctx->sock.cb(&ctx->sock, ULOOP_READ);
152 }
153
154 /* ----------- raw request handling ----------- */
155
156 /* wait for a request to complete and return its status */
157 int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req,
158 int timeout);
159
160 /* complete a request asynchronously */
161 void ubus_complete_request_async(struct ubus_context *ctx,
162 struct ubus_request *req);
163
164 /* abort an asynchronous request */
165 void ubus_abort_request(struct ubus_context *ctx, struct ubus_request *req);
166
167 /* ----------- objects ----------- */
168
169 int ubus_lookup(struct ubus_context *ctx, const char *path,
170 ubus_lookup_handler_t cb, void *priv);
171
172 int ubus_lookup_id(struct ubus_context *ctx, const char *path, uint32_t *id);
173
174 /* make an object visible to remote connections */
175 int ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj);
176
177 /* remove the object from the ubus connection */
178 int ubus_remove_object(struct ubus_context *ctx, struct ubus_object *obj);
179
180 /* ----------- rpc ----------- */
181
182 /* invoke a method on a specific object */
183 int ubus_invoke(struct ubus_context *ctx, uint32_t obj, const char *method,
184 struct blob_attr *msg, ubus_data_handler_t cb, void *priv,
185 int timeout);
186
187 /* asynchronous version of ubus_invoke() */
188 int ubus_invoke_async(struct ubus_context *ctx, uint32_t obj, const char *method,
189 struct blob_attr *msg, struct ubus_request *req);
190
191 /* send a reply to an incoming object method call */
192 int ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req,
193 struct blob_attr *msg);
194
195 /* ----------- events ----------- */
196
197 int ubus_send_event(struct ubus_context *ctx, const char *id,
198 struct blob_attr *data);
199
200 int ubus_register_event_handler(struct ubus_context *ctx,
201 struct ubus_event_handler *ev,
202 const char *pattern);
203
204 static inline int ubus_unregister_event_handler(struct ubus_context *ctx,
205 struct ubus_event_handler *ev)
206 {
207 return ubus_remove_object(ctx, &ev->obj);
208 }