1fc8b497fbe13c49ed1aaa6e61e70224ad76fd86
[src/app-framework-binder.git] / include / afb / afb-binding.hpp
1 /*
2  * Copyright (C) 2016, 2017 "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 <cstdlib>
21 #include <cstdarg>
22 #include <functional>
23
24 /* ensure version */
25 #ifndef AFB_BINDING_VERSION
26 # define AFB_BINDING_VERSION   2
27 #endif
28
29 /* check the version */
30 #if AFB_BINDING_VERSION < 2
31 # error "AFB_BINDING_VERSION must be at least 2"
32 #endif
33
34 /* get C definitions of bindings */
35 extern "C" {
36 #include "afb-binding.h"
37 }
38
39 namespace afb {
40 /*************************************************************************/
41 /* pre-declaration of classes                                            */
42 /*************************************************************************/
43
44 class arg;
45 class event;
46 class req;
47 class stored_req;
48
49 /*************************************************************************/
50 /* declaration of functions                                              */
51 /*************************************************************************/
52
53 struct sd_event *get_event_loop();
54 struct sd_bus *get_system_bus();
55 struct sd_bus *get_user_bus();
56
57 int broadcast_event(const char *name, json_object *object = nullptr);
58
59 event make_event(const char *name);
60
61 void verbose(int level, const char *file, int line, const char * func, const char *fmt, va_list args);
62
63 void verbose(int level, const char *file, int line, const char * func, const char *fmt, ...);
64
65 int rootdir_get_fd();
66
67 int rootdir_open_locale_fd(const char *filename, int flags, const char *locale = nullptr);
68
69 int queue_job(void (*callback)(int signum, void *arg), void *argument, void *group, int timeout);
70
71 int require_api(const char *apiname, bool initialized = true);
72
73 int rename_api(const char *apiname);
74
75 int verbosity();
76
77 bool wants_errors();
78 bool wants_warnings();
79 bool wants_notices();
80 bool wants_infos();
81 bool wants_debugs();
82
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);
84
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);
86
87 bool callsync(const char *api, const char *verb, struct json_object *args, struct json_object *&result);
88
89 /*************************************************************************/
90 /* effective declaration of classes                                      */
91 /*************************************************************************/
92
93 /* events */
94 class event
95 {
96         struct afb_event event_;
97 public:
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);
102
103         operator const struct afb_event&() const;
104
105         operator bool() const;
106         bool is_valid() const;
107
108         void invalidate();
109
110         int broadcast(json_object *object) const;
111         int push(json_object *object) const;
112
113         void drop();
114         const char *name() const;
115 };
116
117 /* args */
118 class arg
119 {
120         struct afb_arg arg_;
121 public:
122         arg() = delete;
123         arg(const struct afb_arg &a);
124         arg(const arg &other);
125         arg &operator=(const arg &other);
126
127         operator const struct afb_arg&() const;
128
129         bool has_name() const;
130         bool has_value() const;
131         bool has_path() const;
132
133         const char *name() const;
134         const char *value() const;
135         const char *path() const;
136 };
137
138 /* req(uest) */
139 class req
140 {
141         struct afb_req req_;
142 public:
143         class stored
144         {
145                 struct afb_stored_req *sreq_;
146
147                 friend class req;
148                 stored() = delete;
149                 stored(struct afb_stored_req *sr);
150         public:
151                 stored(const stored &other);
152                 stored &operator =(const stored &other);
153                 req unstore() const;
154         };
155
156         class stored;
157
158 public:
159         req() = delete;
160         req(const struct afb_req &r);
161         req(const req &other);
162         req &operator=(const req &other);
163
164         operator const struct afb_req&() const;
165
166         operator bool() const;
167         bool is_valid() const;
168
169         arg get(const char *name) const;
170
171         const char *value(const char *name) const;
172
173         const char *path(const char *name) const;
174
175         json_object *json() const;
176
177         void success(json_object *obj = nullptr, const char *info = nullptr) const;
178         void successf(json_object *obj, const char *info, ...) const;
179
180         void fail(const char *status = "failed", const char *info = nullptr) const;
181         void failf(const char *status, const char *info, ...) const;
182
183         void *context_get() const;
184
185         void context_set(void *context, void (*free_context)(void*)) const;
186
187         void *context(void *(*create_context)(), void (*free_context)(void*)) const;
188
189         template < class T > T *context() const;
190
191         void context_clear() const;
192
193         void addref() const;
194
195         void unref() const;
196
197         void session_close() const;
198
199         bool session_set_LOA(unsigned level) const;
200
201         stored store() const;
202
203         bool subscribe(const event &event) const;
204
205         bool unsubscribe(const event &event) const;
206
207         void subcall(const char *api, const char *verb, json_object *args, void (*callback)(void *closure, int iserror, json_object *result), void *closure) const;
208
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;
210
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;
212
213         bool subcallsync(const char *api, const char *verb, json_object *args, struct json_object *&result) const;
214
215         void verbose(int level, const char *file, int line, const char * func, const char *fmt, va_list args) const;
216
217         void verbose(int level, const char *file, int line, const char * func, const char *fmt, ...) const;
218
219         bool has_permission(const char *permission) const;
220
221         char *get_application_id() const;
222 };
223
224 /*************************************************************************/
225 /* effective declaration of classes                                      */
226 /*************************************************************************/
227 /////////////////////////////////////////////////////////////////////////////////////////////////////
228 /////////////////////////////////////////////////////////////////////////////////////////////////////
229 /////////////////////////////////////////////////////////////////////////////////////////////////////
230 /////////////////////////////////////////////////////////////////////////////////////////////////////
231 /////////////////////////////////////////////////////////////////////////////////////////////////////
232 /////////////////////////////////////////////////////////////////////////////////////////////////////
233 /////////////////////////////////////////////////////////////////////////////////////////////////////
234 /////////////////////////////////////////////////////////////////////////////////////////////////////
235 /////////////////////////////////////////////////////////////////////////////////////////////////////
236 /////////////////////////////////////////////////////////////////////////////////////////////////////
237 /////////////////////////////////////////////////////////////////////////////////////////////////////
238 /////////////////////////////////////////////////////////////////////////////////////////////////////
239 /////////////////////////////////////////////////////////////////////////////////////////////////////
240 /////////////////////////////////////////////////////////////////////////////////////////////////////
241 /////////////////////////////////////////////////////////////////////////////////////////////////////
242 /////////////////////////////////////////////////////////////////////////////////////////////////////
243
244
245 /*************************************************************************/
246 /* effective declaration of classes                                      */
247 /*************************************************************************/
248
249 /* events */
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; }
253
254 inline event::operator const struct afb_event&() const { return event_; }
255
256 inline event::operator bool() const { return is_valid(); }
257 inline bool event::is_valid() const { return afb_event_is_valid(event_); } 
258
259 inline void event::invalidate() { event_.itf = NULL; event_.closure = NULL; }
260
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); }
263
264 inline void event::drop() { afb_event_drop(event_); invalidate(); }
265 inline const char *event::name() const { return afb_event_name(event_); }
266
267 /* args */
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; }
271
272 inline arg::operator const struct afb_arg&() const { return arg_; }
273
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; }
277
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; }
281
282 /* req(uests)s */
283
284 inline req::stored::stored(struct afb_stored_req *sr) : sreq_(sr) {}
285
286 inline req::stored::stored(const req::stored &other) : sreq_(other.sreq_) {}
287
288 inline req::stored &req::stored::operator =(const req::stored &other) { sreq_ = other.sreq_; return *this; }
289
290 inline req req::stored::unstore() const { return req(afb_daemon_unstore_req_v2(sreq_)); }
291
292
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; }
296
297 inline req::operator const struct afb_req&() const { return req_; }
298
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_); }
301
302 inline arg req::get(const char *name) const { return arg(afb_req_get(req_, name)); }
303
304 inline const char *req::value(const char *name) const { return afb_req_value(req_, name); }
305
306 inline const char *req::path(const char *name) const { return afb_req_path(req_, name); }
307
308 inline json_object *req::json() const { return afb_req_json(req_); }
309
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
312 {
313         va_list args;
314         va_start(args, info);
315         afb_req_success_v(req_, obj, info, args);
316         va_end(args);
317 }
318
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
321 {
322         va_list args;
323         va_start(args, info);
324         afb_req_fail_v(req_, status, info, args);
325         va_end(args);
326 }
327
328 inline void *req::context_get() const { return afb_req_context_get(req_); }
329
330 inline void req::context_set(void *context, void (*free_context)(void*)) const { afb_req_context_set(req_, context, free_context); }
331
332 inline void *req::context(void *(*create_context)(), void (*free_context)(void*)) const { return afb_req_context(req_, create_context, free_context); }
333
334 template < class T >
335 inline T *req::context() const
336 {
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)));
342 }
343
344 inline void req::context_clear() const { afb_req_context_clear(req_); }
345
346 inline void req::addref() const { afb_req_addref(req_); }
347
348 inline void req::unref() const { afb_req_unref(req_); }
349
350 inline void req::session_close() const { afb_req_session_close(req_); }
351
352 inline bool req::session_set_LOA(unsigned level) const { return !!afb_req_session_set_LOA(req_, level); }
353
354 inline req::stored req::store() const { return stored(afb_req_store_v2(req_)); }
355
356 inline bool req::subscribe(const event &event) const { return !afb_req_subscribe(req_, event); }
357
358 inline bool req::unsubscribe(const event &event) const { return !afb_req_unsubscribe(req_, event); }
359
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
361 {
362         afb_req_subcall(req_, api, verb, args, callback, closure);
363 }
364
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
366 {
367         afb_req_subcall_req(req_, api, verb, args, callback, closure);
368 }
369
370 template <class T>
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
372 {
373         afb_req_subcall(req_, api, verb, args, reinterpret_cast<void(*)(void*,int,json_object*)>(callback), reinterpret_cast<void*>(closure));
374 }
375
376 inline bool req::subcallsync(const char *api, const char *verb, json_object *args, struct json_object *&result) const
377 {
378         return !!afb_req_subcall_sync(req_, api, verb, args, &result);
379 }
380
381 inline void req::verbose(int level, const char *file, int line, const char * func, const char *fmt, va_list args) const
382 {
383         req_.itf->vverbose(req_.closure, level, file, line, func, fmt, args);
384 }
385
386 inline void req::verbose(int level, const char *file, int line, const char * func, const char *fmt, ...) const
387 {
388         va_list args;
389         va_start(args, fmt);
390         req_.itf->vverbose(req_.closure, level, file, line, func, fmt, args);
391         va_end(args);
392 }
393
394 inline bool req::has_permission(const char *permission) const
395 {
396         return bool(req_.itf->has_permission(req_.closure, permission));
397 }
398
399 inline char *req::get_application_id() const
400 {
401         return req_.itf->get_application_id(req_.closure);
402 }
403
404 /* commons */
405 inline struct sd_event *get_event_loop()
406         { return afb_daemon_get_event_loop_v2(); }
407
408 inline struct sd_bus *get_system_bus()
409         { return afb_daemon_get_system_bus_v2(); }
410
411 inline struct sd_bus *get_user_bus()
412         { return afb_daemon_get_user_bus_v2(); }
413
414 inline int broadcast_event(const char *name, json_object *object)
415         { return afb_daemon_broadcast_event_v2(name, object); }
416
417 inline event make_event(const char *name)
418         { return afb_daemon_make_event_v2(name); }
419
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); }
422
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); }
425
426 inline int rootdir_get_fd()
427         { return afb_daemon_rootdir_get_fd_v2(); }
428
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); }
431
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); }
434
435 inline int require_api(const char *apiname, bool initialized)
436         { return afb_daemon_require_api_v2(apiname, int(initialized)); }
437
438 inline int rename_api(const char *apiname)
439         { return afb_daemon_rename_api_v2(apiname); }
440
441 inline int verbosity()
442         { return afb_get_verbosity(); }
443
444 inline bool wants_errors()
445         { return afb_verbose_error(); }
446
447 inline bool wants_warnings()
448         { return afb_verbose_warning(); }
449
450 inline bool wants_notices()
451         { return afb_verbose_notice(); }
452
453 inline bool wants_infos()
454         { return afb_verbose_info(); }
455
456 inline bool wants_debugs()
457         { return afb_verbose_debug(); }
458
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)
460 {
461         afb_service_call(api, verb, args, callback, closure);
462 }
463
464 template <class T>
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)
466 {
467         afb_service_call(api, verb, args, reinterpret_cast<void(*)(void*,int,json_object*)>(callback), reinterpret_cast<void*>(closure));
468 }
469
470 inline bool callsync(const char *api, const char *verb, struct json_object *args, struct json_object *&result)
471 {
472         return !!afb_service_call_sync(api, verb, args, &result);
473 }
474
475 /*************************************************************************/
476 /* declaration of the binding's authorization s                          */
477 /*************************************************************************/
478
479 constexpr afb_auth auth_no()
480 {
481         afb_auth r = { afb_auth_No, 0, 0};
482         r.type = afb_auth_No;
483         return r;
484 }
485
486 constexpr afb_auth auth_yes()
487 {
488         afb_auth r = { afb_auth_No, 0, 0};
489         r.type = afb_auth_Yes;
490         return r;
491 }
492
493 constexpr afb_auth auth_token()
494 {
495         afb_auth r = { afb_auth_No, 0, 0};
496         r.type = afb_auth_Token;
497         return r;
498 }
499
500 constexpr afb_auth auth_LOA(unsigned loa)
501 {
502         afb_auth r = { afb_auth_No, 0, 0};
503         r.type = afb_auth_LOA;
504         r.loa = loa;
505         return r;
506 }
507
508 constexpr afb_auth auth_permission(const char *permission)
509 {
510         afb_auth r = { afb_auth_No, 0, 0};
511         r.type = afb_auth_Permission;
512         r.text = permission;
513         return r;
514 }
515
516 constexpr afb_auth auth_not(const afb_auth *other)
517 {
518         afb_auth r = { afb_auth_No, 0, 0};
519         r.type = afb_auth_Not;
520         r.first = other;
521         return r;
522 }
523
524 constexpr afb_auth auth_not(const afb_auth &other)
525 {
526         return auth_not(&other);
527 }
528
529 constexpr afb_auth auth_or(const afb_auth *first, const afb_auth *next)
530 {
531         afb_auth r = { afb_auth_No, 0, 0};
532         r.type = afb_auth_Or;
533         r.first = first;
534         r.next = next;
535         return r;
536 }
537
538 constexpr afb_auth auth_or(const afb_auth &first, const afb_auth &next)
539 {
540         return auth_or(&first, &next);
541 }
542
543 constexpr afb_auth auth_and(const afb_auth *first, const afb_auth *next)
544 {
545         afb_auth r = { afb_auth_No, 0, 0};
546         r.type = afb_auth_And;
547         r.first = first;
548         r.next = next;
549         return r;
550 }
551
552 constexpr afb_auth auth_and(const afb_auth &first, const afb_auth &next)
553 {
554         return auth_and(&first, &next);
555 }
556
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)
558 {
559         afb_verb_v2 r = { 0, 0, 0, 0, 0 };
560         r.verb = name;
561         r.callback = callback;
562         r.info = info;
563         r.session = session;
564         r.auth = auth;
565         return r;
566 }
567
568 constexpr afb_verb_v2 verbend()
569 {
570         afb_verb_v2 r = { 0, 0, 0, 0, 0 };
571         r.verb = nullptr;
572         r.callback = nullptr;
573         r.info = nullptr;
574         r.session = 0;
575         r.auth = nullptr;
576         return r;
577 }
578
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)
580 {
581         afb_binding_v2 r = { 0, 0, 0, 0, 0, 0, 0, 0 };
582         r.api = name;
583         r.specification = specification;
584         r.info = info;
585         r.verbs = verbs;
586         r.preinit = preinit;
587         r.init = init;
588         r.onevent = onevent;
589         r.noconcurrency = noconcurrency ? 1 : 0;
590         return r;
591 };
592
593 /*************************************************************************/
594 /***                         E N D                                     ***/
595 /*************************************************************************/
596 }