From e9c165399bc87e9d283845efdd2d667061b1e323 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 5 Aug 2016 19:50:46 +0200 Subject: Autodetect inserted stick and connect to it Signed-off-by: Szczepan Zalega --- NK_C_API.cc | 8 +++++++- NK_C_API.h | 9 ++++++++- NitrokeyManager.cc | 20 +++++++++++++++++++- include/NitrokeyManager.h | 3 +++ 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/NK_C_API.cc b/NK_C_API.cc index d93283d..4773036 100644 --- a/NK_C_API.cc +++ b/NK_C_API.cc @@ -73,7 +73,7 @@ extern int NK_login(const char *device_model) { auto m = NitrokeyManager::instance(); try { NK_last_command_status = 0; - m->connect(device_model); + return m->connect(device_model); } catch (CommandFailedException & commandFailedException){ NK_last_command_status = commandFailedException.last_command_status; @@ -432,6 +432,12 @@ extern int NK_is_AES_supported(const char *user_password) { }); } +extern int NK_login_auto() { + auto m = NitrokeyManager::instance(); + return get_with_result([&](){ + return (uint8_t) m->connect(); + }); +} } diff --git a/NK_C_API.h b/NK_C_API.h index 43a2e34..5960885 100644 --- a/NK_C_API.h +++ b/NK_C_API.h @@ -17,10 +17,16 @@ extern void NK_set_debug(bool state); /** * Connect to device of given model. Currently library can be connected only to one device at once. * @param device_model char 'S': Nitrokey Storage, 'P': Nitrokey Pro - * @return command processing error code + * @return 1 if connected, 0 if wrong model or cannot connect */ extern int NK_login(const char *device_model); +/** + * Connect to first available device, starting checking from Pro 1st to Storage 2nd. + * @return 1 if connected, 0 if wrong model or cannot connect + */ +extern int NK_login_auto(); + /** * Disconnect from the device. * @return command processing error code @@ -311,6 +317,7 @@ extern int NK_erase_password_safe_slot(uint8_t slot_number); * @return 0 for no and 1 for yes */ extern int NK_is_AES_supported(const char *user_password); + } diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc index ed9c7b4..79efc49 100644 --- a/NitrokeyManager.cc +++ b/NitrokeyManager.cc @@ -1,5 +1,6 @@ #include #include +#include #include "include/NitrokeyManager.h" namespace nitrokey{ @@ -35,7 +36,24 @@ namespace nitrokey{ NitrokeyManager::NitrokeyManager(): device(nullptr) { set_debug(true); } - NitrokeyManager::~NitrokeyManager() {delete _instance; delete device;} + NitrokeyManager::~NitrokeyManager() { + delete device; + } + + bool NitrokeyManager::connect() { + device = nullptr; + vector devices = { new Stick10(), new Stick20() }; + for( auto d : devices ){ + if (device != nullptr){ + delete d; + } + if (device == nullptr && d->connect()){ + device = d; + } + } + return device != nullptr; + } + bool NitrokeyManager::connect(const char *device_model) { switch (device_model[0]){ diff --git a/include/NitrokeyManager.h b/include/NitrokeyManager.h index 7bc2673..be367fd 100644 --- a/include/NitrokeyManager.h +++ b/include/NitrokeyManager.h @@ -35,6 +35,7 @@ namespace nitrokey { bool erase_totp_slot(uint8_t slot_number, const char *temporary_password); bool erase_hotp_slot(uint8_t slot_number, const char *temporary_password); bool connect(const char *device_model); + bool connect(); bool disconnect(); void set_debug(bool state); string get_status(); @@ -97,6 +98,8 @@ namespace nitrokey { template void change_PIN_general(char *current_PIN, char *new_PIN); + + }; } -- cgit v1.2.3