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>
30 #include "afb-common.h"
31 #include "afb-context.h"
32 #include "afb-api-so.h"
40 static const char afb_api_so_v1_register[] = "afbBindingV1Register";
41 static const char afb_api_so_v1_service_init[] = "afbBindingV1ServiceInit";
42 static const char afb_api_so_v1_service_event[] = "afbBindingV1ServiceEvent";
45 * Description of a binding
48 struct afb_binding *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_desc_v1 *search(struct api_so_v1 *desc, const char *name)
56 const struct afb_verb_desc_v1 *verb;
58 verb = desc->binding->v1.verbs;
59 while (verb->name && strcasecmp(verb->name, name))
61 return verb->name ? verb : NULL;
64 static void call_cb(void *closure, struct afb_xreq *xreq)
66 const struct afb_verb_desc_v1 *verb;
67 struct api_so_v1 *desc = closure;
69 verb = search(desc, xreq->verb);
71 afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, desc->binding->v1.prefix);
73 afb_xreq_call(xreq, verb->session, verb->callback);
76 static int service_start_cb(void *closure, int share_session, int onneed)
78 int (*init)(struct afb_service service);
79 void (*onevent)(const char *event, struct json_object *object);
81 struct api_so_v1 *desc = closure;
84 if (desc->service != NULL) {
85 /* not an error when onneed */
89 /* already started: it is an error */
90 ERROR("Service %s already started", desc->binding->v1.prefix);
94 /* get the initialisation */
95 init = dlsym(desc->handle, afb_api_so_v1_service_init);
97 /* not an error when onneed */
101 /* no initialisation method */
102 ERROR("Binding %s is not a service", desc->binding->v1.prefix);
106 /* get the event handler if any */
107 onevent = dlsym(desc->handle, afb_api_so_v1_service_event);
108 desc->service = afb_svc_create(share_session, init, onevent);
109 if (desc->service == NULL) {
111 ERROR("Starting service %s failed", desc->binding->v1.prefix);
118 int afb_api_so_v1_add(const char *path, void *handle)
120 struct api_so_v1 *desc;
121 struct afb_binding *(*register_function) (const struct afb_binding_interface *interface);
123 /* retrieves the register function */
124 register_function = dlsym(handle, afb_api_so_v1_register);
125 if (!register_function)
127 INFO("binding [%s] is a valid AFB binding V1", path);
129 /* allocates the description */
130 desc = calloc(1, sizeof *desc);
132 ERROR("out of memory");
135 desc->handle = handle;
137 /* init the interface */
138 afb_ditf_init(&desc->ditf, path);
140 /* init the binding */
141 NOTICE("binding [%s] calling registering function %s", path, afb_api_so_v1_register);
142 desc->binding = register_function(&desc->ditf.interface);
143 if (desc->binding == NULL) {
144 ERROR("binding [%s] register function failed. continuing...", path);
148 /* check the returned structure */
149 if (desc->binding->type != AFB_BINDING_VERSION_1) {
150 ERROR("binding [%s] invalid type %d...", path, desc->binding->type);
153 if (desc->binding->v1.prefix == NULL || *desc->binding->v1.prefix == 0) {
154 ERROR("binding [%s] bad prefix...", path);
157 if (!afb_apis_is_valid_api_name(desc->binding->v1.prefix)) {
158 ERROR("binding [%s] invalid prefix...", path);
161 if (desc->binding->v1.info == NULL || *desc->binding->v1.info == 0) {
162 ERROR("binding [%s] bad description...", path);
165 if (desc->binding->v1.verbs == NULL) {
166 ERROR("binding [%s] no APIs...", path);
170 /* records the binding */
171 afb_ditf_rename(&desc->ditf, desc->binding->v1.prefix);
172 if (afb_apis_add(desc->binding->v1.prefix, (struct afb_api){
175 .service_start = service_start_cb }) < 0) {
176 ERROR("binding [%s] can't be registered...", path);
179 NOTICE("binding %s loaded with API prefix %s", path, desc->binding->v1.prefix);