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 <afb/afb-binding.h>
28 #include "afb-apiset.h"
31 #include "afb-common.h"
32 #include "afb-context.h"
33 #include "afb-api-so.h"
41 static const char afb_api_so_v1_register[] = "afbBindingV1Register";
42 static const char afb_api_so_v1_service_init[] = "afbBindingV1ServiceInit";
43 static const char afb_api_so_v1_service_event[] = "afbBindingV1ServiceEvent";
46 * Description of a binding
49 struct afb_binding *binding; /* descriptor */
50 void *handle; /* context of dlopen */
51 struct afb_svc *service; /* handler for service started */
52 struct afb_ditf ditf; /* daemon interface */
55 static const struct afb_verb_desc_v1 *search(struct api_so_v1 *desc, const char *name)
57 const struct afb_verb_desc_v1 *verb;
59 verb = desc->binding->v1.verbs;
60 while (verb->name && strcasecmp(verb->name, name))
62 return verb->name ? verb : NULL;
65 static void call_cb(void *closure, struct afb_xreq *xreq)
67 const struct afb_verb_desc_v1 *verb;
68 struct api_so_v1 *desc = closure;
70 verb = search(desc, xreq->verb);
72 afb_xreq_fail_unknown_verb(xreq);
74 if (!xreq_session_check_apply(xreq, verb->session))
75 afb_xreq_call(xreq, verb->callback);
78 static int service_start_cb(void *closure, int share_session, int onneed, struct afb_apiset *apiset)
80 int (*init)(struct afb_service service);
81 void (*onevent)(const char *event, struct json_object *object);
83 struct api_so_v1 *desc = closure;
86 if (desc->service != NULL) {
87 /* not an error when onneed */
91 /* already started: it is an error */
92 ERROR("Service %s already started", desc->binding->v1.prefix);
96 /* get the initialisation */
97 init = dlsym(desc->handle, afb_api_so_v1_service_init);
99 /* not an error when onneed */
103 /* no initialisation method */
104 ERROR("Binding %s is not a service", desc->binding->v1.prefix);
108 /* get the event handler if any */
109 onevent = dlsym(desc->handle, afb_api_so_v1_service_event);
110 desc->service = afb_svc_create(apiset, share_session, init, onevent);
111 if (desc->service == NULL) {
113 ERROR("Starting service %s failed", desc->binding->v1.prefix);
120 static void update_hooks_cb(void *closure)
122 struct api_so_v1 *desc = closure;
123 afb_ditf_update_hook(&desc->ditf);
126 static int get_verbosity_cb(void *closure)
128 struct api_so_v1 *desc = closure;
129 return desc->ditf.interface.verbosity;
132 static void set_verbosity_cb(void *closure, int level)
134 struct api_so_v1 *desc = closure;
135 desc->ditf.interface.verbosity = level;
138 static struct afb_api_itf so_v1_api_itf = {
140 .service_start = service_start_cb,
141 .update_hooks = update_hooks_cb,
142 .get_verbosity = get_verbosity_cb,
143 .set_verbosity = set_verbosity_cb
146 int afb_api_so_v1_add(const char *path, void *handle, struct afb_apiset *apiset)
148 struct api_so_v1 *desc;
149 struct afb_binding *(*register_function) (const struct afb_binding_interface *interface);
150 struct afb_api afb_api;
152 /* retrieves the register function */
153 register_function = dlsym(handle, afb_api_so_v1_register);
154 if (!register_function)
156 INFO("binding [%s] is a valid AFB binding V1", path);
158 /* allocates the description */
159 desc = calloc(1, sizeof *desc);
161 ERROR("out of memory");
164 desc->handle = handle;
166 /* init the interface */
167 afb_ditf_init(&desc->ditf, path);
169 /* init the binding */
170 NOTICE("binding [%s] calling registering function %s", path, afb_api_so_v1_register);
171 desc->binding = register_function(&desc->ditf.interface);
172 if (desc->binding == NULL) {
173 ERROR("binding [%s] register function failed. continuing...", path);
177 /* check the returned structure */
178 if (desc->binding->type != AFB_BINDING_VERSION_1) {
179 ERROR("binding [%s] invalid type %d...", path, desc->binding->type);
182 if (desc->binding->v1.prefix == NULL || *desc->binding->v1.prefix == 0) {
183 ERROR("binding [%s] bad prefix...", path);
186 if (!afb_api_is_valid_name(desc->binding->v1.prefix)) {
187 ERROR("binding [%s] invalid prefix...", path);
190 if (desc->binding->v1.info == NULL || *desc->binding->v1.info == 0) {
191 ERROR("binding [%s] bad description...", path);
194 if (desc->binding->v1.verbs == NULL) {
195 ERROR("binding [%s] no APIs...", path);
199 /* records the binding */
200 afb_ditf_rename(&desc->ditf, desc->binding->v1.prefix);
201 afb_api.closure = desc;
202 afb_api.itf = &so_v1_api_itf;
203 if (afb_apiset_add(apiset, desc->binding->v1.prefix, afb_api) < 0) {
204 ERROR("binding [%s] can't be registered...", path);
207 NOTICE("binding %s loaded with API prefix %s", path, desc->binding->v1.prefix);