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