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