Update copyright dates
[src/app-framework-binder.git] / include / afb / afb-binding-v2.h
1 /*
2  * Copyright (C) 2015-2020 "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 /******************************************************************************/
21
22 #include "afb-verbosity.h"
23 #include "afb-auth.h"
24 #include "afb-event-x1.h"
25 #include "afb-req-x1.h"
26 #include "afb-service-itf-x1.h"
27 #include "afb-daemon-itf-x1.h"
28
29 #include "afb-req-v2.h"
30 #include "afb-session-x2.h"
31
32 /******************************************************************************/
33
34 /**
35  * @deprecated use bindings version 3
36  *
37  * Description of one verb as provided for binding API version 2
38  */
39 struct afb_verb_v2
40 {
41         const char *verb;                       /**< name of the verb, NULL only at end of the array */
42         void (*callback)(struct afb_req_x1 req);/**< callback function implementing the verb */
43         const struct afb_auth *auth;            /**< required authorisation, can be NULL */
44         const char *info;                       /**< some info about the verb, can be NULL */
45         uint32_t session;                       /**< authorisation and session requirements of the verb */
46 };
47
48 /**
49  * @deprecated use bindings version 3
50  *
51  * Description of the bindings of type version 2
52  */
53 struct afb_binding_v2
54 {
55         const char *api;                        /**< api name for the binding */
56         const char *specification;              /**< textual specification of the binding, can be NULL */
57         const char *info;                       /**< some info about the api, can be NULL */
58         const struct afb_verb_v2 *verbs;        /**< array of descriptions of verbs terminated by a NULL name */
59         int (*preinit)();                       /**< callback at load of the binding */
60         int (*init)();                          /**< callback for starting the service */
61         void (*onevent)(const char *event, struct json_object *object); /**< callback for handling events */
62         unsigned noconcurrency: 1;              /**< avoids concurrent requests to verbs */
63 };
64
65 /**
66  * @deprecated use bindings version 3
67  *
68  * structure for the global data of the binding
69  */
70 struct afb_binding_data_v2
71 {
72         int verbosity;                  /**< level of verbosity */
73         struct afb_daemon_x1 daemon;    /**< access to daemon APIs */
74         struct afb_service_x1 service;  /**< access to service APIs */
75 };
76
77 /**
78  * @page validity-v2 Validity of a binding v2
79  *
80  * A binding V2 MUST have two exported symbols of name:
81  *
82  *            -  @ref afbBindingV2
83  *            -  @ref afbBindingV2data
84  */
85
86 /**
87  * @deprecated use bindings version 3
88  *
89  * The global mandatory description of the binding
90  */
91 #if !defined(AFB_BINDING_MAIN_NAME_V2)
92 extern const struct afb_binding_v2 afbBindingV2;
93 #endif
94
95 /**
96  * @deprecated use bindings version 3
97  *
98  * The global auto declared internal data of the binding
99  */
100 #if AFB_BINDING_VERSION != 2
101 extern
102 #endif
103 struct afb_binding_data_v2 afbBindingV2data  __attribute__ ((weak));
104
105 #define afb_get_verbosity_v2()  (afbBindingV2data.verbosity)
106 #define afb_get_daemon_v2()     (afbBindingV2data.daemon)
107 #define afb_get_service_v2()    (afbBindingV2data.service)
108
109 /******************************************************************************/
110 /*
111  * Macros for logging messages
112  */
113 #if defined(AFB_BINDING_PRAGMA_NO_VERBOSE_DATA)
114
115 #define AFB_VERBOSE_V2(level,...) \
116                 do { if(level <= AFB_VERBOSITY_LEVEL_ERROR) \
117                         afb_daemon_verbose_v2(level,__FILE__,__LINE__,NULL,__VA_ARGS__); \
118                 else afb_daemon_verbose_v2(level,__FILE__,__LINE__,NULL); } while(0)
119
120 #define AFB_REQ_VERBOSE_V2(req,level,...) \
121                 do { if(level <= AFB_VERBOSITY_LEVEL_ERROR) \
122                         afb_req_x1_verbose(req,level,__FILE__,__LINE__,NULL,__VA_ARGS__); \
123                 else afb_req_x1_verbose(req,level,__FILE__,__LINE__,NULL); } while(0)
124
125 #elif defined(AFB_BINDING_PRAGMA_NO_VERBOSE_DETAILS)
126
127 #define AFB_VERBOSE_V2(level,...) \
128                 afb_daemon_verbose_v2(level,NULL,0,NULL,__VA_ARGS__)
129
130 #define AFB_REQ_VERBOSE_V2(req,level,...) \
131                 afb_req_x1_verbose(req,level,NULL,0,NULL,__VA_ARGS__)
132
133 #else
134
135 #define AFB_VERBOSE_V2(level,...) \
136                 afb_daemon_verbose_v2(level,__FILE__,__LINE__,__func__,__VA_ARGS__)
137
138 #define AFB_REQ_VERBOSE_V2(req,level,...) \
139                 afb_req_x1_verbose(req,level,__FILE__,__LINE__,__func__,__VA_ARGS__)
140
141 #endif
142
143 #define _AFB_LOGGING_V2_(vlevel,llevel,...) \
144         do{ if(afb_get_verbosity_v2()>=vlevel) AFB_VERBOSE_V2(llevel,__VA_ARGS__); } while(0)
145 #define _AFB_REQ_LOGGING_V2_(vlevel,llevel,req,...) \
146         do{ if(afb_get_verbosity_v2()>=vlevel) AFB_REQ_VERBOSE_V2(req,llevel,__VA_ARGS__); } while(0)
147
148 #define AFB_ERROR_V2(...)       _AFB_LOGGING_V2_(AFB_VERBOSITY_LEVEL_ERROR,AFB_SYSLOG_LEVEL_ERROR,__VA_ARGS__)
149 #define AFB_WARNING_V2(...)     _AFB_LOGGING_V2_(AFB_VERBOSITY_LEVEL_WARNING,AFB_SYSLOG_LEVEL_WARNING,__VA_ARGS__)
150 #define AFB_NOTICE_V2(...)      _AFB_LOGGING_V2_(AFB_VERBOSITY_LEVEL_NOTICE,AFB_SYSLOG_LEVEL_NOTICE,__VA_ARGS__)
151 #define AFB_INFO_V2(...)        _AFB_LOGGING_V2_(AFB_VERBOSITY_LEVEL_INFO,AFB_SYSLOG_LEVEL_INFO,__VA_ARGS__)
152 #define AFB_DEBUG_V2(...)       _AFB_LOGGING_V2_(AFB_VERBOSITY_LEVEL_DEBUG,AFB_SYSLOG_LEVEL_DEBUG,__VA_ARGS__)
153 #define AFB_REQ_ERROR_V2(...)   _AFB_REQ_LOGGING_V2_(AFB_VERBOSITY_LEVEL_ERROR,AFB_SYSLOG_LEVEL_ERROR,__VA_ARGS__)
154 #define AFB_REQ_WARNING_V2(...) _AFB_REQ_LOGGING_V2_(AFB_VERBOSITY_LEVEL_WARNING,AFB_SYSLOG_LEVEL_WARNING,__VA_ARGS__)
155 #define AFB_REQ_NOTICE_V2(...)  _AFB_REQ_LOGGING_V2_(AFB_VERBOSITY_LEVEL_NOTICE,AFB_SYSLOG_LEVEL_NOTICE,__VA_ARGS__)
156 #define AFB_REQ_INFO_V2(...)    _AFB_REQ_LOGGING_V2_(AFB_VERBOSITY_LEVEL_INFO,AFB_SYSLOG_LEVEL_INFO,__VA_ARGS__)
157 #define AFB_REQ_DEBUG_V2(...)   _AFB_REQ_LOGGING_V2_(AFB_VERBOSITY_LEVEL_DEBUG,AFB_SYSLOG_LEVEL_DEBUG,__VA_ARGS__)
158
159 /******************************************************************************/
160
161 #if 0 && AFB_BINDING_VERSION >= 2
162
163 # define afb_verbose_error()    (afb_get_verbosity() >= AFB_VERBOSITY_LEVEL_ERROR)
164 # define afb_verbose_warning()  (afb_get_verbosity() >= AFB_VERBOSITY_LEVEL_WARNING)
165 # define afb_verbose_notice()   (afb_get_verbosity() >= AFB_VERBOSITY_LEVEL_NOTICE)
166 # define afb_verbose_info()     (afb_get_verbosity() >= AFB_VERBOSITY_LEVEL_INFO)
167 # define afb_verbose_debug()    (afb_get_verbosity() >= AFB_VERBOSITY_LEVEL_DEBUG)
168
169 #endif
170
171 #include "afb-daemon-v2.h"
172 #include "afb-service-v2.h"
173