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