diff --git a/communication/communication.cpp b/communication/communication.cpp index 5b413e6..320a03e 100644 --- a/communication/communication.cpp +++ b/communication/communication.cpp @@ -31,8 +31,10 @@ void Communication::spawnThreads() { } void Communication::joinThreads() { + closing = true; if(readTaskThread.joinable()) readTaskThread.join(); + closing = false; } bool Communication::close() { @@ -40,7 +42,6 @@ bool Communication::close() { return false; isOpen = false; - closing = true; joinThreads(); return impl->close(); diff --git a/communication/multichannelcommunication.cpp b/communication/multichannelcommunication.cpp index 276880d..aa4c920 100644 --- a/communication/multichannelcommunication.cpp +++ b/communication/multichannelcommunication.cpp @@ -12,8 +12,10 @@ void MultiChannelCommunication::spawnThreads() { } void MultiChannelCommunication::joinThreads() { + closing = true; if(mainChannelReadThread.joinable()) mainChannelReadThread.join(); + closing = false; } bool MultiChannelCommunication::sendPacket(std::vector& bytes) { diff --git a/device/device.cpp b/device/device.cpp index 1b62fcb..1d6b043 100644 --- a/device/device.cpp +++ b/device/device.cpp @@ -180,6 +180,7 @@ bool Device::close() { if(internalHandlerCallbackID) com->removeMessageCallback(internalHandlerCallbackID); + goOffline(); return com->close(); } diff --git a/platform/posix/ftdi.cpp b/platform/posix/ftdi.cpp index 9c9fb78..741d6fc 100644 --- a/platform/posix/ftdi.cpp +++ b/platform/posix/ftdi.cpp @@ -97,6 +97,8 @@ bool FTDI::close() { if(writeThread.joinable()) writeThread.join(); + closing = false; + if(ftdiDevice.close()) return false; diff --git a/platform/posix/stm32.cpp b/platform/posix/stm32.cpp index 7fca33c..99ce634 100644 --- a/platform/posix/stm32.cpp +++ b/platform/posix/stm32.cpp @@ -252,6 +252,8 @@ bool STM32::close() { if(writeThread.joinable()) writeThread.join(); + closing = false; + int ret = ::close(fd); fd = -1; diff --git a/platform/windows/pcap.cpp b/platform/windows/pcap.cpp index 803af8a..7b3e469 100644 --- a/platform/windows/pcap.cpp +++ b/platform/windows/pcap.cpp @@ -228,6 +228,7 @@ bool PCAP::close() { closing = true; // Signal the threads that we are closing readThread.join(); writeThread.join(); + closing = false; pcap.close(interface.fp); interface.fp = nullptr; diff --git a/platform/windows/vcp.cpp b/platform/windows/vcp.cpp index 7f6f416..e22930f 100644 --- a/platform/windows/vcp.cpp +++ b/platform/windows/vcp.cpp @@ -214,6 +214,7 @@ bool VCP::close() { t->join(); // Wait for the threads to close readThread.join(); writeThread.join(); + closing = false; if(!CloseHandle(handle)) return false; @@ -224,14 +225,17 @@ bool VCP::close() { if(overlappedRead.hEvent != INVALID_HANDLE_VALUE) { if(!CloseHandle(overlappedRead.hEvent)) ret = false; + overlappedRead.hEvent = INVALID_HANDLE_VALUE; } if(overlappedWrite.hEvent != INVALID_HANDLE_VALUE) { if(!CloseHandle(overlappedWrite.hEvent)) ret = false; + overlappedWrite.hEvent = INVALID_HANDLE_VALUE; } if(overlappedWait.hEvent != INVALID_HANDLE_VALUE) { if(!CloseHandle(overlappedWait.hEvent)) ret = false; + overlappedWait.hEvent = INVALID_HANDLE_VALUE; } // TODO Set up some sort of shared memory, free which COM port we had open so we can try to open it again