From 2079037ae4e0d25f0317e012ca64929c654dde21 Mon Sep 17 00:00:00 2001 From: Paul Hollinsky Date: Wed, 24 Jun 2020 22:15:22 -0400 Subject: [PATCH] POSIX FTDI: Nicer error message for when device is in use Commonly, the device is being used by another libicsneo program (icsscand, maybe) or passed through to a VM. --- api/icsneocpp/event.cpp | 3 +++ include/icsneo/api/event.h | 1 + platform/posix/ftdi.cpp | 6 +++++- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/api/icsneocpp/event.cpp b/api/icsneocpp/event.cpp index 49fa47f..2cd95ed 100644 --- a/api/icsneocpp/event.cpp +++ b/api/icsneocpp/event.cpp @@ -101,6 +101,7 @@ static constexpr const char* DRIVER_FAILED_TO_OPEN = "The device driver encounte static constexpr const char* DRIVER_FAILED_TO_CLOSE = "The device driver encountered a low-level error while closing the device."; static constexpr const char* PACKET_CHECKSUM_ERROR = "There was a checksum error while decoding a packet. The packet was dropped."; static constexpr const char* TRANSMIT_BUFFER_FULL = "The transmit buffer is full and the device is set to non-blocking."; +static constexpr const char* DEVICE_IN_USE = "The device is currently in use by another program."; static constexpr const char* PCAP_COULD_NOT_START = "The PCAP driver could not be started. Ethernet devices will not be found."; static constexpr const char* PCAP_COULD_NOT_FIND_DEVICES = "The PCAP driver failed to find devices. Ethernet devices will not be found."; static constexpr const char* PACKET_DECODING = "The packet could not be decoded."; @@ -197,6 +198,8 @@ const char* APIEvent::DescriptionForType(Type type) { return PACKET_CHECKSUM_ERROR; case Type::TransmitBufferFull: return TRANSMIT_BUFFER_FULL; + case Type::DeviceInUse: + return DEVICE_IN_USE; case Type::PCAPCouldNotStart: return PCAP_COULD_NOT_START; case Type::PCAPCouldNotFindDevices: diff --git a/include/icsneo/api/event.h b/include/icsneo/api/event.h index c2f48fe..fdc5a6b 100644 --- a/include/icsneo/api/event.h +++ b/include/icsneo/api/event.h @@ -78,6 +78,7 @@ public: DriverFailedToClose = 0x3003, PacketChecksumError = 0x3004, TransmitBufferFull = 0x3005, + DeviceInUse = 0x3006, PCAPCouldNotStart = 0x3102, PCAPCouldNotFindDevices = 0x3103, PacketDecodingError = 0x3104, diff --git a/platform/posix/ftdi.cpp b/platform/posix/ftdi.cpp index 317d94d..9e387f2 100644 --- a/platform/posix/ftdi.cpp +++ b/platform/posix/ftdi.cpp @@ -56,7 +56,11 @@ bool FTDI::open() { // At this point the handle has been checked to be within the bounds of the handles array std::tuple& handle = handles[device.handle]; - if(ftdi.openDevice(std::get<0>(handle), std::get<1>(handle).c_str()) != 0) { + const int openError = ftdi.openDevice(std::get<0>(handle), std::get<1>(handle).c_str()); + if(openError == -5) { // Unable to claim device + report(APIEvent::Type::DeviceInUse, APIEvent::Severity::Error); + return false; + } else if(openError != 0) { report(APIEvent::Type::DriverFailedToOpen, APIEvent::Severity::Error); return false; }