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.
19 #define NO_BINDING_VERBOSE_MACRO
25 #include <json-c/json.h>
27 #include <afb/afb-binding.h>
30 #include "afb-apiset.h"
33 #include "afb-common.h"
34 #include "afb-context.h"
35 #include "afb-api-so.h"
43 static const char afb_api_so_v1_register[] = "afbBindingV1Register";
44 static const char afb_api_so_v1_service_init[] = "afbBindingV1ServiceInit";
45 static const char afb_api_so_v1_service_event[] = "afbBindingV1ServiceEvent";
48 * Description of a binding
51 struct afb_binding *binding; /* descriptor */
52 void *handle; /* context of dlopen */
53 struct afb_svc *service; /* handler for service started */
54 struct afb_ditf ditf; /* daemon interface */
57 static const struct afb_verb_desc_v1 *search(struct api_so_v1 *desc, const char *name)
59 const struct afb_verb_desc_v1 *verb;
61 verb = desc->binding->v1.verbs;
62 while (verb->name && strcasecmp(verb->name, name))
64 return verb->name ? verb : NULL;
67 static void call_cb(void *closure, struct afb_xreq *xreq)
69 const struct afb_verb_desc_v1 *verb;
70 struct api_so_v1 *desc = closure;
72 verb = search(desc, xreq->verb);
73 afb_xreq_call_verb_v1(xreq, verb);
76 static int service_start_cb(void *closure, int share_session, int onneed, struct afb_apiset *apiset)
78 int (*init)(struct afb_service service);
79 void (*onevent)(const char *event, struct json_object *object);
81 struct api_so_v1 *desc = closure;
84 if (desc->service != NULL) {
85 /* not an error when onneed */
89 /* already started: it is an error */
90 ERROR("Service %s already started", desc->binding->v1.prefix);
94 /* get the initialisation */
95 init = dlsym(desc->handle, afb_api_so_v1_service_init);
97 /* not an error when onneed */
101 /* no initialisation method */
102 ERROR("Binding %s is not a service", desc->binding->v1.prefix);
106 /* get the event handler if any */
107 onevent = dlsym(desc->handle, afb_api_so_v1_service_event);
108 desc->service = afb_svc_create(apiset, share_session, init, onevent);
109 if (desc->service == NULL) {
111 ERROR("Starting service %s failed", desc->binding->v1.prefix);
118 static void update_hooks_cb(void *closure)
120 struct api_so_v1 *desc = closure;
121 afb_ditf_update_hook(&desc->ditf);
124 static int get_verbosity_cb(void *closure)
126 struct api_so_v1 *desc = closure;
127 return desc->ditf.interface.verbosity;
130 static void set_verbosity_cb(void *closure, int level)
132 struct api_so_v1 *desc = closure;
133 desc->ditf.interface.verbosity = level;
136 struct json_object *describe_cb(void *closure)
138 struct api_so_v1 *desc = closure;
139 const struct afb_verb_desc_v1 *verb;
140 struct json_object *r, *v, *f, *a;
142 r = json_object_new_object();
143 json_object_object_add(r, "version", json_object_new_int(1));
144 json_object_object_add(r, "info", json_object_new_string(desc->binding->v1.info));
145 v = json_object_new_object();
146 json_object_object_add(r, "verbs", v);
147 verb = desc->binding->v1.verbs;
149 f = json_object_new_object();
150 a = json_object_new_array();
151 json_object_object_add(f, "name", json_object_new_string(verb->name));
152 json_object_object_add(f, "info", json_object_new_string(verb->info));
153 if (verb->session & AFB_SESSION_CLOSE)
154 json_object_array_add(a, json_object_new_string("session-close"));
155 if (verb->session & AFB_SESSION_RENEW)
156 json_object_array_add(a, json_object_new_string("session-renew"));
157 if (verb->session & AFB_SESSION_CHECK)
158 json_object_array_add(a, json_object_new_string("session-check"));
159 if (verb->session & AFB_SESSION_LOA_EQ) {
160 const char *rel = "?";
162 switch (verb->session & AFB_SESSION_LOA_EQ) {
163 case AFB_SESSION_LOA_GE: rel = ">="; break;
164 case AFB_SESSION_LOA_LE: rel = "<="; break;
165 case AFB_SESSION_LOA_EQ: rel = "=="; break;
167 snprintf(buffer, sizeof buffer, "LOA%s%d", rel, (int)((verb->session >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK));
168 json_object_array_add(a, json_object_new_string(buffer));
170 json_object_object_add(f, "flags", a);
171 json_object_object_add(v, verb->name, f);
177 static struct afb_api_itf so_v1_api_itf = {
179 .service_start = service_start_cb,
180 .update_hooks = update_hooks_cb,
181 .get_verbosity = get_verbosity_cb,
182 .set_verbosity = set_verbosity_cb,
183 .describe = describe_cb
186 int afb_api_so_v1_add(const char *path, void *handle, struct afb_apiset *apiset)
188 struct api_so_v1 *desc;
189 struct afb_binding *(*register_function) (const struct afb_binding_interface *interface);
190 struct afb_api afb_api;
192 /* retrieves the register function */
193 register_function = dlsym(handle, afb_api_so_v1_register);
194 if (!register_function)
196 INFO("binding [%s] is a valid AFB binding V1", path);
198 /* allocates the description */
199 desc = calloc(1, sizeof *desc);
201 ERROR("out of memory");
204 desc->handle = handle;
206 /* init the interface */
207 afb_ditf_init(&desc->ditf, path);
209 /* init the binding */
210 NOTICE("binding [%s] calling registering function %s", path, afb_api_so_v1_register);
211 desc->binding = register_function(&desc->ditf.interface);
212 if (desc->binding == NULL) {
213 ERROR("binding [%s] register function failed. continuing...", path);
217 /* check the returned structure */
218 if (desc->binding->type != AFB_BINDING_VERSION_1) {
219 ERROR("binding [%s] invalid type %d...", path, desc->binding->type);
222 if (desc->binding->v1.prefix == NULL || *desc->binding->v1.prefix == 0) {
223 ERROR("binding [%s] bad prefix...", path);
226 if (!afb_api_is_valid_name(desc->binding->v1.prefix)) {
227 ERROR("binding [%s] invalid prefix...", path);
230 if (desc->binding->v1.info == NULL || *desc->binding->v1.info == 0) {
231 ERROR("binding [%s] bad description...", path);
234 if (desc->binding->v1.verbs == NULL) {
235 ERROR("binding [%s] no APIs...", path);
239 /* records the binding */
240 afb_ditf_rename(&desc->ditf, desc->binding->v1.prefix);
241 afb_api.closure = desc;
242 afb_api.itf = &so_v1_api_itf;
243 if (afb_apiset_add(apiset, desc->binding->v1.prefix, afb_api) < 0) {
244 ERROR("binding [%s] can't be registered...", path);
247 NOTICE("binding %s loaded with API prefix %s", path, desc->binding->v1.prefix);