/
serio.c
409 lines (354 loc) · 9.38 KB
/
serio.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#include <stdio.h>
#include <termios.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdint.h>
#include "convert.h"
#define STX 2
#define ETX 3
#define ESC 27
enum Code {
REQ,
IND,
ACK,
NAK
};
enum Cmd {
SMARTIO_GET_NO_OF_MODULES = 1,
SMARTIO_GET_NO_OF_ATTRIBUTES,
SMARTIO_GET_ATTRIBUTE_DEFINITION,
SMARTIO_GET_ATTR_VALUE,
SMARTIO_SET_ATTR_VALUE,
SMARTIO_GET_STRING
};
/* message format:
STX
size (from this byte up to but not including ETX)
header
data payload
CRC-16, MSB first
ETX
*/
/* The attribute definition */
#define IO_IS_INPUT 0x80
#define IO_IS_OUTPUT 0x40
#define IO_IS_DEVICE 0x20
#define IO_IS_DIR 0x10
struct dev_attr_info {
uint8_t flags; /* */
uint8_t arraySize;
uint8_t type;
};
void unescape_buffer(unsigned char *buf, int size);
static void write_buf(int fd, unsigned char *buf, int size);
static int read_buf(int fd, unsigned char *buf, const unsigned int max_size);
static void dump_buf(const unsigned char *buf);
int read_no_of_modules(int fd);
int read_no_of_attributes(int fd, unsigned int module);
void read_attr_def(int fd, unsigned int module, unsigned int attr_ix, struct dev_attr_info *attr);
void read_attr_value(int fd, unsigned int module,
unsigned int attr_ix, unsigned int arr_ix,
unsigned int type);
char *serport = "/dev/ttyUSB0";
int trans_id = 4;
int main(int argc, char *argv[])
{
struct termios settings;
const int fd = open(serport, O_RDWR);
int modules;
int i;
if (fd < 0) {
printf("Failed to open %s due to: %s\n", serport, strerror(errno));
goto failed_open;
}
if (tcgetattr(fd, &settings) < 0) {
perror("Failed to get attributes\n");
goto failed_attr;
}
printf("before: c_lflag = %x, c_cflag = %x\n", settings.c_lflag, settings.c_cflag);
if (cfsetspeed(&settings, B9600) < 0) {
perror("Failed to set attributes\n");
goto failed_attr;
}
settings.c_cflag |= CLOCAL;
settings.c_cflag &= ~CSTOPB;
settings.c_cflag &= ~CSIZE;
settings.c_cflag |= CS8;
printf("after: c_lflag = %x, c_cflag = %x\n", settings.c_lflag, settings.c_cflag);
if (tcsetattr(fd, TCSANOW, &settings) < 0) {
perror("Failed to set attributes\n");
goto failed_attr;
}
modules = read_no_of_modules(fd);
for (i=0; i < modules; i++) {
const int attrs = read_no_of_attributes(fd, i);
int j;
for (j=0; j < attrs; j++) {
struct dev_attr_info attr;
read_attr_def(fd, i, j, &attr);
read_attr_value(fd, i, j, 0xFF, attr.type);
}
}
failed_attr:
close(fd);
failed_open:
return 0;
}
int read_no_of_modules(int fd)
{
unsigned char get_no_of_modules[] = {
0,
(REQ << 4),
0, // MODULE 0
SMARTIO_GET_NO_OF_MODULES
};
int isOK;
char node_name[20];
unsigned char readbuf[100];
int no_of_modules = -1;
printf("Sending no_of_modules request.\n");
write_buf(fd, get_no_of_modules, sizeof get_no_of_modules);
isOK = read_buf(fd, readbuf, sizeof readbuf);
printf("read status: %s\n", isOK ? "OK" : "FAIL");
if (isOK) {
const int cmd_status = readbuf[2];
if (cmd_status == 0) {
const int name_size = readbuf[0]- 7;
no_of_modules = (readbuf[3] << 8) + readbuf[4];
printf("name length: %d\n", name_size);
strncpy(node_name, (char*)readbuf + 5, name_size);
node_name[name_size] = '\0';
printf("name : %s\n", node_name);
printf("number of functions: %d\n", no_of_modules);
}
else {
printf("Cmd failure result: %d\n", cmd_status);
dump_buf(readbuf);
}
}
return no_of_modules;
}
int read_no_of_attributes(int fd, unsigned int module)
{
unsigned char req[] = {
0,
(REQ << 4),
module,
SMARTIO_GET_NO_OF_ATTRIBUTES
};
int isOK;
char name[20];
unsigned char readbuf[100];
int no_of_attrs = -1;
printf("Sending no_of_attrs request for function %d.\n", module);
write_buf(fd, req, sizeof req);
isOK = read_buf(fd, readbuf, sizeof readbuf);
printf("read status: %s\n", isOK ? "OK" : "FAIL");
if (isOK) {
const int cmd_status = readbuf[2];
if (cmd_status == 0) {
const int name_size = readbuf[0]- 7;
no_of_attrs = (readbuf[3] << 8) + readbuf[4];
printf("name length: %d\n", name_size);
strncpy(name, (char*)readbuf + 5, name_size);
name[name_size] = '\0';
printf("name : %s\n", name);
printf("number of functions: %d\n", no_of_attrs);
}
else {
printf("Cmd failure result: %d\n", cmd_status);
dump_buf(readbuf);
}
}
return no_of_attrs;
}
void read_attr_def(int fd, unsigned int module, unsigned int attr_ix, struct dev_attr_info *attr)
{
unsigned char req[] = {
0,
(REQ << 4),
module,
SMARTIO_GET_ATTRIBUTE_DEFINITION,
attr_ix >> 8,
attr_ix
};
int isOK;
char name[20];
unsigned char readbuf[100];
printf("Sending attrdef request for function %d, attr %d.\n", module, attr_ix);
write_buf(fd, req, sizeof req);
isOK = read_buf(fd, readbuf, sizeof readbuf);
printf("read status: %s\n", isOK ? "OK" : "FAIL");
if (isOK) {
const int cmd_status = readbuf[2];
if (cmd_status == 0) {
const int name_size = readbuf[0]- 8;
attr->flags = readbuf[3];
attr->arraySize = readbuf[4];
attr->type = readbuf[5];
printf("input bit: %c\n", (attr->flags & IO_IS_INPUT) ? '1' : '0');
printf("output bit: %c\n", (attr->flags & IO_IS_OUTPUT) ? '1' : '0');
printf("device bit: %c\n", (attr->flags & IO_IS_DEVICE) ? '1' : '0');
printf("dir bit: %c\n", (attr->flags & IO_IS_DIR) ? '1' : '0');
printf("Array size: %d\n", (int) attr->arraySize);
printf("Type: %d\n", (int) attr->type);
printf("name length: %d\n", name_size);
strncpy(name, (char*)readbuf + 6, name_size);
name[name_size] = '\0';
printf("name : %s\n", name);
}
else {
printf("Cmd failure result: %d\n", cmd_status);
dump_buf(readbuf);
}
}
}
void read_attr_value(int fd, unsigned int module,
unsigned int attr_ix, unsigned int arr_ix,
unsigned int type)
{
unsigned char req[] = {
0,
(REQ << 4),
module,
SMARTIO_GET_ATTR_VALUE,
attr_ix >> 8,
attr_ix,
arr_ix
};
int isOK;
unsigned char readbuf[100];
printf("Sending attr value request for function %d, attr %d, arr_ix %d.\n",
module, attr_ix, arr_ix);
write_buf(fd, req, sizeof req);
isOK = read_buf(fd, readbuf, sizeof readbuf);
printf("read status: %s\n", isOK ? "OK" : "FAIL");
if (isOK) {
const int cmd_status = readbuf[2];
char value[20];
if (cmd_status == 0) {
const int data_size = readbuf[0] - 5;
int i;
printf("Data size: %d\nData (hex): ", data_size);
for(i=0; i < data_size; i++)
printf("%x ", (int) readbuf[i+3]);
printf("\n");
smartio_raw_to_string(type, readbuf + 3, value);
value[strlen(value)-1] = '\0'; // Get rid of \n at the end
printf("Converted value: '%s'\n", value);
}
else {
printf("Cmd failure result: %d\n", cmd_status);
dump_buf(readbuf);
}
}
}
static void dump_buf(const unsigned char *buf)
{
int i;
printf("Dumping read buffer:\n");
for (i=0; i < buf[0]; i++)
printf("%d: %x (%c)\n", i, (int) buf[i], buf[i]);
printf("\n\n");
}
static void write_buf(int fd, unsigned char *buf, int size)
{
int bytes_written;
int i;
unsigned char escaped_buf[100];
unsigned char *dest = escaped_buf;
buf[1] |= trans_id;
trans_id = (trans_id + 1) % 16;
*dest++ = STX;
for (i=0; i < size; i++) {
if ((buf[i] == STX) || (buf[i] == ETX) || (buf[i] == ESC)) {
*dest++ = ESC;
*dest++ = buf[i] + 0x80;
}
else
*dest++ = buf[i];
}
*dest++ = 0x22; // CRC for now
*dest++ = 0x11;
*dest++ = ETX;
escaped_buf[1] = dest - escaped_buf - 2;
bytes_written = write(fd, escaped_buf, dest - escaped_buf);
if (bytes_written != (dest - escaped_buf)) {
perror("Failed to write message\n");
}
}
static int read_buf(int fd, unsigned char *buf, const unsigned int max_size)
{
unsigned int wr_ix = 0;
int hunting = 1;
int escaping = 0;
printf("Scanning for STX\n");
while (1) {
const int bytes_read = read(fd, buf + wr_ix, 1);
if (bytes_read != 1) {
printf("Error return %d, restarting scanning for STX.\n", bytes_read);
perror("Error reading byte\n");
hunting = 1;
continue;
}
else {
const int c = buf[wr_ix];
printf("Read %d %X %c\n", c, c, (char) c);
}
if (hunting) {
if (buf[wr_ix] == STX) {
printf("Found STX!\n");
hunting = 0;
}
}
else {
#if 0
printf("Read char %d at ix %d\n", (int) buf[wr_ix], wr_ix);
#endif
switch (buf[wr_ix]) {
case STX:
printf("Found STX in mid-stream offset %d, resetting.\n", wr_ix);
wr_ix = 0;
break;
case ESC:
escaping = 1;
break;
case ETX:
return buf[0] == wr_ix;
break;
default:
if (escaping) {
buf[wr_ix] -= 0x80;
escaping = 0;
buf[0]--;
}
wr_ix++;
if (wr_ix >= max_size) {
wr_ix = 0;
hunting = 1;
escaping = 0;
printf("Read beyond buffer size, starting to hunt again.\n");
}
break;
}
}
}
}
void unescape_buffer(unsigned char *buf, int size)
{
int wr_ix = 2;
int rd_ix = 2;
int newSize = buf[1];
while ((rd_ix+1) < size) {
if ((buf[rd_ix] == STX) || (buf[rd_ix] == ETX) || (buf[rd_ix] == ESC)) {
rd_ix++;
newSize--;
buf[wr_ix++] = buf[rd_ix++] - 0x80;
}
else
buf[wr_ix++] = buf[rd_ix++];
}
buf[1] = newSize;
}