Improve handling of verbosity in bindings
[src/app-framework-binder.git] / include / afb / afb-binding-v1.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 struct json_object;
21
22 #include "afb-req-common.h"
23 #include "afb-event-itf.h"
24 #include "afb-service-common.h"
25 #include "afb-daemon-common.h"
26
27 #include "afb-req-v1.h"
28 #include "afb-session-v1.h"
29 #include "afb-service-v1.h"
30 #include "afb-daemon-v1.h"
31
32 struct afb_binding_v1;
33 struct afb_binding_interface_v1;
34
35 /*
36  * Function for registering the binding
37  *
38  * A binding V1 MUST have an exported function of name
39  *
40  *              afbBindingV1Register
41  *
42  * This function is called during loading of the binding. It
43  * receives an 'interface' that should be recorded for later access to
44  * functions provided by the framework.
45  *
46  * This function MUST return the address of a structure that describes
47  * the binding and its implemented verbs.
48  *
49  * In case of initialisation error, NULL must be returned.
50  *
51  * Be aware that the given 'interface' is not fully functionnal
52  * because no provision is given to the name and description
53  * of the binding. Check the function 'afbBindingV1ServiceInit'
54  * defined in the file <afb/afb-service-v1.h> because when
55  * the function 'afbBindingV1ServiceInit' is called, the 'interface'
56  * is fully functionnal.
57  */
58 extern const struct afb_binding_v1 *afbBindingV1Register (const struct afb_binding_interface_v1 *interface);
59
60 /*
61  * When a binding have an exported implementation of the
62  * function 'afbBindingV1ServiceInit', defined below,
63  * the framework calls it for initialising the service after
64  * registration of all bindings.
65  *
66  * The object 'service' should be recorded. It has functions that
67  * allows the binding to call features with its own personality.
68  *
69  * The function should return 0 in case of success or, else, should return
70  * a negative value.
71  */
72 extern int afbBindingV1ServiceInit(struct afb_service service);
73
74 /*
75  * When a binding have an implementation of the function 'afbBindingV1ServiceEvent',
76  * defined below, the framework calls that function for any broadcasted event or for
77  * events that the service subscribed to in its name.
78  *
79  * It receive the 'event' name and its related data in 'object' (be aware that 'object'
80  * might be NULL).
81  */
82 extern void afbBindingV1ServiceEvent(const char *event, struct json_object *object);
83
84
85 /*
86  * Description of one verb of the API provided by the binding
87  * This enumeration is valid for bindings of type version 1
88  */
89 struct afb_verb_desc_v1
90 {
91        const char *name;                       /* name of the verb */
92        enum afb_session_flags_v1 session;      /* authorisation and session requirements of the verb */
93        void (*callback)(struct afb_req req);   /* callback function implementing the verb */
94        const char *info;                       /* textual description of the verb */
95 };
96
97 /*
98  * Description of the bindings of type version 1
99  */
100 struct afb_binding_desc_v1
101 {
102        const char *info;                       /* textual information about the binding */
103        const char *prefix;                     /* required prefix name for the binding */
104        const struct afb_verb_desc_v1 *verbs;   /* array of descriptions of verbs terminated by a NULL name */
105 };
106
107 /*
108  * Definition of the type+versions of the binding.
109  * The definition uses hashes.
110  */
111 enum  afb_binding_type_v1
112 {
113        AFB_BINDING_VERSION_1 = 123456789
114 };
115
116 /*
117  * Description of a binding
118  */
119 struct afb_binding_v1
120 {
121        enum afb_binding_type_v1 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_v1
131 {
132         AFB_MODE_LOCAL = 0,     /* run locally */
133         AFB_MODE_REMOTE,        /* run remotely */
134         AFB_MODE_GLOBAL         /* run either remotely or locally (DONT USE! reserved for future) */
135 };
136
137 /*
138  * Interface between the daemon and the binding.
139  */
140 struct afb_binding_interface_v1
141 {
142         struct afb_daemon daemon;       /* access to the daemon facilies */
143         int verbosity;                  /* level of verbosity */
144         enum afb_mode_v1 mode;          /* run mode (local or remote) */
145 };
146
147 /*
148  * Macros for logging messages
149  */
150 #if defined(AFB_BINDING_PRAGMA_NO_VERBOSE_DATA)
151
152 # define _AFB_LOGGING_V1_(itf,vlevel,llevel,...) \
153         do{ \
154                 if(itf->verbosity>=vlevel) {\
155                         if (llevel <= AFB_VERBOSITY_LEVEL_ERROR) \
156                                 afb_daemon_verbose2_v1(itf->daemon,llevel,__FILE__,__LINE__,__func__,__VA_ARGS__); \
157                         else \
158                                 afb_daemon_verbose2_v1(itf->daemon,llevel,__FILE__,__LINE__,NULL,NULL); \
159                 } \
160         }while(0)
161 # define _AFB_REQ_LOGGING_V1_(itf,vlevel,llevel,req,...) \
162         do{ \
163                 if(itf->verbosity>=vlevel) \
164                         afb_req_verbose(req,llevel,__FILE__,__LINE__,NULL,NULL); \
165         }while(0)
166
167 #elif defined(AFB_BINDING_PRAGMA_NO_VERBOSE_DETAILS)
168
169 # define _AFB_LOGGING_V1_(itf,vlevel,llevel,...) \
170         do{ \
171                 if(itf->verbosity>=vlevel) \
172                         afb_daemon_verbose2_v1(itf->daemon,llevel,NULL,0,NULL,__VA_ARGS__); \
173         }while(0)
174 # define _AFB_REQ_LOGGING_V1_(itf,vlevel,llevel,req,...) \
175         do{ \
176                 if(itf->verbosity>=vlevel) \
177                         afb_req_verbose(req,llevel,NULL,0,NULL,__VA_ARGS__); \
178         }while(0)
179
180 #else
181
182 # define _AFB_LOGGING_V1_(itf,vlevel,llevel,...) \
183         do{ \
184                 if(itf->verbosity>=vlevel) \
185                         afb_daemon_verbose2_v1(itf->daemon,llevel,__FILE__,__LINE__,__func__,__VA_ARGS__); \
186         }while(0)
187 # define _AFB_REQ_LOGGING_V1_(itf,vlevel,llevel,req,...) \
188         do{ \
189                 if(itf->verbosity>=vlevel) \
190                         afb_req_verbose(req,llevel,__FILE__,__LINE__,__func__,__VA_ARGS__); \
191         }while(0)
192
193 #endif
194
195 # include "afb-verbosity.h"
196 # define AFB_ERROR_V1(itf,...)       _AFB_LOGGING_V1_(itf,AFB_VERBOSITY_LEVEL_ERROR,_AFB_SYSLOG_LEVEL_ERROR_,__VA_ARGS__)
197 # define AFB_WARNING_V1(itf,...)     _AFB_LOGGING_V1_(itf,AFB_VERBOSITY_LEVEL_WARNING,_AFB_SYSLOG_LEVEL_WARNING_,__VA_ARGS__)
198 # define AFB_NOTICE_V1(itf,...)      _AFB_LOGGING_V1_(itf,AFB_VERBOSITY_LEVEL_NOTICE,_AFB_SYSLOG_LEVEL_NOTICE_,__VA_ARGS__)
199 # define AFB_INFO_V1(itf,...)        _AFB_LOGGING_V1_(itf,AFB_VERBOSITY_LEVEL_INFO,_AFB_SYSLOG_LEVEL_INFO_,__VA_ARGS__)
200 # define AFB_DEBUG_V1(itf,...)       _AFB_LOGGING_V1_(itf,AFB_VERBOSITY_LEVEL_DEBUG,_AFB_SYSLOG_LEVEL_DEBUG_,__VA_ARGS__)
201 # define AFB_REQ_ERROR_V1(itf,...)   _AFB_REQ_LOGGING_V1_(itf,AFB_VERBOSITY_LEVEL_ERROR,_AFB_SYSLOG_LEVEL_ERROR_,__VA_ARGS__)
202 # define AFB_REQ_WARNING_V1(itf,...) _AFB_REQ_LOGGING_V1_(itf,AFB_VERBOSITY_LEVEL_WARNING,_AFB_SYSLOG_LEVEL_WARNING_,__VA_ARGS__)
203 # define AFB_REQ_NOTICE_V1(itf,...)  _AFB_REQ_LOGGING_V1_(itf,AFB_VERBOSITY_LEVEL_NOTICE,_AFB_SYSLOG_LEVEL_NOTICE_,__VA_ARGS__)
204 # define AFB_REQ_INFO_V1(itf,...)    _AFB_REQ_LOGGING_V1_(itf,AFB_VERBOSITY_LEVEL_INFO,_AFB_SYSLOG_LEVEL_INFO_,__VA_ARGS__)
205 # define AFB_REQ_DEBUG_V1(itf,...)   _AFB_REQ_LOGGING_V1_(itf,AFB_VERBOSITY_LEVEL_DEBUG,_AFB_SYSLOG_LEVEL_DEBUG_,__VA_ARGS__)
206