-
Notifications
You must be signed in to change notification settings - Fork 316
/
dynamic.c
178 lines (141 loc) · 3.62 KB
/
dynamic.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
* libiio - Library for interfacing industrial I/O (IIO) devices
*
* Copyright (C) 2021 Analog Devices, Inc.
* Author: Paul Cercueil <paul.cercueil@analog.com>
*/
#include "dynamic.h"
#include "iio-config.h"
#include "iio-private.h"
#include <iio/iio-backend.h>
#include <iio/iio-debug.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
struct iio_module {
const struct iio_context_params *params;
void *lib;
char *name;
};
struct iio_module * iio_open_module(const struct iio_context_params *params,
const char *name)
{
char buf[PATH_MAX];
struct iio_module *module;
int err = -ENOMEM;
module = zalloc(sizeof(*module));
if (!module)
return iio_ptr(-ENOMEM);
module->name = strdup(name);
if (!module->name)
goto err_free_module;
module->params = params;
iio_snprintf(buf, sizeof(buf),
IIO_MODULES_DIR "libiio-%s" IIO_LIBRARY_SUFFIX, name);
prm_dbg(params, "Looking for plugin: \'%s\'\n", buf);
module->lib = iio_dlopen(buf);
if (!module->lib) {
prm_dbg(params, "Unable to open plug-in\n");
err = -ENOSYS;
goto err_free_name;
}
return module;
err_free_name:
free(module->name);
err_free_module:
free(module);
return iio_ptr(err);
}
void iio_release_module(struct iio_module *module)
{
iio_dlclose(module->lib);
free(module->name);
free(module);
}
const struct iio_backend * iio_module_get_backend(struct iio_module *module)
{
const struct iio_backend *backend;
char buf[1024];
int err;
iio_snprintf(buf, sizeof(buf), "iio_%s_backend", module->name);
backend = iio_dlsym(module->lib, buf);
err = iio_err(backend);
if (err)
prm_err(module->params, "No \'%s\' symbol\n", buf);
return backend;
}
static const struct iio_backend *
get_iio_backend(const struct iio_context_params *params,
const char *name, struct iio_module **libp)
{
const struct iio_backend *backend;
struct iio_module *lib;
int ret;
lib = iio_open_module(params, name);
ret = iio_err(lib);
if (ret)
return iio_err_cast(lib);
backend = iio_module_get_backend(lib);
ret = iio_err(backend);
if (ret) {
prm_err(params, "Module is not a backend\n");
iio_release_module(lib);
return iio_err_cast(backend);
}
*libp = lib;
return backend;
}
struct iio_context *
iio_create_dynamic_context(const struct iio_context_params *params,
const char *uri)
{
struct iio_context_params params2 = *params;
const struct iio_backend *backend;
struct iio_context *ctx;
const char *ptr;
char buf[256];
struct iio_module *lib;
int ret;
ptr = strchr(uri, ':');
if (!ptr) {
prm_err(params, "Invalid URI: %s\n", uri);
return iio_ptr(-EINVAL);
}
iio_snprintf(buf, sizeof(buf), "%.*s", (int) (ptr - uri), uri);
backend = get_iio_backend(params, buf, &lib);
ret = iio_err(backend);
if (ret)
return iio_err_cast(backend);
if (!backend->ops || !backend->ops->create) {
prm_err(params, "Backend has no create function\n");
ret = -EINVAL;
goto out_release_module;
}
prm_dbg(params, "Found backend: %s\n", backend->name);
if (!params2.timeout_ms)
params2.timeout_ms = backend->default_timeout_ms;
uri += strlen(backend->uri_prefix);
ctx = (*backend->ops->create)(¶ms2, uri);
ret = iio_err(ctx);
if (ret)
goto out_release_module;
ctx->lib = lib;
return ctx;
out_release_module:
iio_release_module(lib);
return iio_ptr(ret);
}
bool iio_has_backend_dynamic(const struct iio_context_params *params,
const char *name)
{
const struct iio_backend *backend;
struct iio_module *lib;
bool found;
backend = get_iio_backend(params, name, &lib);
found = !iio_err(backend);
if (found)
iio_release_module(lib);
return found;
}