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"
43 static const char afb_api_so_v2_descriptor[] = "afbBindingV2";
46 * structure for memorizing verbs sorted with permissions
49 const struct afb_verb_v2 *verb;
50 struct afb_perm *perm;
54 * Description of a binding
57 const struct afb_binding_v2 *binding; /* descriptor */
58 void *handle; /* context of dlopen */
59 struct afb_svc *service; /* handler for service started */
60 struct afb_ditf ditf; /* daemon interface */
62 struct verb_v2 verbs[1];
65 static const struct afb_verb_v2 *search(struct api_so_v2 *desc, const char *verb)
67 const struct afb_verb_v2 *v;
74 v = desc->verbs[i].verb;
75 c = strcasecmp(v->verb, verb);
86 static void call_cb(void *closure, struct afb_xreq *xreq)
88 struct api_so_v2 *desc = closure;
89 const struct afb_verb_v2 *verb;
91 verb = search(desc, xreq->verb);
93 afb_xreq_fail_unknown_verb(xreq);
95 if (!xreq_session_check_apply(xreq, verb->session))
96 afb_xreq_call(xreq, verb->callback);
99 static int service_start_cb(void *closure, int share_session, int onneed, struct afb_apiset *apiset)
101 int (*start)(const struct afb_binding_interface *interface, struct afb_service service);
102 void (*onevent)(const char *event, struct json_object *object);
104 struct api_so_v2 *desc = closure;
107 if (desc->service != NULL) {
108 /* not an error when onneed */
112 /* already started: it is an error */
113 ERROR("Service %s already started", desc->binding->api);
117 /* get the initialisation */
118 start = desc->binding->start;
120 /* not an error when onneed */
124 /* no initialisation method */
125 ERROR("Binding %s is not a service", desc->binding->api);
129 /* get the event handler if any */
130 onevent = desc->binding->onevent;
131 desc->service = afb_svc_create_v2(apiset, share_session, onevent, start, &desc->ditf.interface);
132 if (desc->service == NULL) {
134 ERROR("Starting service %s failed", desc->binding->api);
141 static void update_hooks_cb(void *closure)
143 struct api_so_v2 *desc = closure;
144 afb_ditf_update_hook(&desc->ditf);
147 static int get_verbosity_cb(void *closure)
149 struct api_so_v2 *desc = closure;
150 return desc->ditf.interface.verbosity;
153 static void set_verbosity_cb(void *closure, int level)
155 struct api_so_v2 *desc = closure;
156 desc->ditf.interface.verbosity = level;
159 static struct json_object *describe_cb(void *closure)
161 struct api_so_v2 *desc = closure;
162 return desc->binding->specification ? json_tokener_parse(desc->binding->specification) : NULL;
165 static struct afb_api_itf so_v2_api_itf = {
167 .service_start = service_start_cb,
168 .update_hooks = update_hooks_cb,
169 .get_verbosity = get_verbosity_cb,
170 .set_verbosity = set_verbosity_cb,
171 .describe = describe_cb
175 int afb_api_so_v2_add_binding(const struct afb_binding_v2 *binding, void *handle, struct afb_apiset *apiset)
178 struct api_so_v2 *desc;
179 struct afb_api afb_api;
180 const struct afb_verb_v2 *bv;
184 assert(binding->api);
185 assert(binding->specification);
186 assert(binding->verbs);
188 /* count the verbs */
190 while (binding->verbs[count].verb)
193 /* allocates the description */
194 desc = malloc(sizeof *desc + (count - 1) * sizeof desc->verbs);
196 ERROR("out of memory");
199 desc->binding = binding;
200 desc->handle = handle;
201 desc->service = NULL;
202 memset(&desc->ditf, 0, sizeof desc->ditf);
205 /* fill the verbs sorted */
206 for (i = 0 ; i < count ; i++) {
207 desc->verbs[i].perm = NULL;
209 bv = &binding->verbs[j];
210 while (j && strcasecmp(bv->verb, desc->verbs[j-1].verb->verb) < 0) {
211 desc->verbs[j].verb = desc->verbs[j-1].verb;
214 desc->verbs[j].verb = bv;
217 /* makes the permissions */
218 for (i = 0 ; i < count ; i++) {
219 if (desc->verbs[i].verb->permissions) {
220 desc->verbs[i].perm = afb_perm_parse(desc->verbs[i].verb->permissions);
221 if (!desc->verbs[i].perm) {
222 ERROR("Bad permission specification for verb %s of api %s: %s",
223 desc->verbs[i].verb->verb, binding->api,
224 desc->verbs[i].verb->permissions);
230 /* init the interface */
231 afb_ditf_init(&desc->ditf, binding->api);
233 /* init the binding */
235 INFO("binding %s calling init function", binding->api);
236 rc = binding->init(&desc->ditf.interface);
238 ERROR("binding %s initialisation function failed...", binding->api);
243 /* records the binding */
244 afb_api.closure = desc;
245 afb_api.itf = &so_v2_api_itf;
246 if (afb_apiset_add(apiset, binding->api, afb_api) < 0) {
247 ERROR("binding %s can't be registered to set %s...", binding->api, afb_apiset_name(apiset));
250 NOTICE("binding %s added to set %s", binding->api, afb_apiset_name(apiset));
256 if (desc->verbs[--count].perm)
257 afb_perm_unref(desc->verbs[count].perm);
263 int afb_api_so_v2_add(const char *path, void *handle, struct afb_apiset *apiset)
265 const struct afb_binding_v2 *binding;
267 /* retrieves the register function */
268 binding = dlsym(handle, afb_api_so_v2_descriptor);
272 INFO("binding [%s] looks like an AFB binding V2", path);
275 if (binding->api == NULL || *binding->api == 0) {
276 ERROR("binding [%s] bad api name...", path);
279 if (!afb_api_is_valid_name(binding->api)) {
280 ERROR("binding [%s] invalid api name...", path);
283 if (binding->specification == NULL || *binding->specification == 0) {
284 ERROR("binding [%s] bad specification...", path);
287 if (binding->verbs == NULL) {
288 ERROR("binding [%s] no verbs...", path);
292 return afb_api_so_v2_add_binding(binding, handle, apiset);