fix unused but set variable
[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 #include <libubox/avl.h>
15 #include <libubox/list.h>
16 #include <libubox/blobmsg.h>
17 #include <libubox/uloop.h>
18 #include <stdint.h>
19 #include "ubusmsg.h"
20 #include "ubus_common.h"
21
22 struct ubus_context;
23 struct ubus_msg_src;
24 struct ubus_object;
25 struct ubus_request;
26 struct ubus_request_data;
27 struct ubus_object_data;
28 struct ubus_event_handler;
29
30 typedef void (*ubus_lookup_handler_t)(struct ubus_context *ctx,
31                                       struct ubus_object_data *obj,
32                                       void *priv);
33 typedef int (*ubus_handler_t)(struct ubus_context *ctx, struct ubus_object *obj,
34                               struct ubus_request_data *req,
35                               const char *method, struct blob_attr *msg);
36 typedef void (*ubus_event_handler_t)(struct ubus_context *ctx, struct ubus_event_handler *ev,
37                                      const char *type, struct blob_attr *msg);
38 typedef void (*ubus_data_handler_t)(struct ubus_request *req,
39                                     int type, struct blob_attr *msg);
40 typedef void (*ubus_complete_handler_t)(struct ubus_request *req, int ret);
41
42 #define UBUS_OBJECT_TYPE(_name, _methods)               \
43         {                                               \
44                 .name = _name,                          \
45                 .id = 0,                                \
46                 .n_methods = ARRAY_SIZE(_methods),      \
47                 .methods = _methods                     \
48         }
49
50 #define UBUS_METHOD(_name, _handler, _policy)           \
51         {                                               \
52                 .name = _name,                          \
53                 .handler = _handler,                    \
54                 .policy = _policy,                      \
55                 .n_policy = ARRAY_SIZE(_policy)         \
56         }
57
58 struct ubus_method {
59         const char *name;
60         ubus_handler_t handler;
61
62         const struct blobmsg_policy *policy;
63         int n_policy;
64 };
65
66 struct ubus_object_type {
67         const char *name;
68         uint32_t id;
69
70         const struct ubus_method *methods;
71         int n_methods;
72 };
73
74 struct ubus_object {
75         struct avl_node avl;
76
77         const char *name;
78         uint32_t id;
79
80         const char *path;
81         struct ubus_object_type *type;
82
83         const struct ubus_method *methods;
84         int n_methods;
85 };
86
87 struct ubus_event_handler {
88         struct ubus_object obj;
89
90         ubus_event_handler_t cb;
91 };
92
93 struct ubus_context {
94         struct list_head requests;
95         struct avl_tree objects;
96         struct list_head pending;
97
98         struct uloop_fd sock;
99
100         uint32_t local_id;
101         uint32_t request_seq;
102         int stack_depth;
103
104         void (*connection_lost)(struct ubus_context *ctx);
105
106         struct {
107                 struct ubus_msghdr hdr;
108                 char data[UBUS_MAX_MSGLEN];
109         } msgbuf;
110 };
111
112 struct ubus_object_data {
113         uint32_t id;
114         uint32_t type_id;
115         const char *path;
116         struct blob_attr *signature;
117 };
118
119 struct ubus_request_data {
120         uint32_t object;
121         uint32_t peer;
122         uint32_t seq;
123 };
124
125 struct ubus_request {
126         struct list_head list;
127
128         struct list_head pending;
129         bool status_msg;
130         int status_code;
131         bool blocked;
132         bool cancelled;
133
134         uint32_t peer;
135         uint32_t seq;
136
137         ubus_data_handler_t raw_data_cb;
138         ubus_data_handler_t data_cb;
139         ubus_complete_handler_t complete_cb;
140
141         struct ubus_context *ctx;
142         void *priv;
143 };
144
145
146 struct ubus_context *ubus_connect(const char *path);
147 void ubus_free(struct ubus_context *ctx);
148
149 const char *ubus_strerror(int error);
150
151 static inline void ubus_add_uloop(struct ubus_context *ctx)
152 {
153         uloop_fd_add(&ctx->sock, ULOOP_EDGE_TRIGGER | ULOOP_BLOCKING | ULOOP_READ);
154 }
155
156 /* call this for read events on ctx->sock.fd when not using uloop */
157 static inline void ubus_handle_event(struct ubus_context *ctx)
158 {
159         ctx->sock.cb(&ctx->sock, ULOOP_READ);
160 }
161
162 /* ----------- raw request handling ----------- */
163
164 /* wait for a request to complete and return its status */
165 int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req,
166                           int timeout);
167
168 /* complete a request asynchronously */
169 void ubus_complete_request_async(struct ubus_context *ctx,
170                                  struct ubus_request *req);
171
172 /* abort an asynchronous request */
173 void ubus_abort_request(struct ubus_context *ctx, struct ubus_request *req);
174
175 /* ----------- objects ----------- */
176
177 int ubus_lookup(struct ubus_context *ctx, const char *path,
178                 ubus_lookup_handler_t cb, void *priv);
179
180 int ubus_lookup_id(struct ubus_context *ctx, const char *path, uint32_t *id);
181
182 /* make an object visible to remote connections */
183 int ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj);
184
185 /* remove the object from the ubus connection */
186 int ubus_remove_object(struct ubus_context *ctx, struct ubus_object *obj);
187
188 /* ----------- rpc ----------- */
189
190 /* invoke a method on a specific object */
191 int ubus_invoke(struct ubus_context *ctx, uint32_t obj, const char *method,
192                 struct blob_attr *msg, ubus_data_handler_t cb, void *priv,
193                 int timeout);
194
195 /* asynchronous version of ubus_invoke() */
196 int ubus_invoke_async(struct ubus_context *ctx, uint32_t obj, const char *method,
197                       struct blob_attr *msg, struct ubus_request *req);
198
199 /* send a reply to an incoming object method call */
200 int ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req,
201                     struct blob_attr *msg);
202
203 /* ----------- events ----------- */
204
205 int ubus_send_event(struct ubus_context *ctx, const char *id,
206                     struct blob_attr *data);
207
208 int ubus_register_event_handler(struct ubus_context *ctx,
209                                 struct ubus_event_handler *ev,
210                                 const char *pattern);
211
212 static inline int ubus_unregister_event_handler(struct ubus_context *ctx,
213                                                 struct ubus_event_handler *ev)
214 {
215     return ubus_remove_object(ctx, &ev->obj);
216 }