Skip to content

Commit 1acac2c

Browse files
feat: retry failed usb operations
1 parent fe429f1 commit 1acac2c

File tree

2 files changed

+10
-36
lines changed

2 files changed

+10
-36
lines changed

src/ledmatrix/ledmatrix.cpp

+10-16
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,11 @@ namespace fw16led::ledmatrix
4747
// Send the data via bulk OUT transfer
4848
int actual_length = 0;
4949
int ret = libusb_bulk_transfer(device.get(), ENDPOINT_OUT, outData.data(), static_cast<int>(outData.size()), &actual_length, TRANSFER_TIMEOUT_MS);
50-
if (ret != LIBUSB_SUCCESS)
51-
{
52-
LOG_WARN("Bulk OUT transfer failed: {}", libusb_strerror(static_cast<libusb_error>(ret)));
53-
}
54-
55-
if (actual_length != static_cast<int>(outData.size()))
50+
if (ret != LIBUSB_SUCCESS || actual_length != static_cast<int>(outData.size()))
5651
{
57-
LOG_WARN("Bulk OUT transfer size does not match. Expected {}; Got {}", outData.size(), actual_length);
52+
LOG_WARN("Bulk OUT transfer failed or size mismatch: {}", libusb_strerror(static_cast<libusb_error>(ret)));
53+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
54+
return send_command(command, parameters);
5855
}
5956
}
6057

@@ -72,15 +69,11 @@ namespace fw16led::ledmatrix
7269
// Send the data via bulk OUT transfer
7370
int actual_length = 0;
7471
int ret = libusb_bulk_transfer(device.get(), ENDPOINT_OUT, outData.data(), static_cast<int>(outData.size()), &actual_length, TRANSFER_TIMEOUT_MS);
75-
if (ret != LIBUSB_SUCCESS)
76-
{
77-
LOG_WARN("Bulk OUT transfer failed: {}", libusb_strerror(static_cast<libusb_error>(ret)));
78-
return {};
79-
}
80-
81-
if (actual_length != static_cast<int>(outData.size()))
72+
if (ret != LIBUSB_SUCCESS || actual_length != static_cast<int>(outData.size()))
8273
{
83-
LOG_WARN("Bulk OUT transfer size does not match. Expected {}; Got {}", outData.size(), actual_length);
74+
LOG_WARN("Bulk OUT transfer failed or size mismatch: {}", libusb_strerror(static_cast<libusb_error>(ret)));
75+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
76+
return send_command_with_response(command, parameters);
8477
}
8578

8679
std::vector<uint8_t> inData(RESPONSE_SIZE, 0);
@@ -90,7 +83,8 @@ namespace fw16led::ledmatrix
9083
if (ret != LIBUSB_SUCCESS)
9184
{
9285
LOG_WARN("Bulk IN transfer failed: {}", libusb_strerror(static_cast<libusb_error>(ret)));
93-
return {};
86+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
87+
return send_command_with_response(command, parameters);
9488
}
9589

9690
// Shrink the buffer to the actual number of bytes read

src/managers/usb.cpp

-20
Original file line numberDiff line numberDiff line change
@@ -81,26 +81,6 @@ namespace fw16led::managers
8181

8282
LOG_DEBUG("-> Successfully claimed interface 1");
8383

84-
// Wait for the device to be ready
85-
bool ready = false;
86-
std::vector<uint8_t> dummy = {0x32, 0xAC, 0x00, 0x00};
87-
while (!ready)
88-
{
89-
int actual_length = 0;
90-
int ret = libusb_bulk_transfer(handle, 0x01, dummy.data(), 4, &actual_length, 100);
91-
if (ret == LIBUSB_SUCCESS && actual_length == 4)
92-
{
93-
ready = true;
94-
}
95-
else
96-
{
97-
LOG_WARN("-> Waiting for device to be ready...");
98-
std::this_thread::sleep_for(std::chrono::milliseconds(100));
99-
}
100-
}
101-
102-
LOG_DEBUG("-> Device is ready");
103-
10484
// Store the handle
10585
ledpanels.push_back(std::make_shared<LedPanel>(std::make_shared<ledmatrix::LedMatrix>(handle)));
10686
}

0 commit comments

Comments
 (0)