-
Notifications
You must be signed in to change notification settings - Fork 0
/
acmc.c
142 lines (113 loc) · 2.36 KB
/
acmc.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
/**
* acmc.c - Client for f_acm.c verification
*
* Copyright (C) 2010-2016 Felipe Balbi <[email protected]>
*
* This file is part of the USB Verification Tools Project
*
* USB Tools is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public Liicense as published by
* the Free Software Foundation, either version 3 of the license, or
* (at your option) any later version.
*
* USB Tools 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 USB Tools. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <poll.h>
#include <sys/ioctl.h>
static int tty_init(int fd)
{
int ret = 0;
struct termios term;
tcgetattr(fd, &term);
cfmakeraw(&term);
ret = tcflush(fd, TCIOFLUSH);
if (ret < 0) {
perror("tcflush");
goto out;
}
ret = tcsetattr(fd, TCSANOW, &term);
if (ret < 0) {
perror("tcsetattr");
goto out;
}
out:
return ret;
}
static int doit(int fd, int i)
{
struct pollfd pfd;
char cmd[16];
char reply[16];
ssize_t val;
int ret;
ret = snprintf(cmd, 3, "AT\r");
if (ret < 0) {
perror("snprintf");
goto error;
}
if (write(fd, cmd, 3) != 3) {
perror("write");
goto error;
}
pfd.fd = fd;
pfd.events = POLLIN;
pfd.revents = 0;
ret = poll(&pfd, 1, -1);
if (ret < 1) {
perror("pollin");
goto error;
}
val = read(fd, reply, sizeof(reply));
if (val < 0) {
perror("read");
goto error;
}
printf("%d. cmd %s reply %s\n", i, cmd, reply);
return 0;
error:
printf("failed\n");
return -1;
}
int main(void)
{
int fd;
uintmax_t i;
unsigned control = 0;
fd = open("/dev/ttyACM0", O_RDWR);
if (fd < 0) {
perror ("ACM0");
return -1;
}
/* wait for carrier */
while (1) {
int ret;
ret = ioctl(fd, TIOCMGET, &control);
if (ret < 0) {
perror("ioctl");
close(fd);
goto out;
}
if ((control & TIOCM_CD) &&
(control & TIOCM_DSR))
break;
printf("waiting DCD | DSR\n");
}
if (tty_init(fd))
goto out;
for (i = 1; i < UINTMAX_MAX; i++)
doit(fd, i);
out:
close(fd);
return 0;
}