2 * Copyright (C) 2018 "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.
26 #include <json-c/json.h>
27 #include <afb/afb-binding-v3.h>
30 #include "afb-api-so-v3.h"
31 #include "afb-api-v3.h"
32 #include "afb-apiset.h"
33 #include "afb-export.h"
39 static const char afb_api_so_v3_desc[] = "afbBindingV3";
40 static const char afb_api_so_v3_root[] = "afbBindingV3root";
41 static const char afb_api_so_v3_entry[] = "afbBindingV3entry";
45 struct afb_api_x3 **root;
46 const struct afb_binding_v3 *desc;
47 int (*entry)(struct afb_api_x3 *);
50 static int init(void *closure, struct afb_api_x3 *api)
52 const struct args *a = closure;
57 api->userdata = a->desc->userdata;
58 rc = afb_api_v3_set_binding_fields(a->desc, api);
61 if (rc >= 0 && a->entry)
62 rc = afb_api_v3_safe_preinit(api, a->entry);
70 int afb_api_so_v3_add(const char *path, void *handle, struct afb_apiset *declare_set, struct afb_apiset * call_set)
73 struct afb_api_v3 *api;
74 struct afb_export *export;
76 /* retrieves the register function */
77 a.root = dlsym(handle, afb_api_so_v3_root);
78 a.desc = dlsym(handle, afb_api_so_v3_desc);
79 a.entry = dlsym(handle, afb_api_so_v3_entry);
80 if (!a.root && !a.desc && !a.entry)
83 INFO("binding [%s] looks like an AFB binding V3", path);
87 ERROR("binding [%s] incomplete symbol set: %s is missing",
88 path, afb_api_so_v3_root);
92 if (a.desc->api == NULL || *a.desc->api == 0) {
93 ERROR("binding [%s] bad api name...", path);
96 if (!afb_api_is_valid_name(a.desc->api)) {
97 ERROR("binding [%s] invalid api name...", path);
101 a.entry = a.desc->preinit;
102 else if (a.desc->preinit) {
103 ERROR("binding [%s] clash: you can't define %s and %s.preinit, choose only one",
104 path, afb_api_so_v3_entry, afb_api_so_v3_desc);
108 api = afb_api_v3_create(declare_set, call_set, a.desc->api, a.desc->info, a.desc->noconcurrency, init, &a, 0, NULL, path);
113 ERROR("binding [%s] incomplete symbol set: %s is missing",
114 path, afb_api_so_v3_entry);
118 export = afb_export_create_none_for_path(declare_set, call_set, path, init, &a);
121 * No call is done to afb_export_unref(export) because:
122 * - legacy applications may use the root API emitting messages
123 * - it allows writting applications like bindings without API
124 * But this has the sad effect to introduce a kind of leak.
125 * To avoid this, if necessary further developement should list bindings
132 ERROR("binding [%s] initialisation failed", path);