3 ## Structure for declaring binding
5 ### struct afb_binding_v2
7 The main structure, of type **afb_binding_v2**, for describing the binding
8 must be exported under the name **afbBindingV2**.
10 This structure is defined as below.
14 * Description of the bindings of type version 2
18 const char *api; /* api name for the binding */
19 const char *specification; /* textual openAPIv3 specification of the binding */
20 const char *info; /* some info about the api, can be NULL */
21 const struct afb_verb_v2 *verbs; /* array of descriptions of verbs terminated by a NULL name */
22 int (*preinit)(); /* callback at load of the binding */
23 int (*init)(); /* callback for starting the service */
24 void (*onevent)(const char *event, struct json_object *object); /* callback for handling events */
25 unsigned noconcurrency: 1; /* avoids concurrent requests to verbs */
29 ### struct afb_verb_v2
31 Each verb is described with a structure of type **afb_verb_v2**
36 * Description of one verb of the API provided by the binding
37 * This enumeration is valid for bindings of type version 2
41 const char *verb; /* name of the verb */
42 void (*callback)(struct afb_req req); /* callback function implementing the verb */
43 const struct afb_auth *auth; /* required authorization */
44 const char *info; /* some info about the verb, can be NULL */
45 uint32_t session; /* authorization and session requirements of the verb */
49 The **session** flags is one of the constant defined below:
51 - AFB_SESSION_NONE : no flag, synonym to 0
52 - AFB_SESSION_LOA_0 : Requires the LOA to be 0 or more, synonym to 0 or AFB_SESSION_NONE
53 - AFB_SESSION_LOA_1 : Requires the LOA to be 1 or more
54 - AFB_SESSION_LOA_2 : Requires the LOA to be 2 or more
55 - AFB_SESSION_LOA_3 : Requires the LOA to be 3 or more
56 - AFB_SESSION_CHECK : Requires the token to be set and valid
57 - AFB_SESSION_REFRESH : Implies a token refresh
58 - AFB_SESSION_CLOSE : Implies cloing the session
60 The LOA (Level Of Assurance) is set, by binding, using the function **afb_req_session_set_LOA**.
62 ### struct afb_auth and enum afb_auth_type
64 The structure **afb_auth** is used within verb description to
65 set security requirements.
66 The interpretation of the structure depends on the value of the field **type**.
71 const enum afb_auth_type type;
75 const struct afb_auth *first;
77 const struct afb_auth *next;
81 The possible values for **type** is defined here:
85 * Enum for Session/Token/Assurance middleware.
89 afb_auth_No = 0, /** never authorized, no data */
90 afb_auth_Token, /** authorized if token valid, no data */
91 afb_auth_LOA, /** authorized if LOA greater than data 'loa' */
92 afb_auth_Permission, /** authorized if permission 'text' is granted */
93 afb_auth_Or, /** authorized if 'first' or 'next' is authorized */
94 afb_auth_And, /** authorized if 'first' and 'next' are authorized */
95 afb_auth_Not, /** authorized if 'first' is not authorized */
96 afb_auth_Yes /** always authorized, no data */
103 static const struct afb_auth _afb_auths_v2_monitor[] = {
104 { .type = afb_auth_Permission, .text = "urn:AGL:permission:monitor:public:set" },
105 { .type = afb_auth_Permission, .text = "urn:AGL:permission:monitor:public:get" },
106 { .type = afb_auth_Or, .first = &_afb_auths_v2_monitor[1], .next = &_afb_auths_v2_monitor[0] }
110 ## Functions of class afb_daemon
112 The 3 following functions are linked to libsystemd.
113 They allow use of **sd_event** features and access
114 to **sd_bus** features.
118 * Retrieves the common systemd's event loop of AFB
120 struct sd_event *afb_daemon_get_event_loop();
123 * Retrieves the common systemd's user/session d-bus of AFB
125 struct sd_bus *afb_daemon_get_user_bus();
128 * Retrieves the common systemd's system d-bus of AFB
130 struct sd_bus *afb_daemon_get_system_bus();
133 The 2 following functions are linked to event management.
134 Broadcasting an event send it to any possible listener.
138 * Broadcasts widely the event of 'name' with the data 'object'.
139 * 'object' can be NULL.
141 * For convenience, the function calls 'json_object_put' for 'object'.
142 * Thus, in the case where 'object' should remain available after
143 * the function returns, the function 'json_object_get' shall be used.
145 * Calling this function is only forbidden during preinit.
147 * Returns the count of clients that received the event.
149 int afb_daemon_broadcast_event(const char *name, struct json_object *object);
152 * Creates an event of 'name' and returns it.
154 * Calling this function is only forbidden during preinit.
156 * See afb_event_is_valid to check if there is an error.
158 struct afb_event afb_daemon_make_event(const char *name);
161 The following function is used by logging macros and should normally
163 Instead, you should use the macros:
173 * Send a message described by 'fmt' and following parameters
174 * to the journal for the verbosity 'level'.
176 * 'file', 'line' and 'func' are indicators of position of the code in source files
177 * (see macros __FILE__, __LINE__ and __func__).
179 * 'level' is defined by syslog standard:
180 * EMERGENCY 0 System is unusable
181 * ALERT 1 Action must be taken immediately
182 * CRITICAL 2 Critical conditions
183 * ERROR 3 Error conditions
184 * WARNING 4 Warning conditions
185 * NOTICE 5 Normal but significant condition
186 * INFO 6 Informational
187 * DEBUG 7 Debug-level messages
189 void afb_daemon_verbose(int level, const char *file, int line, const char * func, const char *fmt, ...);
192 The 2 following functions MUST be used to access data of the bindings.
196 * Get the root directory file descriptor. This file descriptor can
197 * be used with functions 'openat', 'fstatat', ...
199 int afb_daemon_rootdir_get_fd();
202 * Opens 'filename' within the root directory with 'flags' (see function openat)
203 * using the 'locale' definition (example: "jp,en-US") that can be NULL.
204 * Returns the file descriptor or -1 in case of error.
206 int afb_daemon_rootdir_open_locale(const char *filename, int flags, const char *locale);
209 The following function is used to queue jobs.
213 * Queue the job defined by 'callback' and 'argument' for being executed asynchronously
214 * in this thread (later) or in an other thread.
215 * If 'group' is not NUL, the jobs queued with a same value (as the pointer value 'group')
216 * are executed in sequence in the order of there submission.
217 * If 'timeout' is not 0, it represent the maximum execution time for the job in seconds.
218 * At first, the job is called with 0 as signum and the given argument.
219 * The job is executed with the monitoring of its time and some signals like SIGSEGV and
220 * SIGFPE. When a such signal is catched, the job is terminated and re-executed but with
221 * signum being the signal number (SIGALRM when timeout expired).
223 * Returns 0 in case of success or -1 in case of error.
225 int afb_daemon_queue_job(void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
228 The following function must be used when a binding depends on other
229 bindings at its initialization.
233 * Tells that it requires the API of "name" to exist
234 * and if 'initialized' is not null to be initialized.
235 * Calling this function is only allowed within init.
236 * Returns 0 in case of success or -1 in case of error.
238 int afb_daemon_require_api(const char *name, int initialized)
241 This function allows to give a different name to the binding.
242 It can be called during pre-init.
246 * Set the name of the API to 'name'.
247 * Calling this function is only allowed within preinit.
248 * Returns 0 in case of success or -1 in case of error.
250 int afb_daemon_rename_api(const char *name);
253 ## Functions of class afb_service
255 The following functions allow services to call verbs of other
256 bindings for themselves.
260 * Calls the 'verb' of the 'api' with the arguments 'args' and 'verb' in the name of the binding.
261 * The result of the call is delivered to the 'callback' function with the 'callback_closure'.
263 * For convenience, the function calls 'json_object_put' for 'args'.
264 * Thus, in the case where 'args' should remain available after
265 * the function returns, the function 'json_object_get' shall be used.
267 * The 'callback' receives 3 arguments:
268 * 1. 'closure' the user defined closure pointer 'callback_closure',
269 * 2. 'status' a status being 0 on success or negative when an error occured,
270 * 2. 'result' the resulting data as a JSON object.
272 * @param api The api name of the method to call
273 * @param verb The verb name of the method to call
274 * @param args The arguments to pass to the method
275 * @param callback The to call on completion
276 * @param callback_closure The closure to pass to the callback
278 * @see also 'afb_req_subcall'
280 void afb_service_call(
283 struct json_object *args,
284 void (*callback)(void*closure, int status, struct json_object *result),
285 void *callback_closure);
288 * Calls the 'verb' of the 'api' with the arguments 'args' and 'verb' in the name of the binding.
289 * 'result' will receive the response.
291 * For convenience, the function calls 'json_object_put' for 'args'.
292 * Thus, in the case where 'args' should remain available after
293 * the function returns, the function 'json_object_get' shall be used.
295 * @param api The api name of the method to call
296 * @param verb The verb name of the method to call
297 * @param args The arguments to pass to the method
298 * @param result Where to store the result - should call json_object_put on it -
300 * @returns 0 in case of success or a negative value in case of error.
302 * @see also 'afb_req_subcall'
304 int afb_service_call_sync(
307 struct json_object *args,
308 struct json_object **result);
311 ## Functions of class afb_event
313 This function checks whether the event is valid.
314 It must be used when creating events.
318 * Checks wether the 'event' is valid or not.
320 * Returns 0 if not valid or 1 if valid.
322 int afb_event_is_valid(struct afb_event event);
325 The two following functions are used to broadcast or push
330 * Broadcasts widely the 'event' with the data 'object'.
331 * 'object' can be NULL.
333 * For convenience, the function calls 'json_object_put' for 'object'.
334 * Thus, in the case where 'object' should remain available after
335 * the function returns, the function 'json_object_get' shall be used.
337 * Returns the count of clients that received the event.
339 int afb_event_broadcast(struct afb_event event, struct json_object *object);
342 * Pushes the 'event' with the data 'object' to its observers.
343 * 'object' can be NULL.
345 * For convenience, the function calls 'json_object_put' for 'object'.
346 * Thus, in the case where 'object' should remain available after
347 * the function returns, the function 'json_object_get' shall be used.
349 * Returns the count of clients that received the event.
351 int afb_event_push(struct afb_event event, struct json_object *object);
354 The following function destiys the event.
358 * Drops the data associated to the 'event'
359 * After calling this function, the event
360 * MUST NOT BE USED ANYMORE.
362 void afb_event_drop(struct afb_event event);
365 This function allows to retrieve the exact name of the event.
369 * Gets the name associated to the 'event'.
371 const char *afb_event_name(struct afb_event event);
374 ## Functions of class afb_req
376 This function checks the validity of the **req**.
380 * Checks wether the request 'req' is valid or not.
382 * Returns 0 if not valid or 1 if valid.
384 int afb_req_is_valid(struct afb_req req);
387 The following functions retrieves parameters of the request.
391 * Gets from the request 'req' the argument of 'name'.
392 * Returns a PLAIN structure of type 'struct afb_arg'.
393 * When the argument of 'name' is not found, all fields of result are set to NULL.
394 * When the argument of 'name' is found, the fields are filled,
395 * in particular, the field 'result.name' is set to 'name'.
397 * There is a special name value: the empty string.
398 * The argument of name "" is defined only if the request was made using
399 * an HTTP POST of Content-Type "application/json". In that case, the
400 * argument of name "" receives the value of the body of the HTTP request.
402 struct afb_arg afb_req_get(struct afb_req req, const char *name);
405 * Gets from the request 'req' the string value of the argument of 'name'.
406 * Returns NULL if when there is no argument of 'name'.
407 * Returns the value of the argument of 'name' otherwise.
409 * Shortcut for: afb_req_get(req, name).value
411 const char *afb_req_value(struct afb_req req, const char *name);
414 * Gets from the request 'req' the path for file attached to the argument of 'name'.
415 * Returns NULL if when there is no argument of 'name' or when there is no file.
416 * Returns the path of the argument of 'name' otherwise.
418 * Shortcut for: afb_req_get(req, name).path
420 const char *afb_req_path(struct afb_req req, const char *name);
423 * Gets from the request 'req' the json object hashing the arguments.
424 * The returned object must not be released using 'json_object_put'.
426 struct json_object *afb_req_json(struct afb_req req);
429 The following functions emit the reply to the request.
433 * Sends a reply of kind success to the request 'req'.
434 * The status of the reply is automatically set to "success".
435 * Its send the object 'obj' (can be NULL) with an
436 * informationnal comment 'info (can also be NULL).
438 * For convenience, the function calls 'json_object_put' for 'obj'.
439 * Thus, in the case where 'obj' should remain available after
440 * the function returns, the function 'json_object_get' shall be used.
442 void afb_req_success(struct afb_req req, struct json_object *obj, const char *info);
445 * Same as 'afb_req_success' but the 'info' is a formatting
446 * string followed by arguments.
448 * For convenience, the function calls 'json_object_put' for 'obj'.
449 * Thus, in the case where 'obj' should remain available after
450 * the function returns, the function 'json_object_get' shall be used.
452 void afb_req_success_f(struct afb_req req, struct json_object *obj, const char *info, ...);
455 * Same as 'afb_req_success_f' but the arguments to the format 'info'
456 * are given as a variable argument list instance.
458 * For convenience, the function calls 'json_object_put' for 'obj'.
459 * Thus, in the case where 'obj' should remain available after
460 * the function returns, the function 'json_object_get' shall be used.
462 void afb_req_success_v(struct afb_req req, struct json_object *obj, const char *info, va_list args);
465 * Sends a reply of kind failure to the request 'req'.
466 * The status of the reply is set to 'status' and an
467 * informationnal comment 'info' (can also be NULL) can be added.
469 * Note that calling afb_req_fail("success", info) is equivalent
470 * to call afb_req_success(NULL, info). Thus even if possible it
471 * is strongly recommended to NEVER use "success" for status.
473 void afb_req_fail(struct afb_req req, const char *status, const char *info);
476 * Same as 'afb_req_fail' but the 'info' is a formatting
477 * string followed by arguments.
479 void afb_req_fail_f(struct afb_req req, const char *status, const char *info, ...);
482 * Same as 'afb_req_fail_f' but the arguments to the format 'info'
483 * are given as a variable argument list instance.
485 void afb_req_fail_v(struct afb_req req, const char *status, const char *info, va_list args);
488 The following functions handle the session data.
492 * Gets the pointer stored by the binding for the session of 'req'.
493 * When the binding has not yet recorded a pointer, NULL is returned.
495 void *afb_req_context_get(struct afb_req req);
498 * Stores for the binding the pointer 'context' to the session of 'req'.
499 * The function 'free_context' will be called when the session is closed
500 * or if binding stores an other pointer.
502 void afb_req_context_set(struct afb_req req, void *context, void (*free_context)(void*));
505 * Gets the pointer stored by the binding for the session of 'req'.
506 * If the stored pointer is NULL, indicating that no pointer was
507 * already stored, afb_req_context creates a new context by calling
508 * the function 'create_context' and stores it with the freeing function
511 void *afb_req_context(struct afb_req req, void *(*create_context)(), void (*free_context)(void*));
514 * Frees the pointer stored by the binding for the session of 'req'
515 * and sets it to NULL.
517 * Shortcut for: afb_req_context_set(req, NULL, NULL)
519 void afb_req_context_clear(struct afb_req req);
522 * Closes the session associated with 'req'
523 * and delete all associated contexts.
525 void afb_req_session_close(struct afb_req req);
528 * Sets the level of assurance of the session of 'req'
529 * to 'level'. The effect of this function is subject of
531 * Returns 1 on success or 0 if failed.
533 int afb_req_session_set_LOA(struct afb_req req, unsigned level);
536 The 4 following functions must be used for asynchronous handling requests.
540 * Adds one to the count of references of 'req'.
541 * This function MUST be called by asynchronous implementations
542 * of verbs if no reply was sent before returning.
544 void afb_req_addref(struct afb_req req);
547 * Substracts one to the count of references of 'req'.
548 * This function MUST be called by asynchronous implementations
549 * of verbs after sending the asynchronous reply.
551 void afb_req_unref(struct afb_req req);
554 * Stores 'req' on heap for asynchronous use.
555 * Returns a handler to the stored 'req' or NULL on memory depletion.
556 * The count of reference to 'req' is incremented on success
557 * (see afb_req_addref).
559 struct afb_stored_req *afb_req_store(struct afb_req req);
562 * Retrieves the afb_req stored at 'sreq'.
563 * Returns the stored request.
564 * The count of reference is UNCHANGED, thus, the
565 * function 'afb_req_unref' should be called on the result
566 * after that the asynchronous reply if sent.
568 struct afb_req afb_req_unstore(struct afb_stored_req *sreq);
571 The two following functions are used to associate client with events
576 * Establishes for the client link identified by 'req' a subscription
578 * Returns 0 in case of successful subscription or -1 in case of error.
580 int afb_req_subscribe(struct afb_req req, struct afb_event event);
583 * Revokes the subscription established to the 'event' for the client
584 * link identified by 'req'.
585 * Returns 0 in case of successful subscription or -1 in case of error.
587 int afb_req_unsubscribe(struct afb_req req, struct afb_event event);
590 The following functions must be used to make request in the name of the
591 client (with its permissions).
595 * Makes a call to the method of name 'api' / 'verb' with the object 'args'.
596 * This call is made in the context of the request 'req'.
597 * On completion, the function 'callback' is invoked with the
598 * 'closure' given at call and two other parameters: 'iserror' and 'result'.
599 * 'status' is 0 on success or negative when on an error reply.
600 * 'result' is the json object of the reply, you must not call json_object_put
603 * For convenience, the function calls 'json_object_put' for 'args'.
604 * Thus, in the case where 'args' should remain available after
605 * the function returns, the function 'json_object_get' shall be used.
608 * - 'afb_req_subcall_req' that is convenient to keep request alive automatically.
609 * - 'afb_req_subcall_sync' the synchronous version
611 void afb_req_subcall(
615 struct json_object *args,
616 void (*callback)(void *closure, int status, struct json_object *result),
620 * Makes a call to the method of name 'api' / 'verb' with the object 'args'.
621 * This call is made in the context of the request 'req'.
622 * On completion, the function 'callback' is invoked with the
623 * original request 'req', the 'closure' given at call and two
624 * other parameters: 'iserror' and 'result'.
625 * 'status' is 0 on success or negative when on an error reply.
626 * 'result' is the json object of the reply, you must not call json_object_put
629 * For convenience, the function calls 'json_object_put' for 'args'.
630 * Thus, in the case where 'args' should remain available after
631 * the function returns, the function 'json_object_get' shall be used.
634 * - 'afb_req_subcall' that doesn't keep request alive automatically.
635 * - 'afb_req_subcall_sync' the synchronous version
637 static inline void afb_req_subcall_req(struct afb_req req, const char *api, const char *verb, struct json_object *args, void (*callback)(void *closure, int iserror, struct json_object *result, struct afb_req req), void *closure)
639 req.itf->subcall_req(req.closure, api, verb, args, callback, closure);
643 * Makes a call to the method of name 'api' / 'verb' with the object 'args'.
644 * This call is made in the context of the request 'req'.
645 * This call is synchronous, it waits untill completion of the request.
646 * It returns 0 on success or a negative value on error answer.
647 * The object pointed by 'result' is filled and must be released by the caller
648 * after its use by calling 'json_object_put'.
650 * For convenience, the function calls 'json_object_put' for 'args'.
651 * Thus, in the case where 'args' should remain available after
652 * the function returns, the function 'json_object_get' shall be used.
655 * - 'afb_req_subcall_req' that is convenient to keep request alive automatically.
656 * - 'afb_req_subcall' that doesn't keep request alive automatically.
658 int afb_req_subcall_sync(
662 struct json_object *args,
663 struct json_object **result);
666 The following function is used by logging macros and should normally
668 Instead, you should use the macros:
671 - **AFB_REQ_WARNING**
678 * Send associated to 'req' a message described by 'fmt' and following parameters
679 * to the journal for the verbosity 'level'.
681 * 'file', 'line' and 'func' are indicators of position of the code in source files
682 * (see macros __FILE__, __LINE__ and __func__).
684 * 'level' is defined by syslog standard:
685 * EMERGENCY 0 System is unusable
686 * ALERT 1 Action must be taken immediately
687 * CRITICAL 2 Critical conditions
688 * ERROR 3 Error conditions
689 * WARNING 4 Warning conditions
690 * NOTICE 5 Normal but significant condition
691 * INFO 6 Informational
692 * DEBUG 7 Debug-level messages
694 void afb_req_verbose(struct afb_req req, int level, const char *file, int line, const char * func, const char *fmt, ...);
697 The functions below allow a binding involved in the platform security
698 to explicitely check a permission of a client or to get the calling
699 application identity.
703 * Check whether the 'permission' is granted or not to the client
704 * identified by 'req'.
706 * Returns 1 if the permission is granted or 0 otherwise.
708 int afb_req_has_permission(struct afb_req req, const char *permission);
711 * Get the application identifier of the client application for the
714 * Returns the application identifier or NULL when the application
715 * can not be identified.
717 * The returned value if not NULL must be freed by the caller
719 inline char *afb_req_get_application_id(struct afb_req req);
725 The following macros must be used for logging:
735 The following macros can be used for logging in the context
736 of a request **req** of type **afb_req**:
739 AFB_REQ_ERROR(req,fmt,...)
740 AFB_REQ_WARNING(req,fmt,...)
741 AFB_REQ_NOTICE(req,fmt,...)
742 AFB_REQ_INFO(req,fmt,...)
743 AFB_REQ_DEBUG(req,fmt,...)
746 By default, the logging macros add file, line and function