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