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