2 * Copyright (C) 2017 "IoT.bzh"
3 * Author José Bollo <jose.bollo@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #define AFB_BINDING_PRAGMA_NO_VERBOSE_MACRO
25 #include <json-c/json.h>
26 #include <afb/afb-binding.h>
28 #include "afb-context.h"
31 #include "afb-msg-json.h"
32 #include "afb-subcall.h"
35 #include "afb-apiset.h"
41 static struct json_object *xreq_json_cb(void *closure)
43 struct afb_xreq *xreq = closure;
44 return xreq->json ? : (xreq->json = xreq->queryitf->json(xreq));
47 static struct afb_arg xreq_get_cb(void *closure, const char *name)
49 struct afb_xreq *xreq = closure;
50 if (xreq->queryitf->get)
51 return xreq->queryitf->get(xreq, name);
53 return afb_msg_json_get_arg(xreq_json_cb(closure), name);
56 static void xreq_success_cb(void *closure, struct json_object *obj, const char *info)
58 struct afb_xreq *xreq = closure;
61 ERROR("reply called more than one time!!");
65 if (xreq->queryitf->success)
66 xreq->queryitf->success(xreq, obj, info);
68 xreq->queryitf->reply(xreq, 0, afb_msg_json_reply_ok(info, obj, &xreq->context, NULL));
72 static void xreq_fail_cb(void *closure, const char *status, const char *info)
74 struct afb_xreq *xreq = closure;
77 ERROR("reply called more than one time!!");
80 if (xreq->queryitf->fail)
81 xreq->queryitf->fail(xreq, status, info);
83 xreq->queryitf->reply(xreq, 1, afb_msg_json_reply_error(status, info, &xreq->context, NULL));
87 static const char *xreq_raw_cb(void *closure, size_t *size)
89 struct afb_xreq *xreq = closure;
90 const char *result = json_object_to_json_string(xreq_json_cb(xreq));
92 *size = strlen(result);
96 static void xreq_send_cb(void *closure, const char *buffer, size_t size)
98 struct json_object *obj = json_tokener_parse(buffer);
100 xreq_success_cb(closure, obj, "fake send");
102 xreq_fail_cb(closure, "fake-send-failed", "fake send");
105 static void *xreq_context_get_cb(void *closure)
107 struct afb_xreq *xreq = closure;
108 return afb_context_get(&xreq->context);
111 static void xreq_context_set_cb(void *closure, void *value, void (*free_value)(void*))
113 struct afb_xreq *xreq = closure;
114 afb_context_set(&xreq->context, value, free_value);
117 static void xreq_addref_cb(void *closure)
119 struct afb_xreq *xreq = closure;
120 __atomic_add_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED);
123 static void xreq_unref_cb(void *closure)
125 struct afb_xreq *xreq = closure;
126 if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED)) {
127 xreq->queryitf->unref(xreq);
131 static void xreq_session_close_cb(void *closure)
133 struct afb_xreq *xreq = closure;
134 afb_context_close(&xreq->context);
137 static int xreq_session_set_LOA_cb(void *closure, unsigned level)
139 struct afb_xreq *xreq = closure;
140 return afb_context_change_loa(&xreq->context, level);
143 static int xreq_subscribe_cb(void *closure, struct afb_event event)
145 struct afb_xreq *xreq = closure;
146 return afb_xreq_subscribe(xreq, event);
149 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_event event)
152 return afb_evt_add_watch(xreq->listener, event);
153 if (xreq->queryitf->subscribe)
154 return xreq->queryitf->subscribe(xreq, event);
155 ERROR("no event listener, subscription impossible");
160 static int xreq_unsubscribe_cb(void *closure, struct afb_event event)
162 struct afb_xreq *xreq = closure;
163 return afb_xreq_unsubscribe(xreq, event);
166 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
169 return afb_evt_remove_watch(xreq->listener, event);
170 if (xreq->queryitf->unsubscribe)
171 return xreq->queryitf->unsubscribe(xreq, event);
172 ERROR("no event listener, unsubscription impossible");
177 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)
179 struct afb_xreq *xreq = closure;
181 if (xreq->queryitf->subcall)
182 xreq->queryitf->subcall(xreq, api, verb, args, callback, cb_closure);
184 afb_subcall(xreq, api, verb, args, callback, cb_closure);
187 static int xreq_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
189 struct afb_xreq *xreq = closure;
190 return afb_subcall_sync(xreq, api, verb, args, result);
193 static struct json_object *xreq_hooked_json_cb(void *closure)
195 struct json_object *r = xreq_json_cb(closure);
196 struct afb_xreq *xreq = closure;
197 return afb_hook_xreq_json(xreq, r);
200 static struct afb_arg xreq_hooked_get_cb(void *closure, const char *name)
202 struct afb_arg r = xreq_get_cb(closure, name);
203 struct afb_xreq *xreq = closure;
204 return afb_hook_xreq_get(xreq, name, r);
207 static void xreq_hooked_success_cb(void *closure, struct json_object *obj, const char *info)
209 struct afb_xreq *xreq = closure;
210 afb_hook_xreq_success(xreq, obj, info);
211 xreq_success_cb(closure, obj, info);
214 static void xreq_hooked_fail_cb(void *closure, const char *status, const char *info)
216 struct afb_xreq *xreq = closure;
217 afb_hook_xreq_fail(xreq, status, info);
218 xreq_fail_cb(closure, status, info);
221 static const char *xreq_hooked_raw_cb(void *closure, size_t *size)
224 const char *r = xreq_raw_cb(closure, size ? : &s);
225 struct afb_xreq *xreq = closure;
226 return afb_hook_xreq_raw(xreq, r, *(size ? : &s));
229 static void xreq_hooked_send_cb(void *closure, const char *buffer, size_t size)
231 struct afb_xreq *xreq = closure;
232 afb_hook_xreq_send(xreq, buffer, size);
233 xreq_send_cb(closure, buffer, size);
236 static void *xreq_hooked_context_get_cb(void *closure)
238 void *r = xreq_context_get_cb(closure);
239 struct afb_xreq *xreq = closure;
240 return afb_hook_xreq_context_get(xreq, r);
243 static void xreq_hooked_context_set_cb(void *closure, void *value, void (*free_value)(void*))
245 struct afb_xreq *xreq = closure;
246 afb_hook_xreq_context_set(xreq, value, free_value);
247 xreq_context_set_cb(closure, value, free_value);
250 static void xreq_hooked_addref_cb(void *closure)
252 struct afb_xreq *xreq = closure;
253 afb_hook_xreq_addref(xreq);
254 xreq_addref_cb(closure);
257 static void xreq_hooked_unref_cb(void *closure)
259 struct afb_xreq *xreq = closure;
260 afb_hook_xreq_unref(xreq);
261 if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED)) {
262 afb_hook_xreq_end(xreq);
263 xreq->queryitf->unref(xreq);
267 static void xreq_hooked_session_close_cb(void *closure)
269 struct afb_xreq *xreq = closure;
270 afb_hook_xreq_session_close(xreq);
271 xreq_session_close_cb(closure);
274 static int xreq_hooked_session_set_LOA_cb(void *closure, unsigned level)
276 int r = xreq_session_set_LOA_cb(closure, level);
277 struct afb_xreq *xreq = closure;
278 return afb_hook_xreq_session_set_LOA(xreq, level, r);
281 static int xreq_hooked_subscribe_cb(void *closure, struct afb_event event)
283 int r = xreq_subscribe_cb(closure, event);
284 struct afb_xreq *xreq = closure;
285 return afb_hook_xreq_subscribe(xreq, event, r);
288 static int xreq_hooked_unsubscribe_cb(void *closure, struct afb_event event)
290 int r = xreq_unsubscribe_cb(closure, event);
291 struct afb_xreq *xreq = closure;
292 return afb_hook_xreq_unsubscribe(xreq, event, r);
297 struct afb_xreq *xreq;
298 void (*callback)(void*, int, struct json_object*);
302 static void xreq_hooked_subcall_reply_cb(void *closure, int iserror, struct json_object *result)
304 struct reply *reply = closure;
306 afb_hook_xreq_subcall_result(reply->xreq, iserror, result);
307 reply->callback(reply->closure, iserror, result);
311 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)
313 struct reply *reply = malloc(sizeof *reply);
314 struct afb_xreq *xreq = closure;
315 afb_hook_xreq_subcall(xreq, api, verb, args);
318 reply->callback = callback;
319 reply->closure = cb_closure;
320 xreq_subcall_cb(closure, api, verb, args, xreq_hooked_subcall_reply_cb, reply);
322 ERROR("out of memory");
323 xreq_subcall_cb(closure, api, verb, args, callback, cb_closure);
327 static int xreq_hooked_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
330 struct afb_xreq *xreq = closure;
331 afb_hook_xreq_subcallsync(xreq, api, verb, args);
332 r = xreq_subcallsync_cb(closure, api, verb, args, result);
333 return afb_hook_xreq_subcallsync_result(xreq, r, *result);
336 const struct afb_req_itf xreq_itf = {
337 .json = xreq_json_cb,
339 .success = xreq_success_cb,
340 .fail = xreq_fail_cb,
342 .send = xreq_send_cb,
343 .context_get = xreq_context_get_cb,
344 .context_set = xreq_context_set_cb,
345 .addref = xreq_addref_cb,
346 .unref = xreq_unref_cb,
347 .session_close = xreq_session_close_cb,
348 .session_set_LOA = xreq_session_set_LOA_cb,
349 .subscribe = xreq_subscribe_cb,
350 .unsubscribe = xreq_unsubscribe_cb,
351 .subcall = xreq_subcall_cb,
352 .subcallsync = xreq_subcallsync_cb
355 const struct afb_req_itf xreq_hooked_itf = {
356 .json = xreq_hooked_json_cb,
357 .get = xreq_hooked_get_cb,
358 .success = xreq_hooked_success_cb,
359 .fail = xreq_hooked_fail_cb,
360 .raw = xreq_hooked_raw_cb,
361 .send = xreq_hooked_send_cb,
362 .context_get = xreq_hooked_context_get_cb,
363 .context_set = xreq_hooked_context_set_cb,
364 .addref = xreq_hooked_addref_cb,
365 .unref = xreq_hooked_unref_cb,
366 .session_close = xreq_hooked_session_close_cb,
367 .session_set_LOA = xreq_hooked_session_set_LOA_cb,
368 .subscribe = xreq_hooked_subscribe_cb,
369 .unsubscribe = xreq_hooked_unsubscribe_cb,
370 .subcall = xreq_hooked_subcall_cb,
371 .subcallsync = xreq_hooked_subcallsync_cb
374 static inline struct afb_req to_req(struct afb_xreq *xreq)
376 return (struct afb_req){ .itf = xreq->hookflags ? &xreq_hooked_itf : &xreq_itf, .closure = xreq };
379 struct json_object *afb_xreq_json(struct afb_xreq *xreq)
381 return afb_req_json(to_req(xreq));
384 void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info)
386 afb_req_success(to_req(xreq), obj, info);
389 void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
393 va_start(args, info);
394 if (info == NULL || vasprintf(&message, info, args) < 0)
397 afb_xreq_success(xreq, obj, message);
401 void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info)
403 afb_req_fail(to_req(xreq), status, info);
406 void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...)
410 va_start(args, info);
411 if (info == NULL || vasprintf(&message, info, args) < 0)
414 afb_xreq_fail(xreq, status, message);
418 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
420 return afb_req_raw(to_req(xreq), size);
423 void afb_xreq_addref(struct afb_xreq *xreq)
425 afb_req_addref(to_req(xreq));
428 void afb_xreq_unref(struct afb_xreq *xreq)
430 afb_req_unref(to_req(xreq));
433 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)
435 xreq_subcall_cb(xreq, api, verb, args, callback, cb_closure);
438 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)
440 afb_req_subcall(to_req(xreq), api, verb, args, callback, cb_closure);
443 static int xreq_session_check_apply(struct afb_xreq *xreq, int sessionflags, const struct afb_auth *auth)
447 if ((sessionflags & (AFB_SESSION_CLOSE|AFB_SESSION_RENEW|AFB_SESSION_CHECK|AFB_SESSION_LOA_EQ)) != 0) {
448 if (!afb_context_check(&xreq->context)) {
449 afb_context_close(&xreq->context);
450 afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
456 if ((sessionflags & AFB_SESSION_LOA_GE) != 0) {
457 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
458 if (!afb_context_check_loa(&xreq->context, loa)) {
459 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
465 if ((sessionflags & AFB_SESSION_LOA_LE) != 0) {
466 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
467 if (afb_context_check_loa(&xreq->context, loa + 1)) {
468 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
474 if (auth && !afb_auth_check(auth, xreq)) {
475 afb_xreq_fail_f(xreq, "denied", "authorisation refused");
480 if ((sessionflags & AFB_SESSION_RENEW) != 0) {
481 afb_context_refresh(&xreq->context);
483 if ((sessionflags & AFB_SESSION_CLOSE) != 0) {
484 afb_context_change_loa(&xreq->context, 0);
485 afb_context_close(&xreq->context);
491 void afb_xreq_call_verb_v1(struct afb_xreq *xreq, const struct afb_verb_desc_v1 *verb)
494 afb_xreq_fail_unknown_verb(xreq);
496 if (!xreq_session_check_apply(xreq, verb->session, NULL))
497 verb->callback(to_req(xreq));
500 void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb)
503 afb_xreq_fail_unknown_verb(xreq);
505 if (!xreq_session_check_apply(xreq, verb->session, verb->auth))
506 verb->callback(to_req(xreq));
509 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
511 memset(xreq, 0, sizeof *xreq);
513 xreq->queryitf = queryitf;
516 void afb_xreq_fail_unknown_api(struct afb_xreq *xreq)
518 afb_xreq_fail_f(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->api, xreq->verb);
521 void afb_xreq_fail_unknown_verb(struct afb_xreq *xreq)
523 afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, xreq->api);
526 static void process_async(int signum, void *arg)
528 struct afb_xreq *xreq = arg;
532 afb_xreq_fail_f(xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
535 afb_hook_init_xreq(xreq);
537 afb_hook_xreq_begin(xreq);
540 if (afb_apiset_get(xreq->apiset, xreq->api, &api) < 0) {
541 afb_xreq_fail_f(xreq, "unknown-api", "api %s not found", xreq->api);
543 xreq->context.api_key = api.closure;
544 api.itf->call(api.closure, xreq);
547 afb_xreq_unref(xreq);
550 void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
552 xreq->apiset = apiset;
554 afb_xreq_addref(xreq);
555 if (jobs_queue(NULL, afb_apiset_timeout_get(apiset), process_async, xreq) < 0) {
556 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
557 ERROR("can't process job with threads: %m");
558 afb_xreq_fail_f(xreq, "cancelled", "not able to create a job for the task");
559 afb_xreq_unref(xreq);
561 afb_xreq_unref(xreq);