diff options
author | Szczepan Zalega <szczepan@nitrokey.com> | 2017-03-29 12:22:37 +0200 |
---|---|---|
committer | Szczepan Zalega <szczepan@nitrokey.com> | 2017-03-29 12:49:14 +0200 |
commit | c196b45789377dc2cb76a203853695928872dbb4 (patch) | |
tree | baa78ecddf5e0a8f5ab7485227aa6706d407cfd5 | |
parent | 887929bfdefd5894bce2de6821a948b205915fcc (diff) | |
download | libnitrokey-c196b45789377dc2cb76a203853695928872dbb4.tar.gz libnitrokey-c196b45789377dc2cb76a203853695928872dbb4.tar.bz2 |
Allow to change device commands delays runtime
Signed-off-by: Szczepan Zalega <szczepan@nitrokey.com>
-rw-r--r-- | NitrokeyManager.cc | 11 | ||||
-rw-r--r-- | device.cc | 37 | ||||
-rw-r--r-- | include/NitrokeyManager.h | 2 | ||||
-rw-r--r-- | include/device.h | 12 |
4 files changed, 58 insertions, 4 deletions
diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc index 12f4671..ec74f18 100644 --- a/NitrokeyManager.cc +++ b/NitrokeyManager.cc @@ -69,6 +69,14 @@ namespace nitrokey{ NitrokeyManager::~NitrokeyManager() { } + bool NitrokeyManager::set_current_device_speed(int retry_delay, int send_receive_delay){ + std::lock_guard<std::mutex> lock(mex_dev_com); + if(device != nullptr){ + device->set_receiving_delay(std::chrono::duration<int, std::milli>(send_receive_delay)); + device->set_retry_delay(std::chrono::duration<int, std::milli>(retry_delay)); + } + } + bool NitrokeyManager::connect() { std::lock_guard<std::mutex> lock(mex_dev_com); vector< shared_ptr<Device> > devices = { make_shared<Stick10>(), make_shared<Stick20>() }; @@ -80,6 +88,9 @@ namespace nitrokey{ return device != nullptr; } + bool NitrokeyManager::set_default_commands_delay(int delay){ + Device::set_default_device_speed(delay); + } bool NitrokeyManager::connect(const char *device_model) { std::lock_guard<std::mutex> lock(mex_dev_com); @@ -8,6 +8,7 @@ #include "include/log.h" #include <mutex> #include "DeviceCommunicationExceptions.h" +#include "device.h" std::mutex mex_dev_com; @@ -16,6 +17,7 @@ using namespace nitrokey::log; using namespace std::chrono; std::atomic_int Device::instances_count{0}; +std::chrono::milliseconds Device::default_delay {0} ; Device::Device(const uint16_t vid, const uint16_t pid, const DeviceModel model, const milliseconds send_receive_delay, const int retry_receiving_count, @@ -172,14 +174,33 @@ Device::~Device() { instances_count--; } +void Device::set_default_device_speed(int delay) { + default_delay = std::chrono::duration<int, std::milli>(delay); +} + + +void Device::set_receiving_delay(const std::chrono::milliseconds delay){ + std::lock_guard<std::mutex> lock(mex_dev_com); + m_send_receive_delay = delay; +} + +void Device::set_retry_delay(const std::chrono::milliseconds delay){ + std::lock_guard<std::mutex> lock(mex_dev_com); + m_retry_timeout = delay; +} + Stick10::Stick10(): Device(0x20a0, 0x4108, DeviceModel::PRO, 100ms, 5, 100ms) - {} + { + setDefaultDelay(); + } Stick20::Stick20(): Device(0x20a0, 0x4109, DeviceModel::STORAGE, 20ms, 20, 20ms) - {} + { + setDefaultDelay(); + } #include <sstream> #define p(x) ss << #x << " " << x << ", "; @@ -206,4 +227,16 @@ std::string Device::ErrorCounters::get_as_string() { p(receiving_error); return ss.str(); } + +void Device::setDefaultDelay() { + LOG(__FUNCTION__, Loglevel::DEBUG_L2); + + auto count = default_delay.count(); + if (count != 0){ + LOG("Setting default delay to " + std::to_string(count), Loglevel::DEBUG_L2); + m_retry_timeout = default_delay; + m_send_receive_delay = default_delay; + } +} + #undef p diff --git a/include/NitrokeyManager.h b/include/NitrokeyManager.h index 4f11314..9afe995 100644 --- a/include/NitrokeyManager.h +++ b/include/NitrokeyManager.h @@ -45,6 +45,7 @@ namespace nitrokey { bool disconnect(); bool is_connected() throw() ; bool could_current_device_be_enumerated(); + bool set_default_commands_delay(int delay); DeviceModel get_connected_device_model() const; void set_debug(bool state); @@ -169,6 +170,7 @@ namespace nitrokey { bool _disconnect_no_lock(); + bool set_current_device_speed(int retry_delay, int send_receive_delay); }; } diff --git a/include/device.h b/include/device.h index 7b300e5..674d66b 100644 --- a/include/device.h +++ b/include/device.h @@ -96,6 +96,11 @@ public: void set_last_command_status(uint8_t _err) { last_command_status = _err;} ; bool last_command_sucessfull() const {return last_command_status == 0;}; DeviceModel get_device_model() const {return m_model;} + void set_receiving_delay(std::chrono::milliseconds delay); + void set_retry_delay(std::chrono::milliseconds delay); + static void set_default_device_speed(int delay); + void setDefaultDelay(); + private: std::atomic<uint8_t> last_command_status; void _reconnect(); @@ -114,18 +119,21 @@ protected: */ const int m_retry_sending_count; const int m_retry_receiving_count; - const std::chrono::milliseconds m_retry_timeout; - const std::chrono::milliseconds m_send_receive_delay; + std::chrono::milliseconds m_retry_timeout; + std::chrono::milliseconds m_send_receive_delay; std::atomic<hid_device *>mp_devhandle; static std::atomic_int instances_count; + + static std::chrono::milliseconds default_delay ; }; class Stick10 : public Device { public: Stick10(); + }; class Stick20 : public Device { |