Update copyright dates
[src/app-framework-binder.git] / src / afb-api-so-v2.c
1 /*
2  * Copyright (C) 2015-2020 "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 #if WITH_LEGACY_BINDING_V2 && WITH_DYNAMIC_BINDING
19
20 #define _GNU_SOURCE
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <dlfcn.h>
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <errno.h>
28
29 #include <json-c/json.h>
30 #include <afb/afb-binding-v2.h>
31
32 #include "afb-api.h"
33 #include "afb-api-so-v2.h"
34 #include "afb-apiset.h"
35 #include "afb-auth.h"
36 #include "afb-export.h"
37 #include "afb-evt.h"
38 #include "afb-context.h"
39 #include "afb-api-so.h"
40 #include "afb-xreq.h"
41 #include "sig-monitor.h"
42 #include "verbose.h"
43
44 /*
45  * names of symbols
46  */
47 static const char afb_api_so_v2_descriptor[] = "afbBindingV2";
48 static const char afb_api_so_v2_data[] = "afbBindingV2data";
49
50 struct preinit
51 {
52         int return_code;
53         const struct afb_binding_v2 *binding;
54 };
55
56 static const struct afb_verb_v2 *search(const struct afb_binding_v2 *binding, const char *name)
57 {
58         const struct afb_verb_v2 *verb;
59
60         verb = binding->verbs;
61         while (verb->verb && strcasecmp(verb->verb, name))
62                 verb++;
63         return verb->verb ? verb : NULL;
64         return NULL;
65 }
66
67 void afb_api_so_v2_process_call(const struct afb_binding_v2 *binding, struct afb_xreq *xreq)
68 {
69         const struct afb_verb_v2 *verb;
70
71         verb = search(binding, xreq->request.called_verb);
72         afb_xreq_call_verb_v2(xreq, verb);
73 }
74
75 struct json_object *afb_api_so_v2_make_description_openAPIv3(const struct afb_binding_v2 *binding, const char *apiname)
76 {
77         char buffer[256];
78         const struct afb_verb_v2 *verb;
79         struct json_object *r, *f, *a, *i, *p, *g;
80         enum json_tokener_error jerr;
81
82         if (binding->specification) {
83                 r = json_tokener_parse_verbose(binding->specification, &jerr);
84                 if (jerr == json_tokener_success)
85                         return r;
86         }
87
88         r = json_object_new_object();
89         json_object_object_add(r, "openapi", json_object_new_string("3.0.0"));
90
91         i = json_object_new_object();
92         json_object_object_add(r, "info", i);
93         json_object_object_add(i, "title", json_object_new_string(apiname));
94         json_object_object_add(i, "version", json_object_new_string("0.0.0"));
95         json_object_object_add(i, "description", json_object_new_string(binding->info ?: apiname));
96
97         p = json_object_new_object();
98         json_object_object_add(r, "paths", p);
99         verb = binding->verbs;
100         while (verb->verb) {
101                 buffer[0] = '/';
102                 strncpy(buffer + 1, verb->verb, sizeof buffer - 1);
103                 buffer[sizeof buffer - 1] = 0;
104                 f = json_object_new_object();
105                 json_object_object_add(p, buffer, f);
106                 g = json_object_new_object();
107                 json_object_object_add(f, "get", g);
108
109                 a = afb_auth_json_x2(verb->auth, verb->session);
110                 if (a)
111                         json_object_object_add(g, "x-permissions", a);
112
113                 a = json_object_new_object();
114                 json_object_object_add(g, "responses", a);
115                 f = json_object_new_object();
116                 json_object_object_add(a, "200", f);
117                 json_object_object_add(f, "description", json_object_new_string(verb->info?:verb->verb));
118                 verb++;
119         }
120         return r;
121 }
122
123 static void do_preinit(int sig, void *closure)
124 {
125         struct preinit *preinit = closure;
126
127         if (!sig)
128                 preinit->return_code = preinit->binding->preinit();
129         else {
130                 errno = EINTR;
131                 preinit->return_code = -1;
132         }
133 };
134
135 int afb_api_so_v2_add_binding(
136                 const struct afb_binding_v2 *binding,
137                 void *handle,
138                 struct afb_apiset *declare_set,
139                 struct afb_apiset * call_set,
140                 struct afb_binding_data_v2 *data,
141                 const char *path)
142 {
143         int rc;
144         struct afb_export *export;
145         struct preinit preinit;
146
147         /* basic checks */
148         assert(binding);
149         assert(binding->api);
150         assert(binding->verbs);
151         assert(data);
152
153         /* allocates the description */
154         export = afb_export_create_v2(declare_set, call_set, binding->api, binding, data, binding->init, binding->onevent, path);
155         if (!export) {
156                 ERROR("out of memory");
157                 goto error;
158         }
159
160         /* records the binding */
161         if (afb_export_declare(export, binding->noconcurrency) < 0) {
162                 ERROR("binding %s can't be registered to set %s...", afb_export_apiname(export), afb_apiset_name(declare_set));
163                 goto error;
164         }
165         /* init the binding */
166         if (binding->preinit) {
167                 INFO("binding %s calling preinit function", binding->api);
168                 preinit.binding = binding;
169                 sig_monitor(0, do_preinit, &preinit);
170                 rc = preinit.return_code;
171                 if (rc < 0) {
172                         ERROR("binding %s preinit function failed...", afb_export_apiname(export));
173                         afb_export_undeclare(export);
174                         goto error;
175                 }
176         }
177
178         INFO("binding %s added to set %s", afb_export_apiname(export), afb_apiset_name(declare_set));
179         return 1;
180
181 error:
182         afb_export_unref(export);
183
184         return -1;
185 }
186
187 int afb_api_so_v2_add(const char *path, void *handle, struct afb_apiset *declare_set, struct afb_apiset * call_set)
188 {
189         const struct afb_binding_v2 *binding;
190         struct afb_binding_data_v2 *data;
191
192         /* retrieves the register function */
193         binding = dlsym(handle, afb_api_so_v2_descriptor);
194         data = dlsym(handle, afb_api_so_v2_data);
195         if (!binding && !data)
196                 return 0;
197
198         INFO("binding [%s] looks like an AFB binding V2", path);
199
200         /* basic checks */
201         if (!binding || !data) {
202                 ERROR("binding [%s] incomplete symbol set: %s is missing",
203                         path, binding ? afb_api_so_v2_data : afb_api_so_v2_descriptor);
204                 goto error;
205         }
206         if (binding->api == NULL || *binding->api == 0) {
207                 ERROR("binding [%s] bad api name...", path);
208                 goto error;
209         }
210         if (!afb_api_is_valid_name(binding->api)) {
211                 ERROR("binding [%s] invalid api name...", path);
212                 goto error;
213         }
214
215         if (binding->verbs == NULL) {
216                 ERROR("binding [%s] no verbs...", path);
217                 goto error;
218         }
219
220         return afb_api_so_v2_add_binding(binding, handle, declare_set, call_set, data, path);
221
222  error:
223         return -1;
224 }
225
226 #endif