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"
32 #include "afb-common.h"
33 #include "afb-context.h"
34 #include "afb-api-so.h"
41 static const char afb_api_so_v2_descriptor[] = "afbBindingV2";
44 * Description of a binding
47 struct afb_binding_v2 *binding; /* descriptor */
48 void *handle; /* context of dlopen */
49 struct afb_svc *service; /* handler for service started */
50 struct afb_ditf ditf; /* daemon interface */
53 static const struct afb_verb_v2 *search(struct api_so_v2 *desc, const char *verb)
55 const struct afb_verb_v2 *result;
57 result = desc->binding->verbs;
58 while (result->verb && strcasecmp(result->verb, verb))
60 return result->verb ? result : NULL;
63 static void call_cb(void *closure, struct afb_xreq *xreq)
65 struct api_so_v2 *desc = closure;
66 const struct afb_verb_v2 *verb;
68 verb = search(desc, xreq->verb);
70 afb_xreq_fail_unknown_verb(xreq);
72 if (!xreq_session_check_apply(xreq, verb->session))
73 afb_xreq_call(xreq, verb->callback);
76 static int service_start_cb(void *closure, int share_session, int onneed, struct afb_apiset *apiset)
78 int (*start)(const struct afb_binding_interface *interface, struct afb_service service);
79 void (*onevent)(const char *event, struct json_object *object);
81 struct api_so_v2 *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->api);
94 /* get the initialisation */
95 start = desc->binding->start;
97 /* not an error when onneed */
101 /* no initialisation method */
102 ERROR("Binding %s is not a service", desc->binding->api);
106 /* get the event handler if any */
107 onevent = desc->binding->onevent;
108 desc->service = afb_svc_create_v2(apiset, share_session, onevent, start, &desc->ditf.interface);
109 if (desc->service == NULL) {
111 ERROR("Starting service %s failed", desc->binding->api);
118 static void update_hooks_cb(void *closure)
120 struct api_so_v2 *desc = closure;
121 afb_ditf_update_hook(&desc->ditf);
124 static int get_verbosity_cb(void *closure)
126 struct api_so_v2 *desc = closure;
127 return desc->ditf.interface.verbosity;
130 static void set_verbosity_cb(void *closure, int level)
132 struct api_so_v2 *desc = closure;
133 desc->ditf.interface.verbosity = level;
136 static struct afb_api_itf so_v2_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_v2_add(const char *path, void *handle, struct afb_apiset *apiset)
147 struct api_so_v2 *desc;
148 struct afb_binding_v2 *binding;
149 struct afb_api afb_api;
151 /* retrieves the register function */
152 binding = dlsym(handle, afb_api_so_v2_descriptor);
156 INFO("binding [%s] looks like an AFB binding V2", path);
159 if (binding->api == NULL || *binding->api == 0) {
160 ERROR("binding [%s] bad api name...", path);
163 if (!afb_api_is_valid_name(binding->api)) {
164 ERROR("binding [%s] invalid api name...", path);
167 if (binding->specification == NULL || *binding->specification == 0) {
168 ERROR("binding [%s] bad specification...", path);
171 if (binding->verbs == NULL) {
172 ERROR("binding [%s] no verbs...", path);
176 /* allocates the description */
177 desc = calloc(1, sizeof *desc);
179 ERROR("out of memory");
182 desc->binding = binding;
183 desc->handle = handle;
185 /* init the interface */
186 afb_ditf_init(&desc->ditf, binding->api);
188 /* for log purpose, a fake binding is needed here */
190 /* init the binding */
192 NOTICE("binding %s [%s] calling init function", binding->api, path);
193 rc = binding->init(&desc->ditf.interface);
195 ERROR("binding %s [%s] initialisation function failed...", binding->api, path);
200 /* records the binding */
201 afb_api.closure = desc;
202 afb_api.itf = &so_v2_api_itf;
203 if (afb_apiset_add(apiset, binding->api, afb_api) < 0) {
204 ERROR("binding [%s] can't be registered...", path);
207 NOTICE("binding %s loaded with API prefix %s", path, binding->api);