Update copyright dates
[src/app-framework-binder.git] / include / afb / c++ / binding-wrap.hpp
1 /*
2  * Copyright (C) 2015-2020 "IoT.bzh"
3  * Author: José Bollo <jose.bollo@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #pragma once
19
20 #include <cstddef>
21 #include <cstdlib>
22 #include <cstdarg>
23 #include <functional>
24
25 /* ensure version */
26 #ifndef AFB_BINDING_VERSION
27 # define AFB_BINDING_VERSION   3
28 #endif
29
30 /* check the version */
31 #if AFB_BINDING_VERSION < 3
32 # error "AFB_BINDING_VERSION must be at least 3"
33 #endif
34
35 /* get C definitions of bindings */
36 extern "C" {
37 #include <afb/afb-binding.h>
38 }
39
40 namespace afb {
41 /*************************************************************************/
42 /* pre-declaration of classes                                            */
43 /*************************************************************************/
44
45 class api;
46 class arg;
47 class event;
48 class req;
49
50 /*************************************************************************/
51 /* declaration of functions                                              */
52 /*************************************************************************/
53
54 int broadcast_event(const char *name, json_object *object = nullptr);
55
56 event make_event(const char *name);
57
58 void verbose(int level, const char *file, int line, const char * func, const char *fmt, va_list args);
59
60 void verbose(int level, const char *file, int line, const char * func, const char *fmt, ...);
61
62 int rootdir_get_fd();
63
64 int rootdir_open_locale_fd(const char *filename, int flags, const char *locale = nullptr);
65
66 int queue_job(void (*callback)(int signum, void *arg), void *argument, void *group, int timeout);
67
68 int require_api(const char *apiname, bool initialized = true);
69
70 int add_alias(const char *apiname, const char *aliasname);
71
72 int verbosity();
73
74 bool wants_errors();
75 bool wants_warnings();
76 bool wants_notices();
77 bool wants_infos();
78 bool wants_debugs();
79
80 void call(const char *api, const char *verb, struct json_object *args, void (*callback)(void*closure, int iserror, struct json_object *result, afb_api_t api), void *closure);
81
82 template <class T> void call(const char *api, const char *verb, struct json_object *args, void (*callback)(T*closure, int iserror, struct json_object *result, afb_api_t api), T *closure);
83
84 bool callsync(const char *api, const char *verb, struct json_object *args, struct json_object *&result);
85
86 /*************************************************************************/
87 /* effective declaration of classes                                      */
88 /*************************************************************************/
89
90 /* apis */
91 class api
92 {
93 protected:
94         afb_api_t api_;
95 public:
96         using call_cb = void (*)(void *closure, struct json_object *object, const char *error, const char *info, afb_api_t api);
97         using queue_cb = void (*)(int signum, void *arg);
98         using event_cb = void (*)(void *, const char *, struct json_object *, afb_api_t);
99         using preinit_cb = int (*)(void *, afb_api_t);
100         using verb_cb = void (*)(afb_req_t req);
101         using onevent_cb = void (*)(afb_api_t api, const char *event, struct json_object *object);
102         using oninit_cb = int (*)(afb_api_t api);
103
104         api();
105         api(afb_api_t a);
106         api(const api &other) = delete;
107         api(api &&other);
108         ~api();
109         api &operator=(const api &other) = delete;
110         api &operator=(api &&other);
111
112         operator afb_api_t() const;
113         afb_api_t operator->() const;
114
115         /* General functions */
116         const char *name() const;
117         void *get_userdata() const;
118         void set_userdata(void *value) const;
119         int require_api(const char *name, int initialized) const;
120         int require_api(const std::string &name, int initialized) const;
121
122         /* Verbosity functions */
123         int wants_log_level(int level) const;
124         void vverbose(int level, const char *file, int line, const char *func, const char *fmt, va_list args) const;
125         void verbose(int level, const char *file, int line, const char *func, const char *fmt, ...) const;
126
127         /* Data retrieval functions */
128         int rootdir_get_fd() const;
129         int rootdir_open_locale(const char *filename, int flags, const char *locale) const;
130         int rootdir_open_locale(const std::string &filename, int flags, const std::string &locale) const;
131         struct json_object *settings() const;
132
133         /* Calls and job functions */
134         void call(const char *apiname, const char *verb, struct json_object *args, call_cb callback, void *closure) const;
135         void call(const std::string &apiname, const std::string &verb, struct json_object *args, call_cb callback, void *closure) const;
136         int call_sync(const char *apiname, const char *verb, struct json_object *args, struct json_object **object, char **error, char **info) const;
137         int call_sync(const std::string &apiname, const std::string &verb, struct json_object *args, struct json_object **object, std::string &error, std::string &info) const;
138         int queue_job(queue_cb callback, void *argument, void *group, int timeout) const;
139
140         /* Event functions */
141         int broadcast_event(const char *name, struct json_object *object) const;
142         int broadcast_event(const std::string &name, struct json_object *object) const;
143         event make_event(const char *name) const;
144         event make_event(const std::string &name) const;
145         int event_handler_add(const char *pattern, event_cb callback, void *closure) const;
146         int event_handler_add(const std::string &pattern, event_cb callback, void *closure) const;
147         int event_handler_del(const char *pattern, void **closure) const;
148         int event_handler_del(const std::string &pattern, void **closure) const;
149
150         /* Systemd functions */
151         struct sd_event *get_event_loop() const;
152         struct sd_bus *get_user_bus() const;
153         struct sd_bus *get_system_bus() const;
154
155         /* Dynamic api functions */
156         api new_api(const char *apiname, const char *info, int noconcurrency, preinit_cb preinit, void *closure) const;
157         api new_api(const std::string &apiname, const std::string &info, int noconcurrency, preinit_cb preinit, void *closure) const;
158         int set_verbs(const struct afb_verb_v2 *verbs) const;
159         int set_verbs(const struct afb_verb_v3 *verbs) const;
160         int add_verb(const char *verb, const char *info, verb_cb callback, void *vcbdata, const struct afb_auth *auth, uint32_t session, int glob) const;
161         int add_verb(const std::string &verb, const std::string &info, verb_cb callback, void *vcbdata, const struct afb_auth *auth, uint32_t session, int glob) const;
162         int del_verb(const char *verb, void **vcbdata) const;
163         int del_verb(const std::string &verb, void **vcbdata) const;
164         int on_event(onevent_cb onevent) const;
165         int on_init(oninit_cb oninit) const;
166         int provide_class(const char *name) const;
167         int provide_class(const std::string &name) const;
168         int require_class(const char *name) const;
169         int require_class(const std::string &name) const;
170         void seal() const;
171         int delete_api() const;
172         int add_alias(const char *name, const char *as_name) const;
173         int add_alias(const std::string &name, const std::string &as_name) const;
174 };
175
176 /* events */
177 class event
178 {
179         afb_event_t event_;
180 public:
181         event();
182         event(afb_event_t e);
183         event(const event &other);
184         event(event &&other);
185         ~event();
186         event &operator=(const event &other);
187         event &operator=(event &&other);
188
189         operator afb_event_t() const;
190         afb_event_t operator->() const;
191
192         operator bool() const;
193         bool is_valid() const;
194
195         int broadcast(json_object *object) const;
196         int push(json_object *object) const;
197
198         void unref();
199         void addref();
200         const char *name() const;
201 };
202
203 /* args */
204 class arg
205 {
206         struct afb_arg arg_;
207 public:
208         arg() = delete;
209         arg(const struct afb_arg &a);
210         arg(const arg &other);
211         arg &operator=(const arg &other);
212
213         operator const struct afb_arg&() const;
214
215         bool has_name() const;
216         bool has_value() const;
217         bool has_path() const;
218
219         const char *name() const;
220         const char *value() const;
221         const char *path() const;
222 };
223
224 /* req(uest) */
225 class req
226 {
227         afb_req_t req_;
228
229 public:
230         req() = delete;
231         req(afb_req_t r);
232         req(const req &other);
233         req &operator=(const req &other);
234
235         operator afb_req_t() const;
236         afb_req_t operator->() const;
237
238         operator bool() const;
239         bool is_valid() const;
240
241         arg get(const char *name) const;
242
243         const char *value(const char *name) const;
244
245         const char *path(const char *name) const;
246
247         json_object *json() const;
248
249         void reply(json_object *obj = nullptr, const char *error = nullptr, const char *info = nullptr) const;
250         void replyf(json_object *obj, const char *error, const char *info, ...) const;
251         void replyv(json_object *obj, const char *error, const char *info, va_list args) const;
252
253         void success(json_object *obj = nullptr, const char *info = nullptr) const;
254         void successf(json_object *obj, const char *info, ...) const;
255         void successv(json_object *obj, const char *info, va_list args) const;
256
257         void fail(const char *error = "failed", const char *info = nullptr) const;
258         void failf(const char *error, const char *info, ...) const;
259         void failv(const char *error, const char *info, va_list args) const;
260
261         void addref() const;
262
263         void unref() const;
264
265         void session_close() const;
266
267         bool session_set_LOA(unsigned level) const;
268
269         bool subscribe(const event &event) const;
270
271         bool unsubscribe(const event &event) const;
272
273         void subcall(const char *api, const char *verb, json_object *args, void (*callback)(void *closure, int iserror, json_object *result, afb_req_t req), void *closure) const;
274         template <class T> void subcall(const char *api, const char *verb, json_object *args, void (*callback)(T *closure, int iserror, json_object *result, afb_req_t req), T *closure) const;
275
276         bool subcallsync(const char *api, const char *verb, json_object *args, struct json_object *&result) const;
277
278         void subcall(const char *api, const char *verb, json_object *args, int flags, void (*callback)(void *closure, json_object *object, const char *error, const char *info, afb_req_t req), void *closure) const;
279
280         template <class T> void subcall(const char *api, const char *verb, json_object *args, int flags, void (*callback)(T *closure, json_object *object, const char *error, const char *info, afb_req_t req), T *closure) const;
281
282         bool subcallsync(const char *api, const char *verb, json_object *args, int flags, struct json_object *&object, char *&error, char *&info) const;
283
284         void verbose(int level, const char *file, int line, const char * func, const char *fmt, va_list args) const;
285
286         void verbose(int level, const char *file, int line, const char * func, const char *fmt, ...) const;
287
288         bool has_permission(const char *permission) const;
289
290         char *get_application_id() const;
291
292         int get_uid() const;
293
294         json_object *get_client_info() const;
295
296         template < class T = void >
297         class contextclass {
298
299                 friend class req;
300                 afb_req_t req_;
301                 contextclass(afb_req_t r) : req_(r) {}
302
303         public:
304                 inline operator T *() const { return get(); }
305                 inline operator T &() const { return *get(); }
306                 inline T* get() const {
307                         return reinterpret_cast<T*>(
308                                 afb_req_context(req_, 0,
309                                         nullptr,
310                                         nullptr,
311                                         nullptr));
312                 }
313
314                 inline void set(T *value, void (*destroyer)(T*) = [](T*t){delete t;}) const {
315                         afb_req_context(req_, 1,
316                                 nullptr,
317                                 reinterpret_cast<void(*)(void*)>(destroyer),
318                                 reinterpret_cast<void*>(value));
319                 }
320
321                 inline void unset() { set(nullptr); }
322                 inline void clear() { set(nullptr); }
323
324                 inline T *lazy(T *(*allocator)() = []()->T*{return new T();}, void (*destroyer)(T*) = [](T*t){delete t;}) const {
325                         return reinterpret_cast<T*>(
326                                 afb_req_context(req_, 0,
327                                         [allocator](void*)->T*{return allocator();},
328                                         reinterpret_cast<void(*)(void*)>(destroyer),
329                                         nullptr));
330                 }
331
332                 template <class I>
333                 inline T *lazy(I *i, T *(*allocator)(I*) = [](I*i)->T*{return new T(i);}, void (*destroyer)(T*) = [](T*t){delete t;}) const {
334                         return reinterpret_cast<T*>(
335                                 afb_req_context(req_, 0,
336                                         [allocator](void*i)->T*{return allocator(reinterpret_cast<I*>(i));},
337                                         reinterpret_cast<void(*)(void*)>(destroyer),
338                                         reinterpret_cast<void*>(i)));
339                 }
340         };
341
342         template < class T > contextclass<T> context() const { return contextclass<T>(req_); }
343 };
344
345 /*************************************************************************/
346 /* effective declaration of classes                                      */
347 /*************************************************************************/
348 /////////////////////////////////////////////////////////////////////////////////////////////////////
349 /////////////////////////////////////////////////////////////////////////////////////////////////////
350 /////////////////////////////////////////////////////////////////////////////////////////////////////
351 /////////////////////////////////////////////////////////////////////////////////////////////////////
352 /////////////////////////////////////////////////////////////////////////////////////////////////////
353 /////////////////////////////////////////////////////////////////////////////////////////////////////
354 /////////////////////////////////////////////////////////////////////////////////////////////////////
355 /////////////////////////////////////////////////////////////////////////////////////////////////////
356 /////////////////////////////////////////////////////////////////////////////////////////////////////
357 /////////////////////////////////////////////////////////////////////////////////////////////////////
358 /////////////////////////////////////////////////////////////////////////////////////////////////////
359 /////////////////////////////////////////////////////////////////////////////////////////////////////
360 /////////////////////////////////////////////////////////////////////////////////////////////////////
361 /////////////////////////////////////////////////////////////////////////////////////////////////////
362 /////////////////////////////////////////////////////////////////////////////////////////////////////
363 /////////////////////////////////////////////////////////////////////////////////////////////////////
364
365
366 /*************************************************************************/
367 /* effective declaration of classes                                      */
368 /*************************************************************************/
369
370 /* apis */
371 inline api::api() : api_{nullptr} { }
372 inline api::api(afb_api_t a) : api_{a} { }
373 inline api::api(api &&other) : api_{other.api_} { other.api_ = nullptr; }
374 inline api::~api() { api_ = nullptr; }
375 inline api &api::operator=(api &&other) { api_ = other.api_; other.api_ = nullptr; return *this;}
376 inline api::operator afb_api_t() const { return api_; }
377 inline afb_api_t api::operator->() const { return api_; }
378 inline const char *api::name() const { return afb_api_name(api_); }
379 inline void *api::get_userdata() const { return afb_api_get_userdata(api_); }
380 inline void api::set_userdata(void *value) const { afb_api_set_userdata(api_, value); }
381 inline int api::require_api(const char *name, int initialized) const { return afb_api_require_api(api_, name, initialized); }
382 inline int api::require_api(const std::string& name, int initialized) const { return afb_api_require_api(api_, name.c_str(), initialized); }
383 inline int api::wants_log_level(int level) const { return afb_api_wants_log_level(api_, level); }
384 inline void api::vverbose(int level, const char *file, int line, const char *func, const char *fmt, va_list args) const { afb_api_vverbose(api_, level, file, line, func, fmt, args); }
385 inline void api::verbose(int level, const char *file, int line, const char *func, const char *fmt, ...) const
386 {
387         va_list args;
388         va_start(args, fmt);
389         vverbose(level, file, line, func, fmt, args);
390         va_end(args);
391 }
392 inline int api::rootdir_get_fd() const { return afb_api_rootdir_get_fd(api_); }
393 inline int api::rootdir_open_locale(const char *filename, int flags, const char *locale) const { return afb_api_rootdir_open_locale(api_, filename, flags, locale); }
394 inline int api::rootdir_open_locale(const std::string &filename, int flags, const std::string &locale) const { return afb_api_rootdir_open_locale(api_, filename.c_str(), flags, locale.c_str()); }
395 inline struct json_object *api::settings() const { return afb_api_settings(api_); }
396 inline void api::call(const char *apiname, const char *verb, struct json_object *args, call_cb callback, void *closure) const { afb_api_call(api_, apiname, verb, args, callback, closure); }
397 inline void api::call(const std::string &apiname, const std::string &verb, struct json_object *args, call_cb callback, void *closure) const { afb_api_call(api_, apiname.c_str(), verb.c_str(), args, callback, closure); }
398 inline int api::call_sync(const char *apiname, const char *verb, struct json_object *args, struct json_object **object, char **error, char **info) const { return afb_api_call_sync(api_, apiname, verb, args, object, error, info); }
399 inline int api::call_sync(const std::string &apiname, const std::string &verb, struct json_object *args, struct json_object **object, std::string &error, std::string& info) const
400 {
401         char *err, *inf;
402         int ret = afb_api_call_sync(api_, apiname.c_str(), verb.c_str(), args, object, &err, &inf);
403         error = err;
404         info = inf;
405         return ret;
406 }
407 inline int api::queue_job(queue_cb callback, void *argument, void *group, int timeout) const { return afb_api_queue_job(api_, callback, argument, group, timeout); }
408 inline int api::broadcast_event(const char *name, struct json_object *object) const { return afb_api_broadcast_event(api_, name, object); }
409 inline int api::broadcast_event(const std::string &name, struct json_object *object) const { return afb_api_broadcast_event(api_, name.c_str(), object); }
410 inline event api::make_event(const char *name) const { return event(afb_api_make_event(api_, name)); }
411 inline event api::make_event(const std::string &name) const { return event(afb_api_make_event(api_, name.c_str())); }
412 inline int api::event_handler_add(const char *pattern, event_cb callback, void *closure) const { return afb_api_event_handler_add(api_, pattern, callback, closure); }
413 inline int api::event_handler_add(const std::string &pattern, event_cb callback, void *closure) const { return afb_api_event_handler_add(api_, pattern.c_str(), callback, closure); }
414 inline int api::event_handler_del(const char *pattern, void **closure) const { return afb_api_event_handler_del(api_, pattern, closure); }
415 inline int api::event_handler_del(const std::string &pattern, void **closure) const { return afb_api_event_handler_del(api_, pattern.c_str(), closure); }
416 inline struct sd_event *api::get_event_loop() const { return afb_api_get_event_loop(api_); }
417 inline struct sd_bus *api::get_user_bus() const { return afb_api_get_user_bus(api_); }
418 inline struct sd_bus *api::get_system_bus() const { return afb_api_get_system_bus(api_); }
419 inline api api::new_api(const char *apiname, const char *info, int noconcurrency, preinit_cb preinit, void *closure) const { return api(afb_api_new_api(api_, apiname, info, noconcurrency, preinit, closure)); }
420 inline api api::new_api(const std::string &apiname, const std::string &info, int noconcurrency, preinit_cb preinit, void *closure) const { return api(afb_api_new_api(api_, apiname.c_str(), info.c_str(), noconcurrency, preinit, closure)); }
421 inline int api::set_verbs(const struct afb_verb_v2 *verbs) const { return afb_api_set_verbs_v2(api_, verbs); }
422 inline int api::set_verbs(const struct afb_verb_v3 *verbs) const { return afb_api_set_verbs_v3(api_, verbs); }
423 inline int api::add_verb(const char *verb, const char *info, verb_cb callback, void *vcbdata, const struct afb_auth *auth, uint32_t session, int glob) const { return afb_api_add_verb(api_, verb, info, callback, vcbdata, auth, session, glob); }
424 inline int api::add_verb(const std::string &verb, const std::string &info, verb_cb callback, void *vcbdata, const struct afb_auth *auth, uint32_t session, int glob) const { return afb_api_add_verb(api_, verb.c_str(), info.c_str(), callback, vcbdata, auth, session, glob); }
425 inline int api::del_verb(const char *verb, void **vcbdata) const { return afb_api_del_verb(api_, verb, vcbdata); }
426 inline int api::del_verb(const std::string &verb, void **vcbdata) const { return afb_api_del_verb(api_, verb.c_str(), vcbdata); }
427 inline int api::on_event(onevent_cb onevent) const { return afb_api_on_event(api_, onevent); }
428 inline int api::on_init(oninit_cb oninit) const { return afb_api_on_init(api_, oninit); }
429 inline int api::provide_class(const char *name) const { return afb_api_provide_class(api_, name); }
430 inline int api::provide_class(const std::string &name) const { return afb_api_provide_class(api_, name.c_str()); }
431 inline int api::require_class(const char *name) const { return afb_api_require_class(api_, name); }
432 inline int api::require_class(const std::string &name) const { return afb_api_require_class(api_, name.c_str()); }
433 inline void api::seal() const { afb_api_seal(api_); }
434 inline int api::delete_api() const { return afb_api_delete_api(api_); }
435 inline int api::add_alias(const char *name, const char *as_name) const { return afb_api_add_alias(api_, name, as_name); }
436 inline int api::add_alias(const std::string &name, const std::string &as_name) const { return afb_api_add_alias(api_, name.c_str(), as_name.c_str()); }
437
438 /* events */
439 inline event::event() : event_{nullptr} { }
440 inline event::event(afb_event_t e) : event_{e} { }
441 inline event::event(event &&other) : event_{other.event_} { other.event_ = nullptr; }
442 inline event::event(const event &other) : event_{other.event_} { addref(); }
443 inline event::~event() { unref(); }
444 inline event &event::operator=(const event &other) { event_ = other.event_; return *this; }
445 inline event &event::operator=(event &&other) { event_ = other.event_; other.event_ = nullptr; return *this;}
446
447 inline event::operator afb_event_t() const { return event_; }
448 inline afb_event_t event::operator->() const { return event_; }
449
450 inline event::operator bool() const { return is_valid(); }
451 inline bool event::is_valid() const { return afb_event_is_valid(event_); }
452
453 inline int event::broadcast(json_object *object) const { return afb_event_broadcast(event_, object); }
454 inline int event::push(json_object *object) const { return afb_event_push(event_, object); }
455
456 inline void event::unref() { if (event_) { afb_event_unref(event_); } event_ = nullptr; }
457 inline void event::addref() { afb_event_addref(event_); }
458 inline const char *event::name() const { return afb_event_name(event_); }
459
460 /* args */
461 inline arg::arg(const struct afb_arg &a) : arg_(a) {}
462 inline arg::arg(const arg &other) : arg_(other.arg_) {}
463 inline arg &arg::operator=(const arg &other) { arg_ = other.arg_; return *this; }
464
465 inline arg::operator const struct afb_arg&() const { return arg_; }
466
467 inline bool arg::has_name() const { return !!arg_.name; }
468 inline bool arg::has_value() const { return !!arg_.value; }
469 inline bool arg::has_path() const { return !!arg_.path; }
470
471 inline const char *arg::name() const { return arg_.name; }
472 inline const char *arg::value() const { return arg_.value; }
473 inline const char *arg::path() const { return arg_.path; }
474
475 /* req(uests)s */
476
477 inline req::req(afb_req_t r) : req_(r) {}
478 inline req::req(const req &other) : req_(other.req_) {}
479 inline req &req::operator=(const req &other) { req_ = other.req_; return *this; }
480
481 inline req::operator afb_req_t() const { return req_; }
482 inline afb_req_t req::operator->() const { return req_; }
483
484 inline req::operator bool() const { return is_valid(); }
485 inline bool req::is_valid() const { return afb_req_is_valid(req_); }
486
487 inline arg req::get(const char *name) const { return arg(afb_req_get(req_, name)); }
488
489 inline const char *req::value(const char *name) const { return afb_req_value(req_, name); }
490
491 inline const char *req::path(const char *name) const { return afb_req_path(req_, name); }
492
493 inline json_object *req::json() const { return afb_req_json(req_); }
494
495 inline void req::reply(json_object *obj, const char *error, const char *info) const { afb_req_reply(req_, obj, error, info); }
496 inline void req::replyv(json_object *obj, const char *error, const char *info, va_list args) const { afb_req_reply_v(req_, obj, error, info, args); }
497 inline void req::replyf(json_object *obj, const char *error, const char *info, ...) const
498 {
499         va_list args;
500         va_start(args, info);
501         replyv(obj, error, info, args);
502         va_end(args);
503 }
504
505 inline void req::success(json_object *obj, const char *info) const { reply(obj, nullptr, info); }
506 inline void req::successv(json_object *obj, const char *info, va_list args) const { replyv(obj, nullptr, info, args); }
507 inline void req::successf(json_object *obj, const char *info, ...) const
508 {
509         va_list args;
510         va_start(args, info);
511         successv(obj, info, args);
512         va_end(args);
513 }
514
515 inline void req::fail(const char *error, const char *info) const { reply(nullptr, error, info); }
516 inline void req::failv(const char *error, const char *info, va_list args) const { replyv(nullptr, error, info, args); }
517 inline void req::failf(const char *error, const char *info, ...) const
518 {
519         va_list args;
520         va_start(args, info);
521         failv(error, info, args);
522         va_end(args);
523 }
524
525 inline void req::addref() const { afb_req_addref(req_); }
526
527 inline void req::unref() const { afb_req_unref(req_); }
528
529 inline void req::session_close() const { afb_req_session_close(req_); }
530
531 inline bool req::session_set_LOA(unsigned level) const { return !afb_req_session_set_LOA(req_, level); }
532
533 inline bool req::subscribe(const event &event) const { return !afb_req_subscribe(req_, event); }
534
535 inline bool req::unsubscribe(const event &event) const { return !afb_req_unsubscribe(req_, event); }
536
537 inline void req::subcall(const char *api, const char *verb, json_object *args, int flags, void (*callback)(void *closure, json_object *result, const char *error, const char *info, afb_req_t req), void *closure) const
538 {
539         afb_req_subcall(req_, api, verb, args, flags, callback, closure);
540 }
541
542 template <class T>
543 inline void req::subcall(const char *api, const char *verb, json_object *args, int flags, void (*callback)(T *closure, json_object *result, const char *error, const char *info, afb_req_t req), T *closure) const
544 {
545         subcall(api, verb, args, flags, reinterpret_cast<void(*)(void*,json_object*,const char*,const char*,afb_req_t)>(callback), reinterpret_cast<void*>(closure));
546 }
547
548 inline bool req::subcallsync(const char *api, const char *verb, json_object *args, int flags, struct json_object *&object, char *&error, char *&info) const
549 {
550         return !afb_req_subcall_sync(req_, api, verb, args, flags, &object, &error, &info);
551 }
552
553 inline void req::subcall(const char *api, const char *verb, json_object *args, void (*callback)(void *closure, int iserror, json_object *result, afb_req_t req), void *closure) const
554 {
555         afb_req_subcall_legacy(req_, api, verb, args, callback, closure);
556 }
557
558 template <class T>
559 inline void req::subcall(const char *api, const char *verb, json_object *args, void (*callback)(T *closure, int iserror, json_object *result, afb_req_t req), T *closure) const
560 {
561         subcall(api, verb, args, reinterpret_cast<void(*)(void*,int,json_object*,afb_req_t)>(callback), reinterpret_cast<void*>(closure));
562 }
563
564 inline bool req::subcallsync(const char *api, const char *verb, json_object *args, struct json_object *&result) const
565 {
566         return !afb_req_subcall_sync_legacy(req_, api, verb, args, &result);
567 }
568
569 inline void req::verbose(int level, const char *file, int line, const char * func, const char *fmt, va_list args) const
570 {
571         afb_req_verbose(req_, level, file, line, func, fmt, args);
572 }
573
574 inline void req::verbose(int level, const char *file, int line, const char * func, const char *fmt, ...) const
575 {
576         va_list args;
577         va_start(args, fmt);
578         afb_req_verbose(req_, level, file, line, func, fmt, args);
579         va_end(args);
580 }
581
582 inline bool req::has_permission(const char *permission) const
583 {
584         return bool(afb_req_has_permission(req_, permission));
585 }
586
587 inline char *req::get_application_id() const
588 {
589         return afb_req_get_application_id(req_);
590 }
591
592 inline int req::get_uid() const
593 {
594         return afb_req_get_uid(req_);
595 }
596
597 inline json_object *req::get_client_info() const
598 {
599         return afb_req_get_client_info(req_);
600 }
601
602 /* commons */
603 inline int broadcast_event(const char *name, json_object *object)
604         { return afb_daemon_broadcast_event(name, object); }
605
606 inline event make_event(const char *name)
607         { return afb_daemon_make_event(name); }
608
609 inline void verbose(int level, const char *file, int line, const char * func, const char *fmt, va_list args)
610         { afb_daemon_verbose(level, file, line, func, fmt, args); }
611
612 inline void verbose(int level, const char *file, int line, const char * func, const char *fmt, ...)
613         { va_list args; va_start(args, fmt); verbose(level, file, line, func, fmt, args); va_end(args); }
614
615 inline int rootdir_get_fd()
616         { return afb_daemon_rootdir_get_fd(); }
617
618 inline int rootdir_open_locale_fd(const char *filename, int flags, const char *locale)
619         { return afb_daemon_rootdir_open_locale(filename, flags, locale); }
620
621 inline int queue_job(void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
622         { return afb_daemon_queue_job(callback, argument, group, timeout); }
623
624 inline int require_api(const char *apiname, bool initialized)
625         { return afb_daemon_require_api(apiname, int(initialized)); }
626
627 inline int add_alias(const char *apiname, const char *aliasname)
628         { return afb_daemon_add_alias(apiname, aliasname); }
629
630 inline int logmask()
631         { return afb_get_logmask(); }
632
633 inline bool wants_errors()
634         { return AFB_SYSLOG_MASK_WANT_ERROR(logmask()); }
635
636 inline bool wants_warnings()
637         { return AFB_SYSLOG_MASK_WANT_WARNING(logmask()); }
638
639 inline bool wants_notices()
640         { return AFB_SYSLOG_MASK_WANT_NOTICE(logmask()); }
641
642 inline bool wants_infos()
643         { return AFB_SYSLOG_MASK_WANT_INFO(logmask()); }
644
645 inline bool wants_debugs()
646         { return AFB_SYSLOG_MASK_WANT_DEBUG(logmask()); }
647
648 inline void call(const char *api, const char *verb, struct json_object *args, void (*callback)(void*closure, struct json_object *result, const char *error, const char *info, afb_api_t api), void *closure)
649 {
650         afb_service_call(api, verb, args, callback, closure);
651 }
652
653 template <class T>
654 inline void call(const char *api, const char *verb, struct json_object *args, void (*callback)(T*closure, struct json_object *result, const char *error, const char *info, afb_api_t api), T *closure)
655 {
656         afb_service_call(api, verb, args, reinterpret_cast<void(*)(void*,json_object*,const char*, const char*,afb_api_t)>(callback), reinterpret_cast<void*>(closure));
657 }
658
659 inline bool callsync(const char *api, const char *verb, struct json_object *args, struct json_object *&result, char *&error, char *&info)
660 {
661         return !!afb_service_call_sync(api, verb, args, &result, &error, &info);
662 }
663
664 /*************************************************************************/
665 /* declaration of the binding's authorization s                          */
666 /*************************************************************************/
667
668 constexpr afb_auth auth_no()
669 {
670         afb_auth r = { afb_auth_No, {0}, nullptr};
671         r.type = afb_auth_No;
672         return r;
673 }
674
675 constexpr afb_auth auth_yes()
676 {
677         afb_auth r = { afb_auth_No, {0}, nullptr};
678         r.type = afb_auth_Yes;
679         return r;
680 }
681
682 constexpr afb_auth auth_token()
683 {
684         afb_auth r = { afb_auth_No, {0}, nullptr};
685         r.type = afb_auth_Token;
686         return r;
687 }
688
689 constexpr afb_auth auth_LOA(unsigned loa)
690 {
691         afb_auth r = { afb_auth_No, {0}, nullptr};
692         r.type = afb_auth_LOA;
693         r.loa = loa;
694         return r;
695 }
696
697 constexpr afb_auth auth_permission(const char *permission)
698 {
699         afb_auth r = { afb_auth_No, {0}, nullptr};
700         r.type = afb_auth_Permission;
701         r.text = permission;
702         return r;
703 }
704
705 constexpr afb_auth auth_not(const afb_auth *other)
706 {
707         afb_auth r = { afb_auth_No, {0}, nullptr};
708         r.type = afb_auth_Not;
709         r.first = other;
710         return r;
711 }
712
713 constexpr afb_auth auth_not(const afb_auth &other)
714 {
715         return auth_not(&other);
716 }
717
718 constexpr afb_auth auth_or(const afb_auth *first, const afb_auth *next)
719 {
720         afb_auth r = { afb_auth_No, {0}, nullptr};
721         r.type = afb_auth_Or;
722         r.first = first;
723         r.next = next;
724         return r;
725 }
726
727 constexpr afb_auth auth_or(const afb_auth &first, const afb_auth &next)
728 {
729         return auth_or(&first, &next);
730 }
731
732 constexpr afb_auth auth_and(const afb_auth *first, const afb_auth *next)
733 {
734         afb_auth r = { afb_auth_No, {0}, nullptr};
735         r.type = afb_auth_And;
736         r.first = first;
737         r.next = next;
738         return r;
739 }
740
741 constexpr afb_auth auth_and(const afb_auth &first, const afb_auth &next)
742 {
743         return auth_and(&first, &next);
744 }
745
746 constexpr afb_verb_t verb(
747         const char *name,
748         void (*callback)(afb_req_t),
749         const char *info = nullptr,
750         uint16_t session = 0,
751         const afb_auth *auth = nullptr,
752         bool glob = false,
753         void *vcbdata = nullptr
754 )
755 {
756         return { name, callback, auth, info, vcbdata, session, glob };
757 }
758
759 void __attribute__((weak)) __afb__verb__cb__for__global__(afb_req_t r)
760 {
761         void *vcbdata;
762         void (*callback)(req);
763
764         vcbdata = afb_req_get_vcbdata(r);
765         callback = reinterpret_cast<void(*)(req)>(vcbdata);
766         callback(req(r));
767 }
768
769 constexpr afb_verb_t verb(
770         const char *name,
771         void (*callback)(req),
772         const char *info = nullptr,
773         uint16_t session = 0,
774         const afb_auth *auth = nullptr,
775         bool glob = false
776 )
777 {
778         return verb(
779                 name,
780                 __afb__verb__cb__for__global__,
781                 info,
782                 session,
783                 auth,
784                 glob,
785                 (void*)(&callback)
786         );
787 }
788
789 constexpr afb_verb_t verbend()
790 {
791         return { 0, 0, 0, 0, 0, 0, 0 };
792 }
793
794 constexpr afb_binding_t binding(
795         const char *name,
796         const afb_verb_t *verbs,
797         const char *info = nullptr,
798         int (*init)(afb_api_t) = nullptr,
799         const char *specification = nullptr,
800         void (*onevent)(afb_api_t, const char*, struct json_object*) = nullptr,
801         bool noconcurrency = false,
802         int (*preinit)(afb_api_t) = nullptr,
803         void *userdata = nullptr
804 )
805 {
806         return {
807                 name, specification, info, verbs, preinit, init, onevent, userdata,
808                 nullptr, nullptr, nullptr, static_cast<unsigned int>(noconcurrency) };
809 };
810
811 /*************************************************************************/
812 /***                         E N D                                     ***/
813 /*************************************************************************/
814 }