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>
26 #include <json-c/json.h>
29 #include "afb-apiset.h"
33 #include "afb-common.h"
34 #include "afb-context.h"
35 #include "afb-api-so.h"
42 static const char afb_api_so_v2_descriptor[] = "afbBindingV2";
45 * Description of a binding
48 const struct afb_binding_v2 *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_v2 *search(struct api_so_v2 *desc, const char *verb)
56 const struct afb_verb_v2 *result;
58 result = desc->binding->verbs;
59 while (result->verb && strcasecmp(result->verb, verb))
61 return result->verb ? result : NULL;
64 static void call_cb(void *closure, struct afb_xreq *xreq)
66 struct api_so_v2 *desc = closure;
67 const struct afb_verb_v2 *verb;
69 verb = search(desc, xreq->verb);
71 afb_xreq_fail_unknown_verb(xreq);
73 if (!xreq_session_check_apply(xreq, verb->session))
74 afb_xreq_call(xreq, verb->callback);
77 static int service_start_cb(void *closure, int share_session, int onneed, struct afb_apiset *apiset)
79 int (*start)(const struct afb_binding_interface *interface, struct afb_service service);
80 void (*onevent)(const char *event, struct json_object *object);
82 struct api_so_v2 *desc = closure;
85 if (desc->service != NULL) {
86 /* not an error when onneed */
90 /* already started: it is an error */
91 ERROR("Service %s already started", desc->binding->api);
95 /* get the initialisation */
96 start = desc->binding->start;
98 /* not an error when onneed */
102 /* no initialisation method */
103 ERROR("Binding %s is not a service", desc->binding->api);
107 /* get the event handler if any */
108 onevent = desc->binding->onevent;
109 desc->service = afb_svc_create_v2(apiset, share_session, onevent, start, &desc->ditf.interface);
110 if (desc->service == NULL) {
112 ERROR("Starting service %s failed", desc->binding->api);
119 static void update_hooks_cb(void *closure)
121 struct api_so_v2 *desc = closure;
122 afb_ditf_update_hook(&desc->ditf);
125 static int get_verbosity_cb(void *closure)
127 struct api_so_v2 *desc = closure;
128 return desc->ditf.interface.verbosity;
131 static void set_verbosity_cb(void *closure, int level)
133 struct api_so_v2 *desc = closure;
134 desc->ditf.interface.verbosity = level;
137 static struct json_object *describe_cb(void *closure)
139 struct api_so_v2 *desc = closure;
140 return desc->binding->specification ? json_tokener_parse(desc->binding->specification) : NULL;
143 static struct afb_api_itf so_v2_api_itf = {
145 .service_start = service_start_cb,
146 .update_hooks = update_hooks_cb,
147 .get_verbosity = get_verbosity_cb,
148 .set_verbosity = set_verbosity_cb,
149 .describe = describe_cb
153 int afb_api_so_v2_add_binding(const struct afb_binding_v2 *binding, void *handle, struct afb_apiset *apiset)
156 struct api_so_v2 *desc;
157 struct afb_api afb_api;
160 assert(binding->api);
161 assert(binding->specification);
162 assert(binding->verbs);
164 /* allocates the description */
165 desc = calloc(1, sizeof *desc);
167 ERROR("out of memory");
170 desc->binding = binding;
171 desc->handle = handle;
173 /* init the interface */
174 afb_ditf_init(&desc->ditf, binding->api);
176 /* init the binding */
178 INFO("binding %s calling init function", binding->api);
179 rc = binding->init(&desc->ditf.interface);
181 ERROR("binding %s initialisation function failed...", binding->api);
186 /* records the binding */
187 afb_api.closure = desc;
188 afb_api.itf = &so_v2_api_itf;
189 if (afb_apiset_add(apiset, binding->api, afb_api) < 0) {
190 ERROR("binding %s can't be registered to set %s...", binding->api, afb_apiset_name(apiset));
193 NOTICE("binding %s added to set %s", binding->api, afb_apiset_name(apiset));
202 int afb_api_so_v2_add(const char *path, void *handle, struct afb_apiset *apiset)
204 const struct afb_binding_v2 *binding;
206 /* retrieves the register function */
207 binding = dlsym(handle, afb_api_so_v2_descriptor);
211 INFO("binding [%s] looks like an AFB binding V2", path);
214 if (binding->api == NULL || *binding->api == 0) {
215 ERROR("binding [%s] bad api name...", path);
218 if (!afb_api_is_valid_name(binding->api)) {
219 ERROR("binding [%s] invalid api name...", path);
222 if (binding->specification == NULL || *binding->specification == 0) {
223 ERROR("binding [%s] bad specification...", path);
226 if (binding->verbs == NULL) {
227 ERROR("binding [%s] no verbs...", path);
231 return afb_api_so_v2_add_binding(binding, handle, apiset);