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);
38 static struct afb_arg xreq_get_cb(void *closure, const char *name);
40 static void xreq_success_cb(void *closure, struct json_object *obj, const char *info);
41 static void xreq_fail_cb(void *closure, const char *status, const char *info);
43 static const char *xreq_raw_cb(void *closure, size_t *size);
44 static void xreq_send_cb(void *closure, const char *buffer, size_t size);
46 static void *xreq_context_get_cb(void *closure);
47 static void xreq_context_set_cb(void *closure, void *value, void (*free_value)(void*));
49 static void xreq_addref_cb(void *closure);
50 static void xreq_unref_cb(void *closure);
52 static void xreq_session_close_cb(void *closure);
53 static int xreq_session_set_LOA_cb(void *closure, unsigned level);
55 static int xreq_subscribe_cb(void *closure, struct afb_event event);
56 static int xreq_unsubscribe_cb(void *closure, struct afb_event event);
58 static void xreq_subcall_cb(
62 struct json_object *args,
63 void (*callback)(void*, int, struct json_object*),
66 const struct afb_req_itf xreq_itf = {
69 .success = xreq_success_cb,
73 .context_get = xreq_context_get_cb,
74 .context_set = xreq_context_set_cb,
75 .addref = xreq_addref_cb,
76 .unref = xreq_unref_cb,
77 .session_close = xreq_session_close_cb,
78 .session_set_LOA = xreq_session_set_LOA_cb,
79 .subscribe = xreq_subscribe_cb,
80 .unsubscribe = xreq_unsubscribe_cb,
81 .subcall = xreq_subcall_cb
85 static struct json_object *xreq_json_cb(void *closure)
87 struct afb_xreq *xreq = closure;
88 return xreq->queryitf->json(xreq->query);
91 static struct afb_arg xreq_get_cb(void *closure, const char *name)
93 struct afb_xreq *xreq = closure;
94 if (xreq->queryitf->get)
95 return xreq->queryitf->get(xreq->query, name);
97 return afb_msg_json_get_arg(xreq_json_cb(closure), name);
100 static void xreq_success_cb(void *closure, struct json_object *obj, const char *info)
102 struct afb_xreq *xreq = closure;
104 ERROR("reply called more than one time!!");
105 json_object_put(obj);
108 if (xreq->queryitf->success)
109 xreq->queryitf->success(xreq->query, obj, info);
111 xreq->queryitf->reply(xreq->query, 0, afb_msg_json_reply_ok(info, obj, &xreq->context, NULL));
115 static void xreq_fail_cb(void *closure, const char *status, const char *info)
117 struct afb_xreq *xreq = closure;
119 ERROR("reply called more than one time!!");
122 if (xreq->queryitf->fail)
123 xreq->queryitf->fail(xreq->query, status, info);
125 xreq->queryitf->reply(xreq->query, 1, afb_msg_json_reply_error(status, info, &xreq->context, NULL));
129 static const char *xreq_raw_cb(void *closure, size_t *size)
131 struct afb_xreq *xreq = closure;
132 const char *result = json_object_to_json_string(xreq->queryitf->json(xreq->query));
134 *size = strlen(result);
138 static void xreq_send_cb(void *closure, const char *buffer, size_t size)
140 struct json_object *obj = json_tokener_parse(buffer);
142 xreq_success_cb(closure, obj, "fake send");
144 xreq_fail_cb(closure, "fake-send-failed", "fake send");
147 static void *xreq_context_get_cb(void *closure)
149 struct afb_xreq *xreq = closure;
150 return afb_context_get(&xreq->context);
153 static void xreq_context_set_cb(void *closure, void *value, void (*free_value)(void*))
155 struct afb_xreq *xreq = closure;
156 afb_context_set(&xreq->context, value, free_value);
159 static void xreq_addref_cb(void *closure)
161 struct afb_xreq *xreq = closure;
162 afb_xreq_addref(xreq);
165 void afb_xreq_addref(struct afb_xreq *xreq)
170 static void xreq_unref_cb(void *closure)
172 struct afb_xreq *xreq = closure;
173 afb_xreq_unref(xreq);
176 void afb_xreq_unref(struct afb_xreq *xreq)
178 if (!--xreq->refcount) {
179 xreq->queryitf->unref(xreq->query);
183 static void xreq_session_close_cb(void *closure)
185 struct afb_xreq *xreq = closure;
186 afb_context_close(&xreq->context);
189 static int xreq_session_set_LOA_cb(void *closure, unsigned level)
191 struct afb_xreq *xreq = closure;
192 return afb_context_change_loa(&xreq->context, level);
195 static int xreq_subscribe_cb(void *closure, struct afb_event event)
197 struct afb_xreq *xreq = closure;
199 return afb_evt_add_watch(xreq->listener, event);
200 ERROR("no event listener, subscription impossible");
205 static int xreq_unsubscribe_cb(void *closure, struct afb_event event)
207 struct afb_xreq *xreq = closure;
209 return afb_evt_remove_watch(xreq->listener, event);
210 ERROR("no event listener, unsubscription impossible");
215 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)
217 struct afb_xreq *xreq = closure;
218 if (xreq->queryitf->subcall)
219 xreq->queryitf->subcall(xreq->query, api, verb, args, callback, cb_closure);
221 afb_subcall(&xreq->context, api, verb, args, callback, cb_closure, (struct afb_req){ .itf = &xreq_itf, .closure = xreq });
224 void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
228 va_start(args, info);
229 if (info == NULL || vasprintf(&message, info, args) < 0)
232 xreq_success_cb(xreq, obj, message);
236 void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...)
240 va_start(args, info);
241 if (info == NULL || vasprintf(&message, info, args) < 0)
244 xreq_fail_cb(xreq, status, message);
248 static int xcheck(struct afb_xreq *xreq)
250 int stag = xreq->sessionflags;
252 if ((stag & (AFB_SESSION_CREATE|AFB_SESSION_CLOSE|AFB_SESSION_RENEW|AFB_SESSION_CHECK|AFB_SESSION_LOA_EQ)) != 0) {
253 if (!afb_context_check(&xreq->context)) {
254 afb_context_close(&xreq->context);
255 afb_xreq_fail_f(xreq, "failed", "invalid token's identity");
260 if ((stag & AFB_SESSION_CREATE) != 0) {
261 if (afb_context_check_loa(&xreq->context, 1)) {
262 afb_xreq_fail_f(xreq, "failed", "invalid creation state");
265 afb_context_change_loa(&xreq->context, 1);
266 afb_context_refresh(&xreq->context);
269 if ((stag & (AFB_SESSION_CREATE | AFB_SESSION_RENEW)) != 0)
270 afb_context_refresh(&xreq->context);
272 if ((stag & AFB_SESSION_CLOSE) != 0) {
273 afb_context_change_loa(&xreq->context, 0);
274 afb_context_close(&xreq->context);
277 if ((stag & AFB_SESSION_LOA_GE) != 0) {
278 int loa = (stag >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
279 if (!afb_context_check_loa(&xreq->context, loa)) {
280 afb_xreq_fail_f(xreq, "failed", "invalid LOA");
285 if ((stag & AFB_SESSION_LOA_LE) != 0) {
286 int loa = (stag >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
287 if (afb_context_check_loa(&xreq->context, loa + 1)) {
288 afb_xreq_fail_f(xreq, "failed", "invalid LOA");
295 static void xreq_run_cb(int signum, void *arg)
297 struct afb_xreq *xreq = arg;
300 xreq->callback((struct afb_req){ .itf = &xreq_itf, .closure = xreq });
302 afb_xreq_fail_f(xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
305 afb_xreq_unref(xreq);
308 void afb_xreq_call(struct afb_xreq *xreq)
312 afb_xreq_addref(xreq);
313 rc = jobs_queue(xreq->group, xreq->timeout, xreq_run_cb, xreq);
315 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
316 ERROR("can't process job with threads: %m");
317 afb_xreq_fail_f(xreq, "cancelled", "not able to pipe a job for the task");