aboutsummaryrefslogtreecommitdiffstats
path: root/include/afb/afb-api-x3.h
blob: ca92bb59397ceb37c0ca090b246a440288bb8b4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
/*
 * Copyright (C) 2016-2019 "IoT.bzh"
 * Author: José Bollo <jose.bollo@iot.bzh>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include "afb-verbosity.h"
#include "afb-api-x3-itf.h"

/** @defgroup AFB_API
 *  @{ */

/**
 * Get the name of the 'api'.
 *
 * @param api the api whose name is to be returned
 *
 * @return the name of the api.
 *
 * The returned value must not be changed nor freed.
 */
static inline
const char *afb_api_x3_name(
			struct afb_api_x3 *api)
{
	return api->apiname;
}

/**
 * Get the "userdata" pointer of the 'api'
 *
 * @param api the api whose user's data is to be returned
 *
 * @return the user's data  pointer of the api.
 *
 * @see afb_api_x3_set_userdata
 */
static inline
void *afb_api_x3_get_userdata(
			struct afb_api_x3 *api)
{
	return api->userdata;
}

/**
 * Set the "userdata" pointer of the 'api' to 'value'
 *
 * @param api   the api whose user's data is to be set
 * @param value the data to set
 *
 * @see afb_api_x3_get_userdata
 */
static inline
void afb_api_x3_set_userdata(
			struct afb_api_x3 *api,
			void *value)
{
	api->userdata = value;
}

/**
 * Is the log message of 'level (as defined for syslog) required for the api?
 *
 * @param api   the api to check
 * @param level the level to check as defined for syslog:
 *
 *      EMERGENCY         0        System is unusable
 *      ALERT             1        Action must be taken immediately
 *      CRITICAL          2        Critical conditions
 *      ERROR             3        Error conditions
 *      WARNING           4        Warning conditions
 *      NOTICE            5        Normal but significant condition
 *      INFO              6        Informational
 *      DEBUG             7        Debug-level messages
 *
 * @return 0 if not required or a value not null if required
 *
 * @see syslog
 */
static inline
int afb_api_x3_wants_log_level(
			struct afb_api_x3 *api,
			int level)
{
	return AFB_SYSLOG_MASK_WANT(api->logmask, level);
}

/**
 * Send to the journal with the logging 'level' a message described
 * by 'fmt' applied to the va-list 'args'.
 *
 * 'file', 'line' and 'func' are indicators of code position in source files
 * (see macros __FILE__, __LINE__ and __func__).
 *
 * 'level' is defined by syslog standard:
 *
 *      EMERGENCY         0        System is unusable
 *      ALERT             1        Action must be taken immediately
 *      CRITICAL          2        Critical conditions
 *      ERROR             3        Error conditions
 *      WARNING           4        Warning conditions
 *      NOTICE            5        Normal but significant condition
 *      INFO              6        Informational
 *      DEBUG             7        Debug-level messages
 *
 * @param api the api that collects the logging message
 * @param level the level of the message
 * @param file the source file that logs the messages or NULL
 * @param line the line in the source file that logs the message
 * @param func the name of the function in the source file that logs (or NULL)
 * @param fmt the format of the message as in printf
 * @param args the arguments to the format string of the message as a va_list
 *
 * @see syslog
 * @see printf
 */
static inline
void afb_api_x3_vverbose(
			struct afb_api_x3 *api,
			int level,
			const char *file,
			int line,
			const char *func,
			const char *fmt,
			va_list args)
{
	api->itf->vverbose(api, level, file, line, func, fmt, args);
}

