forked from analogdevicesinc/libtinyiiod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tinyiiod.c
298 lines (242 loc) · 6.48 KB
/
tinyiiod.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
* libtinyiiod - Tiny IIO Daemon Library
*
* Copyright (C) 2016 Analog Devices, Inc.
* Author: Paul Cercueil <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
#include "tinyiiod-private.h"
#include "compat.h"
struct tinyiiod {
struct tinyiiod_ops *ops;
char *buf;
};
struct tinyiiod * tinyiiod_create(struct tinyiiod_ops *ops)
{
struct tinyiiod *iiod = malloc(sizeof(*iiod));
if (!iiod)
return NULL;
iiod->buf = malloc(IIOD_BUFFER_SIZE);
if (!iiod->buf) {
free(iiod);
return NULL;
}
iiod->ops = ops;
return iiod;
}
void tinyiiod_destroy(struct tinyiiod *iiod)
{
free(iiod->buf);
free(iiod);
}
int32_t tinyiiod_read_command(struct tinyiiod *iiod)
{
char buf[128];
int32_t ret;
ret = tinyiiod_read_line(iiod, buf, sizeof(buf));
if (ret < 0)
return ret;
ret = tinyiiod_parse_string(iiod, buf);
if (ret < 0)
tinyiiod_write_value(iiod, ret);
return ret;
}
ssize_t tinyiiod_read(struct tinyiiod *iiod, char *buf, size_t len)
{
return iiod->ops->read(buf, len);
}
ssize_t tinyiiod_read_line(struct tinyiiod *iiod, char *buf, size_t len)
{
char ch;
uint32_t i;
bool found = false;
int32_t ret;
if (iiod->ops->read_line)
return iiod->ops->read_line(buf, len);
for (i = 0; i < len - 1; i++) {
ret = iiod->ops->read(&ch, 1);
if (ret <= 0)
return -EIO;
buf[i] = ch;
if (buf[i] != '\n')
found = true;
else if (found)
break;
}
if (!found || i == len - 1) {
/* No \n found -> garbage data */
return -EIO;
}
buf[i - 1] = '\0';
return i;
}
ssize_t tinyiiod_write_char(struct tinyiiod *iiod, char c)
{
return iiod->ops->write(&c, 1);
}
ssize_t tinyiiod_write(struct tinyiiod *iiod, const char *data, size_t len)
{
return iiod->ops->write(data, len);
}
ssize_t tinyiiod_write_string(struct tinyiiod *iiod, const char *str)
{
return tinyiiod_write(iiod, str, strlen(str));
}
ssize_t tinyiiod_write_value(struct tinyiiod *iiod, int32_t value)
{
char buf[16];
snprintf(buf, sizeof(buf), "%"PRIi32"\n", value);
return tinyiiod_write_string(iiod, buf);
}
void tinyiiod_write_xml(struct tinyiiod *iiod)
{
char *xml;
iiod->ops->get_xml(&xml);
size_t len = strlen(xml);
tinyiiod_write_value(iiod, len);
tinyiiod_write(iiod, xml, len);
tinyiiod_write_char(iiod, '\n');
}
void tinyiiod_do_read_attr(struct tinyiiod *iiod, const char *device,
const char *channel, bool ch_out, const char *attr, enum iio_attr_type type)
{
ssize_t ret;
if (channel)
ret = iiod->ops->ch_read_attr(device, channel,
ch_out, attr, iiod->buf, IIOD_BUFFER_SIZE);
else
ret = iiod->ops->read_attr(device, attr,
iiod->buf, IIOD_BUFFER_SIZE, type);
tinyiiod_write_value(iiod, (int32_t) ret);
if (ret > 0) {
iiod->buf[ret] = '\n';
tinyiiod_write(iiod, iiod->buf, (size_t) ret + 1);
}
}
void tinyiiod_do_write_attr(struct tinyiiod *iiod, const char *device,
const char *channel, bool ch_out, const char *attr,
size_t bytes, enum iio_attr_type type)
{
ssize_t ret;
if (bytes > IIOD_BUFFER_SIZE - 1)
bytes = IIOD_BUFFER_SIZE - 1;
tinyiiod_read(iiod, iiod->buf, bytes);
iiod->buf[bytes] = '\0';
if (channel)
ret = iiod->ops->ch_write_attr(device, channel, ch_out,
attr, iiod->buf, bytes);
else
ret = iiod->ops->write_attr(device, attr, iiod->buf, bytes, type);
tinyiiod_write_value(iiod, (int32_t) ret);
}
void tinyiiod_do_open(struct tinyiiod *iiod, const char *device,
size_t sample_size, uint32_t mask, bool cyclic)
{
int32_t ret = iiod->ops->open(device, sample_size, mask, cyclic);
tinyiiod_write_value(iiod, ret);
}
void tinyiiod_do_close(struct tinyiiod *iiod, const char *device)
{
int32_t ret = iiod->ops->close(device);
tinyiiod_write_value(iiod, ret);
}
int32_t tinyiiod_do_open_instance(struct tinyiiod *iiod)
{
if (iiod->ops->open_instance)
return iiod->ops->open_instance();
return 0;
}
int32_t tinyiiod_do_close_instance(struct tinyiiod *iiod)
{
if (iiod->ops->close_instance)
return iiod->ops->close_instance();
return 0;
}
int32_t tinyiiod_do_writebuf(struct tinyiiod *iiod,
const char *device, size_t bytes_count)
{
size_t bytes, offset = 0, total_bytes = bytes_count;
char buf[256];
int32_t ret = 0;
tinyiiod_write_value(iiod, bytes_count);
while (bytes_count) {
bytes = bytes_count > sizeof(buf) ? sizeof(buf) : bytes_count;
ret = tinyiiod_read(iiod, buf, bytes);
if (ret > 0) {
ret = iiod->ops->write_data(device, buf, offset, ret);
offset += ret;
if (ret < 0)
return ret;
bytes_count -= ret;
} else
return ret;
}
if (iiod->ops->transfer_mem_to_dev) {
ret = iiod->ops->transfer_mem_to_dev(device, total_bytes);
if (ret < 0)
return ret;
}
tinyiiod_write_value(iiod, (int) total_bytes);
return ret;
}
int32_t tinyiiod_do_readbuf(struct tinyiiod *iiod,
const char *device, size_t bytes_count)
{
int32_t ret;
char buf[256];
uint32_t mask;
bool print_mask = true;
size_t offset = 0;
ret = iiod->ops->get_mask(device, &mask);
if (ret < 0) {
return ret;
}
if (iiod->ops->transfer_dev_to_mem) {
ret = iiod->ops->transfer_dev_to_mem(device, bytes_count);
if (ret < 0)
return ret;
}
while (bytes_count) {
size_t bytes = bytes_count > sizeof(buf) ? sizeof(buf) : bytes_count;
ret = (int) iiod->ops->read_data(device, buf, offset, bytes);
offset += bytes;
tinyiiod_write_value(iiod, ret);
if (ret < 0)
return ret;
if (print_mask) {
char buf_mask[10];
snprintf(buf_mask, sizeof(buf_mask), "%08"PRIx32"\n", mask);
tinyiiod_write_string(iiod, buf_mask);
print_mask = false;
}
tinyiiod_write(iiod, buf, (size_t) ret);
bytes_count -= (size_t) ret;
}
return ret;
}
int32_t tinyiiod_set_timeout(struct tinyiiod *iiod, uint32_t timeout)
{
int32_t ret = 0;
if (iiod->ops->set_timeout)
ret = iiod->ops->set_timeout(timeout);
tinyiiod_write_value(iiod, ret);
return ret;
}
int32_t tinyiiod_set_buffers_count(struct tinyiiod *iiod, const char *device,
uint32_t buffers_count)
{
int32_t ret = 0;
if (iiod->ops->set_buffers_count)
ret = iiod->ops->set_buffers_count(device, buffers_count);
tinyiiod_write_value(iiod, ret);
return ret;
}