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