acfb7ea1286df09ee2629619f424d0494945fc35
[src/app-framework-binder.git] / include / afb / afb-request.h
1 /*
2  * Copyright (C) 2016, 2017 "IoT.bzh"
3  * Author: José Bollo <jose.bollo@iot.bzh>
4  *
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
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #pragma once
19
20 #include "afb-request-itf.h"
21
22 typedef struct afb_request afb_request;
23
24 /*
25  * Gets from the request 'request' the argument of 'name'.
26  * Returns a PLAIN structure of type 'struct afb_arg'.
27  * When the argument of 'name' is not found, all fields of result are set to NULL.
28  * When the argument of 'name' is found, the fields are filled,
29  * in particular, the field 'result.name' is set to 'name'.
30  *
31  * There is a special name value: the empty string.
32  * The argument of name "" is defined only if the request was made using
33  * an HTTP POST of Content-Type "application/json". In that case, the
34  * argument of name "" receives the value of the body of the HTTP request.
35  */
36 static inline struct afb_arg afb_request_get(struct afb_request *request, const char *name)
37 {
38         return request->itf->get(request, name);
39 }
40
41 /*
42  * Gets from the request 'request' the string value of the argument of 'name'.
43  * Returns NULL if when there is no argument of 'name'.
44  * Returns the value of the argument of 'name' otherwise.
45  *
46  * Shortcut for: afb_request_get(request, name).value
47  */
48 static inline const char *afb_request_value(struct afb_request *request, const char *name)
49 {
50         return afb_request_get(request, name).value;
51 }
52
53 /*
54  * Gets from the request 'request' the path for file attached to the argument of 'name'.
55  * Returns NULL if when there is no argument of 'name' or when there is no file.
56  * Returns the path of the argument of 'name' otherwise.
57  *
58  * Shortcut for: afb_request_get(request, name).path
59  */
60 static inline const char *afb_request_path(struct afb_request *request, const char *name)
61 {
62         return afb_request_get(request, name).path;
63 }
64
65 /*
66  * Gets from the request 'request' the json object hashing the arguments.
67  * The returned object must not be released using 'json_object_put'.
68  */
69 static inline struct json_object *afb_request_json(struct afb_request *request)
70 {
71         return request->itf->json(request);
72 }
73
74 /*
75  * Sends a reply of kind success to the request 'request'.
76  * The status of the reply is automatically set to "success".
77  * Its send the object 'obj' (can be NULL) with an
78  * informationnal comment 'info (can also be NULL).
79  *
80  * For convenience, the function calls 'json_object_put' for 'obj'.
81  * Thus, in the case where 'obj' should remain available after
82  * the function returns, the function 'json_object_get' shall be used.
83  */
84 static inline void afb_request_success(struct afb_request *request, struct json_object *obj, const char *info)
85 {
86         request->itf->success(request, obj, info);
87 }
88
89 /*
90  * Same as 'afb_request_success' but the 'info' is a formatting
91  * string followed by arguments.
92  *
93  * For convenience, the function calls 'json_object_put' for 'obj'.
94  * Thus, in the case where 'obj' should remain available after
95  * the function returns, the function 'json_object_get' shall be used.
96  */
97 static inline void afb_request_success_f(struct afb_request *request, struct json_object *obj, const char *info, ...) __attribute__((format(printf, 3, 4)));
98 static inline void afb_request_success_f(struct afb_request *request, struct json_object *obj, const char *info, ...)
99 {
100         va_list args;
101         va_start(args, info);
102         request->itf->vsuccess(request, obj, info, args);
103         va_end(args);
104 }
105
106 /*
107  * Same as 'afb_request_success_f' but the arguments to the format 'info'
108  * are given as a variable argument list instance.
109  *
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.
113  */
114 static inline void afb_request_success_v(struct afb_request *request, struct json_object *obj, const char *info, va_list args)
115 {
116         request->itf->vsuccess(request, obj, info, args);
117 }
118
119 /*
120  * Sends a reply of kind failure to the request 'request'.
121  * The status of the reply is set to 'status' and an
122  * informationnal comment 'info' (can also be NULL) can be added.
123  *
124  * Note that calling afb_request_fail("success", info) is equivalent
125  * to call afb_request_success(NULL, info). Thus even if possible it
126  * is strongly recommanded to NEVER use "success" for status.
127  */
128 static inline void afb_request_fail(struct afb_request *request, const char *status, const char *info)
129 {
130         request->itf->fail(request, status, info);
131 }
132
133 /*
134  * Same as 'afb_request_fail' but the 'info' is a formatting
135  * string followed by arguments.
136  */
137 static inline void afb_request_fail_f(struct afb_request *request, const char *status, const char *info, ...) __attribute__((format(printf, 3, 4)));
138 static inline void afb_request_fail_f(struct afb_request *request, const char *status, const char *info, ...)
139 {
140         va_list args;
141         va_start(args, info);
142         request->itf->vfail(request, status, info, args);
143         va_end(args);
144 }
145
146 /*
147  * Same as 'afb_request_fail_f' but the arguments to the format 'info'
148  * are given as a variable argument list instance.
149  */
150 static inline void afb_request_fail_v(struct afb_request *request, const char *status, const char *info, va_list args)
151 {
152         request->itf->vfail(request, status, info, args);
153 }
154
155 /*
156  * Gets the pointer stored by the binding for the session of 'request'.
157  * When the binding has not yet recorded a pointer, NULL is returned.
158  */
159 static inline void *afb_request_context_get(struct afb_request *request)
160 {
161         return request->itf->context_make(request, 0, 0, 0, 0);
162 }
163
164 /*
165  * Stores for the binding the pointer 'context' to the session of 'request'.
166  * The function 'free_context' will be called when the session is closed
167  * or if binding stores an other pointer.
168  */
169 static inline void afb_request_context_set(struct afb_request *request, void *context, void (*free_context)(void*))
170 {
171         request->itf->context_make(request, 1, 0, free_context, context);
172 }
173
174 /*
175  * Gets the pointer stored by the binding for the session of 'request'.
176  * If no previous pointer is stored or if 'replace' is not zero, a new value
177  * is generated using the function 'create_context' called with the 'closure'.
178  * If 'create_context' is NULL the generated value is 'closure'.
179  * When a value is created, the function 'free_context' is recorded and will
180  * be called (with the created value as argument) to free the created value when
181  * it is not more used.
182  * This function is atomic: it ensures that 2 threads will not race together.
183  */
184 static inline void *afb_request_context(struct afb_request *request, int replace, void *(*create_context)(void *closure), void (*free_context)(void*), void *closure)
185 {
186         return request->itf->context_make(request, replace, create_context, free_context, closure);
187 }
188
189 /*
190  * Frees the pointer stored by the binding for the session of 'request'
191  * and sets it to NULL.
192  *
193  * Shortcut for: afb_request_context_set(request, NULL, NULL)
194  */
195 static inline void afb_request_context_clear(struct afb_request *request)
196 {
197         request->itf->context_make(request, 1, 0, 0, 0);
198 }
199
200 /*
201  * Adds one to the count of references of 'request'.
202  * This function MUST be called by asynchronous implementations
203  * of verbs if no reply was sent before returning.
204  */
205 static inline struct afb_request *afb_request_addref(struct afb_request *request)
206 {
207         return request->itf->addref(request);
208 }
209
210 /*
211  * Substracts one to the count of references of 'request'.
212  * This function MUST be called by asynchronous implementations
213  * of verbs after sending the asynchronous reply.
214  */
215 static inline void afb_request_unref(struct afb_request *request)
216 {
217         request->itf->unref(request);
218 }
219
220 /*
221  * Closes the session associated with 'request'
222  * and delete all associated contexts.
223  */
224 static inline void afb_request_session_close(struct afb_request *request)
225 {
226         request->itf->session_close(request);
227 }
228
229 /*
230  * Sets the level of assurance of the session of 'request'
231  * to 'level'. The effect of this function is subject of
232  * security policies.
233  * Returns 1 on success or 0 if failed.
234  */
235 static inline int afb_request_session_set_LOA(struct afb_request *request, unsigned level)
236 {
237         return request->itf->session_set_LOA(request, level);
238 }
239
240 /*
241  * Establishes for the client link identified by 'request' a subscription
242  * to the 'event'.
243  * Returns 0 in case of successful subscription or -1 in case of error.
244  */
245 static inline int afb_request_subscribe(struct afb_request *request, struct afb_eventid *eventid)
246 {
247         return request->itf->subscribe_eventid(request, eventid);
248 }
249
250 /*
251  * Revokes the subscription established to the 'event' for the client
252  * link identified by 'request'.
253  * Returns 0 in case of successful subscription or -1 in case of error.
254  */
255 static inline int afb_request_unsubscribe(struct afb_request *request, struct afb_eventid *eventid)
256 {
257         return request->itf->unsubscribe_eventid(request, eventid);
258 }
259
260 /*
261  * Makes a call to the method of name 'api' / 'verb' with the object 'args'.
262  * This call is made in the context of the request 'request'.
263  * On completion, the function 'callback' is invoked with the
264  * 'closure' given at call and two other parameters: 'iserror' and 'result'.
265  * 'status' is 0 on success or negative when on an error reply.
266  * 'result' is the json object of the reply, you must not call json_object_put
267  * on the result.
268  *
269  * For convenience, the function calls 'json_object_put' for 'args'.
270  * Thus, in the case where 'args' should remain available after
271  * the function returns, the function 'json_object_get' shall be used.
272  *
273  * See also:
274  *  - 'afb_request_subcall_req' that is convenient to keep request alive automatically.
275  *  - 'afb_request_subcall_sync' the synchronous version
276  */
277 static inline void afb_request_subcall(struct afb_request *request, const char *api, const char *verb, struct json_object *args, void (*callback)(void *closure, int iserror, struct json_object *result), void *closure)
278 {
279         request->itf->subcall(request, api, verb, args, callback, closure);
280 }
281
282 /*
283  * Makes a call to the method of name 'api' / 'verb' with the object 'args'.
284  * This call is made in the context of the request 'request'.
285  * This call is synchronous, it waits untill completion of the request.
286  * It returns 0 on success or a negative value on error answer.
287  * The object pointed by 'result' is filled and must be released by the caller
288  * after its use by calling 'json_object_put'.
289  *
290  * For convenience, the function calls 'json_object_put' for 'args'.
291  * Thus, in the case where 'args' should remain available after
292  * the function returns, the function 'json_object_get' shall be used.
293  *
294  * See also:
295  *  - 'afb_request_subcall_req' that is convenient to keep request alive automatically.
296  *  - 'afb_request_subcall' that doesn't keep request alive automatically.
297  */
298 static inline int afb_request_subcall_sync(struct afb_request *request, const char *api, const char *verb, struct json_object *args, struct json_object **result)
299 {
300         return request->itf->subcallsync(request, api, verb, args, result);
301 }
302
303 /*
304  * Send associated to 'request' a message described by 'fmt' and following parameters
305  * to the journal for the verbosity 'level'.
306  *
307  * 'file', 'line' and 'func' are indicators of position of the code in source files
308  * (see macros __FILE__, __LINE__ and __func__).
309  *
310  * 'level' is defined by syslog standard:
311  *      EMERGENCY         0        System is unusable
312  *      ALERT             1        Action must be taken immediately
313  *      CRITICAL          2        Critical conditions
314  *      ERROR             3        Error conditions
315  *      WARNING           4        Warning conditions
316  *      NOTICE            5        Normal but significant condition
317  *      INFO              6        Informational
318  *      DEBUG             7        Debug-level messages
319  */
320 static inline void afb_request_verbose(struct afb_request *request, int level, const char *file, int line, const char * func, const char *fmt, ...) __attribute__((format(printf, 6, 7)));
321 static inline void afb_request_verbose(struct afb_request *request, int level, const char *file, int line, const char * func, const char *fmt, ...)
322 {
323         va_list args;
324         va_start(args, fmt);
325         request->itf->vverbose(request, level, file, line, func, fmt, args);
326         va_end(args);
327 }
328
329 /* macro for setting file, line and function automatically */
330 # if !defined(AFB_BINDING_PRAGMA_NO_VERBOSE_DETAILS)
331 #define AFB_REQUEST_VERBOSE(request,level,...) afb_request_verbose(request,level,__FILE__,__LINE__,__func__,__VA_ARGS__)
332 #else
333 #define AFB_REQUEST_VERBOSE(request,level,...) afb_request_verbose(request,level,NULL,0,NULL,__VA_ARGS__)
334 #endif
335
336 /*
337  * Check whether the 'permission' is granted or not to the client
338  * identified by 'request'.
339  *
340  * Returns 1 if the permission is granted or 0 otherwise.
341  */
342 static inline int afb_request_has_permission(struct afb_request *request, const char *permission)
343 {
344         return request->itf->has_permission(request, permission);
345 }
346
347 /*
348  * Get the application identifier of the client application for the
349  * request 'request'.
350  *
351  * Returns the application identifier or NULL when the application
352  * can not be identified.
353  *
354  * The returned value if not NULL must be freed by the caller
355  */
356 static inline char *afb_request_get_application_id(struct afb_request *request)
357 {
358         return request->itf->get_application_id(request);
359 }
360