Improve names
[src/app-framework-binder.git] / src / afb-xreq.c
1 /*
2  * Copyright (C) 2017 "IoT.bzh"
3  * Author José Bollo <jose.bollo@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #define _GNU_SOURCE
19 #define NO_BINDING_VERBOSE_MACRO
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24
25 #include <json-c/json.h>
26 #include <afb/afb-binding.h>
27
28 #include "afb-context.h"
29 #include "afb-xreq.h"
30 #include "afb-evt.h"
31 #include "afb-msg-json.h"
32 #include "afb-subcall.h"
33 #include "verbose.h"
34
35
36 static struct json_object *xreq_json_cb(void *closure);
37 static struct afb_arg xreq_get_cb(void *closure, const char *name);
38
39 static void xreq_success_cb(void *closure, struct json_object *obj, const char *info);
40 static void xreq_fail_cb(void *closure, const char *status, const char *info);
41
42 static const char *xreq_raw_cb(void *closure, size_t *size);
43 static void xreq_send_cb(void *closure, const char *buffer, size_t size);
44
45 static void *xreq_context_get_cb(void *closure);
46 static void xreq_context_set_cb(void *closure, void *value, void (*free_value)(void*));
47
48 static void xreq_addref_cb(void *closure);
49 static void xreq_unref_cb(void *closure);
50
51 static void xreq_session_close_cb(void *closure);
52 static int xreq_session_set_LOA_cb(void *closure, unsigned level);
53
54 static int xreq_subscribe_cb(void *closure, struct afb_event event);
55 static int xreq_unsubscribe_cb(void *closure, struct afb_event event);
56
57 static void xreq_subcall_cb(
58                 void *closure,
59                 const char *api,
60                 const char *verb,
61                 struct json_object *args,
62                 void (*callback)(void*, int, struct json_object*),
63                 void *cb_closure);
64
65 static int xreq_subcallsync_cb(
66                 void *closure,
67                 const char *api,
68                 const char *verb,
69                 struct json_object *args,
70                 struct json_object **result);
71
72 const struct afb_req_itf xreq_itf = {
73         .json = xreq_json_cb,
74         .get = xreq_get_cb,
75         .success = xreq_success_cb,
76         .fail = xreq_fail_cb,
77         .raw = xreq_raw_cb,
78         .send = xreq_send_cb,
79         .context_get = xreq_context_get_cb,
80         .context_set = xreq_context_set_cb,
81         .addref = xreq_addref_cb,
82         .unref = xreq_unref_cb,
83         .session_close = xreq_session_close_cb,
84         .session_set_LOA = xreq_session_set_LOA_cb,
85         .subscribe = xreq_subscribe_cb,
86         .unsubscribe = xreq_unsubscribe_cb,
87         .subcall = xreq_subcall_cb,
88         .subcallsync = xreq_subcallsync_cb
89 };
90
91 static struct json_object *xreq_json_cb(void *closure)
92 {
93         struct afb_xreq *xreq = closure;
94         return xreq->json ? : (xreq->json = xreq->queryitf->json(xreq->query));
95 }
96
97 static struct afb_arg xreq_get_cb(void *closure, const char *name)
98 {
99         struct afb_xreq *xreq = closure;
100         if (xreq->queryitf->get)
101                 return xreq->queryitf->get(xreq->query, name);
102         else
103                 return afb_msg_json_get_arg(xreq_json_cb(closure), name);
104 }
105
106 static void xreq_success_cb(void *closure, struct json_object *obj, const char *info)
107 {
108         struct afb_xreq *xreq = closure;
109         afb_xreq_success(xreq, obj, info);
110 }
111
112 void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info)
113 {
114         if (xreq->replied) {
115                 ERROR("reply called more than one time!!");
116                 json_object_put(obj);
117         } else {
118                 xreq->replied = 1;
119                 if (xreq->queryitf->success)
120                         xreq->queryitf->success(xreq->query, obj, info);
121                 else
122                         xreq->queryitf->reply(xreq->query, 0, afb_msg_json_reply_ok(info, obj, &xreq->context, NULL));
123         }
124 }
125
126 static void xreq_fail_cb(void *closure, const char *status, const char *info)
127 {
128         struct afb_xreq *xreq = closure;
129         afb_xreq_fail(xreq, status, info);
130 }
131
132 void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info)
133 {
134         if (xreq->replied) {
135                 ERROR("reply called more than one time!!");
136         } else {
137                 xreq->replied = 1;
138                 if (xreq->queryitf->fail)
139                         xreq->queryitf->fail(xreq->query, status, info);
140                 else
141                         xreq->queryitf->reply(xreq->query, 1, afb_msg_json_reply_error(status, info, &xreq->context, NULL));
142         }
143 }
144
145 static const char *xreq_raw_cb(void *closure, size_t *size)
146 {
147         struct afb_xreq *xreq = closure;
148         return afb_xreq_raw(xreq, size);
149 }
150
151 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
152 {
153         const char *result = json_object_to_json_string(xreq_json_cb(xreq));
154         if (size != NULL)
155                 *size = strlen(result);
156         return result;
157 }
158
159 static void xreq_send_cb(void *closure, const char *buffer, size_t size)
160 {
161         struct json_object *obj = json_tokener_parse(buffer);
162         if (!obj == !buffer)
163                 xreq_success_cb(closure, obj, "fake send");
164         else
165                 xreq_fail_cb(closure, "fake-send-failed", "fake send");
166 }
167
168 static void *xreq_context_get_cb(void *closure)
169 {
170         struct afb_xreq *xreq = closure;
171         return afb_context_get(&xreq->context);
172 }
173
174 static void xreq_context_set_cb(void *closure, void *value, void (*free_value)(void*))
175 {
176         struct afb_xreq *xreq = closure;
177         afb_context_set(&xreq->context, value, free_value);
178 }
179
180 static void xreq_addref_cb(void *closure)
181 {
182         struct afb_xreq *xreq = closure;
183         afb_xreq_addref(xreq);
184 }
185
186 void afb_xreq_addref(struct afb_xreq *xreq)
187 {
188         xreq->refcount++;
189 }
190
191 static void xreq_unref_cb(void *closure)
192 {
193         struct afb_xreq *xreq = closure;
194         afb_xreq_unref(xreq);
195 }
196
197 void afb_xreq_unref(struct afb_xreq *xreq)
198 {
199         if (!--xreq->refcount) {
200                 xreq->queryitf->unref(xreq->query);
201         }
202 }
203
204 static void xreq_session_close_cb(void *closure)
205 {
206         struct afb_xreq *xreq = closure;
207         afb_context_close(&xreq->context);
208 }
209
210 static int xreq_session_set_LOA_cb(void *closure, unsigned level)
211 {
212         struct afb_xreq *xreq = closure;
213         return afb_context_change_loa(&xreq->context, level);
214 }
215
216 static int xreq_subscribe_cb(void *closure, struct afb_event event)
217 {
218         struct afb_xreq *xreq = closure;
219         return afb_xreq_subscribe(xreq, event);
220 }
221
222 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_event event)
223 {
224         if (xreq->listener)
225                 return afb_evt_add_watch(xreq->listener, event);
226         if (xreq->queryitf->subscribe)
227                 return xreq->queryitf->subscribe(xreq->query, event);
228         ERROR("no event listener, subscription impossible");
229         errno = EINVAL;
230         return -1;
231 }
232
233 static int xreq_unsubscribe_cb(void *closure, struct afb_event event)
234 {
235         struct afb_xreq *xreq = closure;
236         return afb_xreq_unsubscribe(xreq, event);
237 }
238
239 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
240 {
241         if (xreq->listener)
242                 return afb_evt_remove_watch(xreq->listener, event);
243         if (xreq->queryitf->unsubscribe)
244                 return xreq->queryitf->unsubscribe(xreq->query, event);
245         ERROR("no event listener, unsubscription impossible");
246         errno = EINVAL;
247         return -1;
248 }
249
250 static void xreq_subcall_cb(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
251 {
252         struct afb_xreq *xreq = closure;
253
254         afb_xreq_subcall(xreq, api, verb, args, callback, cb_closure);
255 }
256
257 void afb_xreq_subcall(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
258 {
259         if (xreq->queryitf->subcall)
260                 xreq->queryitf->subcall(xreq->query, api, verb, args, callback, cb_closure);
261         else
262                 afb_subcall(xreq, api, verb, args, callback, cb_closure);
263 }
264
265 static int xreq_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
266 {
267         struct afb_xreq *xreq = closure;
268         return afb_subcall_sync(xreq, api, verb, args, result);
269 }
270
271 void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
272 {
273         char *message;
274         va_list args;
275         va_start(args, info);
276         if (info == NULL || vasprintf(&message, info, args) < 0)
277                 message = NULL;
278         va_end(args);
279         afb_xreq_success(xreq, obj, message);
280         free(message);
281 }
282
283 void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...)
284 {
285         char *message;
286         va_list args;
287         va_start(args, info);
288         if (info == NULL || vasprintf(&message, info, args) < 0)
289                 message = NULL;
290         va_end(args);
291         afb_xreq_fail(xreq, status, message);
292         free(message);
293 }
294
295 static int xcheck(struct afb_xreq *xreq, int sessionflags)
296 {
297         if ((sessionflags & (AFB_SESSION_CREATE|AFB_SESSION_CLOSE|AFB_SESSION_RENEW|AFB_SESSION_CHECK|AFB_SESSION_LOA_EQ)) != 0) {
298                 if (!afb_context_check(&xreq->context)) {
299                         afb_context_close(&xreq->context);
300                         afb_xreq_fail_f(xreq, "failed", "invalid token's identity");
301                         return 0;
302                 }
303         }
304
305         if ((sessionflags & AFB_SESSION_CREATE) != 0) {
306                 if (afb_context_check_loa(&xreq->context, 1)) {
307                         afb_xreq_fail_f(xreq, "failed", "invalid creation state");
308                         return 0;
309                 }
310                 afb_context_change_loa(&xreq->context, 1);
311                 afb_context_refresh(&xreq->context);
312         }
313
314         if ((sessionflags & (AFB_SESSION_CREATE | AFB_SESSION_RENEW)) != 0)
315                 afb_context_refresh(&xreq->context);
316
317         if ((sessionflags & AFB_SESSION_CLOSE) != 0) {
318                 afb_context_change_loa(&xreq->context, 0);
319                 afb_context_close(&xreq->context);
320         }
321
322         if ((sessionflags & AFB_SESSION_LOA_GE) != 0) {
323                 int loa = (sessionflags >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
324                 if (!afb_context_check_loa(&xreq->context, loa)) {
325                         afb_xreq_fail_f(xreq, "failed", "invalid LOA");
326                         return 0;
327                 }
328         }
329
330         if ((sessionflags & AFB_SESSION_LOA_LE) != 0) {
331                 int loa = (sessionflags >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
332                 if (afb_context_check_loa(&xreq->context, loa + 1)) {
333                         afb_xreq_fail_f(xreq, "failed", "invalid LOA");
334                         return 0;
335                 }
336         }
337         return 1;
338 }
339
340 void afb_xreq_call(struct afb_xreq *xreq, int sessionflags, void (*method)(struct afb_req req))
341 {
342         if (xcheck(xreq, sessionflags))
343                 method((struct afb_req){ .itf = &xreq_itf, .closure = xreq });
344 }
345