Make api descriptions unique
[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 <stdlib.h>
22 #include <string.h>
23 #include <dlfcn.h>
24 #include <assert.h>
25
26 #include <afb/afb-binding-v2.h>
27 #include <json-c/json.h>
28
29 #include "afb-api.h"
30 #include "afb-api-so-v2.h"
31 #include "afb-apiset.h"
32 #include "afb-svc.h"
33 #include "afb-ditf.h"
34 #include "afb-evt.h"
35 #include "afb-common.h"
36 #include "afb-context.h"
37 #include "afb-api-so.h"
38 #include "afb-xreq.h"
39 #include "jobs.h"
40 #include "verbose.h"
41
42 /*
43  * names of symbols
44  */
45 static const char afb_api_so_v2_descriptor[] = "afbBindingV2";
46 static const char afb_api_so_v2_data[] = "afbBindingV2data";
47
48 /*
49  * Description of a binding
50  */
51 struct api_so_v2 {
52         const struct afb_binding_v2 *binding;   /* descriptor */
53         struct afb_binding_data_v2 *data;       /* data */
54         void *handle;                   /* context of dlopen */
55         struct afb_svc *service;        /* handler for service started */
56         struct afb_ditf ditf;           /* daemon interface */
57 };
58
59 static const struct afb_verb_v2 *search(struct api_so_v2 *desc, const char *name)
60 {
61         const struct afb_verb_v2 *verb;
62
63         verb = desc->binding->verbs;
64         while (verb->verb && strcasecmp(verb->verb, name))
65                 verb++;
66         return verb->verb ? verb : NULL;
67         return NULL;
68 }
69
70 static void call_cb(void *closure, struct afb_xreq *xreq)
71 {
72         struct api_so_v2 *desc = closure;
73         const struct afb_verb_v2 *verb;
74
75         verb = search(desc, xreq->verb);
76         afb_xreq_call_verb_v2(xreq, verb);
77 }
78
79 struct call_sync
80 {
81         struct api_so_v2 *desc;
82         struct afb_xreq *xreq;
83 };
84
85 static void call_sync_cb_cb(int signum, void *closure)
86 {
87         struct call_sync *cs = closure;
88         if (!signum)
89                 call_cb(cs->desc, cs->xreq);
90         else  {
91                 if (!cs->xreq->replied)
92                         afb_xreq_fail(cs->xreq, "aborted", "internal error");
93         }
94 }
95
96 static void call_sync_cb(void *closure, struct afb_xreq *xreq)
97 {
98         struct call_sync cs = { .desc = closure, .xreq = xreq };
99
100         if (jobs_call(closure, 0, call_sync_cb_cb, &cs))
101                 call_cb(closure, xreq);
102 }
103
104 static int service_start_cb(void *closure, int share_session, int onneed, struct afb_apiset *apiset)
105 {
106         int (*start)();
107         void (*onevent)(const char *event, struct json_object *object);
108
109         struct api_so_v2 *desc = closure;
110
111         /* check state */
112         if (desc->service != NULL) {
113                 /* not an error when onneed */
114                 if (onneed != 0)
115                         return 0;
116
117                 /* already started: it is an error */
118                 ERROR("Service %s already started", desc->binding->api);
119                 return -1;
120         }
121
122         /* get the initialisation */
123         start = desc->binding->init;
124         onevent = desc->binding->onevent;
125         if (start == NULL && onevent == NULL) {
126                 /* not an error when onneed */
127                 if (onneed != 0)
128                         return 0;
129
130                 /* no initialisation method */
131                 ERROR("Binding %s is not a service", desc->binding->api);
132                 return -1;
133         }
134
135         /* get the event handler if any */
136         desc->service = afb_svc_create_v2(desc->binding->api, apiset, share_session, start, onevent, desc->data);
137         if (desc->service == NULL) {
138                 /* starting error */
139                 ERROR("Starting service %s failed", desc->binding->api);
140                 return -1;
141         }
142
143         return 0;
144 }
145
146 static void update_hooks_cb(void *closure)
147 {
148         struct api_so_v2 *desc = closure;
149         afb_ditf_update_hook(&desc->ditf);
150         if (desc->service)
151                 afb_svc_update_hook(desc->service);
152 }
153
154 static int get_verbosity_cb(void *closure)
155 {
156         struct api_so_v2 *desc = closure;
157         return desc->data->verbosity;
158 }
159
160 static void set_verbosity_cb(void *closure, int level)
161 {
162         struct api_so_v2 *desc = closure;
163         desc->data->verbosity = level;
164 }
165
166 static struct json_object *addperm(struct json_object *o, struct json_object *x)
167 {
168         struct json_object *a;
169
170         if (!o)
171                 return x;
172
173         if (!json_object_object_get_ex(o, "allOf", &a)) {
174                 a = json_object_new_array();
175                 json_object_array_add(a, o);
176                 o = json_object_new_object();
177                 json_object_object_add(o, "allOf", a);
178         }
179         json_object_array_add(a, x);
180         return o;
181 }
182
183 static struct json_object *addperm_key_val(struct json_object *o, const char *key, struct json_object *val)
184 {
185         struct json_object *x = json_object_new_object();
186         json_object_object_add(x, key, val);
187         return addperm(o, x);
188 }
189
190 static struct json_object *addperm_key_valstr(struct json_object *o, const char *key, const char *val)
191 {
192         return addperm_key_val(o, key, json_object_new_string(val));
193 }
194
195 static struct json_object *addperm_key_valint(struct json_object *o, const char *key, int val)
196 {
197         return addperm_key_val(o, key, json_object_new_int(val));
198 }
199
200 static struct json_object *make_description_openAPIv3(struct api_so_v2 *desc)
201 {
202         char buffer[256];
203         const struct afb_verb_v2 *verb;
204         struct json_object *r, *f, *a, *i, *p, *g;
205
206         r = json_object_new_object();
207         json_object_object_add(r, "openapi", json_object_new_string("3.0.0"));
208
209         i = json_object_new_object();
210         json_object_object_add(r, "info", i);
211         json_object_object_add(i, "title", json_object_new_string(desc->binding->api));
212         json_object_object_add(i, "version", json_object_new_string("0.0.0"));
213
214         p = json_object_new_object();
215         json_object_object_add(r, "paths", p);
216         verb = desc->binding->verbs;
217         while (verb->verb) {
218                 buffer[0] = '/';
219                 strncpy(buffer + 1, verb->verb, sizeof buffer - 1);
220                 buffer[sizeof buffer - 1] = 0;
221                 f = json_object_new_object();
222                 json_object_object_add(p, buffer, f);
223                 g = json_object_new_object();
224                 json_object_object_add(f, "get", g);
225
226                 a = NULL;
227                 if (verb->session & AFB_SESSION_CLOSE_V2)
228                         a = addperm_key_valstr(a, "session", "close");
229                 if (verb->session & AFB_SESSION_CHECK_V2)
230                         a = addperm_key_valstr(a, "session", "check");
231                 if (verb->session & AFB_SESSION_REFRESH_V2)
232                         a = addperm_key_valstr(a, "token", "refresh");
233                 if (verb->session & AFB_SESSION_LOA_MASK_V2)
234                         a = addperm_key_valint(a, "LOA", verb->session & AFB_SESSION_LOA_MASK_V2);
235 #if 0
236                 if (verb->auth)
237                         a = 
238 #endif
239                 if (a)
240                         json_object_object_add(g, "x-permissions", a);
241
242                 a = json_object_new_object();
243                 json_object_object_add(g, "responses", a);
244                 f = json_object_new_object();
245                 json_object_object_add(a, "200", f);
246                 json_object_object_add(f, "description", json_object_new_string(verb->verb));
247                 verb++;
248         }
249         return r;
250 }
251
252 static struct json_object *describe_cb(void *closure)
253 {
254         struct api_so_v2 *desc = closure;
255         struct json_object *r = desc->binding->specification ? json_tokener_parse(desc->binding->specification) : NULL;
256         if (!r)
257                 r = make_description_openAPIv3(desc);
258         return r;
259 }
260
261 static struct afb_api_itf so_v2_api_itf = {
262         .call = call_cb,
263         .service_start = service_start_cb,
264         .update_hooks = update_hooks_cb,
265         .get_verbosity = get_verbosity_cb,
266         .set_verbosity = set_verbosity_cb,
267         .describe = describe_cb
268 };
269
270 static struct afb_api_itf so_v2_sync_api_itf = {
271         .call = call_sync_cb,
272         .service_start = service_start_cb,
273         .update_hooks = update_hooks_cb,
274         .get_verbosity = get_verbosity_cb,
275         .set_verbosity = set_verbosity_cb,
276         .describe = describe_cb
277 };
278
279 int afb_api_so_v2_add_binding(const struct afb_binding_v2 *binding, void *handle, struct afb_apiset *apiset, struct afb_binding_data_v2 *data)
280 {
281         int rc;
282         struct api_so_v2 *desc;
283         struct afb_api afb_api;
284
285         /* basic checks */
286         assert(binding);
287         assert(binding->api);
288         assert(binding->verbs);
289         assert(data);
290
291         /* allocates the description */
292         desc = calloc(1, sizeof *desc);
293         if (desc == NULL) {
294                 ERROR("out of memory");
295                 goto error;
296         }
297         desc->binding = binding;
298         desc->data = data;
299         desc->handle = handle;
300         desc->service = NULL;
301
302         /* init the interface */
303         desc->data->verbosity = verbosity;
304         afb_ditf_init_v2(&desc->ditf, binding->api, data);
305
306         /* init the binding */
307         if (binding->preinit) {
308                 INFO("binding %s calling preinit function", binding->api);
309                 rc = binding->preinit();
310                 if (rc < 0) {
311                         ERROR("binding %s preinit function failed...", binding->api);
312                         goto error2;
313                 }
314         }
315
316         /* records the binding */
317         afb_api.closure = desc;
318         afb_api.itf = binding->noconcurrency ? &so_v2_sync_api_itf : &so_v2_api_itf;
319         if (afb_apiset_add(apiset, binding->api, afb_api) < 0) {
320                 ERROR("binding %s can't be registered to set %s...", binding->api, afb_apiset_name(apiset));
321                 goto error2;
322         }
323         INFO("binding %s added to set %s", binding->api, afb_apiset_name(apiset));
324         return 1;
325
326 error2:
327         free(desc);
328 error:
329         return -1;
330 }
331
332 int afb_api_so_v2_add(const char *path, void *handle, struct afb_apiset *apiset)
333 {
334         const struct afb_binding_v2 *binding;
335         struct afb_binding_data_v2 *data;
336
337         /* retrieves the register function */
338         binding = dlsym(handle, afb_api_so_v2_descriptor);
339         data = dlsym(handle, afb_api_so_v2_data);
340         if (!binding && !data)
341                 return 0;
342
343         INFO("binding [%s] looks like an AFB binding V2", path);
344
345         /* basic checks */
346         if (!binding || !data) {
347                 ERROR("binding [%s] incomplete symbol set: %s is missing",
348                         path, binding ? afb_api_so_v2_data : afb_api_so_v2_descriptor);
349                 goto error;
350         }
351         if (binding->api == NULL || *binding->api == 0) {
352                 ERROR("binding [%s] bad api name...", path);
353                 goto error;
354         }
355         if (!afb_api_is_valid_name(binding->api)) {
356                 ERROR("binding [%s] invalid api name...", path);
357                 goto error;
358         }
359 #if 0
360         if (binding->specification == NULL || *binding->specification == 0) {
361                 ERROR("binding [%s] bad specification...", path);
362                 goto error;
363         }
364 #endif
365         if (binding->verbs == NULL) {
366                 ERROR("binding [%s] no verbs...", path);
367                 goto error;
368         }
369
370         return afb_api_so_v2_add_binding(binding, handle, apiset, data);
371
372  error:
373         return -1;
374 }
375