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.
26 #include <json-c/json.h>
27 #include <afb/afb-binding-v2.h>
30 #include "afb-api-so-v2.h"
31 #include "afb-apiset.h"
33 #include "afb-export.h"
35 #include "afb-context.h"
36 #include "afb-api-so.h"
43 static const char afb_api_so_v2_descriptor[] = "afbBindingV2";
44 static const char afb_api_so_v2_data[] = "afbBindingV2data";
46 static const struct afb_verb_v2 *search(const struct afb_binding_v2 *binding, const char *name)
48 const struct afb_verb_v2 *verb;
50 verb = binding->verbs;
51 while (verb->verb && strcasecmp(verb->verb, name))
53 return verb->verb ? verb : NULL;
57 void afb_api_so_v2_process_call(const struct afb_binding_v2 *binding, struct afb_xreq *xreq)
59 const struct afb_verb_v2 *verb;
61 verb = search(binding, xreq->request.called_verb);
62 afb_xreq_call_verb_v2(xreq, verb);
65 struct json_object *afb_api_so_v2_make_description_openAPIv3(const struct afb_binding_v2 *binding, const char *apiname)
68 const struct afb_verb_v2 *verb;
69 struct json_object *r, *f, *a, *i, *p, *g;
72 if (binding->specification) {
73 r = json_tokener_parse(binding->specification);
78 r = json_object_new_object();
79 json_object_object_add(r, "openapi", json_object_new_string("3.0.0"));
81 i = json_object_new_object();
82 json_object_object_add(r, "info", i);
83 json_object_object_add(i, "title", json_object_new_string(apiname));
84 json_object_object_add(i, "version", json_object_new_string("0.0.0"));
85 json_object_object_add(i, "description", json_object_new_string(binding->info ?: apiname));
87 p = json_object_new_object();
88 json_object_object_add(r, "paths", p);
89 verb = binding->verbs;
92 strncpy(buffer + 1, verb->verb, sizeof buffer - 1);
93 buffer[sizeof buffer - 1] = 0;
94 f = json_object_new_object();
95 json_object_object_add(p, buffer, f);
96 g = json_object_new_object();
97 json_object_object_add(f, "get", g);
99 a = afb_auth_json_v2(verb->auth, verb->session);
101 json_object_object_add(g, "x-permissions", a);
103 a = json_object_new_object();
104 json_object_object_add(g, "responses", a);
105 f = json_object_new_object();
106 json_object_object_add(a, "200", f);
107 json_object_object_add(f, "description", json_object_new_string(verb->info?:verb->verb));
113 int afb_api_so_v2_add_binding(const struct afb_binding_v2 *binding, void *handle, struct afb_apiset *declare_set, struct afb_apiset * call_set, struct afb_binding_data_v2 *data)
116 struct afb_export *export;
120 assert(binding->api);
121 assert(binding->verbs);
124 /* allocates the description */
125 export = afb_export_create_v2(declare_set, call_set, binding->api, binding, data, binding->init, binding->onevent);
127 ERROR("out of memory");
131 /* records the binding */
132 if (afb_export_declare(export, binding->noconcurrency) < 0) {
133 ERROR("binding %s can't be registered to set %s...", afb_export_apiname(export), afb_apiset_name(declare_set));
136 /* init the binding */
137 if (binding->preinit) {
138 INFO("binding %s calling preinit function", binding->api);
139 rc = binding->preinit();
141 ERROR("binding %s preinit function failed...", afb_export_apiname(export));
142 afb_export_undeclare(export);
147 INFO("binding %s added to set %s", afb_export_apiname(export), afb_apiset_name(declare_set));
151 afb_export_unref(export);
156 int afb_api_so_v2_add(const char *path, void *handle, struct afb_apiset *declare_set, struct afb_apiset * call_set)
158 const struct afb_binding_v2 *binding;
159 struct afb_binding_data_v2 *data;
161 /* retrieves the register function */
162 binding = dlsym(handle, afb_api_so_v2_descriptor);
163 data = dlsym(handle, afb_api_so_v2_data);
164 if (!binding && !data)
167 INFO("binding [%s] looks like an AFB binding V2", path);
170 if (!binding || !data) {
171 ERROR("binding [%s] incomplete symbol set: %s is missing",
172 path, binding ? afb_api_so_v2_data : afb_api_so_v2_descriptor);
175 if (binding->api == NULL || *binding->api == 0) {
176 ERROR("binding [%s] bad api name...", path);
179 if (!afb_api_is_valid_name(binding->api)) {
180 ERROR("binding [%s] invalid api name...", path);
184 if (binding->verbs == NULL) {
185 ERROR("binding [%s] no verbs...", path);
189 return afb_api_so_v2_add_binding(binding, handle, declare_set, call_set, data);