From 39b7c99bbd00c0bc4906939ceefb724a3d9ddc35 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 26 Jul 2016 11:36:04 +0200 Subject: Handle locking device Signed-off-by: Szczepan Zalega --- NK_C_API.cc | 21 ++++++++++++++++++++- NK_C_API.h | 1 + NitrokeyManager.cc | 4 ++++ include/NitrokeyManager.h | 2 ++ unittest/test_bindings.py | 2 +- 5 files changed, 28 insertions(+), 2 deletions(-) diff --git a/NK_C_API.cc b/NK_C_API.cc index 7de5bbd..c2e7df0 100644 --- a/NK_C_API.cc +++ b/NK_C_API.cc @@ -1,6 +1,6 @@ #include #include "NK_C_API.h" - +#include using namespace nitrokey; static uint8_t NK_last_command_status = 0; @@ -16,6 +16,18 @@ auto get_with_result(T func){ } } +template +uint8_t get_without_result(T func){ + try { + func(); + return 0; + } + catch (CommandFailedException & commandFailedException){ + NK_last_command_status = commandFailedException.last_command_status; + return commandFailedException.last_command_status; + } +} + extern "C" { extern uint8_t NK_get_last_command_status(){ @@ -245,6 +257,13 @@ extern uint8_t NK_get_admin_retry_count(){ }); } +extern int NK_lock_device(){ + auto m = NitrokeyManager::instance(); + return get_without_result([&](){ + return m->lock_device(); + }); +} + } diff --git a/NK_C_API.h b/NK_C_API.h index 75702cc..dbbacae 100644 --- a/NK_C_API.h +++ b/NK_C_API.h @@ -29,6 +29,7 @@ extern uint8_t NK_get_user_retry_count(); extern uint8_t NK_get_admin_retry_count(); extern int NK_enable_password_safe(const char *user_pin); extern int NK_get_password_safe_slot_status(); +extern int NK_lock_device(); } diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc index d684853..15f09b8 100644 --- a/NitrokeyManager.cc +++ b/NitrokeyManager.cc @@ -232,5 +232,9 @@ namespace nitrokey{ return response.password_retry_count; } + void NitrokeyManager::lock_device() { + LockDevice::CommandTransaction::run(*device); + } + } \ No newline at end of file diff --git a/include/NitrokeyManager.h b/include/NitrokeyManager.h index 6588711..ecc397d 100644 --- a/include/NitrokeyManager.h +++ b/include/NitrokeyManager.h @@ -47,6 +47,8 @@ namespace nitrokey { uint8_t get_admin_retry_count(); uint8_t get_user_retry_count(); + void lock_device(); + private: NitrokeyManager(); ~NitrokeyManager(); diff --git a/unittest/test_bindings.py b/unittest/test_bindings.py index 51e1233..ec7608e 100644 --- a/unittest/test_bindings.py +++ b/unittest/test_bindings.py @@ -50,7 +50,7 @@ def C(request): def test_enable_password_safe(C): - # lock device + assert C.NK_lock_device() == DeviceErrorCode.STATUS_OK assert C.NK_enable_password_safe('wrong_password') == DeviceErrorCode.WRONG_PASSWORD assert C.NK_enable_password_safe(DefaultPasswords.USER) == DeviceErrorCode.STATUS_OK -- cgit v1.2.1