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