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