2 * Copyright (C) 2018, 2019 "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.
18 #if WITH_DYNAMIC_BINDING
28 #include <json-c/json.h>
29 #include <afb/afb-binding-v3.h>
32 #include "afb-api-so-v3.h"
33 #include "afb-api-v3.h"
34 #include "afb-apiset.h"
35 #include "afb-export.h"
41 static const char afb_api_so_v3_desc[] = "afbBindingV3";
42 static const char afb_api_so_v3_root[] = "afbBindingV3root";
43 static const char afb_api_so_v3_entry[] = "afbBindingV3entry";
47 struct afb_api_x3 **root;
48 const struct afb_binding_v3 *desc;
49 int (*entry)(struct afb_api_x3 *);
52 static int init(void *closure, struct afb_api_x3 *api)
54 const struct args *a = closure;
59 api->userdata = a->desc->userdata;
60 rc = afb_api_v3_set_binding_fields(a->desc, api);
63 if (rc >= 0 && a->entry)
64 rc = afb_api_v3_safe_preinit(api, a->entry);
72 int afb_api_so_v3_add(const char *path, void *handle, struct afb_apiset *declare_set, struct afb_apiset * call_set)
75 struct afb_api_v3 *api;
76 struct afb_export *export;
78 /* retrieves the register function */
79 a.root = dlsym(handle, afb_api_so_v3_root);
80 a.desc = dlsym(handle, afb_api_so_v3_desc);
81 a.entry = dlsym(handle, afb_api_so_v3_entry);
82 if (!a.root && !a.desc && !a.entry)
85 INFO("binding [%s] looks like an AFB binding V3", path);
89 ERROR("binding [%s] incomplete symbol set: %s is missing",
90 path, afb_api_so_v3_root);
94 if (a.desc->api == NULL || *a.desc->api == 0) {
95 ERROR("binding [%s] bad api name...", path);
98 if (!afb_api_is_valid_name(a.desc->api)) {
99 ERROR("binding [%s] invalid api name...", path);
103 a.entry = a.desc->preinit;
104 else if (a.desc->preinit) {
105 ERROR("binding [%s] clash: you can't define %s and %s.preinit, choose only one",
106 path, afb_api_so_v3_entry, afb_api_so_v3_desc);
110 api = afb_api_v3_create(declare_set, call_set, a.desc->api, a.desc->info, a.desc->noconcurrency, init, &a, 0, NULL, path);
115 ERROR("binding [%s] incomplete symbol set: %s is missing",
116 path, afb_api_so_v3_entry);
120 export = afb_export_create_none_for_path(declare_set, call_set, path, init, &a);
123 * No call is done to afb_export_unref(export) because:
124 * - legacy applications may use the root API emitting messages
125 * - it allows writting applications like bindings without API
126 * But this has the sad effect to introduce a kind of leak.
127 * To avoid this, if necessary further developement should list bindings
134 ERROR("binding [%s] initialisation failed", path);