cead47648cebabf6804a22e28877267257eec98a
[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 struct ubus_object_data;
15 struct ubus_event_handler;
16
17 typedef void (*ubus_lookup_handler_t)(struct ubus_context *ctx,
18                                       struct ubus_object_data *obj,
19                                       void *priv);
20 typedef int (*ubus_handler_t)(struct ubus_context *ctx, struct ubus_object *obj,
21                               struct ubus_request_data *req,
22                               const char *method, struct blob_attr *msg);
23 typedef void (*ubus_event_handler_t)(struct ubus_context *ctx, struct ubus_event_handler *ev,
24                                      const char *type, struct blob_attr *msg);
25 typedef void (*ubus_data_handler_t)(struct ubus_request *req,
26                                     int type, struct blob_attr *msg);
27 typedef void (*ubus_complete_handler_t)(struct ubus_request *req, int ret);
28
29 #define UBUS_OBJECT_TYPE(_name, _methods)               \
30         {                                               \
31                 .name = _name,                          \
32                 .id = 0,                                \
33                 .n_methods = ARRAY_SIZE(_methods),      \
34                 .methods = _methods                     \
35         }
36
37 #define UBUS_METHOD(_name, _handler, _policy)           \
38         {                                               \
39                 .name = _name,                          \
40                 .handler = _handler,                    \
41                 .policy = _policy,                      \
42                 .n_policy = ARRAY_SIZE(_policy)         \
43         }
44
45 struct ubus_method {
46         const char *name;
47         ubus_handler_t handler;
48
49         const struct blobmsg_policy *policy;
50         int n_policy;
51 };
52
53 struct ubus_object_type {
54         const char *name;
55         uint32_t id;
56
57         const struct ubus_method *methods;
58         int n_methods;
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_event_handler {
75         struct ubus_object obj;
76
77         ubus_event_handler_t cb;
78 };
79
80 struct ubus_context {
81         struct list_head requests;
82         struct avl_tree objects;
83
84         struct uloop_fd sock;
85
86         uint32_t local_id;
87         uint32_t request_seq;
88
89         void (*connection_lost)(struct ubus_context *ctx);
90
91         struct {
92                 struct ubus_msghdr hdr;
93                 char data[UBUS_MAX_MSGLEN];
94         } msgbuf;
95 };
96
97 struct ubus_object_data {
98         uint32_t id;
99         uint32_t type_id;
100         const char *path;
101         struct blob_attr *signature;
102 };
103
104 struct ubus_request_data {
105         uint32_t object;
106         uint32_t peer;
107         uint32_t seq;
108 };
109
110 struct ubus_request {
111         struct list_head list;
112
113         struct list_head pending;
114         bool status_msg;
115         int status_code;
116         bool blocked;
117         bool cancelled;
118
119         uint32_t peer;
120         uint32_t seq;
121
122         ubus_data_handler_t raw_data_cb;
123         ubus_data_handler_t data_cb;
124         ubus_complete_handler_t complete_cb;
125
126         struct ubus_context *ctx;
127         void *priv;
128 };
129
130
131 struct ubus_context *ubus_connect(const char *path);
132 void ubus_free(struct ubus_context *ctx);
133
134 const char *ubus_strerror(int error);
135
136 static inline void ubus_add_uloop(struct ubus_context *ctx)
137 {
138         uloop_fd_add(&ctx->sock, ULOOP_EDGE_TRIGGER | ULOOP_BLOCKING | ULOOP_READ);
139 }
140
141 /* call this for read events on ctx->sock.fd when not using uloop */
142 static inline void ubus_handle_event(struct ubus_context *ctx)
143 {
144         ctx->sock.cb(&ctx->sock, ULOOP_READ);
145 }
146
147 /* ----------- raw request handling ----------- */
148
149 /* wait for a request to complete and return its status */
150 int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req,
151                           int timeout);
152
153 /* complete a request asynchronously */
154 void ubus_complete_request_async(struct ubus_context *ctx,
155                                  struct ubus_request *req);
156
157 /* abort an asynchronous request */
158 void ubus_abort_request(struct ubus_context *ctx, struct ubus_request *req);
159
160 /* ----------- objects ----------- */
161
162 int ubus_lookup(struct ubus_context *ctx, const char *path,
163                 ubus_lookup_handler_t cb, void *priv);
164
165 int ubus_lookup_id(struct ubus_context *ctx, const char *path, uint32_t *id);
166
167 /* make an object visible to remote connections */
168 int ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj);
169
170 /* remove the object from the ubus connection */
171 int ubus_remove_object(struct ubus_context *ctx, struct ubus_object *obj);
172
173 /* ----------- rpc ----------- */
174
175 /* invoke a method on a specific object */
176 int ubus_invoke(struct ubus_context *ctx, uint32_t obj, const char *method,
177                 struct blob_attr *msg, ubus_data_handler_t cb, void *priv,
178                 int timeout);
179
180 /* asynchronous version of ubus_invoke() */
181 int ubus_invoke_async(struct ubus_context *ctx, uint32_t obj, const char *method,
182                       struct blob_attr *msg, struct ubus_request *req);
183
184 /* send a reply to an incoming object method call */
185 int ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req,
186                     struct blob_attr *msg);
187
188 /* ----------- events ----------- */
189
190 int ubus_send_event(struct ubus_context *ctx, const char *id,
191                     struct blob_attr *data);
192
193 int ubus_register_event_handler(struct ubus_context *ctx,
194                                 struct ubus_event_handler *ev,
195                                 const char *pattern);
196
197 static inline int ubus_unregister_event_handler(struct ubus_context *ctx,
198                                                 struct ubus_event_handler *ev)
199 {
200     return ubus_remove_object(ctx, &ev->obj);
201 }