forked from analogdevicesinc/libiio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan.c
195 lines (157 loc) · 3.74 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
191
192
193
194
195
// 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_context {
bool scan_usb;
bool scan_network;
bool scan_local;
};
const char * iio_context_info_get_description(
const struct iio_context_info *info)
{
return info->description;
}
const char * iio_context_info_get_uri(
const struct iio_context_info *info)
{
return info->uri;
}
ssize_t iio_scan_context_get_info_list(struct iio_scan_context *ctx,
struct iio_context_info ***info)
{
struct iio_scan_result scan_result = { 0, NULL };
if (WITH_LOCAL_BACKEND && ctx->scan_local) {
int ret = local_context_scan(&scan_result);
if (ret < 0) {
if (scan_result.info)
iio_context_info_list_free(scan_result.info);
return ret;
}
}
if (WITH_USB_BACKEND && ctx->scan_usb) {
int ret = usb_context_scan(&scan_result);
if (ret < 0) {
if (scan_result.info)
iio_context_info_list_free(scan_result.info);
return ret;
}
}
if (HAVE_DNS_SD && ctx->scan_network) {
int ret = dnssd_context_scan(&scan_result);
if (ret < 0) {
if (scan_result.info)
iio_context_info_list_free(scan_result.info);
return ret;
}
}
*info = scan_result.info;
return (ssize_t) scan_result.size;
}
void iio_context_info_list_free(struct iio_context_info **list)
{
struct iio_context_info **it;
if (!list)
return;
for (it = list; *it; it++) {
struct iio_context_info *info = *it;
free(info->description);
free(info->uri);
free(info);
}
free(list);
}
struct iio_context_info *
iio_scan_result_add(struct iio_scan_result *scan_result)
{
struct iio_context_info **info;
size_t size = scan_result->size;
info = realloc(scan_result->info, (size + 2) * sizeof(*info));
if (!info)
return NULL;
scan_result->info = info;
scan_result->size = size + 1;
/* Make sure iio_context_info_list_free won't overflow */
info[size + 1] = NULL;
info[size] = zalloc(sizeof(**info));
if (!info[size])
return NULL;
return info[size];
}
struct iio_scan_context * iio_create_scan_context(
const char *backend, unsigned int flags)
{
struct iio_scan_context *ctx;
/* "flags" must be zero for now */
if (flags != 0) {
errno = EINVAL;
return NULL;
}
ctx = calloc(1, sizeof(*ctx));
if (!ctx) {
errno = ENOMEM;
return NULL;
}
if (!backend || strstr(backend, "local"))
ctx->scan_local = true;
if (!backend || strstr(backend, "usb"))
ctx->scan_usb = true;
if (!backend || strstr(backend, "ip"))
ctx->scan_network = true;
return ctx;
}
void iio_scan_context_destroy(struct iio_scan_context *ctx)
{
free(ctx);
}
struct iio_scan_block {
struct iio_scan_context *ctx;
struct iio_context_info **info;
ssize_t ctx_cnt;
};
ssize_t iio_scan_block_scan(struct iio_scan_block *blk)
{
iio_context_info_list_free(blk->info);
blk->info = NULL;
blk->ctx_cnt = iio_scan_context_get_info_list(blk->ctx, &blk->info);
return blk->ctx_cnt;
}
struct iio_context_info *iio_scan_block_get_info(
struct iio_scan_block *blk, unsigned int index)
{
if (!blk->info || (ssize_t)index >= blk->ctx_cnt) {
errno = EINVAL;
return NULL;
}
return blk->info[index];
}
struct iio_scan_block *iio_create_scan_block(
const char *backend, unsigned int flags)
{
struct iio_scan_block *blk;
blk = calloc(1, sizeof(*blk));
if (!blk) {
errno = ENOMEM;
return NULL;
}
blk->ctx = iio_create_scan_context(backend, flags);
if (!blk->ctx) {
free(blk);
return NULL;
}
return blk;
}
void iio_scan_block_destroy(struct iio_scan_block *blk)
{
iio_context_info_list_free(blk->info);
iio_scan_context_destroy(blk->ctx);
free(blk);
}