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