-
Notifications
You must be signed in to change notification settings - Fork 316
/
scan.c
190 lines (151 loc) · 3.94 KB
/
scan.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
179
180
181
182
183
184
185
186
187
188
189
190
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
* libiio - Library for interfacing industrial I/O (IIO) devices
*
* Copyright (C) 2016 Analog Devices, Inc.
* Author: Paul Cercueil <[email protected]>
*/
#include "iio-config.h"
#include "iio-private.h"
#include <errno.h>
#include <stdbool.h>
#include <string.h>
struct iio_scan {
struct iio_context_info *info;
size_t count;
};
struct iio_scan * iio_scan(const struct iio_context_params *params,
const char *backends)
{
const struct iio_context_params *default_params = get_default_params();
struct iio_context_params params2 = { 0 };
char *token, *rest, *rest2, *backend_name;
const struct iio_backend *backend = NULL;
struct iio_module *module;
const char *args, *uri;
struct iio_scan *ctx;
unsigned int i;
char buf[1024];
size_t len;
int ret;
if (!params)
params = default_params;
params2 = *params;
if (!params2.log_level)
params2.log_level = default_params->log_level;
if (!params2.stderr_level)
params2.stderr_level = default_params->stderr_level;
if (!params2.timestamp_level)
params2.timestamp_level = default_params->timestamp_level;
ctx = calloc(1, sizeof(*ctx));
if (!ctx)
return iio_ptr(-ENOMEM);
if (!backends)
backends = LIBIIO_SCAN_BACKENDS;
/* Copy the string into an intermediate buffer for strtok() usage */
iio_snprintf(buf, sizeof(buf), "%s", backends);
for (token = iio_strtok_r(buf, ",", &rest);
token; token = iio_strtok_r(NULL, ",", &rest)) {
args = NULL;
for (i = 0; i < iio_backends_size; i++) {
backend = iio_backends[i];
if (!backend || !backend->ops->scan)
continue;
uri = backend->uri_prefix;
len = strlen(uri) - 1; /* -1: to remove the colon of the URI */
if (strncmp(token, uri, len))
continue;
if (token[len] == '\0')
args = NULL;
else if (token[len] == '=')
args = token + len + 1;
else
continue;
break;
}
if (i == iio_backends_size)
backend = NULL;
if (WITH_MODULES && !backend) {
backend_name = iio_strtok_r(token, "=", &rest2);
module = iio_open_module(¶ms2, backend_name);
if (iio_err(module)) {
module = NULL;
} else {
backend = iio_module_get_backend(module);
if (iio_err(backend)) {
iio_release_module(module);
backend = NULL;
module = NULL;
}
}
args = iio_strtok_r(NULL, "=", &rest2);
} else {
module = NULL;
}
if (!backend) {
prm_warn(params, "No backend found for scan string \'%s\'\n",
token);
continue;
}
if (!backend->ops || !backend->ops->scan) {
prm_warn(params, "Backend %s does not support scanning.\n",
token);
continue;
}
if (params->timeout_ms)
params2.timeout_ms = params->timeout_ms;
else
params2.timeout_ms = backend->default_timeout_ms;
ret = backend->ops->scan(¶ms2, ctx, args);
if (ret < 0) {
prm_perror(¶ms2, ret,
"Unable to scan %s context", token);
}
if (WITH_MODULES && module)
iio_release_module(module);
}
return ctx;
}
void iio_scan_destroy(struct iio_scan *ctx)
{
unsigned int i;
for (i = 0; i < ctx->count; i++) {
free(ctx->info[i].description);
free(ctx->info[i].uri);
}
free(ctx->info);
free(ctx);
}
size_t iio_scan_get_results_count(const struct iio_scan *ctx)
{
return ctx->count;
}
const char *
iio_scan_get_description(const struct iio_scan *ctx, size_t idx)
{
if (idx >= ctx->count)
return NULL;
return ctx->info[idx].description;
}
const char * iio_scan_get_uri(const struct iio_scan *ctx, size_t idx)
{
if (idx >= ctx->count)
return NULL;
return ctx->info[idx].uri;
}
int iio_scan_add_result(struct iio_scan *ctx, const char *desc, const char *uri)
{
struct iio_context_info *info;
size_t size = ctx->count;
info = realloc(ctx->info, (size + 1) * sizeof(*info));
if (!info)
return -ENOMEM;
ctx->info = info;
ctx->count = size + 1;
info = &info[size];
info->description = iio_strdup(desc);
info->uri = iio_strdup(uri);
if (!info->description || !info->uri)
return -ENOMEM;
return 0;
}