/**
 * Send to the journal with the log 'level' a message described
 * by 'fmt' and following parameters.
 *
 * 'file', 'line' and 'func' are indicators of position of the code in source files
 * (see macros __FILE__, __LINE__ and __func__).
 *
 * 'level' is defined by syslog standard:
 *      EMERGENCY         0        System is unusable
 *      ALERT             1        Action must be taken immediately
 *      CRITICAL          2        Critical conditions
 *      ERROR             3        Error conditions
 *      WARNING           4        Warning conditions
 *      NOTICE            5        Normal but significant condition
 *      INFO              6        Informational
 *      DEBUG             7        Debug-level messages
 *
 * @param api the api that collects the logging message
 * @param level the level of the message
 * @param file the source file that logs the messages or NULL
 * @param line the line in the source file that logs the message
 * @param func the name of the function in the source file that logs (or NULL)
 * @param fmt the format of the message as in printf
 * @param ... the arguments to the format string of the message
 *
 * @see syslog
 * @see printf
 */
__attribute__((format(printf, 6, 7)))
static inline
void afb_api_x3_verbose(
		struct afb_api_x3 *api,
		int level,
		const char *file,
		int line,
		const char *func,
		const char *fmt,
		...)
{
	va_list args;
	va_start(args, fmt);
	api->itf->vverbose(api, level, file, line, func, fmt, args);
	va_end(args);
}

/**
 * Retrieves the common systemd's event loop of AFB
 *
 * @param api the api that uses the event loop
 *
 * @return the systemd event loop if active, NULL otherwise
 *
 * @see afb_api_x3_get_user_bus
 * @see afb_api_x3_get_system_bus
 */
static inline
struct sd_event *afb_api_x3_get_event_loop(
			struct afb_api_x3 *api)
{
	return api->itf->get_event_loop(api);
}

/**
 * Retrieves the common systemd's user/session d-bus of AFB
 *
 * @param api the api that uses the user dbus
 *
 * @return the systemd user connection to dbus if active, NULL otherwise
 *
 * @see afb_api_x3_get_event_loop
 * @see afb_api_x3_get_system_bus
 */
static inline
struct sd_bus *afb_api_x3_get_user_bus(
			struct afb_api_x3 *api)
{
	return api->itf->get_user_bus(api);
}

/**
 * Retrieves the common systemd's system d-bus of AFB
 *
 * @param api the api that uses the system dbus
 *
 * @return the systemd system connection to dbus if active, NULL otherwise
 *
 * @see afb_api_x3_get_event_loop
 * @see afb_api_x3_get_user_bus
 */

static inline
struct sd_bus *afb_api_x3_get_system_bus(
			struct afb_api_x3 *api)
{
	return api->itf->get_system_bus(api);
}

/**
 * Get the root directory file descriptor. This file descriptor can
 * be used with functions 'openat', 'fstatat', ...
 *
 * CAUTION, manipulate this this descriptor with care, in particular, don't close
 * it.
 *
 * This can be used to get the path of the root directory using:
 *
 * ```C
 * char buffer[MAX_PATH], proc[100];
 * int dirfd = afb_api_rootdir_get_fd(api);
 * snprintf(proc, sizeof proc, "/proc/self/fd/%d", dirfd);
 * readlink(proc, buffer, sizeof buffer);
 * ```
 *
 * But note that within AGL this is the value given by the environment variable
 * AFM_APP_INSTALL_DIR.
 *
 * @param api the api that uses the directory file descriptor
 *
 * @return the file descriptor of the root directory.
 *
 * @see afb_api_x3_rootdir_open_locale
 */
static inline
int afb_api_x3_rootdir_get_fd(
			struct afb_api_x3 *api)
{
	return api->itf->rootdir_get_fd(api);
}

/**
 * Opens 'filename' within the root directory with 'flags' (see function openat)
 * using the 'locale' definition (example: "jp,en-US") that can be NULL.
 *
 * The filename must be relative to the root of the bindings.
 *
 * The opening mode must be for read or write but not for O_CREAT.
 *
 * The definition of locales and of how files are searched can be checked
 * here: https://www.w3.org/TR/widgets/#folder-based-localization
 * and https://tools.ietf.org/html/rfc7231#section-5.3.5
 *
 * @param api the api that queries the file
 * @param filename the relative path to the file to open
 * @param flags the flags for opening as for C function 'open'
 * @param locale string indicating how to search content, can be NULL
 *
 * @return the file descriptor or -1 in case of error and errno is set with the
 * error indication.
 *
 * @see open
 * @see afb_api_x3_rootdir_get_fd
 */
