-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsolable.c
167 lines (157 loc) · 5.97 KB
/
consolable.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
// Copyright (c) 2013 Quanta Research Cambridge, Inc.
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use, copy,
// modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
/*
* Simple tty program for connecting to console on Zync devices.
* Defaults should be fine for zedboard.
* When connecting to ZC702 on Mac:
* ./consolable tty.SLAB_USBtoUART
*/
#include <termios.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/select.h> // in MacOSX, poll() does not work with devices!!
#include <dirent.h>
#include <unistd.h>
#include <libgen.h>
#ifdef __APPLE__
#define TTYCLASS "/dev/tty.usb"
#else
#define TTYCLASS "/dev/ttyACM"
#endif
static struct termios orig_terminfo;
static void signal_handler(int signame)
{
tcsetattr(0, TCSANOW, &orig_terminfo);
printf("[%s:%d] signal %d\n", __FUNCTION__, __LINE__, signame);
exit(0);
}
static char buf[1000];
int main(int argc, char **argv)
{
struct termios terminfo;
struct sigaction sact;
int fd = -1;
int rc;
fd_set fdset;
int number_fds = 1;
int fdlist[2];
struct timeval timeout;
int maxfd = 1;
int fdsindex;
char *basestring = TTYCLASS, *basedir, *basefile;
if (argc == 2)
basestring = argv[1];
fdlist[0] = 0; // stdin
rc = tcgetattr(0, &orig_terminfo);
sact.sa_handler = signal_handler;
memset(&sact.sa_mask, 0, sizeof(sact.sa_mask));
sact.sa_flags = 0;
rc = sigaction(SIGHUP, &sact, NULL);
rc |= sigaction(SIGINT, &sact, NULL);
rc |= sigaction(SIGQUIT, &sact, NULL);
rc |= sigaction(SIGTERM, &sact, NULL);
if (rc == -1) {
printf("[%s:%d] sigaction err %d\n", __FUNCTION__, __LINE__, errno);
exit(-1);
}
rc = tcgetattr(0, &terminfo);
terminfo.c_lflag &= ~(ICANON | ECHO | ISIG);
rc = tcsetattr(0, TCSANOW, &terminfo);
printf("consolable: To exit program, type ctrl-Z\n\n");
basedir = strdup(dirname(strdup(basestring)));
basefile = strdup(basename(strdup(basestring)));
printf("consolable: Waiting for USB device %s: %s %s\n", basestring, basedir, basefile);
while (1) {
if (fd == -1) {
struct dirent *direntp;
DIR *dirptr = opendir(basedir);
if (dirptr) {
while ((direntp = readdir(dirptr))) {
if (!strncmp(direntp->d_name, basefile, strlen(basefile))) {
sprintf(buf, "/dev/%s", direntp->d_name);
fprintf(stderr, "consolable: opening %s\n", buf);
fd = open(buf, O_RDWR | O_NONBLOCK);
fprintf(stderr, "consolable: fd %d\n", fd);
break;
}
}
closedir(dirptr);
}
if (fd >= 0) {
number_fds = 2;
printf("consolable: USB device '%s' opened fd=%d\n", buf, fd);
fdlist[1] = fd;
maxfd = fd+1;
rc = tcgetattr(fd, &terminfo);
terminfo.c_ispeed = B115200;
terminfo.c_ospeed = B115200;
terminfo.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
terminfo.c_cflag &= ~CRTSCTS; // needed for /dev/tty.SLAB_USBtoUART
terminfo.c_iflag = IGNCR;
terminfo.c_lflag = ICANON;
rc = tcsetattr(fd, TCSANOW, &terminfo);
}
}
FD_ZERO(&fdset);
FD_SET(0, &fdset);
if (fd != -1)
FD_SET(fd, &fdset);
timeout.tv_sec = 0;
timeout.tv_usec = 300000;
rc = select(maxfd, &fdset, NULL, NULL, &timeout);
if (rc > 0) {
for (fdsindex = 0; fdsindex < number_fds; fdsindex++) {
if (FD_ISSET(fdlist[fdsindex], &fdset)) {
int len = read(fdlist[fdsindex], buf, sizeof(buf));
if (len == -1) {
if (errno == EWOULDBLOCK)
continue;
if (fdlist[fdsindex] != 0 && (errno == ENXIO || errno == EBADF)) {
printf("consolable: USB device closed\n");
number_fds = 1;
close(fdlist[fdsindex]);
maxfd = 1;
fd = -1;
fdlist[fdsindex] = -1;
continue;
}
signal_handler(999);
exit(-1);
}
int outfd = 1; // stdout
if (fdsindex == 0) {
char *p = buf;
while (p < &buf[len])
if (*p++ == 0x1a) // exit program with ctrl-Z
signal_handler(999);
if (number_fds > 1)
outfd = fdlist[1];
}
write(outfd, buf, len);
}
}
}
}
signal_handler(999);
return 0;
}