Missing member on new binding v2 struct.
[src/app-framework-binder.git] / docs / afb-binding-references.md
1 Binding Reference
2 =================
3
4 Structure for declaring binding
5 -------------------------------
6
7 ### struct afb_binding_v2
8
9 The main structure, of type **afb_binding_v2**, for describing the binding
10 must be exported under the name **afbBindingV2**.
11
12 This structure is defined as below.
13
14 ```C
15 /*
16  * Description of the bindings of type version 2
17  */
18 struct afb_binding_v2
19 {
20         const char *api;                        /* api name for the binding */
21         const char *specification;              /* textual openAPIv3 specification of the binding */
22         const char *info;                       /* some info about the api, can be NULL */
23         const struct afb_verb_v2 *verbs;        /* array of descriptions of verbs terminated by a NULL name */
24         int (*preinit)();                       /* callback at load of the binding */
25         int (*init)();                          /* callback for starting the service */
26         void (*onevent)(const char *event, struct json_object *object); /* callback for handling events */
27         unsigned noconcurrency: 1;              /* avoids concurrent requests to verbs */
28 };
29 ```
30
31 ### struct afb_verb_v2
32
33 Each verb is described with a structure of type **afb_verb_v2**
34 defined below:
35
36 ```C
37 /*
38  * Description of one verb of the API provided by the binding
39  * This enumeration is valid for bindings of type version 2
40  */
41 struct afb_verb_v2
42 {
43         const char *verb;                       /* name of the verb */
44         void (*callback)(struct afb_req req);   /* callback function implementing the verb */
45         const struct afb_auth *auth;                /* required authorization */
46         const char *info;                       /* some info about the verb, can be NULL */
47         uint32_t session;                       /* authorization and session requirements of the verb */
48 };
49 ```
50
51 The session flags is an or of the constant defined below:
52
53   - AFB_SESSION_NONE : no flag, synonym to 0
54   - AFB_SESSION_LOA_0 : Requires the LOA to be 0 or more, synonym to 0 or AFB_SESSION_NONE
55   - AFB_SESSION_LOA_1 : Requires the LOA to be 1 or more
56   - AFB_SESSION_LOA_2 : Requires the LOA to be 2 or more
57   - AFB_SESSION_LOA_3 : Requires the LOA to be 3 or more
58   - AFB_SESSION_CHECK : Requires the token to be set and valid
59   - AFB_SESSION_REFRESH : Implies a token refresh
60   - AFB_SESSION_CLOSE : Implies cloing the session
61
62 The LOA is set binding by binding using the function **afb_req_session_set_LOA**.
63
64 ### struct afb_auth and enum afb_auth_type
65
66 The structure **afb_auth** is used within verb description to
67 set security requirements. The interpretation of the structure
68 depends on the value of the field **type**.
69
70 ```C
71 struct afb_auth
72 {
73         const enum afb_auth_type type;
74         union {
75                 const char *text;
76                 const unsigned loa;
77                 const struct afb_auth *first;
78         };
79         const struct afb_auth *next;
80 };
81 ```
82 The possible values for **type** is defined here:
83
84 ```C
85 /*
86  * Enum for Session/Token/Assurance middleware.
87  */
88 enum afb_auth_type
89 {
90         afb_auth_No = 0,        /** never authorized, no data */
91         afb_auth_Token,         /** authorized if token valid, no data */
92         afb_auth_LOA,           /** authorized if LOA greater than data 'loa' */
93         afb_auth_Permission,    /** authorized if permission 'text' is granted */
94         afb_auth_Or,            /** authorized if 'first' or 'next' is authorized */
95         afb_auth_And,           /** authorized if 'first' and 'next' are authorized */
96         afb_auth_Not,           /** authorized if 'first' is not authorized */
97         afb_auth_Yes            /** always authorized, no data */
98 };
99 ```
100
101 Example:
102
103 ```C
104 static const struct afb_auth _afb_auths_v2_monitor[] = {
105         { .type = afb_auth_Permission, .text = "urn:AGL:permission:monitor:public:set" },
106         { .type = afb_auth_Permission, .text = "urn:AGL:permission:monitor:public:get" },
107         { .type = afb_auth_Or, .first = &_afb_auths_v2_monitor[1], .next = &_afb_auths_v2_monitor[0] }
108 };
109 ```
110
111 Functions of class afb_daemon...
112 -------------------------
113
114 The 3 following functions are linked to libsystemd.
115 They allow use of **sd_event** features and access
116 to **sd_bus** features.
117
118 ```C
119 /*
120  * Retrieves the common systemd's event loop of AFB
121  */
122 struct sd_event *afb_daemon_get_event_loop();
123
124 /*
125  * Retrieves the common systemd's user/session d-bus of AFB
126  */
127 struct sd_bus *afb_daemon_get_user_bus();
128
129 /*
130  * Retrieves the common systemd's system d-bus of AFB
131  */
132 struct sd_bus *afb_daemon_get_system_bus();
133 ```
134
135 The 2 following functions are linked to event management.
136 Broadcasting an event send it to any possible listener.
137
138 ```C
139 /*
140  * Broadcasts widely the event of 'name' with the data 'object'.
141  * 'object' can be NULL.
142  *
143  * For convenience, the function calls 'json_object_put' for 'object'.
144  * Thus, in the case where 'object' should remain available after
145  * the function returns, the function 'json_object_get' shall be used.
146  *
147  * Returns the count of clients that received the event.
148  */
149 int afb_daemon_broadcast_event(const char *name, struct json_object *object);
150
151 /*
152  * Creates an event of 'name' and returns it.
153  *
154  * See afb_event_is_valid to check if there is an error.
155  */
156 struct afb_event afb_daemon_make_event(const char *name);
157 ```
158
159 The following function is used by logging macros and should normally
160 not be used. Instead, you should use the macros
161 **AFB\_ERROR**, **AFB\_WARNING**, **AFB\_NOTICE**,
162 **AFB\_INFO**, **AFB\_DEBUG**
163
164 ```C
165 /*
166  * Send a message described by 'fmt' and following parameters
167  * to the journal for the verbosity 'level'.
168  *
169  * 'file', 'line' and 'func' are indicators of position of the code in source files
170  * (see macros __FILE__, __LINE__ and __func__).
171  *
172  * 'level' is defined by syslog standard:
173  *      EMERGENCY         0        System is unusable
174  *      ALERT             1        Action must be taken immediately
175  *      CRITICAL          2        Critical conditions
176  *      ERROR             3        Error conditions
177  *      WARNING           4        Warning conditions
178  *      NOTICE            5        Normal but significant condition
179  *      INFO              6        Informational
180  *      DEBUG             7        Debug-level messages
181  */
182 void afb_daemon_verbose(int level, const char *file, int line, const char * func, const char *fmt, ...);
183 ```
184
185 The 2 following functions MUST be used to access data of the bindings.
186
187 ```C
188 /*
189  * Get the root directory file descriptor. This file descriptor can
190  * be used with functions 'openat', 'fstatat', ...
191  */
192 int afb_daemon_rootdir_get_fd();
193
194 /*
195  * Opens 'filename' within the root directory with 'flags' (see function openat)
196  * using the 'locale' definition (example: "jp,en-US") that can be NULL.
197  * Returns the file descriptor or -1 in case of error.
198  */
199 int afb_daemon_rootdir_open_locale(const char *filename, int flags, const char *locale);
200 ```
201
202 The following function is used to queue jobs.
203
204 ```C
205 /*
206  * Queue the job defined by 'callback' and 'argument' for being executed asynchronously
207  * in this thread (later) or in an other thread.
208  * If 'group' is not NUL, the jobs queued with a same value (as the pointer value 'group')
209  * are executed in sequence in the order of there submission.
210  * If 'timeout' is not 0, it represent the maximum execution time for the job in seconds.
211  * At first, the job is called with 0 as signum and the given argument.
212  * The job is executed with the monitoring of its time and some signals like SIGSEGV and
213  * SIGFPE. When a such signal is catched, the job is terminated and re-executed but with
214  * signum being the signal number (SIGALRM when timeout expired).
215  *
216  * Returns 0 in case of success or -1 in case of error.
217  */
218 int afb_daemon_queue_job(void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
219 ```
220
221 The following function must be used when a binding depends on other
222 bindings at its initialization.
223
224 ```C
225 /*
226  * Tells that it requires the API of "name" to exist
227  * and if 'initialized' is not null to be initialized.
228  * Returns 0 in case of success or -1 in case of error.
229  */
230 int afb_daemon_require_api(const char *name, int initialized)
231 ```
232
233 Functions of class afb_service...
234 -------------------------
235
236 The following functions allow services to call verbs of other
237 bindings for themselves.
238
239 ```C
240 /**
241  * Calls the 'verb' of the 'api' with the arguments 'args' and 'verb' in the name of the binding.
242  * The result of the call is delivered to the 'callback' function with the 'callback_closure'.
243  *
244  * For convenience, the function calls 'json_object_put' for 'args'.
245  * Thus, in the case where 'args' should remain available after
246  * the function returns, the function 'json_object_get' shall be used.
247  *
248  * The 'callback' receives 3 arguments:
249  *  1. 'closure' the user defined closure pointer 'callback_closure',
250  *  2. 'status' a status being 0 on success or negative when an error occured,
251  *  2. 'result' the resulting data as a JSON object.
252  *
253  * @param api      The api name of the method to call
254  * @param verb     The verb name of the method to call
255  * @param args     The arguments to pass to the method
256  * @param callback The to call on completion
257  * @param callback_closure The closure to pass to the callback
258  *
259  * @see also 'afb_req_subcall'
260  */
261 void afb_service_call(
262         const char *api,
263         const char *verb,
264         struct json_object *args,
265         void (*callback)(void*closure, int status, struct json_object *result),
266         void *callback_closure);
267
268 /**
269  * Calls the 'verb' of the 'api' with the arguments 'args' and 'verb' in the name of the binding.
270  * 'result' will receive the response.
271  *
272  * For convenience, the function calls 'json_object_put' for 'args'.
273  * Thus, in the case where 'args' should remain available after
274  * the function returns, the function 'json_object_get' shall be used.
275  *
276  * @param api      The api name of the method to call
277  * @param verb     The verb name of the method to call
278  * @param args     The arguments to pass to the method
279  * @param result   Where to store the result - should call json_object_put on it -
280  *
281  * @returns 0 in case of success or a negative value in case of error.
282  *
283  * @see also 'afb_req_subcall'
284  */
285 int afb_service_call_sync(
286         const char *api,
287         const char *verb,
288         struct json_object *args,
289         struct json_object **result);
290 ```
291
292 Functions of class afb_event...
293 -------------------------
294
295 This function checks whether the event is valid. It must be used
296 when creating events.
297
298 ```C
299 /*
300  * Checks wether the 'event' is valid or not.
301  *
302  * Returns 0 if not valid or 1 if valid.
303  */
304 int afb_event_is_valid(struct afb_event event);
305 ```
306
307 The two following functions are used to broadcast or push
308 event with its data.
309
310 ```C
311 /*
312  * Broadcasts widely the 'event' with the data 'object'.
313  * 'object' can be NULL.
314  *
315  * For convenience, the function calls 'json_object_put' for 'object'.
316  * Thus, in the case where 'object' should remain available after
317  * the function returns, the function 'json_object_get' shall be used.
318  *
319  * Returns the count of clients that received the event.
320  */
321 int afb_event_broadcast(struct afb_event event, struct json_object *object);
322
323 /*
324  * Pushes the 'event' with the data 'object' to its observers.
325  * 'object' can be NULL.
326  *
327  * For convenience, the function calls 'json_object_put' for 'object'.
328  * Thus, in the case where 'object' should remain available after
329  * the function returns, the function 'json_object_get' shall be used.
330  *
331  * Returns the count of clients that received the event.
332  */
333 int afb_event_push(struct afb_event event, struct json_object *object);
334 ```
335
336 The following function destiys the event.
337
338 ```C
339 /*
340  * Drops the data associated to the 'event'
341  * After calling this function, the event
342  * MUST NOT BE USED ANYMORE.
343  */
344 void afb_event_drop(struct afb_event event);
345 ```
346
347 This function allows to retrieve the exact name of the event.
348
349 ```C
350 /*
351  * Gets the name associated to the 'event'.
352  */
353 const char *afb_event_name(struct afb_event event);
354 ```
355
356 Functions of class afb_req...
357 -------------------------
358
359 This function checks the validity of the **req**.
360
361 ```C
362 /*
363  * Checks wether the request 'req' is valid or not.
364  *
365  * Returns 0 if not valid or 1 if valid.
366  */
367 int afb_req_is_valid(struct afb_req req);
368 ```
369
370 The following functions retrieves parameters of the request.
371
372 ```C
373 /*
374  * Gets from the request 'req' the argument of 'name'.
375  * Returns a PLAIN structure of type 'struct afb_arg'.
376  * When the argument of 'name' is not found, all fields of result are set to NULL.
377  * When the argument of 'name' is found, the fields are filled,
378  * in particular, the field 'result.name' is set to 'name'.
379  *
380  * There is a special name value: the empty string.
381  * The argument of name "" is defined only if the request was made using
382  * an HTTP POST of Content-Type "application/json". In that case, the
383  * argument of name "" receives the value of the body of the HTTP request.
384  */
385 struct afb_arg afb_req_get(struct afb_req req, const char *name);
386
387 /*
388  * Gets from the request 'req' the string value of the argument of 'name'.
389  * Returns NULL if when there is no argument of 'name'.
390  * Returns the value of the argument of 'name' otherwise.
391  *
392  * Shortcut for: afb_req_get(req, name).value
393  */
394 const char *afb_req_value(struct afb_req req, const char *name);
395
396 /*
397  * Gets from the request 'req' the path for file attached to the argument of 'name'.
398  * Returns NULL if when there is no argument of 'name' or when there is no file.
399  * Returns the path of the argument of 'name' otherwise.
400  *
401  * Shortcut for: afb_req_get(req, name).path
402  */
403 const char *afb_req_path(struct afb_req req, const char *name);
404
405 /*
406  * Gets from the request 'req' the json object hashing the arguments.
407  * The returned object must not be released using 'json_object_put'.
408  */
409 struct json_object *afb_req_json(struct afb_req req);
410 ```
411
412 The following functions emit the reply to the request.
413
414 ```C
415 /*
416  * Sends a reply of kind success to the request 'req'.
417  * The status of the reply is automatically set to "success".
418  * Its send the object 'obj' (can be NULL) with an
419  * informationnal comment 'info (can also be NULL).
420  *
421  * For convenience, the function calls 'json_object_put' for 'obj'.
422  * Thus, in the case where 'obj' should remain available after
423  * the function returns, the function 'json_object_get' shall be used.
424  */
425 void afb_req_success(struct afb_req req, struct json_object *obj, const char *info);
426
427 /*
428  * Same as 'afb_req_success' but the 'info' is a formatting
429  * string followed by arguments.
430  *
431  * For convenience, the function calls 'json_object_put' for 'obj'.
432  * Thus, in the case where 'obj' should remain available after
433  * the function returns, the function 'json_object_get' shall be used.
434  */
435 void afb_req_success_f(struct afb_req req, struct json_object *obj, const char *info, ...);
436
437 /*
438  * Same as 'afb_req_success_f' but the arguments to the format 'info'
439  * are given as a variable argument list instance.
440  *
441  * For convenience, the function calls 'json_object_put' for 'obj'.
442  * Thus, in the case where 'obj' should remain available after
443  * the function returns, the function 'json_object_get' shall be used.
444  */
445 void afb_req_success_v(struct afb_req req, struct json_object *obj, const char *info, va_list args);
446
447 /*
448  * Sends a reply of kind failure to the request 'req'.
449  * The status of the reply is set to 'status' and an
450  * informationnal comment 'info' (can also be NULL) can be added.
451  *
452  * Note that calling afb_req_fail("success", info) is equivalent
453  * to call afb_req_success(NULL, info). Thus even if possible it
454  * is strongly recommended to NEVER use "success" for status.
455  */
456 void afb_req_fail(struct afb_req req, const char *status, const char *info);
457
458 /*
459  * Same as 'afb_req_fail' but the 'info' is a formatting
460  * string followed by arguments.
461  */
462 void afb_req_fail_f(struct afb_req req, const char *status, const char *info, ...);
463
464 /*
465  * Same as 'afb_req_fail_f' but the arguments to the format 'info'
466  * are given as a variable argument list instance.
467  */
468 void afb_req_fail_v(struct afb_req req, const char *status, const char *info, va_list args);
469 ```
470
471 The following functions handle the session data.
472
473 ```C
474 /*
475  * Gets the pointer stored by the binding for the session of 'req'.
476  * When the binding has not yet recorded a pointer, NULL is returned.
477  */
478 void *afb_req_context_get(struct afb_req req);
479
480 /*
481  * Stores for the binding the pointer 'context' to the session of 'req'.
482  * The function 'free_context' will be called when the session is closed
483  * or if binding stores an other pointer.
484  */
485 void afb_req_context_set(struct afb_req req, void *context, void (*free_context)(void*));
486
487 /*
488  * Gets the pointer stored by the binding for the session of 'req'.
489  * If the stored pointer is NULL, indicating that no pointer was
490  * already stored, afb_req_context creates a new context by calling
491  * the function 'create_context' and stores it with the freeing function
492  * 'free_context'.
493  */
494 void *afb_req_context(struct afb_req req, void *(*create_context)(), void (*free_context)(void*));
495
496 /*
497  * Frees the pointer stored by the binding for the session of 'req'
498  * and sets it to NULL.
499  *
500  * Shortcut for: afb_req_context_set(req, NULL, NULL)
501  */
502 void afb_req_context_clear(struct afb_req req);
503
504 /*
505  * Closes the session associated with 'req'
506  * and delete all associated contexts.
507  */
508 void afb_req_session_close(struct afb_req req);
509
510 /*
511  * Sets the level of assurance of the session of 'req'
512  * to 'level'. The effect of this function is subject of
513  * security policies.
514  * Returns 1 on success or 0 if failed.
515  */
516 int afb_req_session_set_LOA(struct afb_req req, unsigned level);
517 ```
518
519
520 The 4 following functions must be used for asynchronous handling requests.
521
522 ```C
523 /*
524  * Adds one to the count of references of 'req'.
525  * This function MUST be called by asynchronous implementations
526  * of verbs if no reply was sent before returning.
527  */
528 void afb_req_addref(struct afb_req req);
529
530 /*
531  * Substracts one to the count of references of 'req'.
532  * This function MUST be called by asynchronous implementations
533  * of verbs after sending the asynchronous reply.
534  */
535 void afb_req_unref(struct afb_req req);
536
537 /*
538  * Stores 'req' on heap for asynchronous use.
539  * Returns a handler to the stored 'req' or NULL on memory depletion.
540  * The count of reference to 'req' is incremented on success
541  * (see afb_req_addref).
542  */
543 struct afb_stored_req *afb_req_store(struct afb_req req);
544
545 /*
546  * Retrieves the afb_req stored at 'sreq'.
547  * Returns the stored request.
548  * The count of reference is UNCHANGED, thus, the
549  * function 'afb_req_unref' should be called on the result
550  * after that the asynchronous reply if sent.
551  */
552 struct afb_req afb_req_unstore(struct afb_stored_req *sreq);
553 ```
554
555 The two following functions are used to associate client with events
556 (subscription).
557
558 ```C
559 /*
560  * Establishes for the client link identified by 'req' a subscription
561  * to the 'event'.
562  * Returns 0 in case of successful subscription or -1 in case of error.
563  */
564 int afb_req_subscribe(struct afb_req req, struct afb_event event);
565
566 /*
567  * Revokes the subscription established to the 'event' for the client
568  * link identified by 'req'.
569  * Returns 0 in case of successful subscription or -1 in case of error.
570  */
571 int afb_req_unsubscribe(struct afb_req req, struct afb_event event);
572 ```
573
574 The following functions must be used to make request in the name of the
575 client (with its permissions).
576
577 ```C
578 /*
579  * Makes a call to the method of name 'api' / 'verb' with the object 'args'.
580  * This call is made in the context of the request 'req'.
581  * On completion, the function 'callback' is invoked with the
582  * 'closure' given at call and two other parameters: 'iserror' and 'result'.
583  * 'status' is 0 on success or negative when on an error reply.
584  * 'result' is the json object of the reply, you must not call json_object_put
585  * on the result.
586  *
587  * For convenience, the function calls 'json_object_put' for 'args'.
588  * Thus, in the case where 'args' should remain available after
589  * the function returns, the function 'json_object_get' shall be used.
590  */
591 void afb_req_subcall(
592                 struct afb_req req,
593                 const char *api,
594                 const char *verb,
595                 struct json_object *args,
596                 void (*callback)(void *closure, int status, struct json_object *result),
597                 void *closure);
598
599 /*
600  * Makes a call to the method of name 'api' / 'verb' with the object 'args'.
601  * This call is made in the context of the request 'req'.
602  * This call is synchronous, it waits untill completion of the request.
603  * It returns 0 on success or a negative value on error answer.
604  * The object pointed by 'result' is filled and must be released by the caller
605  * after its use by calling 'json_object_put'.
606  *
607  * For convenience, the function calls 'json_object_put' for 'args'.
608  * Thus, in the case where 'args' should remain available after
609  * the function returns, the function 'json_object_get' shall be used.
610  */
611 int afb_req_subcall_sync(
612                 struct afb_req req,
613                 const char *api,
614                 const char *verb,
615                 struct json_object *args,
616                 struct json_object **result);
617 ```
618
619 The following function is used by logging macros and should normally
620 not be used. Instead, you should use the macros
621 **AFB_REQ_ERROR**, **AFB_REQ_WARNING**, **AFB_REQ_NOTICE**,
622 **AFB_REQ_INFO**, **AFB_REQ_DEBUG**
623
624 ```C
625 /*
626  * Send associated to 'req' a message described by 'fmt' and following parameters
627  * to the journal for the verbosity 'level'.
628  *
629  * 'file', 'line' and 'func' are indicators of position of the code in source files
630  * (see macros __FILE__, __LINE__ and __func__).
631  *
632  * 'level' is defined by syslog standard:
633  *      EMERGENCY         0        System is unusable
634  *      ALERT             1        Action must be taken immediately
635  *      CRITICAL          2        Critical conditions
636  *      ERROR             3        Error conditions
637  *      WARNING           4        Warning conditions
638  *      NOTICE            5        Normal but significant condition
639  *      INFO              6        Informational
640  *      DEBUG             7        Debug-level messages
641  */
642 void afb_req_verbose(struct afb_req req, int level, const char *file, int line, const char * func, const char *fmt, ...);
643 ```
644
645 Logging macros
646 --------------
647
648 The following macros must be used for logging:
649
650 ```C
651 AFB_ERROR(fmt,...)
652 AFB_WARNING(fmt,...)
653 AFB_NOTICE(fmt,...)
654 AFB_INFO(fmt,...)
655 AFB_DEBUG(fmt,...)
656 ```
657
658 The following macros can be used for logging in the context
659 of a request **req** of type **afb_req**:
660
661 ```C
662 AFB_REQ_ERROR(req,fmt,...)
663 AFB_REQ_WARNING(req,fmt,...)
664 AFB_REQ_NOTICE(req,fmt,...)
665 AFB_REQ_INFO(req,fmt,...)
666 AFB_REQ_DEBUG(req,fmt,...)
667 ```
668
669 By default, the logging macros add file, line and function
670 indication.