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 AFB_BINDING_PRAGMA_NO_VERBOSE_MACRO
25 #include <afb/afb-binding.h>
26 #include <json-c/json.h>
29 #include "afb-api-so-v2.h"
30 #include "afb-apiset.h"
34 #include "afb-common.h"
35 #include "afb-context.h"
36 #include "afb-api-so.h"
44 static const char afb_api_so_v2_descriptor[] = "afbBindingV2";
45 static const char afb_api_so_v2_verbosity[] = "afbBindingV2verbosity";
48 * Description of a binding
51 const struct afb_binding_v2 *binding; /* descriptor */
52 int *verbosity; /* verbosity */
53 void *handle; /* context of dlopen */
54 struct afb_svc *service; /* handler for service started */
55 struct afb_ditf ditf; /* daemon interface */
58 static const struct afb_verb_v2 *search(struct api_so_v2 *desc, const char *name)
60 const struct afb_verb_v2 *verb;
62 verb = desc->binding->verbs;
63 while (verb->verb && strcasecmp(verb->verb, name))
65 return verb->verb ? verb : NULL;
69 static void call_cb(void *closure, struct afb_xreq *xreq)
71 struct api_so_v2 *desc = closure;
72 const struct afb_verb_v2 *verb;
74 verb = search(desc, xreq->verb);
75 afb_xreq_call_verb_v2(xreq, verb);
80 struct api_so_v2 *desc;
81 struct afb_xreq *xreq;
84 static void call_sync_cb_cb(int signum, void *closure)
86 struct call_sync *cs = closure;
88 call_cb(cs->desc, cs->xreq);
90 if (!cs->xreq->replied)
91 afb_xreq_fail(cs->xreq, "aborted", "internal error");
95 static void call_sync_cb(void *closure, struct afb_xreq *xreq)
97 struct call_sync cs = { .desc = closure, .xreq = xreq };
99 if (jobs_call(closure, 0, call_sync_cb_cb, &cs))
100 call_cb(closure, xreq);
103 static int service_start_cb(void *closure, int share_session, int onneed, struct afb_apiset *apiset)
105 int (*start)(struct afb_service service);
106 void (*onevent)(struct afb_service service, const char *event, struct json_object *object);
108 struct api_so_v2 *desc = closure;
111 if (desc->service != NULL) {
112 /* not an error when onneed */
116 /* already started: it is an error */
117 ERROR("Service %s already started", desc->binding->api);
121 /* get the initialisation */
122 start = desc->binding->init;
124 /* not an error when onneed */
128 /* no initialisation method */
129 ERROR("Binding %s is not a service", desc->binding->api);
133 /* get the event handler if any */
134 onevent = desc->binding->onevent;
135 desc->service = afb_svc_create_v2(apiset, share_session, start, onevent, &desc->ditf);
136 if (desc->service == NULL) {
138 ERROR("Starting service %s failed", desc->binding->api);
145 static void update_hooks_cb(void *closure)
147 struct api_so_v2 *desc = closure;
148 afb_ditf_update_hook(&desc->ditf);
151 static int get_verbosity_cb(void *closure)
153 struct api_so_v2 *desc = closure;
154 return *desc->verbosity;
157 static void set_verbosity_cb(void *closure, int level)
159 struct api_so_v2 *desc = closure;
160 *desc->verbosity = level;
163 static struct json_object *describe_cb(void *closure)
165 struct api_so_v2 *desc = closure;
166 return desc->binding->specification ? json_tokener_parse(desc->binding->specification) : NULL;
169 static struct afb_api_itf so_v2_api_itf = {
171 .service_start = service_start_cb,
172 .update_hooks = update_hooks_cb,
173 .get_verbosity = get_verbosity_cb,
174 .set_verbosity = set_verbosity_cb,
175 .describe = describe_cb
178 static struct afb_api_itf so_v2_sync_api_itf = {
179 .call = call_sync_cb,
180 .service_start = service_start_cb,
181 .update_hooks = update_hooks_cb,
182 .get_verbosity = get_verbosity_cb,
183 .set_verbosity = set_verbosity_cb,
184 .describe = describe_cb
187 int afb_api_so_v2_add_binding(const struct afb_binding_v2 *binding, void *handle, struct afb_apiset *apiset, int *pver)
190 struct api_so_v2 *desc;
191 struct afb_api afb_api;
194 assert(binding->api);
195 assert(binding->specification);
196 assert(binding->verbs);
198 /* allocates the description */
199 desc = malloc(sizeof *desc);
201 ERROR("out of memory");
204 desc->binding = binding;
205 desc->verbosity = pver;
206 desc->handle = handle;
207 desc->service = NULL;
208 memset(&desc->ditf, 0, sizeof desc->ditf);
210 /* init the interface */
211 afb_ditf_init_v2(&desc->ditf, binding->api);
213 /* init the binding */
214 if (binding->preinit) {
215 INFO("binding %s calling preinit function", binding->api);
216 rc = binding->preinit(desc->ditf.daemon);
218 ERROR("binding %s preinit function failed...", binding->api);
223 /* records the binding */
224 afb_api.closure = desc;
225 afb_api.itf = binding->concurrent ? &so_v2_api_itf : &so_v2_sync_api_itf;
226 if (afb_apiset_add(apiset, binding->api, afb_api) < 0) {
227 ERROR("binding %s can't be registered to set %s...", binding->api, afb_apiset_name(apiset));
230 INFO("binding %s added to set %s", binding->api, afb_apiset_name(apiset));
239 int afb_api_so_v2_add(const char *path, void *handle, struct afb_apiset *apiset)
241 const struct afb_binding_v2 *binding;
244 /* retrieves the register function */
245 binding = dlsym(handle, afb_api_so_v2_descriptor);
246 pver = dlsym(handle, afb_api_so_v2_verbosity);
247 if (!binding && !pver)
250 INFO("binding [%s] looks like an AFB binding V2", path);
253 if (!binding || !pver) {
254 ERROR("binding [%s] incomplete symbols...", path);
257 if (binding->api == NULL || *binding->api == 0) {
258 ERROR("binding [%s] bad api name...", path);
261 if (!afb_api_is_valid_name(binding->api)) {
262 ERROR("binding [%s] invalid api name...", path);
265 if (binding->specification == NULL || *binding->specification == 0) {
266 ERROR("binding [%s] bad specification...", path);
269 if (binding->verbs == NULL) {
270 ERROR("binding [%s] no verbs...", path);
274 return afb_api_so_v2_add_binding(binding, handle, apiset, pver);