-
Notifications
You must be signed in to change notification settings - Fork 9
/
parser.c
281 lines (216 loc) · 6.36 KB
/
parser.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
/*
* 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"
static int32_t parse_rw_string(struct tinyiiod *iiod, char *str, bool write)
{
char *device, *channel, *attr, *ptr;
bool is_channel = false, output = false;
enum iio_attr_type type = IIO_ATTR_TYPE_DEVICE;
long bytes;
ptr = strchr(str, ' ');
if (!ptr)
return -EINVAL;
*ptr = '\0';
device = str;
str = ptr + 1;
if (!strncmp(str, "INPUT ", sizeof("INPUT ") - 1)) {
is_channel = true;
str += sizeof("INPUT ") - 1;
} else if (!strncmp(str, "OUTPUT ", sizeof("OUTPUT ") - 1)) {
is_channel = true;
output = true;
str += sizeof("OUTPUT ") - 1;
} else if (!strncmp(str, "DEBUG ", sizeof("DEBUG ") - 1)) {
type = IIO_ATTR_TYPE_DEBUG;
str += sizeof("DEBUG ") - 1;
} else if (!strncmp(str, "BUFFER ", sizeof("BUFFER ") - 1)) {
type = IIO_ATTR_TYPE_BUFFER;
str += sizeof("BUFFER ") - 1;
}
if (is_channel) {
ptr = strchr(str, ' ');
if (!ptr)
return -EINVAL;
*ptr = '\0';
channel = str;
str = ptr + 1;
} else {
channel = NULL;
}
ptr = strchr(str, ' ');
if ((!!ptr) ^ write)
return -EINVAL;
attr = str;
if (write) {
*ptr = '\0';
str = ptr + 1;
} else {
tinyiiod_do_read_attr(iiod, device, channel,
output, attr, type);
return 0;
}
bytes = strtol(str, &ptr, 10);
if (str == ptr || bytes < 0)
return -EINVAL;
tinyiiod_do_write_attr(iiod, device, channel,
output, attr, (size_t) bytes, type);
return 0;
}
static int32_t parse_open_string(struct tinyiiod *iiod, char *str)
{
char *device, *ptr;
long samples_count;
uint32_t mask = 0;
bool cyclic = false;
ptr = strchr(str, ' ');
if (!ptr)
return -EINVAL;
*ptr = '\0';
device = str;
str = ptr + 1;
samples_count = strtol(str, &ptr, 10);
if (str == ptr || *ptr != ' ' || samples_count < 0)
return -EINVAL;
str = ptr + 1;
mask = strtoul(str, &ptr, 16);
if (*ptr == ' ') {
str = ptr + 1;
if (!strncmp(str, "CYCLIC", sizeof("CYCLIC") - 1))
cyclic = true;
else
return -EINVAL;
}
tinyiiod_do_open(iiod, device, (size_t) samples_count, mask, cyclic);
return 0;
}
static int32_t parse_timeout_string(struct tinyiiod *iiod, char *str)
{
uint32_t timeout = strtoul(str, NULL, 10);
return tinyiiod_set_timeout(iiod, timeout);
}
static int32_t parse_set_string(struct tinyiiod *iiod, char *str)
{
char *device, *ptr;
uint32_t buffers_count;
ptr = strchr(str, ' ');
if (!ptr)
return -EINVAL;
*ptr = '\0';
device = str;
str = ptr + 1;
if (!strncmp(str, "BUFFERS_COUNT ", sizeof("BUFFERS_COUNT ") - 1))
str += sizeof("BUFFERS_COUNT ") - 1;
else
return -EINVAL;
buffers_count = strtol(str, NULL, 10);
return tinyiiod_set_buffers_count(iiod, device, buffers_count);
}
static int32_t parse_writebuf_string(struct tinyiiod *iiod, char *str)
{
char *device, *ptr;
long bytes_count;
ptr = strchr(str, ' ');
if (!ptr)
return -EINVAL;
*ptr = '\0';
device = str;
str = ptr + 1;
bytes_count = strtol(str, &ptr, 10);
if (str == ptr || *ptr != '\0' || bytes_count < 0)
return -EINVAL;
return tinyiiod_do_writebuf(iiod, device, (size_t) bytes_count);
}
static int32_t parse_readbuf_string(struct tinyiiod *iiod, char *str)
{
char *device, *ptr;
long bytes_count;
ptr = strchr(str, ' ');
if (!ptr)
return -EINVAL;
*ptr = '\0';
device = str;
str = ptr + 1;
bytes_count = strtol(str, &ptr, 10);
if (str == ptr || *ptr != '\0' || bytes_count < 0)
return -EINVAL;
return tinyiiod_do_readbuf(iiod, device, (size_t) bytes_count);
}
static int32_t parse_gettrig_string(struct tinyiiod *iiod, char *str)
{
char *device, token[] = " ";
device = strtok(str, token);
if (!device)
return -EINVAL;
return tinyiiod_do_gettrig(iiod, device);
}
static int32_t parse_settrig_string(struct tinyiiod *iiod, char *str)
{
char *device, *trig, token[] = " ";
device = strtok(str, token);
if (!device)
return -EINVAL;
trig = strtok(NULL, token);
if (!trig)
return tinyiiod_do_settrig(iiod, device, "", 0);
return tinyiiod_do_settrig(iiod, device, trig, strlen(trig));
}
int32_t tinyiiod_parse_string(struct tinyiiod *iiod, char *str)
{
while (*str == '\n' || *str == '\r')
str++;
if (str[0] == '\0')
return 0;
if (!strncmp(str, "VERSION", sizeof("VERSION"))) {
char buf[32];
snprintf(buf, sizeof(buf), "%"PRIu16".%"PRIu16".%s\n",
TINYIIOD_VERSION_MAJOR,
TINYIIOD_VERSION_MINOR,
TINYIIOD_VERSION_GIT);
tinyiiod_write_string(iiod, buf);
return 0;
}
if (!strncmp(str, "PRINT", sizeof("PRINT"))) {
tinyiiod_write_xml(iiod);
return 0;
}
if (!strncmp(str, "READ ", sizeof("READ ") - 1))
return parse_rw_string(iiod, str + sizeof("READ ") - 1, false);
if (!strncmp(str, "WRITE ", sizeof("WRITE ") -1))
return parse_rw_string(iiod, str + sizeof("WRITE ") - 1, true);
if (!strncmp(str, "OPEN ", sizeof("OPEN ") -1))
return parse_open_string(iiod, str + sizeof("OPEN ") - 1);
if (!strncmp(str, "CLOSE ", sizeof("CLOSE ") -1)) {
tinyiiod_do_close(iiod, str + sizeof("CLOSE ") - 1);
return 0;
}
if (!strncmp(str, "READBUF ", sizeof("READBUF ") -1))
return parse_readbuf_string(iiod, str + sizeof("READBUF ") - 1);
if (!strncmp(str, "TIMEOUT ", sizeof("TIMEOUT ") - 1))
return parse_timeout_string(iiod, str + sizeof("TIMEOUT ") - 1);
if (!strncmp(str, "WRITEBUF ", sizeof("WRITEBUF ") -1))
return parse_writebuf_string(iiod, str + sizeof("WRITEBUF ") - 1);
if (!strncmp(str, "EXIT", sizeof("EXIT") - 1))
return tinyiiod_do_close_instance(iiod);
if (!strncmp(str, "GETTRIG", sizeof("GETTRIG") - 1))
return parse_gettrig_string(iiod, str + sizeof("GETTRIG ") - 1);
if (!strncmp(str, "SETTRIG", sizeof("SETTRIG") - 1))
return parse_settrig_string(iiod, str + sizeof("SETTRIG ") - 1);
if (!strncmp(str, "SET", sizeof("SET") - 1))
return parse_set_string(iiod, str + sizeof("SET ") - 1);
return -EINVAL;
}