minor cleanup
[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-export.h"
33 #include "afb-common.h"
34 #include "afb-context.h"
35 #include "afb-api-so.h"
36 #include "afb-xreq.h"
37 #include "verbose.h"
38
39 /*
40  * names of symbols
41  */
42 static const char afb_api_so_v1_register[] = "afbBindingV1Register";
43 static const char afb_api_so_v1_service_init[] = "afbBindingV1ServiceInit";
44 static const char afb_api_so_v1_service_event[] = "afbBindingV1ServiceEvent";
45
46 /*
47  * Description of a binding
48  */
49 struct api_so_v1 {
50         struct afb_binding_v1 *binding; /* descriptor */
51         void *handle;                   /* context of dlopen */
52         struct afb_export *export;      /* export */
53 };
54
55 static const struct afb_verb_desc_v1 *search(struct api_so_v1 *desc, const char *name)
56 {
57         const struct afb_verb_desc_v1 *verb;
58
59         verb = desc->binding->v1.verbs;
60         while (verb->name && strcasecmp(verb->name, name))
61                 verb++;
62         return verb->name ? verb : NULL;
63 }
64
65 static void call_cb(void *closure, struct afb_xreq *xreq)
66 {
67         const struct afb_verb_desc_v1 *verb;
68         struct api_so_v1 *desc = closure;
69
70         verb = search(desc, xreq->verb);
71         afb_xreq_call_verb_v1(xreq, verb);
72 }
73
74 static int service_start_cb(void *closure, int share_session, int onneed, struct afb_apiset *apiset)
75 {
76         int rc;
77         int (*init)(struct afb_service service);
78         void (*onevent)(const char *event, struct json_object *object);
79
80         struct api_so_v1 *desc = closure;
81
82         /* check state */
83         if (afb_export_is_started(desc->export)) {
84                 /* not an error when onneed */
85                 if (onneed != 0)
86                         goto done;
87
88                 /* already started: it is an error */
89                 ERROR("Service %s already started", afb_export_apiname(desc->export));
90                 return -1;
91         }
92
93         /* get the initialisation */
94         init = dlsym(desc->handle, afb_api_so_v1_service_init);
95         onevent = dlsym(desc->handle, afb_api_so_v1_service_event);
96
97         /* unshare the session if asked */
98         if (!share_session) {
99                 rc = afb_export_unshare_session(desc->export);
100                 if (rc < 0) {
101                         ERROR("Can't unshare the session for %s", afb_export_apiname(desc->export));
102                         return -1;
103                 }
104         }
105
106         /* set event handling */
107         rc = afb_export_handle_events(desc->export, onevent);
108         if (rc < 0) {
109                 ERROR("Can't set event handler for %s", afb_export_apiname(desc->export));
110                 return -1;
111         }
112
113         /* Starts the service */
114         rc = afb_export_start_v1(desc->export, init);
115         if (rc < 0) {
116                 /* initialisation error */
117                 ERROR("Initialisation of service %s failed (%d): %m", afb_export_apiname(desc->export), rc);
118                 return rc;
119         }
120
121 done:
122         return 0;
123 }
124
125 static void update_hooks_cb(void *closure)
126 {
127         struct api_so_v1 *desc = closure;
128         afb_export_update_hook(desc->export);
129 }
130
131 static int get_verbosity_cb(void *closure)
132 {
133         struct api_so_v1 *desc = closure;
134         return afb_export_verbosity_get(desc->export);
135 }
136
137 static void set_verbosity_cb(void *closure, int level)
138 {
139         struct api_so_v1 *desc = closure;
140         afb_export_verbosity_set(desc->export, level);
141 }
142
143 static struct json_object *addperm(struct json_object *o, struct json_object *x)
144 {
145         struct json_object *a;
146
147         if (!o)
148                 return x;
149
150         if (!json_object_object_get_ex(o, "allOf", &a)) {
151                 a = json_object_new_array();
152                 json_object_array_add(a, o);
153                 o = json_object_new_object();
154                 json_object_object_add(o, "allOf", a);
155         }
156         json_object_array_add(a, x);
157         return o;
158 }
159
160 static struct json_object *addperm_key_val(struct json_object *o, const char *key, struct json_object *val)
161 {
162         struct json_object *x = json_object_new_object();
163         json_object_object_add(x, key, val);
164         return addperm(o, x);
165 }
166
167 static struct json_object *addperm_key_valstr(struct json_object *o, const char *key, const char *val)
168 {
169         return addperm_key_val(o, key, json_object_new_string(val));
170 }
171
172 static struct json_object *addperm_key_valint(struct json_object *o, const char *key, int val)
173 {
174         return addperm_key_val(o, key, json_object_new_int(val));
175 }
176
177 static struct json_object *make_description_openAPIv3(struct api_so_v1 *desc)
178 {
179         char buffer[256];
180         const struct afb_verb_desc_v1 *verb;
181         struct json_object *r, *f, *a, *i, *p, *g;
182
183         r = json_object_new_object();
184         json_object_object_add(r, "openapi", json_object_new_string("3.0.0"));
185
186         i = json_object_new_object();
187         json_object_object_add(r, "info", i);
188         json_object_object_add(i, "title", json_object_new_string(afb_export_apiname(desc->export)));
189         json_object_object_add(i, "version", json_object_new_string("0.0.0"));
190         json_object_object_add(i, "description", json_object_new_string(desc->binding->v1.info ?: afb_export_apiname(desc->export)));
191
192         p = json_object_new_object();
193         json_object_object_add(r, "paths", p);
194         verb = desc->binding->v1.verbs;
195         while (verb->name) {
196                 buffer[0] = '/';
197                 strncpy(buffer + 1, verb->name, sizeof buffer - 1);
198                 buffer[sizeof buffer - 1] = 0;
199                 f = json_object_new_object();
200                 json_object_object_add(p, buffer, f);
201                 g = json_object_new_object();
202                 json_object_object_add(f, "get", g);
203
204                 a = NULL;
205                 if (verb->session & AFB_SESSION_CLOSE_V1)
206                         a = addperm_key_valstr(a, "session", "close");
207                 if (verb->session & AFB_SESSION_CHECK_V1)
208                         a = addperm_key_valstr(a, "session", "check");
209                 if (verb->session & AFB_SESSION_RENEW_V1)
210                         a = addperm_key_valstr(a, "token", "refresh");
211                 if (verb->session & AFB_SESSION_LOA_MASK_V1)
212                         a = addperm_key_valint(a, "LOA", (verb->session >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1);
213                 if (a)
214                         json_object_object_add(g, "x-permissions", a);
215
216                 a = json_object_new_object();
217                 json_object_object_add(g, "responses", a);
218                 f = json_object_new_object();
219                 json_object_object_add(a, "200", f);
220                 json_object_object_add(f, "description", json_object_new_string(verb->info));
221                 verb++;
222         }
223         return r;
224 }
225
226 static struct json_object *describe_cb(void *closure)
227 {
228         struct api_so_v1 *desc = closure;
229
230         return make_description_openAPIv3(desc);
231 }
232
233 static struct afb_api_itf so_v1_api_itf = {
234         .call = call_cb,
235         .service_start = service_start_cb,
236         .update_hooks = update_hooks_cb,
237         .get_verbosity = get_verbosity_cb,
238         .set_verbosity = set_verbosity_cb,
239         .describe = describe_cb
240 };
241
242 int afb_api_so_v1_add(const char *path, void *handle, struct afb_apiset *apiset)
243 {
244         struct api_so_v1 *desc;
245         struct afb_binding_v1 *(*register_function) (const struct afb_binding_interface_v1 *interface);
246         struct afb_api afb_api;
247         struct afb_export *export;
248
249         /* retrieves the register function */
250         register_function = dlsym(handle, afb_api_so_v1_register);
251         if (!register_function)
252                 return 0;
253         INFO("binding [%s] is a valid AFB binding V1", path);
254
255         /* allocates the description */
256         export = afb_export_create_v1(path);
257         desc = calloc(1, sizeof *desc);
258         if (desc == NULL || export == NULL) {
259                 ERROR("out of memory");
260                 goto error;
261         }
262         desc->export = export;
263         desc->handle = handle;
264
265         /* init the binding */
266         INFO("binding [%s] calling registering function %s", path, afb_api_so_v1_register);
267         desc->binding = afb_export_register_v1(desc->export, register_function);
268         if (desc->binding == NULL) {
269                 ERROR("binding [%s] register function failed. continuing...", path);
270                 goto error;
271         }
272
273         /* check the returned structure */
274         if (desc->binding->type != AFB_BINDING_VERSION_1) {
275                 ERROR("binding [%s] invalid type %d...", path, desc->binding->type);
276                 goto error;
277         }
278         if (desc->binding->v1.prefix == NULL || *desc->binding->v1.prefix == 0) {
279                 ERROR("binding [%s] bad prefix...", path);
280                 goto error;
281         }
282         if (!afb_api_is_valid_name(desc->binding->v1.prefix)) {
283                 ERROR("binding [%s] invalid prefix...", path);
284                 goto error;
285         }
286         if (desc->binding->v1.info == NULL || *desc->binding->v1.info == 0) {
287                 ERROR("binding [%s] bad description...", path);
288                 goto error;
289         }
290         if (desc->binding->v1.verbs == NULL) {
291                 ERROR("binding [%s] no APIs...", path);
292                 goto error;
293         }
294
295         /* records the binding */
296         if (!strcmp(path, afb_export_apiname(desc->export)))
297                 afb_export_rename(desc->export, desc->binding->v1.prefix);
298         afb_api.closure = desc;
299         afb_api.itf = &so_v1_api_itf;
300         afb_api.noconcurrency = 0;
301         if (afb_apiset_add(apiset, afb_export_apiname(desc->export), afb_api) < 0) {
302                 ERROR("binding [%s] can't be registered...", path);
303                 goto error;
304         }
305         INFO("binding %s loaded with API prefix %s", path, afb_export_apiname(desc->export));
306         return 1;
307
308 error:
309         afb_export_destroy(export);
310         free(desc);
311
312         return -1;
313 }
314