2 * Copyright (C) 2016, 2017, 2018 "IoT.bzh"
3 * Author: José Bollo <jose.bollo@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 #ifndef AFB_BINDING_VERSION
27 # define AFB_BINDING_VERSION 3
30 /* check the version */
31 #if AFB_BINDING_VERSION < 2
32 # error "AFB_BINDING_VERSION must be at least 2 but 3 is prefered"
35 /* get C definitions of bindings */
37 #include "afb-binding.h"
41 /*************************************************************************/
42 /* pre-declaration of classes */
43 /*************************************************************************/
49 /*************************************************************************/
50 /* declaration of functions */
51 /*************************************************************************/
53 int broadcast_event(const char *name, json_object *object = nullptr);
55 event make_event(const char *name);
57 void verbose(int level, const char *file, int line, const char * func, const char *fmt, va_list args);
59 void verbose(int level, const char *file, int line, const char * func, const char *fmt, ...);
63 int rootdir_open_locale_fd(const char *filename, int flags, const char *locale = nullptr);
65 int queue_job(void (*callback)(int signum, void *arg), void *argument, void *group, int timeout);
67 int require_api(const char *apiname, bool initialized = true);
69 int add_alias(const char *apiname, const char *aliasname);
74 bool wants_warnings();
79 #if AFB_BINDING_VERSION >= 3
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);
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);
84 bool callsync(const char *api, const char *verb, struct json_object *args, struct json_object *&result);
86 void call(const char *api, const char *verb, struct json_object *args, void (*callback)(void*closure, int iserror, struct json_object *result), void *closure);
88 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), T *closure);
91 bool callsync(const char *api, const char *verb, struct json_object *args, struct json_object *&result);
93 /*************************************************************************/
94 /* effective declaration of classes */
95 /*************************************************************************/
102 event() { invalidate(); }
103 event(afb_event_t e);
104 event(const event &other);
105 event &operator=(const event &other);
107 operator afb_event_t() const;
108 afb_event_t operator->() const;
110 operator bool() const;
111 bool is_valid() const;
115 int broadcast(json_object *object) const;
116 int push(json_object *object) const;
120 const char *name() const;
129 arg(const struct afb_arg &a);
130 arg(const arg &other);
131 arg &operator=(const arg &other);
133 operator const struct afb_arg&() const;
135 bool has_name() const;
136 bool has_value() const;
137 bool has_path() const;
139 const char *name() const;
140 const char *value() const;
141 const char *path() const;
152 req(const req &other);
153 req &operator=(const req &other);
155 operator afb_req_t() const;
156 afb_req_t operator->() const;
158 operator bool() const;
159 bool is_valid() const;
161 arg get(const char *name) const;
163 const char *value(const char *name) const;
165 const char *path(const char *name) const;
167 json_object *json() const;
169 void reply(json_object *obj = nullptr, const char *error = nullptr, const char *info = nullptr) const;
170 void replyf(json_object *obj, const char *error, const char *info, ...) const;
171 void replyv(json_object *obj, const char *error, const char *info, va_list args) const;
173 void success(json_object *obj = nullptr, const char *info = nullptr) const;
174 void successf(json_object *obj, const char *info, ...) const;
175 void successv(json_object *obj, const char *info, va_list args) const;
177 void fail(const char *error = "failed", const char *info = nullptr) const;
178 void failf(const char *error, const char *info, ...) const;
179 void failv(const char *error, const char *info, va_list args) const;
181 template < class T > T *context() const;
187 void session_close() const;
189 bool session_set_LOA(unsigned level) const;
191 bool subscribe(const event &event) const;
193 bool unsubscribe(const event &event) const;
195 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;
196 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;
198 bool subcallsync(const char *api, const char *verb, json_object *args, struct json_object *&result) const;
200 #if AFB_BINDING_VERSION >= 3
201 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;
203 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;
205 bool subcallsync(const char *api, const char *verb, json_object *args, int flags, struct json_object *&object, char *&error, char *&info) const;
208 void verbose(int level, const char *file, int line, const char * func, const char *fmt, va_list args) const;
210 void verbose(int level, const char *file, int line, const char * func, const char *fmt, ...) const;
212 bool has_permission(const char *permission) const;
214 char *get_application_id() const;
218 json_object *get_client_info() const;
221 /*************************************************************************/
222 /* effective declaration of classes */
223 /*************************************************************************/
224 /////////////////////////////////////////////////////////////////////////////////////////////////////
225 /////////////////////////////////////////////////////////////////////////////////////////////////////
226 /////////////////////////////////////////////////////////////////////////////////////////////////////
227 /////////////////////////////////////////////////////////////////////////////////////////////////////
228 /////////////////////////////////////////////////////////////////////////////////////////////////////
229 /////////////////////////////////////////////////////////////////////////////////////////////////////
230 /////////////////////////////////////////////////////////////////////////////////////////////////////
231 /////////////////////////////////////////////////////////////////////////////////////////////////////
232 /////////////////////////////////////////////////////////////////////////////////////////////////////
233 /////////////////////////////////////////////////////////////////////////////////////////////////////
234 /////////////////////////////////////////////////////////////////////////////////////////////////////
235 /////////////////////////////////////////////////////////////////////////////////////////////////////
236 /////////////////////////////////////////////////////////////////////////////////////////////////////
237 /////////////////////////////////////////////////////////////////////////////////////////////////////
238 /////////////////////////////////////////////////////////////////////////////////////////////////////
239 /////////////////////////////////////////////////////////////////////////////////////////////////////
242 /*************************************************************************/
243 /* effective declaration of classes */
244 /*************************************************************************/
247 inline event::event(afb_event_t e) : event_(e) { }
248 inline event::event(const event &other) : event_(other.event_) { }
249 inline event &event::operator=(const event &other) { event_ = other.event_; return *this; }
251 inline event::operator afb_event_t() const { return event_; }
252 inline afb_event_t event::operator->() const { return event_; }
254 inline event::operator bool() const { return is_valid(); }
255 inline bool event::is_valid() const { return afb_event_is_valid(event_); }
257 #if AFB_BINDING_VERSION >= 3
258 inline void event::invalidate() { event_ = nullptr; }
260 inline void event::invalidate() { event_ = { nullptr, nullptr }; }
263 inline int event::broadcast(json_object *object) const { return afb_event_broadcast(event_, object); }
264 inline int event::push(json_object *object) const { return afb_event_push(event_, object); }
266 inline void event::unref() { afb_event_unref(event_); invalidate(); }
267 inline void event::addref() { afb_event_addref(event_); }
268 inline const char *event::name() const { return afb_event_name(event_); }
271 inline arg::arg(const struct afb_arg &a) : arg_(a) {}
272 inline arg::arg(const arg &other) : arg_(other.arg_) {}
273 inline arg &arg::operator=(const arg &other) { arg_ = other.arg_; return *this; }
275 inline arg::operator const struct afb_arg&() const { return arg_; }
277 inline bool arg::has_name() const { return !!arg_.name; }
278 inline bool arg::has_value() const { return !!arg_.value; }
279 inline bool arg::has_path() const { return !!arg_.path; }
281 inline const char *arg::name() const { return arg_.name; }
282 inline const char *arg::value() const { return arg_.value; }
283 inline const char *arg::path() const { return arg_.path; }
288 inline req::req(afb_req_t r) : req_(r) {}
289 inline req::req(const req &other) : req_(other.req_) {}
290 inline req &req::operator=(const req &other) { req_ = other.req_; return *this; }
292 inline req::operator afb_req_t() const { return req_; }
293 inline afb_req_t req::operator->() const { return req_; }
295 inline req::operator bool() const { return is_valid(); }
296 inline bool req::is_valid() const { return afb_req_is_valid(req_); }
298 inline arg req::get(const char *name) const { return arg(afb_req_get(req_, name)); }
300 inline const char *req::value(const char *name) const { return afb_req_value(req_, name); }
302 inline const char *req::path(const char *name) const { return afb_req_path(req_, name); }
304 inline json_object *req::json() const { return afb_req_json(req_); }
306 inline void req::reply(json_object *obj, const char *error, const char *info) const { afb_req_reply(req_, obj, error, info); }
307 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); }
308 inline void req::replyf(json_object *obj, const char *error, const char *info, ...) const
311 va_start(args, info);
312 replyv(obj, error, info, args);
316 inline void req::success(json_object *obj, const char *info) const { reply(obj, nullptr, info); }
317 inline void req::successv(json_object *obj, const char *info, va_list args) const { replyv(obj, nullptr, info, args); }
318 inline void req::successf(json_object *obj, const char *info, ...) const
321 va_start(args, info);
322 successv(obj, info, args);
326 inline void req::fail(const char *error, const char *info) const { reply(nullptr, error, info); }
327 inline void req::failv(const char *error, const char *info, va_list args) const { replyv(nullptr, error, info, args); }
328 inline void req::failf(const char *error, const char *info, ...) const
331 va_start(args, info);
332 failv(error, info, args);
337 inline T *req::context() const
339 #if AFB_BINDING_VERSION >= 3
340 T* (*creater)(void*) = [](){return new T();};
341 void (*freer)(T*) = [](T*t){delete t;};
342 return reinterpret_cast<T*>(afb_req_context(req_, 0,
343 reinterpret_cast<void *(*)(void*)>(creater),
344 reinterpret_cast<void (*)(void*)>(freer), nullptr));
346 T* (*creater)() = [](){return new T();};
347 void (*freer)(T*) = [](T*t){delete t;};
348 return reinterpret_cast<T*>(afb_req_context(req_,
349 reinterpret_cast<void *(*)()>(creater),
350 reinterpret_cast<void (*)(void*)>(freer)));
354 inline void req::addref() const { afb_req_addref(req_); }
356 inline void req::unref() const { afb_req_unref(req_); }
358 inline void req::session_close() const { afb_req_session_close(req_); }
360 inline bool req::session_set_LOA(unsigned level) const { return !afb_req_session_set_LOA(req_, level); }
362 inline bool req::subscribe(const event &event) const { return !afb_req_subscribe(req_, event); }
364 inline bool req::unsubscribe(const event &event) const { return !afb_req_unsubscribe(req_, event); }
370 #if AFB_BINDING_VERSION >= 3
372 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
374 afb_req_subcall(req_, api, verb, args, flags, callback, closure);
378 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
380 subcall(api, verb, args, flags, reinterpret_cast<void(*)(void*,json_object*,const char*,const char*,afb_req_t)>(callback), reinterpret_cast<void*>(closure));
383 inline bool req::subcallsync(const char *api, const char *verb, json_object *args, int flags, struct json_object *&object, char *&error, char *&info) const
385 return !afb_req_subcall_sync(req_, api, verb, args, flags, &object, &error, &info);
390 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
392 #if AFB_BINDING_VERSION >= 3
393 afb_req_subcall_legacy(req_, api, verb, args, callback, closure);
395 afb_req_subcall_req(req_, api, verb, args, callback, closure);
400 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
402 subcall(api, verb, args, reinterpret_cast<void(*)(void*,int,json_object*,afb_req_t)>(callback), reinterpret_cast<void*>(closure));
405 inline bool req::subcallsync(const char *api, const char *verb, json_object *args, struct json_object *&result) const
407 #if AFB_BINDING_VERSION >= 3
408 return !afb_req_subcall_sync_legacy(req_, api, verb, args, &result);
410 return !afb_req_subcall_sync(req_, api, verb, args, &result);
414 inline void req::verbose(int level, const char *file, int line, const char * func, const char *fmt, va_list args) const
416 afb_req_verbose(req_, level, file, line, func, fmt, args);
419 inline void req::verbose(int level, const char *file, int line, const char * func, const char *fmt, ...) const
423 afb_req_verbose(req_, level, file, line, func, fmt, args);
427 inline bool req::has_permission(const char *permission) const
429 return bool(afb_req_has_permission(req_, permission));
432 inline char *req::get_application_id() const
434 return afb_req_get_application_id(req_);
437 inline int req::get_uid() const
439 return afb_req_get_uid(req_);
442 inline json_object *req::get_client_info() const
444 return afb_req_get_client_info(req_);
448 inline int broadcast_event(const char *name, json_object *object)
449 { return afb_daemon_broadcast_event(name, object); }
451 inline event make_event(const char *name)
452 { return afb_daemon_make_event(name); }
454 inline void verbose(int level, const char *file, int line, const char * func, const char *fmt, va_list args)
455 { afb_daemon_verbose(level, file, line, func, fmt, args); }
457 inline void verbose(int level, const char *file, int line, const char * func, const char *fmt, ...)
458 { va_list args; va_start(args, fmt); verbose(level, file, line, func, fmt, args); va_end(args); }
460 inline int rootdir_get_fd()
461 { return afb_daemon_rootdir_get_fd(); }
463 inline int rootdir_open_locale_fd(const char *filename, int flags, const char *locale)
464 { return afb_daemon_rootdir_open_locale(filename, flags, locale); }
466 inline int queue_job(void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
467 { return afb_daemon_queue_job(callback, argument, group, timeout); }
469 inline int require_api(const char *apiname, bool initialized)
470 { return afb_daemon_require_api(apiname, int(initialized)); }
472 inline int add_alias(const char *apiname, const char *aliasname)
473 { return afb_daemon_add_alias(apiname, aliasname); }
475 #if AFB_BINDING_VERSION >= 3
477 { return afb_get_logmask(); }
480 { return (1 << (1 + afb_get_verbosity() + AFB_SYSLOG_LEVEL_ERROR)) - 1; }
483 inline bool wants_errors()
484 { return AFB_SYSLOG_MASK_WANT_ERROR(logmask()); }
486 inline bool wants_warnings()
487 { return AFB_SYSLOG_MASK_WANT_WARNING(logmask()); }
489 inline bool wants_notices()
490 { return AFB_SYSLOG_MASK_WANT_NOTICE(logmask()); }
492 inline bool wants_infos()
493 { return AFB_SYSLOG_MASK_WANT_INFO(logmask()); }
495 inline bool wants_debugs()
496 { return AFB_SYSLOG_MASK_WANT_DEBUG(logmask()); }
498 #if AFB_BINDING_VERSION >= 3
499 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)
501 afb_service_call(api, verb, args, callback, closure);
505 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)
507 afb_service_call(api, verb, args, reinterpret_cast<void(*)(void*,int,json_object*,afb_api_t)>(callback), reinterpret_cast<void*>(closure));
510 inline bool callsync(const char *api, const char *verb, struct json_object *args, struct json_object *&result, char *&error, char *&info)
512 return !!afb_service_call_sync(api, verb, args, &result, &error, &info);
515 inline void call(const char *api, const char *verb, struct json_object *args, void (*callback)(void*closure, int iserror, struct json_object *result), void *closure)
517 afb_service_call(api, verb, args, callback, closure);
521 inline void call(const char *api, const char *verb, struct json_object *args, void (*callback)(T*closure, int iserror, struct json_object *result), T *closure)
523 afb_service_call(api, verb, args, reinterpret_cast<void(*)(void*,int,json_object*)>(callback), reinterpret_cast<void*>(closure));
526 inline bool callsync(const char *api, const char *verb, struct json_object *args, struct json_object *&result)
528 return !!afb_service_call_sync(api, verb, args, &result);
532 /*************************************************************************/
533 /* declaration of the binding's authorization s */
534 /*************************************************************************/
536 constexpr afb_auth auth_no()
538 afb_auth r = { afb_auth_No, 0, 0};
539 r.type = afb_auth_No;
543 constexpr afb_auth auth_yes()
545 afb_auth r = { afb_auth_No, 0, 0};
546 r.type = afb_auth_Yes;
550 constexpr afb_auth auth_token()
552 afb_auth r = { afb_auth_No, 0, 0};
553 r.type = afb_auth_Token;
557 constexpr afb_auth auth_LOA(unsigned loa)
559 afb_auth r = { afb_auth_No, 0, 0};
560 r.type = afb_auth_LOA;
565 constexpr afb_auth auth_permission(const char *permission)
567 afb_auth r = { afb_auth_No, 0, 0};
568 r.type = afb_auth_Permission;
573 constexpr afb_auth auth_not(const afb_auth *other)
575 afb_auth r = { afb_auth_No, 0, 0};
576 r.type = afb_auth_Not;
581 constexpr afb_auth auth_not(const afb_auth &other)
583 return auth_not(&other);
586 constexpr afb_auth auth_or(const afb_auth *first, const afb_auth *next)
588 afb_auth r = { afb_auth_No, 0, 0};
589 r.type = afb_auth_Or;
595 constexpr afb_auth auth_or(const afb_auth &first, const afb_auth &next)
597 return auth_or(&first, &next);
600 constexpr afb_auth auth_and(const afb_auth *first, const afb_auth *next)
602 afb_auth r = { afb_auth_No, 0, 0};
603 r.type = afb_auth_And;
609 constexpr afb_auth auth_and(const afb_auth &first, const afb_auth &next)
611 return auth_and(&first, &next);
614 constexpr afb_verb_t verb(
616 void (*callback)(afb_req_t),
617 const char *info = nullptr,
618 uint16_t session = 0,
619 const afb_auth *auth = nullptr
620 #if AFB_BINDING_VERSION >= 3
623 void *vcbdata = nullptr
627 #if AFB_BINDING_VERSION >= 3
628 afb_verb_t r = { 0, 0, 0, 0, 0, 0, 0 };
630 afb_verb_t r = { 0, 0, 0, 0, 0 };
633 r.callback = callback;
637 #if AFB_BINDING_VERSION >= 3
638 r.glob = (unsigned)glob;
644 constexpr afb_verb_t verbend()
646 afb_verb_t r = verb(nullptr, nullptr);
650 constexpr afb_binding_t binding(
652 const afb_verb_t *verbs,
653 const char *info = nullptr,
654 #if AFB_BINDING_VERSION >= 3
655 int (*init)(afb_api_t) = nullptr,
656 const char *specification = nullptr,
657 void (*onevent)(afb_api_t, const char*, struct json_object*) = nullptr,
658 bool noconcurrency = false,
659 int (*preinit)(afb_api_t) = nullptr,
660 void *userdata = nullptr
662 int (*init)() = nullptr,
663 const char *specification = nullptr,
664 void (*onevent)(const char*, struct json_object*) = nullptr,
665 bool noconcurrency = false,
666 int (*preinit)() = nullptr
670 #if AFB_BINDING_VERSION >= 3
671 afb_binding_t r = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
673 afb_binding_t r = { 0, 0, 0, 0, 0, 0, 0, 0 };
676 r.specification = specification;
682 r.noconcurrency = noconcurrency ? 1 : 0;
683 #if AFB_BINDING_VERSION >= 3
684 r.userdata = userdata;
689 /*************************************************************************/
691 /*************************************************************************/