Prepare migration to binding v2
[src/app-framework-binder.git] / include / afb / afb-binding.h
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 <stdarg.h>
21
22 /*****************************************************************************
23  * This files is the main file to include for writing bindings dedicated to
24  *
25  *                      AFB-DAEMON
26  *
27  * Functions of bindings of afb-daemon are accessible by authorized clients
28  * through the apis module of afb-daemon.
29  *
30  * A binding is a shared library. This shared library must have at least one
31  * exported symbol for being registered in afb-daemon.
32  *
33  */
34
35 /*
36  * Some function of the library are exported to afb-daemon.
37  */
38
39 #include "afb-event-itf.h"
40 #include "afb-req-itf.h"
41 #include "afb-binding-v1.h"
42
43 /*
44  * Definition of the type+versions of the binding.
45  * The definition uses hashes.
46  */
47 enum  afb_binding_type
48 {
49        AFB_BINDING_VERSION_1 = 123456789        /* version 1 */
50 };
51
52 /*
53  * Description of a binding
54  */
55 struct afb_binding
56 {
57        enum afb_binding_type type; /* type of the binding */
58        union {
59                struct afb_binding_desc_v1 v1;   /* description of the binding of type 1 */
60        };
61 };
62
63 /*
64  * config mode
65  */
66 enum afb_mode {
67        AFB_MODE_LOCAL = 0,     /* run locally */
68        AFB_MODE_REMOTE,        /* run remotely */
69        AFB_MODE_GLOBAL         /* run either remotely or locally (DONT USE! reserved for future) */
70 };
71
72 /* declaration of features of libsystemd */
73 struct sd_event;
74 struct sd_bus;
75
76 /*
77  * Definition of the facilities provided by the daemon.
78  */
79 struct afb_daemon_itf {
80        int (*event_broadcast)(void *closure, const char *name, struct json_object *object); /* broadcasts evant 'name' with 'object' */
81        struct sd_event *(*get_event_loop)(void *closure);      /* gets the common systemd's event loop */
82        struct sd_bus *(*get_user_bus)(void *closure);          /* gets the common systemd's user d-bus */
83        struct sd_bus *(*get_system_bus)(void *closure);        /* gets the common systemd's system d-bus */
84        void (*vverbose)(void*closure, int level, const char *file, int line, const char *fmt, va_list args);
85        struct afb_event (*event_make)(void *closure, const char *name); /* creates an event of 'name' */
86        int (*rootdir_get_fd)(void *closure);
87        int (*rootdir_open_locale)(void *closure, const char *filename, int flags, const char *locale);
88 };
89
90 /*
91  * Structure for accessing daemon.
92  * See also: afb_daemon_get_event_sender, afb_daemon_get_event_loop, afb_daemon_get_user_bus, afb_daemon_get_system_bus
93  */
94 struct afb_daemon {
95        const struct afb_daemon_itf *itf;       /* the interfacing functions */
96        void *closure;                          /* the closure when calling these functions */
97 };
98
99 /*
100  * Interface between the daemon and the binding.
101  */
102 struct afb_binding_interface
103 {
104        struct afb_daemon daemon;       /* access to the daemon facilies */
105        int verbosity;                  /* level of verbosity */
106        enum afb_mode mode;             /* run mode (local or remote) */
107 };
108
109 /*
110  * Retrieves the common systemd's event loop of AFB
111  * 'daemon' MUST be the daemon given in interface when activating the binding.
112  */
113 static inline struct sd_event *afb_daemon_get_event_loop(struct afb_daemon daemon)
114 {
115         return daemon.itf->get_event_loop(daemon.closure);
116 }
117
118 /*
119  * Retrieves the common systemd's user/session d-bus of AFB
120  * 'daemon' MUST be the daemon given in interface when activating the binding.
121  */
122 static inline struct sd_bus *afb_daemon_get_user_bus(struct afb_daemon daemon)
123 {
124         return daemon.itf->get_user_bus(daemon.closure);
125 }
126
127 /*
128  * Retrieves the common systemd's system d-bus of AFB
129  * 'daemon' MUST be the daemon given in interface when activating the binding.
130  */
131 static inline struct sd_bus *afb_daemon_get_system_bus(struct afb_daemon daemon)
132 {
133         return daemon.itf->get_system_bus(daemon.closure);
134 }
135
136 /*
137  * Broadcasts widely the event of 'name' with the data 'object'.
138  * 'object' can be NULL.
139  * 'daemon' MUST be the daemon given in interface when activating the binding.
140  *
141  * For convenience, the function calls 'json_object_put' for 'object'.
142  * Thus, in the case where 'object' should remain available after
143  * the function returns, the function 'json_object_get' shall be used.
144  *
145  * Returns the count of clients that received the event.
146  */
147 static inline int afb_daemon_broadcast_event(struct afb_daemon daemon, const char *name, struct json_object *object)
148 {
149         return daemon.itf->event_broadcast(daemon.closure, name, object);
150 }
151
152 /*
153  * Creates an event of 'name' and returns it.
154  * 'daemon' MUST be the daemon given in interface when activating the binding.
155  */
156 static inline struct afb_event afb_daemon_make_event(struct afb_daemon daemon, const char *name)
157 {
158         return daemon.itf->event_make(daemon.closure, name);
159 }
160
161 /*
162  * Send a message described by 'fmt' and following parameters
163  * to the journal for the verbosity 'level'.
164  * 'file' and 'line' are indicators of position of the code in source files.
165  * 'daemon' MUST be the daemon given in interface when activating the binding.
166  */
167 static inline void afb_daemon_verbose(struct afb_daemon daemon, int level, const char *file, int line, const char *fmt, ...) __attribute__((format(printf, 5, 6)));
168 static inline void afb_daemon_verbose(struct afb_daemon daemon, int level, const char *file, int line, const char *fmt, ...)
169 {
170         va_list args;
171         va_start(args, fmt);
172         daemon.itf->vverbose(daemon.closure, level, file, line, fmt, args);
173         va_end(args);
174 }
175
176 /*
177  * Macros for logging messages
178  */
179 #if !defined(NO_BINDING_VERBOSE_MACRO)
180 # if !defined(NO_BINDING_FILE_LINE_INDICATION)
181 #  define ERROR(itf,...)   do{if(itf->verbosity>=0)afb_daemon_verbose(itf->daemon,3,__FILE__,__LINE__,__VA_ARGS__);}while(0)
182 #  define WARNING(itf,...) do{if(itf->verbosity>=1)afb_daemon_verbose(itf->daemon,4,__FILE__,__LINE__,__VA_ARGS__);}while(0)
183 #  define NOTICE(itf,...)  do{if(itf->verbosity>=1)afb_daemon_verbose(itf->daemon,5,__FILE__,__LINE__,__VA_ARGS__);}while(0)
184 #  define INFO(itf,...)    do{if(itf->verbosity>=2)afb_daemon_verbose(itf->daemon,6,__FILE__,__LINE__,__VA_ARGS__);}while(0)
185 #  define DEBUG(itf,...)   do{if(itf->verbosity>=3)afb_daemon_verbose(itf->daemon,7,__FILE__,__LINE__,__VA_ARGS__);}while(0)
186 # else
187 #  define ERROR(itf,...)   do{if(itf->verbosity>=0)afb_daemon_verbose(itf->daemon,3,NULL,0,__VA_ARGS__);}while(0)
188 #  define WARNING(itf,...) do{if(itf->verbosity>=1)afb_daemon_verbose(itf->daemon,4,NULL,0,__VA_ARGS__);}while(0)
189 #  define NOTICE(itf,...)  do{if(itf->verbosity>=1)afb_daemon_verbose(itf->daemon,5,NULL,0,__VA_ARGS__);}while(0)
190 #  define INFO(itf,...)    do{if(itf->verbosity>=2)afb_daemon_verbose(itf->daemon,6,NULL,0,__VA_ARGS__);}while(0)
191 #  define DEBUG(itf,...)   do{if(itf->verbosity>=3)afb_daemon_verbose(itf->daemon,7,NULL,0,__VA_ARGS__);}while(0)
192 # endif
193 #endif
194
195 /*
196  * Get the root directory file descriptor. This file descriptor can
197  * be used with functions 'openat', 'fstatat', ...
198  */
199 static inline int afb_daemon_rootdir_get_fd(struct afb_daemon daemon)
200 {
201         return daemon.itf->rootdir_get_fd(daemon.closure);
202 }
203
204 /*
205  * Opens 'filename' within the root directory with 'flags' (see function openat)
206  * using the 'locale' definition (example: "jp,en-US") that can be NULL.
207  * Returns the file descriptor or -1 in case of error.
208  */
209 static inline int afb_daemon_rootdir_open_locale(struct afb_daemon daemon, const char *filename, int flags, const char *locale)
210 {
211         return daemon.itf->rootdir_open_locale(daemon.closure, filename, flags, locale);
212 }
213
214