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"
39 static const char afb_api_so_v2_descriptor[] = "afbBindingV2";
42 * Description of a binding
45 struct afb_binding_v2 *binding; /* descriptor */
46 size_t apilength; /* length of the API name */
47 void *handle; /* context of dlopen */
48 struct afb_svc *service; /* handler for service started */
49 struct afb_binding_interface interface; /* interface for the binding */
52 static struct afb_event afb_api_so_event_make_cb(void *closure, const char *name);
53 static int afb_api_so_event_broadcast_cb(void *closure, const char *name, struct json_object *object);
54 static void afb_api_so_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args);
55 static int afb_api_so_rootdir_get_fd(void *closure);
56 static int afb_api_so_rootdir_open_locale(void *closure, const char *filename, int flags, const char *locale);
58 static const struct afb_daemon_itf daemon_itf = {
59 .event_broadcast = afb_api_so_event_broadcast_cb,
60 .get_event_loop = afb_common_get_event_loop,
61 .get_user_bus = afb_common_get_user_bus,
62 .get_system_bus = afb_common_get_system_bus,
63 .vverbose = afb_api_so_vverbose_cb,
64 .event_make = afb_api_so_event_make_cb,
65 .rootdir_get_fd = afb_api_so_rootdir_get_fd,
66 .rootdir_open_locale = afb_api_so_rootdir_open_locale
69 static struct afb_event afb_api_so_event_make_cb(void *closure, const char *name)
73 struct api_so_v2 *desc = closure;
75 /* makes the event name */
76 assert(desc->binding != NULL);
77 length = strlen(name);
78 event = alloca(length + 2 + desc->apilength);
79 memcpy(event, desc->binding->api, desc->apilength);
80 event[desc->apilength] = '/';
81 memcpy(event + desc->apilength + 1, name, length + 1);
84 return afb_evt_create_event(event);
87 static int afb_api_so_event_broadcast_cb(void *closure, const char *name, struct json_object *object)
91 struct api_so_v2 *desc = closure;
93 /* makes the event name */
94 assert(desc->binding != NULL);
95 length = strlen(name);
96 event = alloca(length + 2 + desc->apilength);
97 memcpy(event, desc->binding->api, desc->apilength);
98 event[desc->apilength] = '/';
99 memcpy(event + desc->apilength + 1, name, length + 1);
101 return afb_evt_broadcast(event, object);
104 static void afb_api_so_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args)
107 struct api_so_v2 *desc = closure;
109 if (vasprintf(&p, fmt, args) < 0)
110 vverbose(level, file, line, fmt, args);
112 verbose(level, file, line, "%s {binding %s}", p, desc->binding->api);
117 static int afb_api_so_rootdir_get_fd(void *closure)
119 return afb_common_rootdir_get_fd();
122 static int afb_api_so_rootdir_open_locale(void *closure, const char *filename, int flags, const char *locale)
124 return afb_common_rootdir_open_locale(filename, flags, locale);
127 static const struct afb_verb_v2 *search(struct api_so_v2 *desc, const char *verb)
129 const struct afb_verb_v2 *result;
131 result = desc->binding->verbs;
132 while (result->verb && strcasecmp(result->verb, verb))
134 return result->verb ? result : NULL;
137 static void call_cb(void *closure, struct afb_xreq *xreq)
139 struct api_so_v2 *desc = closure;
140 const struct afb_verb_v2 *verb;
142 verb = search(desc, xreq->verb);
144 afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, desc->binding->api);
146 afb_xreq_call(xreq, verb->session, verb->callback);
149 static int service_start_cb(void *closure, int share_session, int onneed)
151 int (*start)(const struct afb_binding_interface *interface, struct afb_service service);
152 void (*onevent)(const char *event, struct json_object *object);
154 struct api_so_v2 *desc = closure;
157 if (desc->service != NULL) {
158 /* not an error when onneed */
162 /* already started: it is an error */
163 ERROR("Service %s already started", desc->binding->api);
167 /* get the initialisation */
168 start = desc->binding->start;
170 /* not an error when onneed */
174 /* no initialisation method */
175 ERROR("Binding %s is not a service", desc->binding->api);
179 /* get the event handler if any */
180 onevent = desc->binding->onevent;
181 desc->service = afb_svc_create_v2(share_session, onevent, start, &desc->interface);
182 if (desc->service == NULL) {
184 ERROR("Starting service %s failed", desc->binding->api);
191 int afb_api_so_v2_add(const char *path, void *handle)
194 struct api_so_v2 *desc;
195 struct afb_binding_v2 *binding;
197 /* retrieves the register function */
198 binding = dlsym(handle, afb_api_so_v2_descriptor);
202 INFO("binding [%s] looks like an AFB binding V2", path);
205 if (binding->api == NULL || *binding->api == 0) {
206 ERROR("binding [%s] bad api name...", path);
209 if (!afb_apis_is_valid_api_name(binding->api)) {
210 ERROR("binding [%s] invalid api name...", path);
213 if (binding->specification == NULL || *binding->specification == 0) {
214 ERROR("binding [%s] bad specification...", path);
217 if (binding->verbs == NULL) {
218 ERROR("binding [%s] no verbs...", path);
222 /* allocates the description */
223 desc = calloc(1, sizeof *desc);
225 ERROR("out of memory");
228 desc->binding = binding;
229 desc->handle = handle;
231 /* init the interface */
232 desc->interface.verbosity = verbosity;
233 desc->interface.mode = AFB_MODE_LOCAL;
234 desc->interface.daemon.itf = &daemon_itf;
235 desc->interface.daemon.closure = desc;
237 /* for log purpose, a fake binding is needed here */
239 /* init the binding */
241 NOTICE("binding %s [%s] calling init function", binding->api, path);
242 rc = binding->init(&desc->interface);
244 ERROR("binding %s [%s] initialisation function failed...", binding->api, path);
249 /* records the binding */
250 desc->apilength = strlen(binding->api);
251 if (afb_apis_add(binding->api, (struct afb_api){
254 .service_start = service_start_cb }) < 0) {
255 ERROR("binding [%s] can't be registered...", path);
258 NOTICE("binding %s loaded with API prefix %s", path, binding->api);