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;
55 afb_xreq_success(xreq, obj, info);
58 void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info)
61 ERROR("reply called more than one time!!");
65 if (xreq->queryitf->success)
66 xreq->queryitf->success(xreq->query, obj, info);
68 xreq->queryitf->reply(xreq->query, 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;
75 afb_xreq_fail(xreq, status, info);
78 void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info)
81 ERROR("reply called more than one time!!");
84 if (xreq->queryitf->fail)
85 xreq->queryitf->fail(xreq->query, status, info);
87 xreq->queryitf->reply(xreq->query, 1, afb_msg_json_reply_error(status, info, &xreq->context, NULL));
91 static const char *xreq_raw_cb(void *closure, size_t *size)
93 struct afb_xreq *xreq = closure;
94 return afb_xreq_raw(xreq, size);
97 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
99 const char *result = json_object_to_json_string(xreq_json_cb(xreq));
101 *size = strlen(result);
105 static void xreq_send_cb(void *closure, const char *buffer, size_t size)
107 struct json_object *obj = json_tokener_parse(buffer);
109 xreq_success_cb(closure, obj, "fake send");
111 xreq_fail_cb(closure, "fake-send-failed", "fake send");
114 static void *xreq_context_get_cb(void *closure)
116 struct afb_xreq *xreq = closure;
117 return afb_context_get(&xreq->context);
120 static void xreq_context_set_cb(void *closure, void *value, void (*free_value)(void*))
122 struct afb_xreq *xreq = closure;
123 afb_context_set(&xreq->context, value, free_value);
126 static void xreq_addref_cb(void *closure)
128 struct afb_xreq *xreq = closure;
129 afb_xreq_addref(xreq);
132 void afb_xreq_addref(struct afb_xreq *xreq)
137 static void xreq_unref_cb(void *closure)
139 struct afb_xreq *xreq = closure;
140 afb_xreq_unref(xreq);
143 void afb_xreq_unref(struct afb_xreq *xreq)
145 if (!--xreq->refcount) {
146 xreq->queryitf->unref(xreq->query);
150 static void xreq_session_close_cb(void *closure)
152 struct afb_xreq *xreq = closure;
153 afb_context_close(&xreq->context);
156 static int xreq_session_set_LOA_cb(void *closure, unsigned level)
158 struct afb_xreq *xreq = closure;
159 return afb_context_change_loa(&xreq->context, level);
162 static int xreq_subscribe_cb(void *closure, struct afb_event event)
164 struct afb_xreq *xreq = closure;
165 return afb_xreq_subscribe(xreq, event);
168 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_event event)
171 return afb_evt_add_watch(xreq->listener, event);
172 if (xreq->queryitf->subscribe)
173 return xreq->queryitf->subscribe(xreq->query, event);
174 ERROR("no event listener, subscription impossible");
179 static int xreq_unsubscribe_cb(void *closure, struct afb_event event)
181 struct afb_xreq *xreq = closure;
182 return afb_xreq_unsubscribe(xreq, event);
185 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
188 return afb_evt_remove_watch(xreq->listener, event);
189 if (xreq->queryitf->unsubscribe)
190 return xreq->queryitf->unsubscribe(xreq->query, event);
191 ERROR("no event listener, unsubscription impossible");
196 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)
198 struct afb_xreq *xreq = closure;
200 afb_xreq_subcall(xreq, api, verb, args, callback, cb_closure);
203 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)
205 if (xreq->queryitf->subcall)
206 xreq->queryitf->subcall(xreq->query, api, verb, args, callback, cb_closure);
208 afb_subcall(xreq, api, verb, args, callback, cb_closure);
211 static int xreq_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
213 struct afb_xreq *xreq = closure;
214 return afb_subcall_sync(xreq, api, verb, args, result);
217 static struct json_object *xreq_hooked_json_cb(void *closure)
219 struct json_object *r = xreq_json_cb(closure);
220 struct afb_xreq *xreq = closure;
221 return afb_hook_xreq_json(xreq, r);
224 static struct afb_arg xreq_hooked_get_cb(void *closure, const char *name)
226 struct afb_arg r = xreq_get_cb(closure, name);
227 struct afb_xreq *xreq = closure;
228 return afb_hook_xreq_get(xreq, name, r);
231 static void xreq_hooked_success_cb(void *closure, struct json_object *obj, const char *info)
233 struct afb_xreq *xreq = closure;
234 afb_hook_xreq_success(xreq, obj, info);
235 xreq_success_cb(closure, obj, info);
238 static void xreq_hooked_fail_cb(void *closure, const char *status, const char *info)
240 struct afb_xreq *xreq = closure;
241 afb_hook_xreq_fail(xreq, status, info);
242 xreq_fail_cb(closure, status, info);
245 static const char *xreq_hooked_raw_cb(void *closure, size_t *size)
248 const char *r = xreq_raw_cb(closure, size ? : &s);
249 struct afb_xreq *xreq = closure;
250 return afb_hook_xreq_raw(xreq, r, *(size ? : &s));
253 static void xreq_hooked_send_cb(void *closure, const char *buffer, size_t size)
255 struct afb_xreq *xreq = closure;
256 afb_hook_xreq_send(xreq, buffer, size);
257 xreq_send_cb(closure, buffer, size);
260 static void *xreq_hooked_context_get_cb(void *closure)
262 void *r = xreq_context_get_cb(closure);
263 struct afb_xreq *xreq = closure;
264 return afb_hook_xreq_context_get(xreq, r);
267 static void xreq_hooked_context_set_cb(void *closure, void *value, void (*free_value)(void*))
269 struct afb_xreq *xreq = closure;
270 afb_hook_xreq_context_set(xreq, value, free_value);
271 xreq_context_set_cb(closure, value, free_value);
274 static void xreq_hooked_addref_cb(void *closure)
276 struct afb_xreq *xreq = closure;
277 afb_hook_xreq_addref(xreq);
278 xreq_addref_cb(closure);
281 static void xreq_hooked_unref_cb(void *closure)
286 static void xreq_hooked_session_close_cb(void *closure)
288 struct afb_xreq *xreq = closure;
289 afb_hook_xreq_session_close(xreq);
290 xreq_session_close_cb(closure);
293 static int xreq_hooked_session_set_LOA_cb(void *closure, unsigned level)
295 int r = xreq_session_set_LOA_cb(closure, level);
296 struct afb_xreq *xreq = closure;
297 return afb_hook_xreq_session_set_LOA(xreq, level, r);
300 static int xreq_hooked_subscribe_cb(void *closure, struct afb_event event)
302 int r = xreq_subscribe_cb(closure, event);
303 struct afb_xreq *xreq = closure;
304 return afb_hook_xreq_subscribe(xreq, event, r);
307 static int xreq_hooked_unsubscribe_cb(void *closure, struct afb_event event)
309 int r = xreq_unsubscribe_cb(closure, event);
310 struct afb_xreq *xreq = closure;
311 return afb_hook_xreq_unsubscribe(xreq, event, r);
315 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)
317 struct afb_xreq *xreq = closure;
319 afb_xreq_subcall(xreq, api, verb, args, callback, cb_closure);
322 static int xreq_hooked_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
324 struct afb_xreq *xreq = closure;
325 return afb_subcall_sync(xreq, api, verb, args, result);
328 const struct afb_req_itf xreq_itf = {
329 .json = xreq_json_cb,
331 .success = xreq_success_cb,
332 .fail = xreq_fail_cb,
334 .send = xreq_send_cb,
335 .context_get = xreq_context_get_cb,
336 .context_set = xreq_context_set_cb,
337 .addref = xreq_addref_cb,
338 .unref = xreq_unref_cb,
339 .session_close = xreq_session_close_cb,
340 .session_set_LOA = xreq_session_set_LOA_cb,
341 .subscribe = xreq_subscribe_cb,
342 .unsubscribe = xreq_unsubscribe_cb,
343 .subcall = xreq_subcall_cb,
344 .subcallsync = xreq_subcallsync_cb
347 const struct afb_req_itf xreq_hooked_itf = {
348 .json = xreq_hooked_json_cb,
349 .get = xreq_hooked_get_cb,
350 .success = xreq_hooked_success_cb,
351 .fail = xreq_hooked_fail_cb,
352 .raw = xreq_hooked_raw_cb,
353 .send = xreq_hooked_send_cb,
354 .context_get = xreq_hooked_context_get_cb,
355 .context_set = xreq_hooked_context_set_cb,
356 .addref = xreq_hooked_addref_cb,
357 .unref = xreq_unref_cb,
358 .session_close = xreq_hooked_session_close_cb,
359 .session_set_LOA = xreq_hooked_session_set_LOA_cb,
360 .subscribe = xreq_hooked_subscribe_cb,
361 .unsubscribe = xreq_hooked_unsubscribe_cb,
362 .subcall = xreq_subcall_cb,
363 .subcallsync = xreq_subcallsync_cb
366 static inline struct afb_req to_req(struct afb_xreq *xreq)
368 return (struct afb_req){ .itf = xreq->hookflags ? &xreq_hooked_itf : &xreq_itf, .closure = xreq };
371 void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
375 va_start(args, info);
376 if (info == NULL || vasprintf(&message, info, args) < 0)
379 afb_xreq_success(xreq, obj, message);
383 void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...)
387 va_start(args, info);
388 if (info == NULL || vasprintf(&message, info, args) < 0)
391 afb_xreq_fail(xreq, status, message);
395 static int xcheck(struct afb_xreq *xreq, int sessionflags)
397 if ((sessionflags & (AFB_SESSION_CREATE|AFB_SESSION_CLOSE|AFB_SESSION_RENEW|AFB_SESSION_CHECK|AFB_SESSION_LOA_EQ)) != 0) {
398 if (!afb_context_check(&xreq->context)) {
399 afb_context_close(&xreq->context);
400 afb_xreq_fail_f(xreq, "failed", "invalid token's identity");
405 if ((sessionflags & AFB_SESSION_CREATE) != 0) {
406 if (afb_context_check_loa(&xreq->context, 1)) {
407 afb_xreq_fail_f(xreq, "failed", "invalid creation state");
410 afb_context_change_loa(&xreq->context, 1);
411 afb_context_refresh(&xreq->context);
414 if ((sessionflags & (AFB_SESSION_CREATE | AFB_SESSION_RENEW)) != 0)
415 afb_context_refresh(&xreq->context);
417 if ((sessionflags & AFB_SESSION_CLOSE) != 0) {
418 afb_context_change_loa(&xreq->context, 0);
419 afb_context_close(&xreq->context);
422 if ((sessionflags & AFB_SESSION_LOA_GE) != 0) {
423 int loa = (sessionflags >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
424 if (!afb_context_check_loa(&xreq->context, loa)) {
425 afb_xreq_fail_f(xreq, "failed", "invalid LOA");
430 if ((sessionflags & AFB_SESSION_LOA_LE) != 0) {
431 int loa = (sessionflags >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
432 if (afb_context_check_loa(&xreq->context, loa + 1)) {
433 afb_xreq_fail_f(xreq, "failed", "invalid LOA");
440 void afb_xreq_call(struct afb_xreq *xreq, int sessionflags, void (*method)(struct afb_req req))
442 if (xcheck(xreq, sessionflags))
443 method(to_req(xreq));
446 void afb_xreq_begin(struct afb_xreq *xreq)
448 afb_hook_init_xreq(xreq);
450 afb_hook_xreq_begin(xreq);