ubus: add notification for subscribers present/gone
[project/ubus.git] / libubus.h
1 /*
2  * Copyright (C) 2011 Felix Fietkau <nbd@openwrt.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 2.1
6  * as published by the Free Software Foundation
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #ifndef __LIBUBUS_H
15 #define __LIBUBUS_H
16
17 #include <libubox/avl.h>
18 #include <libubox/list.h>
19 #include <libubox/blobmsg.h>
20 #include <libubox/uloop.h>
21 #include <stdint.h>
22 #include "ubusmsg.h"
23 #include "ubus_common.h"
24
25 struct ubus_context;
26 struct ubus_msg_src;
27 struct ubus_object;
28 struct ubus_request;
29 struct ubus_request_data;
30 struct ubus_object_data;
31 struct ubus_event_handler;
32 struct ubus_subscriber;
33
34 typedef void (*ubus_lookup_handler_t)(struct ubus_context *ctx,
35                                       struct ubus_object_data *obj,
36                                       void *priv);
37 typedef int (*ubus_handler_t)(struct ubus_context *ctx, struct ubus_object *obj,
38                               struct ubus_request_data *req,
39                               const char *method, struct blob_attr *msg);
40 typedef void (*ubus_state_handler_t)(struct ubus_context *ctx, struct ubus_object *obj);
41 typedef void (*ubus_remove_handler_t)(struct ubus_context *ctx,
42                                       struct ubus_subscriber *obj, uint32_t id);
43 typedef void (*ubus_event_handler_t)(struct ubus_context *ctx, struct ubus_event_handler *ev,
44                                      const char *type, struct blob_attr *msg);
45 typedef void (*ubus_data_handler_t)(struct ubus_request *req,
46                                     int type, struct blob_attr *msg);
47 typedef void (*ubus_complete_handler_t)(struct ubus_request *req, int ret);
48
49 #define UBUS_OBJECT_TYPE(_name, _methods)               \
50         {                                               \
51                 .name = _name,                          \
52                 .id = 0,                                \
53                 .n_methods = ARRAY_SIZE(_methods),      \
54                 .methods = _methods                     \
55         }
56
57 #define UBUS_METHOD(_name, _handler, _policy)           \
58         {                                               \
59                 .name = _name,                          \
60                 .handler = _handler,                    \
61                 .policy = _policy,                      \
62                 .n_policy = ARRAY_SIZE(_policy)         \
63         }
64
65 struct ubus_method {
66         const char *name;
67         ubus_handler_t handler;
68
69         const struct blobmsg_policy *policy;
70         int n_policy;
71 };
72
73 struct ubus_object_type {
74         const char *name;
75         uint32_t id;
76
77         const struct ubus_method *methods;
78         int n_methods;
79 };
80
81 struct ubus_object {
82         struct avl_node avl;
83
84         const char *name;
85         uint32_t id;
86
87         const char *path;
88         struct ubus_object_type *type;
89
90         ubus_state_handler_t subscribe_cb;
91         bool has_subscribers;
92
93         const struct ubus_method *methods;
94         int n_methods;
95 };
96
97 struct ubus_subscriber {
98         struct ubus_object obj;
99
100         ubus_handler_t cb;
101         ubus_remove_handler_t remove_cb;
102 };
103
104 struct ubus_event_handler {
105         struct ubus_object obj;
106
107         ubus_event_handler_t cb;
108 };
109
110 struct ubus_context {
111         struct list_head requests;
112         struct avl_tree objects;
113         struct list_head pending;
114
115         struct uloop_fd sock;
116
117         uint32_t local_id;
118         uint32_t request_seq;
119         int stack_depth;
120
121         void (*connection_lost)(struct ubus_context *ctx);
122
123         struct {
124                 struct ubus_msghdr hdr;
125                 char data[UBUS_MAX_MSGLEN];
126         } msgbuf;
127 };
128
129 struct ubus_object_data {
130         uint32_t id;
131         uint32_t type_id;
132         const char *path;
133         struct blob_attr *signature;
134 };
135
136 struct ubus_request_data {
137         uint32_t object;
138         uint32_t peer;
139         uint32_t seq;
140         bool deferred;
141         bool notify;
142 };
143
144 struct ubus_request {
145         struct list_head list;
146
147         struct list_head pending;
148         bool status_msg;
149         int status_code;
150         bool blocked;
151         bool cancelled;
152
153         uint32_t peer;
154         uint32_t seq;
155
156         ubus_data_handler_t raw_data_cb;
157         ubus_data_handler_t data_cb;
158         ubus_complete_handler_t complete_cb;
159
160         struct ubus_context *ctx;
161         void *priv;
162 };
163
164
165 struct ubus_context *ubus_connect(const char *path);
166 int ubus_reconnect(struct ubus_context *ctx, const char *path);
167 void ubus_free(struct ubus_context *ctx);
168
169 const char *ubus_strerror(int error);
170
171 static inline void ubus_add_uloop(struct ubus_context *ctx)
172 {
173         uloop_fd_add(&ctx->sock, ULOOP_BLOCKING | ULOOP_READ);
174 }
175
176 /* call this for read events on ctx->sock.fd when not using uloop */
177 static inline void ubus_handle_event(struct ubus_context *ctx)
178 {
179         ctx->sock.cb(&ctx->sock, ULOOP_READ);
180 }
181
182 /* ----------- raw request handling ----------- */
183
184 /* wait for a request to complete and return its status */
185 int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req,
186                           int timeout);
187
188 /* complete a request asynchronously */
189 void ubus_complete_request_async(struct ubus_context *ctx,
190                                  struct ubus_request *req);
191
192 /* abort an asynchronous request */
193 void ubus_abort_request(struct ubus_context *ctx, struct ubus_request *req);
194
195 /* ----------- objects ----------- */
196
197 int ubus_lookup(struct ubus_context *ctx, const char *path,
198                 ubus_lookup_handler_t cb, void *priv);
199
200 int ubus_lookup_id(struct ubus_context *ctx, const char *path, uint32_t *id);
201
202 /* make an object visible to remote connections */
203 int ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj);
204
205 /* remove the object from the ubus connection */
206 int ubus_remove_object(struct ubus_context *ctx, struct ubus_object *obj);
207
208 /* add a subscriber notifications from another object */
209 int ubus_register_subscriber(struct ubus_context *ctx, struct ubus_subscriber *obj);
210 int ubus_subscribe(struct ubus_context *ctx, struct ubus_subscriber *obj, uint32_t id);
211 int ubus_unsubscribe(struct ubus_context *ctx, struct ubus_subscriber *obj, uint32_t id);
212
213 /* ----------- rpc ----------- */
214
215 /* invoke a method on a specific object */
216 int ubus_invoke(struct ubus_context *ctx, uint32_t obj, const char *method,
217                 struct blob_attr *msg, ubus_data_handler_t cb, void *priv,
218                 int timeout);
219
220 /* asynchronous version of ubus_invoke() */
221 int ubus_invoke_async(struct ubus_context *ctx, uint32_t obj, const char *method,
222                       struct blob_attr *msg, struct ubus_request *req);
223
224 /* send a reply to an incoming object method call */
225 int ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req,
226                     struct blob_attr *msg);
227
228 static inline void ubus_defer_request(struct ubus_context *ctx,
229                                       struct ubus_request_data *req,
230                                       struct ubus_request_data *new_req)
231 {
232     memcpy(new_req, req, sizeof(*req));
233     req->deferred = true;
234 }
235
236 void ubus_complete_deferred_request(struct ubus_context *ctx,
237                                     struct ubus_request_data *req, int ret);
238
239 /* ----------- events ----------- */
240
241 int ubus_send_event(struct ubus_context *ctx, const char *id,
242                     struct blob_attr *data);
243
244 int ubus_register_event_handler(struct ubus_context *ctx,
245                                 struct ubus_event_handler *ev,
246                                 const char *pattern);
247
248 static inline int ubus_unregister_event_handler(struct ubus_context *ctx,
249                                                 struct ubus_event_handler *ev)
250 {
251     return ubus_remove_object(ctx, &ev->obj);
252 }
253
254 #endif