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-x2-itf.h"
21 #include "afb-api-x3.h"
24 * Checks whether the request 'req' is valid or not.
26 * @param req the request to check
28 * @return 0 if not valid or 1 if valid.
31 int afb_req_x2_is_valid(
32 struct afb_req_x2 *req)
38 * Retrieves the api that serves the request
40 * @param req the request whose serving api is queried
42 * @return the api serving the request
45 struct afb_api_x3 *afb_req_x2_get_api(
46 struct afb_req_x2 *req)
52 * Retrieves the callback data of the verb. This callback data is set
53 * when the verb is created.
55 * @param req whose verb vcbdata is queried
57 * @return the callback data attached to the verb description
60 void *afb_req_x2_get_vcbdata(
61 struct afb_req_x2 *req)
67 * Retrieve the name of the called api.
69 * @param req the request
71 * @return the name of the called api
73 * @see afb_api_x3_add_alias
76 const char *afb_req_x2_get_called_api(
77 struct afb_req_x2 *req)
79 return req->called_api;
83 * Retrieve the name of the called verb
85 * @param req the request
87 * @return the name of the called verb
90 const char *afb_req_x2_get_called_verb(
91 struct afb_req_x2 *req)
93 return req->called_verb;
97 * Is the log message of 'level (as defined for syslog) required for the
100 * @param req the request
101 * @param level the level to check as defined for syslog:
103 * EMERGENCY 0 System is unusable
104 * ALERT 1 Action must be taken immediately
105 * CRITICAL 2 Critical conditions
106 * ERROR 3 Error conditions
107 * WARNING 4 Warning conditions
108 * NOTICE 5 Normal but significant condition
109 * INFO 6 Informational
110 * DEBUG 7 Debug-level messages
112 * @return 0 if not required or a value not null if required
117 int afb_req_x2_wants_log_level(
118 struct afb_req_x2 *req,
121 return afb_api_x3_wants_log_level(afb_req_x2_get_api(req), level);
125 * Gets from the request 'req' the argument of 'name'.
127 * Returns a PLAIN structure of type 'struct afb_arg'.
129 * When the argument of 'name' is not found, all fields of result are set to NULL.
131 * When the argument of 'name' is found, the fields are filled,
132 * in particular, the field 'result.name' is set to 'name'.
134 * There is a special name value: the empty string.
135 * The argument of name "" is defined only if the request was made using
136 * an HTTP POST of Content-Type "application/json". In that case, the
137 * argument of name "" receives the value of the body of the HTTP request.
139 * @param req the request
140 * @param name the name of the argument to get
142 * @return a structure describing the retrieved argument for the request
144 * @see afb_req_x2_value
145 * @see afb_req_x2_path
148 struct afb_arg afb_req_x2_get(
149 struct afb_req_x2 *req,
152 return req->itf->get(req, name);
156 * Gets from the request 'req' the string value of the argument of 'name'.
157 * Returns NULL if when there is no argument of 'name'.
158 * Returns the value of the argument of 'name' otherwise.
160 * Shortcut for: afb_req_x2_get(req, name).value
162 * @param req the request
163 * @param name the name of the argument's value to get
165 * @return the value as a string or NULL
167 * @see afb_req_x2_get
168 * @see afb_req_x2_path
171 const char *afb_req_x2_value(
172 struct afb_req_x2 *req,
175 return afb_req_x2_get(req, name).value;
179 * Gets from the request 'req' the path for file attached to the argument of 'name'.
180 * Returns NULL if when there is no argument of 'name' or when there is no file.
181 * Returns the path of the argument of 'name' otherwise.
183 * Shortcut for: afb_req_x2_get(req, name).path
185 * @param req the request
186 * @param name the name of the argument's path to get
188 * @return the path if any or NULL
190 * @see afb_req_x2_get
191 * @see afb_req_x2_value
194 const char *afb_req_x2_path(
195 struct afb_req_x2 *req,
198 return afb_req_x2_get(req, name).path;
202 * Gets from the request 'req' the json object hashing the arguments.
204 * The returned object must not be released using 'json_object_put'.
206 * @param req the request
208 * @return the JSON object of the query
210 * @see afb_req_x2_get
211 * @see afb_req_x2_value
212 * @see afb_req_x2_path
215 struct json_object *afb_req_x2_json(
216 struct afb_req_x2 *req)
218 return req->itf->json(req);
222 * Sends a reply to the request 'req'.
224 * The status of the reply is set to 'error' (that must be NULL on success).
225 * Its send the object 'obj' (can be NULL) with an
226 * informational comment 'info (can also be NULL).
228 * For convenience, the function calls 'json_object_put' for 'obj'.
229 * Thus, in the case where 'obj' should remain available after
230 * the function returns, the function 'json_object_get' shall be used.
232 * @param req the request
233 * @param obj the replied object or NULL
234 * @param error the error message if it is a reply error or NULL
235 * @param info an informative text or NULL
237 * @see afb_req_x2_reply_v
238 * @see afb_req_x2_reply_f
241 void afb_req_x2_reply(
242 struct afb_req_x2 *req,
243 struct json_object *obj,
247 req->itf->reply(req, obj, error, info);
251 * Same as 'afb_req_x2_reply_f' but the arguments to the format 'info'
252 * are given as a variable argument list instance.
254 * For convenience, the function calls 'json_object_put' for 'obj'.
255 * Thus, in the case where 'obj' should remain available after
256 * the function returns, the function 'json_object_get' shall be used.
258 * @param req the request
259 * @param obj the replied object or NULL
260 * @param error the error message if it is a reply error or NULL
261 * @param info an informative text containing a format as for vprintf
262 * @param args the va_list of arguments to the format as for vprintf
264 * @see afb_req_x2_reply
265 * @see afb_req_x2_reply_f
269 void afb_req_x2_reply_v(
270 struct afb_req_x2 *req,
271 struct json_object *obj,
276 req->itf->vreply(req, obj, error, info, args);
280 * Same as 'afb_req_x2_reply' but the 'info' is a formatting
281 * string followed by arguments.
283 * For convenience, the function calls 'json_object_put' for 'obj'.
284 * Thus, in the case where 'obj' should remain available after
285 * the function returns, the function 'json_object_get' shall be used.
287 * @param req the request
288 * @param obj the replied object or NULL
289 * @param error the error message if it is a reply error or NULL
290 * @param info an informative text containing a format as for printf
291 * @param ... the arguments to the format as for printf
293 * @see afb_req_x2_reply
294 * @see afb_req_x2_reply_v
297 __attribute__((format(printf, 4, 5)))
299 void afb_req_x2_reply_f(
300 struct afb_req_x2 *req,
301 struct json_object *obj,
307 va_start(args, info);
308 afb_req_x2_reply_v(req, obj, error, info, args);
313 * Manage the pointer stored by the binding for the client session of 'req'.
315 * If no previous pointer is stored or if 'replace' is not zero, a new value
316 * is generated using the function 'create_context' called with the 'closure'.
317 * If 'create_context' is NULL the generated value is 'closure'.
319 * When a value is created, the function 'free_context' is recorded and will
320 * be called (with the created value as argument) to free the created value when
321 * it is not more used.
323 * This function is atomic: it ensures that 2 threads will not race together.
325 * @param req the request
326 * @param replace if not zero an existing value is replaced
327 * @param create_context the creation function or NULL
328 * @param free_context the destroying function or NULL
329 * @param closure the closure to the creation function
331 * @return the stored value
334 void *afb_req_x2_context(
335 struct afb_req_x2 *req,
337 void *(*create_context)(void *closure),
338 void (*free_context)(void*),
341 return req->itf->context_make(req, replace, create_context, free_context, closure);
345 * Gets the pointer stored by the binding for the session of 'req'.
346 * When the binding has not yet recorded a pointer, NULL is returned.
348 * Shortcut for: afb_req_x2_context(req, 0, NULL, NULL, NULL)
350 * @param req the request
352 * @return the previously stored value
355 void *afb_req_x2_context_get(
356 struct afb_req_x2 *req)
358 return afb_req_x2_context(req, 0, 0, 0, 0);
362 * Stores for the binding the pointer 'context' to the session of 'req'.
363 * The function 'free_context' will be called when the session is closed
364 * or if binding stores an other pointer.
366 * Shortcut for: afb_req_x2_context(req, 1, NULL, free_context, context)
369 * @param req the request
370 * @param context the context value to store
371 * @param free_context the cleaning function for the stored context (can be NULL)
374 void afb_req_x2_context_set(
375 struct afb_req_x2 *req,
377 void (*free_context)(void*))
379 afb_req_x2_context(req, 1, 0, free_context, context);
383 * Frees the pointer stored by the binding for the session of 'req'
384 * and sets it to NULL.
386 * Shortcut for: afb_req_x2_context_set(req, NULL, NULL)
388 * @param req the request
391 void afb_req_x2_context_clear(
392 struct afb_req_x2 *req)
394 afb_req_x2_context(req, 1, 0, 0, 0);
398 * Increments the count of references of 'req'.
400 * @param req the request
402 * @return returns the request req
405 struct afb_req_x2 *afb_req_x2_addref(
406 struct afb_req_x2 *req)
408 return req->itf->addref(req);
412 * Decrement the count of references of 'req'.
414 * @param req the request
417 void afb_req_x2_unref(
418 struct afb_req_x2 *req)
420 req->itf->unref(req);
424 * Closes the session associated with 'req'
425 * and delete all associated contexts.
427 * @param req the request
430 void afb_req_x2_session_close(
431 struct afb_req_x2 *req)
433 req->itf->session_close(req);
437 * Sets the level of assurance of the session of 'req'
438 * to 'level'. The effect of this function is subject of
441 * @param req the request
442 * @param level of assurance from 0 to 7
444 * @return 0 on success or -1 if failed.
447 int afb_req_x2_session_set_LOA(
448 struct afb_req_x2 *req,
451 return req->itf->session_set_LOA(req, level);
455 * Establishes for the client link identified by 'req' a subscription
458 * @param req the request
459 * @param event the event to subscribe
461 * @return 0 in case of successful subscription or -1 in case of error.
464 int afb_req_x2_subscribe(
465 struct afb_req_x2 *req,
466 struct afb_event_x2 *event)
468 return req->itf->subscribe_event_x2(req, event);
472 * Revokes the subscription established to the 'event' for the client
473 * link identified by 'req'.
474 * Returns 0 in case of successful subscription or -1 in case of error.
476 * @param req the request
477 * @param event the event to revoke
479 * @return 0 in case of successful subscription or -1 in case of error.
482 int afb_req_x2_unsubscribe(
483 struct afb_req_x2 *req,
484 struct afb_event_x2 *event)
486 return req->itf->unsubscribe_event_x2(req, event);
490 * @deprecated use @ref afb_req_x2_subcall
492 * Makes a call to the method of name 'api' / 'verb' with the object 'args'.
493 * This call is made in the context of the request 'req'.
494 * On completion, the function 'callback' is invoked with the
495 * 'closure' given at call and two other parameters: 'iserror' and 'result'.
496 * 'status' is 0 on success or negative when on an error reply.
497 * 'result' is the json object of the reply, you must not call json_object_put
500 * For convenience, the function calls 'json_object_put' for 'args'.
501 * Thus, in the case where 'args' should remain available after
502 * the function returns, the function 'json_object_get' shall be used.
504 * @param req the request
505 * @param api the name of the api to call
506 * @param verb the name of the verb to call
507 * @param args the arguments of the call as a JSON object
508 * @param callback the call back that will receive the reply
509 * @param closure the closure passed back to the callback
511 * @see afb_req_x2_subcall
512 * @see afb_req_x2_subcall_sync
515 void afb_req_x2_subcall_legacy(
516 struct afb_req_x2 *req,
519 struct json_object *args,
520 void (*callback)(void *closure, int iserror, struct json_object *result, struct afb_req_x2 *req),
523 req->itf->legacy_subcall_request(req, api, verb, args, callback, closure);
527 * @deprecated use @ref afb_req_x2_subcall_sync
529 * Makes a call to the method of name 'api' / 'verb' with the object 'args'.
530 * This call is made in the context of the request 'req'.
531 * This call is synchronous, it waits untill completion of the request.
532 * It returns 0 on success or a negative value on error answer.
533 * The object pointed by 'result' is filled and must be released by the caller
534 * after its use by calling 'json_object_put'.
536 * For convenience, the function calls 'json_object_put' for 'args'.
537 * Thus, in the case where 'args' should remain available after
538 * the function returns, the function 'json_object_get' shall be used.
540 * @param req the request
541 * @param api the name of the api to call
542 * @param verb the name of the verb to call
543 * @param args the arguments of the call as a JSON object
544 * @param result the pointer to the JSON object pointer that will receive the result
546 * @return 0 on success or a negative value on error answer.
548 * @see afb_req_x2_subcall
549 * @see afb_req_x2_subcall_sync
552 int afb_req_x2_subcall_sync_legacy(
553 struct afb_req_x2 *req,
556 struct json_object *args,
557 struct json_object **result)
559 return req->itf->legacy_subcallsync(req, api, verb, args, result);
563 * Send associated to 'req' a message described by 'fmt' and following parameters
564 * to the journal for the verbosity 'level'.
566 * 'file', 'line' and 'func' are indicators of position of the code in source files
567 * (see macros __FILE__, __LINE__ and __func__).
569 * 'level' is defined by syslog standard:
570 * EMERGENCY 0 System is unusable
571 * ALERT 1 Action must be taken immediately
572 * CRITICAL 2 Critical conditions
573 * ERROR 3 Error conditions
574 * WARNING 4 Warning conditions
575 * NOTICE 5 Normal but significant condition
576 * INFO 6 Informational
577 * DEBUG 7 Debug-level messages
579 * @param req the request
580 * @param level the level of the message
581 * @param file the source filename that emits the message or NULL
582 * @param line the line number in the source filename that emits the message
583 * @param func the name of the function that emits the message or NULL
584 * @param fmt the message format as for printf
585 * @param ... the arguments of the printf
589 __attribute__((format(printf, 6, 7)))
591 void afb_req_x2_verbose(
592 struct afb_req_x2 *req,
593 int level, const char *file,
601 req->itf->vverbose(req, level, file, line, func, fmt, args);
606 * Check whether the 'permission' is granted or not to the client
607 * identified by 'req'.
609 * @param req the request
610 * @param permission string to check
612 * @return 1 if the permission is granted or 0 otherwise.
615 int afb_req_x2_has_permission(
616 struct afb_req_x2 *req,
617 const char *permission)
619 return req->itf->has_permission(req, permission);
623 * Get the application identifier of the client application for the
626 * Returns the application identifier or NULL when the application
627 * can not be identified.
629 * The returned value if not NULL must be freed by the caller
631 * @param req the request
633 * @return the string for the application id of the client MUST BE FREED
636 char *afb_req_x2_get_application_id(
637 struct afb_req_x2 *req)
639 return req->itf->get_application_id(req);
643 * Get the user identifier (UID) of the client for the
646 * @param req the request
648 * @return -1 when the application can not be identified or the unix uid.
652 int afb_req_x2_get_uid(
653 struct afb_req_x2 *req)
655 return req->itf->get_uid(req);
659 * Get informations about the client of the
662 * Returns an object with client informations:
664 * "pid": int, "uid": int, "gid": int,
665 * "label": string, "id": string, "user": string,
666 * "uuid": string, "LOA": int
669 * If some of this information can't be computed, the field of the return
670 * object will not be set at all.
672 * @param req the request
674 * @return a JSON object that must be freed using @ref json_object_put
677 struct json_object *afb_req_x2_get_client_info(
678 struct afb_req_x2 *req)
680 return req->itf->get_client_info(req);
684 * Calls the 'verb' of the 'api' with the arguments 'args' and 'verb' in the name of the binding.
685 * The result of the call is delivered to the 'callback' function with the 'callback_closure'.
687 * For convenience, the function calls 'json_object_put' for 'args'.
688 * Thus, in the case where 'args' should remain available after
689 * the function returns, the function 'json_object_get' shall be used.
691 * The 'callback' receives 5 arguments:
692 * 1. 'closure' the user defined closure pointer 'closure',
693 * 2. 'object' a JSON object returned (can be NULL)
694 * 3. 'error' a string not NULL in case of error
695 * 4. 'info' a string handling some info (can be NULL)
698 * @param req The request
699 * @param api The api name of the method to call
700 * @param verb The verb name of the method to call
701 * @param args The arguments to pass to the method
702 * @param flags The bit field of flags for the subcall as defined by @ref afb_req_x2_subcall_flags
703 * @param callback The to call on completion
704 * @param closure The closure to pass to the callback
706 * @see also 'afb_req_subcall_sync'
709 void afb_req_x2_subcall(
710 struct afb_req_x2 *req,
713 struct json_object *args,
715 void (*callback)(void *closure, struct json_object *object, const char *error, const char * info, struct afb_req_x2 *req),
718 req->itf->subcall(req, api, verb, args, flags, callback, closure);
722 * Makes a call to the method of name 'api' / 'verb' with the object 'args'.
723 * This call is made in the context of the request 'req'.
724 * This call is synchronous, it waits untill completion of the request.
725 * It returns 0 on success or a negative value on error answer.
727 * For convenience, the function calls 'json_object_put' for 'args'.
728 * Thus, in the case where 'args' should remain available after
729 * the function returns, the function 'json_object_get' shall be used.
732 * - 'afb_req_x2_subcall_req' that is convenient to keep request alive automatically.
733 * - 'afb_req_x2_subcall' that doesn't keep request alive automatically.
735 * @param req The request
736 * @param api The api name of the method to call
737 * @param verb The verb name of the method to call
738 * @param args The arguments to pass to the method
739 * @param flags The bit field of flags for the subcall as defined by @ref afb_req_x2_subcall_flags
740 * @param object a pointer where the replied JSON object is stored must be freed using @ref json_object_put (can be NULL)
741 * @param error a pointer where a copy of the replied error is stored must be freed using @ref free (can be NULL)
742 * @param info a pointer where a copy of the replied info is stored must be freed using @ref free (can be NULL)
744 * @return 0 in case of success or -1 in case of error
747 int afb_req_x2_subcall_sync(
748 struct afb_req_x2 *req,
751 struct json_object *args,
753 struct json_object **object,
757 return req->itf->subcallsync(req, api, verb, args, flags, object, error, info);