libubus.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #include <libubox/avl.h>
  2. #include <libubox/list.h>
  3. #include <libubox/blobmsg.h>
  4. #include <libubox/uloop.h>
  5. #include <stdint.h>
  6. #include "ubusmsg.h"
  7. #include "ubus_common.h"
  8. struct ubus_context;
  9. struct ubus_msg_src;
  10. struct ubus_object;
  11. struct ubus_request;
  12. struct ubus_request_data;
  13. struct ubus_object_data;
  14. typedef void (*ubus_lookup_handler_t)(struct ubus_context *ctx,
  15. struct ubus_object_data *obj,
  16. void *priv);
  17. typedef int (*ubus_handler_t)(struct ubus_context *ctx, struct ubus_object *obj,
  18. struct ubus_request_data *req,
  19. const char *method, struct blob_attr *msg);
  20. typedef void (*ubus_data_handler_t)(struct ubus_request *req,
  21. int type, struct blob_attr *msg);
  22. typedef void (*ubus_complete_handler_t)(struct ubus_request *req, int ret);
  23. #define UBUS_SIGNATURE(_type, _name) { .type = _type, .name = _name }
  24. #define UBUS_METHOD_START(_name) UBUS_SIGNATURE(UBUS_SIGNATURE_METHOD, _name)
  25. #define UBUS_METHOD_END() UBUS_SIGNATURE(UBUS_SIGNATURE_END, NULL)
  26. #define UBUS_FIELD(_type, _name) UBUS_SIGNATURE(BLOBMSG_TYPE_ ## _type, _name)
  27. #define UBUS_ARRAY(_name) UBUS_FIELD(ARRAY, _name)
  28. #define UBUS_ARRAY_END() UBUS_SIGNATURE(UBUS_SIGNATURE_END, NULL)
  29. #define UBUS_TABLE_START(_name) UBUS_FIELD(TABLE, _name)
  30. #define UBUS_TABLE_END() UBUS_SIGNATURE(UBUS_SIGNATURE_END, NULL)
  31. #define UBUS_OBJECT_TYPE(_name, _signature) \
  32. { \
  33. .name = _name, \
  34. .id = 0, \
  35. .n_signature = ARRAY_SIZE(_signature), \
  36. .signature = _signature \
  37. }
  38. struct ubus_signature {
  39. enum blobmsg_type type;
  40. const char *name;
  41. };
  42. struct ubus_object_type {
  43. const char *name;
  44. uint32_t id;
  45. int n_signature;
  46. const struct ubus_signature *signature;
  47. };
  48. struct ubus_method {
  49. const char *name;
  50. ubus_handler_t handler;
  51. };
  52. struct ubus_object {
  53. struct avl_node avl;
  54. const char *name;
  55. uint32_t id;
  56. const char *path;
  57. struct ubus_object_type *type;
  58. const struct ubus_method *methods;
  59. int n_methods;
  60. };
  61. struct ubus_context {
  62. struct list_head requests;
  63. struct avl_tree objects;
  64. struct uloop_fd sock;
  65. uint32_t local_id;
  66. uint32_t request_seq;
  67. void (*connection_lost)(struct ubus_context *ctx);
  68. struct {
  69. struct ubus_msghdr hdr;
  70. char data[UBUS_MAX_MSGLEN - sizeof(struct ubus_msghdr)];
  71. } msgbuf;
  72. };
  73. struct ubus_object_data {
  74. uint32_t id;
  75. uint32_t type_id;
  76. const char *path;
  77. struct blob_attr *signature;
  78. };
  79. struct ubus_request_data {
  80. uint32_t object;
  81. uint32_t peer;
  82. uint32_t seq;
  83. };
  84. struct ubus_request {
  85. struct list_head list;
  86. struct list_head pending;
  87. bool status_msg;
  88. int status_code;
  89. bool blocked;
  90. bool cancelled;
  91. uint32_t peer;
  92. uint32_t seq;
  93. ubus_data_handler_t raw_data_cb;
  94. ubus_data_handler_t data_cb;
  95. ubus_complete_handler_t complete_cb;
  96. struct ubus_context *ctx;
  97. void *priv;
  98. };
  99. struct ubus_context *ubus_connect(const char *path);
  100. void ubus_free(struct ubus_context *ctx);
  101. const char *ubus_strerror(int error);
  102. static inline void ubus_add_uloop(struct ubus_context *ctx)
  103. {
  104. uloop_fd_add(&ctx->sock, ULOOP_EDGE_TRIGGER | ULOOP_BLOCKING | ULOOP_READ);
  105. }
  106. /* ----------- raw request handling ----------- */
  107. /* wait for a request to complete and return its status */
  108. int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req);
  109. /* complete a request asynchronously */
  110. void ubus_complete_request_async(struct ubus_context *ctx,
  111. struct ubus_request *req);
  112. /* abort an asynchronous request */
  113. void ubus_abort_request(struct ubus_context *ctx, struct ubus_request *req);
  114. /* ----------- objects ----------- */
  115. int ubus_lookup(struct ubus_context *ctx, const char *path,
  116. ubus_lookup_handler_t cb, void *priv);
  117. int ubus_lookup_id(struct ubus_context *ctx, const char *path, uint32_t *id);
  118. /* ----------- rpc ----------- */
  119. /* invoke a method on a specific object */
  120. int ubus_invoke(struct ubus_context *ctx, uint32_t obj, const char *method,
  121. struct blob_attr *msg, ubus_data_handler_t cb, void *priv);
  122. /* asynchronous version of ubus_invoke() */
  123. void ubus_invoke_async(struct ubus_context *ctx, uint32_t obj, const char *method,
  124. struct blob_attr *msg, struct ubus_request *req);
  125. /* make an object visible to remote connections */
  126. int ubus_publish(struct ubus_context *ctx, struct ubus_object *obj);
  127. /* send a reply to an incoming object method call */
  128. int ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req,
  129. struct blob_attr *msg);
  130. /* ----------- events ----------- */
  131. int ubus_register_event_handler(struct ubus_context *ctx, struct ubus_object *obj,
  132. const char *pattern);