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