afb-binding.hpp: Fix constexpr of 'binding'
[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 unref();
114         void addref();
115         const char *name() const;
116 };
117
118 /* args */
119 class arg
120 {
121         struct afb_arg arg_;
122 public:
123         arg() = delete;
124         arg(const struct afb_arg &a);
125         arg(const arg &other);
126         arg &operator=(const arg &other);
127
128         operator const struct afb_arg&() const;
129
130         bool has_name() const;
131         bool has_value() const;
132         bool has_path() const;
133
134         const char *name() const;
135         const char *value() const;
136         const char *path() const;
137 };
138
139 /* req(uest) */
140 class req
141 {
142         struct afb_req req_;
143 public:
144         class stored
145         {
146                 struct afb_stored_req *sreq_;
147
148                 friend class req;
149                 stored() = delete;
150                 stored(struct afb_stored_req *sr);
151         public:
152                 stored(const stored &other);
153                 stored &operator =(const stored &other);
154                 req unstore() const;
155         };
156
157         class stored;
158
159 public:
160         req() = delete;
161         req(const struct afb_req &r);
162         req(const req &other);
163         req &operator=(const req &other);
164
165         operator const struct afb_req&() const;
166
167         operator bool() const;
168         bool is_valid() const;
169
170         arg get(const char *name) const;
171
172         const char *value(const char *name) const;
173
174         const char *path(const char *name) const;
175
176         json_object *json() const;
177
178         void success(json_object *obj = nullptr, const char *info = nullptr) const;
179         void successf(json_object *obj, const char *info, ...) const;
180
181         void fail(const char *status = "failed", const char *info = nullptr) const;
182         void failf(const char *status, const char *info, ...) const;
183
184         void *context_get() const;
185
186         void context_set(void *context, void (*free_context)(void*)) const;
187
188         void *context(void *(*create_context)(), void (*free_context)(void*)) const;
189
190         template < class T > T *context() const;
191
192         void context_clear() const;
193
194         void addref() const;
195
196         void unref() const;
197
198         void session_close() const;
199
200         bool session_set_LOA(unsigned level) const;
201
202         stored store() const;
203
204         bool subscribe(const event &event) const;
205
206         bool unsubscribe(const event &event) const;
207
208         void subcall(const char *api, const char *verb, json_object *args, void (*callback)(void *closure, int iserror, json_object *result), void *closure) const;
209
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;
211
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;
213
214         bool subcallsync(const char *api, const char *verb, json_object *args, struct json_object *&result) const;
215
216         void verbose(int level, const char *file, int line, const char * func, const char *fmt, va_list args) const;
217
218         void verbose(int level, const char *file, int line, const char * func, const char *fmt, ...) const;
219
220         bool has_permission(const char *permission) const;
221
222         char *get_application_id() const;
223
224         int get_uid() const;
225 };
226
227 /*************************************************************************/
228 /* effective declaration of classes                                      */
229 /*************************************************************************/
230 /////////////////////////////////////////////////////////////////////////////////////////////////////
231 /////////////////////////////////////////////////////////////////////////////////////////////////////
232 /////////////////////////////////////////////////////////////////////////////////////////////////////
233 /////////////////////////////////////////////////////////////////////////////////////////////////////
234 /////////////////////////////////////////////////////////////////////////////////////////////////////
235 /////////////////////////////////////////////////////////////////////////////////////////////////////
236 /////////////////////////////////////////////////////////////////////////////////////////////////////
237 /////////////////////////////////////////////////////////////////////////////////////////////////////
238 /////////////////////////////////////////////////////////////////////////////////////////////////////
239 /////////////////////////////////////////////////////////////////////////////////////////////////////
240 /////////////////////////////////////////////////////////////////////////////////////////////////////
241 /////////////////////////////////////////////////////////////////////////////////////////////////////
242 /////////////////////////////////////////////////////////////////////////////////////////////////////
243 /////////////////////////////////////////////////////////////////////////////////////////////////////
244 /////////////////////////////////////////////////////////////////////////////////////////////////////
245 /////////////////////////////////////////////////////////////////////////////////////////////////////
246
247
248 /*************************************************************************/
249 /* effective declaration of classes                                      */
250 /*************************************************************************/
251
252 /* events */
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; }
256
257 inline event::operator const struct afb_event&() const { return event_; }
258
259 inline event::operator bool() const { return is_valid(); }
260 inline bool event::is_valid() const { return afb_event_is_valid(event_); } 
261
262 inline void event::invalidate() { event_.itf = NULL; event_.closure = NULL; }
263
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); }
266
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_); }
270
271 /* args */
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; }
275
276 inline arg::operator const struct afb_arg&() const { return arg_; }
277
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; }
281
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; }
285
286 /* req(uests)s */
287
288 inline req::stored::stored(struct afb_stored_req *sr) : sreq_(sr) {}
289
290 inline req::stored::stored(const req::stored &other) : sreq_(other.sreq_) {}
291
292 inline req::stored &req::stored::operator =(const req::stored &other) { sreq_ = other.sreq_; return *this; }
293
294 inline req req::stored::unstore() const { return req(afb_daemon_unstore_req_v2(sreq_)); }
295
296
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; }
300
301 inline req::operator const struct afb_req&() const { return req_; }
302
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_); }
305
306 inline arg req::get(const char *name) const { return arg(afb_req_get(req_, name)); }
307
308 inline const char *req::value(const char *name) const { return afb_req_value(req_, name); }
309
310 inline const char *req::path(const char *name) const { return afb_req_path(req_, name); }
311
312 inline json_object *req::json() const { return afb_req_json(req_); }
313
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
316 {
317         va_list args;
318         va_start(args, info);
319         afb_req_success_v(req_, obj, info, args);
320         va_end(args);
321 }
322
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
325 {
326         va_list args;
327         va_start(args, info);
328         afb_req_fail_v(req_, status, info, args);
329         va_end(args);
330 }
331
332 inline void *req::context_get() const { return afb_req_context_get(req_); }
333
334 inline void req::context_set(void *context, void (*free_context)(void*)) const { afb_req_context_set(req_, context, free_context); }
335
336 inline void *req::context(void *(*create_context)(), void (*free_context)(void*)) const { return afb_req_context(req_, create_context, free_context); }
337
338 template < class T >
339 inline T *req::context() const
340 {
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)));
346 }
347
348 inline void req::context_clear() const { afb_req_context_clear(req_); }
349
350 inline void req::addref() const { afb_req_addref(req_); }
351
352 inline void req::unref() const { afb_req_unref(req_); }
353
354 inline void req::session_close() const { afb_req_session_close(req_); }
355
356 inline bool req::session_set_LOA(unsigned level) const { return !!afb_req_session_set_LOA(req_, level); }
357
358 inline req::stored req::store() const { return stored(afb_req_store_v2(req_)); }
359
360 inline bool req::subscribe(const event &event) const { return !afb_req_subscribe(req_, event); }
361
362 inline bool req::unsubscribe(const event &event) const { return !afb_req_unsubscribe(req_, event); }
363
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
365 {
366         afb_req_subcall(req_, api, verb, args, callback, closure);
367 }
368
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
370 {
371         afb_req_subcall_req(req_, api, verb, args, callback, closure);
372 }
373
374 template <class T>
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
376 {
377         afb_req_subcall(req_, api, verb, args, reinterpret_cast<void(*)(void*,int,json_object*)>(callback), reinterpret_cast<void*>(closure));
378 }
379
380 inline bool req::subcallsync(const char *api, const char *verb, json_object *args, struct json_object *&result) const
381 {
382         return !!afb_req_subcall_sync(req_, api, verb, args, &result);
383 }
384
385 inline void req::verbose(int level, const char *file, int line, const char * func, const char *fmt, va_list args) const
386 {
387         req_.itf->vverbose(req_.closure, level, file, line, func, fmt, args);
388 }
389
390 inline void req::verbose(int level, const char *file, int line, const char * func, const char *fmt, ...) const
391 {
392         va_list args;
393         va_start(args, fmt);
394         req_.itf->vverbose(req_.closure, level, file, line, func, fmt, args);
395         va_end(args);
396 }
397
398 inline bool req::has_permission(const char *permission) const
399 {
400         return bool(req_.itf->has_permission(req_.closure, permission));
401 }
402
403 inline char *req::get_application_id() const
404 {
405         return req_.itf->get_application_id(req_.closure);
406 }
407
408 inline int req::get_uid() const
409 {
410         return req_.itf->get_uid(req_.closure);
411 }
412
413 /* commons */
414 inline struct sd_event *get_event_loop()
415         { return afb_daemon_get_event_loop_v2(); }
416
417 inline struct sd_bus *get_system_bus()
418         { return afb_daemon_get_system_bus_v2(); }
419
420 inline struct sd_bus *get_user_bus()
421         { return afb_daemon_get_user_bus_v2(); }
422
423 inline int broadcast_event(const char *name, json_object *object)
424         { return afb_daemon_broadcast_event_v2(name, object); }
425
426 inline event make_event(const char *name)
427         { return afb_daemon_make_event_v2(name); }
428
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); }
431
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); }
434
435 inline int rootdir_get_fd()
436         { return afb_daemon_rootdir_get_fd_v2(); }
437
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); }
440
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); }
443
444 inline int require_api(const char *apiname, bool initialized)
445         { return afb_daemon_require_api_v2(apiname, int(initialized)); }
446
447 inline int rename_api(const char *apiname)
448         { return afb_daemon_rename_api_v2(apiname); }
449
450 inline int verbosity()
451         { return afb_get_verbosity(); }
452
453 inline bool wants_errors()
454         { return afb_verbose_error(); }
455
456 inline bool wants_warnings()
457         { return afb_verbose_warning(); }
458
459 inline bool wants_notices()
460         { return afb_verbose_notice(); }
461
462 inline bool wants_infos()
463         { return afb_verbose_info(); }
464
465 inline bool wants_debugs()
466         { return afb_verbose_debug(); }
467
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)
469 {
470         afb_service_call(api, verb, args, callback, closure);
471 }
472
473 template <class T>
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)
475 {
476         afb_service_call(api, verb, args, reinterpret_cast<void(*)(void*,int,json_object*)>(callback), reinterpret_cast<void*>(closure));
477 }
478
479 inline bool callsync(const char *api, const char *verb, struct json_object *args, struct json_object *&result)
480 {
481         return !!afb_service_call_sync(api, verb, args, &result);
482 }
483
484 /*************************************************************************/
485 /* declaration of the binding's authorization s                          */
486 /*************************************************************************/
487
488 constexpr afb_auth auth_no()
489 {
490         afb_auth r = { afb_auth_No, 0, 0};
491         r.type = afb_auth_No;
492         return r;
493 }
494
495 constexpr afb_auth auth_yes()
496 {
497         afb_auth r = { afb_auth_No, 0, 0};
498         r.type = afb_auth_Yes;
499         return r;
500 }
501
502 constexpr afb_auth auth_token()
503 {
504         afb_auth r = { afb_auth_No, 0, 0};
505         r.type = afb_auth_Token;
506         return r;
507 }
508
509 constexpr afb_auth auth_LOA(unsigned loa)
510 {
511         afb_auth r = { afb_auth_No, 0, 0};
512         r.type = afb_auth_LOA;
513         r.loa = loa;
514         return r;
515 }
516
517 constexpr afb_auth auth_permission(const char *permission)
518 {
519         afb_auth r = { afb_auth_No, 0, 0};
520         r.type = afb_auth_Permission;
521         r.text = permission;
522         return r;
523 }
524
525 constexpr afb_auth auth_not(const afb_auth *other)
526 {
527         afb_auth r = { afb_auth_No, 0, 0};
528         r.type = afb_auth_Not;
529         r.first = other;
530         return r;
531 }
532
533 constexpr afb_auth auth_not(const afb_auth &other)
534 {
535         return auth_not(&other);
536 }
537
538 constexpr afb_auth auth_or(const afb_auth *first, const afb_auth *next)
539 {
540         afb_auth r = { afb_auth_No, 0, 0};
541         r.type = afb_auth_Or;
542         r.first = first;
543         r.next = next;
544         return r;
545 }
546
547 constexpr afb_auth auth_or(const afb_auth &first, const afb_auth &next)
548 {
549         return auth_or(&first, &next);
550 }
551
552 constexpr afb_auth auth_and(const afb_auth *first, const afb_auth *next)
553 {
554         afb_auth r = { afb_auth_No, 0, 0};
555         r.type = afb_auth_And;
556         r.first = first;
557         r.next = next;
558         return r;
559 }
560
561 constexpr afb_auth auth_and(const afb_auth &first, const afb_auth &next)
562 {
563         return auth_and(&first, &next);
564 }
565
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)
567 {
568         afb_verb_v2 r = { 0, 0, 0, 0, 0 };
569         r.verb = name;
570         r.callback = callback;
571         r.info = info;
572         r.session = session;
573         r.auth = auth;
574         return r;
575 }
576
577 constexpr afb_verb_v2 verbend()
578 {
579         afb_verb_v2 r = { 0, 0, 0, 0, 0 };
580         r.verb = nullptr;
581         r.callback = nullptr;
582         r.info = nullptr;
583         r.session = 0;
584         r.auth = nullptr;
585         return r;
586 }
587
588 constexpr 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)
589 {
590         afb_binding_v2 r = { 0, 0, 0, 0, 0, 0, 0, 0 };
591         r.api = name;
592         r.specification = specification;
593         r.info = info;
594         r.verbs = verbs;
595         r.preinit = preinit;
596         r.init = init;
597         r.onevent = onevent;
598         r.noconcurrency = noconcurrency ? 1 : 0;
599         return r;
600 };
601
602 /*************************************************************************/
603 /***                         E N D                                     ***/
604 /*************************************************************************/
605 }