Prepare xreq to be aware of the version
[src/app-framework-binder.git] / src / afb-api-so-v2.c
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 #define _GNU_SOURCE
19 #define NO_BINDING_VERBOSE_MACRO
20
21 #include <string.h>
22 #include <dlfcn.h>
23 #include <assert.h>
24
25 #include <afb/afb-binding.h>
26 #include <json-c/json.h>
27
28 #include "afb-api.h"
29 #include "afb-apiset.h"
30 #include "afb-svc.h"
31 #include "afb-ditf.h"
32 #include "afb-evt.h"
33 #include "afb-common.h"
34 #include "afb-context.h"
35 #include "afb-api-so.h"
36 #include "afb-xreq.h"
37 #include "afb-perm.h"
38 #include "verbose.h"
39
40 /*
41  * names of symbols
42  */
43 static const char afb_api_so_v2_descriptor[] = "afbBindingV2";
44
45 /**
46  * structure for memorizing verbs sorted with permissions
47  */
48 struct verb_v2 {
49         const struct afb_verb_v2 *verb;
50         struct afb_perm *perm;
51 };
52
53 /*
54  * Description of a binding
55  */
56 struct api_so_v2 {
57         const struct afb_binding_v2 *binding;   /* descriptor */
58         void *handle;                   /* context of dlopen */
59         struct afb_svc *service;        /* handler for service started */
60         struct afb_ditf ditf;           /* daemon interface */
61         int count;
62         struct verb_v2 verbs[1];
63 };
64
65 static const struct afb_verb_v2 *search(struct api_so_v2 *desc, const char *verb)
66 {
67         const struct afb_verb_v2 *v;
68         int i, l, u, c;
69
70         l = 0;
71         u = desc->count;
72         while (l < u) {
73                 i = (l + u) >> 1;
74                 v = desc->verbs[i].verb;
75                 c = strcasecmp(v->verb, verb);
76                 if (c == 0)
77                         return v;
78                 if (c < 0)
79                         l = i + 1;
80                 else
81                         u = i;
82         }
83         return NULL;
84 }
85
86 static void call_cb(void *closure, struct afb_xreq *xreq)
87 {
88         struct api_so_v2 *desc = closure;
89         const struct afb_verb_v2 *verb;
90
91         verb = search(desc, xreq->verb);
92         afb_xreq_call_verb_v2(xreq, verb);
93 }
94
95 static int service_start_cb(void *closure, int share_session, int onneed, struct afb_apiset *apiset)
96 {
97         int (*start)(const struct afb_binding_interface *interface, struct afb_service service);
98         void (*onevent)(const char *event, struct json_object *object);
99
100         struct api_so_v2 *desc = closure;
101
102         /* check state */
103         if (desc->service != NULL) {
104                 /* not an error when onneed */
105                 if (onneed != 0)
106                         return 0;
107
108                 /* already started: it is an error */
109                 ERROR("Service %s already started", desc->binding->api);
110                 return -1;
111         }
112
113         /* get the initialisation */
114         start = desc->binding->start;
115         if (start == NULL) {
116                 /* not an error when onneed */
117                 if (onneed != 0)
118                         return 0;
119
120                 /* no initialisation method */
121                 ERROR("Binding %s is not a service", desc->binding->api);
122                 return -1;
123         }
124
125         /* get the event handler if any */
126         onevent = desc->binding->onevent;
127         desc->service = afb_svc_create_v2(apiset, share_session, onevent, start, &desc->ditf.interface);
128         if (desc->service == NULL) {
129                 /* starting error */
130                 ERROR("Starting service %s failed", desc->binding->api);
131                 return -1;
132         }
133
134         return 0;
135 }
136
137 static void update_hooks_cb(void *closure)
138 {
139         struct api_so_v2 *desc = closure;
140         afb_ditf_update_hook(&desc->ditf);
141 }
142
143 static int get_verbosity_cb(void *closure)
144 {
145         struct api_so_v2 *desc = closure;
146         return desc->ditf.interface.verbosity;
147 }
148
149 static void set_verbosity_cb(void *closure, int level)
150 {
151         struct api_so_v2 *desc = closure;
152         desc->ditf.interface.verbosity = level;
153 }
154
155 static struct json_object *describe_cb(void *closure)
156 {
157         struct api_so_v2 *desc = closure;
158         return desc->binding->specification ? json_tokener_parse(desc->binding->specification) : NULL;
159 }
160
161 static struct afb_api_itf so_v2_api_itf = {
162         .call = call_cb,
163         .service_start = service_start_cb,
164         .update_hooks = update_hooks_cb,
165         .get_verbosity = get_verbosity_cb,
166         .set_verbosity = set_verbosity_cb,
167         .describe = describe_cb
168
169 };
170
171 int afb_api_so_v2_add_binding(const struct afb_binding_v2 *binding, void *handle, struct afb_apiset *apiset)
172 {
173         int rc;
174         struct api_so_v2 *desc;
175         struct afb_api afb_api;
176         const struct afb_verb_v2 *bv;
177         int count, i, j;
178
179         /* basic checks */
180         assert(binding->api);
181         assert(binding->specification);
182         assert(binding->verbs);
183
184         /* count the verbs */
185         count = 0;
186         while (binding->verbs[count].verb)
187                 count++;
188
189         /* allocates the description */
190         desc = malloc(sizeof *desc + (count - 1) * sizeof desc->verbs);
191         if (desc == NULL) {
192                 ERROR("out of memory");
193                 goto error;
194         }
195         desc->binding = binding;
196         desc->handle = handle;
197         desc->service = NULL;
198         memset(&desc->ditf, 0, sizeof desc->ditf);
199         desc->count = count;
200
201         /* fill the verbs sorted */
202         for (i = 0 ; i < count ; i++) {
203                 desc->verbs[i].perm = NULL;
204                 j = i;
205                 bv = &binding->verbs[j];
206                 while (j && strcasecmp(bv->verb, desc->verbs[j-1].verb->verb) < 0) {
207                         desc->verbs[j].verb = desc->verbs[j-1].verb;
208                         j--;
209                 }
210                 desc->verbs[j].verb = bv;
211         }
212
213         /* makes the permissions */
214         for (i = 0 ; i < count ; i++) {
215                 if (desc->verbs[i].verb->permissions) {
216                         desc->verbs[i].perm = afb_perm_parse(desc->verbs[i].verb->permissions);
217                         if (!desc->verbs[i].perm) {
218                                 ERROR("Bad permission specification for verb %s of api %s: %s",
219                                         desc->verbs[i].verb->verb, binding->api,
220                                         desc->verbs[i].verb->permissions);
221                                 goto error2;
222                         }
223                 }
224         }
225
226         /* init the interface */
227         afb_ditf_init(&desc->ditf, binding->api);
228
229         /* init the binding */
230         if (binding->init) {
231                 INFO("binding %s calling init function", binding->api);
232                 rc = binding->init(&desc->ditf.interface);
233                 if (rc < 0) {
234                         ERROR("binding %s initialisation function failed...", binding->api);
235                         goto error2;
236                 }
237         }
238
239         /* records the binding */
240         afb_api.closure = desc;
241         afb_api.itf = &so_v2_api_itf;
242         if (afb_apiset_add(apiset, binding->api, afb_api) < 0) {
243                 ERROR("binding %s can't be registered to set %s...", binding->api, afb_apiset_name(apiset));
244                 goto error2;
245         }
246         NOTICE("binding %s added to set %s", binding->api, afb_apiset_name(apiset));
247         return 1;
248
249 error2:
250         count = desc->count;
251         while (count)
252                 if (desc->verbs[--count].perm)
253                         afb_perm_unref(desc->verbs[count].perm);
254         free(desc);
255 error:
256         return -1;
257 }
258
259 int afb_api_so_v2_add(const char *path, void *handle, struct afb_apiset *apiset)
260 {
261         const struct afb_binding_v2 *binding;
262
263         /* retrieves the register function */
264         binding = dlsym(handle, afb_api_so_v2_descriptor);
265         if (!binding)
266                 return 0;
267
268         INFO("binding [%s] looks like an AFB binding V2", path);
269
270         /* basic checks */
271         if (binding->api == NULL || *binding->api == 0) {
272                 ERROR("binding [%s] bad api name...", path);
273                 goto error;
274         }
275         if (!afb_api_is_valid_name(binding->api)) {
276                 ERROR("binding [%s] invalid api name...", path);
277                 goto error;
278         }
279         if (binding->specification == NULL || *binding->specification == 0) {
280                 ERROR("binding [%s] bad specification...", path);
281                 goto error;
282         }
283         if (binding->verbs == NULL) {
284                 ERROR("binding [%s] no verbs...", path);
285                 goto error;
286         }
287
288         return afb_api_so_v2_add_binding(binding, handle, apiset);
289
290  error:
291         return -1;
292 }
293