2 * Copyright (C) 2016-2019 "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 < 3
32 # error "AFB_BINDING_VERSION must be at least 3"
35 /* get C definitions of bindings */
37 #include <afb/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 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 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 bool callsync(const char *api, const char *verb, struct json_object *args, struct json_object *&result);
85 /*************************************************************************/
86 /* effective declaration of classes */
87 /*************************************************************************/
94 event() { invalidate(); }
96 event(const event &other);
97 event &operator=(const event &other);
99 operator afb_event_t() const;
100 afb_event_t operator->() const;
102 operator bool() const;
103 bool is_valid() const;
107 int broadcast(json_object *object) const;
108 int push(json_object *object) const;
112 const char *name() const;
121 arg(const struct afb_arg &a);
122 arg(const arg &other);
123 arg &operator=(const arg &other);
125 operator const struct afb_arg&() const;
127 bool has_name() const;
128 bool has_value() const;
129 bool has_path() const;
131 const char *name() const;
132 const char *value() const;
133 const char *path() const;
144 req(const req &other);
145 req &operator=(const req &other);
147 operator afb_req_t() const;
148 afb_req_t operator->() const;
150 operator bool() const;
151 bool is_valid() const;
153 arg get(const char *name) const;
155 const char *value(const char *name) const;
157 const char *path(const char *name) const;
159 json_object *json() const;
161 void reply(json_object *obj = nullptr, const char *error = nullptr, const char *info = nullptr) const;
162 void replyf(json_object *obj, const char *error, const char *info, ...) const;
163 void replyv(json_object *obj, const char *error, const char *info, va_list args) const;
165 void success(json_object *obj = nullptr, const char *info = nullptr) const;
166 void successf(json_object *obj, const char *info, ...) const;
167 void successv(json_object *obj, const char *info, va_list args) const;
169 void fail(const char *error = "failed", const char *info = nullptr) const;
170 void failf(const char *error, const char *info, ...) const;
171 void failv(const char *error, const char *info, va_list args) const;
173 template < class T > T *context() const;
179 void session_close() const;
181 bool session_set_LOA(unsigned level) const;
183 bool subscribe(const event &event) const;
185 bool unsubscribe(const event &event) const;
187 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;
188 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;
190 bool subcallsync(const char *api, const char *verb, json_object *args, struct json_object *&result) const;
192 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;
194 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;
196 bool subcallsync(const char *api, const char *verb, json_object *args, int flags, struct json_object *&object, char *&error, char *&info) const;
198 void verbose(int level, const char *file, int line, const char * func, const char *fmt, va_list args) const;
200 void verbose(int level, const char *file, int line, const char * func, const char *fmt, ...) const;
202 bool has_permission(const char *permission) const;
204 char *get_application_id() const;
208 json_object *get_client_info() const;
211 /*************************************************************************/
212 /* effective declaration of classes */
213 /*************************************************************************/
214 /////////////////////////////////////////////////////////////////////////////////////////////////////
215 /////////////////////////////////////////////////////////////////////////////////////////////////////
216 /////////////////////////////////////////////////////////////////////////////////////////////////////
217 /////////////////////////////////////////////////////////////////////////////////////////////////////
218 /////////////////////////////////////////////////////////////////////////////////////////////////////
219 /////////////////////////////////////////////////////////////////////////////////////////////////////
220 /////////////////////////////////////////////////////////////////////////////////////////////////////
221 /////////////////////////////////////////////////////////////////////////////////////////////////////
222 /////////////////////////////////////////////////////////////////////////////////////////////////////
223 /////////////////////////////////////////////////////////////////////////////////////////////////////
224 /////////////////////////////////////////////////////////////////////////////////////////////////////
225 /////////////////////////////////////////////////////////////////////////////////////////////////////
226 /////////////////////////////////////////////////////////////////////////////////////////////////////
227 /////////////////////////////////////////////////////////////////////////////////////////////////////
228 /////////////////////////////////////////////////////////////////////////////////////////////////////
229 /////////////////////////////////////////////////////////////////////////////////////////////////////
232 /*************************************************************************/
233 /* effective declaration of classes */
234 /*************************************************************************/
237 inline event::event(afb_event_t e) : event_(e) { }
238 inline event::event(const event &other) : event_(other.event_) { }
239 inline event &event::operator=(const event &other) { event_ = other.event_; return *this; }
241 inline event::operator afb_event_t() const { return event_; }
242 inline afb_event_t event::operator->() const { return event_; }
244 inline event::operator bool() const { return is_valid(); }
245 inline bool event::is_valid() const { return afb_event_is_valid(event_); }
247 inline void event::invalidate() { event_ = nullptr; }
249 inline int event::broadcast(json_object *object) const { return afb_event_broadcast(event_, object); }
250 inline int event::push(json_object *object) const { return afb_event_push(event_, object); }
252 inline void event::unref() { afb_event_unref(event_); invalidate(); }
253 inline void event::addref() { afb_event_addref(event_); }
254 inline const char *event::name() const { return afb_event_name(event_); }
257 inline arg::arg(const struct afb_arg &a) : arg_(a) {}
258 inline arg::arg(const arg &other) : arg_(other.arg_) {}
259 inline arg &arg::operator=(const arg &other) { arg_ = other.arg_; return *this; }
261 inline arg::operator const struct afb_arg&() const { return arg_; }
263 inline bool arg::has_name() const { return !!arg_.name; }
264 inline bool arg::has_value() const { return !!arg_.value; }
265 inline bool arg::has_path() const { return !!arg_.path; }
267 inline const char *arg::name() const { return arg_.name; }
268 inline const char *arg::value() const { return arg_.value; }
269 inline const char *arg::path() const { return arg_.path; }
274 inline req::req(afb_req_t r) : req_(r) {}
275 inline req::req(const req &other) : req_(other.req_) {}
276 inline req &req::operator=(const req &other) { req_ = other.req_; return *this; }
278 inline req::operator afb_req_t() const { return req_; }
279 inline afb_req_t req::operator->() const { return req_; }
281 inline req::operator bool() const { return is_valid(); }
282 inline bool req::is_valid() const { return afb_req_is_valid(req_); }
284 inline arg req::get(const char *name) const { return arg(afb_req_get(req_, name)); }
286 inline const char *req::value(const char *name) const { return afb_req_value(req_, name); }
288 inline const char *req::path(const char *name) const { return afb_req_path(req_, name); }
290 inline json_object *req::json() const { return afb_req_json(req_); }
292 inline void req::reply(json_object *obj, const char *error, const char *info) const { afb_req_reply(req_, obj, error, info); }
293 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); }
294 inline void req::replyf(json_object *obj, const char *error, const char *info, ...) const
297 va_start(args, info);
298 replyv(obj, error, info, args);
302 inline void req::success(json_object *obj, const char *info) const { reply(obj, nullptr, info); }
303 inline void req::successv(json_object *obj, const char *info, va_list args) const { replyv(obj, nullptr, info, args); }
304 inline void req::successf(json_object *obj, const char *info, ...) const
307 va_start(args, info);
308 successv(obj, info, args);
312 inline void req::fail(const char *error, const char *info) const { reply(nullptr, error, info); }
313 inline void req::failv(const char *error, const char *info, va_list args) const { replyv(nullptr, error, info, args); }
314 inline void req::failf(const char *error, const char *info, ...) const
317 va_start(args, info);
318 failv(error, info, args);
323 inline T *req::context() const
325 T* (*creater)(void*) = [](){return new T();};
326 void (*freer)(T*) = [](T*t){delete t;};
327 return reinterpret_cast<T*>(afb_req_context(req_, 0,
328 reinterpret_cast<void *(*)(void*)>(creater),
329 reinterpret_cast<void (*)(void*)>(freer), nullptr));
332 inline void req::addref() const { afb_req_addref(req_); }
334 inline void req::unref() const { afb_req_unref(req_); }
336 inline void req::session_close() const { afb_req_session_close(req_); }
338 inline bool req::session_set_LOA(unsigned level) const { return !afb_req_session_set_LOA(req_, level); }
340 inline bool req::subscribe(const event &event) const { return !afb_req_subscribe(req_, event); }
342 inline bool req::unsubscribe(const event &event) const { return !afb_req_unsubscribe(req_, event); }
344 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
346 afb_req_subcall(req_, api, verb, args, flags, callback, closure);
350 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
352 subcall(api, verb, args, flags, reinterpret_cast<void(*)(void*,json_object*,const char*,const char*,afb_req_t)>(callback), reinterpret_cast<void*>(closure));
355 inline bool req::subcallsync(const char *api, const char *verb, json_object *args, int flags, struct json_object *&object, char *&error, char *&info) const
357 return !afb_req_subcall_sync(req_, api, verb, args, flags, &object, &error, &info);
360 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
362 afb_req_subcall_legacy(req_, api, verb, args, callback, closure);
366 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
368 subcall(api, verb, args, reinterpret_cast<void(*)(void*,int,json_object*,afb_req_t)>(callback), reinterpret_cast<void*>(closure));
371 inline bool req::subcallsync(const char *api, const char *verb, json_object *args, struct json_object *&result) const
373 return !afb_req_subcall_sync_legacy(req_, api, verb, args, &result);
376 inline void req::verbose(int level, const char *file, int line, const char * func, const char *fmt, va_list args) const
378 afb_req_verbose(req_, level, file, line, func, fmt, args);
381 inline void req::verbose(int level, const char *file, int line, const char * func, const char *fmt, ...) const
385 afb_req_verbose(req_, level, file, line, func, fmt, args);
389 inline bool req::has_permission(const char *permission) const
391 return bool(afb_req_has_permission(req_, permission));
394 inline char *req::get_application_id() const
396 return afb_req_get_application_id(req_);
399 inline int req::get_uid() const
401 return afb_req_get_uid(req_);
404 inline json_object *req::get_client_info() const
406 return afb_req_get_client_info(req_);
410 inline int broadcast_event(const char *name, json_object *object)
411 { return afb_daemon_broadcast_event(name, object); }
413 inline event make_event(const char *name)
414 { return afb_daemon_make_event(name); }
416 inline void verbose(int level, const char *file, int line, const char * func, const char *fmt, va_list args)
417 { afb_daemon_verbose(level, file, line, func, fmt, args); }
419 inline void verbose(int level, const char *file, int line, const char * func, const char *fmt, ...)
420 { va_list args; va_start(args, fmt); verbose(level, file, line, func, fmt, args); va_end(args); }
422 inline int rootdir_get_fd()
423 { return afb_daemon_rootdir_get_fd(); }
425 inline int rootdir_open_locale_fd(const char *filename, int flags, const char *locale)
426 { return afb_daemon_rootdir_open_locale(filename, flags, locale); }
428 inline int queue_job(void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
429 { return afb_daemon_queue_job(callback, argument, group, timeout); }
431 inline int require_api(const char *apiname, bool initialized)
432 { return afb_daemon_require_api(apiname, int(initialized)); }
434 inline int add_alias(const char *apiname, const char *aliasname)
435 { return afb_daemon_add_alias(apiname, aliasname); }
438 { return afb_get_logmask(); }
440 inline bool wants_errors()
441 { return AFB_SYSLOG_MASK_WANT_ERROR(logmask()); }
443 inline bool wants_warnings()
444 { return AFB_SYSLOG_MASK_WANT_WARNING(logmask()); }
446 inline bool wants_notices()
447 { return AFB_SYSLOG_MASK_WANT_NOTICE(logmask()); }
449 inline bool wants_infos()
450 { return AFB_SYSLOG_MASK_WANT_INFO(logmask()); }
452 inline bool wants_debugs()
453 { return AFB_SYSLOG_MASK_WANT_DEBUG(logmask()); }
455 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)
457 afb_service_call(api, verb, args, callback, closure);
461 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)
463 afb_service_call(api, verb, args, reinterpret_cast<void(*)(void*,int,json_object*,afb_api_t)>(callback), reinterpret_cast<void*>(closure));
466 inline bool callsync(const char *api, const char *verb, struct json_object *args, struct json_object *&result, char *&error, char *&info)
468 return !!afb_service_call_sync(api, verb, args, &result, &error, &info);
471 /*************************************************************************/
472 /* declaration of the binding's authorization s */
473 /*************************************************************************/
475 constexpr afb_auth auth_no()
477 afb_auth r = { afb_auth_No, 0, 0};
478 r.type = afb_auth_No;
482 constexpr afb_auth auth_yes()
484 afb_auth r = { afb_auth_No, 0, 0};
485 r.type = afb_auth_Yes;
489 constexpr afb_auth auth_token()
491 afb_auth r = { afb_auth_No, 0, 0};
492 r.type = afb_auth_Token;
496 constexpr afb_auth auth_LOA(unsigned loa)
498 afb_auth r = { afb_auth_No, 0, 0};
499 r.type = afb_auth_LOA;
504 constexpr afb_auth auth_permission(const char *permission)
506 afb_auth r = { afb_auth_No, 0, 0};
507 r.type = afb_auth_Permission;
512 constexpr afb_auth auth_not(const afb_auth *other)
514 afb_auth r = { afb_auth_No, 0, 0};
515 r.type = afb_auth_Not;
520 constexpr afb_auth auth_not(const afb_auth &other)
522 return auth_not(&other);
525 constexpr afb_auth auth_or(const afb_auth *first, const afb_auth *next)
527 afb_auth r = { afb_auth_No, 0, 0};
528 r.type = afb_auth_Or;
534 constexpr afb_auth auth_or(const afb_auth &first, const afb_auth &next)
536 return auth_or(&first, &next);
539 constexpr afb_auth auth_and(const afb_auth *first, const afb_auth *next)
541 afb_auth r = { afb_auth_No, 0, 0};
542 r.type = afb_auth_And;
548 constexpr afb_auth auth_and(const afb_auth &first, const afb_auth &next)
550 return auth_and(&first, &next);
553 constexpr afb_verb_t verb(
555 void (*callback)(afb_req_t),
556 const char *info = nullptr,
557 uint16_t session = 0,
558 const afb_auth *auth = nullptr,
560 void *vcbdata = nullptr
563 afb_verb_t r = { 0, 0, 0, 0, 0, 0, 0 };
565 r.callback = callback;
569 r.glob = (unsigned)glob;
574 constexpr afb_verb_t verbend()
576 afb_verb_t r = verb(nullptr, nullptr);
580 constexpr afb_binding_t binding(
582 const afb_verb_t *verbs,
583 const char *info = nullptr,
584 int (*init)(afb_api_t) = nullptr,
585 const char *specification = nullptr,
586 void (*onevent)(afb_api_t, const char*, struct json_object*) = nullptr,
587 bool noconcurrency = false,
588 int (*preinit)(afb_api_t) = nullptr,
589 void *userdata = nullptr
592 afb_binding_t r = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
594 r.specification = specification;
600 r.noconcurrency = noconcurrency ? 1 : 0;
601 r.userdata = userdata;
605 /*************************************************************************/
607 /*************************************************************************/