-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstdin.cxx
292 lines (235 loc) · 10.4 KB
/
stdin.cxx
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
// papoon_usb: "Not Insane" USB library for STM32F103xx MCUs
// Copyright (C) 2019,2020 Mark R. Rubin
//
// This file is part of papoon_usb.
//
// The papoon_usb 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.
//
// The papoon_usb 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
// (LICENSE.txt) along with the papoon_usb program. If not, see
// <https://www.gnu.org/licenses/gpl.html>
#include <signal.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <libusb.h>
namespace {
static const uint16_t VENDOR = 0x0483, // usb_dev_xxx.cxx
PRODUCT = 0x62e3, // "
MAX_IN_PACKET = 64 ; // usb_dev_simple.hxx
static const uint8_t NUM_IN_OUT_ENDPOINTS = 7, // usb_dev_max_endpts.hxx
IN_ENDPOINT = 0x81, // usb_dev_simple.hxx
OUT_ENDPOINT = 0x02; // "
static const int OUT_TIMEOUT = 1000, // milliseconds? not documented
IN_TIMEOUT = 1000; // in libusb.h
// see usb_dev_max_endpts.hxx
static const uint8_t ENDPOINTS[NUM_IN_OUT_ENDPOINTS] = {7, 2, 13, 9, 1, 15, 5};
const char* const speed_names[] = {"illegal speed code value",
"1.5 Mbit/s (USB LowSpeed)",
"12 Mbit/s (USB FullSpeed)",
"480 Mbit/s (USB HighSpeed)",
"5000 Mbit/s (USB SuperSpeed)",
"10000 Mbit/s (USB SuperSpeedPlus)",
"illegal speed code value",
};
libusb_device_handle *device_handle ;
uint8_t echo[MAX_IN_PACKET + 1]; // terminating '\0'
extern "C" void close(
int signum)
{
if (signum != SIGINT)
return;
std::cout << "\nclose (signal "
<< signum
<< "):"
<< std::endl;
std::cout << "releasing interface ..." << std::endl;
libusb_release_interface(device_handle, 0);
std::cout << "closing device handle ..." << std::endl;
libusb_close(device_handle);
std::cout << "exiting" << std::endl;
exit(signum);
}
} // namespace
int main(
int argc ,
char *argv[])
{
bool use_many = false ;
uint8_t vp_ndx = 1 ,
pended = 7 ; // prepended "cccc+ " + appended '\n'
uint16_t max_in_packet = MAX_IN_PACKET,
max_out_packet = max_in_packet,
vendor = VENDOR ,
product = PRODUCT ;
if (argc < 2 || argv[1][0] != '-') {
std::cerr << "Usage: "
<< argv[0]
<< " -s|-m [vid pid]\n"
<< "-s use with usb_echo_simple.elf\n"
<< "-m use with usb_echo_max_endpts.elf\n"
<< "vid pid vendor id, product id"
<< std::endl;
return 1;
}
if (argv[1][1] == 'm') {
use_many = true;
max_in_packet = 16 ; // see usb_dev_max_endpts.hxx
max_out_packet = 16 ; // " " . "
pended = 9 ; // prepended "e cccc+ " + appended '\n'
vp_ndx = 2 ;
}
uint16_t max_expect_packet = max_out_packet + pended < max_in_packet
? max_out_packet + pended
: max_in_packet ,
max_echo_data = max_in_packet - pended ;
if (argc == vp_ndx + 2) {
vendor = strtol(argv[vp_ndx ], 0, 16);
product = strtol(argv[vp_ndx + 1], 0, 16);
}
int error;
if ((error = libusb_init(0)) != static_cast<int>(LIBUSB_SUCCESS)) {
std::cerr << "libusb_init() failure: "
<< libusb_strerror(static_cast<libusb_error>(error))
<< '('
<< error
<< ')'
<< std::endl;
return error;
}
if (!(device_handle = libusb_open_device_with_vid_pid(0, vendor, product))){
std::cerr << "libusb_open_device_with_vid_pid(0, "
<< std::hex
<< std::setw(4)
<< std::setfill('0')
<< vendor
<< ", "
<< product
<< ") failure"
<< std::endl;
return 1;
}
if ( (error = libusb_set_auto_detach_kernel_driver(device_handle, 1))
!= static_cast<int>(LIBUSB_SUCCESS) ) {
std::cerr << "libusb_set_auto_detach_kernel_driver(device_handle, 1) "
"failure: "
<< libusb_strerror(static_cast<libusb_error>(error))
<< '('
<< error
<< ')'
<< std::endl;
return error;
}
if ( (error = libusb_claim_interface(device_handle, 0))
!= static_cast<int>(LIBUSB_SUCCESS) ) {
std::cerr << "libusb_claim_interface() failure: "
<< libusb_strerror(static_cast<libusb_error>(error))
<< '('
<< error
<< ')'
<< std::endl;
return error;
}
signal(SIGINT, close);
uint8_t out_endpoint = OUT_ENDPOINT,
in_endpoint = IN_ENDPOINT;
for (std::string input ; std::getline(std::cin, input) ; ) {
size_t length = input.length(),
offset = 0 ;
int bytes_transferred ;
if (use_many) {
out_endpoint = ENDPOINTS[rand() % NUM_IN_OUT_ENDPOINTS];
in_endpoint = out_endpoint | 0x80; // high bit, USB API
}
while (length) {
uint8_t *current = reinterpret_cast<uint8_t*>(
const_cast<char*>(
input.c_str()))
+ offset ;
size_t xfer_length
= std::min(length,
static_cast<size_t>(max_out_packet));
if ( (error = libusb_interrupt_transfer(device_handle ,
out_endpoint ,
current ,
xfer_length ,
&bytes_transferred,
OUT_TIMEOUT ))
!= static_cast<int>(LIBUSB_SUCCESS) ) {
std::cerr << "libusb_interrupt_transfer(, "
<< static_cast<unsigned>(out_endpoint)
<< "(OUT_EP), "
<< xfer_length
<< ", <"
<< bytes_transferred
<< ">, "
<< OUT_TIMEOUT
<< ") failure: "
<< libusb_strerror(static_cast<libusb_error>(error))
<< '('
<< error
<< ')'
<< std::endl;
close(error);
}
unsigned fulls = xfer_length / max_echo_data,
extra = xfer_length % max_echo_data,
expect = fulls * max_in_packet ;
if (extra) expect += extra + pended;
bool zero_length_packet = false;
while (expect || zero_length_packet) {
if ( (error = libusb_interrupt_transfer(device_handle ,
in_endpoint ,
echo ,
max_in_packet ,
&bytes_transferred,
IN_TIMEOUT ))
!= static_cast<int>(LIBUSB_SUCCESS) ) {
std::cerr << "libusb_interrupt_transfer(, "
<< static_cast<unsigned>(in_endpoint)
<< "(IN_EP), <"
<< bytes_transferred
<< ">, "
<< IN_TIMEOUT
<< ") failure: "
<< libusb_strerror(static_cast<libusb_error>
(error) )
<< '('
<< error
<< ") expect: "
<< expect
<< " bytes (in max "
<< max_in_packet
<< " sized transfers)"
<< std::endl;
close(error);
}
echo[bytes_transferred] = '\0';
std::cout << echo ;
if (zero_length_packet)
break; // got it, done
// can't be here if expect already zero but check anyway
// errors can cause stuck data to be sent late/unexpectedly
else if ( expect
&& ((expect -= bytes_transferred) == 0)
&& bytes_transferred == max_in_packet )
zero_length_packet = true;
}
std::cout << std::endl;
offset += xfer_length ;
length -= xfer_length ;
} // while (length)
} // main loop
close(SIGINT);
} // main()