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