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 * structure for memorizing verbs sorted with permissions
51 const struct afb_verb_v2 *verb;
52 struct afb_perm *perm;
56 * Description of a binding
59 const struct afb_binding_v2 *binding; /* descriptor */
60 int *verbosity; /* verbosity */
61 void *handle; /* context of dlopen */
62 struct afb_svc *service; /* handler for service started */
63 struct afb_ditf ditf; /* daemon interface */
65 struct verb_v2 verbs[1];
68 static const struct afb_verb_v2 *search(struct api_so_v2 *desc, const char *verb)
70 const struct afb_verb_v2 *v;
77 v = desc->verbs[i].verb;
78 c = strcasecmp(v->verb, verb);
89 static void call_cb(void *closure, struct afb_xreq *xreq)
91 struct api_so_v2 *desc = closure;
92 const struct afb_verb_v2 *verb;
94 verb = search(desc, xreq->verb);
95 afb_xreq_call_verb_v2(xreq, verb);
98 static int service_start_cb(void *closure, int share_session, int onneed, struct afb_apiset *apiset)
100 int (*start)(struct afb_service service);
101 void (*onevent)(struct afb_service service, const char *event, struct json_object *object);
103 struct api_so_v2 *desc = closure;
106 if (desc->service != NULL) {
107 /* not an error when onneed */
111 /* already started: it is an error */
112 ERROR("Service %s already started", desc->binding->api);
116 /* get the initialisation */
117 start = desc->binding->start;
119 /* not an error when onneed */
123 /* no initialisation method */
124 ERROR("Binding %s is not a service", desc->binding->api);
128 /* get the event handler if any */
129 onevent = desc->binding->onevent;
130 desc->service = afb_svc_create_v2(apiset, share_session, start, onevent, &desc->ditf);
131 if (desc->service == NULL) {
133 ERROR("Starting service %s failed", desc->binding->api);
140 static void update_hooks_cb(void *closure)
142 struct api_so_v2 *desc = closure;
143 afb_ditf_update_hook(&desc->ditf);
146 static int get_verbosity_cb(void *closure)
148 struct api_so_v2 *desc = closure;
149 return *desc->verbosity;
152 static void set_verbosity_cb(void *closure, int level)
154 struct api_so_v2 *desc = closure;
155 *desc->verbosity = level;
158 static struct json_object *describe_cb(void *closure)
160 struct api_so_v2 *desc = closure;
161 return desc->binding->specification ? json_tokener_parse(desc->binding->specification) : NULL;
164 static struct afb_api_itf so_v2_api_itf = {
166 .service_start = service_start_cb,
167 .update_hooks = update_hooks_cb,
168 .get_verbosity = get_verbosity_cb,
169 .set_verbosity = set_verbosity_cb,
170 .describe = describe_cb
174 int afb_api_so_v2_add_binding(const struct afb_binding_v2 *binding, void *handle, struct afb_apiset *apiset, int *pver)
177 struct api_so_v2 *desc;
178 struct afb_api afb_api;
179 const struct afb_verb_v2 *bv;
183 assert(binding->api);
184 assert(binding->specification);
185 assert(binding->verbs);
187 /* count the verbs */
189 while (binding->verbs[count].verb)
192 /* allocates the description */
193 desc = malloc(sizeof *desc + (count - 1) * sizeof desc->verbs);
195 ERROR("out of memory");
198 desc->binding = binding;
199 desc->verbosity = pver;
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_v2(&desc->ditf, binding->api);
233 /* init the binding */
235 INFO("binding %s calling init function", binding->api);
236 rc = binding->init(desc->ditf.daemon);
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;
268 /* retrieves the register function */
269 binding = dlsym(handle, afb_api_so_v2_descriptor);
270 pver = dlsym(handle, afb_api_so_v2_verbosity);
271 if (!binding && !pver)
274 INFO("binding [%s] looks like an AFB binding V2", path);
277 if (!binding || !pver) {
278 ERROR("binding [%s] incomplete symbols...", path);
281 if (binding->api == NULL || *binding->api == 0) {
282 ERROR("binding [%s] bad api name...", path);
285 if (!afb_api_is_valid_name(binding->api)) {
286 ERROR("binding [%s] invalid api name...", path);
289 if (binding->specification == NULL || *binding->specification == 0) {
290 ERROR("binding [%s] bad specification...", path);
293 if (binding->verbs == NULL) {
294 ERROR("binding [%s] no verbs...", path);
298 return afb_api_so_v2_add_binding(binding, handle, apiset, pver);