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"
37 static struct json_object *xreq_json_cb(void *closure)
39 struct afb_xreq *xreq = closure;
40 return xreq->json ? : (xreq->json = xreq->queryitf->json(xreq->query));
43 static struct afb_arg xreq_get_cb(void *closure, const char *name)
45 struct afb_xreq *xreq = closure;
46 if (xreq->queryitf->get)
47 return xreq->queryitf->get(xreq->query, name);
49 return afb_msg_json_get_arg(xreq_json_cb(closure), name);
52 static void xreq_success_cb(void *closure, struct json_object *obj, const char *info)
54 struct afb_xreq *xreq = closure;
57 ERROR("reply called more than one time!!");
61 if (xreq->queryitf->success)
62 xreq->queryitf->success(xreq->query, obj, info);
64 xreq->queryitf->reply(xreq->query, 0, afb_msg_json_reply_ok(info, obj, &xreq->context, NULL));
68 static void xreq_fail_cb(void *closure, const char *status, const char *info)
70 struct afb_xreq *xreq = closure;
73 ERROR("reply called more than one time!!");
76 if (xreq->queryitf->fail)
77 xreq->queryitf->fail(xreq->query, status, info);
79 xreq->queryitf->reply(xreq->query, 1, afb_msg_json_reply_error(status, info, &xreq->context, NULL));
83 static const char *xreq_raw_cb(void *closure, size_t *size)
85 struct afb_xreq *xreq = closure;
86 const char *result = json_object_to_json_string(xreq_json_cb(xreq));
88 *size = strlen(result);
92 static void xreq_send_cb(void *closure, const char *buffer, size_t size)
94 struct json_object *obj = json_tokener_parse(buffer);
96 xreq_success_cb(closure, obj, "fake send");
98 xreq_fail_cb(closure, "fake-send-failed", "fake send");
101 static void *xreq_context_get_cb(void *closure)
103 struct afb_xreq *xreq = closure;
104 return afb_context_get(&xreq->context);
107 static void xreq_context_set_cb(void *closure, void *value, void (*free_value)(void*))
109 struct afb_xreq *xreq = closure;
110 afb_context_set(&xreq->context, value, free_value);
113 static void xreq_addref_cb(void *closure)
115 struct afb_xreq *xreq = closure;
116 __atomic_add_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED);
119 static void xreq_unref_cb(void *closure)
121 struct afb_xreq *xreq = closure;
122 if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED)) {
123 xreq->queryitf->unref(xreq->query);
127 static void xreq_session_close_cb(void *closure)
129 struct afb_xreq *xreq = closure;
130 afb_context_close(&xreq->context);
133 static int xreq_session_set_LOA_cb(void *closure, unsigned level)
135 struct afb_xreq *xreq = closure;
136 return afb_context_change_loa(&xreq->context, level);
139 static int xreq_subscribe_cb(void *closure, struct afb_event event)
141 struct afb_xreq *xreq = closure;
142 return afb_xreq_subscribe(xreq, event);
145 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_event event)
148 return afb_evt_add_watch(xreq->listener, event);
149 if (xreq->queryitf->subscribe)
150 return xreq->queryitf->subscribe(xreq->query, event);
151 ERROR("no event listener, subscription impossible");
156 static int xreq_unsubscribe_cb(void *closure, struct afb_event event)
158 struct afb_xreq *xreq = closure;
159 return afb_xreq_unsubscribe(xreq, event);
162 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
165 return afb_evt_remove_watch(xreq->listener, event);
166 if (xreq->queryitf->unsubscribe)
167 return xreq->queryitf->unsubscribe(xreq->query, event);
168 ERROR("no event listener, unsubscription impossible");
173 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)
175 struct afb_xreq *xreq = closure;
177 if (xreq->queryitf->subcall)
178 xreq->queryitf->subcall(xreq->query, api, verb, args, callback, cb_closure);
180 afb_subcall(xreq, api, verb, args, callback, cb_closure);
183 static int xreq_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
185 struct afb_xreq *xreq = closure;
186 return afb_subcall_sync(xreq, api, verb, args, result);
189 static struct json_object *xreq_hooked_json_cb(void *closure)
191 struct json_object *r = xreq_json_cb(closure);
192 struct afb_xreq *xreq = closure;
193 return afb_hook_xreq_json(xreq, r);
196 static struct afb_arg xreq_hooked_get_cb(void *closure, const char *name)
198 struct afb_arg r = xreq_get_cb(closure, name);
199 struct afb_xreq *xreq = closure;
200 return afb_hook_xreq_get(xreq, name, r);
203 static void xreq_hooked_success_cb(void *closure, struct json_object *obj, const char *info)
205 struct afb_xreq *xreq = closure;
206 afb_hook_xreq_success(xreq, obj, info);
207 xreq_success_cb(closure, obj, info);
210 static void xreq_hooked_fail_cb(void *closure, const char *status, const char *info)
212 struct afb_xreq *xreq = closure;
213 afb_hook_xreq_fail(xreq, status, info);
214 xreq_fail_cb(closure, status, info);
217 static const char *xreq_hooked_raw_cb(void *closure, size_t *size)
220 const char *r = xreq_raw_cb(closure, size ? : &s);
221 struct afb_xreq *xreq = closure;
222 return afb_hook_xreq_raw(xreq, r, *(size ? : &s));
225 static void xreq_hooked_send_cb(void *closure, const char *buffer, size_t size)
227 struct afb_xreq *xreq = closure;
228 afb_hook_xreq_send(xreq, buffer, size);
229 xreq_send_cb(closure, buffer, size);
232 static void *xreq_hooked_context_get_cb(void *closure)
234 void *r = xreq_context_get_cb(closure);
235 struct afb_xreq *xreq = closure;
236 return afb_hook_xreq_context_get(xreq, r);
239 static void xreq_hooked_context_set_cb(void *closure, void *value, void (*free_value)(void*))
241 struct afb_xreq *xreq = closure;
242 afb_hook_xreq_context_set(xreq, value, free_value);
243 xreq_context_set_cb(closure, value, free_value);
246 static void xreq_hooked_addref_cb(void *closure)
248 struct afb_xreq *xreq = closure;
249 afb_hook_xreq_addref(xreq);
250 xreq_addref_cb(closure);
253 static void xreq_hooked_unref_cb(void *closure)
255 struct afb_xreq *xreq = closure;
256 afb_hook_xreq_unref(xreq);
257 if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED)) {
258 afb_hook_xreq_end(xreq);
259 xreq->queryitf->unref(xreq->query);
263 static void xreq_hooked_session_close_cb(void *closure)
265 struct afb_xreq *xreq = closure;
266 afb_hook_xreq_session_close(xreq);
267 xreq_session_close_cb(closure);
270 static int xreq_hooked_session_set_LOA_cb(void *closure, unsigned level)
272 int r = xreq_session_set_LOA_cb(closure, level);
273 struct afb_xreq *xreq = closure;
274 return afb_hook_xreq_session_set_LOA(xreq, level, r);
277 static int xreq_hooked_subscribe_cb(void *closure, struct afb_event event)
279 int r = xreq_subscribe_cb(closure, event);
280 struct afb_xreq *xreq = closure;
281 return afb_hook_xreq_subscribe(xreq, event, r);
284 static int xreq_hooked_unsubscribe_cb(void *closure, struct afb_event event)
286 int r = xreq_unsubscribe_cb(closure, event);
287 struct afb_xreq *xreq = closure;
288 return afb_hook_xreq_unsubscribe(xreq, event, r);
293 struct afb_xreq *xreq;
294 void (*callback)(void*, int, struct json_object*);
298 static void xreq_hooked_subcall_reply_cb(void *closure, int iserror, struct json_object *result)
300 struct reply *reply = closure;
302 afb_hook_xreq_subcall_result(reply->xreq, iserror, result);
303 reply->callback(reply->closure, iserror, result);
307 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)
309 struct reply *reply = malloc(sizeof *reply);
310 struct afb_xreq *xreq = closure;
311 afb_hook_xreq_subcall(xreq, api, verb, args);
314 reply->callback = callback;
315 reply->closure = cb_closure;
316 xreq_subcall_cb(closure, api, verb, args, xreq_hooked_subcall_reply_cb, reply);
318 ERROR("out of memory");
319 xreq_subcall_cb(closure, api, verb, args, callback, cb_closure);
323 static int xreq_hooked_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
326 struct afb_xreq *xreq = closure;
327 afb_hook_xreq_subcallsync(xreq, api, verb, args);
328 r = xreq_subcallsync_cb(closure, api, verb, args, result);
329 return afb_hook_xreq_subcallsync_result(xreq, r, *result);
332 const struct afb_req_itf xreq_itf = {
333 .json = xreq_json_cb,
335 .success = xreq_success_cb,
336 .fail = xreq_fail_cb,
338 .send = xreq_send_cb,
339 .context_get = xreq_context_get_cb,
340 .context_set = xreq_context_set_cb,
341 .addref = xreq_addref_cb,
342 .unref = xreq_unref_cb,
343 .session_close = xreq_session_close_cb,
344 .session_set_LOA = xreq_session_set_LOA_cb,
345 .subscribe = xreq_subscribe_cb,
346 .unsubscribe = xreq_unsubscribe_cb,
347 .subcall = xreq_subcall_cb,
348 .subcallsync = xreq_subcallsync_cb
351 const struct afb_req_itf xreq_hooked_itf = {
352 .json = xreq_hooked_json_cb,
353 .get = xreq_hooked_get_cb,
354 .success = xreq_hooked_success_cb,
355 .fail = xreq_hooked_fail_cb,
356 .raw = xreq_hooked_raw_cb,
357 .send = xreq_hooked_send_cb,
358 .context_get = xreq_hooked_context_get_cb,
359 .context_set = xreq_hooked_context_set_cb,
360 .addref = xreq_hooked_addref_cb,
361 .unref = xreq_hooked_unref_cb,
362 .session_close = xreq_hooked_session_close_cb,
363 .session_set_LOA = xreq_hooked_session_set_LOA_cb,
364 .subscribe = xreq_hooked_subscribe_cb,
365 .unsubscribe = xreq_hooked_unsubscribe_cb,
366 .subcall = xreq_hooked_subcall_cb,
367 .subcallsync = xreq_hooked_subcallsync_cb
370 static inline struct afb_req to_req(struct afb_xreq *xreq)
372 return (struct afb_req){ .itf = xreq->hookflags ? &xreq_hooked_itf : &xreq_itf, .closure = xreq };
375 void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info)
377 afb_req_success(to_req(xreq), obj, info);
380 void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
384 va_start(args, info);
385 if (info == NULL || vasprintf(&message, info, args) < 0)
388 afb_xreq_success(xreq, obj, message);
392 void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info)
394 afb_req_fail(to_req(xreq), status, info);
397 void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...)
401 va_start(args, info);
402 if (info == NULL || vasprintf(&message, info, args) < 0)
405 afb_xreq_fail(xreq, status, message);
409 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
411 return afb_req_raw(to_req(xreq), size);
414 void afb_xreq_addref(struct afb_xreq *xreq)
416 afb_req_addref(to_req(xreq));
419 void afb_xreq_unref(struct afb_xreq *xreq)
421 afb_req_unref(to_req(xreq));
424 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)
426 xreq_subcall_cb(xreq, api, verb, args, callback, cb_closure);
429 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)
431 afb_req_subcall(to_req(xreq), api, verb, args, callback, cb_closure);
434 static int xcheck(struct afb_xreq *xreq, int sessionflags)
436 if ((sessionflags & (AFB_SESSION_CREATE|AFB_SESSION_CLOSE|AFB_SESSION_RENEW|AFB_SESSION_CHECK|AFB_SESSION_LOA_EQ)) != 0) {
437 if (!afb_context_check(&xreq->context)) {
438 afb_context_close(&xreq->context);
439 afb_xreq_fail_f(xreq, "failed", "invalid token's identity");
444 if ((sessionflags & AFB_SESSION_CREATE) != 0) {
445 if (afb_context_check_loa(&xreq->context, 1)) {
446 afb_xreq_fail_f(xreq, "failed", "invalid creation state");
449 afb_context_change_loa(&xreq->context, 1);
450 afb_context_refresh(&xreq->context);
453 if ((sessionflags & (AFB_SESSION_CREATE | AFB_SESSION_RENEW)) != 0)
454 afb_context_refresh(&xreq->context);
456 if ((sessionflags & AFB_SESSION_CLOSE) != 0) {
457 afb_context_change_loa(&xreq->context, 0);
458 afb_context_close(&xreq->context);
461 if ((sessionflags & AFB_SESSION_LOA_GE) != 0) {
462 int loa = (sessionflags >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
463 if (!afb_context_check_loa(&xreq->context, loa)) {
464 afb_xreq_fail_f(xreq, "failed", "invalid LOA");
469 if ((sessionflags & AFB_SESSION_LOA_LE) != 0) {
470 int loa = (sessionflags >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
471 if (afb_context_check_loa(&xreq->context, loa + 1)) {
472 afb_xreq_fail_f(xreq, "failed", "invalid LOA");
479 void afb_xreq_call(struct afb_xreq *xreq, int sessionflags, void (*method)(struct afb_req req))
481 if (xcheck(xreq, sessionflags))
482 method(to_req(xreq));
485 void afb_xreq_begin(struct afb_xreq *xreq)
487 afb_hook_init_xreq(xreq);
489 afb_hook_xreq_begin(xreq);