2 * Copyright (C) 2016, 2017, 2018 "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"
24 * @deprecated use bindings version 3
26 * Converts the 'req' to an afb_request.
28 static inline struct afb_req_x2 *afb_req_x1_to_req_x2(struct afb_req_x1 req)
34 * @deprecated use bindings version 3
36 * Checks whether the request 'req' is valid or not.
38 * Returns 0 if not valid or 1 if valid.
40 static inline int afb_req_x1_is_valid(struct afb_req_x1 req)
46 * @deprecated use bindings version 3
48 * Gets from the request 'req' the argument of 'name'.
49 * Returns a PLAIN structure of type 'struct afb_arg'.
50 * When the argument of 'name' is not found, all fields of result are set to NULL.
51 * When the argument of 'name' is found, the fields are filled,
52 * in particular, the field 'result.name' is set to 'name'.
54 * There is a special name value: the empty string.
55 * The argument of name "" is defined only if the request was made using
56 * an HTTP POST of Content-Type "application/json". In that case, the
57 * argument of name "" receives the value of the body of the HTTP request.
59 static inline struct afb_arg afb_req_x1_get(struct afb_req_x1 req, const char *name)
61 return req.itf->get(req.closure, name);
65 * @deprecated use bindings version 3
67 * Gets from the request 'req' the string value of the argument of 'name'.
68 * Returns NULL if when there is no argument of 'name'.
69 * Returns the value of the argument of 'name' otherwise.
71 * Shortcut for: afb_req_get(req, name).value
73 static inline const char *afb_req_x1_value(struct afb_req_x1 req, const char *name)
75 return afb_req_x1_get(req, name).value;
79 * @deprecated use bindings version 3
81 * Gets from the request 'req' the path for file attached to the argument of 'name'.
82 * Returns NULL if when there is no argument of 'name' or when there is no file.
83 * Returns the path of the argument of 'name' otherwise.
85 * Shortcut for: afb_req_get(req, name).path
87 static inline const char *afb_req_x1_path(struct afb_req_x1 req, const char *name)
89 return afb_req_x1_get(req, name).path;
93 * @deprecated use bindings version 3
95 * Gets from the request 'req' the json object hashing the arguments.
96 * The returned object must not be released using 'json_object_put'.
98 static inline struct json_object *afb_req_x1_json(struct afb_req_x1 req)
100 return req.itf->json(req.closure);
104 * @deprecated use bindings version 3
106 * Sends a reply to the request 'req'.
107 * The status of the reply is set to 'error' (that must be NULL on success).
108 * Its send the object 'obj' (can be NULL) with an
109 * informational comment 'info (can also be NULL).
111 * For convenience, the function calls 'json_object_put' for 'obj'.
112 * Thus, in the case where 'obj' should remain available after
113 * the function returns, the function 'json_object_get' shall be used.
115 static inline void afb_req_x1_reply(struct afb_req_x1 req, struct json_object *obj, const char *error, const char *info)
117 req.itf->reply(req.closure, obj, error, info);
121 * @deprecated use bindings version 3
123 * Same as 'afb_req_x1_reply' but the 'info' is a formatting
124 * string followed by arguments.
126 * For convenience, the function calls 'json_object_put' for 'obj'.
127 * Thus, in the case where 'obj' should remain available after
128 * the function returns, the function 'json_object_get' shall be used.
130 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)));
131 static inline void afb_req_x1_reply_f(struct afb_req_x1 req, struct json_object *obj, const char *error, const char *info, ...)
134 va_start(args, info);
135 req.itf->vreply(req.closure, obj, error, info, args);
140 * @deprecated use bindings version 3
142 * Same as 'afb_req_x1_reply_f' but the arguments to the format 'info'
143 * are given as a variable argument list instance.
145 * For convenience, the function calls 'json_object_put' for 'obj'.
146 * Thus, in the case where 'obj' should remain available after
147 * the function returns, the function 'json_object_get' shall be used.
149 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)
151 req.itf->vreply(req.closure, obj, error, info, args);
155 * @deprecated use bindings version 3
157 * Gets the pointer stored by the binding for the session of 'req'.
158 * When the binding has not yet recorded a pointer, NULL is returned.
160 static inline void *afb_req_x1_context_get(struct afb_req_x1 req)
162 return req.itf->context_make(req.closure, 0, 0, 0, 0);
166 * @deprecated use bindings version 3
168 * Stores for the binding the pointer 'context' to the session of 'req'.
169 * The function 'free_context' will be called when the session is closed
170 * or if binding stores an other pointer.
172 static inline void afb_req_x1_context_set(struct afb_req_x1 req, void *context, void (*free_context)(void*))
174 req.itf->context_make(req.closure, 1, 0, free_context, context);
178 * @deprecated use bindings version 3
180 * Gets the pointer stored by the binding for the session of 'req'.
181 * If the stored pointer is NULL, indicating that no pointer was
182 * already stored, afb_req_context creates a new context by calling
183 * the function 'create_context' and stores it with the freeing function
186 static inline void *afb_req_x1_context(struct afb_req_x1 req, void *(*create_context)(), void (*free_context)(void*))
188 return req.itf->context_make(req.closure, 0, (void *(*)(void*))(void*)create_context, free_context, 0);
192 * @deprecated use bindings version 3
194 * Gets the pointer stored by the binding for the session of 'request'.
195 * If no previous pointer is stored or if 'replace' is not zero, a new value
196 * is generated using the function 'create_context' called with the 'closure'.
197 * If 'create_context' is NULL the generated value is 'closure'.
198 * When a value is created, the function 'free_context' is recorded and will
199 * be called (with the created value as argument) to free the created value when
200 * it is not more used.
201 * This function is atomic: it ensures that 2 threads will not race together.
203 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)
205 return req.itf->context_make(req.closure, replace, create_context, free_context, closure);
209 * @deprecated use bindings version 3
211 * Frees the pointer stored by the binding for the session of 'req'
212 * and sets it to NULL.
214 * Shortcut for: afb_req_context_set(req, NULL, NULL)
216 static inline void afb_req_x1_context_clear(struct afb_req_x1 req)
218 req.itf->context_make(req.closure, 1, 0, 0, 0);
222 * @deprecated use bindings version 3
224 * Adds one to the count of references of 'req'.
225 * This function MUST be called by asynchronous implementations
226 * of verbs if no reply was sent before returning.
228 static inline void afb_req_x1_addref(struct afb_req_x1 req)
230 req.itf->addref(req.closure);
234 * @deprecated use bindings version 3
236 * Substracts one to the count of references of 'req'.
237 * This function MUST be called by asynchronous implementations
238 * of verbs after sending the asynchronous reply.
240 static inline void afb_req_x1_unref(struct afb_req_x1 req)
242 req.itf->unref(req.closure);
246 * @deprecated use bindings version 3
248 * Closes the session associated with 'req'
249 * and delete all associated contexts.
251 static inline void afb_req_x1_session_close(struct afb_req_x1 req)
253 req.itf->session_close(req.closure);
257 * @deprecated use bindings version 3
259 * Sets the level of assurance of the session of 'req'
260 * to 'level'. The effect of this function is subject of
262 * Returns 1 on success or 0 if failed.
264 static inline int afb_req_x1_session_set_LOA(struct afb_req_x1 req, unsigned level)
266 return 1 + req.itf->session_set_LOA(req.closure, level);
270 * @deprecated use bindings version 3
272 * Establishes for the client link identified by 'req' a subscription
274 * Returns 0 in case of successful subscription or -1 in case of error.
276 static inline int afb_req_x1_subscribe(struct afb_req_x1 req, struct afb_event_x1 event)
278 return req.itf->legacy_subscribe_event_x1(req.closure, event);
282 * @deprecated use bindings version 3
284 * Revokes the subscription established to the 'event' for the client
285 * link identified by 'req'.
286 * Returns 0 in case of successful subscription or -1 in case of error.
288 static inline int afb_req_x1_unsubscribe(struct afb_req_x1 req, struct afb_event_x1 event)
290 return req.itf->legacy_unsubscribe_event_x1(req.closure, event);
294 * @deprecated use bindings version 3
296 * Makes a call to the method of name 'api' / 'verb' with the object 'args'.
297 * This call is made in the context of the request 'req'.
298 * On completion, the function 'callback' is invoked with the
299 * 'closure' given at call and two other parameters: 'iserror' and 'result'.
300 * 'status' is 0 on success or negative when on an error reply.
301 * 'result' is the json object of the reply, you must not call json_object_put
304 * For convenience, the function calls 'json_object_put' for 'args'.
305 * Thus, in the case where 'args' should remain available after
306 * the function returns, the function 'json_object_get' shall be used.
309 * - 'afb_req_subcall_req' that is convenient to keep request alive automatically.
310 * - 'afb_req_subcall_sync' the synchronous version
312 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)
314 req.itf->legacy_subcall(req.closure, api, verb, args, callback, closure);
318 * @deprecated use bindings version 3
320 * Makes a call to the method of name 'api' / 'verb' with the object 'args'.
321 * This call is made in the context of the request 'req'.
322 * On completion, the function 'callback' is invoked with the
323 * original request 'req', the 'closure' given at call and two
324 * other parameters: 'iserror' and 'result'.
325 * 'status' is 0 on success or negative when on an error reply.
326 * 'result' is the json object of the reply, you must not call json_object_put
329 * For convenience, the function calls 'json_object_put' for 'args'.
330 * Thus, in the case where 'args' should remain available after
331 * the function returns, the function 'json_object_get' shall be used.
334 * - 'afb_req_subcall' that doesn't keep request alive automatically.
335 * - 'afb_req_subcall_sync' the synchronous version
337 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)
339 req.itf->legacy_subcall_req(req.closure, api, verb, args, callback, closure);
343 * @deprecated use bindings version 3
345 * Makes a call to the method of name 'api' / 'verb' with the object 'args'.
346 * This call is made in the context of the request 'req'.
347 * This call is synchronous, it waits untill completion of the request.
348 * It returns 0 on success or a negative value on error answer.
349 * The object pointed by 'result' is filled and must be released by the caller
350 * after its use by calling 'json_object_put'.
352 * For convenience, the function calls 'json_object_put' for 'args'.
353 * Thus, in the case where 'args' should remain available after
354 * the function returns, the function 'json_object_get' shall be used.
357 * - 'afb_req_subcall_req' that is convenient to keep request alive automatically.
358 * - 'afb_req_subcall' that doesn't keep request alive automatically.
360 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)
362 return req.itf->legacy_subcallsync(req.closure, api, verb, args, result);
366 * @deprecated use bindings version 3
368 * Send associated to 'req' a message described by 'fmt' and following parameters
369 * to the journal for the verbosity 'level'.
371 * 'file', 'line' and 'func' are indicators of position of the code in source files
372 * (see macros __FILE__, __LINE__ and __func__).
374 * 'level' is defined by syslog standard:
375 * EMERGENCY 0 System is unusable
376 * ALERT 1 Action must be taken immediately
377 * CRITICAL 2 Critical conditions
378 * ERROR 3 Error conditions
379 * WARNING 4 Warning conditions
380 * NOTICE 5 Normal but significant condition
381 * INFO 6 Informational
382 * DEBUG 7 Debug-level messages
384 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)));
385 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, ...)
389 req.itf->vverbose(req.closure, level, file, line, func, fmt, args);
394 * @deprecated use bindings version 3
396 * Check whether the 'permission' is granted or not to the client
397 * identified by 'req'.
399 * Returns 1 if the permission is granted or 0 otherwise.
401 static inline int afb_req_x1_has_permission(struct afb_req_x1 req, const char *permission)
403 return req.itf->has_permission(req.closure, permission);
407 * @deprecated use bindings version 3
409 * Get the application identifier of the client application for the
412 * Returns the application identifier or NULL when the application
413 * can not be identified.
415 * The returned value if not NULL must be freed by the caller
417 static inline char *afb_req_x1_get_application_id(struct afb_req_x1 req)
419 return req.itf->get_application_id(req.closure);
423 * @deprecated use bindings version 3
425 * Get the user identifier (UID) of the client application for the
428 * Returns -1 when the application can not be identified.
430 static inline int afb_req_x1_get_uid(struct afb_req_x1 req)
432 return req.itf->get_uid(req.closure);
436 * @deprecated use bindings version 3
438 * Get informations about the client of the
441 * Returns an object with client informations:
443 * "pid": int, "uid": int, "gid": int,
444 * "smack": string, "appid": string,
445 * "uuid": string, "LOA": int
448 static inline struct json_object *afb_req_x1_get_client_info(struct afb_req_x1 req)
450 return req.itf->get_client_info(req.closure);