86581182082995e06dde13712d6c63be5be2419a
[project/ubus.git] / libubus.h
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
9 struct ubus_context;
10 struct ubus_msg_src;
11 struct ubus_object;
12 struct ubus_request;
13 struct ubus_request_data;
14
15 typedef int (*ubus_handler_t)(struct ubus_context *ctx, struct ubus_object *obj,
16                               struct ubus_request_data *req,
17                               const char *method, struct blob_attr *msg);
18 typedef void (*ubus_data_handler_t)(struct ubus_request *req,
19                                     int type, struct blob_attr *msg);
20 typedef void (*ubus_complete_handler_t)(struct ubus_request *req, int ret);
21
22
23 #define UBUS_SIGNATURE(_type, _name)    { .type = _type, .name = _name }
24
25 #define UBUS_METHOD_START(_name)                UBUS_SIGNATURE(UBUS_SIGNATURE_METHOD, _name)
26 #define UBUS_METHOD_END()                       UBUS_SIGNATURE(UBUS_SIGNATURE_END, NULL)
27
28 #define UBUS_FIELD(_type, _name)                UBUS_SIGNATURE(BLOBMSG_TYPE_ ## _type, _name)
29
30 #define UBUS_ARRAY(_name)                       UBUS_FIELD(ARRAY, _name)
31 #define UBUS_ARRAY_END()                        UBUS_SIGNATURE(UBUS_SIGNATURE_END, NULL)
32
33 #define UBUS_TABLE_START(_name)                 UBUS_FIELD(TABLE, _name)
34 #define UBUS_TABLE_END()                        UBUS_SIGNATURE(UBUS_SIGNATURE_END, NULL)
35
36 #define UBUS_OBJECT_TYPE(_name, _signature)             \
37         {                                               \
38                 .name = _name,                          \
39                 .id = 0,                                \
40                 .n_signature = ARRAY_SIZE(_signature),  \
41                 .signature = _signature                 \
42         }
43
44 struct ubus_signature {
45         enum blobmsg_type type;
46         const char *name;
47 };
48
49 struct ubus_object_type {
50         const char *name;
51         uint32_t id;
52         int n_signature;
53         const struct ubus_signature *signature;
54 };
55
56 struct ubus_method {
57         const char *name;
58         ubus_handler_t handler;
59 };
60
61 struct ubus_object {
62         struct avl_node avl;
63
64         const char *name;
65         uint32_t id;
66
67         const char *path;
68         struct ubus_object_type *type;
69
70         const struct ubus_method *methods;
71         int n_methods;
72 };
73
74 struct ubus_context {
75         struct list_head requests;
76         struct avl_tree objects;
77
78         struct uloop_fd sock;
79
80         uint32_t local_id;
81         uint32_t request_seq;
82
83         void (*connection_lost)(struct ubus_context *ctx);
84
85         struct {
86                 struct ubus_msghdr hdr;
87                 char data[UBUS_MAX_MSGLEN - sizeof(struct ubus_msghdr)];
88         } msgbuf;
89 };
90
91 struct ubus_request_data {
92         uint32_t object;
93         uint32_t peer;
94         uint32_t seq;
95 };
96
97 struct ubus_request {
98         struct list_head list;
99
100         struct list_head pending;
101         bool status_msg;
102         int status_code;
103         bool blocked;
104         bool cancelled;
105
106         uint32_t peer;
107         uint32_t seq;
108
109         ubus_data_handler_t raw_data_cb;
110         ubus_data_handler_t data_cb;
111         ubus_complete_handler_t complete_cb;
112
113         struct ubus_context *ctx;
114         void *priv;
115 };
116
117 #define BLOBMSG_END_TABLE       BLOBMSG_TYPE_UNSPEC
118
119 struct ubus_context *ubus_connect(const char *path);
120 void ubus_free(struct ubus_context *ctx);
121
122 const char *ubus_strerror(int error);
123
124 /* ----------- helpers for message handling ----------- */
125
126 struct blob_attr **ubus_parse_msg(struct blob_attr *msg);
127
128 /* ----------- raw request handling ----------- */
129
130 /* start a raw request */
131 int ubus_start_request(struct ubus_context *ctx, struct ubus_request *req,
132                        struct blob_attr *msg, int cmd, uint32_t peer);
133
134 /* wait for a request to complete and return its status */
135 int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req);
136
137 /* complete a request asynchronously */
138 void ubus_complete_request_async(struct ubus_context *ctx,
139                                  struct ubus_request *req);
140
141 /* abort an asynchronous request */
142 void ubus_abort_request(struct ubus_context *ctx, struct ubus_request *req);
143
144 /* ----------- rpc ----------- */
145
146 /* invoke a method on a specific object */
147 int ubus_invoke(struct ubus_context *ctx, uint32_t obj, const char *method,
148                 struct blob_attr *msg, ubus_data_handler_t cb, void *priv);
149
150 /* asynchronous version of ubus_invoke() */
151 void ubus_invoke_async(struct ubus_context *ctx, uint32_t obj, const char *method,
152                        struct blob_attr *msg, struct ubus_request *req);
153
154 /* make an object visible to remote connections */
155 int ubus_publish(struct ubus_context *ctx, struct ubus_object *obj);
156
157 /* send a reply to an incoming object method call */
158 int ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req,
159                     struct blob_attr *msg);