From 45a0b1cecde0eeecf8b8be75ec24d1ecc3ad884f Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Wed, 4 Apr 2018 23:56:31 +0200 Subject: Add a NitrokeyManager::connect overload for enum Identifying the model to connect to by the first character of a string is not intuitive. This patch adds an overload for the connect function that accepts a device::DeviceModel enum value, providing a cleaner interface. --- NitrokeyManager.cc | 15 +++++++++++++++ libnitrokey/NitrokeyManager.h | 1 + 2 files changed, 16 insertions(+) diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc index db0c0a9..48196a9 100644 --- a/NitrokeyManager.cc +++ b/NitrokeyManager.cc @@ -273,6 +273,21 @@ using nitrokey::misc::strcpyT; return device->connect(); } + bool NitrokeyManager::connect(device::DeviceModel device_model) { + const char *model_string; + switch (device_model) { + case device::DeviceModel::PRO: + model_string = "P"; + break; + case device::DeviceModel::STORAGE: + model_string = "S"; + break; + default: + throw std::runtime_error("Unknown model"); + } + return connect(model_string); + } + shared_ptr NitrokeyManager::instance() { static std::mutex mutex; std::lock_guard lock(mutex); diff --git a/libnitrokey/NitrokeyManager.h b/libnitrokey/NitrokeyManager.h index ca58d24..1f4cec4 100644 --- a/libnitrokey/NitrokeyManager.h +++ b/libnitrokey/NitrokeyManager.h @@ -80,6 +80,7 @@ char * strndup(const char* str, size_t maxlen); bool connect_with_ID(const std::string id); bool connect_with_path (std::string path); bool connect(const char *device_model); + bool connect(device::DeviceModel device_model); bool connect(); bool disconnect(); bool is_connected() throw() ; -- cgit v1.2.1 From 5a56b83084d797728e5ed557cce2637b3a1e7e56 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Wed, 4 Apr 2018 23:54:07 +0200 Subject: Add NK_login_enum to the C API As with the C++ API, identifying the model by the first character of a string is not intuitive. This patch adds the NK_login_enum function to the C API that accepts an enum value instead of a string. It also adds a new enum NK_device_model as device::DeviceModel is an enum class and therefore cannot be used in C. --- NK_C_API.cc | 16 ++++++++++++++++ NK_C_API.h | 22 ++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/NK_C_API.cc b/NK_C_API.cc index 64355f5..b245940 100644 --- a/NK_C_API.cc +++ b/NK_C_API.cc @@ -161,6 +161,22 @@ extern "C" { return 0; } + NK_C_API int NK_login_enum(NK_device_model device_model) { + const char *model_string; + switch (device_model) { + case NK_PRO: + model_string = "P"; + break; + case NK_STORAGE: + model_string = "S"; + break; + default: + /* no such enum value -- return error code */ + return 0; + } + return NK_login(model_string); + } + NK_C_API int NK_logout() { auto m = NitrokeyManager::instance(); return get_without_result([&]() { diff --git a/NK_C_API.h b/NK_C_API.h index 27730c9..b78bde5 100644 --- a/NK_C_API.h +++ b/NK_C_API.h @@ -34,6 +34,21 @@ #ifdef __cplusplus extern "C" { #endif + + /** + * The Nitrokey device models supported by the API. + */ + enum NK_device_model { + /** + * Nitrokey Pro. + */ + NK_PRO, + /** + * Nitrokey Storage. + */ + NK_STORAGE + }; + /** * Set debug level of messages written on stderr * @param state state=True - most messages, state=False - only errors level @@ -53,6 +68,13 @@ extern "C" { */ NK_C_API int NK_login(const char *device_model); + /** + * Connect to device of given model. Currently library can be connected only to one device at once. + * @param device_model NK_device_model: NK_PRO: Nitrokey Pro, NK_STORAGE: Nitrokey Storage + * @return 1 if connected, 0 if wrong model or cannot connect + */ + NK_C_API int NK_login_enum(NK_device_model 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 -- cgit v1.2.1