static inline
int afb_api_x3_rootdir_open_locale(
			struct afb_api_x3 *api,
			const char *filename,
			int flags,
			const char *locale)
{
	return api->itf->rootdir_open_locale(api, filename, flags, locale);
}

/**
 * Queue the job defined by 'callback' and 'argument' for being executed asynchronously
 * in this thread (later) or in an other thread.
 *
 * If 'group' is not NULL, the jobs queued with a same value (as the pointer value 'group')
 * are executed in sequence in the order of there submission.
 *
 * If 'timeout' is not 0, it represent the maximum execution time for the job in seconds.
 * At first, the job is called with 0 as signum and the given argument.
 *
 * The job is executed with the monitoring of its time and some signals like SIGSEGV and
 * SIGFPE. When a such signal is catched, the job is terminated and reexecuted but with
 * signum being the signal number (SIGALRM when timeout expired).
 *
 * When executed, the callback function receives 2 arguments:
 *
 *  - int signum: the signal catched if any or zero at the beginning
 *  - void *arg: the parameter 'argument'
 *
 * A typical implementation of the job callback is:
 *
 * ```C
 * void my_job_cb(int signum, void *arg)
 * {
 *	struct myarg_t *myarg = arg;
 *	if (signum)
 *		AFB_API_ERROR(myarg->api, "job interrupted with signal %s", strsignal(signum));
 *	else
 *		really_do_my_job(myarg);
 * }
 * ```
 *
 * @param api the api that queue the job
 * @param callback the job as a callback function
 * @param argument the argument to pass to the queued job
 * @param group the group of the job, NULL if no group
 * @param timeout the timeout of execution of the job
 *
 * @return 0 in case of success or -1 in case of error with errno set appropriately.
 */
static inline
int afb_api_x3_queue_job(
			struct afb_api_x3 *api,
			void (*callback)(int signum, void *arg),
			void *argument,
			void *group,
			int timeout)
{
	return api->itf->queue_job(api, callback, argument, group, timeout);
}

/**
 * Check that it requires the API of 'name'.
 * If 'initialized' is not zero it request the API to be
 * initialized, implying its initialization if needed.
 *
 * Calling this function is only allowed within init.
 *
 * A single request allows to require multiple apis.
 *
 * @param api the api that requires the other api by its name
 * @param name a space separated list of required api names
 * @param initialized if zero, the api is just required to exist. If not zero,
 * the api is required to exist and to be initialized at return of the call
 * (initializing it if needed and possible as a side effect of the call).
 *
 * @return 0 in case of success or -1 in case of error with errno set appropriately.
 */
static inline
int afb_api_x3_require_api(
			struct afb_api_x3 *api,
			const char *name,
			int initialized)
{
	return api->itf->require_api(api, name, initialized);
}

/**
 * Create an aliased name 'as_name' for the api 'name'.
 * Calling this function is only allowed within preinit.
 *
 * @param api the api that requires the aliasing
 * @param name the api to alias
 * @param as_name the aliased name of the aliased api
 *
 * @return 0 in case of success or -1 in case of error with errno set appropriately.
 */
static inline
int afb_api_x3_add_alias(
			struct afb_api_x3 *api,
			const char *name,
			const char *as_name)
{
	return api->itf->add_alias(api, name, as_name);
}

/**
 * Broadcasts widely the event of 'name' with the data 'object'.
 * 'object' can be NULL.
 *
 * For convenience, the function calls 'json_object_put' for 'object'.
 * Thus, in the case where 'object' should remain available after
 * the function returns, the function 'json_object_get' shall be used.
 *
 * Calling this function is only forbidden during preinit.
 *
 * The event sent as the name API/name where API is the name of the
 * api.
 *
 * @param api the api that broadcast the event
 * @param name the event name suffix
 * @param object the object that comes with the event
 *
 * @return the count of clients that received the event.
 */
