From c847d68f3c1da4933eb952fe8f7bc254f635e980 Mon Sep 17 00:00:00 2001 From: Paul Hollinsky Date: Mon, 20 Sep 2021 19:30:59 -0400 Subject: [PATCH] POSIX FTDI: Count LIBUSB_ERROR_IO as a disconnection --- include/icsneo/platform/posix/ftdi.h | 1 + platform/posix/ftdi.cpp | 10 ++++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/include/icsneo/platform/posix/ftdi.h b/include/icsneo/platform/posix/ftdi.h index 2b3274d..d67e6b1 100644 --- a/include/icsneo/platform/posix/ftdi.h +++ b/include/icsneo/platform/posix/ftdi.h @@ -54,6 +54,7 @@ private: static std::vector> handles; + static bool ErrorIsDisconnection(int errorCode); void readTask(); void writeTask(); bool openable; // Set to false in the constructor if the object has not been found in searchResultDevices diff --git a/platform/posix/ftdi.cpp b/platform/posix/ftdi.cpp index eabb77c..fac0add 100644 --- a/platform/posix/ftdi.cpp +++ b/platform/posix/ftdi.cpp @@ -185,6 +185,12 @@ bool FTDI::FTDIContext::closeDevice() { return true; } +bool FTDI::ErrorIsDisconnection(int errorCode) { + return errorCode == LIBUSB_ERROR_NO_DEVICE || + errorCode == LIBUSB_ERROR_PIPE || + errorCode == LIBUSB_ERROR_IO; +} + void FTDI::readTask() { constexpr size_t READ_BUFFER_SIZE = 8; uint8_t readbuf[READ_BUFFER_SIZE]; @@ -192,7 +198,7 @@ void FTDI::readTask() { while(!closing && !isDisconnected()) { auto readBytes = ftdi.read(readbuf, READ_BUFFER_SIZE); if(readBytes < 0) { - if(readBytes == LIBUSB_ERROR_NO_DEVICE || readBytes == LIBUSB_ERROR_PIPE) { + if(ErrorIsDisconnection(readBytes)) { if(!isDisconnected()) { disconnected = true; report(APIEvent::Type::DeviceDisconnected, APIEvent::Severity::Error); @@ -215,7 +221,7 @@ void FTDI::writeTask() { while(offset < writeOp.bytes.size()) { auto writeBytes = ftdi.write(writeOp.bytes.data() + offset, (int)writeOp.bytes.size() - offset); if(writeBytes < 0) { - if(writeBytes == LIBUSB_ERROR_NO_DEVICE || writeBytes == LIBUSB_ERROR_PIPE) { + if(ErrorIsDisconnection(writeBytes)) { if(!isDisconnected()) { disconnected = true; report(APIEvent::Type::DeviceDisconnected, APIEvent::Severity::Error);