2 * Copyright (C) 2016, 2017, 2018 "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.
20 #include "afb-req-itf.h"
21 #include "afb-event.h"
24 * Converts the 'req' to an afb_request.
26 static inline struct afb_request *afb_req_to_request(struct afb_req req)
32 * Checks whether the request 'req' is valid or not.
34 * Returns 0 if not valid or 1 if valid.
36 static inline int afb_req_is_valid(struct afb_req req)
42 * Gets from the request 'req' the argument of 'name'.
43 * Returns a PLAIN structure of type 'struct afb_arg'.
44 * When the argument of 'name' is not found, all fields of result are set to NULL.
45 * When the argument of 'name' is found, the fields are filled,
46 * in particular, the field 'result.name' is set to 'name'.
48 * There is a special name value: the empty string.
49 * The argument of name "" is defined only if the request was made using
50 * an HTTP POST of Content-Type "application/json". In that case, the
51 * argument of name "" receives the value of the body of the HTTP request.
53 static inline struct afb_arg afb_req_get(struct afb_req req, const char *name)
55 return req.itf->get(req.closure, name);
59 * Gets from the request 'req' the string value of the argument of 'name'.
60 * Returns NULL if when there is no argument of 'name'.
61 * Returns the value of the argument of 'name' otherwise.
63 * Shortcut for: afb_req_get(req, name).value
65 static inline const char *afb_req_value(struct afb_req req, const char *name)
67 return afb_req_get(req, name).value;
71 * Gets from the request 'req' the path for file attached to the argument of 'name'.
72 * Returns NULL if when there is no argument of 'name' or when there is no file.
73 * Returns the path of the argument of 'name' otherwise.
75 * Shortcut for: afb_req_get(req, name).path
77 static inline const char *afb_req_path(struct afb_req req, const char *name)
79 return afb_req_get(req, name).path;
83 * Gets from the request 'req' the json object hashing the arguments.
84 * The returned object must not be released using 'json_object_put'.
86 static inline struct json_object *afb_req_json(struct afb_req req)
88 return req.itf->json(req.closure);
92 * Sends a reply of kind success to the request 'req'.
93 * The status of the reply is automatically set to "success".
94 * Its send the object 'obj' (can be NULL) with an
95 * informationnal comment 'info (can also be NULL).
97 * For convenience, the function calls 'json_object_put' for 'obj'.
98 * Thus, in the case where 'obj' should remain available after
99 * the function returns, the function 'json_object_get' shall be used.
101 static inline void afb_req_success(struct afb_req req, struct json_object *obj, const char *info)
103 req.itf->success(req.closure, obj, info);
107 * Same as 'afb_req_success' but the 'info' is a formatting
108 * string followed by arguments.
110 * For convenience, the function calls 'json_object_put' for 'obj'.
111 * Thus, in the case where 'obj' should remain available after
112 * the function returns, the function 'json_object_get' shall be used.
114 static inline void afb_req_success_f(struct afb_req req, struct json_object *obj, const char *info, ...) __attribute__((format(printf, 3, 4)));
115 static inline void afb_req_success_f(struct afb_req req, struct json_object *obj, const char *info, ...)
118 va_start(args, info);
119 req.itf->vsuccess(req.closure, obj, info, args);
124 * Same as 'afb_req_success_f' but the arguments to the format 'info'
125 * are given as a variable argument list instance.
127 * For convenience, the function calls 'json_object_put' for 'obj'.
128 * Thus, in the case where 'obj' should remain available after
129 * the function returns, the function 'json_object_get' shall be used.
131 static inline void afb_req_success_v(struct afb_req req, struct json_object *obj, const char *info, va_list args)
133 req.itf->vsuccess(req.closure, obj, info, args);
137 * Sends a reply of kind failure to the request 'req'.
138 * The status of the reply is set to 'status' and an
139 * informationnal comment 'info' (can also be NULL) can be added.
141 * Note that calling afb_req_fail("success", info) is equivalent
142 * to call afb_req_success(NULL, info). Thus even if possible it
143 * is strongly recommanded to NEVER use "success" for status.
145 static inline void afb_req_fail(struct afb_req req, const char *status, const char *info)
147 req.itf->fail(req.closure, status, info);
151 * Same as 'afb_req_fail' but the 'info' is a formatting
152 * string followed by arguments.
154 static inline void afb_req_fail_f(struct afb_req req, const char *status, const char *info, ...) __attribute__((format(printf, 3, 4)));
155 static inline void afb_req_fail_f(struct afb_req req, const char *status, const char *info, ...)
158 va_start(args, info);
159 req.itf->vfail(req.closure, status, info, args);
164 * Same as 'afb_req_fail_f' but the arguments to the format 'info'
165 * are given as a variable argument list instance.
167 static inline void afb_req_fail_v(struct afb_req req, const char *status, const char *info, va_list args)
169 req.itf->vfail(req.closure, status, info, args);
173 * Gets the pointer stored by the binding for the session of 'req'.
174 * When the binding has not yet recorded a pointer, NULL is returned.
176 static inline void *afb_req_context_get(struct afb_req req)
178 return req.itf->context_get(req.closure);
182 * Stores for the binding the pointer 'context' to the session of 'req'.
183 * The function 'free_context' will be called when the session is closed
184 * or if binding stores an other pointer.
186 static inline void afb_req_context_set(struct afb_req req, void *context, void (*free_context)(void*))
188 req.itf->context_set(req.closure, context, free_context);
192 * Gets the pointer stored by the binding for the session of 'req'.
193 * If the stored pointer is NULL, indicating that no pointer was
194 * already stored, afb_req_context creates a new context by calling
195 * the function 'create_context' and stores it with the freeing function
198 static inline void *afb_req_context(struct afb_req req, void *(*create_context)(), void (*free_context)(void*))
200 return req.itf->context_make(req.closure, 0, (void *(*)(void*))(void*)create_context, free_context, 0);
204 * Gets the pointer stored by the binding for the session of 'request'.
205 * If no previous pointer is stored or if 'replace' is not zero, a new value
206 * is generated using the function 'create_context' called with the 'closure'.
207 * If 'create_context' is NULL the generated value is 'closure'.
208 * When a value is created, the function 'free_context' is recorded and will
209 * be called (with the created value as argument) to free the created value when
210 * it is not more used.
211 * This function is atomic: it ensures that 2 threads will not race together.
213 static inline void *afb_req_context_make(struct afb_req req, int replace, void *(*create_context)(void *closure), void (*free_context)(void*), void *closure)
215 return req.itf->context_make(req.closure, replace, create_context, free_context, closure);
219 * Frees the pointer stored by the binding for the session of 'req'
220 * and sets it to NULL.
222 * Shortcut for: afb_req_context_set(req, NULL, NULL)
224 static inline void afb_req_context_clear(struct afb_req req)
226 afb_req_context_set(req, 0, 0);
230 * Adds one to the count of references of 'req'.
231 * This function MUST be called by asynchronous implementations
232 * of verbs if no reply was sent before returning.
234 static inline void afb_req_addref(struct afb_req req)
236 req.itf->addref(req.closure);
240 * Substracts one to the count of references of 'req'.
241 * This function MUST be called by asynchronous implementations
242 * of verbs after sending the asynchronous reply.
244 static inline void afb_req_unref(struct afb_req req)
246 req.itf->unref(req.closure);
250 * Closes the session associated with 'req'
251 * and delete all associated contexts.
253 static inline void afb_req_session_close(struct afb_req req)
255 req.itf->session_close(req.closure);
259 * Sets the level of assurance of the session of 'req'
260 * to 'level'. The effect of this function is subject of
262 * Returns 1 on success or 0 if failed.
264 static inline int afb_req_session_set_LOA(struct afb_req req, unsigned level)
266 return req.itf->session_set_LOA(req.closure, level);
270 * Establishes for the client link identified by 'req' a subscription
272 * Returns 0 in case of successful subscription or -1 in case of error.
274 static inline int afb_req_subscribe(struct afb_req req, struct afb_event event)
276 return req.itf->subscribe(req.closure, event);
280 * Revokes the subscription established to the 'event' for the client
281 * link identified by 'req'.
282 * Returns 0 in case of successful subscription or -1 in case of error.
284 static inline int afb_req_unsubscribe(struct afb_req req, struct afb_event event)
286 return req.itf->unsubscribe(req.closure, event);
290 * Makes a call to the method of name 'api' / 'verb' with the object 'args'.
291 * This call is made in the context of the request 'req'.
292 * On completion, the function 'callback' is invoked with the
293 * 'closure' given at call and two other parameters: 'iserror' and 'result'.
294 * 'status' is 0 on success or negative when on an error reply.
295 * 'result' is the json object of the reply, you must not call json_object_put
298 * For convenience, the function calls 'json_object_put' for 'args'.
299 * Thus, in the case where 'args' should remain available after
300 * the function returns, the function 'json_object_get' shall be used.
303 * - 'afb_req_subcall_req' that is convenient to keep request alive automatically.
304 * - 'afb_req_subcall_sync' the synchronous version
306 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)
308 req.itf->subcall(req.closure, api, verb, args, callback, closure);
312 * Makes a call to the method of name 'api' / 'verb' with the object 'args'.
313 * This call is made in the context of the request 'req'.
314 * On completion, the function 'callback' is invoked with the
315 * original request 'req', the 'closure' given at call and two
316 * other parameters: 'iserror' and 'result'.
317 * 'status' is 0 on success or negative when on an error reply.
318 * 'result' is the json object of the reply, you must not call json_object_put
321 * For convenience, the function calls 'json_object_put' for 'args'.
322 * Thus, in the case where 'args' should remain available after
323 * the function returns, the function 'json_object_get' shall be used.
326 * - 'afb_req_subcall' that doesn't keep request alive automatically.
327 * - 'afb_req_subcall_sync' the synchronous version
329 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)
331 req.itf->subcall_req(req.closure, api, verb, args, callback, closure);
335 * Makes a call to the method of name 'api' / 'verb' with the object 'args'.
336 * This call is made in the context of the request 'req'.
337 * This call is synchronous, it waits untill completion of the request.
338 * It returns 0 on success or a negative value on error answer.
339 * The object pointed by 'result' is filled and must be released by the caller
340 * after its use by calling 'json_object_put'.
342 * For convenience, the function calls 'json_object_put' for 'args'.
343 * Thus, in the case where 'args' should remain available after
344 * the function returns, the function 'json_object_get' shall be used.
347 * - 'afb_req_subcall_req' that is convenient to keep request alive automatically.
348 * - 'afb_req_subcall' that doesn't keep request alive automatically.
350 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)
352 return req.itf->subcallsync(req.closure, api, verb, args, result);
356 * Send associated to 'req' a message described by 'fmt' and following parameters
357 * to the journal for the verbosity 'level'.
359 * 'file', 'line' and 'func' are indicators of position of the code in source files
360 * (see macros __FILE__, __LINE__ and __func__).
362 * 'level' is defined by syslog standard:
363 * EMERGENCY 0 System is unusable
364 * ALERT 1 Action must be taken immediately
365 * CRITICAL 2 Critical conditions
366 * ERROR 3 Error conditions
367 * WARNING 4 Warning conditions
368 * NOTICE 5 Normal but significant condition
369 * INFO 6 Informational
370 * DEBUG 7 Debug-level messages
372 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)));
373 static inline void afb_req_verbose(struct afb_req req, int level, const char *file, int line, const char * func, const char *fmt, ...)
377 req.itf->vverbose(req.closure, level, file, line, func, fmt, args);
381 /* macro for setting file, line and function automatically */
382 # if !defined(AFB_BINDING_PRAGMA_NO_VERBOSE_DETAILS)
383 #define AFB_REQ_VERBOSE(req,level,...) afb_req_verbose(req,level,__FILE__,__LINE__,__func__,__VA_ARGS__)
385 #define AFB_REQ_VERBOSE(req,level,...) afb_req_verbose(req,level,NULL,0,NULL,__VA_ARGS__)
389 * Check whether the 'permission' is granted or not to the client
390 * identified by 'req'.
392 * Returns 1 if the permission is granted or 0 otherwise.
394 static inline int afb_req_has_permission(struct afb_req req, const char *permission)
396 return req.itf->has_permission(req.closure, permission);
400 * Get the application identifier of the client application for the
403 * Returns the application identifier or NULL when the application
404 * can not be identified.
406 * The returned value if not NULL must be freed by the caller
408 static inline char *afb_req_get_application_id(struct afb_req req)
410 return req.itf->get_application_id(req.closure);
414 * Get the user identifier (UID) of the client application for the
417 * Returns -1 when the application can not be identified.
419 static inline int afb_req_get_uid(struct afb_req req)
421 return req.itf->get_uid(req.closure);