1d60daee16401223bdaca57be47ecbebfa9187bc
[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 #if !defined(_GNU_SOURCE)
21 # error "_GNU_SOURCE must be defined for using vasprintf"
22 #endif
23
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27
28 #include "afb-event-itf.h"
29
30 /* avoid inclusion of <json-c/json.h> */
31 struct json_object;
32
33 /*
34  * Describes an argument (or parameter) of a request
35  */
36 struct afb_arg {
37         const char *name;       /* name of the argument or NULL if invalid */
38         const char *value;      /* string representation of the value of the argument */
39                                 /* original filename of the argument if path != NULL */
40         const char *path;       /* if not NULL, path of the received file for the argument */
41                                 /* when the request is finalized this file is removed */
42 };
43
44 /*
45  * Interface for handling requests.
46  * It records the functions to be called for the request.
47  * Don't use this structure directly.
48  * Use the helper functions documented below.
49  */
50 struct afb_req_itf {
51         /* CAUTION: respect the order, add at the end */
52
53         struct json_object *(*json)(void *closure);
54         struct afb_arg (*get)(void *closure, const char *name);
55
56         void (*success)(void *closure, struct json_object *obj, const char *info);
57         void (*fail)(void *closure, const char *status, const char *info);
58
59         const char *(*raw)(void *closure, size_t *size);
60         void (*send)(void *closure, const char *buffer, size_t size);
61
62         void *(*context_get)(void *closure);
63         void (*context_set)(void *closure, void *value, void (*free_value)(void*));
64
65         void (*addref)(void *closure);
66         void (*unref)(void *closure);
67
68         void (*session_close)(void *closure);
69         int (*session_set_LOA)(void *closure, unsigned level);
70
71         int (*subscribe)(void *closure, struct afb_event event);
72         int (*unsubscribe)(void *closure, struct afb_event event);
73
74         void (*subcall)(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure);
75         int (*subcallsync)(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result);
76 };
77
78 /*
79  * Describes the request by bindings from afb-daemon
80  */
81 struct afb_req {
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 != NULL;
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, ...)
170 {
171         char *message;
172         va_list args;
173         va_start(args, info);
174         if (info == NULL || vasprintf(&message, info, args) < 0)
175                 message = NULL;
176         va_end(args);
177         afb_req_success(req, obj, message);
178         free(message);
179 }
180
181 /*
182  * Sends a reply of kind failure to the request 'req'.
183  * The status of the reply is set to 'status' and an
184  * informationnal comment 'info' (can also be NULL) can be added.
185  *
186  * Note that calling afb_req_fail("success", info) is equivalent
187  * to call afb_req_success(NULL, info). Thus even if possible it
188  * is strongly recommanded to NEVER use "success" for status.
189  *
190  * For convenience, the function calls 'json_object_put' for 'obj'.
191  * Thus, in the case where 'obj' should remain available after
192  * the function returns, the function 'json_object_get' shall be used.
193  */
194 static inline void afb_req_fail(struct afb_req req, const char *status, const char *info)
195 {
196         req.itf->fail(req.closure, status, info);
197 }
198
199 /*
200  * Same as 'afb_req_fail' but the 'info' is a formatting
201  * string followed by arguments.
202  *
203  * For convenience, the function calls 'json_object_put' for 'obj'.
204  * Thus, in the case where 'obj' should remain available after
205  * the function returns, the function 'json_object_get' shall be used.
206  */
207 static inline void afb_req_fail_f(struct afb_req req, const char *status, const char *info, ...)
208 {
209         char *message;
210         va_list args;
211         va_start(args, info);
212         if (info == NULL || vasprintf(&message, info, args) < 0)
213                 message = NULL;
214         va_end(args);
215         afb_req_fail(req, status, message);
216         free(message);
217 }
218
219 /*
220  * Gets the pointer stored by the binding for the session of 'req'.
221  * When the binding has not yet recorded a pointer, NULL is returned.
222  */
223 static inline void *afb_req_context_get(struct afb_req req)
224 {
225         return req.itf->context_get(req.closure);
226 }
227
228 /*
229  * Stores for the binding the pointer 'context' to the session of 'req'.
230  * The function 'free_context' will be called when the session is closed
231  * or if binding stores an other pointer.
232  */
233 static inline void afb_req_context_set(struct afb_req req, void *context, void (*free_context)(void*))
234 {
235         req.itf->context_set(req.closure, context, free_context);
236 }
237
238 /*
239  * Gets the pointer stored by the binding for the session of 'req'.
240  * If the stored pointer is NULL, indicating that no pointer was
241  * already stored, afb_req_context creates a new context by calling
242  * the function 'create_context' and stores it with the freeing function
243  * 'free_context'.
244  */
245 static inline void *afb_req_context(struct afb_req req, void *(*create_context)(), void (*free_context)(void*))
246 {
247         void *result = afb_req_context_get(req);
248         if (result == NULL) {
249                 result = create_context();
250                 afb_req_context_set(req, result, free_context);
251         }
252         return result;
253 }
254
255 /*
256  * Frees the pointer stored by the binding for the session of 'req'
257  * and sets it to NULL.
258  *
259  * Shortcut for: afb_req_context_set(req, NULL, NULL)
260  */
261 static inline void afb_req_context_clear(struct afb_req req)
262 {
263         afb_req_context_set(req, NULL, NULL);
264 }
265
266 /*
267  * Adds one to the count of references of 'req'.
268  * This function MUST be called by asynchronous implementations
269  * of verbs if no reply was sent before returning.
270  */
271 static inline void afb_req_addref(struct afb_req req)
272 {
273         req.itf->addref(req.closure);
274 }
275
276 /*
277  * Substracts one to the count of references of 'req'.
278  * This function MUST be called by asynchronous implementations
279  * of verbs after sending the asynchronous reply.
280  */
281 static inline void afb_req_unref(struct afb_req req)
282 {
283         req.itf->unref(req.closure);
284 }
285
286 /*
287  * Closes the session associated with 'req'
288  * and delete all associated contexts.
289  */
290 static inline void afb_req_session_close(struct afb_req req)
291 {
292         req.itf->session_close(req.closure);
293 }
294
295 /*
296  * Sets the level of assurance of the session of 'req'
297  * to 'level'. The effect of this function is subject of
298  * security policies.
299  * Returns 1 on success or 0 if failed.
300  */
301 static inline int afb_req_session_set_LOA(struct afb_req req, unsigned level)
302 {
303         return req.itf->session_set_LOA(req.closure, level);
304 }
305
306 /*
307  * Stores 'req' on heap for asynchrnous use.
308  * Returns a pointer to the stored 'req' or NULL on memory depletion.
309  * The count of reference to 'req' is incremented on success
310  * (see afb_req_addref).
311  */
312 static inline struct afb_req *afb_req_store(struct afb_req req)
313 {
314         struct afb_req *result = (struct afb_req*)malloc(sizeof *result);
315         if (result != NULL) {
316                 *result = req;
317                 afb_req_addref(req);
318         }
319         return result;
320 }
321
322 /*
323  * Retrieves the afb_req stored at 'req' and frees the memory.
324  * Returns the stored request.
325  * The count of reference is UNCHANGED, thus, normally, the
326  * function 'afb_req_unref' should be called on the result
327  * after that the asynchronous reply if sent.
328  */
329 static inline struct afb_req afb_req_unstore(struct afb_req *req)
330 {
331         struct afb_req result = *req;
332         free(req);
333         return result;
334 }
335
336 /*
337  * Establishes for the client link identified by 'req' a subscription
338  * to the 'event'.
339  * Returns 0 in case of successful subscription or -1 in case of error.
340  */
341 static inline int afb_req_subscribe(struct afb_req req, struct afb_event event)
342 {
343         return req.itf->subscribe(req.closure, event);
344 }
345
346 /*
347  * Revokes the subscription established to the 'event' for the client
348  * link identified by 'req'.
349  * Returns 0 in case of successful subscription or -1 in case of error.
350  */
351 static inline int afb_req_unsubscribe(struct afb_req req, struct afb_event event)
352 {
353         return req.itf->unsubscribe(req.closure, event);
354 }
355
356 /*
357  * Makes a call to the method of name 'api' / 'verb' with the object 'args'.
358  * This call is made in the context of the request 'req'.
359  * On completion, the function 'callback' is invoked with the
360  * 'closure' given at call and two other parameters: 'iserror' and 'result'.
361  * 'iserror' is a boolean that indicates if the reply is an error reply.
362  * 'result' is the json object of the reply, you must not call json_object_put
363  * on the result.
364  *
365  * For convenience, the function calls 'json_object_put' for 'args'.
366  * Thus, in the case where 'args' should remain available after
367  * the function returns, the function 'json_object_get' shall be used.
368  */
369 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)
370 {
371         req.itf->subcall(req.closure, api, verb, args, callback, closure);
372 }
373
374 /*
375  * Makes a call to the method of name 'api' / 'verb' with the object 'args'.
376  * This call is made in the context of the request 'req'.
377  * This call is synchronous, it waits untill completion of the request.
378  * It returns 0 on an error answer and returns 1 when no error was detected.
379  * The object pointed by 'result' is filled and must be released by the caller
380  * after its use by calling 'json_object_put'.
381  *
382  * For convenience, the function calls 'json_object_put' for 'args'.
383  * Thus, in the case where 'args' should remain available after
384  * the function returns, the function 'json_object_get' shall be used.
385  */
386 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)
387 {
388         return req.itf->subcallsync(req.closure, api, verb, args, result);
389 }
390
391 /* internal use */
392 static inline const char *afb_req_raw(struct afb_req req, size_t *size)
393 {
394         return req.itf->raw(req.closure, size);
395 }
396
397 /* internal use */
398 static inline void afb_req_send(struct afb_req req, const char *buffer, size_t size)
399 {
400         req.itf->send(req.closure, buffer, size);
401 }
402