libubus.h 11 KB

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