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