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