afb-export: record apiset at creation
[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
20 #include <stdlib.h>
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-auth.h"
32 #include "afb-export.h"
33 #include "afb-evt.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_v2_descriptor[] = "afbBindingV2";
43 static const char afb_api_so_v2_data[] = "afbBindingV2data";
44
45 /*
46  * Description of a binding
47  */
48 struct api_so_v2 {
49         const struct afb_binding_v2 *binding;   /* descriptor */
50         void *handle;                           /* context of dlopen */
51         struct afb_export *export;              /* exportations */
52 };
53
54 static const struct afb_verb_v2 *search(struct api_so_v2 *desc, const char *name)
55 {
56         const struct afb_verb_v2 *verb;
57
58         verb = desc->binding->verbs;
59         while (verb->verb && strcasecmp(verb->verb, name))
60                 verb++;
61         return verb->verb ? verb : NULL;
62         return NULL;
63 }
64
65 static void call_cb(void *closure, struct afb_xreq *xreq)
66 {
67         struct api_so_v2 *desc = closure;
68         const struct afb_verb_v2 *verb;
69
70         verb = search(desc, xreq->verb);
71         afb_xreq_call_verb_v2(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_v2 *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_v2 *desc = closure;
83         afb_export_update_hook(desc->export);
84 }
85
86 static int get_verbosity_cb(void *closure)
87 {
88         struct api_so_v2 *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_v2 *desc = closure;
95         afb_export_verbosity_set(desc->export, level);
96 }
97
98 static struct json_object *make_description_openAPIv3(struct api_so_v2 *desc)
99 {
100         char buffer[256];
101         const struct afb_verb_v2 *verb;
102         struct json_object *r, *f, *a, *i, *p, *g;
103
104         r = json_object_new_object();
105         json_object_object_add(r, "openapi", json_object_new_string("3.0.0"));
106
107         i = json_object_new_object();
108         json_object_object_add(r, "info", i);
109         json_object_object_add(i, "title", json_object_new_string(afb_export_apiname(desc->export)));
110         json_object_object_add(i, "version", json_object_new_string("0.0.0"));
111         json_object_object_add(i, "description", json_object_new_string(desc->binding->info ?: afb_export_apiname(desc->export)));
112
113         p = json_object_new_object();
114         json_object_object_add(r, "paths", p);
115         verb = desc->binding->verbs;
116         while (verb->verb) {
117                 buffer[0] = '/';
118                 strncpy(buffer + 1, verb->verb, sizeof buffer - 1);
119                 buffer[sizeof buffer - 1] = 0;
120                 f = json_object_new_object();
121                 json_object_object_add(p, buffer, f);
122                 g = json_object_new_object();
123                 json_object_object_add(f, "get", g);
124
125                 a = afb_auth_json_v2(verb->auth, verb->session);
126                 if (a)
127                         json_object_object_add(g, "x-permissions", a);
128
129                 a = json_object_new_object();
130                 json_object_object_add(g, "responses", a);
131                 f = json_object_new_object();
132                 json_object_object_add(a, "200", f);
133                 json_object_object_add(f, "description", json_object_new_string(verb->info?:verb->verb));
134                 verb++;
135         }
136         return r;
137 }
138
139 static struct json_object *describe_cb(void *closure)
140 {
141         struct api_so_v2 *desc = closure;
142         struct json_object *r = desc->binding->specification ? json_tokener_parse(desc->binding->specification) : NULL;
143         if (!r)
144                 r = make_description_openAPIv3(desc);
145         return r;
146 }
147
148 static struct afb_api_itf so_v2_api_itf = {
149         .call = call_cb,
150         .service_start = service_start_cb,
151         .update_hooks = update_hooks_cb,
152         .get_verbosity = get_verbosity_cb,
153         .set_verbosity = set_verbosity_cb,
154         .describe = describe_cb
155 };
156
157 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)
158 {
159         int rc;
160         struct api_so_v2 *desc;
161         struct afb_api afb_api;
162         struct afb_export *export;
163
164         /* basic checks */
165         assert(binding);
166         assert(binding->api);
167         assert(binding->verbs);
168         assert(data);
169
170         /* allocates the description */
171         export = afb_export_create_v2(apiset, binding->api, data, binding->init, binding->onevent);
172         desc = calloc(1, sizeof *desc);
173         if (!desc || !export) {
174                 ERROR("out of memory");
175                 goto error;
176         }
177         desc->binding = binding;
178         desc->handle = handle;
179         desc->export = export;
180
181         /* init the binding */
182         if (binding->preinit) {
183                 INFO("binding %s calling preinit function", binding->api);
184                 rc = binding->preinit();
185                 if (rc < 0) {
186                         ERROR("binding %s preinit function failed...", afb_export_apiname(desc->export));
187                         goto error;
188                 }
189         }
190
191         /* records the binding */
192         afb_api.closure = desc;
193         afb_api.itf = &so_v2_api_itf;
194         afb_api.group = binding->noconcurrency ? export : NULL;
195         if (afb_apiset_add(apiset, afb_export_apiname(desc->export), afb_api) < 0) {
196                 ERROR("binding %s can't be registered to set %s...", afb_export_apiname(desc->export), afb_apiset_name(apiset));
197                 goto error;
198         }
199         INFO("binding %s added to set %s", afb_export_apiname(desc->export), afb_apiset_name(apiset));
200         return 1;
201
202 error:
203         afb_export_destroy(export);
204         free(desc);
205
206         return -1;
207 }
208
209 int afb_api_so_v2_add(const char *path, void *handle, struct afb_apiset *apiset)
210 {
211         const struct afb_binding_v2 *binding;
212         struct afb_binding_data_v2 *data;
213
214         /* retrieves the register function */
215         binding = dlsym(handle, afb_api_so_v2_descriptor);
216         data = dlsym(handle, afb_api_so_v2_data);
217         if (!binding && !data)
218                 return 0;
219
220         INFO("binding [%s] looks like an AFB binding V2", path);
221
222         /* basic checks */
223         if (!binding || !data) {
224                 ERROR("binding [%s] incomplete symbol set: %s is missing",
225                         path, binding ? afb_api_so_v2_data : afb_api_so_v2_descriptor);
226                 goto error;
227         }
228         if (binding->api == NULL || *binding->api == 0) {
229                 ERROR("binding [%s] bad api name...", path);
230                 goto error;
231         }
232         if (!afb_api_is_valid_name(binding->api)) {
233                 ERROR("binding [%s] invalid api name...", path);
234                 goto error;
235         }
236 #if 0
237         if (binding->specification == NULL || *binding->specification == 0) {
238                 ERROR("binding [%s] bad specification...", path);
239                 goto error;
240         }
241 #endif
242         if (binding->verbs == NULL) {
243                 ERROR("binding [%s] no verbs...", path);
244                 goto error;
245         }
246
247         return afb_api_so_v2_add_binding(binding, handle, apiset, data);
248
249  error:
250         return -1;
251 }
252