Allow renaming of API
[src/app-framework-binder.git] / src / afb-api-so-v1.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 <stdio.h>
21 #include <string.h>
22 #include <dlfcn.h>
23 #include <assert.h>
24
25 #include <json-c/json.h>
26
27 #include <afb/afb-binding-v1.h>
28
29 #include "afb-api.h"
30 #include "afb-api-so-v1.h"
31 #include "afb-apiset.h"
32 #include "afb-svc.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-ditf.h"
39 #include "verbose.h"
40
41 /*
42  * names of symbols
43  */
44 static const char afb_api_so_v1_register[] = "afbBindingV1Register";
45 static const char afb_api_so_v1_service_init[] = "afbBindingV1ServiceInit";
46 static const char afb_api_so_v1_service_event[] = "afbBindingV1ServiceEvent";
47
48 /*
49  * Description of a binding
50  */
51 struct api_so_v1 {
52         struct afb_binding_v1 *binding; /* descriptor */
53         void *handle;                   /* context of dlopen */
54         struct afb_svc *service;        /* handler for service started */
55         struct afb_binding_interface_v1 interface;
56         struct afb_ditf ditf;           /* daemon interface */
57 };
58
59 static const struct afb_verb_desc_v1 *search(struct api_so_v1 *desc, const char *name)
60 {
61         const struct afb_verb_desc_v1 *verb;
62
63         verb = desc->binding->v1.verbs;
64         while (verb->name && strcasecmp(verb->name, name))
65                 verb++;
66         return verb->name ? verb : NULL;
67 }
68
69 static void call_cb(void *closure, struct afb_xreq *xreq)
70 {
71         const struct afb_verb_desc_v1 *verb;
72         struct api_so_v1 *desc = closure;
73
74         verb = search(desc, xreq->verb);
75         afb_xreq_call_verb_v1(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 (*init)(struct afb_service service);
82         void (*onevent)(const char *event, struct json_object *object);
83
84         struct api_so_v1 *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->ditf.api);
94                 return -1;
95         }
96
97         /* get the initialisation */
98         init = dlsym(desc->handle, afb_api_so_v1_service_init);
99         onevent = dlsym(desc->handle, afb_api_so_v1_service_event);
100         if (init == 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->ditf.api);
107                 return -1;
108         }
109
110         /* get the event handler if any */
111         desc->service = afb_svc_create(desc->ditf.api, apiset, share_session, onevent, NULL);
112         if (desc->service == NULL) {
113                 ERROR("Creation of service %s failed", desc->ditf.api);
114                 return -1;
115         }
116
117         /* Starts the service */
118         desc->ditf.state = Daemon_Init;
119         rc = afb_svc_start_v1(desc->service, init);
120         if (rc < 0) {
121                 /* initialisation error */
122                 ERROR("Initialisation of service %s failed (%d): %m", desc->ditf.api, rc);
123                 afb_svc_destroy(desc->service, NULL);
124                 desc->service = NULL;
125                 return rc;
126         }
127
128 done:
129         desc->ditf.state = Daemon_Run;
130         return 0;
131 }
132
133 static void update_hooks_cb(void *closure)
134 {
135         struct api_so_v1 *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_v1 *desc = closure;
144         return desc->interface.verbosity;
145 }
146
147 static void set_verbosity_cb(void *closure, int level)
148 {
149         struct api_so_v1 *desc = closure;
150         desc->interface.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_v1 *desc)
188 {
189         char buffer[256];
190         const struct afb_verb_desc_v1 *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->ditf.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->v1.info ?: desc->ditf.api));
201
202         p = json_object_new_object();
203         json_object_object_add(r, "paths", p);
204         verb = desc->binding->v1.verbs;
205         while (verb->name) {
206                 buffer[0] = '/';
207                 strncpy(buffer + 1, verb->name, 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_V1)
216                         a = addperm_key_valstr(a, "session", "close");
217                 if (verb->session & AFB_SESSION_CHECK_V1)
218                         a = addperm_key_valstr(a, "session", "check");
219                 if (verb->session & AFB_SESSION_RENEW_V1)
220                         a = addperm_key_valstr(a, "token", "refresh");
221                 if (verb->session & AFB_SESSION_LOA_MASK_V1)
222                         a = addperm_key_valint(a, "LOA", (verb->session >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1);
223                 if (a)
224                         json_object_object_add(g, "x-permissions", a);
225
226                 a = json_object_new_object();
227                 json_object_object_add(g, "responses", a);
228                 f = json_object_new_object();
229                 json_object_object_add(a, "200", f);
230                 json_object_object_add(f, "description", json_object_new_string(verb->info));
231                 verb++;
232         }
233         return r;
234 }
235
236 static struct json_object *describe_cb(void *closure)
237 {
238         struct api_so_v1 *desc = closure;
239
240         return make_description_openAPIv3(desc);
241 }
242
243 static struct afb_api_itf so_v1_api_itf = {
244         .call = call_cb,
245         .service_start = service_start_cb,
246         .update_hooks = update_hooks_cb,
247         .get_verbosity = get_verbosity_cb,
248         .set_verbosity = set_verbosity_cb,
249         .describe = describe_cb
250 };
251
252 int afb_api_so_v1_add(const char *path, void *handle, struct afb_apiset *apiset)
253 {
254         struct api_so_v1 *desc;
255         struct afb_binding_v1 *(*register_function) (const struct afb_binding_interface_v1 *interface);
256         struct afb_api afb_api;
257
258         /* retrieves the register function */
259         register_function = dlsym(handle, afb_api_so_v1_register);
260         if (!register_function)
261                 return 0;
262         INFO("binding [%s] is a valid AFB binding V1", path);
263
264         /* allocates the description */
265         desc = calloc(1, sizeof *desc);
266         if (desc == NULL) {
267                 ERROR("out of memory");
268                 goto error;
269         }
270         desc->handle = handle;
271
272         /* init the interface */
273         afb_ditf_init_v1(&desc->ditf, path, &desc->interface);
274
275         /* init the binding */
276         INFO("binding [%s] calling registering function %s", path, afb_api_so_v1_register);
277         desc->binding = register_function(&desc->interface);
278         if (desc->binding == NULL) {
279                 ERROR("binding [%s] register function failed. continuing...", path);
280                 goto error2;
281         }
282
283         /* check the returned structure */
284         if (desc->binding->type != AFB_BINDING_VERSION_1) {
285                 ERROR("binding [%s] invalid type %d...", path, desc->binding->type);
286                 goto error2;
287         }
288         if (desc->binding->v1.prefix == NULL || *desc->binding->v1.prefix == 0) {
289                 ERROR("binding [%s] bad prefix...", path);
290                 goto error2;
291         }
292         if (!afb_api_is_valid_name(desc->binding->v1.prefix)) {
293                 ERROR("binding [%s] invalid prefix...", path);
294                 goto error2;
295         }
296         if (desc->binding->v1.info == NULL || *desc->binding->v1.info == 0) {
297                 ERROR("binding [%s] bad description...", path);
298                 goto error2;
299         }
300         if (desc->binding->v1.verbs == NULL) {
301                 ERROR("binding [%s] no APIs...", path);
302                 goto error2;
303         }
304
305         /* records the binding */
306         if (desc->ditf.api == path)
307                 afb_ditf_rename(&desc->ditf, desc->binding->v1.prefix);
308         afb_api.closure = desc;
309         afb_api.itf = &so_v1_api_itf;
310         afb_api.noconcurrency = 0;
311         if (afb_apiset_add(apiset, desc->ditf.api, afb_api) < 0) {
312                 ERROR("binding [%s] can't be registered...", path);
313                 goto error2;
314         }
315         INFO("binding %s loaded with API prefix %s", path, desc->ditf.api);
316         return 1;
317
318 error2:
319         free(desc);
320 error:
321         return -1;
322 }
323