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_V1 && WITH_DYNAMIC_BINDING
28 #include <json-c/json.h>
29 #include <afb/afb-binding-v1.h>
32 #include "afb-api-so-v1.h"
33 #include "afb-apiset.h"
34 #include "afb-export.h"
35 #include "afb-common.h"
36 #include "afb-context.h"
37 #include "afb-api-so.h"
45 static const char afb_api_so_v1_register[] = "afbBindingV1Register";
46 static const char afb_api_so_v1_service_init[] = "afbBindingV1ServiceInit";
47 static const char afb_api_so_v1_service_event[] = "afbBindingV1ServiceEvent";
49 static const struct afb_verb_desc_v1 *search(struct afb_binding_v1 *binding, const char *name)
51 const struct afb_verb_desc_v1 *verb;
53 verb = binding->v1.verbs;
54 while (verb->name && strcasecmp(verb->name, name))
56 return verb->name ? verb : NULL;
59 void afb_api_so_v1_process_call(struct afb_binding_v1 *binding, struct afb_xreq *xreq)
61 const struct afb_verb_desc_v1 *verb;
63 verb = search(binding, xreq->request.called_verb);
64 afb_xreq_call_verb_v1(xreq, verb);
67 struct json_object *afb_api_so_v1_make_description_openAPIv3(struct afb_binding_v1 *binding, const char *apiname)
70 const struct afb_verb_desc_v1 *verb;
71 struct json_object *r, *f, *a, *i, *p, *g;
73 r = json_object_new_object();
74 json_object_object_add(r, "openapi", json_object_new_string("3.0.0"));
76 i = json_object_new_object();
77 json_object_object_add(r, "info", i);
78 json_object_object_add(i, "title", json_object_new_string(apiname));
79 json_object_object_add(i, "version", json_object_new_string("0.0.0"));
80 json_object_object_add(i, "description", json_object_new_string(binding->v1.info ?: apiname));
82 p = json_object_new_object();
83 json_object_object_add(r, "paths", p);
84 verb = binding->v1.verbs;
87 strncpy(buffer + 1, verb->name, sizeof buffer - 1);
88 buffer[sizeof buffer - 1] = 0;
89 f = json_object_new_object();
90 json_object_object_add(p, buffer, f);
91 g = json_object_new_object();
92 json_object_object_add(f, "get", g);
94 a = afb_auth_json_x1(verb->session);
96 json_object_object_add(g, "x-permissions", a);
98 a = json_object_new_object();
99 json_object_object_add(g, "responses", a);
100 f = json_object_new_object();
101 json_object_object_add(a, "200", f);
102 json_object_object_add(f, "description", json_object_new_string(verb->info));
108 int afb_api_so_v1_add(const char *path, void *handle, struct afb_apiset *declare_set, struct afb_apiset * call_set)
110 struct afb_binding_v1 *binding; /* descriptor */
111 struct afb_binding_v1 *(*register_function) (const struct afb_binding_interface_v1 *interface);
112 int (*init)(struct afb_service_x1 service);
113 void (*onevent)(const char *event, struct json_object *object);
114 struct afb_export *export;
116 /* retrieves the register function */
117 register_function = dlsym(handle, afb_api_so_v1_register);
118 if (!register_function)
121 INFO("binding [%s] is a valid AFB binding V1", path);
123 /* allocates the description */
124 init = dlsym(handle, afb_api_so_v1_service_init);
125 onevent = dlsym(handle, afb_api_so_v1_service_event);
126 export = afb_export_create_v1(declare_set, call_set, path, init, onevent, path);
127 if (export == NULL) {
128 ERROR("binding [%s] creation failure...", path);
131 binding = afb_export_register_v1(export, register_function);
132 if (binding == NULL) {
133 ERROR("binding [%s] register failure...", path);
137 /* check the returned structure */
138 if (binding->type != AFB_BINDING_VERSION_1) {
139 ERROR("binding [%s] invalid type %d...", path, binding->type);
142 if (binding->v1.prefix == NULL || *binding->v1.prefix == 0) {
143 ERROR("binding [%s] bad prefix...", path);
146 if (!afb_api_is_valid_name(binding->v1.prefix)) {
147 ERROR("binding [%s] invalid prefix...", path);
150 if (binding->v1.info == NULL || *binding->v1.info == 0) {
151 ERROR("binding [%s] bad description...", path);
154 if (binding->v1.verbs == NULL) {
155 ERROR("binding [%s] no verbs...", path);
159 /* records the binding */
160 if (!strcmp(path, afb_export_apiname(export))) {
161 if (afb_export_rename(export, binding->v1.prefix) < 0) {
162 ERROR("binding [%s] can't be renamed to %s", path, binding->v1.prefix);
167 if (afb_export_declare(export, 0) < 0) {
168 ERROR("binding [%s] can't be registered...", path);
171 INFO("binding %s loaded with API prefix %s", path, afb_export_apiname(export));
172 afb_export_unref(export);
176 afb_export_unref(export);