From 17309629889269e99d14b4e880675682d8b6435f Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Mon, 11 Dec 2017 10:51:53 +0100 Subject: Allow to execute client-supplied function on each retry and cancel with it Signed-off-by: Szczepan Zalega --- NitrokeyManager.cc | 6 ++++++ hidapi | 2 +- include/NitrokeyManager.h | 2 +- include/device_proto.h | 5 +++++ unittest/conftest.py | 7 ++++--- 5 files changed, 17 insertions(+), 5 deletions(-) diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc index b29b27d..6c3634e 100644 --- a/NitrokeyManager.cc +++ b/NitrokeyManager.cc @@ -12,6 +12,8 @@ std::mutex nitrokey::proto::send_receive_mtx; +nitrokey::proto::retry_type nitrokey::proto::on_retry = [](int){return 0;}; + namespace nitrokey{ std::mutex mex_dev_com_manager; @@ -61,6 +63,10 @@ using nitrokey::misc::strcpyT; NitrokeyManager::~NitrokeyManager() { } + void NitrokeyManager::set_retry_function(const nitrokey::proto::retry_type retry_func){ + nitrokey::proto::on_retry = retry_func; + } + bool NitrokeyManager::set_current_device_speed(int retry_delay, int send_receive_delay){ if (retry_delay < 20 || send_receive_delay < 20){ LOG("Delay set too low: " + to_string(retry_delay) +" "+ to_string(send_receive_delay), Loglevel::WARNING); diff --git a/hidapi b/hidapi index b767b43..b24dd9c 160000 --- a/hidapi +++ b/hidapi @@ -1 +1 @@ -Subproject commit b767b43f3e6f9c5b92ea7d738331deb8e03c4baf +Subproject commit b24dd9c36c4aacfd5ba7b1f0277d98bbbee43f1b diff --git a/include/NitrokeyManager.h b/include/NitrokeyManager.h index de14fbc..e4842c2 100644 --- a/include/NitrokeyManager.h +++ b/include/NitrokeyManager.h @@ -179,8 +179,8 @@ char * strndup(const char* str, size_t maxlen); public: bool set_current_device_speed(int retry_delay, int send_receive_delay); void set_loglevel(Loglevel loglevel); - void set_loglevel(int loglevel); + void set_retry_function(const retry_type retry_func); }; } diff --git a/include/device_proto.h b/include/device_proto.h index 388c721..89fed12 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -1,6 +1,7 @@ #ifndef DEVICE_PROTO_H #define DEVICE_PROTO_H +#include #include #include #include @@ -38,6 +39,8 @@ namespace nitrokey { namespace proto { extern std::mutex send_receive_mtx; + using retry_type = std::function ; + extern retry_type on_retry; /* @@ -362,6 +365,8 @@ namespace nitrokey { dev->m_counters.total_retries++; LOG(".", Loglevel::DEBUG_L1); std::this_thread::sleep_for(retry_timeout); + if (on_retry(receiving_retry_counter) == -1) + throw DeviceReceivingFailure("User canceled"); continue; } if (successful_communication) break; diff --git a/unittest/conftest.py b/unittest/conftest.py index a53b005..d54a362 100644 --- a/unittest/conftest.py +++ b/unittest/conftest.py @@ -56,7 +56,8 @@ def C(request): print("No library file found") sys.exit(1) - C.NK_set_debug(False) + C.NK_set_debug(True) + C.NK_set_debug_level(4) nk_login = C.NK_login_auto() if nk_login != 1: print('No devices detected!') @@ -77,7 +78,7 @@ def C(request): print('Finished') request.addfinalizer(fin) - # C.NK_set_debug(True) - C.NK_set_debug_level(3) + C.NK_set_debug(True) + C.NK_set_debug_level(4) return C -- cgit v1.2.3