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