2 * Copyright (C) 2016-2019 "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.
20 #include "afb-req-x1-itf.h"
21 #include "afb-event-x1.h"
23 /** @addtogroup AFB_REQ
27 * @deprecated use bindings version 3
29 * Converts the 'req' to an afb_request.
31 static inline struct afb_req_x2 *afb_req_x1_to_req_x2(struct afb_req_x1 req)
37 * @deprecated use bindings version 3
39 * Checks whether the request 'req' is valid or not.
41 * Returns 0 if not valid or 1 if valid.
43 static inline int afb_req_x1_is_valid(struct afb_req_x1 req)
49 * @deprecated use bindings version 3
51 * Gets from the request 'req' the argument of 'name'.
52 * Returns a PLAIN structure of type 'struct afb_arg'.
53 * When the argument of 'name' is not found, all fields of result are set to NULL.
54 * When the argument of 'name' is found, the fields are filled,
55 * in particular, the field 'result.name' is set to 'name'.
57 * There is a special name value: the empty string.
58 * The argument of name "" is defined only if the request was made using
59 * an HTTP POST of Content-Type "application/json". In that case, the
60 * argument of name "" receives the value of the body of the HTTP request.
62 static inline struct afb_arg afb_req_x1_get(struct afb_req_x1 req, const char *name)
64 return req.itf->get(req.closure, name);
68 * @deprecated use bindings version 3
70 * Gets from the request 'req' the string value of the argument of 'name'.
71 * Returns NULL if when there is no argument of 'name'.
72 * Returns the value of the argument of 'name' otherwise.
74 * Shortcut for: afb_req_get(req, name).value
76 static inline const char *afb_req_x1_value(struct afb_req_x1 req, const char *name)
78 return afb_req_x1_get(req, name).value;
82 * @deprecated use bindings version 3
84 * Gets from the request 'req' the path for file attached to the argument of 'name'.
85 * Returns NULL if when there is no argument of 'name' or when there is no file.
86 * Returns the path of the argument of 'name' otherwise.
88 * Shortcut for: afb_req_get(req, name).path
90 static inline const char *afb_req_x1_path(struct afb_req_x1 req, const char *name)
92 return afb_req_x1_get(req, name).path;
96 * @deprecated use bindings version 3
98 * Gets from the request 'req' the json object hashing the arguments.
99 * The returned object must not be released using 'json_object_put'.
101 static inline struct json_object *afb_req_x1_json(struct afb_req_x1 req)
103 return req.itf->json(req.closure);
107 * @deprecated use bindings version 3
109 * Sends a reply to the request 'req'.
110 * The status of the reply is set to 'error' (that must be NULL on success).
111 * Its send the object 'obj' (can be NULL) with an
112 * informational comment 'info (can also be NULL).
114 * For convenience, the function calls 'json_object_put' for 'obj'.
115 * Thus, in the case where 'obj' should remain available after
116 * the function returns, the function 'json_object_get' shall be used.
118 static inline void afb_req_x1_reply(struct afb_req_x1 req, struct json_object *obj, const char *error, const char *info)
120 req.itf->reply(req.closure, obj, error, info);
124 * @deprecated use bindings version 3
126 * Same as 'afb_req_x1_reply' but the 'info' is a formatting
127 * string followed by arguments.
129 * For convenience, the function calls 'json_object_put' for 'obj'.
130 * Thus, in the case where 'obj' should remain available after
131 * the function returns, the function 'json_object_get' shall be used.
133 static inline void afb_req_x1_reply_f(struct afb_req_x1 req, struct json_object *obj, const char *error, const char *info, ...) __attribute__((format(printf, 4, 5)));
134 static inline void afb_req_x1_reply_f(struct afb_req_x1 req, struct json_object *obj, const char *error, const char *info, ...)
137 va_start(args, info);
138 req.itf->vreply(req.closure, obj, error, info, args);
143 * @deprecated use bindings version 3
145 * Same as 'afb_req_x1_reply_f' but the arguments to the format 'info'
146 * are given as a variable argument list instance.
148 * For convenience, the function calls 'json_object_put' for 'obj'.
149 * Thus, in the case where 'obj' should remain available after
150 * the function returns, the function 'json_object_get' shall be used.
152 static inline void afb_req_x1_reply_v(struct afb_req_x1 req, struct json_object *obj, const char *error, const char *info, va_list args)
154 req.itf->vreply(req.closure, obj, error, info, args);
158 * @deprecated use bindings version 3
160 * Gets the pointer stored by the binding for the session of 'req'.
161 * When the binding has not yet recorded a pointer, NULL is returned.
163 static inline void *afb_req_x1_context_get(struct afb_req_x1 req)
165 return req.itf->context_make(req.closure, 0, 0, 0, 0);
169 * @deprecated use bindings version 3
171 * Stores for the binding the pointer 'context' to the session of 'req'.
172 * The function 'free_context' will be called when the session is closed
173 * or if binding stores an other pointer.
175 static inline void afb_req_x1_context_set(struct afb_req_x1 req, void *context, void (*free_context)(void*))
177 req.itf->context_make(req.closure, 1, 0, free_context, context);
181 * @deprecated use bindings version 3
183 * Gets the pointer stored by the binding for the session of 'req'.
184 * If the stored pointer is NULL, indicating that no pointer was
185 * already stored, afb_req_context creates a new context by calling
186 * the function 'create_context' and stores it with the freeing function
189 static inline void *afb_req_x1_context(struct afb_req_x1 req, void *(*create_context)(), void (*free_context)(void*))
191 return req.itf->context_make(req.closure, 0, (void *(*)(void*))(void*)create_context, free_context, 0);
195 * @deprecated use bindings version 3
197 * Gets the pointer stored by the binding for the session of 'request'.
198 * If no previous pointer is stored or if 'replace' is not zero, a new value
199 * is generated using the function 'create_context' called with the 'closure'.
200 * If 'create_context' is NULL the generated value is 'closure'.
201 * When a value is created, the function 'free_context' is recorded and will
202 * be called (with the created value as argument) to free the created value when
203 * it is not more used.
204 * This function is atomic: it ensures that 2 threads will not race together.
206 static inline void *afb_req_x1_context_make(struct afb_req_x1 req, int replace, void *(*create_context)(void *closure), void (*free_context)(void*), void *closure)
208 return req.itf->context_make(req.closure, replace, create_context, free_context, closure);
212 * @deprecated use bindings version 3
214 * Frees the pointer stored by the binding for the session of 'req'
215 * and sets it to NULL.
217 * Shortcut for: afb_req_context_set(req, NULL, NULL)
219 static inline void afb_req_x1_context_clear(struct afb_req_x1 req)
221 req.itf->context_make(req.closure, 1, 0, 0, 0);
225 * @deprecated use bindings version 3
227 * Adds one to the count of references of 'req'.
228 * This function MUST be called by asynchronous implementations
229 * of verbs if no reply was sent before returning.
231 static inline void afb_req_x1_addref(struct afb_req_x1 req)
233 req.itf->addref(req.closure);
237 * @deprecated use bindings version 3
239 * Substracts one to the count of references of 'req'.
240 * This function MUST be called by asynchronous implementations
241 * of verbs after sending the asynchronous reply.
243 static inline void afb_req_x1_unref(struct afb_req_x1 req)
245 req.itf->unref(req.closure);
249 * @deprecated use bindings version 3
251 * Closes the session associated with 'req'
252 * and delete all associated contexts.
254 static inline void afb_req_x1_session_close(struct afb_req_x1 req)
256 req.itf->session_close(req.closure);
260 * @deprecated use bindings version 3
262 * Sets the level of assurance of the session of 'req'
263 * to 'level'. The effect of this function is subject of
265 * Returns 1 on success or 0 if failed.
267 static inline int afb_req_x1_session_set_LOA(struct afb_req_x1 req, unsigned level)
269 return 1 + req.itf->session_set_LOA(req.closure, level);
273 * @deprecated use bindings version 3
275 * Establishes for the client link identified by 'req' a subscription
277 * Establishing subscriptions MUST be called BEFORE replying to the request.
278 * Returns 0 in case of successful subscription or -1 in case of error.
280 static inline int afb_req_x1_subscribe(struct afb_req_x1 req, struct afb_event_x1 event)
282 return req.itf->legacy_subscribe_event_x1(req.closure, event);
286 * @deprecated use bindings version 3
288 * Revokes the subscription established to the 'event' for the client
289 * link identified by 'req'.
290 * Revoking subscription MUST be called BEFORE replying to the request.
291 * Returns 0 in case of successful subscription or -1 in case of error.
293 static inline int afb_req_x1_unsubscribe(struct afb_req_x1 req, struct afb_event_x1 event)
295 return req.itf->legacy_unsubscribe_event_x1(req.closure, event);
299 * @deprecated use bindings version 3
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 'req'.
303 * On completion, the function 'callback' is invoked with the
304 * 'closure' given at call and two other parameters: 'iserror' and 'result'.
305 * 'status' is 0 on success or negative when on an error reply.
306 * 'result' is the json object of the reply, you must not call json_object_put
309 * For convenience, the function calls 'json_object_put' for 'args'.
310 * Thus, in the case where 'args' should remain available after
311 * the function returns, the function 'json_object_get' shall be used.
314 * - 'afb_req_subcall_req' that is convenient to keep request alive automatically.
315 * - 'afb_req_subcall_sync' the synchronous version
317 static inline void afb_req_x1_subcall(struct afb_req_x1 req, const char *api, const char *verb, struct json_object *args, void (*callback)(void *closure, int iserror, struct json_object *result), void *closure)
319 req.itf->legacy_subcall(req.closure, api, verb, args, callback, closure);
323 * @deprecated use bindings version 3
325 * Makes a call to the method of name 'api' / 'verb' with the object 'args'.
326 * This call is made in the context of the request 'req'.
327 * On completion, the function 'callback' is invoked with the
328 * original request 'req', the 'closure' given at call and two
329 * other parameters: 'iserror' and 'result'.
330 * 'status' is 0 on success or negative when on an error reply.
331 * 'result' is the json object of the reply, you must not call json_object_put
334 * For convenience, the function calls 'json_object_put' for 'args'.
335 * Thus, in the case where 'args' should remain available after
336 * the function returns, the function 'json_object_get' shall be used.
339 * - 'afb_req_subcall' that doesn't keep request alive automatically.
340 * - 'afb_req_subcall_sync' the synchronous version
342 static inline void afb_req_x1_subcall_req(struct afb_req_x1 req, const char *api, const char *verb, struct json_object *args, void (*callback)(void *closure, int iserror, struct json_object *result, struct afb_req_x1 req), void *closure)
344 req.itf->legacy_subcall_req(req.closure, api, verb, args, callback, closure);
348 * @deprecated use bindings version 3
350 * Makes a call to the method of name 'api' / 'verb' with the object 'args'.
351 * This call is made in the context of the request 'req'.
352 * This call is synchronous, it waits untill completion of the request.
353 * It returns 0 on success or a negative value on error answer.
354 * The object pointed by 'result' is filled and must be released by the caller
355 * after its use by calling 'json_object_put'.
357 * For convenience, the function calls 'json_object_put' for 'args'.
358 * Thus, in the case where 'args' should remain available after
359 * the function returns, the function 'json_object_get' shall be used.
362 * - 'afb_req_subcall_req' that is convenient to keep request alive automatically.
363 * - 'afb_req_subcall' that doesn't keep request alive automatically.
365 static inline int afb_req_x1_subcall_sync(struct afb_req_x1 req, const char *api, const char *verb, struct json_object *args, struct json_object **result)
367 return req.itf->legacy_subcallsync(req.closure, api, verb, args, result);
371 * @deprecated use bindings version 3
373 * Send associated to 'req' a message described by 'fmt' and following parameters
374 * to the journal for the verbosity 'level'.
376 * 'file', 'line' and 'func' are indicators of position of the code in source files
377 * (see macros __FILE__, __LINE__ and __func__).
379 * 'level' is defined by syslog standard:
380 * EMERGENCY 0 System is unusable
381 * ALERT 1 Action must be taken immediately
382 * CRITICAL 2 Critical conditions
383 * ERROR 3 Error conditions
384 * WARNING 4 Warning conditions
385 * NOTICE 5 Normal but significant condition
386 * INFO 6 Informational
387 * DEBUG 7 Debug-level messages
389 static inline void afb_req_x1_verbose(struct afb_req_x1 req, int level, const char *file, int line, const char * func, const char *fmt, ...) __attribute__((format(printf, 6, 7)));
390 static inline void afb_req_x1_verbose(struct afb_req_x1 req, int level, const char *file, int line, const char * func, const char *fmt, ...)
394 req.itf->vverbose(req.closure, level, file, line, func, fmt, args);
399 * @deprecated use bindings version 3
401 * Check whether the 'permission' is granted or not to the client
402 * identified by 'req'.
404 * Returns 1 if the permission is granted or 0 otherwise.
406 static inline int afb_req_x1_has_permission(struct afb_req_x1 req, const char *permission)
408 return req.itf->has_permission(req.closure, permission);
412 * @deprecated use bindings version 3
414 * Get the application identifier of the client application for the
417 * Returns the application identifier or NULL when the application
418 * can not be identified.
420 * The returned value if not NULL must be freed by the caller
422 static inline char *afb_req_x1_get_application_id(struct afb_req_x1 req)
424 return req.itf->get_application_id(req.closure);
428 * @deprecated use bindings version 3
430 * Get the user identifier (UID) of the client application for the
433 * Returns -1 when the application can not be identified.
435 static inline int afb_req_x1_get_uid(struct afb_req_x1 req)
437 return req.itf->get_uid(req.closure);
441 * @deprecated use bindings version 3
443 * Get informations about the client of the
446 * Returns an object with client informations:
448 * "pid": int, "uid": int, "gid": int,
449 * "smack": string, "appid": string,
450 * "uuid": string, "LOA": int
453 static inline struct json_object *afb_req_x1_get_client_info(struct afb_req_x1 req)
455 return req.itf->get_client_info(req.closure);