6784a3b14b058d22669ae5b42257226265166a5a
[src/app-framework-binder.git] / include / afb / afb-req-common.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 <stdarg.h>
21
22 #include "afb-event.h"
23
24 struct json_object;
25 struct afb_stored_req;
26 struct afb_req;
27
28 /*
29  * Describes an argument (or parameter) of a request
30  */
31 struct afb_arg
32 {
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 */
38 };
39
40 /*
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.
45  */
46 struct afb_req_itf
47 {
48         /* CAUTION: respect the order, add at the end */
49
50         struct json_object *(*json)(void *closure);
51         struct afb_arg (*get)(void *closure, const char *name);
52
53         void (*success)(void *closure, struct json_object *obj, const char *info);
54         void (*fail)(void *closure, const char *status, const char *info);
55
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);
58
59         void *(*context_get)(void *closure);
60         void (*context_set)(void *closure, void *value, void (*free_value)(void*));
61
62         void (*addref)(void *closure);
63         void (*unref)(void *closure);
64
65         void (*session_close)(void *closure);
66         int (*session_set_LOA)(void *closure, unsigned level);
67
68         int (*subscribe)(void *closure, struct afb_event event);
69         int (*unsubscribe)(void *closure, struct afb_event event);
70
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);
73
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);
77
78         int (*has_permission)(void *closure, const char *permission);
79         char *(*get_application_id)(void *closure);
80
81         void *(*context_make)(void *closure, int replace, void *(*create_value)(void *creation_closure), void (*free_value)(void*), void *creation_closure);
82 };
83
84 /*
85  * Describes the request by bindings from afb-daemon
86  */
87 struct afb_req
88 {
89         const struct afb_req_itf *itf;  /* the interface to use */
90         void *closure;                  /* the closure argument for functions of 'itf' */
91 };
92
93 /*
94  * Checks whether the request 'req' is valid or not.
95  *
96  * Returns 0 if not valid or 1 if valid.
97  */
98 static inline int afb_req_is_valid(struct afb_req req)
99 {
100         return !!req.itf;
101 }
102
103 /*
104  * Gets from the request 'req' the argument of 'name'.
105  * Returns a PLAIN structure of type 'struct afb_arg'.
106  * When the argument of 'name' is not found, all fields of result are set to NULL.
107  * When the argument of 'name' is found, the fields are filled,
108  * in particular, the field 'result.name' is set to 'name'.
109  *
110  * There is a special name value: the empty string.
111  * The argument of name "" is defined only if the request was made using
112  * an HTTP POST of Content-Type "application/json". In that case, the
113  * argument of name "" receives the value of the body of the HTTP request.
114  */
115 static inline struct afb_arg afb_req_get(struct afb_req req, const char *name)
116 {
117         return req.itf->get(req.closure, name);
118 }
119
120 /*
121  * Gets from the request 'req' the string value of the argument of 'name'.
122  * Returns NULL if when there is no argument of 'name'.
123  * Returns the value of the argument of 'name' otherwise.
124  *
125  * Shortcut for: afb_req_get(req, name).value
126  */
127 static inline const char *afb_req_value(struct afb_req req, const char *name)
128 {
129         return afb_req_get(req, name).value;
130 }
131
132 /*
133  * Gets from the request 'req' the path for file attached to the argument of 'name'.
134  * Returns NULL if when there is no argument of 'name' or when there is no file.
135  * Returns the path of the argument of 'name' otherwise.
136  *
137  * Shortcut for: afb_req_get(req, name).path
138  */
139 static inline const char *afb_req_path(struct afb_req req, const char *name)
140 {
141         return afb_req_get(req, name).path;
142 }
143
144 /*
145  * Gets from the request 'req' the json object hashing the arguments.
146  * The returned object must not be released using 'json_object_put'.
147  */
148 static inline struct json_object *afb_req_json(struct afb_req req)
149 {
150         return req.itf->json(req.closure);
151 }
152
153 /*
154  * Sends a reply of kind success to the request 'req'.
155  * The status of the reply is automatically set to "success".
156  * Its send the object 'obj' (can be NULL) with an
157  * informationnal comment 'info (can also be NULL).
158  *
159  * For convenience, the function calls 'json_object_put' for 'obj'.
160  * Thus, in the case where 'obj' should remain available after
161  * the function returns, the function 'json_object_get' shall be used.
162  */
163 static inline void afb_req_success(struct afb_req req, struct json_object *obj, const char *info)
164 {
165         req.itf->success(req.closure, obj, info);
166 }
167
168 /*
169  * Same as 'afb_req_success' but the 'info' is a formatting
170  * string followed by arguments.
171  *
172  * For convenience, the function calls 'json_object_put' for 'obj'.
173  * Thus, in the case where 'obj' should remain available after
174  * the function returns, the function 'json_object_get' shall be used.
175  */
176 static inline void afb_req_success_f(struct afb_req req, struct json_object *obj, const char *info, ...) __attribute__((format(printf, 3, 4)));
177 static inline void afb_req_success_f(struct afb_req req, struct json_object *obj, const char *info, ...)
178 {
179         va_list args;
180         va_start(args, info);
181         req.itf->vsuccess(req.closure, obj, info, args);
182         va_end(args);
183 }
184
185 /*
186  * Same as 'afb_req_success_f' but the arguments to the format 'info'
187  * are given as a variable argument list instance.
188  *
189  * For convenience, the function calls 'json_object_put' for 'obj'.
190  * Thus, in the case where 'obj' should remain available after
191  * the function returns, the function 'json_object_get' shall be used.
192  */
193 static inline void afb_req_success_v(struct afb_req req, struct json_object *obj, const char *info, va_list args)
194 {
195         req.itf->vsuccess(req.closure, obj, info, args);
196 }
197
198 /*
199  * Sends a reply of kind failure to the request 'req'.
200  * The status of the reply is set to 'status' and an
201  * informationnal comment 'info' (can also be NULL) can be added.
202  *
203  * Note that calling afb_req_fail("success", info) is equivalent
204  * to call afb_req_success(NULL, info). Thus even if possible it
205  * is strongly recommanded to NEVER use "success" for status.
206  */
207 static inline void afb_req_fail(struct afb_req req, const char *status, const char *info)
208 {
209         req.itf->fail(req.closure, status, info);
210 }
211
212 /*
213  * Same as 'afb_req_fail' but the 'info' is a formatting
214  * string followed by arguments.
215  */
216 static inline void afb_req_fail_f(struct afb_req req, const char *status, const char *info, ...) __attribute__((format(printf, 3, 4)));
217 static inline void afb_req_fail_f(struct afb_req req, const char *status, const char *info, ...)
218 {
219         va_list args;
220         va_start(args, info);
221         req.itf->vfail(req.closure, status, info, args);
222         va_end(args);
223 }
224
225 /*
226  * Same as 'afb_req_fail_f' but the arguments to the format 'info'
227  * are given as a variable argument list instance.
228  */
229 static inline void afb_req_fail_v(struct afb_req req, const char *status, const char *info, va_list args)
230 {
231         req.itf->vfail(req.closure, status, info, args);
232 }
233
234 /*
235  * Gets the pointer stored by the binding for the session of 'req'.
236  * When the binding has not yet recorded a pointer, NULL is returned.
237  */
238 static inline void *afb_req_context_get(struct afb_req req)
239 {
240         return req.itf->context_get(req.closure);
241 }
242
243 /*
244  * Stores for the binding the pointer 'context' to the session of 'req'.
245  * The function 'free_context' will be called when the session is closed
246  * or if binding stores an other pointer.
247  */
248 static inline void afb_req_context_set(struct afb_req req, void *context, void (*free_context)(void*))
249 {
250         req.itf->context_set(req.closure, context, free_context);
251 }
252
253 /*
254  * Gets the pointer stored by the binding for the session of 'req'.
255  * If the stored pointer is NULL, indicating that no pointer was
256  * already stored, afb_req_context creates a new context by calling
257  * the function 'create_context' and stores it with the freeing function
258  * 'free_context'.
259  */
260 static inline void *afb_req_context(struct afb_req req, void *(*create_context)(), void (*free_context)(void*))
261 {
262         return req.itf->context_make(req.closure, 0, (void *(*)(void*))(void*)create_context, free_context, 0);
263 }
264
265 /*
266  * Gets the pointer stored by the binding for the session of 'request'.
267  * If no previous pointer is stored or if 'replace' is not zero, a new value
268  * is generated using the function 'create_context' called with the 'closure'.
269  * If 'create_context' is NULL the generated value is 'closure'.
270  * When a value is created, the function 'free_context' is recorded and will
271  * be called (with the created value as argument) to free the created value when
272  * it is not more used.
273  * This function is atomic: it ensures that 2 threads will not race together.
274  */
275 static inline void *afb_req_context_make(struct afb_req req, int replace, void *(*create_context)(void *closure), void (*free_context)(void*), void *closure)
276 {
277         return req.itf->context_make(req.closure, replace, create_context, free_context, closure);
278 }
279
280 /*
281  * Frees the pointer stored by the binding for the session of 'req'
282  * and sets it to NULL.
283  *
284  * Shortcut for: afb_req_context_set(req, NULL, NULL)
285  */
286 static inline void afb_req_context_clear(struct afb_req req)
287 {
288         afb_req_context_set(req, 0, 0);
289 }
290
291 /*
292  * Adds one to the count of references of 'req'.
293  * This function MUST be called by asynchronous implementations
294  * of verbs if no reply was sent before returning.
295  */
296 static inline void afb_req_addref(struct afb_req req)
297 {
298         req.itf->addref(req.closure);
299 }
300
301 /*
302  * Substracts one to the count of references of 'req'.
303  * This function MUST be called by asynchronous implementations
304  * of verbs after sending the asynchronous reply.
305  */
306 static inline void afb_req_unref(struct afb_req req)
307 {
308         req.itf->unref(req.closure);
309 }
310
311 /*
312  * Closes the session associated with 'req'
313  * and delete all associated contexts.
314  */
315 static inline void afb_req_session_close(struct afb_req req)
316 {
317         req.itf->session_close(req.closure);
318 }
319
320 /*
321  * Sets the level of assurance of the session of 'req'
322  * to 'level'. The effect of this function is subject of
323  * security policies.
324  * Returns 1 on success or 0 if failed.
325  */
326 static inline int afb_req_session_set_LOA(struct afb_req req, unsigned level)
327 {
328         return req.itf->session_set_LOA(req.closure, level);
329 }
330
331 /*
332  * Establishes for the client link identified by 'req' a subscription
333  * to the 'event'.
334  * Returns 0 in case of successful subscription or -1 in case of error.
335  */
336 static inline int afb_req_subscribe(struct afb_req req, struct afb_event event)
337 {
338         return req.itf->subscribe(req.closure, event);
339 }
340
341 /*
342  * Revokes the subscription established to the 'event' for the client
343  * link identified by 'req'.
344  * Returns 0 in case of successful subscription or -1 in case of error.
345  */
346 static inline int afb_req_unsubscribe(struct afb_req req, struct afb_event event)
347 {
348         return req.itf->unsubscribe(req.closure, event);
349 }
350
351 /*
352  * Makes a call to the method of name 'api' / 'verb' with the object 'args'.
353  * This call is made in the context of the request 'req'.
354  * On completion, the function 'callback' is invoked with the
355  * 'closure' given at call and two other parameters: 'iserror' and 'result'.
356  * 'status' is 0 on success or negative when on an error reply.
357  * 'result' is the json object of the reply, you must not call json_object_put
358  * on the result.
359  *
360  * For convenience, the function calls 'json_object_put' for 'args'.
361  * Thus, in the case where 'args' should remain available after
362  * the function returns, the function 'json_object_get' shall be used.
363  *
364  * See also:
365  *  - 'afb_req_subcall_req' that is convenient to keep request alive automatically.
366  *  - 'afb_req_subcall_sync' the synchronous version
367  */
368 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)
369 {
370         req.itf->subcall(req.closure, api, verb, args, callback, closure);
371 }
372
373 /*
374  * Makes a call to the method of name 'api' / 'verb' with the object 'args'.
375  * This call is made in the context of the request 'req'.
376  * On completion, the function 'callback' is invoked with the
377  * original request 'req', the 'closure' given at call and two
378  * other parameters: 'iserror' and 'result'.
379  * 'status' is 0 on success or negative when on an error reply.
380  * 'result' is the json object of the reply, you must not call json_object_put
381  * on the result.
382  *
383  * For convenience, the function calls 'json_object_put' for 'args'.
384  * Thus, in the case where 'args' should remain available after
385  * the function returns, the function 'json_object_get' shall be used.
386  *
387  * See also:
388  *  - 'afb_req_subcall' that doesn't keep request alive automatically.
389  *  - 'afb_req_subcall_sync' the synchronous version
390  */
391 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)
392 {
393         req.itf->subcall_req(req.closure, api, verb, args, callback, closure);
394 }
395
396 /*
397  * Makes a call to the method of name 'api' / 'verb' with the object 'args'.
398  * This call is made in the context of the request 'req'.
399  * This call is synchronous, it waits untill completion of the request.
400  * It returns 0 on success or a negative value on error answer.
401  * The object pointed by 'result' is filled and must be released by the caller
402  * after its use by calling 'json_object_put'.
403  *
404  * For convenience, the function calls 'json_object_put' for 'args'.
405  * Thus, in the case where 'args' should remain available after
406  * the function returns, the function 'json_object_get' shall be used.
407  *
408  * See also:
409  *  - 'afb_req_subcall_req' that is convenient to keep request alive automatically.
410  *  - 'afb_req_subcall' that doesn't keep request alive automatically.
411  */
412 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)
413 {
414         return req.itf->subcallsync(req.closure, api, verb, args, result);
415 }
416
417 /*
418  * Send associated to 'req' a message described by 'fmt' and following parameters
419  * to the journal for the verbosity 'level'.
420  *
421  * 'file', 'line' and 'func' are indicators of position of the code in source files
422  * (see macros __FILE__, __LINE__ and __func__).
423  *
424  * 'level' is defined by syslog standard:
425  *      EMERGENCY         0        System is unusable
426  *      ALERT             1        Action must be taken immediately
427  *      CRITICAL          2        Critical conditions
428  *      ERROR             3        Error conditions
429  *      WARNING           4        Warning conditions
430  *      NOTICE            5        Normal but significant condition
431  *      INFO              6        Informational
432  *      DEBUG             7        Debug-level messages
433  */
434 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)));
435 static inline void afb_req_verbose(struct afb_req req, int level, const char *file, int line, const char * func, const char *fmt, ...)
436 {
437         va_list args;
438         va_start(args, fmt);
439         req.itf->vverbose(req.closure, level, file, line, func, fmt, args);
440         va_end(args);
441 }
442
443 /* macro for setting file, line and function automatically */
444 # if !defined(AFB_BINDING_PRAGMA_NO_VERBOSE_DETAILS)
445 #define AFB_REQ_VERBOSE(req,level,...) afb_req_verbose(req,level,__FILE__,__LINE__,__func__,__VA_ARGS__)
446 #else
447 #define AFB_REQ_VERBOSE(req,level,...) afb_req_verbose(req,level,NULL,0,NULL,__VA_ARGS__)
448 #endif
449
450 /*
451  * Check whether the 'permission' is granted or not to the client
452  * identified by 'req'.
453  *
454  * Returns 1 if the permission is granted or 0 otherwise.
455  */
456 static inline int afb_req_has_permission(struct afb_req req, const char *permission)
457 {
458         return req.itf->has_permission(req.closure, permission);
459 }
460
461 /*
462  * Get the application identifier of the client application for the
463  * request 'req'.
464  *
465  * Returns the application identifier or NULL when the application
466  * can not be identified.
467  *
468  * The returned value if not NULL must be freed by the caller
469  */
470 static inline char *afb_req_get_application_id(struct afb_req req)
471 {
472         return req.itf->get_application_id(req.closure);
473 }
474