From 48786a730bbbdd75719069e81e620eb916e98be0 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 14 Oct 2016 13:00:30 +0200 Subject: C/C++ API: make connection and disconnection more robust Signed-off-by: Szczepan Zalega --- NitrokeyManager.cc | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc index 872ebb4..e5d912d 100644 --- a/NitrokeyManager.cc +++ b/NitrokeyManager.cc @@ -45,7 +45,7 @@ namespace nitrokey{ } bool NitrokeyManager::connect() { - device = nullptr; + this->disconnect(); vector< shared_ptr > devices = { make_shared(), make_shared() }; for( auto & d : devices ){ if (d->connect()){ @@ -57,7 +57,8 @@ namespace nitrokey{ bool NitrokeyManager::connect(const char *device_model) { - switch (device_model[0]){ + this->disconnect(); + switch (device_model[0]){ case 'P': device = make_shared(); break; @@ -78,7 +79,12 @@ namespace nitrokey{ } bool NitrokeyManager::disconnect() { - return device->disconnect(); + if (device == nullptr){ + return false; + } + const auto res = device->disconnect(); + device = nullptr; + return res; } void NitrokeyManager::set_debug(bool state) { -- cgit v1.2.3