2 * Copyright (C) 2016, 2017, 2018 "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.
27 #include <json-c/json.h>
28 #include <afb/afb-binding-v2.h>
31 #include "afb-api-so-v2.h"
32 #include "afb-apiset.h"
34 #include "afb-export.h"
36 #include "afb-context.h"
37 #include "afb-api-so.h"
39 #include "sig-monitor.h"
45 static const char afb_api_so_v2_descriptor[] = "afbBindingV2";
46 static const char afb_api_so_v2_data[] = "afbBindingV2data";
51 const struct afb_binding_v2 *binding;
54 static const struct afb_verb_v2 *search(const struct afb_binding_v2 *binding, const char *name)
56 const struct afb_verb_v2 *verb;
58 verb = binding->verbs;
59 while (verb->verb && strcasecmp(verb->verb, name))
61 return verb->verb ? verb : NULL;
65 void afb_api_so_v2_process_call(const struct afb_binding_v2 *binding, struct afb_xreq *xreq)
67 const struct afb_verb_v2 *verb;
69 verb = search(binding, xreq->request.called_verb);
70 afb_xreq_call_verb_v2(xreq, verb);
73 struct json_object *afb_api_so_v2_make_description_openAPIv3(const struct afb_binding_v2 *binding, const char *apiname)
76 const struct afb_verb_v2 *verb;
77 struct json_object *r, *f, *a, *i, *p, *g;
78 enum json_tokener_error jerr;
80 if (binding->specification) {
81 r = json_tokener_parse_verbose(binding->specification, &jerr);
82 if (jerr == json_tokener_success)
86 r = json_object_new_object();
87 json_object_object_add(r, "openapi", json_object_new_string("3.0.0"));
89 i = json_object_new_object();
90 json_object_object_add(r, "info", i);
91 json_object_object_add(i, "title", json_object_new_string(apiname));
92 json_object_object_add(i, "version", json_object_new_string("0.0.0"));
93 json_object_object_add(i, "description", json_object_new_string(binding->info ?: apiname));
95 p = json_object_new_object();
96 json_object_object_add(r, "paths", p);
97 verb = binding->verbs;
100 strncpy(buffer + 1, verb->verb, sizeof buffer - 1);
101 buffer[sizeof buffer - 1] = 0;
102 f = json_object_new_object();
103 json_object_object_add(p, buffer, f);
104 g = json_object_new_object();
105 json_object_object_add(f, "get", g);
107 a = afb_auth_json_v2(verb->auth, verb->session);
109 json_object_object_add(g, "x-permissions", a);
111 a = json_object_new_object();
112 json_object_object_add(g, "responses", a);
113 f = json_object_new_object();
114 json_object_object_add(a, "200", f);
115 json_object_object_add(f, "description", json_object_new_string(verb->info?:verb->verb));
121 static void do_preinit(int sig, void *closure)
123 struct preinit *preinit = closure;
126 preinit->return_code = preinit->binding->preinit();
129 preinit->return_code = -1;
133 int afb_api_so_v2_add_binding(
134 const struct afb_binding_v2 *binding,
136 struct afb_apiset *declare_set,
137 struct afb_apiset * call_set,
138 struct afb_binding_data_v2 *data,
142 struct afb_export *export;
143 struct preinit preinit;
147 assert(binding->api);
148 assert(binding->verbs);
151 /* allocates the description */
152 export = afb_export_create_v2(declare_set, call_set, binding->api, binding, data, binding->init, binding->onevent, path);
154 ERROR("out of memory");
158 /* records the binding */
159 if (afb_export_declare(export, binding->noconcurrency) < 0) {
160 ERROR("binding %s can't be registered to set %s...", afb_export_apiname(export), afb_apiset_name(declare_set));
163 /* init the binding */
164 if (binding->preinit) {
165 INFO("binding %s calling preinit function", binding->api);
166 preinit.binding = binding;
167 sig_monitor(0, do_preinit, &preinit);
168 rc = preinit.return_code;
170 ERROR("binding %s preinit function failed...", afb_export_apiname(export));
171 afb_export_undeclare(export);
176 INFO("binding %s added to set %s", afb_export_apiname(export), afb_apiset_name(declare_set));
180 afb_export_unref(export);
185 int afb_api_so_v2_add(const char *path, void *handle, struct afb_apiset *declare_set, struct afb_apiset * call_set)
187 const struct afb_binding_v2 *binding;
188 struct afb_binding_data_v2 *data;
190 /* retrieves the register function */
191 binding = dlsym(handle, afb_api_so_v2_descriptor);
192 data = dlsym(handle, afb_api_so_v2_data);
193 if (!binding && !data)
196 INFO("binding [%s] looks like an AFB binding V2", path);
199 if (!binding || !data) {
200 ERROR("binding [%s] incomplete symbol set: %s is missing",
201 path, binding ? afb_api_so_v2_data : afb_api_so_v2_descriptor);
204 if (binding->api == NULL || *binding->api == 0) {
205 ERROR("binding [%s] bad api name...", path);
208 if (!afb_api_is_valid_name(binding->api)) {
209 ERROR("binding [%s] invalid api name...", path);
213 if (binding->verbs == NULL) {
214 ERROR("binding [%s] no verbs...", path);
218 return afb_api_so_v2_add_binding(binding, handle, declare_set, call_set, data, path);