Added error checking and removed some redundancy from device isOpen()

This commit is contained in:
EricLiu2000
2019-06-13 16:01:35 -04:00
parent 2806c935f1
commit 965679c370
8 changed files with 73 additions and 35 deletions
+15 -6
View File
@@ -44,9 +44,11 @@ FTDI::FTDI(const device_errorhandler_t& err, neodevice_t& forDevice) : ICommunic
}
bool FTDI::open() {
if(isOpen())
if(isOpen()) {
err(APIError::DeviceCurrentlyOpen);
return false;
}
if(!openable) {
err(APIError::InvalidNeoDevice);
return false;
@@ -74,9 +76,11 @@ bool FTDI::open() {
}
bool FTDI::close() {
if(!isOpen())
if(!isOpen()) {
err(APIError::DeviceCurrentlyClosed);
return false;
}
closing = true;
if(readThread.joinable())
@@ -86,7 +90,9 @@ bool FTDI::close() {
writeThread.join();
bool ret = ftdi.closeDevice();
if(ret != 0)
err(APIError::DriverFailedToClose);
uint8_t flush;
WriteOperation flushop;
while(readQueue.try_dequeue(flush)) {}
@@ -156,12 +162,15 @@ int FTDI::FTDIContext::openDevice(int pid, const char* serial) {
bool FTDI::FTDIContext::closeDevice() {
if(context == nullptr)
return false;
if(!deviceOpen)
return true;
int ret = ftdi_usb_close(context);
if(ret != 0)
if(ret != 0)
return false;
deviceOpen = false;
return true;
}
+15 -2
View File
@@ -191,6 +191,10 @@ std::vector<neodevice_t> STM32::FindByProduct(int product) {
}
bool STM32::open() {
if(isOpen()) {
err(APIError::DeviceCurrentlyOpen);
return false;
}
std::stringstream ss;
ss << "/dev/ttyACM" << (int)(device.handle - HANDLE_OFFSET);
fd = ::open(ss.str().c_str(), O_RDWR | O_NOCTTY | O_SYNC);
@@ -205,6 +209,7 @@ bool STM32::open() {
if(tcgetattr(fd, &tty) != 0) {
close();
err(APIError::DriverFailedToOpen);
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
close();
err(APIError::DriverFailedToOpen);
return false;
}
@@ -247,8 +253,10 @@ bool STM32::isOpen() {
}
bool STM32::close() {
if(!isOpen())
if(!isOpen()) {
err(APIError::DeviceCurrentlyClosed);
return false;
}
closing = true;
@@ -268,7 +276,12 @@ bool STM32::close() {
while (readQueue.try_dequeue(flush)) {}
while (writeQueue.try_dequeue(flushop)) {}
return ret == 0;
if(ret == 0) {
return true;
} else {
err(APIError::DriverFailedToClose);
return false;
}
}
void STM32::readTask() {