static inline
int afb_api_x3_broadcast_event(
			struct afb_api_x3 *api,
			const char *name,
			struct json_object *object)
{
	return api->itf->event_broadcast(api, name, object);
}

/**
 * Creates an event of 'name' and returns it.
 *
 * Calling this function is only forbidden during preinit.
 *
 * See afb_event_is_valid to check if there is an error.
 *
 * The event created as the name API/name where API is the name of the
 * api.
 *
 * @param api the api that creates the event
 * @param name the event name suffix
 *
 * @return the created event. Use the function afb_event_is_valid to check
 * whether the event is valid (created) or not (error as reported by errno).
 *
 * @see afb_event_is_valid
 */
static inline
struct afb_event_x2 *afb_api_x3_make_event_x2(
			struct afb_api_x3 *api,
			const char *name)
{
	return api->itf->event_make(api, name);
}

/**
 * @deprecated try to use @ref afb_api_x3_call instead
 *
 *  * Calls the 'verb' of the 'api' with the arguments 'args' and 'verb' in the name of the binding.
 * The result of the call is delivered to the 'callback' function with the 'callback_closure'.
 *
 * For convenience, the function calls 'json_object_put' for 'args'.
 * Thus, in the case where 'args' should remain available after
 * the function returns, the function 'json_object_get' shall be used.
 *
 * The 'callback' receives 3 arguments:
 *  1. 'closure' the user defined closure pointer 'closure',
 *  2. 'status' a status being 0 on success or negative when an error occurred,
 *  2. 'result' the resulting data as a JSON object.
 *
 * @param api      The api
 * @param apiname  The api name of the method to call
 * @param verb     The verb name of the method to call
 * @param args     The arguments to pass to the method
 * @param callback The to call on completion
 * @param closure  The closure to pass to the callback
 *
 * @see also 'afb_api_call'
 * @see also 'afb_api_call_sync'
 * @see also 'afb_api_call_sync_legacy'
 * @see also 'afb_req_subcall'
 * @see also 'afb_req_subcall_sync'
 */
static inline
void afb_api_x3_call_legacy(
			struct afb_api_x3 *api,
			const char *apiname,
			const char *verb,
			struct json_object *args,
			void (*callback)(void *closure,
					int status,
					struct json_object *result,
					struct afb_api_x3 *api),
			void *closure)
{
	api->itf->legacy_call(api, apiname, verb, args, callback, closure);
}

/**
 * @deprecated try to use @ref afb_api_x3_call_sync instead
 *
 * Calls the 'verb' of the 'api' with the arguments 'args' and 'verb' in the name of the binding.
 * 'result' will receive the response.
 *
 * For convenience, the function calls 'json_object_put' for 'args'.
 * Thus, in the case where 'args' should remain available after
 * the function returns, the function 'json_object_get' shall be used.
 *
 * @param api      The api
 * @param apiname  The api name of the method to call
 * @param verb     The verb name of the method to call
 * @param args     The arguments to pass to the method
 * @param result   Where to store the result - should call json_object_put on it -
 *
 * @returns 0 in case of success or a negative value in case of error.
 *
 * @see also 'afb_api_call'
 * @see also 'afb_api_call_sync'
 * @see also 'afb_api_call_legacy'
 * @see also 'afb_req_subcall'
 * @see also 'afb_req_subcall_sync'
 */
static inline
int afb_api_x3_call_sync_legacy(
			struct afb_api_x3 *api,
			const char *apiname,
			const char *verb,
			struct json_object *args,
			struct json_object **result)
{
	return api->itf->legacy_call_sync(api, apiname, verb, args, result);
}

/**
 * Creates a new api of name 'apiname' briefly described by 'info' (that can
 * be NULL).
 *
 * When the pre-initialization function is given, it is a function that
 * receives 2 parameters:
 *
 *  - the closure as given in the call
 *  - the created api that can be initialised
 *
 * This pre-initialization function must return a negative value to abort
 * the creation of the api. Otherwise, it returns a non-negative value to
 * continue.
 *
 * @param api the api that creates the other one
 * @param apiname the name of the new api
 * @param info the brief description of the new api (can be NULL)
 * @param noconcurrency zero or not zero whether the new api is reentrant or not
 * @param preinit the pre-initialization function if any (can be NULL)
 * @param closure the closure for the pre-initialization preinit
 *
 * @return the created api in case of success or NULL on error
 *
 * @see afb_api_x3_delete_api
 */
