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 NO_BINDING_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"
40 static struct json_object *xreq_json_cb(void *closure)
42 struct afb_xreq *xreq = closure;
43 return xreq->json ? : (xreq->json = xreq->queryitf->json(xreq));
46 static struct afb_arg xreq_get_cb(void *closure, const char *name)
48 struct afb_xreq *xreq = closure;
49 if (xreq->queryitf->get)
50 return xreq->queryitf->get(xreq, name);
52 return afb_msg_json_get_arg(xreq_json_cb(closure), name);
55 static void xreq_success_cb(void *closure, struct json_object *obj, const char *info)
57 struct afb_xreq *xreq = closure;
60 ERROR("reply called more than one time!!");
64 if (xreq->queryitf->success)
65 xreq->queryitf->success(xreq, obj, info);
67 xreq->queryitf->reply(xreq, 0, afb_msg_json_reply_ok(info, obj, &xreq->context, NULL));
71 static void xreq_fail_cb(void *closure, const char *status, const char *info)
73 struct afb_xreq *xreq = closure;
76 ERROR("reply called more than one time!!");
79 if (xreq->queryitf->fail)
80 xreq->queryitf->fail(xreq, status, info);
82 xreq->queryitf->reply(xreq, 1, afb_msg_json_reply_error(status, info, &xreq->context, NULL));
86 static const char *xreq_raw_cb(void *closure, size_t *size)
88 struct afb_xreq *xreq = closure;
89 const char *result = json_object_to_json_string(xreq_json_cb(xreq));
91 *size = strlen(result);
95 static void xreq_send_cb(void *closure, const char *buffer, size_t size)
97 struct json_object *obj = json_tokener_parse(buffer);
99 xreq_success_cb(closure, obj, "fake send");
101 xreq_fail_cb(closure, "fake-send-failed", "fake send");
104 static void *xreq_context_get_cb(void *closure)
106 struct afb_xreq *xreq = closure;
107 return afb_context_get(&xreq->context);
110 static void xreq_context_set_cb(void *closure, void *value, void (*free_value)(void*))
112 struct afb_xreq *xreq = closure;
113 afb_context_set(&xreq->context, value, free_value);
116 static void xreq_addref_cb(void *closure)
118 struct afb_xreq *xreq = closure;
119 __atomic_add_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED);
122 static void xreq_unref_cb(void *closure)
124 struct afb_xreq *xreq = closure;
125 if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED)) {
126 xreq->queryitf->unref(xreq);
130 static void xreq_session_close_cb(void *closure)
132 struct afb_xreq *xreq = closure;
133 afb_context_close(&xreq->context);
136 static int xreq_session_set_LOA_cb(void *closure, unsigned level)
138 struct afb_xreq *xreq = closure;
139 return afb_context_change_loa(&xreq->context, level);
142 static int xreq_subscribe_cb(void *closure, struct afb_event event)
144 struct afb_xreq *xreq = closure;
145 return afb_xreq_subscribe(xreq, event);
148 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_event event)
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");
159 static int xreq_unsubscribe_cb(void *closure, struct afb_event event)
161 struct afb_xreq *xreq = closure;
162 return afb_xreq_unsubscribe(xreq, event);
165 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
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");
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)
178 struct afb_xreq *xreq = closure;
180 if (xreq->queryitf->subcall)
181 xreq->queryitf->subcall(xreq, api, verb, args, callback, cb_closure);
183 afb_subcall(xreq, api, verb, args, callback, cb_closure);
186 static int xreq_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
188 struct afb_xreq *xreq = closure;
189 return afb_subcall_sync(xreq, api, verb, args, result);
192 static struct json_object *xreq_hooked_json_cb(void *closure)
194 struct json_object *r = xreq_json_cb(closure);
195 struct afb_xreq *xreq = closure;
196 return afb_hook_xreq_json(xreq, r);
199 static struct afb_arg xreq_hooked_get_cb(void *closure, const char *name)
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);
206 static void xreq_hooked_success_cb(void *closure, struct json_object *obj, const char *info)
208 struct afb_xreq *xreq = closure;
209 afb_hook_xreq_success(xreq, obj, info);
210 xreq_success_cb(closure, obj, info);
213 static void xreq_hooked_fail_cb(void *closure, const char *status, const char *info)
215 struct afb_xreq *xreq = closure;
216 afb_hook_xreq_fail(xreq, status, info);
217 xreq_fail_cb(closure, status, info);
220 static const char *xreq_hooked_raw_cb(void *closure, size_t *size)
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));
228 static void xreq_hooked_send_cb(void *closure, const char *buffer, size_t size)
230 struct afb_xreq *xreq = closure;
231 afb_hook_xreq_send(xreq, buffer, size);
232 xreq_send_cb(closure, buffer, size);
235 static void *xreq_hooked_context_get_cb(void *closure)
237 void *r = xreq_context_get_cb(closure);
238 struct afb_xreq *xreq = closure;
239 return afb_hook_xreq_context_get(xreq, r);
242 static void xreq_hooked_context_set_cb(void *closure, void *value, void (*free_value)(void*))
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);
249 static void xreq_hooked_addref_cb(void *closure)
251 struct afb_xreq *xreq = closure;
252 afb_hook_xreq_addref(xreq);
253 xreq_addref_cb(closure);
256 static void xreq_hooked_unref_cb(void *closure)
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);
266 static void xreq_hooked_session_close_cb(void *closure)
268 struct afb_xreq *xreq = closure;
269 afb_hook_xreq_session_close(xreq);
270 xreq_session_close_cb(closure);
273 static int xreq_hooked_session_set_LOA_cb(void *closure, unsigned level)
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);
280 static int xreq_hooked_subscribe_cb(void *closure, struct afb_event event)
282 int r = xreq_subscribe_cb(closure, event);
283 struct afb_xreq *xreq = closure;
284 return afb_hook_xreq_subscribe(xreq, event, r);
287 static int xreq_hooked_unsubscribe_cb(void *closure, struct afb_event event)
289 int r = xreq_unsubscribe_cb(closure, event);
290 struct afb_xreq *xreq = closure;
291 return afb_hook_xreq_unsubscribe(xreq, event, r);
296 struct afb_xreq *xreq;
297 void (*callback)(void*, int, struct json_object*);
301 static void xreq_hooked_subcall_reply_cb(void *closure, int iserror, struct json_object *result)
303 struct reply *reply = closure;
305 afb_hook_xreq_subcall_result(reply->xreq, iserror, result);
306 reply->callback(reply->closure, iserror, result);
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)
312 struct reply *reply = malloc(sizeof *reply);
313 struct afb_xreq *xreq = closure;
314 afb_hook_xreq_subcall(xreq, api, verb, args);
317 reply->callback = callback;
318 reply->closure = cb_closure;
319 xreq_subcall_cb(closure, api, verb, args, xreq_hooked_subcall_reply_cb, reply);
321 ERROR("out of memory");
322 xreq_subcall_cb(closure, api, verb, args, callback, cb_closure);
326 static int xreq_hooked_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
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);
335 const struct afb_req_itf xreq_itf = {
336 .json = xreq_json_cb,
338 .success = xreq_success_cb,
339 .fail = xreq_fail_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
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
373 static inline struct afb_req to_req(struct afb_xreq *xreq)
375 return (struct afb_req){ .itf = xreq->hookflags ? &xreq_hooked_itf : &xreq_itf, .closure = xreq };
378 void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info)
380 afb_req_success(to_req(xreq), obj, info);
383 void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
387 va_start(args, info);
388 if (info == NULL || vasprintf(&message, info, args) < 0)
391 afb_xreq_success(xreq, obj, message);
395 void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info)
397 afb_req_fail(to_req(xreq), status, info);
400 void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...)
404 va_start(args, info);
405 if (info == NULL || vasprintf(&message, info, args) < 0)
408 afb_xreq_fail(xreq, status, message);
412 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
414 return afb_req_raw(to_req(xreq), size);
417 void afb_xreq_addref(struct afb_xreq *xreq)
419 afb_req_addref(to_req(xreq));
422 void afb_xreq_unref(struct afb_xreq *xreq)
424 afb_req_unref(to_req(xreq));
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)
429 xreq_subcall_cb(xreq, api, verb, args, callback, cb_closure);
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)
434 afb_req_subcall(to_req(xreq), api, verb, args, callback, cb_closure);
437 static int xcheck(struct afb_xreq *xreq, int sessionflags)
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");
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");
452 afb_context_change_loa(&xreq->context, 1);
453 afb_context_refresh(&xreq->context);
456 if ((sessionflags & (AFB_SESSION_CREATE | AFB_SESSION_RENEW)) != 0)
457 afb_context_refresh(&xreq->context);
459 if ((sessionflags & AFB_SESSION_CLOSE) != 0) {
460 afb_context_change_loa(&xreq->context, 0);
461 afb_context_close(&xreq->context);
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");
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");
482 void afb_xreq_so_call(struct afb_xreq *xreq, int sessionflags, void (*method)(struct afb_req req))
484 if (xcheck(xreq, sessionflags))
485 method(to_req(xreq));
488 void afb_xreq_begin(struct afb_xreq *xreq)
490 afb_hook_init_xreq(xreq);
492 afb_hook_xreq_begin(xreq);
495 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
497 memset(xreq, 0, sizeof *xreq);
499 xreq->queryitf = queryitf;
503 static void process_async(int signum, void *arg)
505 struct afb_xreq *xreq = arg;
509 afb_xreq_fail_f(xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
512 afb_hook_init_xreq(xreq);
514 afb_hook_xreq_begin(xreq);
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);
520 xreq->context.api_key = api.closure;
521 api.itf->call(api.closure, xreq);
524 afb_xreq_unref(xreq);
527 void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
529 xreq->apiset = apiset;
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);
538 afb_xreq_unref(xreq);