Introduce afb_request
[src/app-framework-binder.git] / include / afb / afb-binding-v2.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 <stdint.h>
21
22 #include "afb-auth.h"
23 #include "afb-event.h"
24 #include "afb-req.h"
25 #include "afb-service-common.h"
26 #include "afb-daemon-common.h"
27
28 #include "afb-req-v2.h"
29 #include "afb-session-v2.h"
30
31 struct json_object;
32
33 /*
34  * Description of one verb of the API provided by the binding
35  * This enumeration is valid for bindings of type version 2
36  */
37 struct afb_verb_v2
38 {
39         const char *verb;                       /* name of the verb, NULL only at end of the array */
40         void (*callback)(struct afb_req req);   /* callback function implementing the verb */
41         const struct afb_auth *auth;            /* required authorisation, can be NULL */
42         const char *info;                       /* some info about the verb, can be NULL */
43         uint32_t session;                       /* authorisation and session requirements of the verb */
44 };
45
46 /*
47  * Description of the bindings of type version 2
48  */
49 struct afb_binding_v2
50 {
51         const char *api;                        /* api name for the binding */
52         const char *specification;              /* textual specification of the binding, can be NULL */
53         const char *info;                       /* some info about the api, can be NULL */
54         const struct afb_verb_v2 *verbs;        /* array of descriptions of verbs terminated by a NULL name */
55         int (*preinit)();                       /* callback at load of the binding */
56         int (*init)();                          /* callback for starting the service */
57         void (*onevent)(const char *event, struct json_object *object); /* callback for handling events */
58         unsigned noconcurrency: 1;              /* avoids concurrent requests to verbs */
59 };
60
61 struct afb_binding_data_v2
62 {
63         int verbosity;                  /* level of verbosity */
64         struct afb_daemon daemon;       /* access to daemon APIs */
65         struct afb_service service;     /* access to service APIs */
66 };
67
68 /*
69  * A binding V2 MUST have two exported symbols of name:
70  *
71  *            -  afbBindingV2
72  *            -  afbBindingV2data
73  *
74  */
75 #if !defined(AFB_BINDING_MAIN_NAME_V2)
76 extern const struct afb_binding_v2 afbBindingV2;
77 #endif
78
79 #if !defined(AFB_BINDING_DATA_NAME_V2)
80 #define AFB_BINDING_DATA_NAME_V2 afbBindingV2data
81 #endif
82
83 #if AFB_BINDING_VERSION != 2
84 extern
85 #endif
86 struct afb_binding_data_v2 AFB_BINDING_DATA_NAME_V2  __attribute__ ((weak));
87
88 #define afb_get_verbosity_v2()  (AFB_BINDING_DATA_NAME_V2.verbosity)
89 #define afb_get_daemon_v2()     (AFB_BINDING_DATA_NAME_V2.daemon)
90 #define afb_get_service_v2()    (AFB_BINDING_DATA_NAME_V2.service)
91
92 /*
93  * Macros for logging messages
94  */
95 #if defined(AFB_BINDING_PRAGMA_NO_VERBOSE_DATA)
96
97 # define _AFB_LOGGING_V2_(vlevel,llevel,...) \
98         do{ \
99                 if(AFB_BINDING_DATA_NAME_V2.verbosity>=vlevel) {\
100                         if (llevel <= AFB_VERBOSITY_LEVEL_ERROR) \
101                                 afb_daemon_verbose_v2(llevel,__FILE__,__LINE__,__func__,__VA_ARGS__); \
102                         else \
103                                 afb_daemon_verbose_v2(llevel,__FILE__,__LINE__,NULL,NULL); \
104                 } \
105         }while(0)
106 # define _AFB_REQ_LOGGING_V2_(vlevel,llevel,req,...) \
107         do{ \
108                 if(AFB_BINDING_DATA_NAME_V2.verbosity>=vlevel) \
109                         afb_req_verbose(req,llevel,__FILE__,__LINE__,NULL,NULL); \
110         }while(0)
111
112 #elif defined(AFB_BINDING_PRAGMA_NO_VERBOSE_DETAILS)
113
114 # define _AFB_LOGGING_V2_(vlevel,llevel,...) \
115         do{ \
116                 if(AFB_BINDING_DATA_NAME_V2.verbosity>=vlevel) \
117                         afb_daemon_verbose_v2(llevel,NULL,0,NULL,__VA_ARGS__); \
118         }while(0)
119 # define _AFB_REQ_LOGGING_V2_(vlevel,llevel,req,...) \
120         do{ \
121                 if(AFB_BINDING_DATA_NAME_V2.verbosity>=vlevel) \
122                         afb_req_verbose(req,llevel,NULL,0,NULL,__VA_ARGS__); \
123         }while(0)
124
125 #else
126
127 # define _AFB_LOGGING_V2_(vlevel,llevel,...) \
128         do{ \
129                 if(AFB_BINDING_DATA_NAME_V2.verbosity>=vlevel) \
130                         afb_daemon_verbose_v2(llevel,__FILE__,__LINE__,__func__,__VA_ARGS__); \
131         }while(0)
132 # define _AFB_REQ_LOGGING_V2_(vlevel,llevel,req,...) \
133         do{ \
134                 if(AFB_BINDING_DATA_NAME_V2.verbosity>=vlevel) \
135                         afb_req_verbose(req,llevel,__FILE__,__LINE__,__func__,__VA_ARGS__); \
136         }while(0)
137
138 #endif
139
140 #include "afb-verbosity.h"
141 #define AFB_ERROR_V2(...)       _AFB_LOGGING_V2_(AFB_VERBOSITY_LEVEL_ERROR,_AFB_SYSLOG_LEVEL_ERROR_,__VA_ARGS__)
142 #define AFB_WARNING_V2(...)     _AFB_LOGGING_V2_(AFB_VERBOSITY_LEVEL_WARNING,_AFB_SYSLOG_LEVEL_WARNING_,__VA_ARGS__)
143 #define AFB_NOTICE_V2(...)      _AFB_LOGGING_V2_(AFB_VERBOSITY_LEVEL_NOTICE,_AFB_SYSLOG_LEVEL_NOTICE_,__VA_ARGS__)
144 #define AFB_INFO_V2(...)        _AFB_LOGGING_V2_(AFB_VERBOSITY_LEVEL_INFO,_AFB_SYSLOG_LEVEL_INFO_,__VA_ARGS__)
145 #define AFB_DEBUG_V2(...)       _AFB_LOGGING_V2_(AFB_VERBOSITY_LEVEL_DEBUG,_AFB_SYSLOG_LEVEL_DEBUG_,__VA_ARGS__)
146 #define AFB_REQ_ERROR_V2(...)   _AFB_REQ_LOGGING_V2_(AFB_VERBOSITY_LEVEL_ERROR,_AFB_SYSLOG_LEVEL_ERROR_,__VA_ARGS__)
147 #define AFB_REQ_WARNING_V2(...) _AFB_REQ_LOGGING_V2_(AFB_VERBOSITY_LEVEL_WARNING,_AFB_SYSLOG_LEVEL_WARNING_,__VA_ARGS__)
148 #define AFB_REQ_NOTICE_V2(...)  _AFB_REQ_LOGGING_V2_(AFB_VERBOSITY_LEVEL_NOTICE,_AFB_SYSLOG_LEVEL_NOTICE_,__VA_ARGS__)
149 #define AFB_REQ_INFO_V2(...)    _AFB_REQ_LOGGING_V2_(AFB_VERBOSITY_LEVEL_INFO,_AFB_SYSLOG_LEVEL_INFO_,__VA_ARGS__)
150 #define AFB_REQ_DEBUG_V2(...)   _AFB_REQ_LOGGING_V2_(AFB_VERBOSITY_LEVEL_DEBUG,_AFB_SYSLOG_LEVEL_DEBUG_,__VA_ARGS__)
151
152 #include "afb-daemon-v2.h"
153 #include "afb-service-v2.h"
154