From cb6b2dd65e1f0132353159b83ae05c944d8e62f0 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 31 Jan 2017 18:10:59 +0100 Subject: Make disconnect thread safe. Check is device actually connected by invoking its checking method Signed-off-by: Szczepan Zalega --- NitrokeyManager.cc | 32 ++++++++++++++++++++++++-------- include/NitrokeyManager.h | 3 ++- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc index ee7ca92..3213417 100644 --- a/NitrokeyManager.cc +++ b/NitrokeyManager.cc @@ -100,16 +100,32 @@ namespace nitrokey{ bool NitrokeyManager::disconnect() { std::lock_guard lock(mex_dev_com); - if (!is_connected()){ - return false; - } - const auto res = device->disconnect(); - device = nullptr; - return res; + return _disconnect_no_lock(); } - bool NitrokeyManager::is_connected() const throw(){ - return device != nullptr; + bool NitrokeyManager::_disconnect_no_lock() { + //do not use directly without locked mutex, + //used by is_connected, disconnect + if (device == nullptr){ + return false; + } + const auto res = device->disconnect(); + device = nullptr; + return res; + } + + bool NitrokeyManager::is_connected() throw(){ + std::lock_guard lock(mex_dev_com); + if(device != nullptr){ + auto connected = device->is_connected(); + if(connected){ + return true; + } else { + _disconnect_no_lock(); + return false; + } + } + return false; } void NitrokeyManager::set_debug(bool state) { diff --git a/include/NitrokeyManager.h b/include/NitrokeyManager.h index 3e38cc3..4a98e94 100644 --- a/include/NitrokeyManager.h +++ b/include/NitrokeyManager.h @@ -43,7 +43,7 @@ namespace nitrokey { bool connect(const char *device_model); bool connect(); bool disconnect(); - bool is_connected() const throw() ; + bool is_connected() throw() ; DeviceModel get_connected_device_model() const; void set_debug(bool state); stick10::GetStatus::ResponsePayload get_status(); @@ -156,6 +156,7 @@ namespace nitrokey { bool use_8_digits, bool use_enter, bool use_tokenID, const char *token_ID, const char *temporary_password) const; + bool _disconnect_no_lock(); }; } -- cgit v1.2.3