-
Notifications
You must be signed in to change notification settings - Fork 2
/
tmc_dev.c
326 lines (222 loc) · 5.51 KB
/
tmc_dev.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
299
300
301
302
/*
***************************************************************************
*
* Author: Teunis van Beelen
*
* Copyright (C) 2015 - 2020 Teunis van Beelen
*
* Email: [email protected]
*
***************************************************************************
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
***************************************************************************
*/
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <linux/usb/tmc.h>
#include <stdio.h>
#include <errno.h>
#include "tmc_dev.h"
#include "utils.h"
#define MAX_CMD_LEN (255)
#define MAX_RESP_LEN (1024 * 1024 * 2)
struct tmcdev * tmcdev_open(const char *device)
{
struct tmcdev *dev;
dev = (struct tmcdev *)calloc(1, sizeof(struct tmcdev));
if(dev == NULL)
{
return NULL;
}
dev->hdrbuf = (char *)calloc(1, MAX_RESP_LEN + 1024);
if(dev->hdrbuf == NULL)
{
free(dev);
return NULL;
}
dev->buf = dev->hdrbuf;
dev->fd = open(device, O_RDWR);
if(dev->fd == -1)
{
free(dev->hdrbuf);
free(dev);
return NULL;
}
return dev;
}
void tmcdev_close(struct tmcdev *dev)
{
if(dev == NULL)
{
return;
}
close(dev->fd);
free(dev->hdrbuf);
free(dev);
}
int tmcdev_write(struct tmcdev *dev, const char *cmd)
{
int i, n, len, qry=0;
char buf[MAX_CMD_LEN + 16],
str[256];
if(dev == NULL)
{
return -1;
}
len = strlen(cmd);
if(len > MAX_CMD_LEN)
{
printf("tmcdev error: command too long\n");
return -1;
}
if(len < 2)
{
printf("tmcdev error: command too short\n");
return -1;
}
if(cmd[len - 1] == '?')
{
qry = 1;
}
strlcpy(buf, cmd, MAX_CMD_LEN + 16);
strlcat(buf, "\n", MAX_CMD_LEN + 16);
if(!(!strncmp(buf, ":TRIG:STAT?", 11) || /* don't print these commands to the console */
!strncmp(buf, ":TRIG:SWE?", 10) || /* because they are used repeatedly */
!strncmp(buf, ":WAV:DATA?", 10) ||
!strncmp(buf, ":WAV:MODE NORM", 14) ||
!strncmp(buf, ":WAV:FORM BYTE", 14) ||
!strncmp(buf, ":WAV:SOUR CHAN", 14) ||
!strncmp(buf, ":TRIG:SWE?", 10) ||
!strncmp(buf, ":ACQ:SRAT?", 10) ||
!strncmp(buf, ":ACQ:MDEP?", 10) ||
!strncmp(buf, ":MEAS:COUN:VAL?", 15) ||
!strncmp(buf, ":FUNC:WREC:OPER?", 16) ||
!strncmp(buf, ":FUNC:WREP:OPER?", 16) ||
!strncmp(buf, ":FUNC:WREP:FMAX?", 16) ||
!strncmp(buf, ":FUNC:WREC:FMAX?", 16) ||
!strncmp(buf, ":FUNC:WREP:FCUR?", 16) ||
!strncmp(buf, ":WAV:XOR?", 9)))
{
printf("tmc_dev write: %s", buf);
}
if((!strncmp(buf, "*RST", 4)) || (!strncmp(buf, ":AUT", 4)))
{
qry = 1;
}
n = write(dev->fd, buf, strlen(buf));
if(n != (len + 1))
{
printf("tmcdev error: device write error");
return -1;
}
if(!qry)
{
for(i=0; i<20; i++)
{
usleep(25000);
n = write(dev->fd, "*OPC?\n", 6);
if(n < 0)
{
printf("tmcdev error: device write error");
return -1;
}
n = read(dev->fd, str, 128);
if(n < 0)
{
printf("tmcdev error: device read error");
return -1;
}
if(n == 2)
{
if(str[0] == '1')
{
break;
}
}
}
}
return len;
}
/*
* TMC Blockheader ::= #NXXXXXX: is used to describe
* the length of the data stream, wherein, # is the start denoter of
* the data stream; N is less than or equal to 9; the N figures
* followed N represents the length of the data stream in bytes.
* For example, #9001152054. Wherein, N is 9 and 001152054
* represents that the data stream contains 1152054 bytes
* effective data.
* Reading from the file descriptor blocks,
* there is a timeout of 5000 milli-Sec.
*/
int tmcdev_read(struct tmcdev *dev)
{
int size, size2, len;
char blockhdr[32];
if(dev == NULL)
{
return -1;
}
dev->hdrbuf[0] = 0;
dev->sz = 0;
size = read(dev->fd, dev->hdrbuf, MAX_RESP_LEN);
if((size < 2) || (size > MAX_RESP_LEN))
{
dev->hdrbuf[0] = 0;
dev->buf[0] = 0;
return -1;
}
dev->hdrbuf[size] = 0;
if(dev->hdrbuf[0] != '#')
{
if(dev->hdrbuf[size - 1] == '\n')
{
dev->hdrbuf[--size] = 0;
}
dev->buf = dev->hdrbuf;
dev->sz = size;
return dev->sz;
}
strncpy(blockhdr, dev->hdrbuf, 16);
len = blockhdr[1] - '0';
if((len < 1) || (len > 9))
{
blockhdr[31] = 0;
return -1;
}
blockhdr[len + 2] = 0;
size2 = atoi(blockhdr + 2);
while(size < size2 && size<MAX_RESP_LEN) // we did not get all the data
{
ssize_t read_size = read(dev->fd, &dev->hdrbuf[size], MAX_RESP_LEN - size);
if(read_size < 1) // timeout or error occurred
{
blockhdr[31] = 0;
return -1;
}
size += read_size;
}
size--; // remove the last character
if(size < size2)
{
blockhdr[31] = 0;
return -1;
}
dev->buf = dev->hdrbuf + len + 2;
dev->sz = size2;
return dev->sz;
}