5db2992e94d8b5cafe231dfaffb9d056dc1b342a
[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
129 struct ubus_context *ubus_connect(const char *path);
130 void ubus_free(struct ubus_context *ctx);
131
132 const char *ubus_strerror(int error);
133
134 /* ----------- raw request handling ----------- */
135
136 /* wait for a request to complete and return its status */
137 int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req);
138
139 /* complete a request asynchronously */
140 void ubus_complete_request_async(struct ubus_context *ctx,
141                                  struct ubus_request *req);
142
143 /* abort an asynchronous request */
144 void ubus_abort_request(struct ubus_context *ctx, struct ubus_request *req);
145
146 /* ----------- objects ----------- */
147
148 int ubus_lookup(struct ubus_context *ctx, const char *path,
149                 ubus_lookup_handler_t cb, void *priv);
150
151 int ubus_lookup_id(struct ubus_context *ctx, const char *path, uint32_t *id);
152
153 /* ----------- rpc ----------- */
154
155 /* invoke a method on a specific object */
156 int ubus_invoke(struct ubus_context *ctx, uint32_t obj, const char *method,
157                 struct blob_attr *msg, ubus_data_handler_t cb, void *priv);
158
159 /* asynchronous version of ubus_invoke() */
160 void ubus_invoke_async(struct ubus_context *ctx, uint32_t obj, const char *method,
161                        struct blob_attr *msg, struct ubus_request *req);
162
163 /* make an object visible to remote connections */
164 int ubus_publish(struct ubus_context *ctx, struct ubus_object *obj);
165
166 /* send a reply to an incoming object method call */
167 int ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req,
168                     struct blob_attr *msg);