add log macros for plugins
[src/app-framework-binder.git] / include / afb / afb-plugin.h
1 /*
2  * Copyright (C) 2016 "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 plugins dedicated to
24  *
25  *                      AFB-DAEMON
26  *
27  * Functions of plugins of afb-daemon are accessible by authorized clients
28  * through the apis module of afb-daemon.
29  *
30  * A plugin 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  *                  pluginAfbV1Register
35  */
36
37 /*
38  * Some function of the library are exported to afb-daemon.
39  */
40
41 #include <afb/afb-req-itf.h>
42 #include <afb/afb-event-sender-itf.h>
43
44 /*
45  * Definition of the versions of the plugin.
46  * The definition uses hashes.
47  */
48 enum  AFB_plugin_version
49 {
50        AFB_PLUGIN_VERSION_1 = 123456789        /* version 1 */
51 };
52
53 /*
54  * Enum for Session/Token/Authentication middleware.
55  * This enumeration is valid for plugins of type 1
56  */
57 enum AFB_session_v1
58 {
59        AFB_SESSION_NONE = 0,   /* no session and no authentification required */
60        AFB_SESSION_CREATE = 1, /* requires authentification and first call of the session */
61        AFB_SESSION_CLOSE = 2,  /* closes the session after authentification */
62        AFB_SESSION_RENEW = 4,  /* refreshes the token after authentification */
63        AFB_SESSION_CHECK = 8,  /* enforce 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 = 3,  /* 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
77        AFB_SESSION_LOA_LE_0 = AFB_SESSION_LOA_LE | AFB_SESSION_LOA_0, /* check LOA <= 0 */
78        AFB_SESSION_LOA_LE_1 = AFB_SESSION_LOA_LE | AFB_SESSION_LOA_1, /* check LOA <= 1 */
79        AFB_SESSION_LOA_LE_2 = AFB_SESSION_LOA_LE | AFB_SESSION_LOA_2, /* check LOA <= 2 */
80        AFB_SESSION_LOA_LE_3 = AFB_SESSION_LOA_LE | AFB_SESSION_LOA_3, /* check LOA <= 3 */
81
82        AFB_SESSION_LOA_GE_0 = AFB_SESSION_LOA_GE | AFB_SESSION_LOA_0, /* check LOA >= 0 */
83        AFB_SESSION_LOA_GE_1 = AFB_SESSION_LOA_GE | AFB_SESSION_LOA_1, /* check LOA >= 1 */
84        AFB_SESSION_LOA_GE_2 = AFB_SESSION_LOA_GE | AFB_SESSION_LOA_2, /* check LOA >= 2 */
85        AFB_SESSION_LOA_GE_3 = AFB_SESSION_LOA_GE | AFB_SESSION_LOA_3, /* check LOA >= 3 */
86
87        AFB_SESSION_LOA_EQ_0 = AFB_SESSION_LOA_EQ | AFB_SESSION_LOA_0, /* check LOA == 0 */
88        AFB_SESSION_LOA_EQ_1 = AFB_SESSION_LOA_EQ | AFB_SESSION_LOA_1, /* check LOA == 1 */
89        AFB_SESSION_LOA_EQ_2 = AFB_SESSION_LOA_EQ | AFB_SESSION_LOA_2, /* check LOA == 2 */
90        AFB_SESSION_LOA_EQ_3 = AFB_SESSION_LOA_EQ | AFB_SESSION_LOA_3  /* check LOA == 3 */
91 };
92
93 /*
94  * Description of one verb of the API provided by the plugin
95  * This enumeration is valid for plugins of type 1
96  */
97 struct AFB_verb_desc_v1
98 {
99        const char *name;                       /* name of the verb */
100        enum AFB_session_v1 session;            /* authorisation and session requirements of the verb */
101        void (*callback)(struct afb_req req);   /* callback function implementing the verb */
102        const char *info;                       /* textual description of the verb */
103 };
104
105 /*
106  * Description of the plugins of type 1
107  */
108 struct AFB_plugin_desc_v1
109 {
110        const char *info;                       /* textual information about the plugin */
111        const char *prefix;                     /* required prefix name for the plugin */
112        const struct AFB_verb_desc_v1 *verbs;   /* array of descriptions of verbs terminated by a NULL name */
113 };
114
115 /*
116  * Description of a plugin
117  */
118 struct AFB_plugin
119 {
120        enum AFB_plugin_version type; /* type of the plugin */
121        union {
122                struct AFB_plugin_desc_v1 v1;   /* description of the plugin of type 1 */
123        };
124 };
125
126 /*
127  * config mode
128  */
129 enum AFB_Mode {
130        AFB_MODE_LOCAL = 0,     /* run locally */
131        AFB_MODE_REMOTE,        /* run remotely */
132        AFB_MODE_GLOBAL         /* run either remotely or locally (DONT USE! reserved for future) */
133 };
134
135 /* declaration of features of libsystemd */
136 struct sd_event;
137 struct sd_bus;
138
139 /*
140  * Definition of the facilities provided by the daemon.
141  */
142 struct afb_daemon_itf {
143        struct afb_event_sender (*get_event_sender)(void *closure);           /* get the event manager */
144        struct sd_event *(*get_event_loop)(void *closure);      /* get the common systemd's event loop */
145        struct sd_bus *(*get_user_bus)(void *closure);          /* get the common systemd's user d-bus */
146        struct sd_bus *(*get_system_bus)(void *closure);        /* get the common systemd's system d-bus */
147        void (*vverbose)(void*closure, int level, const char *file, int line, const char *fmt, va_list args);
148 };
149
150 /*
151  * Structure for accessing daemon.
152  * See also: afb_daemon_get_event_sender, afb_daemon_get_event_loop, afb_daemon_get_user_bus, afb_daemon_get_system_bus
153  */
154 struct afb_daemon {
155        const struct afb_daemon_itf *itf;       /* the interfacing functions */
156        void *closure;                          /* the closure when calling these functions */
157 };
158
159 /*
160  * Interface between the daemon and the plugin.
161  */
162 struct AFB_interface
163 {
164        struct afb_daemon daemon;       /* access to the daemon facilies */
165        int verbosity;                  /* level of verbosity */
166        enum AFB_Mode mode;             /* run mode (local or remote) */
167 };
168
169 /*
170  * Function for registering the plugin
171  */
172 extern const struct AFB_plugin *pluginAfbV1Register (const struct AFB_interface *interface);
173
174 /*
175  * Retrieves the event sender of AFB
176  * 'daemon' MUST be the daemon given in interface when activating the plugin.
177  */
178 static inline struct afb_event_sender afb_daemon_get_event_sender(struct afb_daemon daemon)
179 {
180         return daemon.itf->get_event_sender(daemon.closure);
181 }
182
183 /*
184  * Retrieves the common systemd's event loop of AFB
185  * 'daemon' MUST be the daemon given in interface when activating the plugin.
186  */
187 static inline struct sd_event *afb_daemon_get_event_loop(struct afb_daemon daemon)
188 {
189         return daemon.itf->get_event_loop(daemon.closure);
190 }
191
192 /*
193  * Retrieves the common systemd's user/session d-bus of AFB
194  * 'daemon' MUST be the daemon given in interface when activating the plugin.
195  */
196 static inline struct sd_bus *afb_daemon_get_user_bus(struct afb_daemon daemon)
197 {
198         return daemon.itf->get_user_bus(daemon.closure);
199 }
200
201 /*
202  * Retrieves the common systemd's system d-bus of AFB
203  * 'daemon' MUST be the daemon given in interface when activating the plugin.
204  */
205 static inline struct sd_bus *afb_daemon_get_system_bus(struct afb_daemon daemon)
206 {
207         return daemon.itf->get_system_bus(daemon.closure);
208 }
209
210 /*
211  * Send a message described by 'fmt' and following parameters
212  * to the journal for the verbosity 'level'.
213  * 'file' and 'line' are indicators of position of the code in source files.
214  * 'daemon' MUST be the daemon given in interface when activating the plugin.
215  */
216 static inline void afb_daemon_verbose(struct afb_daemon daemon, int level, const char *file, int line, const char *fmt, ...)
217 {
218         va_list args;
219         va_start(args, fmt);
220         return daemon.itf->vverbose(daemon.closure, level, file, line, fmt, args);
221         va_end(args);
222 }
223
224 #if !defined(NO_PLUGIN_VERBOSE_MACRO)
225 # define ERROR(itf,...)   do{if(itf->verbosity>=0)afb_daemon_verbose(itf->daemon,3,__FILE__,__LINE__,__VA_ARGS__);}while(0)
226 # define WARNING(itf,...) do{if(itf->verbosity>=1)afb_daemon_verbose(itf->daemon,4,__FILE__,__LINE__,__VA_ARGS__);}while(0)
227 # define NOTICE(itf,...)  do{if(itf->verbosity>=1)afb_daemon_verbose(itf->daemon,5,__FILE__,__LINE__,__VA_ARGS__);}while(0)
228 # define INFO(itf,...)    do{if(itf->verbosity>=2)afb_daemon_verbose(itf->daemon,6,__FILE__,__LINE__,__VA_ARGS__);}while(0)
229 # define DEBUG(itf,...)   do{if(itf->verbosity>=3)afb_daemon_verbose(itf->daemon,7,__FILE__,__LINE__,__VA_ARGS__);}while(0)
230 #endif