Added error checking and removed some redundancy from device isOpen()
parent
2806c935f1
commit
965679c370
|
|
@ -18,11 +18,12 @@ using namespace icsneo;
|
||||||
int Communication::messageCallbackIDCounter = 1;
|
int Communication::messageCallbackIDCounter = 1;
|
||||||
|
|
||||||
bool Communication::open() {
|
bool Communication::open() {
|
||||||
if(isOpen)
|
if(isOpen()) {
|
||||||
return true;
|
err(APIError::DeviceCurrentlyOpen);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
spawnThreads();
|
spawnThreads();
|
||||||
isOpen = true;
|
|
||||||
return impl->open();
|
return impl->open();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -38,15 +39,20 @@ void Communication::joinThreads() {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Communication::close() {
|
bool Communication::close() {
|
||||||
if(!isOpen)
|
if(!isOpen()) {
|
||||||
|
err(APIError::DeviceCurrentlyClosed);
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
isOpen = false;
|
|
||||||
joinThreads();
|
joinThreads();
|
||||||
|
|
||||||
return impl->close();
|
return impl->close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Communication::isOpen() {
|
||||||
|
return impl->isOpen();
|
||||||
|
}
|
||||||
|
|
||||||
bool Communication::sendPacket(std::vector<uint8_t>& bytes) {
|
bool Communication::sendPacket(std::vector<uint8_t>& bytes) {
|
||||||
// This is here so that other communication types (like multichannel) can override it
|
// This is here so that other communication types (like multichannel) can override it
|
||||||
return rawWrite(bytes);
|
return rawWrite(bytes);
|
||||||
|
|
|
||||||
|
|
@ -135,17 +135,14 @@ void Device::enforcePollingMessageLimit() {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Device::open() {
|
bool Device::open() {
|
||||||
if(opened) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!com) {
|
if(!com) {
|
||||||
err(APIError::Unknown);
|
err(APIError::Unknown);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!com->open())
|
if(!com->open()) {
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
auto serial = com->getSerialNumberSync();
|
auto serial = com->getSerialNumberSync();
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
@ -179,10 +176,6 @@ bool Device::open() {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Device::close() {
|
bool Device::close() {
|
||||||
if(!opened) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!com) {
|
if(!com) {
|
||||||
err(APIError::Unknown);
|
err(APIError::Unknown);
|
||||||
return false;
|
return false;
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,7 @@ public:
|
||||||
|
|
||||||
bool open();
|
bool open();
|
||||||
bool close();
|
bool close();
|
||||||
|
bool isOpen();
|
||||||
virtual void spawnThreads();
|
virtual void spawnThreads();
|
||||||
virtual void joinThreads();
|
virtual void joinThreads();
|
||||||
bool rawWrite(const std::vector<uint8_t>& bytes) { return impl->write(bytes); }
|
bool rawWrite(const std::vector<uint8_t>& bytes) { return impl->write(bytes); }
|
||||||
|
|
@ -62,8 +63,6 @@ protected:
|
||||||
std::atomic<bool> closing{false};
|
std::atomic<bool> closing{false};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool isOpen = false;
|
|
||||||
|
|
||||||
std::thread readTaskThread;
|
std::thread readTaskThread;
|
||||||
void readTask();
|
void readTask();
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,7 @@ public:
|
||||||
virtual bool open();
|
virtual bool open();
|
||||||
virtual bool close();
|
virtual bool close();
|
||||||
virtual bool isOnline() const { return online; }
|
virtual bool isOnline() const { return online; }
|
||||||
virtual bool isOpen() const { return opened; }
|
virtual bool isOpen() const { return com->isOpen(); }
|
||||||
virtual bool goOnline();
|
virtual bool goOnline();
|
||||||
virtual bool goOffline();
|
virtual bool goOffline();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -44,8 +44,10 @@ FTDI::FTDI(const device_errorhandler_t& err, neodevice_t& forDevice) : ICommunic
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FTDI::open() {
|
bool FTDI::open() {
|
||||||
if(isOpen())
|
if(isOpen()) {
|
||||||
|
err(APIError::DeviceCurrentlyOpen);
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if(!openable) {
|
if(!openable) {
|
||||||
err(APIError::InvalidNeoDevice);
|
err(APIError::InvalidNeoDevice);
|
||||||
|
|
@ -74,8 +76,10 @@ bool FTDI::open() {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FTDI::close() {
|
bool FTDI::close() {
|
||||||
if(!isOpen())
|
if(!isOpen()) {
|
||||||
|
err(APIError::DeviceCurrentlyClosed);
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
closing = true;
|
closing = true;
|
||||||
|
|
||||||
|
|
@ -86,6 +90,8 @@ bool FTDI::close() {
|
||||||
writeThread.join();
|
writeThread.join();
|
||||||
|
|
||||||
bool ret = ftdi.closeDevice();
|
bool ret = ftdi.closeDevice();
|
||||||
|
if(ret != 0)
|
||||||
|
err(APIError::DriverFailedToClose);
|
||||||
|
|
||||||
uint8_t flush;
|
uint8_t flush;
|
||||||
WriteOperation flushop;
|
WriteOperation flushop;
|
||||||
|
|
@ -156,12 +162,15 @@ int FTDI::FTDIContext::openDevice(int pid, const char* serial) {
|
||||||
bool FTDI::FTDIContext::closeDevice() {
|
bool FTDI::FTDIContext::closeDevice() {
|
||||||
if(context == nullptr)
|
if(context == nullptr)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
|
||||||
if(!deviceOpen)
|
if(!deviceOpen)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
int ret = ftdi_usb_close(context);
|
int ret = ftdi_usb_close(context);
|
||||||
if(ret != 0)
|
if(ret != 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
deviceOpen = false;
|
deviceOpen = false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -191,6 +191,10 @@ std::vector<neodevice_t> STM32::FindByProduct(int product) {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool STM32::open() {
|
bool STM32::open() {
|
||||||
|
if(isOpen()) {
|
||||||
|
err(APIError::DeviceCurrentlyOpen);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << "/dev/ttyACM" << (int)(device.handle - HANDLE_OFFSET);
|
ss << "/dev/ttyACM" << (int)(device.handle - HANDLE_OFFSET);
|
||||||
fd = ::open(ss.str().c_str(), O_RDWR | O_NOCTTY | O_SYNC);
|
fd = ::open(ss.str().c_str(), O_RDWR | O_NOCTTY | O_SYNC);
|
||||||
|
|
@ -205,6 +209,7 @@ bool STM32::open() {
|
||||||
|
|
||||||
if(tcgetattr(fd, &tty) != 0) {
|
if(tcgetattr(fd, &tty) != 0) {
|
||||||
close();
|
close();
|
||||||
|
err(APIError::DriverFailedToOpen);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -227,6 +232,7 @@ bool STM32::open() {
|
||||||
|
|
||||||
if(tcsetattr(fd, TCSAFLUSH, &tty) != 0) { // Flushes input and output buffers as well as setting settings
|
if(tcsetattr(fd, TCSAFLUSH, &tty) != 0) { // Flushes input and output buffers as well as setting settings
|
||||||
close();
|
close();
|
||||||
|
err(APIError::DriverFailedToOpen);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -247,8 +253,10 @@ bool STM32::isOpen() {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool STM32::close() {
|
bool STM32::close() {
|
||||||
if(!isOpen())
|
if(!isOpen()) {
|
||||||
|
err(APIError::DeviceCurrentlyClosed);
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
closing = true;
|
closing = true;
|
||||||
|
|
||||||
|
|
@ -268,7 +276,12 @@ bool STM32::close() {
|
||||||
while (readQueue.try_dequeue(flush)) {}
|
while (readQueue.try_dequeue(flush)) {}
|
||||||
while (writeQueue.try_dequeue(flushop)) {}
|
while (writeQueue.try_dequeue(flushop)) {}
|
||||||
|
|
||||||
return ret == 0;
|
if(ret == 0) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
err(APIError::DriverFailedToClose);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void STM32::readTask() {
|
void STM32::readTask() {
|
||||||
|
|
|
||||||
|
|
@ -194,14 +194,20 @@ PCAP::PCAP(const device_errorhandler_t& err, neodevice_t& forDevice) : ICommunic
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PCAP::open() {
|
bool PCAP::open() {
|
||||||
if(!openable)
|
if(!openable) {
|
||||||
|
err(APIError::InvalidNeoDevice);
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if(!pcap.ok())
|
if(!pcap.ok()) {
|
||||||
|
err(APIError::DriverFailedToOpen);
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if(isOpen())
|
if(isOpen()) {
|
||||||
|
err(APIError::DeviceCurrentlyOpen);
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// Open the interface
|
// Open the interface
|
||||||
interface.fp = pcap.open(interface.nameFromWinPCAP.c_str(), 100, PCAP_OPENFLAG_PROMISCUOUS | PCAP_OPENFLAG_MAX_RESPONSIVENESS, 1, nullptr, errbuf);
|
interface.fp = pcap.open(interface.nameFromWinPCAP.c_str(), 100, PCAP_OPENFLAG_PROMISCUOUS | PCAP_OPENFLAG_MAX_RESPONSIVENESS, 1, nullptr, errbuf);
|
||||||
|
|
@ -222,8 +228,10 @@ bool PCAP::isOpen() {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PCAP::close() {
|
bool PCAP::close() {
|
||||||
if(!isOpen())
|
if(!isOpen()) {
|
||||||
|
err(APIError::DeviceCurrentlyClosed);
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
closing = true; // Signal the threads that we are closing
|
closing = true; // Signal the threads that we are closing
|
||||||
readThread.join();
|
readThread.join();
|
||||||
|
|
|
||||||
|
|
@ -183,8 +183,10 @@ bool VCP::IsHandleValid(neodevice_handle_t handle) {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool VCP::open(bool fromAsync) {
|
bool VCP::open(bool fromAsync) {
|
||||||
if(isOpen() || (!fromAsync && opening))
|
if(isOpen() || (!fromAsync && opening)) {
|
||||||
|
err(APIError::DeviceCurrentlyOpen);
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if(!IsHandleValid(device.handle)) {
|
if(!IsHandleValid(device.handle)) {
|
||||||
err(APIError::DriverFailedToOpen);
|
err(APIError::DriverFailedToOpen);
|
||||||
|
|
@ -216,6 +218,7 @@ bool VCP::open(bool fromAsync) {
|
||||||
COMMTIMEOUTS timeouts;
|
COMMTIMEOUTS timeouts;
|
||||||
if(!GetCommTimeouts(handle, &timeouts)) {
|
if(!GetCommTimeouts(handle, &timeouts)) {
|
||||||
close();
|
close();
|
||||||
|
err(APIError::DriverFailedToOpen);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -288,8 +291,10 @@ void VCP::openAsync(fn_boolCallback callback) {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool VCP::close() {
|
bool VCP::close() {
|
||||||
if(!isOpen())
|
if(!isOpen()) {
|
||||||
|
err(APIError::DeviceCurrentlyClosed);
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
closing = true; // Signal the threads that we are closing
|
closing = true; // Signal the threads that we are closing
|
||||||
for(auto& t : threads)
|
for(auto& t : threads)
|
||||||
|
|
@ -298,8 +303,10 @@ bool VCP::close() {
|
||||||
writeThread.join();
|
writeThread.join();
|
||||||
closing = false;
|
closing = false;
|
||||||
|
|
||||||
if(!CloseHandle(handle))
|
if(!CloseHandle(handle)) {
|
||||||
|
err(APIError::DriverFailedToClose);
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
handle = INVALID_HANDLE_VALUE;
|
handle = INVALID_HANDLE_VALUE;
|
||||||
|
|
||||||
|
|
@ -325,6 +332,9 @@ bool VCP::close() {
|
||||||
while(readQueue.try_dequeue(flush)) {}
|
while(readQueue.try_dequeue(flush)) {}
|
||||||
while(writeQueue.try_dequeue(flushop)) {}
|
while(writeQueue.try_dequeue(flushop)) {}
|
||||||
|
|
||||||
|
if(!ret)
|
||||||
|
err(APIError::DriverFailedToClose);
|
||||||
|
|
||||||
// TODO Set up some sort of shared memory, free which COM port we had open so we can try to open it again
|
// TODO Set up some sort of shared memory, free which COM port we had open so we can try to open it again
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue