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>
31 #include "afb-common.h"
32 #include "afb-context.h"
33 #include "afb-api-so.h"
40 static const char afb_api_so_v2_descriptor[] = "afbBindingV2";
43 * Description of a binding
46 struct afb_binding_v2 *binding; /* descriptor */
47 void *handle; /* context of dlopen */
48 struct afb_svc *service; /* handler for service started */
49 struct afb_ditf ditf; /* daemon interface */
52 static const struct afb_verb_v2 *search(struct api_so_v2 *desc, const char *verb)
54 const struct afb_verb_v2 *result;
56 result = desc->binding->verbs;
57 while (result->verb && strcasecmp(result->verb, verb))
59 return result->verb ? result : NULL;
62 static void call_cb(void *closure, struct afb_xreq *xreq)
64 struct api_so_v2 *desc = closure;
65 const struct afb_verb_v2 *verb;
67 verb = search(desc, xreq->verb);
69 afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, desc->binding->api);
71 afb_xreq_call(xreq, verb->session, verb->callback);
74 static int service_start_cb(void *closure, int share_session, int onneed)
76 int (*start)(const struct afb_binding_interface *interface, struct afb_service service);
77 void (*onevent)(const char *event, struct json_object *object);
79 struct api_so_v2 *desc = closure;
82 if (desc->service != NULL) {
83 /* not an error when onneed */
87 /* already started: it is an error */
88 ERROR("Service %s already started", desc->binding->api);
92 /* get the initialisation */
93 start = desc->binding->start;
95 /* not an error when onneed */
99 /* no initialisation method */
100 ERROR("Binding %s is not a service", desc->binding->api);
104 /* get the event handler if any */
105 onevent = desc->binding->onevent;
106 desc->service = afb_svc_create_v2(share_session, onevent, start, &desc->ditf.interface);
107 if (desc->service == NULL) {
109 ERROR("Starting service %s failed", desc->binding->api);
116 static void update_hooks_cb(void *closure)
118 struct api_so_v2 *desc = closure;
119 afb_ditf_update_hook(&desc->ditf);
122 static int get_verbosity_cb(void *closure)
124 struct api_so_v2 *desc = closure;
125 return desc->ditf.interface.verbosity;
128 static void set_verbosity_cb(void *closure, int level)
130 struct api_so_v2 *desc = closure;
131 desc->ditf.interface.verbosity = level;
134 static struct afb_api_itf so_v2_api_itf = {
136 .service_start = service_start_cb,
137 .update_hooks = update_hooks_cb,
138 .get_verbosity = get_verbosity_cb,
139 .set_verbosity = set_verbosity_cb
142 int afb_api_so_v2_add(const char *path, void *handle)
145 struct api_so_v2 *desc;
146 struct afb_binding_v2 *binding;
147 struct afb_api afb_api;
149 /* retrieves the register function */
150 binding = dlsym(handle, afb_api_so_v2_descriptor);
154 INFO("binding [%s] looks like an AFB binding V2", path);
157 if (binding->api == NULL || *binding->api == 0) {
158 ERROR("binding [%s] bad api name...", path);
161 if (!afb_apis_is_valid_api_name(binding->api)) {
162 ERROR("binding [%s] invalid api name...", path);
165 if (binding->specification == NULL || *binding->specification == 0) {
166 ERROR("binding [%s] bad specification...", path);
169 if (binding->verbs == NULL) {
170 ERROR("binding [%s] no verbs...", path);
174 /* allocates the description */
175 desc = calloc(1, sizeof *desc);
177 ERROR("out of memory");
180 desc->binding = binding;
181 desc->handle = handle;
183 /* init the interface */
184 afb_ditf_init(&desc->ditf, binding->api);
186 /* for log purpose, a fake binding is needed here */
188 /* init the binding */
190 NOTICE("binding %s [%s] calling init function", binding->api, path);
191 rc = binding->init(&desc->ditf.interface);
193 ERROR("binding %s [%s] initialisation function failed...", binding->api, path);
198 /* records the binding */
199 afb_api.closure = desc;
200 afb_api.itf = &so_v2_api_itf;
201 if (afb_apis_add(binding->api, afb_api) < 0) {
202 ERROR("binding [%s] can't be registered...", path);
205 NOTICE("binding %s loaded with API prefix %s", path, binding->api);