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>
30 #include "afb-common.h"
31 #include "afb-context.h"
32 #include "afb-api-so.h"
40 static const char afb_api_so_v1_register[] = "afbBindingV1Register";
41 static const char afb_api_so_v1_service_init[] = "afbBindingV1ServiceInit";
42 static const char afb_api_so_v1_service_event[] = "afbBindingV1ServiceEvent";
45 * Description of a binding
48 struct afb_binding *binding; /* descriptor */
49 void *handle; /* context of dlopen */
50 struct afb_svc *service; /* handler for service started */
51 struct afb_ditf ditf; /* daemon interface */
54 static const struct afb_verb_desc_v1 *search(struct api_so_v1 *desc, const char *name)
56 const struct afb_verb_desc_v1 *verb;
58 verb = desc->binding->v1.verbs;
59 while (verb->name && strcasecmp(verb->name, name))
61 return verb->name ? verb : NULL;
64 static void call_cb(void *closure, struct afb_xreq *xreq)
66 const struct afb_verb_desc_v1 *verb;
67 struct api_so_v1 *desc = closure;
69 verb = search(desc, xreq->verb);
71 afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, desc->binding->v1.prefix);
73 afb_xreq_call(xreq, verb->session, verb->callback);
76 static int service_start_cb(void *closure, int share_session, int onneed)
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(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 static struct afb_api_itf so_v1_api_itf = {
138 .service_start = service_start_cb,
139 .update_hooks = update_hooks_cb,
140 .get_verbosity = get_verbosity_cb,
141 .set_verbosity = set_verbosity_cb
144 int afb_api_so_v1_add(const char *path, void *handle)
146 struct api_so_v1 *desc;
147 struct afb_binding *(*register_function) (const struct afb_binding_interface *interface);
148 struct afb_api afb_api;
150 /* retrieves the register function */
151 register_function = dlsym(handle, afb_api_so_v1_register);
152 if (!register_function)
154 INFO("binding [%s] is a valid AFB binding V1", path);
156 /* allocates the description */
157 desc = calloc(1, sizeof *desc);
159 ERROR("out of memory");
162 desc->handle = handle;
164 /* init the interface */
165 afb_ditf_init(&desc->ditf, path);
167 /* init the binding */
168 NOTICE("binding [%s] calling registering function %s", path, afb_api_so_v1_register);
169 desc->binding = register_function(&desc->ditf.interface);
170 if (desc->binding == NULL) {
171 ERROR("binding [%s] register function failed. continuing...", path);
175 /* check the returned structure */
176 if (desc->binding->type != AFB_BINDING_VERSION_1) {
177 ERROR("binding [%s] invalid type %d...", path, desc->binding->type);
180 if (desc->binding->v1.prefix == NULL || *desc->binding->v1.prefix == 0) {
181 ERROR("binding [%s] bad prefix...", path);
184 if (!afb_apis_is_valid_api_name(desc->binding->v1.prefix)) {
185 ERROR("binding [%s] invalid prefix...", path);
188 if (desc->binding->v1.info == NULL || *desc->binding->v1.info == 0) {
189 ERROR("binding [%s] bad description...", path);
192 if (desc->binding->v1.verbs == NULL) {
193 ERROR("binding [%s] no APIs...", path);
197 /* records the binding */
198 afb_ditf_rename(&desc->ditf, desc->binding->v1.prefix);
199 afb_api.closure = desc;
200 afb_api.itf = &so_v1_api_itf;
201 if (afb_apis_add(desc->binding->v1.prefix, afb_api) < 0) {
202 ERROR("binding [%s] can't be registered...", path);
205 NOTICE("binding %s loaded with API prefix %s", path, desc->binding->v1.prefix);