static inline
struct afb_api_x3 *afb_api_x3_new_api(
			struct afb_api_x3 *api,
			const char *apiname,
			const char *info,
			int noconcurrency,
			int (*preinit)(void*, struct afb_api_x3 *),
			void *closure)
{
	return api->itf->api_new_api(api, apiname, info, noconcurrency, preinit, closure);
}

/**
 * @deprecated use @ref afb_api_x3_set_verbs_v3 instead
 *
 * Set the verbs of the 'api' using description of verbs of the api v2
 *
 * @param api the api that will get the verbs
 * @param verbs the array of verbs to add terminated with an item with name=NULL
 *
 * @return 0 in case of success or -1 on failure with errno set
 *
 * @see afb_verb_v2
 * @see afb_api_x3_add_verb
 * @see afb_api_x3_set_verbs_v3
 */
static inline
int afb_api_x3_set_verbs_v2(
			struct afb_api_x3 *api,
			const struct afb_verb_v2 *verbs)
{
	return api->itf->api_set_verbs_v2(api, verbs);
}

/**
 * Add one verb to the dynamic set of the api
 *
 * @param api the api to change
 * @param verb name of the verb
 * @param info brief description of the verb, can be NULL
 * @param callback callback function implementing the verb
 * @param vcbdata data for the verb callback, available through req
 * @param auth required authorization, can be NULL
 * @param session authorization and session requirements of the verb
 * @param glob is the verb glob name
 *
 * @return 0 in case of success or -1 on failure with errno set
 *
 * @see afb_verb_v3
 * @see afb_api_x3_del_verb
 * @see afb_api_x3_set_verbs_v3
 * @see fnmatch for matching names using glob
 */
static inline
int afb_api_x3_add_verb(
			struct afb_api_x3 *api,
			const char *verb,
			const char *info,
			void (*callback)(struct afb_req_x2 *req),
			void *vcbdata,
			const struct afb_auth *auth,
			uint32_t session,
			int glob)
{
	return api->itf->api_add_verb(api, verb, info, callback, vcbdata, auth, session, glob);
}

/**
 * Delete one verb from the dynamic set of the api
 *
 * @param api the api to change
 * @param verb name of the verb to delete
 * @param vcbdata if not NULL will receive the vcbdata of the deleted verb
 *
 * @return 0 in case of success or -1 on failure with errno set
 *
 * @see afb_api_x3_add_verb
 */
static inline
int afb_api_x3_del_verb(
			struct afb_api_x3 *api,
			const char *verb,
			void **vcbdata)
{
	return api->itf->api_del_verb(api, verb, vcbdata);
}

/**
 * Set the callback 'onevent' to process events in the name of the 'api'.
 *
 * This setting can be done statically using the global variable
 * @ref afbBindingV3.
 *
 * This function replace any previously global event callback set.
 *
 * When an event is received, the callback 'onevent' is called with 3 parameters
 *
 *  - the api that recorded the event handler
 *  - the full name of the event
 *  - the companion JSON object of the event
 *
 * @param api the api that wants to process events
 * @param onevent the callback function that will process events (can be NULL
 *        to remove event callback)
 *
 * @return 0 in case of success or -1 on failure with errno set
 *
 * @see afbBindingV3
 * @see afb_binding_v3
 * @see afb_api_x3_event_handler_add
 * @see afb_api_x3_event_handler_del
 */
static inline
int afb_api_x3_on_event(
			struct afb_api_x3 *api,
			void (*onevent)(
				struct afb_api_x3 *api,
				const char *event,
				struct json_object *object))
{
	return api->itf->api_set_on_event(api, onevent);
}

