Adds 2017 to copyrights
[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  * For the current version of afb-daemon, the function exported MUST be named
33  *
34  *                  afbBindingV1Register
35  */
36
37 /*
38  * Some function of the library are exported to afb-daemon.
39  */
40
41 #include <afb/afb-event-itf.h>
42 #include <afb/afb-req-itf.h>
43
44 /*
45  * Definition of the type+versions of the binding.
46  * The definition uses hashes.
47  */
48 enum  afb_binding_type
49 {
50        AFB_BINDING_VERSION_1 = 123456789        /* version 1 */
51 };
52
53 /*
54  * Enum for Session/Token/Assurance middleware.
55  * This enumeration is valid for bindings of type 1
56  */
57 enum afb_session_v1
58 {
59        AFB_SESSION_NONE = 0,   /* nothing required */
60        AFB_SESSION_CREATE = 1, /* Obsolete */
61        AFB_SESSION_CLOSE = 2,  /* After token authentification, closes the session at end */
62        AFB_SESSION_RENEW = 4,  /* After token authentification, refreshes the token at end */
63        AFB_SESSION_CHECK = 8,  /* Requires token authentification */
64
65        AFB_SESSION_LOA_GE = 16, /* check that the LOA is greater or equal to the given value */
66        AFB_SESSION_LOA_LE = 32, /* check that the LOA is lesser or equal to the given value */
67        AFB_SESSION_LOA_EQ = 48, /* check that the LOA is equal to the given value */
68
69        AFB_SESSION_LOA_SHIFT = 6, /* shift for LOA */
70        AFB_SESSION_LOA_MASK = 7,  /* mask for LOA */
71
72        AFB_SESSION_LOA_0 = 0,   /* value for LOA of 0 */
73        AFB_SESSION_LOA_1 = 64,  /* value for LOA of 1 */
74        AFB_SESSION_LOA_2 = 128, /* value for LOA of 2 */
75        AFB_SESSION_LOA_3 = 192, /* value for LOA of 3 */
76        AFB_SESSION_LOA_4 = 256, /* value for LOA of 4 */
77
78        AFB_SESSION_LOA_LE_0 = AFB_SESSION_LOA_LE | AFB_SESSION_LOA_0, /* check LOA <= 0 */
79        AFB_SESSION_LOA_LE_1 = AFB_SESSION_LOA_LE | AFB_SESSION_LOA_1, /* check LOA <= 1 */
80        AFB_SESSION_LOA_LE_2 = AFB_SESSION_LOA_LE | AFB_SESSION_LOA_2, /* check LOA <= 2 */
81        AFB_SESSION_LOA_LE_3 = AFB_SESSION_LOA_LE | AFB_SESSION_LOA_3, /* check LOA <= 3 */
82
83        AFB_SESSION_LOA_GE_0 = AFB_SESSION_LOA_GE | AFB_SESSION_LOA_0, /* check LOA >= 0 */
84        AFB_SESSION_LOA_GE_1 = AFB_SESSION_LOA_GE | AFB_SESSION_LOA_1, /* check LOA >= 1 */
85        AFB_SESSION_LOA_GE_2 = AFB_SESSION_LOA_GE | AFB_SESSION_LOA_2, /* check LOA >= 2 */
86        AFB_SESSION_LOA_GE_3 = AFB_SESSION_LOA_GE | AFB_SESSION_LOA_3, /* check LOA >= 3 */
87
88        AFB_SESSION_LOA_EQ_0 = AFB_SESSION_LOA_EQ | AFB_SESSION_LOA_0, /* check LOA == 0 */
89        AFB_SESSION_LOA_EQ_1 = AFB_SESSION_LOA_EQ | AFB_SESSION_LOA_1, /* check LOA == 1 */
90        AFB_SESSION_LOA_EQ_2 = AFB_SESSION_LOA_EQ | AFB_SESSION_LOA_2, /* check LOA == 2 */
91        AFB_SESSION_LOA_EQ_3 = AFB_SESSION_LOA_EQ | AFB_SESSION_LOA_3  /* check LOA == 3 */
92 };
93
94 /*
95  * Description of one verb of the API provided by the binding
96  * This enumeration is valid for bindings of type 1
97  */
98 struct afb_verb_desc_v1
99 {
100        const char *name;                       /* name of the verb */
101        enum afb_session_v1 session;            /* authorisation and session requirements of the verb */
102        void (*callback)(struct afb_req req);   /* callback function implementing the verb */
103        const char *info;                       /* textual description of the verb */
104 };
105
106 /*
107  * Description of the bindings of type 1
108  */
109 struct afb_binding_desc_v1
110 {
111        const char *info;                       /* textual information about the binding */
112        const char *prefix;                     /* required prefix name for the binding */
113        const struct afb_verb_desc_v1 *verbs;   /* array of descriptions of verbs terminated by a NULL name */
114 };
115
116 /*
117  * Description of a binding
118  */
119 struct afb_binding
120 {
121        enum afb_binding_type type; /* type of the binding */
122        union {
123                struct afb_binding_desc_v1 v1;   /* description of the binding of type 1 */
124        };
125 };
126
127 /*
128  * config mode
129  */
130 enum afb_mode {
131        AFB_MODE_LOCAL = 0,     /* run locally */
132        AFB_MODE_REMOTE,        /* run remotely */
133        AFB_MODE_GLOBAL         /* run either remotely or locally (DONT USE! reserved for future) */
134 };
135
136 /* declaration of features of libsystemd */
137 struct sd_event;
138 struct sd_bus;
139
140 /*
141  * Definition of the facilities provided by the daemon.
142  */
143 struct afb_daemon_itf {
144        int (*event_broadcast)(void *closure, const char *name, struct json_object *object); /* broadcasts evant 'name' with 'object' */
145        struct sd_event *(*get_event_loop)(void *closure);      /* gets the common systemd's event loop */
146        struct sd_bus *(*get_user_bus)(void *closure);          /* gets the common systemd's user d-bus */
147        struct sd_bus *(*get_system_bus)(void *closure);        /* gets the common systemd's system d-bus */
148        void (*vverbose)(void*closure, int level, const char *file, int line, const char *fmt, va_list args);
149        struct afb_event (*event_make)(void *closure, const char *name); /* creates an event of 'name' */
150        int (*rootdir_get_fd)(void *closure);
151        int (*rootdir_open_locale)(void *closure, const char *filename, int flags, const char *locale);
152 };
153
154 /*
155  * Structure for accessing daemon.
156  * See also: afb_daemon_get_event_sender, afb_daemon_get_event_loop, afb_daemon_get_user_bus, afb_daemon_get_system_bus
157  */
158 struct afb_daemon {
159        const struct afb_daemon_itf *itf;       /* the interfacing functions */
160        void *closure;                          /* the closure when calling these functions */
161 };
162
163 /*
164  * Interface between the daemon and the binding.
165  */
166 struct afb_binding_interface
167 {
168        struct afb_daemon daemon;       /* access to the daemon facilies */
169        int verbosity;                  /* level of verbosity */
170        enum afb_mode mode;             /* run mode (local or remote) */
171 };
172
173 /*
174  * Function for registering the binding
175  *
176  * A binding V1 MUST have a function of this name and signature.
177  * This function is called during loading of the binding. It
178  * receives an 'interface' that should be recorded for later access to
179  * functions provided by the framework.
180  *
181  * This function MUST return the address of a structure that describes
182  * the binding and its implemented verbs.
183  *
184  * In case of initialisation error, NULL must be returned.
185  *
186  * Be aware that the given 'interface' is not fully functionnal
187  * because no provision is given to the name and description
188  * of the binding. Check the function 'afbBindingV1ServiceInit'
189  * defined in the file <afb/afb-service-itf.h> because when
190  * the function 'afbBindingV1ServiceInit' is called, the 'interface'
191  * is fully functionnal.
192  */
193 extern const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *interface);
194
195 /*
196  * Retrieves the common systemd's event loop of AFB
197  * 'daemon' MUST be the daemon given in interface when activating the binding.
198  */
199 static inline struct sd_event *afb_daemon_get_event_loop(struct afb_daemon daemon)
200 {
201         return daemon.itf->get_event_loop(daemon.closure);
202 }
203
204 /*
205  * Retrieves the common systemd's user/session d-bus of AFB
206  * 'daemon' MUST be the daemon given in interface when activating the binding.
207  */
208 static inline struct sd_bus *afb_daemon_get_user_bus(struct afb_daemon daemon)
209 {
210         return daemon.itf->get_user_bus(daemon.closure);
211 }
212
213 /*
214  * Retrieves the common systemd's system d-bus of AFB
215  * 'daemon' MUST be the daemon given in interface when activating the binding.
216  */
217 static inline struct sd_bus *afb_daemon_get_system_bus(struct afb_daemon daemon)
218 {
219         return daemon.itf->get_system_bus(daemon.closure);
220 }
221
222 /*
223  * Broadcasts widely the event of 'name' with the data 'object'.
224  * 'object' can be NULL.
225  * 'daemon' MUST be the daemon given in interface when activating the binding.
226  *
227  * For convenience, the function calls 'json_object_put' for 'object'.
228  * Thus, in the case where 'object' should remain available after
229  * the function returns, the function 'json_object_get' shall be used.
230  *
231  * Returns the count of clients that received the event.
232  */
233 static inline int afb_daemon_broadcast_event(struct afb_daemon daemon, const char *name, struct json_object *object)
234 {
235         return daemon.itf->event_broadcast(daemon.closure, name, object);
236 }
237
238 /*
239  * Creates an event of 'name' and returns it.
240  * 'daemon' MUST be the daemon given in interface when activating the binding.
241  */
242 static inline struct afb_event afb_daemon_make_event(struct afb_daemon daemon, const char *name)
243 {
244         return daemon.itf->event_make(daemon.closure, name);
245 }
246
247 /*
248  * Send a message described by 'fmt' and following parameters
249  * to the journal for the verbosity 'level'.
250  * 'file' and 'line' are indicators of position of the code in source files.
251  * 'daemon' MUST be the daemon given in interface when activating the binding.
252  */
253 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)));
254 static inline void afb_daemon_verbose(struct afb_daemon daemon, int level, const char *file, int line, const char *fmt, ...)
255 {
256         va_list args;
257         va_start(args, fmt);
258         daemon.itf->vverbose(daemon.closure, level, file, line, fmt, args);
259         va_end(args);
260 }
261
262 /*
263  * Macros for logging messages
264  */
265 #if !defined(NO_BINDING_VERBOSE_MACRO)
266 # if !defined(NO_BINDING_FILE_LINE_INDICATION)
267 #  define ERROR(itf,...)   do{if(itf->verbosity>=0)afb_daemon_verbose(itf->daemon,3,__FILE__,__LINE__,__VA_ARGS__);}while(0)
268 #  define WARNING(itf,...) do{if(itf->verbosity>=1)afb_daemon_verbose(itf->daemon,4,__FILE__,__LINE__,__VA_ARGS__);}while(0)
269 #  define NOTICE(itf,...)  do{if(itf->verbosity>=1)afb_daemon_verbose(itf->daemon,5,__FILE__,__LINE__,__VA_ARGS__);}while(0)
270 #  define INFO(itf,...)    do{if(itf->verbosity>=2)afb_daemon_verbose(itf->daemon,6,__FILE__,__LINE__,__VA_ARGS__);}while(0)
271 #  define DEBUG(itf,...)   do{if(itf->verbosity>=3)afb_daemon_verbose(itf->daemon,7,__FILE__,__LINE__,__VA_ARGS__);}while(0)
272 # else
273 #  define ERROR(itf,...)   do{if(itf->verbosity>=0)afb_daemon_verbose(itf->daemon,3,NULL,0,__VA_ARGS__);}while(0)
274 #  define WARNING(itf,...) do{if(itf->verbosity>=1)afb_daemon_verbose(itf->daemon,4,NULL,0,__VA_ARGS__);}while(0)
275 #  define NOTICE(itf,...)  do{if(itf->verbosity>=1)afb_daemon_verbose(itf->daemon,5,NULL,0,__VA_ARGS__);}while(0)
276 #  define INFO(itf,...)    do{if(itf->verbosity>=2)afb_daemon_verbose(itf->daemon,6,NULL,0,__VA_ARGS__);}while(0)
277 #  define DEBUG(itf,...)   do{if(itf->verbosity>=3)afb_daemon_verbose(itf->daemon,7,NULL,0,__VA_ARGS__);}while(0)
278 # endif
279 #endif
280
281 /*
282  * Get the root directory file descriptor. This file descriptor can
283  * be used with functions 'openat', 'fstatat', ...
284  */
285 static inline int afb_daemon_rootdir_get_fd(struct afb_daemon daemon)
286 {
287         return daemon.itf->rootdir_get_fd(daemon.closure);
288 }
289
290 /*
291  * Opens 'filename' within the root directory with 'flags' (see function openat)
292  * using the 'locale' definition (example: "jp,en-US") that can be NULL.
293  * Returns the file descriptor or -1 in case of error.
294  */
295 static inline int afb_daemon_rootdir_open_locale(struct afb_daemon daemon, const char *filename, int flags, const char *locale)
296 {
297         return daemon.itf->rootdir_open_locale(daemon.closure, filename, flags, locale);
298 }
299
300