2 * Copyright (C) 2016, 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.
22 #include "afb-event-itf.h"
25 struct afb_stored_req;
29 * Describes an argument (or parameter) of a request
33 const char *name; /* name of the argument or NULL if invalid */
34 const char *value; /* string representation of the value of the argument */
35 /* original filename of the argument if path != NULL */
36 const char *path; /* if not NULL, path of the received file for the argument */
37 /* when the request is finalized this file is removed */
41 * Interface for handling requests.
42 * It records the functions to be called for the request.
43 * Don't use this structure directly.
44 * Use the helper functions documented below.
48 /* CAUTION: respect the order, add at the end */
50 struct json_object *(*json)(void *closure);
51 struct afb_arg (*get)(void *closure, const char *name);
53 void (*success)(void *closure, struct json_object *obj, const char *info);
54 void (*fail)(void *closure, const char *status, const char *info);
56 void (*vsuccess)(void *closure, struct json_object *obj, const char *fmt, va_list args);
57 void (*vfail)(void *closure, const char *status, const char *fmt, va_list args);
59 void *(*context_get)(void *closure);
60 void (*context_set)(void *closure, void *value, void (*free_value)(void*));
62 void (*addref)(void *closure);
63 void (*unref)(void *closure);
65 void (*session_close)(void *closure);
66 int (*session_set_LOA)(void *closure, unsigned level);
68 int (*subscribe)(void *closure, struct afb_event event);
69 int (*unsubscribe)(void *closure, struct afb_event event);
71 void (*subcall)(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure);
72 int (*subcallsync)(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result);
74 void (*vverbose)(void *closure, int level, const char *file, int line, const char * func, const char *fmt, va_list args);
75 struct afb_stored_req *(*store)(void *closure);
76 void (*subcall_req)(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure);
80 * Describes the request by bindings from afb-daemon
84 const struct afb_req_itf *itf; /* the interface to use */
85 void *closure; /* the closure argument for functions of 'itf' */
89 * Checks wether the request 'req' is valid or not.
91 * Returns 0 if not valid or 1 if valid.
93 static inline int afb_req_is_valid(struct afb_req req)
99 * Gets from the request 'req' the argument of 'name'.
100 * Returns a PLAIN structure of type 'struct afb_arg'.
101 * When the argument of 'name' is not found, all fields of result are set to NULL.
102 * When the argument of 'name' is found, the fields are filled,
103 * in particular, the field 'result.name' is set to 'name'.
105 * There is a special name value: the empty string.
106 * The argument of name "" is defined only if the request was made using
107 * an HTTP POST of Content-Type "application/json". In that case, the
108 * argument of name "" receives the value of the body of the HTTP request.
110 static inline struct afb_arg afb_req_get(struct afb_req req, const char *name)
112 return req.itf->get(req.closure, name);
116 * Gets from the request 'req' the string value of the argument of 'name'.
117 * Returns NULL if when there is no argument of 'name'.
118 * Returns the value of the argument of 'name' otherwise.
120 * Shortcut for: afb_req_get(req, name).value
122 static inline const char *afb_req_value(struct afb_req req, const char *name)
124 return afb_req_get(req, name).value;
128 * Gets from the request 'req' the path for file attached to the argument of 'name'.
129 * Returns NULL if when there is no argument of 'name' or when there is no file.
130 * Returns the path of the argument of 'name' otherwise.
132 * Shortcut for: afb_req_get(req, name).path
134 static inline const char *afb_req_path(struct afb_req req, const char *name)
136 return afb_req_get(req, name).path;
140 * Gets from the request 'req' the json object hashing the arguments.
141 * The returned object must not be released using 'json_object_put'.
143 static inline struct json_object *afb_req_json(struct afb_req req)
145 return req.itf->json(req.closure);
149 * Sends a reply of kind success to the request 'req'.
150 * The status of the reply is automatically set to "success".
151 * Its send the object 'obj' (can be NULL) with an
152 * informationnal comment 'info (can also be NULL).
154 * For convenience, the function calls 'json_object_put' for 'obj'.
155 * Thus, in the case where 'obj' should remain available after
156 * the function returns, the function 'json_object_get' shall be used.
158 static inline void afb_req_success(struct afb_req req, struct json_object *obj, const char *info)
160 req.itf->success(req.closure, obj, info);
164 * Same as 'afb_req_success' but the 'info' is a formatting
165 * string followed by arguments.
167 * For convenience, the function calls 'json_object_put' for 'obj'.
168 * Thus, in the case where 'obj' should remain available after
169 * the function returns, the function 'json_object_get' shall be used.
171 static inline void afb_req_success_f(struct afb_req req, struct json_object *obj, const char *info, ...) __attribute__((format(printf, 3, 4)));
172 static inline void afb_req_success_f(struct afb_req req, struct json_object *obj, const char *info, ...)
175 va_start(args, info);
176 req.itf->vsuccess(req.closure, obj, info, args);
181 * Same as 'afb_req_success_f' but the arguments to the format 'info'
182 * are given as a variable argument list instance.
184 * For convenience, the function calls 'json_object_put' for 'obj'.
185 * Thus, in the case where 'obj' should remain available after
186 * the function returns, the function 'json_object_get' shall be used.
188 static inline void afb_req_success_v(struct afb_req req, struct json_object *obj, const char *info, va_list args)
190 req.itf->vsuccess(req.closure, obj, info, args);
194 * Sends a reply of kind failure to the request 'req'.
195 * The status of the reply is set to 'status' and an
196 * informationnal comment 'info' (can also be NULL) can be added.
198 * Note that calling afb_req_fail("success", info) is equivalent
199 * to call afb_req_success(NULL, info). Thus even if possible it
200 * is strongly recommanded to NEVER use "success" for status.
202 static inline void afb_req_fail(struct afb_req req, const char *status, const char *info)
204 req.itf->fail(req.closure, status, info);
208 * Same as 'afb_req_fail' but the 'info' is a formatting
209 * string followed by arguments.
211 static inline void afb_req_fail_f(struct afb_req req, const char *status, const char *info, ...) __attribute__((format(printf, 3, 4)));
212 static inline void afb_req_fail_f(struct afb_req req, const char *status, const char *info, ...)
215 va_start(args, info);
216 req.itf->vfail(req.closure, status, info, args);
221 * Same as 'afb_req_fail_f' but the arguments to the format 'info'
222 * are given as a variable argument list instance.
224 static inline void afb_req_fail_v(struct afb_req req, const char *status, const char *info, va_list args)
226 req.itf->vfail(req.closure, status, info, args);
230 * Gets the pointer stored by the binding for the session of 'req'.
231 * When the binding has not yet recorded a pointer, NULL is returned.
233 static inline void *afb_req_context_get(struct afb_req req)
235 return req.itf->context_get(req.closure);
239 * Stores for the binding the pointer 'context' to the session of 'req'.
240 * The function 'free_context' will be called when the session is closed
241 * or if binding stores an other pointer.
243 static inline void afb_req_context_set(struct afb_req req, void *context, void (*free_context)(void*))
245 req.itf->context_set(req.closure, context, free_context);
249 * Gets the pointer stored by the binding for the session of 'req'.
250 * If the stored pointer is NULL, indicating that no pointer was
251 * already stored, afb_req_context creates a new context by calling
252 * the function 'create_context' and stores it with the freeing function
255 static inline void *afb_req_context(struct afb_req req, void *(*create_context)(), void (*free_context)(void*))
257 void *result = afb_req_context_get(req);
259 result = create_context();
260 afb_req_context_set(req, result, free_context);
266 * Frees the pointer stored by the binding for the session of 'req'
267 * and sets it to NULL.
269 * Shortcut for: afb_req_context_set(req, NULL, NULL)
271 static inline void afb_req_context_clear(struct afb_req req)
273 afb_req_context_set(req, 0, 0);
277 * Adds one to the count of references of 'req'.
278 * This function MUST be called by asynchronous implementations
279 * of verbs if no reply was sent before returning.
281 static inline void afb_req_addref(struct afb_req req)
283 req.itf->addref(req.closure);
287 * Substracts one to the count of references of 'req'.
288 * This function MUST be called by asynchronous implementations
289 * of verbs after sending the asynchronous reply.
291 static inline void afb_req_unref(struct afb_req req)
293 req.itf->unref(req.closure);
297 * Closes the session associated with 'req'
298 * and delete all associated contexts.
300 static inline void afb_req_session_close(struct afb_req req)
302 req.itf->session_close(req.closure);
306 * Sets the level of assurance of the session of 'req'
307 * to 'level'. The effect of this function is subject of
309 * Returns 1 on success or 0 if failed.
311 static inline int afb_req_session_set_LOA(struct afb_req req, unsigned level)
313 return req.itf->session_set_LOA(req.closure, level);
317 * Establishes for the client link identified by 'req' a subscription
319 * Returns 0 in case of successful subscription or -1 in case of error.
321 static inline int afb_req_subscribe(struct afb_req req, struct afb_event event)
323 return req.itf->subscribe(req.closure, event);
327 * Revokes the subscription established to the 'event' for the client
328 * link identified by 'req'.
329 * Returns 0 in case of successful subscription or -1 in case of error.
331 static inline int afb_req_unsubscribe(struct afb_req req, struct afb_event event)
333 return req.itf->unsubscribe(req.closure, event);
337 * Makes a call to the method of name 'api' / 'verb' with the object 'args'.
338 * This call is made in the context of the request 'req'.
339 * On completion, the function 'callback' is invoked with the
340 * 'closure' given at call and two other parameters: 'iserror' and 'result'.
341 * 'status' is 0 on success or negative when on an error reply.
342 * 'result' is the json object of the reply, you must not call json_object_put
345 * For convenience, the function calls 'json_object_put' for 'args'.
346 * Thus, in the case where 'args' should remain available after
347 * the function returns, the function 'json_object_get' shall be used.
350 * - 'afb_req_subcall_req' that is convenient to keep request alive automatically.
351 * - 'afb_req_subcall_sync' the synchronous version
353 static inline void afb_req_subcall(struct afb_req req, const char *api, const char *verb, struct json_object *args, void (*callback)(void *closure, int iserror, struct json_object *result), void *closure)
355 req.itf->subcall(req.closure, api, verb, args, callback, closure);
359 * Makes a call to the method of name 'api' / 'verb' with the object 'args'.
360 * This call is made in the context of the request 'req'.
361 * On completion, the function 'callback' is invoked with the
362 * original request 'req', the 'closure' given at call and two
363 * other parameters: 'iserror' and 'result'.
364 * 'status' is 0 on success or negative when on an error reply.
365 * 'result' is the json object of the reply, you must not call json_object_put
368 * For convenience, the function calls 'json_object_put' for 'args'.
369 * Thus, in the case where 'args' should remain available after
370 * the function returns, the function 'json_object_get' shall be used.
373 * - 'afb_req_subcall' that doesn't keep request alive automatically.
374 * - 'afb_req_subcall_sync' the synchronous version
376 static inline void afb_req_subcall_req(struct afb_req req, const char *api, const char *verb, struct json_object *args, void (*callback)(void *closure, int iserror, struct json_object *result, struct afb_req req), void *closure)
378 req.itf->subcall_req(req.closure, api, verb, args, callback, closure);
382 * Makes a call to the method of name 'api' / 'verb' with the object 'args'.
383 * This call is made in the context of the request 'req'.
384 * This call is synchronous, it waits untill completion of the request.
385 * It returns 0 on success or a negative value on error answer.
386 * The object pointed by 'result' is filled and must be released by the caller
387 * after its use by calling 'json_object_put'.
389 * For convenience, the function calls 'json_object_put' for 'args'.
390 * Thus, in the case where 'args' should remain available after
391 * the function returns, the function 'json_object_get' shall be used.
394 * - 'afb_req_subcall_req' that is convenient to keep request alive automatically.
395 * - 'afb_req_subcall' that doesn't keep request alive automatically.
397 static inline int afb_req_subcall_sync(struct afb_req req, const char *api, const char *verb, struct json_object *args, struct json_object **result)
399 return req.itf->subcallsync(req.closure, api, verb, args, result);
403 * Send associated to 'req' a message described by 'fmt' and following parameters
404 * to the journal for the verbosity 'level'.
406 * 'file', 'line' and 'func' are indicators of position of the code in source files
407 * (see macros __FILE__, __LINE__ and __func__).
409 * 'level' is defined by syslog standard:
410 * EMERGENCY 0 System is unusable
411 * ALERT 1 Action must be taken immediately
412 * CRITICAL 2 Critical conditions
413 * ERROR 3 Error conditions
414 * WARNING 4 Warning conditions
415 * NOTICE 5 Normal but significant condition
416 * INFO 6 Informational
417 * DEBUG 7 Debug-level messages
419 static inline void afb_req_verbose(struct afb_req req, int level, const char *file, int line, const char * func, const char *fmt, ...) __attribute__((format(printf, 6, 7)));
420 static inline void afb_req_verbose(struct afb_req req, int level, const char *file, int line, const char * func, const char *fmt, ...)
424 req.itf->vverbose(req.closure, level, file, line, func, fmt, args);
428 /* macro for setting file, line and function automatically */
429 # if !defined(AFB_BINDING_PRAGMA_NO_VERBOSE_DETAILS)
430 #define AFB_REQ_VERBOSE(req,level,...) afb_req_verbose(req,level,__FILE__,__LINE__,__func__,__VA_ARGS__)
432 #define AFB_REQ_VERBOSE(req,level,...) afb_req_verbose(req,level,NULL,0,NULL,__VA_ARGS__)