Events: refactoring
[src/app-framework-binder.git] / include / afb / afb-req-itf.h
1 /*
2  * Copyright (C) 2016 "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/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
75 /*
76  * Describes the request by plugins from afb-daemon
77  */
78 struct afb_req {
79         const struct afb_req_itf *itf;  /* the interface to use */
80         void *closure;                  /* the closure argument for functions of 'itf' */
81 };
82
83 /*
84  * Gets from the request 'req' the argument of 'name'.
85  * Returns a PLAIN structure of type 'struct afb_arg'.
86  * When the argument of 'name' is not found, all fields of result are set to NULL.
87  * When the argument of 'name' is found, the fields are filled,
88  * in particular, the field 'result.name' is set to 'name'.
89  *
90  * There is a special name value: the empty string.
91  * The argument of name "" is defined only if the request was made using
92  * an HTTP POST of Content-Type "application/json". In that case, the
93  * argument of name "" receives the value of the body of the HTTP request.
94  */
95 static inline struct afb_arg afb_req_get(struct afb_req req, const char *name)
96 {
97         return req.itf->get(req.closure, name);
98 }
99
100 /*
101  * Gets from the request 'req' the string value of the argument of 'name'.
102  * Returns NULL if when there is no argument of 'name'.
103  * Returns the value of the argument of 'name' otherwise.
104  *
105  * Shortcut for: afb_req_get(req, name).value
106  */
107 static inline const char *afb_req_value(struct afb_req req, const char *name)
108 {
109         return afb_req_get(req, name).value;
110 }
111
112 /*
113  * Gets from the request 'req' the path for file attached to the argument of 'name'.
114  * Returns NULL if when there is no argument of 'name' or when there is no file.
115  * Returns the path of the argument of 'name' otherwise.
116  *
117  * Shortcut for: afb_req_get(req, name).path
118  */
119 static inline const char *afb_req_path(struct afb_req req, const char *name)
120 {
121         return afb_req_get(req, name).path;
122 }
123
124 /*
125  * Gets from the request 'req' the json object hashing the arguments.
126  * The returned object must not be released using 'json_object_put'.
127  */
128 static inline struct json_object *afb_req_json(struct afb_req req)
129 {
130         return req.itf->json(req.closure);
131 }
132
133 /*
134  * Sends a reply of kind success to the request 'req'.
135  * The status of the reply is automatically set to "success".
136  * Its send the object 'obj' (can be NULL) with an
137  * informationnal comment 'info (can also be NULL).
138  *
139  * For conveniency, the function calls 'json_object_put' for 'obj'.
140  * Thus, in the case where 'obj' should remain available after
141  * the function returns, the function 'json_object_get' shall be used.
142  */
143 static inline void afb_req_success(struct afb_req req, struct json_object *obj, const char *info)
144 {
145         req.itf->success(req.closure, obj, info);
146 }
147
148 /*
149  * Same as 'afb_req_success' but the 'info' is a formatting
150  * string followed by arguments.
151  *
152  * For conveniency, 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_f(struct afb_req req, struct json_object *obj, const char *info, ...)
157 {
158         char *message;
159         va_list args;
160         va_start(args, info);
161         if (info == NULL || vasprintf(&message, info, args) < 0)
162                 message = NULL;
163         va_end(args);
164         afb_req_success(req, obj, message);
165         free(message);
166 }
167
168 /*
169  * Sends a reply of kind failure to the request 'req'.
170  * The status of the reply is set to 'status' and an
171  * informationnal comment 'info' (can also be NULL) can be added.
172  *
173  * Note that calling afb_req_fail("success", info) is equivalent
174  * to call afb_req_success(NULL, info). Thus even if possible it
175  * is strongly recommanded to NEVER use "success" for status.
176  *
177  * For conveniency, the function calls 'json_object_put' for 'obj'.
178  * Thus, in the case where 'obj' should remain available after
179  * the function returns, the function 'json_object_get' shall be used.
180  */
181 static inline void afb_req_fail(struct afb_req req, const char *status, const char *info)
182 {
183         req.itf->fail(req.closure, status, info);
184 }
185
186 /*
187  * Same as 'afb_req_fail' but the 'info' is a formatting
188  * string followed by arguments.
189  *
190  * For conveniency, 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_f(struct afb_req req, const char *status, const char *info, ...)
195 {
196         char *message;
197         va_list args;
198         va_start(args, info);
199         if (info == NULL || vasprintf(&message, info, args) < 0)
200                 message = NULL;
201         va_end(args);
202         afb_req_fail(req, status, message);
203         free(message);
204 }
205
206 /*
207  * Gets the pointer stored by the plugin for the session of 'req'.
208  * When the plugin has not yet recorded a pointer, NULL is returned.
209  */
210 static inline void *afb_req_context_get(struct afb_req req)
211 {
212         return req.itf->context_get(req.closure);
213 }
214
215 /*
216  * Stores for the plugin the pointer 'context' to the session of 'req'.
217  * The function 'free_context' will be called when the session is closed
218  * or if plugin stores an other pointer.
219  */
220 static inline void afb_req_context_set(struct afb_req req, void *context, void (*free_context)(void*))
221 {
222         req.itf->context_set(req.closure, context, free_context);
223 }
224
225 /*
226  * Gets the pointer stored by the plugin for the session of 'req'.
227  * If the stored pointer is NULL, indicating that no pointer was
228  * already stored, afb_req_context creates a new context by calling
229  * the function 'create_context' and stores it with the freeing function
230  * 'free_context'.
231  */
232 static inline void *afb_req_context(struct afb_req req, void *(*create_context)(), void (*free_context)(void*))
233 {
234         void *result = afb_req_context_get(req);
235         if (result == NULL) {
236                 result = create_context();
237                 afb_req_context_set(req, result, free_context);
238         }
239         return result;
240 }
241
242 /*
243  * Frees the pointer stored by the plugin for the session of 'req'
244  * and sets it to NULL.
245  *
246  * Shortcut for: afb_req_context_set(req, NULL, NULL)
247  */
248 static inline void afb_req_context_clear(struct afb_req req)
249 {
250         afb_req_context_set(req, NULL, NULL);
251 }
252
253 /*
254  * Adds one to the count of references of 'req'.
255  * This function MUST be called by asynchronous implementations
256  * of verbs if no reply was sent before returning.
257  */
258 static inline void afb_req_addref(struct afb_req req)
259 {
260         req.itf->addref(req.closure);
261 }
262
263 /*
264  * Substracts one to the count of references of 'req'.
265  * This function MUST be called by asynchronous implementations
266  * of verbs after sending the asynchronous reply.
267  */
268 static inline void afb_req_unref(struct afb_req req)
269 {
270         req.itf->unref(req.closure);
271 }
272
273 /*
274  * Closes the session associated with 'req'
275  * and delete all associated contexts.
276  */
277 static inline void afb_req_session_close(struct afb_req req)
278 {
279         req.itf->session_close(req.closure);
280 }
281
282 /*
283  * Sets the level of assurance of the session of 'req'
284  * to 'level'. The effect of this function is subject of
285  * security policies.
286  * Returns 1 on success or 0 if failed.
287  */
288 static inline int afb_req_session_set_LOA(struct afb_req req, unsigned level)
289 {
290         return req.itf->session_set_LOA(req.closure, level);
291 }
292
293 /*
294  * Stores 'req' on heap for asynchrnous use.
295  * Returns a pointer to the stored 'req' or NULL on memory depletion.
296  * The count of reference to 'req' is incremented on success
297  * (see afb_req_addref).
298  */
299 static inline struct afb_req *afb_req_store(struct afb_req req)
300 {
301         struct afb_req *result = malloc(sizeof *result);
302         if (result != NULL) {
303                 *result = req;
304                 afb_req_addref(req);
305         }
306         return result;
307 }
308
309 /*
310  * Retrieves the afb_req stored at 'req' and frees the memory.
311  * Returns the stored request.
312  * The count of reference is UNCHANGED, thus, normally, the
313  * function 'afb_req_unref' should be called on the result
314  * after that the asynchronous reply if sent.
315  */
316 static inline struct afb_req afb_req_unstore(struct afb_req *req)
317 {
318         struct afb_req result = *req;
319         free(req);
320         return result;
321 }
322
323 /*
324  * Establishes for the client link identified by 'req' a subscription
325  * to the 'event'.
326  * Returns 0 in case of successful subscription or -1 in case of error.
327  */
328 static inline int afb_req_subscribe(struct afb_req req, struct afb_event event)
329 {
330         return req.itf->subscribe(req.closure, event);
331 }
332
333 /*
334  * Revokes the subscription established to the 'event' for the client
335  * link identified by 'req'.
336  * Returns 0 in case of successful subscription or -1 in case of error.
337  */
338 static inline int afb_req_unsubscribe(struct afb_req req, struct afb_event event)
339 {
340         return req.itf->unsubscribe(req.closure, event);
341 }
342
343
344
345
346 /* internal use */
347 static inline const char *afb_req_raw(struct afb_req req, size_t *size)
348 {
349         return req.itf->raw(req.closure, size);
350 }
351
352 /* internal use */
353 static inline void afb_req_send(struct afb_req req, const char *buffer, size_t size)
354 {
355         req.itf->send(req.closure, buffer, size);
356 }
357