/**
 * Set the callback 'oninit' to process initialization of the 'api'.
 *
 * This setting can be done statically using the global variable
 * @ref afbBindingV3
 *
 * This function replace any previously initialization callback set.
 *
 * This function is only valid during the pre-initialization stage.
 *
 * The callback initialization function will receive one argument: the api
 * to initialize.
 *
 * @param api the api that wants to process events
 * @param oninit the callback function that initialize the api
 *
 * @return 0 in case of success or -1 on failure with errno set
 *
 * @see afbBindingV3
 * @see afb_binding_v3
 */
static inline
int afb_api_x3_on_init(
			struct afb_api_x3 *api,
			int (*oninit)(struct afb_api_x3 *api))
{
	return api->itf->api_set_on_init(api, oninit);
}

/**
 * Seal the api. After a call to this function the api can not be modified
 * anymore.
 *
 * @param api the api to be sealed
 */
static inline
void afb_api_x3_seal(
			struct afb_api_x3 *api)
{
	api->itf->api_seal(api);
}

/**
 * Set the verbs of the 'api' using description of verbs of the api v2
 *
 * @param api the api that will get the verbs
 * @param verbs the array of verbs to add terminated with an item with name=NULL
 *
 * @return 0 in case of success or -1 on failure with errno set
 *
 * @see afb_verb_v3
 * @see afb_api_x3_add_verb
 * @see afb_api_x3_del_verb
 */
static inline
int afb_api_x3_set_verbs_v3(
			struct afb_api_x3 *api,
			const struct afb_verb_v3 *verbs)
{
	return api->itf->api_set_verbs_v3(api, verbs);
}

/**
 * Add a specific event handler for the api
 *
 * The handler callback is called when an event matching the given pattern
 * is received (it is received if broadcasted or after subscription through
 * a call or a subcall).
 *
 * The handler callback receive 4 arguments:
 *
 *  - the closure given here
 *  - the event full name
 *  - the companion JSON object of the event
 *  - the api that subscribed the event
 *
 * @param api the api that creates the handler
 * @param pattern the global pattern of the event to handle
 * @param callback the handler callback function
 * @param closure the closure of the handler
 *
 * @return 0 in case of success or -1 on failure with errno set
 *
 * @see afb_api_x3_on_event
 * @see afb_api_x3_event_handler_del
 */
static inline
int afb_api_x3_event_handler_add(
			struct afb_api_x3 *api,
			const char *pattern,
			void (*callback)(
				void *,
				const char*,
				struct json_object*,
				struct afb_api_x3*),
			void *closure)
{
	return api->itf->event_handler_add(api, pattern, callback, closure);
}

/**
 * Delete a specific event handler for the api
 *
 * @param api the api that the handler belongs to
 * @param pattern the global pattern of the handler to remove
 * @param closure if not NULL it will receive the closure set to the handler
 *
 * @return 0 in case of success or -1 on failure with errno set
 *
 * @see afb_api_x3_on_event
 * @see afb_api_x3_event_handler_add
 */
static inline
int afb_api_x3_event_handler_del(
			struct afb_api_x3 *api,
			const char *pattern,
			void **closure)
{
	return api->itf->event_handler_del(api, pattern, closure);
}

/**
 * Calls the 'verb' of the 'api' with the arguments 'args' and 'verb' in the name of the binding.
 * The result of the call is delivered to the 'callback' function with the 'callback_closure'.
 *
 * For convenience, the function calls 'json_object_put' for 'args'.
 * Thus, in the case where 'args' should remain available after
 * the function returns, the function 'json_object_get' shall be used.
 *
 * The 'callback' receives 5 arguments:
 *  1. 'closure' the user defined closure pointer 'closure',
 *  2. 'object'  a JSON object returned (can be NULL)
 *  3. 'error'   a string not NULL in case of error but NULL on success
 *  4. 'info'    a string handling some info (can be NULL)
 *  5. 'api'     the api
 *
 * NOTE: For convenience, *json_object_put* is called on 'object' after the
 * callback returns. So, it is wrong to call *json_object_put* in the callback.
 *
 * @param api      The api that makes the call
 * @param apiname  The api name of the method to call
 * @param verb     The verb name of the method to call
 * @param args     The arguments to pass to the method
 * @param callback The to call on completion
 * @param closure  The closure to pass to the callback
 *
 * @see afb_req_subcall
 * @see afb_req_subcall_sync
 * @see afb_api_x3_call_sync
 */
