• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ubus/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 struct ubus_msghdr_buf {
 38         struct ubus_msghdr hdr;
 39         struct blob_attr *data;
 40 };
 41 
 42 typedef void (*ubus_lookup_handler_t)(struct ubus_context *ctx,
 43                                       struct ubus_object_data *obj,
 44                                       void *priv);
 45 typedef int (*ubus_handler_t)(struct ubus_context *ctx, struct ubus_object *obj,
 46                               struct ubus_request_data *req,
 47                               const char *method, struct blob_attr *msg);
 48 typedef void (*ubus_state_handler_t)(struct ubus_context *ctx, struct ubus_object *obj);
 49 typedef void (*ubus_remove_handler_t)(struct ubus_context *ctx,
 50                                       struct ubus_subscriber *obj, uint32_t id);
 51 typedef void (*ubus_event_handler_t)(struct ubus_context *ctx, struct ubus_event_handler *ev,
 52                                      const char *type, struct blob_attr *msg);
 53 typedef void (*ubus_data_handler_t)(struct ubus_request *req,
 54                                     int type, struct blob_attr *msg);
 55 typedef void (*ubus_fd_handler_t)(struct ubus_request *req, int fd);
 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 typedef void (*ubus_notify_data_handler_t)(struct ubus_notify_request *req,
 60                                            int type, struct blob_attr *msg);
 61 typedef void (*ubus_connect_handler_t)(struct ubus_context *ctx);
 62 
 63 #define UBUS_OBJECT_TYPE(_name, _methods)               \
 64         {                                               \
 65                 .name = _name,                          \
 66                 .id = 0,                                \
 67                 .n_methods = ARRAY_SIZE(_methods),      \
 68                 .methods = _methods                     \
 69         }
 70 
 71 #define __UBUS_METHOD_NOARG(_name, _handler, _tags)     \
 72         .name = _name,                                  \
 73         .handler = _handler,                            \
 74         .tags = _tags
 75 
 76 #define __UBUS_METHOD(_name, _handler, _policy, _tags)  \
 77         __UBUS_METHOD_NOARG(_name, _handler, _tags),    \
 78         .policy = _policy,                              \
 79         .n_policy = ARRAY_SIZE(_policy)
 80 
 81 #define UBUS_METHOD(_name, _handler, _policy)           \
 82         { __UBUS_METHOD(_name, _handler, _policy, 0) }
 83 
 84 #define UBUS_METHOD_TAG(_name, _handler, _policy, _tags)\
 85         { __UBUS_METHOD(_name, _handler, _policy, _tags) }
 86 
 87 #define UBUS_METHOD_MASK(_name, _handler, _policy, _mask) \
 88         {                                               \
 89                 __UBUS_METHOD(_name, _handler, _policy, 0),\
 90                 .mask = _mask                           \
 91         }
 92 
 93 #define UBUS_METHOD_NOARG(_name, _handler)              \
 94         { __UBUS_METHOD_NOARG(_name, _handler, 0) }
 95 
 96 #define UBUS_METHOD_TAG_NOARG(_name, _handler, _tags)   \
 97         { __UBUS_METHOD_NOARG(_name, _handler, _tags) }
 98 
 99 #define UBUS_TAG_STATUS         BIT(0)
