libubus.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. #ifndef __LIBUBUS_H
  14. #define __LIBUBUS_H
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #include <libubox/avl.h>
  19. #include <libubox/list.h>
  20. #include <libubox/blobmsg.h>
  21. #include <libubox/uloop.h>
  22. #include <stdint.h>
  23. #include "ubusmsg.h"
  24. #include "ubus_common.h"
  25. #define UBUS_MAX_NOTIFY_PEERS 16
  26. struct ubus_context;
  27. struct ubus_msg_src;
  28. struct ubus_object;
  29. struct ubus_request;
  30. struct ubus_request_data;
  31. struct ubus_object_data;
  32. struct ubus_event_handler;
  33. struct ubus_subscriber;
  34. struct ubus_notify_request;
  35. struct ubus_msghdr_buf {
  36. struct ubus_msghdr hdr;
  37. struct blob_attr *data;
  38. };
  39. typedef void (*ubus_lookup_handler_t)(struct ubus_context *ctx,
  40. struct ubus_object_data *obj,
  41. void *priv);
  42. typedef int (*ubus_handler_t)(struct ubus_context *ctx, struct ubus_object *obj,
  43. struct ubus_request_data *req,
  44. const char *method, struct blob_attr *msg);
  45. typedef void (*ubus_state_handler_t)(struct ubus_context *ctx, struct ubus_object *obj);
  46. typedef void (*ubus_remove_handler_t)(struct ubus_context *ctx,
  47. struct ubus_subscriber *obj, uint32_t id);
  48. typedef void (*ubus_event_handler_t)(struct ubus_context *ctx, struct ubus_event_handler *ev,
  49. const char *type, struct blob_attr *msg);
  50. typedef void (*ubus_data_handler_t)(struct ubus_request *req,
  51. int type, struct blob_attr *msg);
  52. typedef void (*ubus_fd_handler_t)(struct ubus_request *req, int fd);
  53. typedef void (*ubus_complete_handler_t)(struct ubus_request *req, int ret);
  54. typedef void (*ubus_notify_complete_handler_t)(struct ubus_notify_request *req,
  55. int idx, int ret);
  56. typedef void (*ubus_notify_data_handler_t)(struct ubus_notify_request *req,
  57. int type, struct blob_attr *msg);
  58. typedef void (*ubus_connect_handler_t)(struct ubus_context *ctx);
  59. typedef bool (*ubus_new_object_handler_t)(struct ubus_context *ctx, struct ubus_subscriber *sub, const char *path);
  60. #define UBUS_OBJECT_TYPE(_name, _methods) \
  61. { \
  62. .name = _name, \
  63. .id = 0, \
  64. .n_methods = ARRAY_SIZE(_methods), \
  65. .methods = _methods \
  66. }
  67. #define __UBUS_METHOD_NOARG(_name, _handler, _tags) \
  68. .name = _name, \
  69. .handler = _handler, \
  70. .tags = _tags
  71. #define __UBUS_METHOD(_name, _handler, _policy, _tags) \
  72. __UBUS_METHOD_NOARG(_name, _handler, _tags), \
  73. .policy = _policy, \
  74. .n_policy = ARRAY_SIZE(_policy)
  75. #define UBUS_METHOD(_name, _handler, _policy) \
  76. { __UBUS_METHOD(_name, _handler, _policy, 0) }
  77. #define UBUS_METHOD_TAG(_name, _handler, _policy, _tags)\
  78. { __UBUS_METHOD(_name, _handler, _policy, _tags) }
  79. #define UBUS_METHOD_MASK(_name, _handler, _policy, _mask) \
  80. { \
  81. __UBUS_METHOD(_name, _handler, _policy, 0),\
  82. .mask = _mask \
  83. }
  84. #define UBUS_METHOD_NOARG(_name, _handler) \
  85. { __UBUS_METHOD_NOARG(_name, _handler, 0) }
  86. #define UBUS_METHOD_TAG_NOARG(_name, _handler, _tags) \
  87. { __UBUS_METHOD_NOARG(_name, _handler, _tags) }
  88. #define UBUS_TAG_STATUS BIT(0)
  89. #define UBUS_TAG_ADMIN BIT(1)
  90. #define UBUS_TAG_PRIVATE BIT(2)
  91. struct ubus_method {
  92. const char *name;
  93. ubus_handler_t handler;
  94. unsigned long mask;
  95. unsigned long tags;
  96. const struct blobmsg_policy *policy;
  97. int n_policy;
  98. };
  99. struct ubus_object_type {
  100. const char *name;
  101. uint32_t id;
  102. const struct ubus_method *methods;
  103. int n_methods;
  104. };
  105. struct ubus_object {
  106. struct avl_node avl;
  107. const char *name;
  108. uint32_t id;
  109. const char *path;
  110. struct ubus_object_type *type;
  111. ubus_state_handler_t subscribe_cb;
  112. bool has_subscribers;
  113. const struct ubus_method *methods;
  114. int n_methods;
  115. };
  116. struct ubus_subscriber {
  117. struct list_head list;
  118. struct ubus_object obj;
  119. ubus_handler_t cb;
  120. ubus_remove_handler_t remove_cb;
  121. ubus_new_object_handler_t new_obj_cb;
  122. };
  123. struct ubus_event_handler {
  124. struct ubus_object obj;
  125. ubus_event_handler_t cb;
  126. };
  127. struct ubus_context {
  128. struct list_head requests;
  129. struct avl_tree objects;
  130. struct list_head pending;
  131. struct uloop_fd sock;
  132. struct uloop_timeout pending_timer;
  133. uint32_t local_id;
  134. uint16_t request_seq;
  135. bool cancel_poll;
  136. int stack_depth;
  137. void (*connection_lost)(struct ubus_context *ctx);
  138. void (*monitor_cb)(struct ubus_context *ctx, uint32_t seq, struct blob_attr *data);
  139. struct ubus_msghdr_buf msgbuf;
  140. uint32_t msgbuf_data_len;
  141. int msgbuf_reduction_counter;
  142. struct list_head auto_subscribers;
  143. struct ubus_event_handler auto_subscribe_event_handler;
  144. };
  145. struct ubus_object_data {
  146. uint32_t id;
  147. uint32_t type_id;
  148. const char *path;
  149. struct blob_attr *signature;
  150. };
  151. struct ubus_acl_key {
  152. const char *user;
  153. const char *group;
  154. const char *object;
  155. };
  156. struct ubus_request_data {
  157. uint32_t object;
  158. uint32_t peer;
  159. uint16_t seq;
  160. struct ubus_acl_key acl;
  161. /* internal use */
  162. bool deferred;
  163. int fd;
  164. int req_fd; /* fd received from the initial request */
  165. };
  166. struct ubus_request {
  167. struct list_head list;
  168. struct list_head pending;
  169. int status_code;
  170. bool status_msg;
  171. bool blocked;
  172. bool cancelled;
  173. bool notify;
  174. uint32_t peer;
  175. uint16_t seq;
  176. ubus_data_handler_t raw_data_cb;
  177. ubus_data_handler_t data_cb;
  178. ubus_fd_handler_t fd_cb;
  179. ubus_complete_handler_t complete_cb;
  180. int fd;
  181. struct ubus_context *ctx;
  182. void *priv;
  183. };
  184. struct ubus_notify_request {
  185. struct ubus_request req;
  186. ubus_notify_complete_handler_t status_cb;
  187. ubus_notify_complete_handler_t complete_cb;
  188. ubus_notify_data_handler_t data_cb;
  189. uint32_t pending;
  190. uint32_t id[UBUS_MAX_NOTIFY_PEERS + 1];
  191. };
  192. struct ubus_auto_conn {
  193. struct ubus_context ctx;
  194. struct uloop_timeout timer;
  195. const char *path;
  196. ubus_connect_handler_t cb;
  197. };
  198. struct ubus_context *ubus_connect(const char *path);
  199. int ubus_connect_ctx(struct ubus_context *ctx, const char *path);
  200. void ubus_auto_connect(struct ubus_auto_conn *conn);
  201. int ubus_reconnect(struct ubus_context *ctx, const char *path);
  202. /* call this only for struct ubus_context pointers returned by ubus_connect() */
  203. void ubus_free(struct ubus_context *ctx);
  204. /* call this only for struct ubus_context pointers initialised by ubus_connect_ctx() */
  205. void ubus_shutdown(struct ubus_context *ctx);
  206. static inline void ubus_auto_shutdown(struct ubus_auto_conn *conn)
  207. {
  208. uloop_timeout_cancel(&conn->timer);
  209. ubus_shutdown(&conn->ctx);
  210. }
  211. const char *ubus_strerror(int error);
  212. static inline void ubus_add_uloop(struct ubus_context *ctx)
  213. {
  214. uloop_fd_add(&ctx->sock, ULOOP_BLOCKING | ULOOP_READ);
  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. /* ----------- raw request handling ----------- */
  222. /* wait for a request to complete and return its status */
  223. int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req,
  224. int timeout);
  225. /* complete a request asynchronously */
  226. void ubus_complete_request_async(struct ubus_context *ctx,
  227. struct ubus_request *req);
  228. /* abort an asynchronous request */
  229. void ubus_abort_request(struct ubus_context *ctx, struct ubus_request *req);
  230. /* ----------- objects ----------- */
  231. int ubus_lookup(struct ubus_context *ctx, const char *path,
  232. ubus_lookup_handler_t cb, void *priv);
  233. int ubus_lookup_id(struct ubus_context *ctx, const char *path, uint32_t *id);
  234. /* make an object visible to remote connections */
  235. int ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj);
  236. /* remove the object from the ubus connection */
  237. int ubus_remove_object(struct ubus_context *ctx, struct ubus_object *obj);
  238. /* add a subscriber notifications from another object */
  239. int ubus_register_subscriber(struct ubus_context *ctx, struct ubus_subscriber *obj);
  240. static inline int
  241. ubus_unregister_subscriber(struct ubus_context *ctx, struct ubus_subscriber *obj)
  242. {
  243. if (!list_empty(&obj->list))
  244. list_del_init(&obj->list);
  245. return ubus_remove_object(ctx, &obj->obj);
  246. }
  247. int ubus_subscribe(struct ubus_context *ctx, struct ubus_subscriber *obj, uint32_t id);
  248. int ubus_unsubscribe(struct ubus_context *ctx, struct ubus_subscriber *obj, uint32_t id);
  249. int __ubus_monitor(struct ubus_context *ctx, const char *type);
  250. static inline int ubus_monitor_start(struct ubus_context *ctx)
  251. {
  252. return __ubus_monitor(ctx, "add");
  253. }
  254. static inline int ubus_monitor_stop(struct ubus_context *ctx)
  255. {
  256. return __ubus_monitor(ctx, "remove");
  257. }
  258. /* ----------- acl ----------- */
  259. struct acl_object {
  260. struct ubus_acl_key key;
  261. struct avl_node avl;
  262. struct blob_attr *acl;
  263. };
  264. extern struct avl_tree acl_objects;
  265. int ubus_register_acl(struct ubus_context *ctx);
  266. #define acl_for_each(o, m) \
  267. if ((m)->object && (m)->user && (m)->group) \
  268. avl_for_element_range(avl_find_ge_element(&acl_objects, m, o, avl), avl_find_le_element(&acl_objects, m, o, avl), o, avl)
  269. /* ----------- rpc ----------- */
  270. /* invoke a method on a specific object */
  271. int ubus_invoke_fd(struct ubus_context *ctx, uint32_t obj, const char *method,
  272. struct blob_attr *msg, ubus_data_handler_t cb, void *priv,
  273. int timeout, int fd);
  274. static inline int
  275. ubus_invoke(struct ubus_context *ctx, uint32_t obj, const char *method,
  276. struct blob_attr *msg, ubus_data_handler_t cb, void *priv,
  277. int timeout)
  278. {
  279. return ubus_invoke_fd(ctx, obj, method, msg, cb, priv, timeout, -1);
  280. }
  281. /* asynchronous version of ubus_invoke() */
  282. int ubus_invoke_async_fd(struct ubus_context *ctx, uint32_t obj, const char *method,
  283. struct blob_attr *msg, struct ubus_request *req, int fd);
  284. static inline int
  285. ubus_invoke_async(struct ubus_context *ctx, uint32_t obj, const char *method,
  286. struct blob_attr *msg, struct ubus_request *req)
  287. {
  288. return ubus_invoke_async_fd(ctx, obj, method, msg, req, -1);
  289. }
  290. /* send a reply to an incoming object method call */
  291. int ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req,
  292. struct blob_attr *msg);
  293. static inline void ubus_defer_request(struct ubus_context *ctx,
  294. struct ubus_request_data *req,
  295. struct ubus_request_data *new_req)
  296. {
  297. (void) ctx;
  298. memcpy(new_req, req, sizeof(*req));
  299. req->deferred = true;
  300. }
  301. static inline void ubus_request_set_fd(struct ubus_context *ctx,
  302. struct ubus_request_data *req, int fd)
  303. {
  304. (void) ctx;
  305. req->fd = fd;
  306. }
  307. static inline int ubus_request_get_caller_fd(struct ubus_request_data *req)
  308. {
  309. int fd = req->req_fd;
  310. req->req_fd = -1;
  311. return fd;
  312. }
  313. void ubus_complete_deferred_request(struct ubus_context *ctx,
  314. struct ubus_request_data *req, int ret);
  315. /*
  316. * send a notification to all subscribers of an object
  317. * if timeout < 0, no reply is expected from subscribers
  318. */
  319. int ubus_notify(struct ubus_context *ctx, struct ubus_object *obj,
  320. const char *type, struct blob_attr *msg, int timeout);
  321. int ubus_notify_async(struct ubus_context *ctx, struct ubus_object *obj,
  322. const char *type, struct blob_attr *msg,
  323. struct ubus_notify_request *req);
  324. /* ----------- events ----------- */
  325. int ubus_send_event(struct ubus_context *ctx, const char *id,
  326. struct blob_attr *data);
  327. int ubus_register_event_handler(struct ubus_context *ctx,
  328. struct ubus_event_handler *ev,
  329. const char *pattern);
  330. static inline int ubus_unregister_event_handler(struct ubus_context *ctx,
  331. struct ubus_event_handler *ev)
  332. {
  333. return ubus_remove_object(ctx, &ev->obj);
  334. }
  335. #ifdef __cplusplus
  336. }
  337. #endif
  338. #endif