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