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