static inline
void afb_api_x3_call(
			struct afb_api_x3 *api,
			const char *apiname,
			const char *verb,
			struct json_object *args,
			void (*callback)(
				void *closure,
				struct json_object *object,
				const char *error,
				const char * info,
				struct afb_api_x3 *api),
			void *closure)
{
	api->itf->call(api, apiname, verb, args, callback, closure);
}

/**
 * Calls the 'verb' of the 'api' with the arguments 'args' and 'verb' in the name of the binding.
 * 'result' will receive the response.
 *
 * For convenience, the function calls 'json_object_put' for 'args'.
 * Thus, in the case where 'args' should remain available after
 * the function returns, the function 'json_object_get' shall be used.
 *
 * @param api      The api that makes the call
 * @param apiname  The api name of the method to call
 * @param verb     The verb name of the method to call
 * @param args     The arguments to pass to the method
 * @param object   Where to store the returned object - should call json_object_put on it - can be NULL
 * @param error    Where to store the copied returned error - should call free on it - can be NULL
 * @param info     Where to store the copied returned info - should call free on it - can be NULL
 *
 * @returns 0 in case of success or a negative value in case of error.
 *
 * @see afb_req_subcall
 * @see afb_req_subcall_sync
 * @see afb_api_x3_call
 */
static inline
int afb_api_x3_call_sync(
			struct afb_api_x3 *api,
			const char *apiname,
			const char *verb,
			struct json_object *args,
			struct json_object **object,
			char **error,
			char **info)
{
	return api->itf->call_sync(api, apiname, verb, args, object, error, info);
}

/**
 * Tells that the api provides a class of features. Classes are intended to
 * allow ordering of initializations: apis that provides a given class are
 * initialized before apis requiring it.
 *
 * This function is only valid during the pre-initialization stage.
 *
 * @param api  the api that provides the classes
 * @param name a space separated list of the names of the provided classes
 *
 * @returns 0 in case of success or a negative value in case of error.
 *
 * @see afb_api_x3_require_class
 */
static inline
int afb_api_x3_provide_class(
			struct afb_api_x3 *api,
			const char *name)
{
	return api->itf->class_provide(api, name);
}

/**
 * Tells that the api requires a set of class features. Classes are intended to
 * allow ordering of initializations: apis that provides a given class are
 * initialized before apis requiring it.
 *
 * This function is only valid during the pre-initialization stage.
 *
 * @param api  the api that requires the classes
 * @param name a space separated list of the names of the required classes
 *
 * @returns 0 in case of success or a negative value in case of error.
 *
 * @see afb_api_x3_provide_class
 */
static inline
int afb_api_x3_require_class(
			struct afb_api_x3 *api,
			const char *name)
{
	return api->itf->class_require(api, name);
}

/**
 * Delete the given api.
 *
 * It is of the responsibility of the caller to don't used the deleted api
 * anymore after this function returned.
 *
 * @param api the api to delete
 *
 * @returns 0 in case of success or a negative value in case of error.
 *
 * @see afb_api_x3_new_api
 */
static inline
int afb_api_x3_delete_api(
			struct afb_api_x3 *api)
{
	return api->itf->delete_api(api);
}

/**
 * Settings of the api.
 *
 * Get the settings of the API. The settings are recorded
 * as a JSON object. The returned object should not be modified.
 * It MUST NOT be released using json_object_put.
 *
 * @param api the api whose settings are required
 *
 * @returns The setting object.
 */
static inline
struct json_object *afb_api_x3_settings(
			struct afb_api_x3 *api)
{
	return api->itf->settings(api);
}

/** @} */