Refactor of starting apis
[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         struct api_so_v1 *desc = closure;
77         return afb_export_start(desc->export, share_session, onneed, apiset);
78 }
79
80 static void update_hooks_cb(void *closure)
81 {
82         struct api_so_v1 *desc = closure;
83         afb_export_update_hook(desc->export);
84 }
85
86 static int get_verbosity_cb(void *closure)
87 {
88         struct api_so_v1 *desc = closure;
89         return afb_export_verbosity_get(desc->export);
90 }
91
92 static void set_verbosity_cb(void *closure, int level)
93 {
94         struct api_so_v1 *desc = closure;
95         afb_export_verbosity_set(desc->export, level);
96 }
97
98 static struct json_object *addperm(struct json_object *o, struct json_object *x)
99 {
100         struct json_object *a;
101
102         if (!o)
103                 return x;
104
105         if (!json_object_object_get_ex(o, "allOf", &a)) {
106                 a = json_object_new_array();
107                 json_object_array_add(a, o);
108                 o = json_object_new_object();
109                 json_object_object_add(o, "allOf", a);
110         }
111         json_object_array_add(a, x);
112         return o;
113 }
114
115 static struct json_object *addperm_key_val(struct json_object *o, const char *key, struct json_object *val)
116 {
117         struct json_object *x = json_object_new_object();
118         json_object_object_add(x, key, val);
119         return addperm(o, x);
120 }
121
122 static struct json_object *addperm_key_valstr(struct json_object *o, const char *key, const char *val)
123 {
124         return addperm_key_val(o, key, json_object_new_string(val));
125 }
126
127 static struct json_object *addperm_key_valint(struct json_object *o, const char *key, int val)
128 {
129         return addperm_key_val(o, key, json_object_new_int(val));
130 }
131
132 static struct json_object *make_description_openAPIv3(struct api_so_v1 *desc)
133 {
134         char buffer[256];
135         const struct afb_verb_desc_v1 *verb;
136         struct json_object *r, *f, *a, *i, *p, *g;
137
138         r = json_object_new_object();
139         json_object_object_add(r, "openapi", json_object_new_string("3.0.0"));
140
141         i = json_object_new_object();
142         json_object_object_add(r, "info", i);
143         json_object_object_add(i, "title", json_object_new_string(afb_export_apiname(desc->export)));
144         json_object_object_add(i, "version", json_object_new_string("0.0.0"));
145         json_object_object_add(i, "description", json_object_new_string(desc->binding->v1.info ?: afb_export_apiname(desc->export)));
146
147         p = json_object_new_object();
148         json_object_object_add(r, "paths", p);
149         verb = desc->binding->v1.verbs;
150         while (verb->name) {
151                 buffer[0] = '/';
152                 strncpy(buffer + 1, verb->name, sizeof buffer - 1);
153                 buffer[sizeof buffer - 1] = 0;
154                 f = json_object_new_object();
155                 json_object_object_add(p, buffer, f);
156                 g = json_object_new_object();
157                 json_object_object_add(f, "get", g);
158
159                 a = NULL;
160                 if (verb->session & AFB_SESSION_CLOSE_V1)
161                         a = addperm_key_valstr(a, "session", "close");
162                 if (verb->session & AFB_SESSION_CHECK_V1)
163                         a = addperm_key_valstr(a, "session", "check");
164                 if (verb->session & AFB_SESSION_RENEW_V1)
165                         a = addperm_key_valstr(a, "token", "refresh");
166                 if (verb->session & AFB_SESSION_LOA_MASK_V1)
167                         a = addperm_key_valint(a, "LOA", (verb->session >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1);
168                 if (a)
169                         json_object_object_add(g, "x-permissions", a);
170
171                 a = json_object_new_object();
172                 json_object_object_add(g, "responses", a);
173                 f = json_object_new_object();
174                 json_object_object_add(a, "200", f);
175                 json_object_object_add(f, "description", json_object_new_string(verb->info));
176                 verb++;
177         }
178         return r;
179 }
180
181 static struct json_object *describe_cb(void *closure)
182 {
183         struct api_so_v1 *desc = closure;
184
185         return make_description_openAPIv3(desc);
186 }
187
188 static struct afb_api_itf so_v1_api_itf = {
189         .call = call_cb,
190         .service_start = service_start_cb,
191         .update_hooks = update_hooks_cb,
192         .get_verbosity = get_verbosity_cb,
193         .set_verbosity = set_verbosity_cb,
194         .describe = describe_cb
195 };
196
197 int afb_api_so_v1_add(const char *path, void *handle, struct afb_apiset *apiset)
198 {
199         struct api_so_v1 *desc;
200         struct afb_binding_v1 *(*register_function) (const struct afb_binding_interface_v1 *interface);
201         int (*init)(struct afb_service service);
202         void (*onevent)(const char *event, struct json_object *object);
203         struct afb_api afb_api;
204         struct afb_export *export;
205
206         /* retrieves the register function */
207         register_function = dlsym(handle, afb_api_so_v1_register);
208         if (!register_function)
209                 return 0;
210         INFO("binding [%s] is a valid AFB binding V1", path);
211
212         /* allocates the description */
213         init = dlsym(handle, afb_api_so_v1_service_init);
214         onevent = dlsym(handle, afb_api_so_v1_service_event);
215         export = afb_export_create_v1(path, init, onevent);
216         desc = calloc(1, sizeof *desc);
217         if (desc == NULL || export == NULL) {
218                 ERROR("out of memory");
219                 goto error;
220         }
221         desc->export = export;
222         desc->handle = handle;
223
224         /* init the binding */
225         INFO("binding [%s] calling registering function %s", path, afb_api_so_v1_register);
226         desc->binding = afb_export_register_v1(desc->export, register_function);
227         if (desc->binding == NULL) {
228                 ERROR("binding [%s] register function failed. continuing...", path);
229                 goto error;
230         }
231
232         /* check the returned structure */
233         if (desc->binding->type != AFB_BINDING_VERSION_1) {
234                 ERROR("binding [%s] invalid type %d...", path, desc->binding->type);
235                 goto error;
236         }
237         if (desc->binding->v1.prefix == NULL || *desc->binding->v1.prefix == 0) {
238                 ERROR("binding [%s] bad prefix...", path);
239                 goto error;
240         }
241         if (!afb_api_is_valid_name(desc->binding->v1.prefix)) {
242                 ERROR("binding [%s] invalid prefix...", path);
243                 goto error;
244         }
245         if (desc->binding->v1.info == NULL || *desc->binding->v1.info == 0) {
246                 ERROR("binding [%s] bad description...", path);
247                 goto error;
248         }
249         if (desc->binding->v1.verbs == NULL) {
250                 ERROR("binding [%s] no APIs...", path);
251                 goto error;
252         }
253
254         /* records the binding */
255         if (!strcmp(path, afb_export_apiname(desc->export)))
256                 afb_export_rename(desc->export, desc->binding->v1.prefix);
257         afb_api.closure = desc;
258         afb_api.itf = &so_v1_api_itf;
259         afb_api.noconcurrency = 0;
260         if (afb_apiset_add(apiset, afb_export_apiname(desc->export), afb_api) < 0) {
261                 ERROR("binding [%s] can't be registered...", path);
262                 goto error;
263         }
264         INFO("binding %s loaded with API prefix %s", path, afb_export_apiname(desc->export));
265         return 1;
266
267 error:
268         afb_export_destroy(export);
269         free(desc);
270
271         return -1;
272 }
273