Skip to content

Commit 4dd0590

Browse files
committed
Added test_long_write.
1 parent b4666d1 commit 4dd0590

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

manual_tests/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
add_subdirectory(test_async_in)
22
add_subdirectory(test_long_read)
3+
add_subdirectory(test_long_write)
34
add_subdirectory(test_transitions)

manual_tests/test_long_read/test_long_read.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ void long_read(libusbp::generic_handle & handle)
2020
size_t transferred;
2121

2222
printf("Reading %d bytes...\n", (unsigned int)transfer_size);
23+
fflush(stdout);
2324
handle.read_pipe(endpoint_address, buffer, sizeof(buffer), &transferred);
2425
if (transferred == transfer_size)
2526
{
@@ -30,6 +31,7 @@ void long_read(libusbp::generic_handle & handle)
3031
printf("Transferred only %d bytes out of %d.\n",
3132
(unsigned int)transferred, (unsigned int)transfer_size);
3233
}
34+
fflush(stdout);
3335
}
3436

3537
void long_control_read(libusbp::generic_handle & handle)
@@ -38,6 +40,7 @@ void long_control_read(libusbp::generic_handle & handle)
3840
size_t transferred;
3941

4042
printf("Performing a slow control read...\n");
43+
fflush(stdout);
4144
handle.control_transfer(0xC0, 0x91, 10000, 5, buffer, sizeof(buffer), &transferred);
4245
if (transferred == 5)
4346
{
@@ -48,6 +51,7 @@ void long_control_read(libusbp::generic_handle & handle)
4851
printf("Transferred only %d bytes out of %d.\n",
4952
(unsigned int)transferred, (unsigned int)sizeof(buffer));
5053
}
54+
fflush(stdout);
5155
}
5256

5357
int main_with_exceptions()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use_cxx11()
2+
3+
add_executable(test_long_write test_long_write.cpp)
4+
5+
include_directories (
6+
"${CMAKE_SOURCE_DIR}/include"
7+
)
8+
9+
target_link_libraries(test_long_write usbp)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/* Tests what happens if we do a synchronous write that takes a long time to
2+
* complete. This can be used to check that pressing Ctrl+C is able to
3+
* interrupt the write. */
4+
5+
#include <libusbp.hpp>
6+
#include <stdio.h>
7+
#include <iostream>
8+
9+
const uint16_t vendor_id = 0x1FFB;
10+
const uint16_t product_id = 0xDA01;
11+
const uint8_t interface_number = 0;
12+
const bool composite = true;
13+
const uint8_t endpoint_address = 0x03;
14+
const size_t packet_size = 32;
15+
const size_t transfer_size = packet_size * 4;
16+
17+
void long_write(libusbp::generic_handle & handle)
18+
{
19+
// First packet causes a delay of 4 seconds (0x0FA0 ms)
20+
uint8_t buffer[transfer_size] = { 0xDE, 0xA0, 0x0F };
21+
size_t transferred;
22+
23+
printf("Writing...\n");
24+
fflush(stdout);
25+
handle.write_pipe(endpoint_address, buffer, sizeof(buffer), &transferred);
26+
if (transferred == transfer_size)
27+
{
28+
printf("Transfer successful.\n");
29+
}
30+
else
31+
{
32+
printf("Transferred only %d bytes out of %d.\n",
33+
(unsigned int)transferred, (unsigned int)transfer_size);
34+
}
35+
}
36+
37+
int main_with_exceptions()
38+
{
39+
libusbp::device device = libusbp::find_device_with_vid_pid(
40+
vendor_id, product_id);
41+
if (!device)
42+
{
43+
std::cerr << "Device not found." << std::endl;
44+
return 1;
45+
}
46+
47+
libusbp::generic_interface gi(device, interface_number, composite);
48+
libusbp::generic_handle handle(gi);
49+
50+
long_write(handle);
51+
52+
return 0;
53+
}
54+
55+
int main(int argc, char ** argv)
56+
{
57+
// Suppress unused parameter warnings.
58+
(void)argc;
59+
(void)argv;
60+
61+
try
62+
{
63+
return main_with_exceptions();
64+
}
65+
catch(const std::exception & error)
66+
{
67+
std::cerr << "Error: " << error.what() << std::endl;
68+
}
69+
return 1;
70+
}

0 commit comments

Comments
 (0)