2 * Copyright (C) 2016, 2017 "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.
25 #ifndef AFB_BINDING_VERSION
26 # define AFB_BINDING_VERSION 2
29 /* check the version */
30 #if AFB_BINDING_VERSION < 2
31 # error "AFB_BINDING_VERSION must be at least 2"
34 /* get C definitions of bindings */
36 #include "afb-binding.h"
40 /*************************************************************************/
41 /* pre-declaration of classes */
42 /*************************************************************************/
49 /*************************************************************************/
50 /* declaration of functions */
51 /*************************************************************************/
53 struct sd_event *get_event_loop();
54 struct sd_bus *get_system_bus();
55 struct sd_bus *get_user_bus();
57 int broadcast_event(const char *name, json_object *object = nullptr);
59 event make_event(const char *name);
61 void verbose(int level, const char *file, int line, const char * func, const char *fmt, va_list args);
63 void verbose(int level, const char *file, int line, const char * func, const char *fmt, ...);
67 int rootdir_open_locale_fd(const char *filename, int flags, const char *locale = nullptr);
69 int queue_job(void (*callback)(int signum, void *arg), void *argument, void *group, int timeout);
71 int require_api(const char *apiname, bool initialized = true);
73 int rename_api(const char *apiname);
78 bool wants_warnings();
83 void call(const char *api, const char *verb, struct json_object *args, void (*callback)(void*closure, int iserror, struct json_object *result), void *closure);
85 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);
87 bool callsync(const char *api, const char *verb, struct json_object *args, struct json_object *&result);
89 /*************************************************************************/
90 /* effective declaration of classes */
91 /*************************************************************************/
96 struct afb_event event_;
98 event() { event_.itf = nullptr; event_.closure = nullptr; }
99 event(const struct afb_event &e);
100 event(const event &other);
101 event &operator=(const event &other);
103 operator const struct afb_event&() const;
105 operator bool() const;
106 bool is_valid() const;
110 int broadcast(json_object *object) const;
111 int push(json_object *object) const;
114 const char *name() const;
123 arg(const struct afb_arg &a);
124 arg(const arg &other);
125 arg &operator=(const arg &other);
127 operator const struct afb_arg&() const;
129 bool has_name() const;
130 bool has_value() const;
131 bool has_path() const;
133 const char *name() const;
134 const char *value() const;
135 const char *path() const;
145 struct afb_stored_req *sreq_;
149 stored(struct afb_stored_req *sr);
151 stored(const stored &other);
152 stored &operator =(const stored &other);
160 req(const struct afb_req &r);
161 req(const req &other);
162 req &operator=(const req &other);
164 operator const struct afb_req&() const;
166 operator bool() const;
167 bool is_valid() const;
169 arg get(const char *name) const;
171 const char *value(const char *name) const;
173 const char *path(const char *name) const;
175 json_object *json() const;
177 void success(json_object *obj = nullptr, const char *info = nullptr) const;
178 void successf(json_object *obj, const char *info, ...) const;
180 void fail(const char *status = "failed", const char *info = nullptr) const;
181 void failf(const char *status, const char *info, ...) const;
183 void *context_get() const;
185 void context_set(void *context, void (*free_context)(void*)) const;
187 void *context(void *(*create_context)(), void (*free_context)(void*)) const;
189 template < class T > T *context() const;
191 void context_clear() const;
197 void session_close() const;
199 bool session_set_LOA(unsigned level) const;
201 stored store() const;
203 bool subscribe(const event &event) const;
205 bool unsubscribe(const event &event) const;
207 void subcall(const char *api, const char *verb, json_object *args, void (*callback)(void *closure, int iserror, json_object *result), void *closure) const;
209 void subcall(const char *api, const char *verb, json_object *args, void (*callback)(void *closure, int iserror, json_object *result, afb_req req), void *closure) const;
211 template <class T> void subcall(const char *api, const char *verb, json_object *args, void (*callback)(T *closure, int iserror, json_object *result), T *closure) const;
213 bool subcallsync(const char *api, const char *verb, json_object *args, struct json_object *&result) const;
215 void verbose(int level, const char *file, int line, const char * func, const char *fmt, va_list args) const;
217 void verbose(int level, const char *file, int line, const char * func, const char *fmt, ...) const;
219 bool has_permission(const char *permission) const;
221 char *get_application_id() const;
224 /*************************************************************************/
225 /* effective declaration of classes */
226 /*************************************************************************/
227 /////////////////////////////////////////////////////////////////////////////////////////////////////
228 /////////////////////////////////////////////////////////////////////////////////////////////////////
229 /////////////////////////////////////////////////////////////////////////////////////////////////////
230 /////////////////////////////////////////////////////////////////////////////////////////////////////
231 /////////////////////////////////////////////////////////////////////////////////////////////////////
232 /////////////////////////////////////////////////////////////////////////////////////////////////////
233 /////////////////////////////////////////////////////////////////////////////////////////////////////
234 /////////////////////////////////////////////////////////////////////////////////////////////////////
235 /////////////////////////////////////////////////////////////////////////////////////////////////////
236 /////////////////////////////////////////////////////////////////////////////////////////////////////
237 /////////////////////////////////////////////////////////////////////////////////////////////////////
238 /////////////////////////////////////////////////////////////////////////////////////////////////////
239 /////////////////////////////////////////////////////////////////////////////////////////////////////
240 /////////////////////////////////////////////////////////////////////////////////////////////////////
241 /////////////////////////////////////////////////////////////////////////////////////////////////////
242 /////////////////////////////////////////////////////////////////////////////////////////////////////
245 /*************************************************************************/
246 /* effective declaration of classes */
247 /*************************************************************************/
250 inline event::event(const struct afb_event &e) : event_(e) { }
251 inline event::event(const event &other) : event_(other.event_) { }
252 inline event &event::operator=(const event &other) { event_ = other.event_; return *this; }
254 inline event::operator const struct afb_event&() const { return event_; }
256 inline event::operator bool() const { return is_valid(); }
257 inline bool event::is_valid() const { return afb_event_is_valid(event_); }
259 inline void event::invalidate() { event_.itf = NULL; event_.closure = NULL; }
261 inline int event::broadcast(json_object *object) const { return afb_event_broadcast(event_, object); }
262 inline int event::push(json_object *object) const { return afb_event_push(event_, object); }
264 inline void event::drop() { afb_event_drop(event_); invalidate(); }
265 inline const char *event::name() const { return afb_event_name(event_); }
268 inline arg::arg(const struct afb_arg &a) : arg_(a) {}
269 inline arg::arg(const arg &other) : arg_(other.arg_) {}
270 inline arg &arg::operator=(const arg &other) { arg_ = other.arg_; return *this; }
272 inline arg::operator const struct afb_arg&() const { return arg_; }
274 inline bool arg::has_name() const { return !!arg_.name; }
275 inline bool arg::has_value() const { return !!arg_.value; }
276 inline bool arg::has_path() const { return !!arg_.path; }
278 inline const char *arg::name() const { return arg_.name; }
279 inline const char *arg::value() const { return arg_.value; }
280 inline const char *arg::path() const { return arg_.path; }
284 inline req::stored::stored(struct afb_stored_req *sr) : sreq_(sr) {}
286 inline req::stored::stored(const req::stored &other) : sreq_(other.sreq_) {}
288 inline req::stored &req::stored::operator =(const req::stored &other) { sreq_ = other.sreq_; return *this; }
290 inline req req::stored::unstore() const { return req(afb_daemon_unstore_req_v2(sreq_)); }
293 inline req::req(const struct afb_req &r) : req_(r) {}
294 inline req::req(const req &other) : req_(other.req_) {}
295 inline req &req::operator=(const req &other) { req_ = other.req_; return *this; }
297 inline req::operator const struct afb_req&() const { return req_; }
299 inline req::operator bool() const { return !!afb_req_is_valid(req_); }
300 inline bool req::is_valid() const { return !!afb_req_is_valid(req_); }
302 inline arg req::get(const char *name) const { return arg(afb_req_get(req_, name)); }
304 inline const char *req::value(const char *name) const { return afb_req_value(req_, name); }
306 inline const char *req::path(const char *name) const { return afb_req_path(req_, name); }
308 inline json_object *req::json() const { return afb_req_json(req_); }
310 inline void req::success(json_object *obj, const char *info) const { afb_req_success(req_, obj, info); }
311 inline void req::successf(json_object *obj, const char *info, ...) const
314 va_start(args, info);
315 afb_req_success_v(req_, obj, info, args);
319 inline void req::fail(const char *status, const char *info) const { afb_req_fail(req_, status, info); }
320 inline void req::failf(const char *status, const char *info, ...) const
323 va_start(args, info);
324 afb_req_fail_v(req_, status, info, args);
328 inline void *req::context_get() const { return afb_req_context_get(req_); }
330 inline void req::context_set(void *context, void (*free_context)(void*)) const { afb_req_context_set(req_, context, free_context); }
332 inline void *req::context(void *(*create_context)(), void (*free_context)(void*)) const { return afb_req_context(req_, create_context, free_context); }
335 inline T *req::context() const
337 T* (*creater)() = [](){return new T();};
338 void (*freer)(T*) = [](T*t){delete t;};
339 return reinterpret_cast<T*>(afb_req_context(req_,
340 reinterpret_cast<void *(*)()>(creater),
341 reinterpret_cast<void (*)(void*)>(freer)));
344 inline void req::context_clear() const { afb_req_context_clear(req_); }
346 inline void req::addref() const { afb_req_addref(req_); }
348 inline void req::unref() const { afb_req_unref(req_); }
350 inline void req::session_close() const { afb_req_session_close(req_); }
352 inline bool req::session_set_LOA(unsigned level) const { return !!afb_req_session_set_LOA(req_, level); }
354 inline req::stored req::store() const { return stored(afb_req_store_v2(req_)); }
356 inline bool req::subscribe(const event &event) const { return !afb_req_subscribe(req_, event); }
358 inline bool req::unsubscribe(const event &event) const { return !afb_req_unsubscribe(req_, event); }
360 inline void req::subcall(const char *api, const char *verb, json_object *args, void (*callback)(void *closure, int iserror, json_object *result), void *closure) const
362 afb_req_subcall(req_, api, verb, args, callback, closure);
365 inline void req::subcall(const char *api, const char *verb, json_object *args, void (*callback)(void *closure, int iserror, json_object *result, struct afb_req req), void *closure) const
367 afb_req_subcall_req(req_, api, verb, args, callback, closure);
371 inline void req::subcall(const char *api, const char *verb, json_object *args, void (*callback)(T *closure, int iserror, json_object *result), T *closure) const
373 afb_req_subcall(req_, api, verb, args, reinterpret_cast<void(*)(void*,int,json_object*)>(callback), reinterpret_cast<void*>(closure));
376 inline bool req::subcallsync(const char *api, const char *verb, json_object *args, struct json_object *&result) const
378 return !!afb_req_subcall_sync(req_, api, verb, args, &result);
381 inline void req::verbose(int level, const char *file, int line, const char * func, const char *fmt, va_list args) const
383 req_.itf->vverbose(req_.closure, level, file, line, func, fmt, args);
386 inline void req::verbose(int level, const char *file, int line, const char * func, const char *fmt, ...) const
390 req_.itf->vverbose(req_.closure, level, file, line, func, fmt, args);
394 inline bool req::has_permission(const char *permission) const
396 return bool(req_.itf->has_permission(req_.closure, permission));
399 inline char *req::get_application_id() const
401 return req_.itf->get_application_id(req_.closure);
405 inline struct sd_event *get_event_loop()
406 { return afb_daemon_get_event_loop_v2(); }
408 inline struct sd_bus *get_system_bus()
409 { return afb_daemon_get_system_bus_v2(); }
411 inline struct sd_bus *get_user_bus()
412 { return afb_daemon_get_user_bus_v2(); }
414 inline int broadcast_event(const char *name, json_object *object)
415 { return afb_daemon_broadcast_event_v2(name, object); }
417 inline event make_event(const char *name)
418 { return afb_daemon_make_event_v2(name); }
420 inline void verbose(int level, const char *file, int line, const char * func, const char *fmt, va_list args)
421 { afb_daemon_verbose_v2(level, file, line, func, fmt, args); }
423 inline void verbose(int level, const char *file, int line, const char * func, const char *fmt, ...)
424 { va_list args; va_start(args, fmt); verbose(level, file, line, func, fmt, args); va_end(args); }
426 inline int rootdir_get_fd()
427 { return afb_daemon_rootdir_get_fd_v2(); }
429 inline int rootdir_open_locale_fd(const char *filename, int flags, const char *locale)
430 { return afb_daemon_rootdir_open_locale_v2(filename, flags, locale); }
432 inline int queue_job(void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
433 { return afb_daemon_queue_job_v2(callback, argument, group, timeout); }
435 inline int require_api(const char *apiname, bool initialized)
436 { return afb_daemon_require_api_v2(apiname, int(initialized)); }
438 inline int rename_api(const char *apiname)
439 { return afb_daemon_rename_api_v2(apiname); }
441 inline int verbosity()
442 { return afb_get_verbosity(); }
444 inline bool wants_errors()
445 { return afb_verbose_error(); }
447 inline bool wants_warnings()
448 { return afb_verbose_warning(); }
450 inline bool wants_notices()
451 { return afb_verbose_notice(); }
453 inline bool wants_infos()
454 { return afb_verbose_info(); }
456 inline bool wants_debugs()
457 { return afb_verbose_debug(); }
459 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)
461 afb_service_call(api, verb, args, callback, closure);
465 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)
467 afb_service_call(api, verb, args, reinterpret_cast<void(*)(void*,int,json_object*)>(callback), reinterpret_cast<void*>(closure));
470 inline bool callsync(const char *api, const char *verb, struct json_object *args, struct json_object *&result)
472 return !!afb_service_call_sync(api, verb, args, &result);
475 /*************************************************************************/
476 /* declaration of the binding's authorization s */
477 /*************************************************************************/
479 constexpr afb_auth auth_no()
481 afb_auth r = { afb_auth_No, 0, 0};
482 r.type = afb_auth_No;
486 constexpr afb_auth auth_yes()
488 afb_auth r = { afb_auth_No, 0, 0};
489 r.type = afb_auth_Yes;
493 constexpr afb_auth auth_token()
495 afb_auth r = { afb_auth_No, 0, 0};
496 r.type = afb_auth_Token;
500 constexpr afb_auth auth_LOA(unsigned loa)
502 afb_auth r = { afb_auth_No, 0, 0};
503 r.type = afb_auth_LOA;
508 constexpr afb_auth auth_permission(const char *permission)
510 afb_auth r = { afb_auth_No, 0, 0};
511 r.type = afb_auth_Permission;
516 constexpr afb_auth auth_not(const afb_auth *other)
518 afb_auth r = { afb_auth_No, 0, 0};
519 r.type = afb_auth_Not;
524 constexpr afb_auth auth_not(const afb_auth &other)
526 return auth_not(&other);
529 constexpr afb_auth auth_or(const afb_auth *first, const afb_auth *next)
531 afb_auth r = { afb_auth_No, 0, 0};
532 r.type = afb_auth_Or;
538 constexpr afb_auth auth_or(const afb_auth &first, const afb_auth &next)
540 return auth_or(&first, &next);
543 constexpr afb_auth auth_and(const afb_auth *first, const afb_auth *next)
545 afb_auth r = { afb_auth_No, 0, 0};
546 r.type = afb_auth_And;
552 constexpr afb_auth auth_and(const afb_auth &first, const afb_auth &next)
554 return auth_and(&first, &next);
557 constexpr afb_verb_v2 verb(const char *name, void (*callback)(afb_req), const char *info = nullptr, unsigned session = 0, const afb_auth *auth = nullptr)
559 afb_verb_v2 r = { 0, 0, 0, 0, 0 };
561 r.callback = callback;
568 constexpr afb_verb_v2 verbend()
570 afb_verb_v2 r = { 0, 0, 0, 0, 0 };
572 r.callback = nullptr;
579 const afb_binding_v2 binding(const char *name, const struct afb_verb_v2 *verbs, const char *info = nullptr, int (*init)() = nullptr, const char *specification = nullptr, void (*onevent)(const char*, struct json_object*) = nullptr, bool noconcurrency = false, int (*preinit)() = nullptr)
581 afb_binding_v2 r = { 0, 0, 0, 0, 0, 0, 0, 0 };
583 r.specification = specification;
589 r.noconcurrency = noconcurrency ? 1 : 0;
593 /*************************************************************************/
595 /*************************************************************************/