100 #define UBUS_TAG_ADMIN          BIT(1)
101 #define UBUS_TAG_PRIVATE        BIT(2)
102 
103 struct ubus_method {
104         const char *name;
105         ubus_handler_t handler;
106 
107         unsigned long mask;
108         unsigned long tags;
109         const struct blobmsg_policy *policy;
110         int n_policy;
111 };
112 
113 struct ubus_object_type {
114         const char *name;
115         uint32_t id;
116 
117         const struct ubus_method *methods;
118         int n_methods;
119 };
120 
121 struct ubus_object {
122         struct avl_node avl;
123 
124         const char *name;
125         uint32_t id;
126 
127         const char *path;
128         struct ubus_object_type *type;
129 
130         ubus_state_handler_t subscribe_cb;
131         bool has_subscribers;
132 
133         const struct ubus_method *methods;
134         int n_methods;
135 };
136 
137 struct ubus_subscriber {
138         struct ubus_object obj;
139 
140         ubus_handler_t cb;
141         ubus_remove_handler_t remove_cb;
142 };
143 
144 struct ubus_event_handler {
145         struct ubus_object obj;
146 
147         ubus_event_handler_t cb;
148 };
149 
150 struct ubus_context {
151         struct list_head requests;
152         struct avl_tree objects;
153         struct list_head pending;
154 
155         struct uloop_fd sock;
156         struct uloop_timeout pending_timer;
157 
158         uint32_t local_id;
159         uint16_t request_seq;
160         bool cancel_poll;
161         int stack_depth;
162 
163         void (*connection_lost)(struct ubus_context *ctx);
164         void (*monitor_cb)(struct ubus_context *ctx, uint32_t seq, struct blob_attr *data);
165 
166         struct ubus_msghdr_buf msgbuf;
167         uint32_t msgbuf_data_len;
168         int msgbuf_reduction_counter;
169 };
170 
171 struct ubus_object_data {
172         uint32_t id;
173         uint32_t type_id;
174         const char *path;
175         struct blob_attr *signature;
176 };
177 
178 struct ubus_acl_key {
179         const char *user;
180         const char *group;
181         const char *object;
182 };
183 
184 struct ubus_request_data {
185         uint32_t object;
186         uint32_t peer;
187         uint16_t seq;
188 
189         struct ubus_acl_key acl;
190 
191         /* internal use */
192         bool deferred;
193         int fd;
194         int req_fd; /* fd received from the initial request */
195 };
196 
197 struct ubus_request {
198         struct list_head list;
199 
200         struct list_head pending;
201         int status_code;
202         bool status_msg;
203         bool blocked;
204         bool cancelled;
205         bool notify;
206 
207         uint32_t peer;
208         uint16_t seq;
209 
210         ubus_data_handler_t raw_data_cb;
211         ubus_data_handler_t data_cb;
212         ubus_fd_handler_t fd_cb;
213         ubus_complete_handler_t complete_cb;
214 
215         int fd;
216 
217         struct ubus_context *ctx;
218         void *priv;
219 };
220 
221 struct ubus_notify_request {
222         struct ubus_request req;
223 
224         ubus_notify_complete_handler_t status_cb;
225         ubus_notify_complete_handler_t complete_cb;
226         ubus_notify_data_handler_t data_cb;
227 
228         uint32_t pending;
229         uint32_t id[UBUS_MAX_NOTIFY_PEERS + 1];
230 };
231 
232 struct ubus_auto_conn {
233         struct ubus_context ctx;
234         struct uloop_timeout timer;
235         const char *path;
236         ubus_connect_handler_t cb;
237 };
238 
239 struct ubus_context *ubus_connect(const char *path);
240 int ubus_connect_ctx(struct ubus_context *ctx, const char *path);
241 void ubus_auto_connect(struct ubus_auto_conn *conn);
242 int ubus_reconnect(struct ubus_context *ctx, const char *path);
243 
244 /* call this only for struct ubus_context pointers returned by ubus_connect() */
245 void ubus_free(struct ubus_context *ctx);
246 
247 /* call this only for struct ubus_context pointers initialised by ubus_connect_ctx() */
248 void ubus_shutdown(struct ubus_context *ctx);
249 
250 static inline void ubus_auto_shutdown(struct ubus_auto_conn *conn)
251 {
252         uloop_timeout_cancel(&conn->timer);
253         ubus_shutdown(&conn->ctx);
254 }
255 
256 const char *ubus_strerror(int error);
257 
258 static inline void ubus_add_uloop(struct ubus_context *ctx)
259 {
260         uloop_fd_add(&ctx->sock, ULOOP_BLOCKING | ULOOP_READ);
261 }
262 
263 /* call this for read events on ctx->sock.fd when not using uloop */
264 static inline void ubus_handle_event(struct ubus_context *ctx)
265 {
266         ctx->sock.cb(&ctx->sock, ULOOP_READ);
267 }
268 
269 /* ----------- raw request handling ----------- */
270 
271 /* wait for a request to complete and return its status */
272 int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req,
273                           int timeout);
274 
275 /* complete a request asynchronously */
276 void ubus_complete_request_async(struct ubus_context *ctx,
277                                  struct ubus_request *req);
278 
279 /* abort an asynchronous request */
280 void ubus_abort_request(struct ubus_context *ctx, struct ubus_request *req);
281 
282 /* ----------- objects ----------- */
283 
284 int ubus_lookup(struct ubus_context *ctx, const char *path,
285                 ubus_lookup_handler_t cb, void *priv);
286 
287 int ubus_lookup_id(struct ubus_context *ctx, const char *path, uint32_t *id);
288 
289 /* make an object visible to remote connections */
290 int ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj);
291 
292 /* remove the object from the ubus connection */
293 int ubus_remove_object(struct ubus_context *ctx, struct ubus_object *obj);
294 
295 /* add a subscriber notifications from another object */
296 int ubus_register_subscriber(struct ubus_context *ctx, struct ubus_subscriber *obj);
297 
298 static inline int
299 ubus_unregister_subscriber(struct ubus_context *ctx, struct ubus_subscriber *obj)
300 {
301         return ubus_remove_object(ctx, &obj->obj);
302 }
303 
304 int ubus_subscribe(struct ubus_context *ctx, struct ubus_subscriber *obj, uint32_t id);
305 int ubus_unsubscribe(struct ubus_context *ctx, struct ubus_subscriber *obj, uint32_t id);
306 
307 int __ubus_monitor(struct ubus_context *ctx, const char *type);
308 
309 static inline int ubus_monitor_start(struct ubus_context *ctx)
310 {
311         return __ubus_monitor(ctx, "add");
312 }
313 
314 static inline int ubus_monitor_stop(struct ubus_context *ctx)
315 {
316         return __ubus_monitor(ctx, "remove");
317 }
318 
319 
320 /* ----------- acl ----------- */
321 
322 struct acl_object {
323         struct ubus_acl_key key;
324         struct avl_node avl;
325         struct blob_attr *acl;
326 };
327 
328 extern struct avl_tree acl_objects;
329 int ubus_register_acl(struct ubus_context *ctx);
330 
331 #define acl_for_each(o, m) \
332         if ((m)->object && (m)->user && (m)->group) \
333                 avl_for_element_range(avl_find_ge_element(&acl_objects, m, o, avl), avl_find_le_element(&acl_objects, m, o, avl), o, avl)
334 
335 /* ----------- rpc ----------- */
336 
337 /* invoke a method on a specific object */
338 int ubus_invoke_fd(struct ubus_context *ctx, uint32_t obj, const char *method,
339                 struct blob_attr *msg, ubus_data_handler_t cb, void *priv,
340                 int timeout, int fd);
341 static inline int
342 ubus_invoke(struct ubus_context *ctx, uint32_t obj, const char *method,
343             struct blob_attr *msg, ubus_data_handler_t cb, void *priv,
344             int timeout)
345 {
346         return ubus_invoke_fd(ctx, obj, method, msg, cb, priv, timeout, -1);
347 }
348 
349 /* asynchronous version of ubus_invoke() */
350 int ubus_invoke_async_fd(struct ubus_context *ctx, uint32_t obj, const char *method,
351                       struct blob_attr *msg, struct ubus_request *req, int fd);
352 static inline int
353 ubus_invoke_async(struct ubus_context *ctx, uint32_t obj, const char *method,
354                   struct blob_attr *msg, struct ubus_request *req)
355 {
356         return ubus_invoke_async_fd(ctx, obj, method, msg, req, -1);
357 }
358 
359 /* send a reply to an incoming object method call */
360 int ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req,
361                     struct blob_attr *msg);
362 
363 static inline void ubus_defer_request(struct ubus_context *ctx,
364                                       struct ubus_request_data *req,
365                                       struct ubus_request_data *new_req)
366 {
367     (void) ctx;
368     memcpy(new_req, req, sizeof(*req));
369     req->deferred = true;
370 }
371 
372 static inline void ubus_request_set_fd(struct ubus_context *ctx,
373                                        struct ubus_request_data *req, int fd)
374 {
375     (void) ctx;
376     req->fd = fd;
377 }
378 
379 static inline int ubus_request_get_caller_fd(struct ubus_request_data *req)
380 {
381     int fd = req->req_fd;
382     req->req_fd = -1;
383     
384     return fd;
385 }
386 
387 void ubus_complete_deferred_request(struct ubus_context *ctx,
388                                     struct ubus_request_data *req, int ret);
389 
390 /*
391  * send a notification to all subscribers of an object
392  * if timeout < 0, no reply is expected from subscribers
393  */
394 int ubus_notify(struct ubus_context *ctx, struct ubus_object *obj,
395                 const char *type, struct blob_attr *msg, int timeout);
396 
397 int ubus_notify_async(struct ubus_context *ctx, struct ubus_object *obj,
398                       const char *type, struct blob_attr *msg,
399                       struct ubus_notify_request *req);
400 
401 
402 /* ----------- events ----------- */
403 
404 int ubus_send_event(struct ubus_context *ctx, const char *id,
405                     struct blob_attr *data);
406 
407 int ubus_register_event_handler(struct ubus_context *ctx,
408                                 struct ubus_event_handler *ev,
409                                 const char *pattern);
410 
411 static inline int ubus_unregister_event_handler(struct ubus_context *ctx,
412                                                 struct ubus_event_handler *ev)
413 {
414     return ubus_remove_object(ctx, &ev->obj);
415 }
416 
417 #endif
418 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt