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