3 title: Functions of class afb_api
5 https://git.automotivelinux.org/src/app-framework-binder/plain/docs/reference-v3/func-api.md?h=master
8 <!-- WARNING: This file is generated by fetch_docs.js using /home/boron/Documents/AGL/docs-webtemplate/site/_data/tocs/apis_services/master/app-framework-binder-developer-guides-api-services-book.yml -->
10 Functions of class **afb_api**
11 ============================
19 * Get the name of the 'api'.
21 * @param api the api whose name is to be returned
23 * @return the name of the api.
25 * The returned value must not be changed nor freed.
27 const char *afb_api_name(
31 ### afb_api_get_userdata
35 * Get the "userdata" pointer of the 'api'
37 * @param api the api whose user's data is to be returned
39 * @return the user's data pointer of the api.
41 * @see afb_api_set_userdata
43 void *afb_api_get_userdata(
47 ### afb_api_set_userdata
51 * Set the "userdata" pointer of the 'api' to 'value'
53 * @param api the api whose user's data is to be set
54 * @param value the data to set
56 * @see afb_api_get_userdata
58 void afb_api_set_userdata(
63 ### afb_api_require_api
67 * Check that it requires the API of 'name'.
68 * If 'initialized' is not zero it requests the API to be
69 * initialized, implying its initialization if needed.
71 * Calling this function is only allowed within init.
73 * A single request allows to require multiple apis.
75 * @param api the api that requires the other api by its name
76 * @param name a space separated list of required api names
77 * @param initialized if zero, the api is just required to exist. If not zero,
78 * the api is required to exist and to be initialized at return of the call
79 * (initializing it if needed and possible as a side effect of the call).
81 * @return 0 in case of success or -1 in case of error with errno set appropriately.
83 int afb_api_require_api(
90 ## Verbosity functions
92 ### afb_api_wants_log_level
96 * Is the log message of 'level (as defined for syslog) required for the api?
98 * @param api the api to check
99 * @param level the level to check as defined for syslog:
101 * EMERGENCY 0 System is unusable
102 * ALERT 1 Action must be taken immediately
103 * CRITICAL 2 Critical conditions
104 * ERROR 3 Error conditions
105 * WARNING 4 Warning conditions
106 * NOTICE 5 Normal but significant condition
107 * INFO 6 Informational
108 * DEBUG 7 Debug-level messages
110 * @return 0 if not required or a value not null if required
114 int afb_api_wants_log_level(
123 * Send to the journal with the logging 'level' a message described
124 * by 'fmt' applied to the va-list 'args'.
126 * 'file', 'line' and 'func' are indicators of code position in source files
127 * (see macros __FILE__, __LINE__ and __func__).
129 * 'level' is defined by syslog standard:
131 * EMERGENCY 0 System is unusable
132 * ALERT 1 Action must be taken immediately
133 * CRITICAL 2 Critical conditions
134 * ERROR 3 Error conditions
135 * WARNING 4 Warning conditions
136 * NOTICE 5 Normal but significant condition
137 * INFO 6 Informational
138 * DEBUG 7 Debug-level messages
140 * @param api the api that collects the logging message
141 * @param level the level of the message
142 * @param file the source file that logs the messages or NULL
143 * @param line the line in the source file that logs the message
144 * @param func the name of the function in the source file that logs
145 * @param fmt the format of the message as in printf
146 * @param args the arguments to the format string of the message as a va_list
151 void afb_api_vverbose(
165 * Send to the journal with the log 'level' a message described
166 * by 'fmt' and following parameters.
168 * 'file', 'line' and 'func' are indicators of position of the code in source files
169 * (see macros __FILE__, __LINE__ and __func__).
171 * 'level' is defined by syslog standard:
172 * EMERGENCY 0 System is unusable
173 * ALERT 1 Action must be taken immediately
174 * CRITICAL 2 Critical conditions
175 * ERROR 3 Error conditions
176 * WARNING 4 Warning conditions
177 * NOTICE 5 Normal but significant condition
178 * INFO 6 Informational
179 * DEBUG 7 Debug-level messages
181 * @param api the api that collects the logging message
182 * @param level the level of the message
183 * @param file the source file that logs the messages or NULL
184 * @param line the line in the source file that logs the message
185 * @param func the name of the function in the source file that logs
186 * @param fmt the format of the message as in printf
187 * @param ... the arguments to the format string of the message
192 void afb_api_verbose(
202 ## Data retrieval functions
204 ### afb_api_rootdir_get_fd
208 * Get the root directory file descriptor. This file descriptor can
209 * be used with functions 'openat', 'fstatat', ...
211 * CAUTION, manipulate this descriptor with care, in particular, don't close
214 * This can be used to get the path of the root directory using:
216 * char buffer[MAX_PATH], proc[100];
217 * int dirfd = afb_api_rootdir_get_fd(api);
218 * snprintf(proc, sizeof proc, "/proc/self/fd/%d", dirfd);
219 * readlink(proc, buffer, sizeof buffer);
221 * But note that within AGL this is the value given by the environment variable
222 * AFM_APP_INSTALL_DIR.
224 * @param api the api that uses the directory file descriptor
226 * @return the file descriptor of the root directory.
228 * @see afb_api_rootdir_open_locale
230 int afb_api_rootdir_get_fd(
234 ### afb_api_rootdir_open_locale
238 * Opens 'filename' within the root directory with 'flags' (see function openat)
239 * using the 'locale' definition (example: "jp,en-US") that can be NULL.
241 * The filename must be relative to the root of the bindings.
243 * The opening mode must be for read or write but not for O_CREAT.
245 * The definition of locales and of how files are searched can be checked
246 * here: https://www.w3.org/TR/widgets/#folder-based-localization
247 * and https://tools.ietf.org/html/rfc7231#section-5.3.5
249 * @param api the api that queries the file
250 * @param filename the relative path to the file to open
251 * @param flags the flags for opening as for C function 'open'
252 * @param locale string indicating how to search content, can be NULL
254 * @return the file descriptor or -1 in case of error and errno is set with the
258 * @see afb_api_rootdir_get_fd
260 int afb_api_rootdir_open_locale(
262 const char *filename,
271 * Settings of the api.
273 * Get the settings of the API. The settings are recorded
274 * as a JSON object. The returned object should not be modified.
275 * It MUST NOT be released using json_object_put.
277 * @param api the api whose settings are required
279 * @returns The setting object.
281 struct json_object *afb_api_settings(
282 struct afb_api_x3 *api);
285 ## Calls and job functions
291 * Calls the 'verb' of the 'apiname' with the arguments 'args' and 'verb' in the name of the binding 'api'.
292 * The result of the call is delivered to the 'callback' function with the 'callback_closure'.
294 * For convenience, the function calls 'json_object_put' for 'args'.
295 * Thus, in the case where 'args' should remain available after
296 * the function returns, the function 'json_object_get' shall be used.
298 * The 'callback' receives 5 arguments:
299 * 1. 'closure' the user defined closure pointer 'closure',
300 * 2. 'object' a JSON object returned (can be NULL)
301 * 3. 'error' a string not NULL in case of error but NULL on success
302 * 4. 'info' a string handling some info (can be NULL)
305 * NOTE: For convenience, *json_object_put* is called on 'object' after the
306 * callback returns. So, it is wrong to call *json_object_put* in the callback.
308 * @param api The api that makes the call
309 * @param apiname The api name of the method to call
310 * @param verb The verb name of the method to call
311 * @param args The arguments to pass to the method
312 * @param callback The to call on completion
313 * @param closure The closure to pass to the callback
316 * @see afb_req_subcall
317 * @see afb_req_subcall_sync
318 * @see afb_api_call_sync
324 struct json_object *args,
327 struct json_object *object,
334 ### afb_api_call_sync
338 * Calls the 'verb' of the 'apiname' with the arguments 'args' and 'verb' in the name of the binding 'api'.
339 * 'result' will receive the response.
341 * For convenience, the function calls 'json_object_put' for 'args'.
342 * Thus, in the case where 'args' should remain available after
343 * the function returns, the function 'json_object_get' shall be used.
345 * @param api The api that makes the call
346 * @param apiname The api name of the method to call
347 * @param verb The verb name of the method to call
348 * @param args The arguments to pass to the method
349 * @param object Where to store the returned object - should call json_object_put on it - can be NULL
350 * @param error Where to store the copied returned error - should call free on it - can be NULL
351 * @param info Where to store the copied returned info - should call free on it - can be NULL
353 * @returns 0 in case of success or a negative value in case of error.
355 * @see afb_req_subcall
356 * @see afb_req_subcall_sync
359 int afb_api_call_sync(
363 struct json_object *args,
364 struct json_object **object,
369 ### afb_api_queue_job
373 * Queue the job defined by 'callback' and 'argument' for being executed asynchronously
374 * in this thread (later) or in an other thread.
376 * If 'group' is not NULL, the jobs queued with a same value (as the pointer value 'group')
377 * are executed in sequence in the order of there submission.
379 * If 'timeout' is not 0, it represent the maximum execution time for the job in seconds.
380 * At first, the job is called with 0 as signum and the given argument.
382 * The job is executed with the monitoring of its time and some signals like SIGSEGV and
383 * SIGFPE. When a such signal is catched, the job is terminated and reexecuted but with
384 * signum being the signal number (SIGALRM when timeout expired).
386 * When executed, the callback function receives 2 arguments:
388 * - int signum: the signal catched if any or zero at the beginning
389 * - void *arg: the parameter 'argument'
391 * A typical implementation of the job callback is:
393 * void my_job_cb(int signum, void *arg)
395 * struct myarg_t *myarg = arg;
397 * AFB_API_ERROR(myarg->api, "job interrupted with signal %s", strsignal(signum));
399 * really_do_my_job(myarg);
402 * @param api the api that queue the job
403 * @param callback the job as a callback function
404 * @param argument the argument to pass to the queued job
405 * @param group the group of the job, NULL if no group
406 * @param timeout the timeout of execution of the job
408 * @return 0 in case of success or -1 in case of error with errno set appropriately.
410 int afb_api_queue_job(
412 void (*callback)(int signum, void *arg),
420 ### afb_api_broadcast_event
424 * Broadcasts widely the event of 'name' with the data 'object'.
425 * 'object' can be NULL.
427 * For convenience, the function calls 'json_object_put' for 'object'.
428 * Thus, in the case where 'object' should remain available after
429 * the function returns, the function 'json_object_get' shall be used.
431 * Calling this function is only forbidden during preinit.
433 * The event sent as the name API/name where API is the name of the
436 * @param api the api that broadcast the event
437 * @param name the event name suffix
438 * @param object the object that comes with the event
440 * @return 0 in case of success or -1 in case of error
442 int afb_api_broadcast_event(
445 struct json_object *object);
448 ### afb_api_make_event
452 * Creates an event of 'name' and returns it.
454 * Calling this function is only forbidden during preinit.
456 * See afb_event_is_valid to check if there is an error.
458 * The event created as the name API/name where API is the name of the
461 * @param api the api that creates the event
462 * @param name the event name suffix
464 * @return the created event. Use the function afb_event_is_valid to check
465 * whether the event is valid (created) or not (error as reported by errno).
467 * @see afb_event_is_valid
469 afb_event_t afb_api_make_event(
474 ### afb_api_event_handler_add
478 * Add a specific event handler for the api
480 * The handler callback is called when an event matching the given pattern
481 * is received (it is received if broadcasted or after subscription through
482 * a call or a subcall).
484 * The handler callback receive 4 arguments:
486 * - the closure given here
487 * - the event full name
488 * - the companion JSON object of the event
489 * - the api that subscribed the event
491 * @param api the api that creates the handler
492 * @param pattern the global pattern of the event to handle
493 * @param callback the handler callback function
494 * @param closure the closure of the handler
496 * @return 0 in case of success or -1 on failure with errno set
498 * @see afb_api_on_event
499 * @see afb_api_event_handler_del
501 int afb_api_event_handler_add(
512 ### afb_api_event_handler_del
516 * Delete a specific event handler for the api
518 * @param api the api that the handler belongs to
519 * @param pattern the global pattern of the handler to remove
520 * @param closure if not NULL it will receive the closure set to the handler
522 * @return 0 in case of success or -1 on failure with errno set
524 * @see afb_api_on_event
525 * @see afb_api_event_handler_add
527 int afb_api_event_handler_del(
536 ### afb_api_get_event_loop
540 * Retrieves the common systemd's event loop of AFB
542 * @param api the api that uses the event loop
544 * @return the systemd event loop if active, NULL otherwise
546 * @see afb_api_get_user_bus
547 * @see afb_api_get_system_bus
549 struct sd_event *afb_api_get_event_loop(
553 ### afb_api_get_user_bus
557 * Retrieves the common systemd's user/session d-bus of AFB
559 * @param api the api that uses the user dbus
561 * @return the systemd user connection to dbus if active, NULL otherwise
563 * @see afb_api_get_event_loop
564 * @see afb_api_get_system_bus
566 struct sd_bus *afb_api_get_user_bus(
570 ### afb_api_get_system_bus
574 * Retrieves the common systemd's system d-bus of AFB
576 * @param api the api that uses the system dbus
578 * @return the systemd system connection to dbus if active, NULL otherwise
580 * @see afb_api_get_event_loop
581 * @see afb_api_get_user_bus
583 struct sd_bus *afb_api_get_system_bus(
588 ## Dynamic api functions
594 * Creates a new api of name 'apiname' briefly described by 'info' (that can
597 * When the pre-initialization function is given, it is a function that
598 * receives 2 parameters:
600 * - the closure as given in the call
601 * - the created api that can be initialised
603 * This pre-initialization function must return a negative value to abort
604 * the creation of the api. Otherwise, it returns a non-negative value to
607 * @param api the api that creates the other one
608 * @param apiname the name of the new api
609 * @param info the brief description of the new api (can be NULL)
610 * @param noconcurrency zero or not zero whether the new api is reentrant or not
611 * @param preinit the pre-initialization function if any (can be NULL)
612 * @param closure the closure for the pre-initialization preinit
614 * @return the created api in case of success or NULL on error
616 * @see afb_api_delete_api
618 afb_api_t afb_api_new_api(
623 int (*preinit)(void*, afb_api_t ),
627 ### afb_api_set_verbs_v2
631 * @deprecated use @ref afb_api_set_verbs_v3 instead
633 * Set the verbs of the 'api' using description of verbs of the api v2
635 * @param api the api that will get the verbs
636 * @param verbs the array of verbs to add terminated with an item with name=NULL
638 * @return 0 in case of success or -1 on failure with errno set
641 * @see afb_api_add_verb
642 * @see afb_api_set_verbs_v3
644 int afb_api_set_verbs_v2(
646 const struct afb_verb_v2 *verbs);
649 ### afb_api_set_verbs_v3
653 * Set the verbs of the 'api' using description of verbs of the api v2
655 * @param api the api that will get the verbs
656 * @param verbs the array of verbs to add terminated with an item with name=NULL
658 * @return 0 in case of success or -1 on failure with errno set
661 * @see afb_api_add_verb
662 * @see afb_api_del_verb
664 int afb_api_set_verbs_v3(
666 const struct afb_verb_v3 *verbs);
673 * Add one verb to the dynamic set of the api
675 * @param api the api to change
676 * @param verb name of the verb
677 * @param info brief description of the verb, can be NULL
678 * @param callback callback function implementing the verb
679 * @param vcbdata data for the verb callback, available through req
680 * @param auth required authorization, can be NULL
681 * @param session authorization and session requirements of the verb
682 * @param glob is the verb glob name
684 * @return 0 in case of success or -1 on failure with errno set
687 * @see afb_api_del_verb
688 * @see afb_api_set_verbs_v3
689 * @see fnmatch for matching names using glob
691 int afb_api_add_verb(
695 void (*callback)(struct afb_req_x2 *req),
697 const struct afb_auth *auth,
706 * Delete one verb from the dynamic set of the api
708 * @param api the api to change
709 * @param verb name of the verb to delete
710 * @param vcbdata if not NULL will receive the vcbdata of the deleted verb
712 * @return 0 in case of success or -1 on failure with errno set
714 * @see afb_api_add_verb
716 int afb_api_del_verb(
726 * Set the callback 'onevent' to process events in the name of the 'api'.
728 * This setting can be done statically using the global variable
731 * This function replace any previously global event callback set.
733 * When an event is received, the callback 'onevent' is called with 3 parameters
735 * - the api that recorded the event handler
736 * - the full name of the event
737 * - the companion JSON object of the event
739 * @param api the api that wants to process events
740 * @param onevent the callback function that will process events (can be NULL
741 * to remove event callback)
743 * @return 0 in case of success or -1 on failure with errno set
746 * @see afb_binding_v3
747 * @see afb_api_event_handler_add
748 * @see afb_api_event_handler_del
750 int afb_api_on_event(
755 struct json_object *object));
762 * Set the callback 'oninit' to process initialization of the 'api'.
764 * This setting can be done statically using the global variable
767 * This function replace any previously initialization callback set.
769 * This function is only valid during the pre-initialization stage.
771 * The callback initialization function will receive one argument: the api
774 * @param api the api that wants to process events
775 * @param oninit the callback function that initialize the api
777 * @return 0 in case of success or -1 on failure with errno set
780 * @see afb_binding_v3
784 int (*oninit)(afb_api_t api));
787 ### afb_api_provide_class
791 * Tells that the api provides a class of features. Classes are intended to
792 * allow ordering of initializations: apis that provides a given class are
793 * initialized before apis requiring it.
795 * This function is only valid during the pre-initialization stage.
797 * @param api the api that provides the classes
798 * @param name a space separated list of the names of the provided classes
800 * @returns 0 in case of success or a negative value in case of error.
802 * @see afb_api_require_class
804 int afb_api_provide_class(
809 ### afb_api_require_class
813 * Tells that the api requires a set of class features. Classes are intended to
814 * allow ordering of initializations: apis that provides a given class are
815 * initialized before apis requiring it.
817 * This function is only valid during the pre-initialization stage.
819 * @param api the api that requires the classes
820 * @param name a space separated list of the names of the required classes
822 * @returns 0 in case of success or a negative value in case of error.
824 * @see afb_api_provide_class
826 int afb_api_require_class(
835 * Seal the api. After a call to this function the api can not be modified
838 * @param api the api to be sealed
844 ### afb_api_delete_api
848 * Delete the given api.
850 * It is of the responsibility of the caller to don't used the deleted api
851 * anymore after this function returned.
853 * @param api the api to delete
855 * @returns 0 in case of success or a negative value in case of error.
857 * @see afb_api_new_api
859 int afb_api_delete_api(
863 ### afb_api_add_alias
867 * Create an aliased name 'as_name' for the api 'name'.
868 * Calling this function is only allowed within preinit.
870 * @param api the api that requires the aliasing
871 * @param name the api to alias
872 * @param as_name the aliased name of the aliased api
874 * @return 0 in case of success or -1 in case of error with errno set appropriately.
876 int afb_api_add_alias(
879 const char *as_name);
885 The function for legacy calls are still provided for some time because
886 adaptation of existing code to the new call functions require a small amount
889 ### afb_api_call_legacy
893 * @deprecated try to use @ref afb_api_call instead
895 * Calls the 'verb' of the 'apiname' with the arguments 'args' and 'verb'
896 * in the name of the binding.
897 * The result of the call is delivered to the 'callback' function
898 * with the 'callback_closure'.
900 * For convenience, the function calls 'json_object_put' for 'args'.
901 * Thus, in the case where 'args' should remain available after
902 * the function returns, the function 'json_object_get' shall be used.
904 * The 'callback' receives 3 arguments:
905 * 1. 'closure' the user defined closure pointer 'closure',
906 * 2. 'status' a status being 0 on success or negative when an error occurred,
907 * 2. 'result' the resulting data as a JSON object.
910 * @param apiname The api name of the method to call
911 * @param verb The verb name of the method to call
912 * @param args The arguments to pass to the method
913 * @param callback The to call on completion
914 * @param closure The closure to pass to the callback
916 * @see also 'afb_api_call'
917 * @see also 'afb_api_call_sync'
918 * @see also 'afb_api_call_sync_legacy'
919 * @see also 'afb_req_subcall'
920 * @see also 'afb_req_subcall_sync'
922 void afb_api_call_legacy(
926 struct json_object *args,
930 struct json_object *result,
935 ### afb_api_call_sync_legacy
939 * @deprecated try to use @ref afb_api_call_sync instead
941 * Calls the 'verb' of the 'apiname' with the arguments 'args' and 'verb'
942 * in the name of the binding.
943 * 'result' will receive the response.
945 * For convenience, the function calls 'json_object_put' for 'args'.
946 * Thus, in the case where 'args' should remain available after
947 * the function returns, the function 'json_object_get' shall be used.
950 * @param apiname The api name of the method to call
951 * @param verb The verb name of the method to call
952 * @param args The arguments to pass to the method
953 * @param result Where to store the result - should call json_object_put on it -
955 * @returns 0 in case of success or a negative value in case of error.
957 * @see also 'afb_api_call'
958 * @see also 'afb_api_call_sync'
959 * @see also 'afb_api_call_legacy'
960 * @see also 'afb_req_subcall'
961 * @see also 'afb_req_subcall_sync'
963 int afb_api_call_sync_legacy(
967 struct json_object *args,
968 struct json_object **result);