2 * Copyright (C) 2016, 2017 "IoT.bzh"
3 * Author: José Bollo <jose.bollo@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include "afb-event-itf.h"
24 /* avoid inclusion of <json-c/json.h> */
28 * Describes an argument (or parameter) of a request
31 const char *name; /* name of the argument or NULL if invalid */
32 const char *value; /* string representation of the value of the argument */
33 /* original filename of the argument if path != NULL */
34 const char *path; /* if not NULL, path of the received file for the argument */
35 /* when the request is finalized this file is removed */
39 * Interface for handling requests.
40 * It records the functions to be called for the request.
41 * Don't use this structure directly.
42 * Use the helper functions documented below.
45 /* CAUTION: respect the order, add at the end */
47 struct json_object *(*json)(void *closure);
48 struct afb_arg (*get)(void *closure, const char *name);
50 void (*success)(void *closure, struct json_object *obj, const char *info);
51 void (*fail)(void *closure, const char *status, const char *info);
53 void (*vsuccess)(void *closure, struct json_object *obj, const char *fmt, va_list args);
54 void (*vfail)(void *closure, const char *status, const char *fmt, va_list args);
56 void *(*context_get)(void *closure);
57 void (*context_set)(void *closure, void *value, void (*free_value)(void*));
59 void (*addref)(void *closure);
60 void (*unref)(void *closure);
62 void (*session_close)(void *closure);
63 int (*session_set_LOA)(void *closure, unsigned level);
65 int (*subscribe)(void *closure, struct afb_event event);
66 int (*unsubscribe)(void *closure, struct afb_event event);
68 void (*subcall)(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure);
69 int (*subcallsync)(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result);
73 * Describes the request by bindings from afb-daemon
76 const struct afb_req_itf *itf; /* the interface to use */
77 void *closure; /* the closure argument for functions of 'itf' */
81 * Checks wether the request 'req' is valid or not.
83 * Returns 0 if not valid or 1 if valid.
85 static inline int afb_req_is_valid(struct afb_req req)
87 return req.itf != NULL;
91 * Gets from the request 'req' the argument of 'name'.
92 * Returns a PLAIN structure of type 'struct afb_arg'.
93 * When the argument of 'name' is not found, all fields of result are set to NULL.
94 * When the argument of 'name' is found, the fields are filled,
95 * in particular, the field 'result.name' is set to 'name'.
97 * There is a special name value: the empty string.
98 * The argument of name "" is defined only if the request was made using
99 * an HTTP POST of Content-Type "application/json". In that case, the
100 * argument of name "" receives the value of the body of the HTTP request.
102 static inline struct afb_arg afb_req_get(struct afb_req req, const char *name)
104 return req.itf->get(req.closure, name);
108 * Gets from the request 'req' the string value of the argument of 'name'.
109 * Returns NULL if when there is no argument of 'name'.
110 * Returns the value of the argument of 'name' otherwise.
112 * Shortcut for: afb_req_get(req, name).value
114 static inline const char *afb_req_value(struct afb_req req, const char *name)
116 return afb_req_get(req, name).value;
120 * Gets from the request 'req' the path for file attached to the argument of 'name'.
121 * Returns NULL if when there is no argument of 'name' or when there is no file.
122 * Returns the path of the argument of 'name' otherwise.
124 * Shortcut for: afb_req_get(req, name).path
126 static inline const char *afb_req_path(struct afb_req req, const char *name)
128 return afb_req_get(req, name).path;
132 * Gets from the request 'req' the json object hashing the arguments.
133 * The returned object must not be released using 'json_object_put'.
135 static inline struct json_object *afb_req_json(struct afb_req req)
137 return req.itf->json(req.closure);
141 * Sends a reply of kind success to the request 'req'.
142 * The status of the reply is automatically set to "success".
143 * Its send the object 'obj' (can be NULL) with an
144 * informationnal comment 'info (can also be NULL).
146 * For convenience, the function calls 'json_object_put' for 'obj'.
147 * Thus, in the case where 'obj' should remain available after
148 * the function returns, the function 'json_object_get' shall be used.
150 static inline void afb_req_success(struct afb_req req, struct json_object *obj, const char *info)
152 req.itf->success(req.closure, obj, info);
156 * Same as 'afb_req_success' but the 'info' is a formatting
157 * string followed by arguments.
159 * For convenience, the function calls 'json_object_put' for 'obj'.
160 * Thus, in the case where 'obj' should remain available after
161 * the function returns, the function 'json_object_get' shall be used.
163 static inline void afb_req_success_f(struct afb_req req, struct json_object *obj, const char *info, ...) __attribute__((format(printf, 3, 4)));
164 static inline void afb_req_success_f(struct afb_req req, struct json_object *obj, const char *info, ...)
167 va_start(args, info);
168 req.itf->vsuccess(req.closure, obj, info, args);
173 * Same as 'afb_req_success_f' but the arguments to the format 'info'
174 * are given as a variable argument list instance.
176 * For convenience, the function calls 'json_object_put' for 'obj'.
177 * Thus, in the case where 'obj' should remain available after
178 * the function returns, the function 'json_object_get' shall be used.
180 static inline void afb_req_success_v(struct afb_req req, struct json_object *obj, const char *info, va_list args)
182 req.itf->vsuccess(req.closure, obj, info, args);
186 * Sends a reply of kind failure to the request 'req'.
187 * The status of the reply is set to 'status' and an
188 * informationnal comment 'info' (can also be NULL) can be added.
190 * Note that calling afb_req_fail("success", info) is equivalent
191 * to call afb_req_success(NULL, info). Thus even if possible it
192 * is strongly recommanded to NEVER use "success" for status.
194 static inline void afb_req_fail(struct afb_req req, const char *status, const char *info)
196 req.itf->fail(req.closure, status, info);
200 * Same as 'afb_req_fail' but the 'info' is a formatting
201 * string followed by arguments.
203 static inline void afb_req_fail_f(struct afb_req req, const char *status, const char *info, ...) __attribute__((format(printf, 3, 4)));
204 static inline void afb_req_fail_f(struct afb_req req, const char *status, const char *info, ...)
207 va_start(args, info);
208 req.itf->vfail(req.closure, status, info, args);
213 * Same as 'afb_req_fail_f' but the arguments to the format 'info'
214 * are given as a variable argument list instance.
216 static inline void afb_req_fail_v(struct afb_req req, const char *status, const char *info, va_list args)
218 req.itf->vfail(req.closure, status, info, args);
222 * Gets the pointer stored by the binding for the session of 'req'.
223 * When the binding has not yet recorded a pointer, NULL is returned.
225 static inline void *afb_req_context_get(struct afb_req req)
227 return req.itf->context_get(req.closure);
231 * Stores for the binding the pointer 'context' to the session of 'req'.
232 * The function 'free_context' will be called when the session is closed
233 * or if binding stores an other pointer.
235 static inline void afb_req_context_set(struct afb_req req, void *context, void (*free_context)(void*))
237 req.itf->context_set(req.closure, context, free_context);
241 * Gets the pointer stored by the binding for the session of 'req'.
242 * If the stored pointer is NULL, indicating that no pointer was
243 * already stored, afb_req_context creates a new context by calling
244 * the function 'create_context' and stores it with the freeing function
247 static inline void *afb_req_context(struct afb_req req, void *(*create_context)(), void (*free_context)(void*))
249 void *result = afb_req_context_get(req);
250 if (result == NULL) {
251 result = create_context();
252 afb_req_context_set(req, result, free_context);
258 * Frees the pointer stored by the binding for the session of 'req'
259 * and sets it to NULL.
261 * Shortcut for: afb_req_context_set(req, NULL, NULL)
263 static inline void afb_req_context_clear(struct afb_req req)
265 afb_req_context_set(req, NULL, NULL);
269 * Adds one to the count of references of 'req'.
270 * This function MUST be called by asynchronous implementations
271 * of verbs if no reply was sent before returning.
273 static inline void afb_req_addref(struct afb_req req)
275 req.itf->addref(req.closure);
279 * Substracts one to the count of references of 'req'.
280 * This function MUST be called by asynchronous implementations
281 * of verbs after sending the asynchronous reply.
283 static inline void afb_req_unref(struct afb_req req)
285 req.itf->unref(req.closure);
289 * Closes the session associated with 'req'
290 * and delete all associated contexts.
292 static inline void afb_req_session_close(struct afb_req req)
294 req.itf->session_close(req.closure);
298 * Sets the level of assurance of the session of 'req'
299 * to 'level'. The effect of this function is subject of
301 * Returns 1 on success or 0 if failed.
303 static inline int afb_req_session_set_LOA(struct afb_req req, unsigned level)
305 return req.itf->session_set_LOA(req.closure, level);
309 * Stores 'req' on heap for asynchrnous use.
310 * Returns a pointer to the stored 'req' or NULL on memory depletion.
311 * The count of reference to 'req' is incremented on success
312 * (see afb_req_addref).
314 static inline struct afb_req *afb_req_store(struct afb_req req)
316 struct afb_req *result = (struct afb_req*)malloc(sizeof *result);
317 if (result != NULL) {
325 * Retrieves the afb_req stored at 'req' and frees the memory.
326 * Returns the stored request.
327 * The count of reference is UNCHANGED, thus, normally, the
328 * function 'afb_req_unref' should be called on the result
329 * after that the asynchronous reply if sent.
331 static inline struct afb_req afb_req_unstore(struct afb_req *req)
333 struct afb_req result = *req;
339 * Establishes for the client link identified by 'req' a subscription
341 * Returns 0 in case of successful subscription or -1 in case of error.
343 static inline int afb_req_subscribe(struct afb_req req, struct afb_event event)
345 return req.itf->subscribe(req.closure, event);
349 * Revokes the subscription established to the 'event' for the client
350 * link identified by 'req'.
351 * Returns 0 in case of successful subscription or -1 in case of error.
353 static inline int afb_req_unsubscribe(struct afb_req req, struct afb_event event)
355 return req.itf->unsubscribe(req.closure, event);
359 * Makes a call to the method of name 'api' / 'verb' with the object 'args'.
360 * This call is made in the context of the request 'req'.
361 * On completion, the function 'callback' is invoked with the
362 * 'closure' given at call and two other parameters: 'iserror' and 'result'.
363 * 'iserror' is a boolean that indicates if the reply is an error reply.
364 * 'result' is the json object of the reply, you must not call json_object_put
367 * For convenience, the function calls 'json_object_put' for 'args'.
368 * Thus, in the case where 'args' should remain available after
369 * the function returns, the function 'json_object_get' shall be used.
371 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)
373 req.itf->subcall(req.closure, api, verb, args, callback, closure);
377 * Makes a call to the method of name 'api' / 'verb' with the object 'args'.
378 * This call is made in the context of the request 'req'.
379 * This call is synchronous, it waits untill completion of the request.
380 * It returns 0 on an error answer and returns 1 when no error was detected.
381 * The object pointed by 'result' is filled and must be released by the caller
382 * after its use by calling 'json_object_put'.
384 * For convenience, the function calls 'json_object_put' for 'args'.
385 * Thus, in the case where 'args' should remain available after
386 * the function returns, the function 'json_object_get' shall be used.
388 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)
390 return req.itf->subcallsync(req.closure, api, verb, args, result);