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