2 * Copyright (C) 2015-2020 "IoT.bzh"
3 * Author José Bollo <jose.bollo@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #if WITH_LEGACY_BINDING_V2 && WITH_DYNAMIC_BINDING
29 #include <json-c/json.h>
30 #include <afb/afb-binding-v2.h>
33 #include "afb-api-so-v2.h"
34 #include "afb-apiset.h"
36 #include "afb-export.h"
38 #include "afb-context.h"
39 #include "afb-api-so.h"
41 #include "sig-monitor.h"
47 static const char afb_api_so_v2_descriptor[] = "afbBindingV2";
48 static const char afb_api_so_v2_data[] = "afbBindingV2data";
53 const struct afb_binding_v2 *binding;
56 static const struct afb_verb_v2 *search(const struct afb_binding_v2 *binding, const char *name)
58 const struct afb_verb_v2 *verb;
60 verb = binding->verbs;
61 while (verb->verb && strcasecmp(verb->verb, name))
63 return verb->verb ? verb : NULL;
67 void afb_api_so_v2_process_call(const struct afb_binding_v2 *binding, struct afb_xreq *xreq)
69 const struct afb_verb_v2 *verb;
71 verb = search(binding, xreq->request.called_verb);
72 afb_xreq_call_verb_v2(xreq, verb);
75 struct json_object *afb_api_so_v2_make_description_openAPIv3(const struct afb_binding_v2 *binding, const char *apiname)
78 const struct afb_verb_v2 *verb;
79 struct json_object *r, *f, *a, *i, *p, *g;
80 enum json_tokener_error jerr;
82 if (binding->specification) {
83 r = json_tokener_parse_verbose(binding->specification, &jerr);
84 if (jerr == json_tokener_success)
88 r = json_object_new_object();
89 json_object_object_add(r, "openapi", json_object_new_string("3.0.0"));
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));
97 p = json_object_new_object();
98 json_object_object_add(r, "paths", p);
99 verb = binding->verbs;
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);
109 a = afb_auth_json_x2(verb->auth, verb->session);
111 json_object_object_add(g, "x-permissions", a);
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));
123 static void do_preinit(int sig, void *closure)
125 struct preinit *preinit = closure;
128 preinit->return_code = preinit->binding->preinit();
131 preinit->return_code = -1;
135 int afb_api_so_v2_add_binding(
136 const struct afb_binding_v2 *binding,
138 struct afb_apiset *declare_set,
139 struct afb_apiset * call_set,
140 struct afb_binding_data_v2 *data,
144 struct afb_export *export;
145 struct preinit preinit;
149 assert(binding->api);
150 assert(binding->verbs);
153 /* allocates the description */
154 export = afb_export_create_v2(declare_set, call_set, binding->api, binding, data, binding->init, binding->onevent, path);
156 ERROR("out of memory");
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));
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;
172 ERROR("binding %s preinit function failed...", afb_export_apiname(export));
173 afb_export_undeclare(export);
178 INFO("binding %s added to set %s", afb_export_apiname(export), afb_apiset_name(declare_set));
182 afb_export_unref(export);
187 int afb_api_so_v2_add(const char *path, void *handle, struct afb_apiset *declare_set, struct afb_apiset * call_set)
189 const struct afb_binding_v2 *binding;
190 struct afb_binding_data_v2 *data;
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)
198 INFO("binding [%s] looks like an AFB binding V2", path);
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);
206 if (binding->api == NULL || *binding->api == 0) {
207 ERROR("binding [%s] bad api name...", path);
210 if (!afb_api_is_valid_name(binding->api)) {
211 ERROR("binding [%s] invalid api name...", path);
215 if (binding->verbs == NULL) {
216 ERROR("binding [%s] no verbs...", path);
220 return afb_api_so_v2_add_binding(binding, handle, declare_set, call_set, data, path);