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