-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserial_jsd.c
More file actions
150 lines (120 loc) · 2.88 KB
/
Copy pathserial_jsd.c
File metadata and controls
150 lines (120 loc) · 2.88 KB
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
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <sys/ioctl.h>
#define MSG_SIZE 36
int set_interface_attribs(int fd, int speed)
{
struct termios tty;
if (tcgetattr(fd, &tty) < 0) {
printf("Error from tcgetattr: %s\n", strerror(errno));
return -1;
}
cfmakeraw(&tty);
cfsetospeed(&tty, (speed_t)speed);
cfsetispeed(&tty, (speed_t)speed);
tty.c_cc[VMIN] = 0;
tty.c_cc[VTIME] = 1;
if (tcsetattr(fd, TCSANOW, &tty) != 0) {
printf("Error from tcsetattr: %s\n", strerror(errno));
return -1;
}
return 0;
}
void sendBuffer(const unsigned char *buffer)
{
static int outFDs[] = {-1, -1, -1, -1};
char device[16];
int index = buffer[0] & 0x0F;
if (index < 0 || index > 3)
{
printf("Invalid message\n");
return;
}
if (outFDs[index] == -1)
{
sprintf(device, "/dev/serial_js%d", index);
outFDs[index] = open(device, O_RDWR);
}
write(outFDs[index], buffer + 1, MSG_SIZE - 1);
}
int connect(const char *portname)
{
int fd = -1;
while (fd < 0)
{
fd = open(portname, O_RDONLY | O_NOCTTY);
if (fd < 0) {
printf("Error opening %s: %s\n", portname, strerror(errno));
sleep(5);
}
}
/*baudrate 115200, 8 bits, no parity, 1 stop bit */
set_interface_attribs(fd, B115200);
return fd;
}
int main(int argc, char *argv[])
{
int fd;
unsigned char buf[2 * MSG_SIZE];
unsigned char *pBuf = buf;
bool starting = true;
unsigned int emptyCount = 0;
if (argc != 2)
{
printf("Error on number of program arguments %d\n", argc);
return -1;
}
fd = connect(argv[1]);
/* simple noncanonical input */
do {
int rdlen;
rdlen = read(fd, pBuf, sizeof(buf) - (pBuf - buf) - 1);
if (rdlen > 0) {
unsigned char *firstBreak = strchr(buf, '\n');
starting = false;
emptyCount = 0;
pBuf[rdlen] = 0;
pBuf += rdlen;
if (firstBreak == NULL)
{
continue;
}
if (firstBreak - buf == MSG_SIZE)
{
sendBuffer(buf);
}
memmove(buf, firstBreak + 1, (pBuf - firstBreak));
pBuf = strchr(buf, '\0');
memset(pBuf, 0, sizeof(buf) - (pBuf - buf));
}
else if (rdlen < 0 || emptyCount > 2)
{
int flags;
printf("Error from read: %d: %s\n", rdlen, strerror(errno));
close(fd);
starting = true;
emptyCount = 0;
// Try to reconnect
fd = connect(argv[1]);
flags = TIOCM_DTR;
ioctl(fd, TIOCMBIC, &flags);
flags = TIOCM_DTR;
ioctl(fd, TIOCMBIS, &flags);
}
else // rdlen == 0
{
if (starting)
{
sleep(1);
}
emptyCount++;
}
/* repeat read to get full message */
} while (1);
}