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;
115 const char *name() const;
124 arg(const struct afb_arg &a);
125 arg(const arg &other);
126 arg &operator=(const arg &other);
128 operator const struct afb_arg&() const;
130 bool has_name() const;
131 bool has_value() const;
132 bool has_path() const;
134 const char *name() const;
135 const char *value() const;
136 const char *path() const;
146 struct afb_stored_req *sreq_;
150 stored(struct afb_stored_req *sr);
152 stored(const stored &other);
153 stored &operator =(const stored &other);
161 req(const struct afb_req &r);
162 req(const req &other);
163 req &operator=(const req &other);
165 operator const struct afb_req&() const;
167 operator bool() const;
168 bool is_valid() const;
170 arg get(const char *name) const;
172 const char *value(const char *name) const;
174 const char *path(const char *name) const;
176 json_object *json() const;
178 void success(json_object *obj = nullptr, const char *info = nullptr) const;
179 void successf(json_object *obj, const char *info, ...) const;
181 void fail(const char *status = "failed", const char *info = nullptr) const;
182 void failf(const char *status, const char *info, ...) const;
184 void *context_get() const;
186 void context_set(void *context, void (*free_context)(void*)) const;
188 void *context(void *(*create_context)(), void (*free_context)(void*)) const;
190 template < class T > T *context() const;
192 void context_clear() const;
198 void session_close() const;
200 bool session_set_LOA(unsigned level) const;
202 stored store() const;
204 bool subscribe(const event &event) const;
206 bool unsubscribe(const event &event) const;
208 void subcall(const char *api, const char *verb, json_object *args, void (*callback)(void *closure, int iserror, json_object *result), void *closure) const;
210 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;
212 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;
214 bool subcallsync(const char *api, const char *verb, json_object *args, struct json_object *&result) const;
216 void verbose(int level, const char *file, int line, const char * func, const char *fmt, va_list args) const;
218 void verbose(int level, const char *file, int line, const char * func, const char *fmt, ...) const;
220 bool has_permission(const char *permission) const;
222 char *get_application_id() const;
227 /*************************************************************************/
228 /* effective declaration of classes */
229 /*************************************************************************/
230 /////////////////////////////////////////////////////////////////////////////////////////////////////
231 /////////////////////////////////////////////////////////////////////////////////////////////////////
232 /////////////////////////////////////////////////////////////////////////////////////////////////////
233 /////////////////////////////////////////////////////////////////////////////////////////////////////
234 /////////////////////////////////////////////////////////////////////////////////////////////////////
235 /////////////////////////////////////////////////////////////////////////////////////////////////////
236 /////////////////////////////////////////////////////////////////////////////////////////////////////
237 /////////////////////////////////////////////////////////////////////////////////////////////////////
238 /////////////////////////////////////////////////////////////////////////////////////////////////////
239 /////////////////////////////////////////////////////////////////////////////////////////////////////
240 /////////////////////////////////////////////////////////////////////////////////////////////////////
241 /////////////////////////////////////////////////////////////////////////////////////////////////////
242 /////////////////////////////////////////////////////////////////////////////////////////////////////
243 /////////////////////////////////////////////////////////////////////////////////////////////////////
244 /////////////////////////////////////////////////////////////////////////////////////////////////////
245 /////////////////////////////////////////////////////////////////////////////////////////////////////
248 /*************************************************************************/
249 /* effective declaration of classes */
250 /*************************************************************************/
253 inline event::event(const struct afb_event &e) : event_(e) { }
254 inline event::event(const event &other) : event_(other.event_) { }
255 inline event &event::operator=(const event &other) { event_ = other.event_; return *this; }
257 inline event::operator const struct afb_event&() const { return event_; }
259 inline event::operator bool() const { return is_valid(); }
260 inline bool event::is_valid() const { return afb_event_is_valid(event_); }
262 inline void event::invalidate() { event_.itf = NULL; event_.closure = NULL; }
264 inline int event::broadcast(json_object *object) const { return afb_event_broadcast(event_, object); }
265 inline int event::push(json_object *object) const { return afb_event_push(event_, object); }
267 inline void event::unref() { afb_event_unref(event_); invalidate(); }
268 inline void event::addref() { afb_event_addref(event_); }
269 inline const char *event::name() const { return afb_event_name(event_); }
272 inline arg::arg(const struct afb_arg &a) : arg_(a) {}
273 inline arg::arg(const arg &other) : arg_(other.arg_) {}
274 inline arg &arg::operator=(const arg &other) { arg_ = other.arg_; return *this; }
276 inline arg::operator const struct afb_arg&() const { return arg_; }
278 inline bool arg::has_name() const { return !!arg_.name; }
279 inline bool arg::has_value() const { return !!arg_.value; }
280 inline bool arg::has_path() const { return !!arg_.path; }
282 inline const char *arg::name() const { return arg_.name; }
283 inline const char *arg::value() const { return arg_.value; }
284 inline const char *arg::path() const { return arg_.path; }
288 inline req::stored::stored(struct afb_stored_req *sr) : sreq_(sr) {}
290 inline req::stored::stored(const req::stored &other) : sreq_(other.sreq_) {}
292 inline req::stored &req::stored::operator =(const req::stored &other) { sreq_ = other.sreq_; return *this; }
294 inline req req::stored::unstore() const { return req(afb_daemon_unstore_req_v2(sreq_)); }
297 inline req::req(const struct afb_req &r) : req_(r) {}
298 inline req::req(const req &other) : req_(other.req_) {}
299 inline req &req::operator=(const req &other) { req_ = other.req_; return *this; }
301 inline req::operator const struct afb_req&() const { return req_; }
303 inline req::operator bool() const { return !!afb_req_is_valid(req_); }
304 inline bool req::is_valid() const { return !!afb_req_is_valid(req_); }
306 inline arg req::get(const char *name) const { return arg(afb_req_get(req_, name)); }
308 inline const char *req::value(const char *name) const { return afb_req_value(req_, name); }
310 inline const char *req::path(const char *name) const { return afb_req_path(req_, name); }
312 inline json_object *req::json() const { return afb_req_json(req_); }
314 inline void req::success(json_object *obj, const char *info) const { afb_req_success(req_, obj, info); }
315 inline void req::successf(json_object *obj, const char *info, ...) const
318 va_start(args, info);
319 afb_req_success_v(req_, obj, info, args);
323 inline void req::fail(const char *status, const char *info) const { afb_req_fail(req_, status, info); }
324 inline void req::failf(const char *status, const char *info, ...) const
327 va_start(args, info);
328 afb_req_fail_v(req_, status, info, args);
332 inline void *req::context_get() const { return afb_req_context_get(req_); }
334 inline void req::context_set(void *context, void (*free_context)(void*)) const { afb_req_context_set(req_, context, free_context); }
336 inline void *req::context(void *(*create_context)(), void (*free_context)(void*)) const { return afb_req_context(req_, create_context, free_context); }
339 inline T *req::context() const
341 T* (*creater)() = [](){return new T();};
342 void (*freer)(T*) = [](T*t){delete t;};
343 return reinterpret_cast<T*>(afb_req_context(req_,
344 reinterpret_cast<void *(*)()>(creater),
345 reinterpret_cast<void (*)(void*)>(freer)));
348 inline void req::context_clear() const { afb_req_context_clear(req_); }
350 inline void req::addref() const { afb_req_addref(req_); }
352 inline void req::unref() const { afb_req_unref(req_); }
354 inline void req::session_close() const { afb_req_session_close(req_); }
356 inline bool req::session_set_LOA(unsigned level) const { return !!afb_req_session_set_LOA(req_, level); }
358 inline req::stored req::store() const { return stored(afb_req_store_v2(req_)); }
360 inline bool req::subscribe(const event &event) const { return !afb_req_subscribe(req_, event); }
362 inline bool req::unsubscribe(const event &event) const { return !afb_req_unsubscribe(req_, event); }
364 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
366 afb_req_subcall(req_, api, verb, args, callback, closure);
369 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
371 afb_req_subcall_req(req_, api, verb, args, callback, closure);
375 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
377 afb_req_subcall(req_, api, verb, args, reinterpret_cast<void(*)(void*,int,json_object*)>(callback), reinterpret_cast<void*>(closure));
380 inline bool req::subcallsync(const char *api, const char *verb, json_object *args, struct json_object *&result) const
382 return !!afb_req_subcall_sync(req_, api, verb, args, &result);
385 inline void req::verbose(int level, const char *file, int line, const char * func, const char *fmt, va_list args) const
387 req_.itf->vverbose(req_.closure, level, file, line, func, fmt, args);
390 inline void req::verbose(int level, const char *file, int line, const char * func, const char *fmt, ...) const
394 req_.itf->vverbose(req_.closure, level, file, line, func, fmt, args);
398 inline bool req::has_permission(const char *permission) const
400 return bool(req_.itf->has_permission(req_.closure, permission));
403 inline char *req::get_application_id() const
405 return req_.itf->get_application_id(req_.closure);
408 inline int req::get_uid() const
410 return req_.itf->get_uid(req_.closure);
414 inline struct sd_event *get_event_loop()
415 { return afb_daemon_get_event_loop_v2(); }
417 inline struct sd_bus *get_system_bus()
418 { return afb_daemon_get_system_bus_v2(); }
420 inline struct sd_bus *get_user_bus()
421 { return afb_daemon_get_user_bus_v2(); }
423 inline int broadcast_event(const char *name, json_object *object)
424 { return afb_daemon_broadcast_event_v2(name, object); }
426 inline event make_event(const char *name)
427 { return afb_daemon_make_event_v2(name); }
429 inline void verbose(int level, const char *file, int line, const char * func, const char *fmt, va_list args)
430 { afb_daemon_verbose_v2(level, file, line, func, fmt, args); }
432 inline void verbose(int level, const char *file, int line, const char * func, const char *fmt, ...)
433 { va_list args; va_start(args, fmt); verbose(level, file, line, func, fmt, args); va_end(args); }
435 inline int rootdir_get_fd()
436 { return afb_daemon_rootdir_get_fd_v2(); }
438 inline int rootdir_open_locale_fd(const char *filename, int flags, const char *locale)
439 { return afb_daemon_rootdir_open_locale_v2(filename, flags, locale); }
441 inline int queue_job(void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
442 { return afb_daemon_queue_job_v2(callback, argument, group, timeout); }
444 inline int require_api(const char *apiname, bool initialized)
445 { return afb_daemon_require_api_v2(apiname, int(initialized)); }
447 inline int rename_api(const char *apiname)
448 { return afb_daemon_rename_api_v2(apiname); }
450 inline int verbosity()
451 { return afb_get_verbosity(); }
453 inline bool wants_errors()
454 { return afb_verbose_error(); }
456 inline bool wants_warnings()
457 { return afb_verbose_warning(); }
459 inline bool wants_notices()
460 { return afb_verbose_notice(); }
462 inline bool wants_infos()
463 { return afb_verbose_info(); }
465 inline bool wants_debugs()
466 { return afb_verbose_debug(); }
468 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)
470 afb_service_call(api, verb, args, callback, closure);
474 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)
476 afb_service_call(api, verb, args, reinterpret_cast<void(*)(void*,int,json_object*)>(callback), reinterpret_cast<void*>(closure));
479 inline bool callsync(const char *api, const char *verb, struct json_object *args, struct json_object *&result)
481 return !!afb_service_call_sync(api, verb, args, &result);
484 /*************************************************************************/
485 /* declaration of the binding's authorization s */
486 /*************************************************************************/
488 constexpr afb_auth auth_no()
490 afb_auth r = { afb_auth_No, 0, 0};
491 r.type = afb_auth_No;
495 constexpr afb_auth auth_yes()
497 afb_auth r = { afb_auth_No, 0, 0};
498 r.type = afb_auth_Yes;
502 constexpr afb_auth auth_token()
504 afb_auth r = { afb_auth_No, 0, 0};
505 r.type = afb_auth_Token;
509 constexpr afb_auth auth_LOA(unsigned loa)
511 afb_auth r = { afb_auth_No, 0, 0};
512 r.type = afb_auth_LOA;
517 constexpr afb_auth auth_permission(const char *permission)
519 afb_auth r = { afb_auth_No, 0, 0};
520 r.type = afb_auth_Permission;
525 constexpr afb_auth auth_not(const afb_auth *other)
527 afb_auth r = { afb_auth_No, 0, 0};
528 r.type = afb_auth_Not;
533 constexpr afb_auth auth_not(const afb_auth &other)
535 return auth_not(&other);
538 constexpr afb_auth auth_or(const afb_auth *first, const afb_auth *next)
540 afb_auth r = { afb_auth_No, 0, 0};
541 r.type = afb_auth_Or;
547 constexpr afb_auth auth_or(const afb_auth &first, const afb_auth &next)
549 return auth_or(&first, &next);
552 constexpr afb_auth auth_and(const afb_auth *first, const afb_auth *next)
554 afb_auth r = { afb_auth_No, 0, 0};
555 r.type = afb_auth_And;
561 constexpr afb_auth auth_and(const afb_auth &first, const afb_auth &next)
563 return auth_and(&first, &next);
566 constexpr afb_verb_v2 verb(const char *name, void (*callback)(afb_req), const char *info = nullptr, unsigned session = 0, const afb_auth *auth = nullptr)
568 afb_verb_v2 r = { 0, 0, 0, 0, 0 };
570 r.callback = callback;
577 constexpr afb_verb_v2 verbend()
579 afb_verb_v2 r = { 0, 0, 0, 0, 0 };
581 r.callback = nullptr;
588 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)
590 afb_binding_v2 r = { 0, 0, 0, 0, 0, 0, 0, 0 };
592 r.specification = specification;
598 r.noconcurrency = noconcurrency ? 1 : 0;
602 /*************************************************************************/
604 /*************************************************************************/