summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSzczepan Zalega <szczepan@nitrokey.com>2016-08-05 19:50:46 +0200
committerSzczepan Zalega <szczepan@nitrokey.com>2016-08-05 20:32:45 +0200
commite9c165399bc87e9d283845efdd2d667061b1e323 (patch)
tree8ce834d70805fad5bde2ed8a2aec3b9beba3b37d
parent2675570622b808aec77be240b4663e29598049ef (diff)
downloadlibnitrokey-e9c165399bc87e9d283845efdd2d667061b1e323.tar.gz
libnitrokey-e9c165399bc87e9d283845efdd2d667061b1e323.tar.bz2
Autodetect inserted stick and connect to it
Signed-off-by: Szczepan Zalega <szczepan@nitrokey.com>
-rw-r--r--NK_C_API.cc8
-rw-r--r--NK_C_API.h9
-rw-r--r--NitrokeyManager.cc20
-rw-r--r--include/NitrokeyManager.h3
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,11 +17,17 @@ 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 <cassert>
#include <cstring>
+#include <iostream>
#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<Device*> 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 <typename ProCommand, PasswordKind StoKind>
void change_PIN_general(char *current_PIN, char *new_PIN);
+
+
};
}