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.
18 #if defined(WITH_LEGACY_BINDING_V1)
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"
44 static const char afb_api_so_v1_register[] = "afbBindingV1Register";
45 static const char afb_api_so_v1_service_init[] = "afbBindingV1ServiceInit";
46 static const char afb_api_so_v1_service_event[] = "afbBindingV1ServiceEvent";
48 static const struct afb_verb_desc_v1 *search(struct afb_binding_v1 *binding, const char *name)
50 const struct afb_verb_desc_v1 *verb;
52 verb = binding->v1.verbs;
53 while (verb->name && strcasecmp(verb->name, name))
55 return verb->name ? verb : NULL;
58 void afb_api_so_v1_process_call(struct afb_binding_v1 *binding, struct afb_xreq *xreq)
60 const struct afb_verb_desc_v1 *verb;
62 verb = search(binding, xreq->request.called_verb);
63 afb_xreq_call_verb_v1(xreq, verb);
66 static struct json_object *addperm(struct json_object *o, struct json_object *x)
68 struct json_object *a;
73 if (!json_object_object_get_ex(o, "allOf", &a)) {
74 a = json_object_new_array();
75 json_object_array_add(a, o);
76 o = json_object_new_object();
77 json_object_object_add(o, "allOf", a);
79 json_object_array_add(a, x);
83 static struct json_object *addperm_key_val(struct json_object *o, const char *key, struct json_object *val)
85 struct json_object *x = json_object_new_object();
86 json_object_object_add(x, key, val);
90 static struct json_object *addperm_key_valstr(struct json_object *o, const char *key, const char *val)
92 return addperm_key_val(o, key, json_object_new_string(val));
95 static struct json_object *addperm_key_valint(struct json_object *o, const char *key, int val)
97 return addperm_key_val(o, key, json_object_new_int(val));
100 struct json_object *afb_api_so_v1_make_description_openAPIv3(struct afb_binding_v1 *binding, const char *apiname)
103 const struct afb_verb_desc_v1 *verb;
104 struct json_object *r, *f, *a, *i, *p, *g;
106 r = json_object_new_object();
107 json_object_object_add(r, "openapi", json_object_new_string("3.0.0"));
109 i = json_object_new_object();
110 json_object_object_add(r, "info", i);
111 json_object_object_add(i, "title", json_object_new_string(apiname));
112 json_object_object_add(i, "version", json_object_new_string("0.0.0"));
113 json_object_object_add(i, "description", json_object_new_string(binding->v1.info ?: apiname));
115 p = json_object_new_object();
116 json_object_object_add(r, "paths", p);
117 verb = binding->v1.verbs;
120 strncpy(buffer + 1, verb->name, sizeof buffer - 1);
121 buffer[sizeof buffer - 1] = 0;
122 f = json_object_new_object();
123 json_object_object_add(p, buffer, f);
124 g = json_object_new_object();
125 json_object_object_add(f, "get", g);
128 if (verb->session & AFB_SESSION_CLOSE_X1)
129 a = addperm_key_valstr(a, "session", "close");
130 if (verb->session & AFB_SESSION_CHECK_X1)
131 a = addperm_key_valstr(a, "session", "check");
132 if (verb->session & AFB_SESSION_RENEW_X1)
133 a = addperm_key_valstr(a, "token", "refresh");
134 if (verb->session & AFB_SESSION_LOA_MASK_X1)
135 a = addperm_key_valint(a, "LOA", (verb->session >> AFB_SESSION_LOA_SHIFT_X1) & AFB_SESSION_LOA_MASK_X1);
137 json_object_object_add(g, "x-permissions", a);
139 a = json_object_new_object();
140 json_object_object_add(g, "responses", a);
141 f = json_object_new_object();
142 json_object_object_add(a, "200", f);
143 json_object_object_add(f, "description", json_object_new_string(verb->info));
149 int afb_api_so_v1_add(const char *path, void *handle, struct afb_apiset *declare_set, struct afb_apiset * call_set)
151 struct afb_binding_v1 *binding; /* descriptor */
152 struct afb_binding_v1 *(*register_function) (const struct afb_binding_interface_v1 *interface);
153 int (*init)(struct afb_service_x1 service);
154 void (*onevent)(const char *event, struct json_object *object);
155 struct afb_export *export;
157 /* retrieves the register function */
158 register_function = dlsym(handle, afb_api_so_v1_register);
159 if (!register_function)
162 INFO("binding [%s] is a valid AFB binding V1", path);
164 /* allocates the description */
165 init = dlsym(handle, afb_api_so_v1_service_init);
166 onevent = dlsym(handle, afb_api_so_v1_service_event);
167 export = afb_export_create_v1(declare_set, call_set, path, init, onevent, path);
168 if (export == NULL) {
169 ERROR("binding [%s] creation failure...", path);
172 binding = afb_export_register_v1(export, register_function);
173 if (binding == NULL) {
174 ERROR("binding [%s] register failure...", path);
178 /* check the returned structure */
179 if (binding->type != AFB_BINDING_VERSION_1) {
180 ERROR("binding [%s] invalid type %d...", path, binding->type);
183 if (binding->v1.prefix == NULL || *binding->v1.prefix == 0) {
184 ERROR("binding [%s] bad prefix...", path);
187 if (!afb_api_is_valid_name(binding->v1.prefix)) {
188 ERROR("binding [%s] invalid prefix...", path);
191 if (binding->v1.info == NULL || *binding->v1.info == 0) {
192 ERROR("binding [%s] bad description...", path);
195 if (binding->v1.verbs == NULL) {
196 ERROR("binding [%s] no verbs...", path);
200 /* records the binding */
201 if (!strcmp(path, afb_export_apiname(export))) {
202 if (afb_export_rename(export, binding->v1.prefix) < 0) {
203 ERROR("binding [%s] can't be renamed to %s", path, binding->v1.prefix);
208 if (afb_export_declare(export, 0) < 0) {
209 ERROR("binding [%s] can't be registered...", path);
212 INFO("binding %s loaded with API prefix %s", path, afb_export_apiname(export));
213 afb_export_unref(export);
217 afb_export_unref(export);