3 ## Structure for declaring binding
7 The main structure, of type **afb_binding_t**, for describing the binding
8 must be exported under the name **afbBindingExport**.
10 This structure is defined as below.
13 typedef struct afb_binding_v3 afb_binding_t;
20 * Description of the bindings of type version 3
24 /** api name for the binding, can't be NULL */
27 /** textual specification of the binding, can be NULL */
28 const char *specification;
30 /** some info about the api, can be NULL */
33 /** array of descriptions of verbs terminated by a NULL name, can be NULL */
34 const struct afb_verb_v3 *verbs;
36 /** callback at load of the binding */
37 int (*preinit)(struct afb_api_x3 *api);
39 /** callback for starting the service */
40 int (*init)(struct afb_api_x3 *api);
42 /** callback for handling events */
43 void (*onevent)(struct afb_api_x3 *api, const char *event, struct json_object *object);
45 /** userdata for afb_api_x3 */
48 /** space separated list of provided class(es) */
49 const char *provide_class;
51 /** space separated list of required class(es) */
52 const char *require_class;
54 /** space separated list of required API(es) */
55 const char *require_api;
57 /** avoids concurrent requests to verbs */
58 unsigned noconcurrency: 1;
64 Each verb is described with a structure of type **afb_verb_t**
68 typedef struct afb_verb_v3 afb_verb_t;
73 * Description of one verb as provided for binding API version 3
77 /** name of the verb, NULL only at end of the array */
80 /** callback function implementing the verb */
81 void (*callback)(afb_req_t_x2 *req);
83 /** required authorization, can be NULL */
84 const struct afb_auth *auth;
86 /** some info about the verb, can be NULL */
89 /**< data for the verb callback */
92 /** authorization and session requirements of the verb */
95 /** is the verb glob name */
100 The **session** flags is one of the constant defined below:
102 - AFB_SESSION_NONE : no flag, synonym to 0
103 - AFB_SESSION_LOA_0 : Requires the LOA to be 0 or more, synonym to 0 or AFB_SESSION_NONE
104 - AFB_SESSION_LOA_1 : Requires the LOA to be 1 or more
105 - AFB_SESSION_LOA_2 : Requires the LOA to be 2 or more
106 - AFB_SESSION_LOA_3 : Requires the LOA to be 3 or more
107 - AFB_SESSION_CHECK : Requires the token to be set and valid
108 - AFB_SESSION_REFRESH : Implies a token refresh
109 - AFB_SESSION_CLOSE : Implies closing the session after request processed
111 The LOA (Level Of Assurance) is set, by binding api, using the function **afb_req_session_set_LOA**.
113 The session can be closed, by binding api, using the function **afb_req_session_close**.
115 ### afb_auth_t and afb_auth_type_t
117 The structure **afb_auth_t** is used within verb description to
118 set security requirements.
119 The interpretation of the structure depends on the value of the field **type**.
122 typedef struct afb_auth afb_auth_t;
125 * Definition of an authorization entry
129 /** type of entry @see afb_auth_type */
130 enum afb_auth_type type;
133 /** text when @ref type == @ref afb_auth_Permission */
136 /** level of assurancy when @ref type == @ref afb_auth_LOA */
139 /** first child when @ref type in { @ref afb_auth_Or, @ref afb_auth_And, @ref afb_auth_Not } */
140 const struct afb_auth *first;
143 /** second child when @ref type in { @ref afb_auth_Or, @ref afb_auth_And } */
144 const struct afb_auth *next;
149 The possible values for **type** is defined here:
152 typedef enum afb_auth_type afb_auth_type_t;
155 * Enumeration for authority (Session/Token/Assurance) definitions.
161 /** never authorized, no data */
164 /** authorized if token valid, no data */
167 /** authorized if LOA greater than data 'loa' */
170 /** authorized if permission 'text' is granted */
173 /** authorized if 'first' or 'next' is authorized */
176 /** authorized if 'first' and 'next' are authorized */
179 /** authorized if 'first' is not authorized */
182 /** always authorized, no data */
190 static const afb_auth_t myauth[] = {
191 { .type = afb_auth_Permission, .text = "urn:AGL:permission:me:public:set" },
192 { .type = afb_auth_Permission, .text = "urn:AGL:permission:me:public:get" },
193 { .type = afb_auth_Or, .first = &myauth[1], .next = &myauth[0] }
197 ## Functions of class afb_daemon
199 The 3 following functions are linked to libsystemd.
200 They allow use of **sd_event** features and access
201 to **sd_bus** features.
205 * Retrieves the common systemd's event loop of AFB
208 struct sd_event *afb_daemon_get_event_loop();
211 * Retrieves the common systemd's user/session d-bus of AFB if active
213 struct sd_bus *afb_daemon_get_user_bus();
216 * Retrieves the common systemd's system d-bus of AFB if active or NULL
218 struct sd_bus *afb_daemon_get_system_bus();
221 The 2 following functions are linked to event management.
222 Broadcasting an event send it to any possible listener.
226 * Broadcasts widely the event of 'name' with the data 'object'.
227 * 'object' can be NULL.
229 * For convenience, the function calls 'json_object_put' for 'object'.
230 * Thus, in the case where 'object' should remain available after
231 * the function returns, the function 'json_object_get' shall be used.
233 * Calling this function is only forbidden during preinit.
235 * Returns the count of clients that received the event.
237 int afb_daemon_broadcast_event(const char *name, struct json_object *object);
240 * Creates an event of 'name' and returns it.
242 * Calling this function is only forbidden during preinit.
244 * See afb_event_is_valid to check if there is an error.
246 afb_event_t afb_daemon_make_event(const char *name);
249 The following function is used by logging macros and should normally
251 Instead, you should use the macros:
261 * Send a message described by 'fmt' and following parameters
262 * to the journal for the verbosity 'level'.
264 * 'file', 'line' and 'func' are indicators of position of the code in source files
265 * (see macros __FILE__, __LINE__ and __func__).
267 * 'level' is defined by syslog standard:
268 * EMERGENCY 0 System is unusable
269 * ALERT 1 Action must be taken immediately
270 * CRITICAL 2 Critical conditions
271 * ERROR 3 Error conditions
272 * WARNING 4 Warning conditions
273 * NOTICE 5 Normal but significant condition
274 * INFO 6 Informational
275 * DEBUG 7 Debug-level messages
277 void afb_daemon_verbose(int level, const char *file, int line, const char * func, const char *fmt, ...);
280 The 2 following functions MUST be used to access data of the bindings.
284 * Get the root directory file descriptor. This file descriptor can
285 * be used with functions 'openat', 'fstatat', ...
287 int afb_daemon_rootdir_get_fd();
290 * Opens 'filename' within the root directory with 'flags' (see function openat)
291 * using the 'locale' definition (example: "jp,en-US") that can be NULL.
292 * Returns the file descriptor or -1 in case of error.
294 int afb_daemon_rootdir_open_locale(const char *filename, int flags, const char *locale);
297 The following function is used to queue jobs.
301 * Queue the job defined by 'callback' and 'argument' for being executed asynchronously
302 * in this thread (later) or in an other thread.
303 * If 'group' is not NUL, the jobs queued with a same value (as the pointer value 'group')
304 * are executed in sequence in the order of there submission.
305 * If 'timeout' is not 0, it represent the maximum execution time for the job in seconds.
306 * At first, the job is called with 0 as signum and the given argument.
307 * The job is executed with the monitoring of its time and some signals like SIGSEGV and
308 * SIGFPE. When a such signal is catched, the job is terminated and re-executed but with
309 * signum being the signal number (SIGALRM when timeout expired).
311 * Returns 0 in case of success or -1 in case of error.
313 int afb_daemon_queue_job(void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
316 The following function must be used when a binding depends on other
317 bindings at its initialization.
321 * Tells that it requires the API of "name" to exist
322 * and if 'initialized' is not null to be initialized.
323 * Calling this function is only allowed within init.
324 * Returns 0 in case of success or -1 in case of error.
326 int afb_daemon_require_api(const char *name, int initialized)
329 This function allows to give a different name to the binding.
330 It can be called during pre-init.
334 * Set the name of the API to 'name'.
335 * Calling this function is only allowed within preinit.
336 * Returns 0 in case of success or -1 in case of error.
338 int afb_daemon_rename_api(const char *name);
341 ## Functions of class afb_service
343 The following functions allow services to call verbs of other
344 bindings for themselves.
348 * Calls the 'verb' of the 'api' with the arguments 'args' and 'verb' in the name of the binding.
349 * The result of the call is delivered to the 'callback' function with the 'callback_closure'.
351 * For convenience, the function calls 'json_object_put' for 'args'.
352 * Thus, in the case where 'args' should remain available after
353 * the function returns, the function 'json_object_get' shall be used.
355 * The 'callback' receives 3 arguments:
356 * 1. 'closure' the user defined closure pointer 'callback_closure',
357 * 2. 'status' a status being 0 on success or negative when an error occured,
358 * 2. 'result' the resulting data as a JSON object.
360 * @param api The api name of the method to call
361 * @param verb The verb name of the method to call
362 * @param args The arguments to pass to the method
363 * @param callback The to call on completion
364 * @param callback_closure The closure to pass to the callback
366 * @see also 'afb_req_subcall'
368 void afb_service_call(
371 struct json_object *args,
372 void (*callback)(void*closure, int status, struct json_object *result),
373 void *callback_closure);
376 * Calls the 'verb' of the 'api' with the arguments 'args' and 'verb' in the name of the binding.
377 * 'result' will receive the response.
379 * For convenience, the function calls 'json_object_put' for 'args'.
380 * Thus, in the case where 'args' should remain available after
381 * the function returns, the function 'json_object_get' shall be used.
383 * @param api The api name of the method to call
384 * @param verb The verb name of the method to call
385 * @param args The arguments to pass to the method
386 * @param result Where to store the result - should call json_object_put on it -
388 * @returns 0 in case of success or a negative value in case of error.
390 * @see also 'afb_req_subcall'
392 int afb_service_call_sync(
395 struct json_object *args,
396 struct json_object **result);
399 ## Functions of class afb_event
401 This function checks whether the event is valid.
402 It must be used when creating events.
406 * Checks wether the 'event' is valid or not.
408 * Returns 0 if not valid or 1 if valid.
410 int afb_event_is_valid(afb_event_t event);
413 The two following functions are used to broadcast or push
418 * Broadcasts widely the 'event' with the data 'object'.
419 * 'object' can be NULL.
421 * For convenience, the function calls 'json_object_put' for 'object'.
422 * Thus, in the case where 'object' should remain available after
423 * the function returns, the function 'json_object_get' shall be used.
425 * Returns the count of clients that received the event.
427 int afb_event_broadcast(afb_event_t event, struct json_object *object);
430 * Pushes the 'event' with the data 'object' to its observers.
431 * 'object' can be NULL.
433 * For convenience, the function calls 'json_object_put' for 'object'.
434 * Thus, in the case where 'object' should remain available after
435 * the function returns, the function 'json_object_get' shall be used.
437 * Returns the count of clients that received the event.
439 int afb_event_push(afb_event_t event, struct json_object *object);
442 The following function remove one reference to the event.
446 * Decrease the reference count of the event.
447 * After calling this function, the event
448 * MUST NOT BE USED ANYMORE.
450 void afb_event_unref(afb_event_t event);
453 The following function add one reference to the event.
457 * Decrease the reference count of the event.
458 * After calling this function, the event
459 * MUST NOT BE USED ANYMORE.
461 void afb_event_unref(afb_event_t event);
464 This function allows to retrieve the exact name of the event.
468 * Gets the name associated to the 'event'.
470 const char *afb_event_name(afb_event_t event);
473 ## Functions of class afb_req
475 This function checks the validity of the **req**.
479 * Checks wether the request 'req' is valid or not.
481 * Returns 0 if not valid or 1 if valid.
483 int afb_req_is_valid(afb_req_t req);
486 The following functions retrieves parameters of the request.
490 * Gets from the request 'req' the argument of 'name'.
491 * Returns a PLAIN structure of type 'struct afb_arg'.
492 * When the argument of 'name' is not found, all fields of result are set to NULL.
493 * When the argument of 'name' is found, the fields are filled,
494 * in particular, the field 'result.name' is set to 'name'.
496 * There is a special name value: the empty string.
497 * The argument of name "" is defined only if the request was made using
498 * an HTTP POST of Content-Type "application/json". In that case, the
499 * argument of name "" receives the value of the body of the HTTP request.
501 afb_arg_t afb_req_get(afb_req_t req, const char *name);
504 * Gets from the request 'req' the string value of the argument of 'name'.
505 * Returns NULL if when there is no argument of 'name'.
506 * Returns the value of the argument of 'name' otherwise.
508 * Shortcut for: afb_req_get(req, name).value
510 const char *afb_req_value(afb_req_t req, const char *name);
513 * Gets from the request 'req' the path for file attached to the argument of 'name'.
514 * Returns NULL if when there is no argument of 'name' or when there is no file.
515 * Returns the path of the argument of 'name' otherwise.
517 * Shortcut for: afb_req_get(req, name).path
519 const char *afb_req_path(afb_req_t req, const char *name);
522 * Gets from the request 'req' the json object hashing the arguments.
523 * The returned object must not be released using 'json_object_put'.
525 struct json_object *afb_req_json(afb_req_t req);
528 The following functions emit the reply to the request.
532 * Sends a reply of kind success to the request 'req'.
533 * The status of the reply is automatically set to "success".
534 * Its send the object 'obj' (can be NULL) with an
535 * informationnal comment 'info (can also be NULL).
537 * For convenience, the function calls 'json_object_put' for 'obj'.
538 * Thus, in the case where 'obj' should remain available after
539 * the function returns, the function 'json_object_get' shall be used.
541 void afb_req_success(afb_req_t req, struct json_object *obj, const char *info);
544 * Same as 'afb_req_success' but the 'info' is a formatting
545 * string followed by arguments.
547 * For convenience, the function calls 'json_object_put' for 'obj'.
548 * Thus, in the case where 'obj' should remain available after
549 * the function returns, the function 'json_object_get' shall be used.
551 void afb_req_success_f(afb_req_t req, struct json_object *obj, const char *info, ...);
554 * Same as 'afb_req_success_f' but the arguments to the format 'info'
555 * are given as a variable argument list instance.
557 * For convenience, the function calls 'json_object_put' for 'obj'.
558 * Thus, in the case where 'obj' should remain available after
559 * the function returns, the function 'json_object_get' shall be used.
561 void afb_req_success_v(afb_req_t req, struct json_object *obj, const char *info, va_list args);
564 * Sends a reply of kind failure to the request 'req'.
565 * The status of the reply is set to 'status' and an
566 * informational comment 'info' (can also be NULL) can be added.
568 * Note that calling afb_req_fail("success", info) is equivalent
569 * to call afb_req_success(NULL, info). Thus even if possible it
570 * is strongly recommended to NEVER use "success" for status.
572 void afb_req_fail(afb_req_t req, const char *status, const char *info);
575 * Same as 'afb_req_fail' but the 'info' is a formatting
576 * string followed by arguments.
578 void afb_req_fail_f(afb_req_t req, const char *status, const char *info, ...);
581 * Same as 'afb_req_fail_f' but the arguments to the format 'info'
582 * are given as a variable argument list instance.
584 void afb_req_fail_v(afb_req_t req, const char *status, const char *info, va_list args);
587 The following functions handle the session data.
591 * Gets the pointer stored by the binding for the session of 'req'.
592 * When the binding has not yet recorded a pointer, NULL is returned.
594 void *afb_req_context_get(afb_req_t req);
597 * Stores for the binding the pointer 'context' to the session of 'req'.
598 * The function 'free_context' will be called when the session is closed
599 * or if binding stores an other pointer.
601 void afb_req_context_set(afb_req_t req, void *context, void (*free_context)(void*));
604 * Gets the pointer stored by the binding for the session of 'req'.
605 * If the stored pointer is NULL, indicating that no pointer was
606 * already stored, afb_req_context creates a new context by calling
607 * the function 'create_context' and stores it with the freeing function
610 void *afb_req_context(afb_req_t req, void *(*create_context)(), void (*free_context)(void*));
613 * Frees the pointer stored by the binding for the session of 'req'
614 * and sets it to NULL.
616 * Shortcut for: afb_req_context_set(req, NULL, NULL)
618 void afb_req_context_clear(afb_req_t req);
621 * Closes the session associated with 'req'
622 * and delete all associated contexts.
624 void afb_req_session_close(afb_req_t req);
627 * Sets the level of assurance of the session of 'req'
628 * to 'level'. The effect of this function is subject of
630 * Returns 1 on success or 0 if failed.
632 int afb_req_session_set_LOA(afb_req_t req, unsigned level);
635 The 4 following functions must be used for asynchronous handling requests.
639 * Adds one to the count of references of 'req'.
640 * This function MUST be called by asynchronous implementations
641 * of verbs if no reply was sent before returning.
643 void afb_req_addref(afb_req_t req);
646 * Substracts one to the count of references of 'req'.
647 * This function MUST be called by asynchronous implementations
648 * of verbs after sending the asynchronous reply.
650 void afb_req_unref(afb_req_t req);
653 * Stores 'req' on heap for asynchronous use.
654 * Returns a handler to the stored 'req' or NULL on memory depletion.
655 * The count of reference to 'req' is incremented on success
656 * (see afb_req_addref).
658 struct afb_stored_req *afb_req_store(afb_req_t req);
661 * Retrieves the afb_req stored at 'sreq'.
662 * Returns the stored request.
663 * The count of reference is UNCHANGED, thus, the
664 * function 'afb_req_unref' should be called on the result
665 * after that the asynchronous reply if sent.
667 afb_req_t afb_req_unstore(struct afb_stored_req *sreq);
670 The two following functions are used to associate client with events
675 * Establishes for the client link identified by 'req' a subscription
677 * Returns 0 in case of successful subscription or -1 in case of error.
679 int afb_req_subscribe(afb_req_t req, afb_event_t event);
682 * Revokes the subscription established to the 'event' for the client
683 * link identified by 'req'.
684 * Returns 0 in case of successful subscription or -1 in case of error.
686 int afb_req_unsubscribe(afb_req_t req, afb_event_t event);
689 The following functions must be used to make request in the name of the
690 client (with its permissions).
694 * Makes a call to the method of name 'api' / 'verb' with the object 'args'.
695 * This call is made in the context of the request 'req'.
696 * On completion, the function 'callback' is invoked with the
697 * 'closure' given at call and two other parameters: 'iserror' and 'result'.
698 * 'status' is 0 on success or negative when on an error reply.
699 * 'result' is the json object of the reply, you must not call json_object_put
702 * For convenience, the function calls 'json_object_put' for 'args'.
703 * Thus, in the case where 'args' should remain available after
704 * the function returns, the function 'json_object_get' shall be used.
707 * - 'afb_req_subcall_req' that is convenient to keep request alive automatically.
708 * - 'afb_req_subcall_sync' the synchronous version
710 void afb_req_subcall(
714 struct json_object *args,
715 void (*callback)(void *closure, int status, struct json_object *result),
719 * Makes a call to the method of name 'api' / 'verb' with the object 'args'.
720 * This call is made in the context of the request 'req'.
721 * On completion, the function 'callback' is invoked with the
722 * original request 'req', the 'closure' given at call and two
723 * other parameters: 'iserror' and 'result'.
724 * 'status' is 0 on success or negative when on an error reply.
725 * 'result' is the json object of the reply, you must not call json_object_put
728 * For convenience, the function calls 'json_object_put' for 'args'.
729 * Thus, in the case where 'args' should remain available after
730 * the function returns, the function 'json_object_get' shall be used.
733 * - 'afb_req_subcall' that doesn't keep request alive automatically.
734 * - 'afb_req_subcall_sync' the synchronous version
736 static inline void afb_req_subcall_req(afb_req_t req, const char *api, const char *verb, struct json_object *args, void (*callback)(void *closure, int iserror, struct json_object *result, afb_req_t req), void *closure)
738 req.itf->subcall_req(req.closure, api, verb, args, callback, closure);
742 * Makes a call to the method of name 'api' / 'verb' with the object 'args'.
743 * This call is made in the context of the request 'req'.
744 * This call is synchronous, it waits untill completion of the request.
745 * It returns 0 on success or a negative value on error answer.
746 * The object pointed by 'result' is filled and must be released by the caller
747 * after its use by calling 'json_object_put'.
749 * For convenience, the function calls 'json_object_put' for 'args'.
750 * Thus, in the case where 'args' should remain available after
751 * the function returns, the function 'json_object_get' shall be used.
754 * - 'afb_req_subcall_req' that is convenient to keep request alive automatically.
755 * - 'afb_req_subcall' that doesn't keep request alive automatically.
757 int afb_req_subcall_sync(
761 struct json_object *args,
762 struct json_object **result);
765 The following function is used by logging macros and should normally
767 Instead, you should use the macros:
770 - **AFB_REQ_WARNING**
777 * Send associated to 'req' a message described by 'fmt' and following parameters
778 * to the journal for the verbosity 'level'.
780 * 'file', 'line' and 'func' are indicators of position of the code in source files
781 * (see macros __FILE__, __LINE__ and __func__).
783 * 'level' is defined by syslog standard:
784 * EMERGENCY 0 System is unusable
785 * ALERT 1 Action must be taken immediately
786 * CRITICAL 2 Critical conditions
787 * ERROR 3 Error conditions
788 * WARNING 4 Warning conditions
789 * NOTICE 5 Normal but significant condition
790 * INFO 6 Informational
791 * DEBUG 7 Debug-level messages
793 void afb_req_verbose(afb_req_t req, int level, const char *file, int line, const char * func, const char *fmt, ...);
796 The functions below allow a binding involved in the platform security
797 to explicitely check a permission of a client or to get the calling
798 application identity.
802 * Check whether the 'permission' is granted or not to the client
803 * identified by 'req'.
805 * Returns 1 if the permission is granted or 0 otherwise.
807 int afb_req_has_permission(afb_req_t req, const char *permission);
810 * Get the application identifier of the client application for the
813 * Returns the application identifier or NULL when the application
814 * can not be identified.
816 * The returned value if not NULL must be freed by the caller
818 char *afb_req_get_application_id(afb_req_t req);
821 * Get the user identifier (UID) of the client application for the
824 * Returns -1 when the application can not be identified.
826 int afb_req_get_uid(afb_req_t req);
831 The following macros must be used for logging:
841 The following macros can be used for logging in the context
842 of a request **req** of type **afb_req**:
845 AFB_REQ_ERROR(req,fmt,...)
846 AFB_REQ_WARNING(req,fmt,...)
847 AFB_REQ_NOTICE(req,fmt,...)
848 AFB_REQ_INFO(req,fmt,...)
849 AFB_REQ_DEBUG(req,fmt,...)
852 By default, the logging macros add file, line and function