Introduce apiset for grouping apis
[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 "afb-hook.h"
34 #include "afb-api.h"
35 #include "afb-apiset.h"
36 #include "jobs.h"
37 #include "verbose.h"
38
39
40 static struct json_object *xreq_json_cb(void *closure)
41 {
42         struct afb_xreq *xreq = closure;
43         return xreq->json ? : (xreq->json = xreq->queryitf->json(xreq));
44 }
45
46 static struct afb_arg xreq_get_cb(void *closure, const char *name)
47 {
48         struct afb_xreq *xreq = closure;
49         if (xreq->queryitf->get)
50                 return xreq->queryitf->get(xreq, name);
51         else
52                 return afb_msg_json_get_arg(xreq_json_cb(closure), name);
53 }
54
55 static void xreq_success_cb(void *closure, struct json_object *obj, const char *info)
56 {
57         struct afb_xreq *xreq = closure;
58
59         if (xreq->replied) {
60                 ERROR("reply called more than one time!!");
61                 json_object_put(obj);
62         } else {
63                 xreq->replied = 1;
64                 if (xreq->queryitf->success)
65                         xreq->queryitf->success(xreq, obj, info);
66                 else
67                         xreq->queryitf->reply(xreq, 0, afb_msg_json_reply_ok(info, obj, &xreq->context, NULL));
68         }
69 }
70
71 static void xreq_fail_cb(void *closure, const char *status, const char *info)
72 {
73         struct afb_xreq *xreq = closure;
74
75         if (xreq->replied) {
76                 ERROR("reply called more than one time!!");
77         } else {
78                 xreq->replied = 1;
79                 if (xreq->queryitf->fail)
80                         xreq->queryitf->fail(xreq, status, info);
81                 else
82                         xreq->queryitf->reply(xreq, 1, afb_msg_json_reply_error(status, info, &xreq->context, NULL));
83         }
84 }
85
86 static const char *xreq_raw_cb(void *closure, size_t *size)
87 {
88         struct afb_xreq *xreq = closure;
89         const char *result = json_object_to_json_string(xreq_json_cb(xreq));
90         if (size != NULL)
91                 *size = strlen(result);
92         return result;
93 }
94
95 static void xreq_send_cb(void *closure, const char *buffer, size_t size)
96 {
97         struct json_object *obj = json_tokener_parse(buffer);
98         if (!obj == !buffer)
99                 xreq_success_cb(closure, obj, "fake send");
100         else
101                 xreq_fail_cb(closure, "fake-send-failed", "fake send");
102 }
103
104 static void *xreq_context_get_cb(void *closure)
105 {
106         struct afb_xreq *xreq = closure;
107         return afb_context_get(&xreq->context);
108 }
109
110 static void xreq_context_set_cb(void *closure, void *value, void (*free_value)(void*))
111 {
112         struct afb_xreq *xreq = closure;
113         afb_context_set(&xreq->context, value, free_value);
114 }
115
116 static void xreq_addref_cb(void *closure)
117 {
118         struct afb_xreq *xreq = closure;
119         __atomic_add_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED);
120 }
121
122 static void xreq_unref_cb(void *closure)
123 {
124         struct afb_xreq *xreq = closure;
125         if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED)) {
126                 xreq->queryitf->unref(xreq);
127         }
128 }
129
130 static void xreq_session_close_cb(void *closure)
131 {
132         struct afb_xreq *xreq = closure;
133         afb_context_close(&xreq->context);
134 }
135
136 static int xreq_session_set_LOA_cb(void *closure, unsigned level)
137 {
138         struct afb_xreq *xreq = closure;
139         return afb_context_change_loa(&xreq->context, level);
140 }
141
142 static int xreq_subscribe_cb(void *closure, struct afb_event event)
143 {
144         struct afb_xreq *xreq = closure;
145         return afb_xreq_subscribe(xreq, event);
146 }
147
148 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_event event)
149 {
150         if (xreq->listener)
151                 return afb_evt_add_watch(xreq->listener, event);
152         if (xreq->queryitf->subscribe)
153                 return xreq->queryitf->subscribe(xreq, event);
154         ERROR("no event listener, subscription impossible");
155         errno = EINVAL;
156         return -1;
157 }
158
159 static int xreq_unsubscribe_cb(void *closure, struct afb_event event)
160 {
161         struct afb_xreq *xreq = closure;
162         return afb_xreq_unsubscribe(xreq, event);
163 }
164
165 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
166 {
167         if (xreq->listener)
168                 return afb_evt_remove_watch(xreq->listener, event);
169         if (xreq->queryitf->unsubscribe)
170                 return xreq->queryitf->unsubscribe(xreq, event);
171         ERROR("no event listener, unsubscription impossible");
172         errno = EINVAL;
173         return -1;
174 }
175
176 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)
177 {
178         struct afb_xreq *xreq = closure;
179
180         if (xreq->queryitf->subcall)
181                 xreq->queryitf->subcall(xreq, api, verb, args, callback, cb_closure);
182         else
183                 afb_subcall(xreq, api, verb, args, callback, cb_closure);
184 }
185
186 static int xreq_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
187 {
188         struct afb_xreq *xreq = closure;
189         return afb_subcall_sync(xreq, api, verb, args, result);
190 }
191
192 static struct json_object *xreq_hooked_json_cb(void *closure)
193 {
194         struct json_object *r = xreq_json_cb(closure);
195         struct afb_xreq *xreq = closure;
196         return afb_hook_xreq_json(xreq, r);
197 }
198
199 static struct afb_arg xreq_hooked_get_cb(void *closure, const char *name)
200 {
201         struct afb_arg r = xreq_get_cb(closure, name);
202         struct afb_xreq *xreq = closure;
203         return afb_hook_xreq_get(xreq, name, r);
204 }
205
206 static void xreq_hooked_success_cb(void *closure, struct json_object *obj, const char *info)
207 {
208         struct afb_xreq *xreq = closure;
209         afb_hook_xreq_success(xreq, obj, info);
210         xreq_success_cb(closure, obj, info);
211 }
212
213 static void xreq_hooked_fail_cb(void *closure, const char *status, const char *info)
214 {
215         struct afb_xreq *xreq = closure;
216         afb_hook_xreq_fail(xreq, status, info);
217         xreq_fail_cb(closure, status, info);
218 }
219
220 static const char *xreq_hooked_raw_cb(void *closure, size_t *size)
221 {
222         size_t s;
223         const char *r = xreq_raw_cb(closure, size ? : &s);
224         struct afb_xreq *xreq = closure;
225         return afb_hook_xreq_raw(xreq, r, *(size ? : &s));
226 }
227
228 static void xreq_hooked_send_cb(void *closure, const char *buffer, size_t size)
229 {
230         struct afb_xreq *xreq = closure;
231         afb_hook_xreq_send(xreq, buffer, size);
232         xreq_send_cb(closure, buffer, size);
233 }
234
235 static void *xreq_hooked_context_get_cb(void *closure)
236 {
237         void *r = xreq_context_get_cb(closure);
238         struct afb_xreq *xreq = closure;
239         return afb_hook_xreq_context_get(xreq, r);
240 }
241
242 static void xreq_hooked_context_set_cb(void *closure, void *value, void (*free_value)(void*))
243 {
244         struct afb_xreq *xreq = closure;
245         afb_hook_xreq_context_set(xreq, value, free_value);
246         xreq_context_set_cb(closure, value, free_value);
247 }
248
249 static void xreq_hooked_addref_cb(void *closure)
250 {
251         struct afb_xreq *xreq = closure;
252         afb_hook_xreq_addref(xreq);
253         xreq_addref_cb(closure);
254 }
255
256 static void xreq_hooked_unref_cb(void *closure)
257 {
258         struct afb_xreq *xreq = closure;
259         afb_hook_xreq_unref(xreq);
260         if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED)) {
261                 afb_hook_xreq_end(xreq);
262                 xreq->queryitf->unref(xreq);
263         }
264 }
265
266 static void xreq_hooked_session_close_cb(void *closure)
267 {
268         struct afb_xreq *xreq = closure;
269         afb_hook_xreq_session_close(xreq);
270         xreq_session_close_cb(closure);
271 }
272
273 static int xreq_hooked_session_set_LOA_cb(void *closure, unsigned level)
274 {
275         int r = xreq_session_set_LOA_cb(closure, level);
276         struct afb_xreq *xreq = closure;
277         return afb_hook_xreq_session_set_LOA(xreq, level, r);
278 }
279
280 static int xreq_hooked_subscribe_cb(void *closure, struct afb_event event)
281 {
282         int r = xreq_subscribe_cb(closure, event);
283         struct afb_xreq *xreq = closure;
284         return afb_hook_xreq_subscribe(xreq, event, r);
285 }
286
287 static int xreq_hooked_unsubscribe_cb(void *closure, struct afb_event event)
288 {
289         int r = xreq_unsubscribe_cb(closure, event);
290         struct afb_xreq *xreq = closure;
291         return afb_hook_xreq_unsubscribe(xreq, event, r);
292 }
293
294 struct reply
295 {
296         struct afb_xreq *xreq;
297         void (*callback)(void*, int, struct json_object*);
298         void *closure;
299 };
300
301 static void xreq_hooked_subcall_reply_cb(void *closure, int iserror, struct json_object *result)
302 {
303         struct reply *reply = closure;
304         
305         afb_hook_xreq_subcall_result(reply->xreq, iserror, result);
306         reply->callback(reply->closure, iserror, result);
307         free(reply);
308 }
309
310 static void xreq_hooked_subcall_cb(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
311 {
312         struct reply *reply = malloc(sizeof *reply);
313         struct afb_xreq *xreq = closure;
314         afb_hook_xreq_subcall(xreq, api, verb, args);
315         if (reply) {
316                 reply->xreq = xreq;
317                 reply->callback = callback;
318                 reply->closure = cb_closure;
319                 xreq_subcall_cb(closure, api, verb, args, xreq_hooked_subcall_reply_cb, reply);
320         } else {
321                 ERROR("out of memory");
322                 xreq_subcall_cb(closure, api, verb, args, callback, cb_closure);
323         }
324 }
325
326 static int xreq_hooked_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
327 {
328         int r;
329         struct afb_xreq *xreq = closure;
330         afb_hook_xreq_subcallsync(xreq, api, verb, args);
331         r = xreq_subcallsync_cb(closure, api, verb, args, result);
332         return afb_hook_xreq_subcallsync_result(xreq, r, *result);
333 }
334
335 const struct afb_req_itf xreq_itf = {
336         .json = xreq_json_cb,
337         .get = xreq_get_cb,
338         .success = xreq_success_cb,
339         .fail = xreq_fail_cb,
340         .raw = xreq_raw_cb,
341         .send = xreq_send_cb,
342         .context_get = xreq_context_get_cb,
343         .context_set = xreq_context_set_cb,
344         .addref = xreq_addref_cb,
345         .unref = xreq_unref_cb,
346         .session_close = xreq_session_close_cb,
347         .session_set_LOA = xreq_session_set_LOA_cb,
348         .subscribe = xreq_subscribe_cb,
349         .unsubscribe = xreq_unsubscribe_cb,
350         .subcall = xreq_subcall_cb,
351         .subcallsync = xreq_subcallsync_cb
352 };
353
354 const struct afb_req_itf xreq_hooked_itf = {
355         .json = xreq_hooked_json_cb,
356         .get = xreq_hooked_get_cb,
357         .success = xreq_hooked_success_cb,
358         .fail = xreq_hooked_fail_cb,
359         .raw = xreq_hooked_raw_cb,
360         .send = xreq_hooked_send_cb,
361         .context_get = xreq_hooked_context_get_cb,
362         .context_set = xreq_hooked_context_set_cb,
363         .addref = xreq_hooked_addref_cb,
364         .unref = xreq_hooked_unref_cb,
365         .session_close = xreq_hooked_session_close_cb,
366         .session_set_LOA = xreq_hooked_session_set_LOA_cb,
367         .subscribe = xreq_hooked_subscribe_cb,
368         .unsubscribe = xreq_hooked_unsubscribe_cb,
369         .subcall = xreq_hooked_subcall_cb,
370         .subcallsync = xreq_hooked_subcallsync_cb
371 };
372
373 static inline struct afb_req to_req(struct afb_xreq *xreq)
374 {
375         return (struct afb_req){ .itf = xreq->hookflags ? &xreq_hooked_itf : &xreq_itf, .closure = xreq };
376 }
377
378 void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info)
379 {
380         afb_req_success(to_req(xreq), obj, info);
381 }
382
383 void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
384 {
385         char *message;
386         va_list args;
387         va_start(args, info);
388         if (info == NULL || vasprintf(&message, info, args) < 0)
389                 message = NULL;
390         va_end(args);
391         afb_xreq_success(xreq, obj, message);
392         free(message);
393 }
394
395 void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info)
396 {
397         afb_req_fail(to_req(xreq), status, info);
398 }
399
400 void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...)
401 {
402         char *message;
403         va_list args;
404         va_start(args, info);
405         if (info == NULL || vasprintf(&message, info, args) < 0)
406                 message = NULL;
407         va_end(args);
408         afb_xreq_fail(xreq, status, message);
409         free(message);
410 }
411
412 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
413 {
414         return afb_req_raw(to_req(xreq), size);
415 }
416
417 void afb_xreq_addref(struct afb_xreq *xreq)
418 {
419         afb_req_addref(to_req(xreq));
420 }
421
422 void afb_xreq_unref(struct afb_xreq *xreq)
423 {
424         afb_req_unref(to_req(xreq));
425 }
426
427 void afb_xreq_unhooked_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)
428 {
429         xreq_subcall_cb(xreq, api, verb, args, callback, cb_closure);
430 }
431
432 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)
433 {
434         afb_req_subcall(to_req(xreq), api, verb, args, callback, cb_closure);
435 }
436
437 static int xcheck(struct afb_xreq *xreq, int sessionflags)
438 {
439         if ((sessionflags & (AFB_SESSION_CREATE|AFB_SESSION_CLOSE|AFB_SESSION_RENEW|AFB_SESSION_CHECK|AFB_SESSION_LOA_EQ)) != 0) {
440                 if (!afb_context_check(&xreq->context)) {
441                         afb_context_close(&xreq->context);
442                         afb_xreq_fail_f(xreq, "failed", "invalid token's identity");
443                         return 0;
444                 }
445         }
446
447         if ((sessionflags & AFB_SESSION_CREATE) != 0) {
448                 if (afb_context_check_loa(&xreq->context, 1)) {
449                         afb_xreq_fail_f(xreq, "failed", "invalid creation state");
450                         return 0;
451                 }
452                 afb_context_change_loa(&xreq->context, 1);
453                 afb_context_refresh(&xreq->context);
454         }
455
456         if ((sessionflags & (AFB_SESSION_CREATE | AFB_SESSION_RENEW)) != 0)
457                 afb_context_refresh(&xreq->context);
458
459         if ((sessionflags & AFB_SESSION_CLOSE) != 0) {
460                 afb_context_change_loa(&xreq->context, 0);
461                 afb_context_close(&xreq->context);
462         }
463
464         if ((sessionflags & AFB_SESSION_LOA_GE) != 0) {
465                 int loa = (sessionflags >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
466                 if (!afb_context_check_loa(&xreq->context, loa)) {
467                         afb_xreq_fail_f(xreq, "failed", "invalid LOA");
468                         return 0;
469                 }
470         }
471
472         if ((sessionflags & AFB_SESSION_LOA_LE) != 0) {
473                 int loa = (sessionflags >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
474                 if (afb_context_check_loa(&xreq->context, loa + 1)) {
475                         afb_xreq_fail_f(xreq, "failed", "invalid LOA");
476                         return 0;
477                 }
478         }
479         return 1;
480 }
481
482 void afb_xreq_so_call(struct afb_xreq *xreq, int sessionflags, void (*method)(struct afb_req req))
483 {
484         if (xcheck(xreq, sessionflags))
485                 method(to_req(xreq));
486 }
487
488 void afb_xreq_begin(struct afb_xreq *xreq)
489 {
490         afb_hook_init_xreq(xreq);
491         if (xreq->hookflags)
492                 afb_hook_xreq_begin(xreq);
493 }
494
495 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
496 {
497         memset(xreq, 0, sizeof *xreq);
498         xreq->refcount = 1;
499         xreq->queryitf = queryitf;
500 }
501
502
503 static void process_async(int signum, void *arg)
504 {
505         struct afb_xreq *xreq = arg;
506         struct afb_api api;
507
508         if (signum != 0) {
509                 afb_xreq_fail_f(xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
510         } else {
511                 /* init hooking */
512                 afb_hook_init_xreq(xreq);
513                 if (xreq->hookflags)
514                         afb_hook_xreq_begin(xreq);
515
516                 /* search the api */
517                 if (afb_apiset_get(xreq->apiset, xreq->api, &api) < 0) {
518                         afb_xreq_fail_f(xreq, "unknown-api", "api %s not found", xreq->api);
519                 } else {
520                         xreq->context.api_key = api.closure;
521                         api.itf->call(api.closure, xreq);
522                 }
523         }
524         afb_xreq_unref(xreq);
525 }
526
527 void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
528 {
529         xreq->apiset = apiset;
530
531         afb_xreq_addref(xreq);
532         if (jobs_queue(NULL, afb_apiset_get_timeout(apiset), process_async, xreq) < 0) {
533                 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
534                 ERROR("can't process job with threads: %m");
535                 afb_xreq_fail_f(xreq, "cancelled", "not able to create a job for the task");
536                 afb_xreq_unref(xreq);
537         }
538         afb_xreq_unref(xreq);
539 }
540