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