From 02f19713c155457e8cc7d80d581bdac35cb1716d Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Thu, 2 Apr 2020 15:14:11 +0200 Subject: NitrokeyManager: Refactor get_serial_number MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To avoid duplicate code, this patch refactors the get_serial_number method to use get_serial_number_as_u32 internally. It does not change get_serial_number’s behavior. --- NitrokeyManager.cc | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc index 496496e..b6a076a 100644 --- a/NitrokeyManager.cc +++ b/NitrokeyManager.cc @@ -380,22 +380,16 @@ using nitrokey::misc::strcpyT; string NitrokeyManager::get_serial_number() { - if (device == nullptr) { return ""; }; - switch (device->get_device_model()) { - case DeviceModel::PRO: { - auto response = GetStatus::CommandTransaction::run(device); - return nitrokey::misc::toHex(response.data().card_serial_u32); - } - break; - - case DeviceModel::STORAGE: - { - auto response = stick20::GetDeviceStatus::CommandTransaction::run(device); - return nitrokey::misc::toHex(response.data().ActiveSmartCardID_u32); + try { + auto serial_number = this->get_serial_number_as_u32(); + if (serial_number == 0) { + return "NA"; + } else { + return nitrokey::misc::toHex(serial_number); } - break; + } catch (DeviceNotConnected& e) { + return ""; } - return "NA"; } uint32_t NitrokeyManager::get_serial_number_as_u32() { -- cgit v1.2.3