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.
25 #include <afb/afb-binding-v2.h>
26 #include <json-c/json.h>
29 #include "afb-api-so-v2.h"
30 #include "afb-apiset.h"
32 #include "afb-export.h"
34 #include "afb-context.h"
35 #include "afb-api-so.h"
42 static const char afb_api_so_v2_descriptor[] = "afbBindingV2";
43 static const char afb_api_so_v2_data[] = "afbBindingV2data";
46 * Description of a binding
49 const struct afb_binding_v2 *binding; /* descriptor */
50 void *handle; /* context of dlopen */
51 struct afb_export *export; /* exportations */
54 static const struct afb_verb_v2 *search(struct api_so_v2 *desc, const char *name)
56 const struct afb_verb_v2 *verb;
58 verb = desc->binding->verbs;
59 while (verb->verb && strcasecmp(verb->verb, name))
61 return verb->verb ? verb : NULL;
65 static void call_cb(void *closure, struct afb_xreq *xreq)
67 struct api_so_v2 *desc = closure;
68 const struct afb_verb_v2 *verb;
70 xreq->request.dynapi = (void*)desc->export; /* hack: this avoids to export afb_export structure */
71 verb = search(desc, xreq->request.verb);
72 afb_xreq_call_verb_v2(xreq, verb);
75 static int service_start_cb(void *closure, int share_session, int onneed, struct afb_apiset *apiset)
77 struct api_so_v2 *desc = closure;
78 return afb_export_start(desc->export, share_session, onneed, apiset);
81 static void update_hooks_cb(void *closure)
83 struct api_so_v2 *desc = closure;
84 afb_export_update_hook(desc->export);
87 static int get_verbosity_cb(void *closure)
89 struct api_so_v2 *desc = closure;
90 return afb_export_verbosity_get(desc->export);
93 static void set_verbosity_cb(void *closure, int level)
95 struct api_so_v2 *desc = closure;
96 afb_export_verbosity_set(desc->export, level);
99 static struct json_object *make_description_openAPIv3(struct api_so_v2 *desc)
102 const struct afb_verb_v2 *verb;
103 struct json_object *r, *f, *a, *i, *p, *g;
105 r = json_object_new_object();
106 json_object_object_add(r, "openapi", json_object_new_string("3.0.0"));
108 i = json_object_new_object();
109 json_object_object_add(r, "info", i);
110 json_object_object_add(i, "title", json_object_new_string(afb_export_apiname(desc->export)));
111 json_object_object_add(i, "version", json_object_new_string("0.0.0"));
112 json_object_object_add(i, "description", json_object_new_string(desc->binding->info ?: afb_export_apiname(desc->export)));
114 p = json_object_new_object();
115 json_object_object_add(r, "paths", p);
116 verb = desc->binding->verbs;
119 strncpy(buffer + 1, verb->verb, sizeof buffer - 1);
120 buffer[sizeof buffer - 1] = 0;
121 f = json_object_new_object();
122 json_object_object_add(p, buffer, f);
123 g = json_object_new_object();
124 json_object_object_add(f, "get", g);
126 a = afb_auth_json_v2(verb->auth, verb->session);
128 json_object_object_add(g, "x-permissions", a);
130 a = json_object_new_object();
131 json_object_object_add(g, "responses", a);
132 f = json_object_new_object();
133 json_object_object_add(a, "200", f);
134 json_object_object_add(f, "description", json_object_new_string(verb->info?:verb->verb));
140 static struct json_object *describe_cb(void *closure)
142 struct api_so_v2 *desc = closure;
143 struct json_object *r = desc->binding->specification ? json_tokener_parse(desc->binding->specification) : NULL;
145 r = make_description_openAPIv3(desc);
149 static struct afb_api_itf so_v2_api_itf = {
151 .service_start = service_start_cb,
152 .update_hooks = update_hooks_cb,
153 .get_verbosity = get_verbosity_cb,
154 .set_verbosity = set_verbosity_cb,
155 .describe = describe_cb
158 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)
161 struct api_so_v2 *desc;
162 struct afb_api afb_api;
163 struct afb_export *export;
167 assert(binding->api);
168 assert(binding->verbs);
171 /* allocates the description */
172 export = afb_export_create_v2(apiset, binding->api, data, binding->init, binding->onevent);
173 desc = calloc(1, sizeof *desc);
174 if (!desc || !export) {
175 ERROR("out of memory");
178 desc->binding = binding;
179 desc->handle = handle;
180 desc->export = export;
182 /* init the binding */
183 if (binding->preinit) {
184 INFO("binding %s calling preinit function", binding->api);
185 rc = binding->preinit();
187 ERROR("binding %s preinit function failed...", afb_export_apiname(desc->export));
192 /* records the binding */
193 afb_api.closure = desc;
194 afb_api.itf = &so_v2_api_itf;
195 afb_api.group = binding->noconcurrency ? export : NULL;
196 if (afb_apiset_add(apiset, afb_export_apiname(desc->export), afb_api) < 0) {
197 ERROR("binding %s can't be registered to set %s...", afb_export_apiname(desc->export), afb_apiset_name(apiset));
200 INFO("binding %s added to set %s", afb_export_apiname(desc->export), afb_apiset_name(apiset));
204 afb_export_destroy(export);
210 int afb_api_so_v2_add(const char *path, void *handle, struct afb_apiset *apiset)
212 const struct afb_binding_v2 *binding;
213 struct afb_binding_data_v2 *data;
215 /* retrieves the register function */
216 binding = dlsym(handle, afb_api_so_v2_descriptor);
217 data = dlsym(handle, afb_api_so_v2_data);
218 if (!binding && !data)
221 INFO("binding [%s] looks like an AFB binding V2", path);
224 if (!binding || !data) {
225 ERROR("binding [%s] incomplete symbol set: %s is missing",
226 path, binding ? afb_api_so_v2_data : afb_api_so_v2_descriptor);
229 if (binding->api == NULL || *binding->api == 0) {
230 ERROR("binding [%s] bad api name...", path);
233 if (!afb_api_is_valid_name(binding->api, 1)) {
234 ERROR("binding [%s] invalid api name...", path);
238 if (binding->specification == NULL || *binding->specification == 0) {
239 ERROR("binding [%s] bad specification...", path);
243 if (binding->verbs == NULL) {
244 ERROR("binding [%s] no verbs...", path);
248 return afb_api_so_v2_add_binding(binding, handle, apiset, data);