Provide efficient store/unstore for afb_req
[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_MACRO)
151 # if !defined(AFB_BINDING_PRAGMA_NO_VERBOSE_DETAILS)
152 #  define _AFB_LOGGING_V1_(itf,vlevel,llevel,...) \
153         do{ \
154                 if(itf->verbosity>=vlevel) \
155                         afb_daemon_verbose2_v1(itf->daemon,llevel,__FILE__,__LINE__,__func__,__VA_ARGS__); \
156         }while(0)
157 #  define _AFB_REQ_LOGGING_V1_(itf,vlevel,llevel,req,...) \
158         do{ \
159                 if(itf->verbosity>=vlevel) \
160                         afb_req_verbose(req,llevel,__FILE__,__LINE__,__func__,__VA_ARGS__); \
161         }while(0)
162 # else
163 #  define _AFB_LOGGING_V1_(itf,vlevel,llevel,...) \
164         do{ \
165                 if(itf->verbosity>=vlevel) \
166                         afb_daemon_verbose_v1(itf->daemon,llevel,NULL,0,NULL,__VA_ARGS__); \
167         }while(0)
168 #  define _AFB_REQ_LOGGING_V1_(itf,vlevel,llevel,req,...) \
169         do{ \
170                 if(itf->verbosity>=vlevel) \
171                         afb_req_verbose(req,llevel,NULL,0,NULL,__VA_ARGS__); \
172         }while(0)
173 # endif
174 # define AFB_ERROR_V1(itf,...)       _AFB_LOGGING_V1_(itf,0,3,__VA_ARGS__)
175 # define AFB_WARNING_V1(itf,...)     _AFB_LOGGING_V1_(itf,1,4,__VA_ARGS__)
176 # define AFB_NOTICE_V1(itf,...)      _AFB_LOGGING_V1_(itf,1,5,__VA_ARGS__)
177 # define AFB_INFO_V1(itf,...)        _AFB_LOGGING_V1_(itf,2,6,__VA_ARGS__)
178 # define AFB_DEBUG_V1(itf,...)       _AFB_LOGGING_V1_(itf,3,7,__VA_ARGS__)
179 # define AFB_REQ_ERROR_V1(itf,...)   _AFB_REQ_LOGGING_V1_(itf,0,3,__VA_ARGS__)
180 # define AFB_REQ_WARNING_V1(itf,...) _AFB_REQ_LOGGING_V1_(itf,1,4,__VA_ARGS__)
181 # define AFB_REQ_NOTICE_V1(itf,...)  _AFB_REQ_LOGGING_V1_(itf,1,5,__VA_ARGS__)
182 # define AFB_REQ_INFO_V1(itf,...)    _AFB_REQ_LOGGING_V1_(itf,2,6,__VA_ARGS__)
183 # define AFB_REQ_DEBUG_V1(itf,...)   _AFB_REQ_LOGGING_V1_(itf,3,7,__VA_ARGS__)
184 #endif
185
186