2 * Copyright (C) 2016, 2017 "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 verb = search(desc, xreq->verb);
71 afb_xreq_call_verb_v2(xreq, verb);
74 static int service_start_cb(void *closure, int share_session, int onneed, struct afb_apiset *apiset)
76 struct api_so_v2 *desc = closure;
77 return afb_export_start(desc->export, share_session, onneed, apiset);
80 static void update_hooks_cb(void *closure)
82 struct api_so_v2 *desc = closure;
83 afb_export_update_hook(desc->export);
86 static int get_verbosity_cb(void *closure)
88 struct api_so_v2 *desc = closure;
89 return afb_export_verbosity_get(desc->export);
92 static void set_verbosity_cb(void *closure, int level)
94 struct api_so_v2 *desc = closure;
95 afb_export_verbosity_set(desc->export, level);
98 static struct json_object *make_description_openAPIv3(struct api_so_v2 *desc)
101 const struct afb_verb_v2 *verb;
102 struct json_object *r, *f, *a, *i, *p, *g;
104 r = json_object_new_object();
105 json_object_object_add(r, "openapi", json_object_new_string("3.0.0"));
107 i = json_object_new_object();
108 json_object_object_add(r, "info", i);
109 json_object_object_add(i, "title", json_object_new_string(afb_export_apiname(desc->export)));
110 json_object_object_add(i, "version", json_object_new_string("0.0.0"));
111 json_object_object_add(i, "description", json_object_new_string(desc->binding->info ?: afb_export_apiname(desc->export)));
113 p = json_object_new_object();
114 json_object_object_add(r, "paths", p);
115 verb = desc->binding->verbs;
118 strncpy(buffer + 1, verb->verb, sizeof buffer - 1);
119 buffer[sizeof buffer - 1] = 0;
120 f = json_object_new_object();
121 json_object_object_add(p, buffer, f);
122 g = json_object_new_object();
123 json_object_object_add(f, "get", g);
125 a = afb_auth_json_v2(verb->auth, verb->session);
127 json_object_object_add(g, "x-permissions", a);
129 a = json_object_new_object();
130 json_object_object_add(g, "responses", a);
131 f = json_object_new_object();
132 json_object_object_add(a, "200", f);
133 json_object_object_add(f, "description", json_object_new_string(verb->info?:verb->verb));
139 static struct json_object *describe_cb(void *closure)
141 struct api_so_v2 *desc = closure;
142 struct json_object *r = desc->binding->specification ? json_tokener_parse(desc->binding->specification) : NULL;
144 r = make_description_openAPIv3(desc);
148 static struct afb_api_itf so_v2_api_itf = {
150 .service_start = service_start_cb,
151 .update_hooks = update_hooks_cb,
152 .get_verbosity = get_verbosity_cb,
153 .set_verbosity = set_verbosity_cb,
154 .describe = describe_cb
157 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)
160 struct api_so_v2 *desc;
161 struct afb_api afb_api;
162 struct afb_export *export;
166 assert(binding->api);
167 assert(binding->verbs);
170 /* allocates the description */
171 export = afb_export_create_v2(apiset, binding->api, data, binding->init, binding->onevent);
172 desc = calloc(1, sizeof *desc);
173 if (!desc || !export) {
174 ERROR("out of memory");
177 desc->binding = binding;
178 desc->handle = handle;
179 desc->export = export;
181 /* init the binding */
182 if (binding->preinit) {
183 INFO("binding %s calling preinit function", binding->api);
184 rc = binding->preinit();
186 ERROR("binding %s preinit function failed...", afb_export_apiname(desc->export));
191 /* records the binding */
192 afb_api.closure = desc;
193 afb_api.itf = &so_v2_api_itf;
194 afb_api.group = binding->noconcurrency ? export : NULL;
195 if (afb_apiset_add(apiset, afb_export_apiname(desc->export), afb_api) < 0) {
196 ERROR("binding %s can't be registered to set %s...", afb_export_apiname(desc->export), afb_apiset_name(apiset));
199 INFO("binding %s added to set %s", afb_export_apiname(desc->export), afb_apiset_name(apiset));
203 afb_export_destroy(export);
209 int afb_api_so_v2_add(const char *path, void *handle, struct afb_apiset *apiset)
211 const struct afb_binding_v2 *binding;
212 struct afb_binding_data_v2 *data;
214 /* retrieves the register function */
215 binding = dlsym(handle, afb_api_so_v2_descriptor);
216 data = dlsym(handle, afb_api_so_v2_data);
217 if (!binding && !data)
220 INFO("binding [%s] looks like an AFB binding V2", path);
223 if (!binding || !data) {
224 ERROR("binding [%s] incomplete symbol set: %s is missing",
225 path, binding ? afb_api_so_v2_data : afb_api_so_v2_descriptor);
228 if (binding->api == NULL || *binding->api == 0) {
229 ERROR("binding [%s] bad api name...", path);
232 if (!afb_api_is_valid_name(binding->api)) {
233 ERROR("binding [%s] invalid api name...", path);
237 if (binding->specification == NULL || *binding->specification == 0) {
238 ERROR("binding [%s] bad specification...", path);
242 if (binding->verbs == NULL) {
243 ERROR("binding [%s] no verbs...", path);
247 return afb_api_so_v2_add_binding(binding, handle, apiset, data);