libubus: pull the variable length data array out of struct ubus_msghdr to fix builds...
[project/ubus.git] / libubus.h
1 /*
2 * Copyright (C) 2011-2012 Felix Fietkau <nbd@openwrt.org>
3 *
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
7 *
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.
12 */
13
14 #ifndef __LIBUBUS_H
15 #define __LIBUBUS_H
16
17 #include <libubox/avl.h>
18 #include <libubox/list.h>
19 #include <libubox/blobmsg.h>
20 #include <libubox/uloop.h>
21 #include <stdint.h>
22 #include "ubusmsg.h"
23 #include "ubus_common.h"
24
25 #define UBUS_MAX_NOTIFY_PEERS 16
26
27 struct ubus_context;
28 struct ubus_msg_src;
29 struct ubus_object;
30 struct ubus_request;
31 struct ubus_request_data;
32 struct ubus_object_data;
33 struct ubus_event_handler;
34 struct ubus_subscriber;
35 struct ubus_notify_request;
36
37 static inline struct blob_attr *
38 ubus_msghdr_data(struct ubus_msghdr *hdr)
39 {
40 return (struct blob_attr *) (hdr + 1);
41 }
42
43 typedef void (*ubus_lookup_handler_t)(struct ubus_context *ctx,
44 struct ubus_object_data *obj,
45 void *priv);
46 typedef int (*ubus_handler_t)(struct ubus_context *ctx, struct ubus_object *obj,
47 struct ubus_request_data *req,
48 const char *method, struct blob_attr *msg);
49 typedef void (*ubus_state_handler_t)(struct ubus_context *ctx, struct ubus_object *obj);
50 typedef void (*ubus_remove_handler_t)(struct ubus_context *ctx,
51 struct ubus_subscriber *obj, uint32_t id);
52 typedef void (*ubus_event_handler_t)(struct ubus_context *ctx, struct ubus_event_handler *ev,
53 const char *type, struct blob_attr *msg);
54 typedef void (*ubus_data_handler_t)(struct ubus_request *req,
55 int type, struct blob_attr *msg);
56 typedef void (*ubus_complete_handler_t)(struct ubus_request *req, int ret);
57 typedef void (*ubus_notify_complete_handler_t)(struct ubus_notify_request *req,
58 int idx, int ret);
59
60 #define UBUS_OBJECT_TYPE(_name, _methods) \
61 { \
62 .name = _name, \
63 .id = 0, \
64 .n_methods = ARRAY_SIZE(_methods), \
65 .methods = _methods \
66 }
67
68 #define UBUS_METHOD(_name, _handler, _policy) \
69 { \
70 .name = _name, \
71 .handler = _handler, \
72 .policy = _policy, \
73 .n_policy = ARRAY_SIZE(_policy) \
74 }
75
76 #define UBUS_METHOD_NOARG(_name, _handler) \
77 { \
78 .name = _name, \
79 .handler = _handler, \
80 }
81
82 struct ubus_method {
83 const char *name;
84 ubus_handler_t handler;
85
86 const struct blobmsg_policy *policy;
87 int n_policy;
88 };
89
90 struct ubus_object_type {
91 const char *name;
92 uint32_t id;
93
94 const struct ubus_method *methods;
95 int n_methods;
96 };
97
98 struct ubus_object {
99 struct avl_node avl;
100
101 const char *name;
102 uint32_t id;
103
104 const char *path;
105 struct ubus_object_type *type;
106
107 ubus_state_handler_t subscribe_cb;
108 bool has_subscribers;
109
110 const struct ubus_method *methods;
111 int n_methods;
112 };
113
114 struct ubus_subscriber {
115 struct ubus_object obj;
116
117 ubus_handler_t cb;
118 ubus_remove_handler_t remove_cb;
119 };
120
121 struct ubus_event_handler {
122 struct ubus_object obj;
123
124 ubus_event_handler_t cb;
125 };
126
127 struct ubus_context {
128 struct list_head requests;
129 struct avl_tree objects;
130 struct list_head pending;
131
132 struct uloop_fd sock;
133
134 uint32_t local_id;
135 uint16_t request_seq;
136 int stack_depth;
137
138 void (*connection_lost)(struct ubus_context *ctx);
139
140 struct {
141 struct ubus_msghdr hdr;
142 char data[UBUS_MAX_MSGLEN];
143 } msgbuf;
144 };
145
146 struct ubus_object_data {
147 uint32_t id;
148 uint32_t type_id;
149 const char *path;
150 struct blob_attr *signature;
151 };
152
153 struct ubus_request_data {
154 uint32_t object;
155 uint32_t peer;
156 uint16_t seq;
157 bool deferred;
158 };
159
160 struct ubus_request {
161 struct list_head list;
162
163 struct list_head pending;
164 int status_code;
165 bool status_msg;
166 bool blocked;
167 bool cancelled;
168 bool notify;
169
170 uint32_t peer;
171 uint16_t seq;
172
173 ubus_data_handler_t raw_data_cb;
174 ubus_data_handler_t data_cb;
175 ubus_complete_handler_t complete_cb;
176
177 struct ubus_context *ctx;
178 void *priv;
179 };
180
181 struct ubus_notify_request {
182 struct ubus_request req;
183
184 ubus_notify_complete_handler_t status_cb;
185 ubus_notify_complete_handler_t complete_cb;
186
187 uint32_t pending;
188 uint32_t id[UBUS_MAX_NOTIFY_PEERS + 1];
189 };
190
191 struct ubus_context *ubus_connect(const char *path);
192 int ubus_reconnect(struct ubus_context *ctx, const char *path);
193 void ubus_free(struct ubus_context *ctx);
194
195 const char *ubus_strerror(int error);
196
197 static inline void ubus_add_uloop(struct ubus_context *ctx)
198 {
199 uloop_fd_add(&ctx->sock, ULOOP_BLOCKING | ULOOP_READ);
200 }
201
202 /* call this for read events on ctx->sock.fd when not using uloop */
203 static inline void ubus_handle_event(struct ubus_context *ctx)
204 {
205 ctx->sock.cb(&ctx->sock, ULOOP_READ);
206 }
207
208 /* ----------- raw request handling ----------- */
209
210 /* wait for a request to complete and return its status */
211 int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req,
212 int timeout);
213
214 /* complete a request asynchronously */
215 void ubus_complete_request_async(struct ubus_context *ctx,
216 struct ubus_request *req);
217
218 /* abort an asynchronous request */
219 void ubus_abort_request(struct ubus_context *ctx, struct ubus_request *req);
220
221 /* ----------- objects ----------- */
222
223 int ubus_lookup(struct ubus_context *ctx, const char *path,
224 ubus_lookup_handler_t cb, void *priv);
225
226 int ubus_lookup_id(struct ubus_context *ctx, const char *path, uint32_t *id);
227
228 /* make an object visible to remote connections */
229 int ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj);
230
231 /* remove the object from the ubus connection */
232 int ubus_remove_object(struct ubus_context *ctx, struct ubus_object *obj);
233
234 /* add a subscriber notifications from another object */
235 int ubus_register_subscriber(struct ubus_context *ctx, struct ubus_subscriber *obj);
236
237 static inline int
238 ubus_unregister_subscriber(struct ubus_context *ctx, struct ubus_subscriber *obj)
239 {
240 return ubus_remove_object(ctx, &obj->obj);
241 }
242
243 int ubus_subscribe(struct ubus_context *ctx, struct ubus_subscriber *obj, uint32_t id);
244 int ubus_unsubscribe(struct ubus_context *ctx, struct ubus_subscriber *obj, uint32_t id);
245
246 /* ----------- rpc ----------- */
247
248 /* invoke a method on a specific object */
249 int ubus_invoke(struct ubus_context *ctx, uint32_t obj, const char *method,
250 struct blob_attr *msg, ubus_data_handler_t cb, void *priv,
251 int timeout);
252
253 /* asynchronous version of ubus_invoke() */
254 int ubus_invoke_async(struct ubus_context *ctx, uint32_t obj, const char *method,
255 struct blob_attr *msg, struct ubus_request *req);
256
257 /* send a reply to an incoming object method call */
258 int ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req,
259 struct blob_attr *msg);
260
261 static inline void ubus_defer_request(struct ubus_context *ctx,
262 struct ubus_request_data *req,
263 struct ubus_request_data *new_req)
264 {
265 memcpy(new_req, req, sizeof(*req));
266 req->deferred = true;
267 }
268
269 void ubus_complete_deferred_request(struct ubus_context *ctx,
270 struct ubus_request_data *req, int ret);
271
272 /*
273 * send a notification to all subscribers of an object
274 * if timeout < 0, no reply is expected from subscribers
275 */
276 int ubus_notify(struct ubus_context *ctx, struct ubus_object *obj,
277 const char *type, struct blob_attr *msg, int timeout);
278
279 int ubus_notify_async(struct ubus_context *ctx, struct ubus_object *obj,
280 const char *type, struct blob_attr *msg,
281 struct ubus_notify_request *req);
282
283
284 /* ----------- events ----------- */
285
286 int ubus_send_event(struct ubus_context *ctx, const char *id,
287 struct blob_attr *data);
288
289 int ubus_register_event_handler(struct ubus_context *ctx,
290 struct ubus_event_handler *ev,
291 const char *pattern);
292
293 static inline int ubus_unregister_event_handler(struct ubus_context *ctx,
294 struct ubus_event_handler *ev)
295 {
296 return ubus_remove_object(ctx, &ev->obj);
297 }
298
299 #endif