From 774f3a0d93bac42aa50d4d30753e00f769b17881 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Wed, 11 Jan 2017 16:09:08 +0100 Subject: Make device configuration const, protect non-const with ::atomic Signed-off-by: Szczepan Zalega --- device.cc | 49 +++++++++++++++++++++++-------------------------- 1 file changed, 23 insertions(+), 26 deletions(-) (limited to 'device.cc') diff --git a/device.cc b/device.cc index 8c3c799..6eaaca5 100644 --- a/device.cc +++ b/device.cc @@ -9,20 +9,26 @@ using namespace nitrokey::device; using namespace nitrokey::log; +using namespace std::chrono; -Device::Device() - : m_vid(0), - m_pid(0), +Device::Device(const uint16_t vid, const uint16_t pid, const DeviceModel model, + const milliseconds send_receive_delay, const int retry_receiving_count, + const milliseconds retry_timeout) + : m_vid(vid), + m_pid(pid), m_retry_sending_count(3), - m_retry_receiving_count(40), - m_retry_timeout(100), + m_retry_receiving_count(retry_receiving_count), + m_retry_timeout(retry_timeout), mp_devhandle(NULL), - last_command_status(0){} + last_command_status(0), + m_model(model), + m_send_receive_delay(send_receive_delay) +{} bool Device::disconnect() { Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2); - if(mp_devhandle== nullptr) return false; + if(mp_devhandle == nullptr) return false; hid_close(mp_devhandle); mp_devhandle = NULL; hid_exit(); @@ -31,9 +37,8 @@ bool Device::disconnect() { bool Device::connect() { Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2); -// hid_init(); +// hid_init(); // done automatically on hid_open mp_devhandle = hid_open(m_vid, m_pid, NULL); - // hid_init(); return mp_devhandle != NULL; } @@ -41,7 +46,7 @@ int Device::send(const void *packet) { Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2); if (mp_devhandle == NULL) - throw std::runtime_error("Attempted HID send on an invalid descriptor."); + throw std::runtime_error("Attempted HID send on an invalid descriptor."); //TODO migrate except to library_error return (hid_send_feature_report( mp_devhandle, (const unsigned char *)(packet), HID_REPORT_SIZE)); @@ -54,7 +59,7 @@ int Device::recv(void *packet) { Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2); if (mp_devhandle == NULL) - throw std::runtime_error("Attempted HID receive on an invalid descriptor."); + throw std::runtime_error("Attempted HID receive on an invalid descriptor."); //TODO migrate except to library_error // FIXME extract error handling and repeating to parent function in // device_proto:192 @@ -84,19 +89,11 @@ int Device::recv(void *packet) { return status; } -Stick10::Stick10() { - m_vid = 0x20a0; - m_pid = 0x4108; - m_model = DeviceModel::PRO; - m_send_receive_delay = 100ms; - m_retry_receiving_count = 100; -} +Stick10::Stick10(): + Device(0x20a0, 0x4108, DeviceModel::PRO, 100ms, 100, 100ms) + {} -Stick20::Stick20() { - m_vid = 0x20a0; - m_pid = 0x4109; - m_retry_timeout = 200ms; - m_model = DeviceModel::STORAGE; - m_send_receive_delay = 200ms; - m_retry_receiving_count = 40; -} + +Stick20::Stick20(): + Device(0x20a0, 0x4109, DeviceModel::STORAGE, 200ms, 40, 200ms) + {} -- cgit v1.2.1 From 2dec896b3f853d2e509c4eb15fe5dd6c28e0a1d7 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Wed, 11 Jan 2017 16:10:30 +0100 Subject: Use lock_guard while accessing hid_api Signed-off-by: Szczepan Zalega --- device.cc | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'device.cc') diff --git a/device.cc b/device.cc index 6eaaca5..7bdbe9a 100644 --- a/device.cc +++ b/device.cc @@ -6,6 +6,9 @@ #include "include/misc.h" #include "include/device.h" #include "include/log.h" +#include + +std::mutex mex_dev_com; using namespace nitrokey::device; using namespace nitrokey::log; @@ -26,6 +29,7 @@ Device::Device(const uint16_t vid, const uint16_t pid, const DeviceModel model, {} bool Device::disconnect() { + std::lock_guard lock(mex_dev_com); Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2); if(mp_devhandle == nullptr) return false; @@ -35,6 +39,7 @@ bool Device::disconnect() { return true; } bool Device::connect() { + std::lock_guard lock(mex_dev_com); Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2); // hid_init(); // done automatically on hid_open @@ -43,6 +48,7 @@ bool Device::connect() { } int Device::send(const void *packet) { + std::lock_guard lock(mex_dev_com); Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2); if (mp_devhandle == NULL) @@ -53,6 +59,7 @@ int Device::send(const void *packet) { } int Device::recv(void *packet) { + std::lock_guard lock(mex_dev_com); int status; int retry_count = 0; -- cgit v1.2.1 From 55745fccf0c4233c536d8bacead3443a8e431b8d Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Wed, 11 Jan 2017 16:40:05 +0100 Subject: Use nullptr instead of NULL Signed-off-by: Szczepan Zalega --- device.cc | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'device.cc') diff --git a/device.cc b/device.cc index 7bdbe9a..6ff370f 100644 --- a/device.cc +++ b/device.cc @@ -22,7 +22,7 @@ Device::Device(const uint16_t vid, const uint16_t pid, const DeviceModel model, m_retry_sending_count(3), m_retry_receiving_count(retry_receiving_count), m_retry_timeout(retry_timeout), - mp_devhandle(NULL), + mp_devhandle(nullptr), last_command_status(0), m_model(model), m_send_receive_delay(send_receive_delay) @@ -34,7 +34,7 @@ bool Device::disconnect() { if(mp_devhandle == nullptr) return false; hid_close(mp_devhandle); - mp_devhandle = NULL; + mp_devhandle = nullptr; hid_exit(); return true; } @@ -43,15 +43,15 @@ bool Device::connect() { Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2); // hid_init(); // done automatically on hid_open - mp_devhandle = hid_open(m_vid, m_pid, NULL); - return mp_devhandle != NULL; + mp_devhandle = hid_open(m_vid, m_pid, nullptr); + return mp_devhandle != nullptr; } int Device::send(const void *packet) { std::lock_guard lock(mex_dev_com); Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2); - if (mp_devhandle == NULL) + if (mp_devhandle == nullptr) throw std::runtime_error("Attempted HID send on an invalid descriptor."); //TODO migrate except to library_error return (hid_send_feature_report( @@ -65,7 +65,7 @@ int Device::recv(void *packet) { Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2); - if (mp_devhandle == NULL) + if (mp_devhandle == nullptr) throw std::runtime_error("Attempted HID receive on an invalid descriptor."); //TODO migrate except to library_error // FIXME extract error handling and repeating to parent function in @@ -76,7 +76,7 @@ int Device::recv(void *packet) { // FIXME handle getting libhid error message somewhere else auto pwherr = hid_error(mp_devhandle); - std::wstring wherr = (pwherr != NULL) ? pwherr : L"No error message"; + std::wstring wherr = (pwherr != nullptr) ? pwherr : L"No error message"; std::string herr(wherr.begin(), wherr.end()); Log::instance()(std::string("libhid error message: ") + herr, Loglevel::DEBUG_L2); -- cgit v1.2.1 From 5558c22eefb35dabb341b568b3f233fca51ff056 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 13 Jan 2017 10:50:20 +0100 Subject: More debug log info during dev connection Signed-off-by: Szczepan Zalega --- device.cc | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'device.cc') diff --git a/device.cc b/device.cc index 6ff370f..e6fe98e 100644 --- a/device.cc +++ b/device.cc @@ -29,27 +29,36 @@ Device::Device(const uint16_t vid, const uint16_t pid, const DeviceModel model, {} bool Device::disconnect() { - std::lock_guard lock(mex_dev_com); + //called in object's destructor Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2); + std::lock_guard lock(mex_dev_com); + Log::instance()(std::string(__FUNCTION__) + std::string(m_model==DeviceModel::PRO?"PRO":"STORAGE"), Loglevel::DEBUG_L2); + Log::instance()(std::string(__FUNCTION__) + std::string(" *IN* "), Loglevel::DEBUG_L2); + Log::instance()(std::string("Disconnection success: ") + std::to_string(mp_devhandle == nullptr), Loglevel::DEBUG_L2); if(mp_devhandle == nullptr) return false; + hid_close(mp_devhandle); mp_devhandle = nullptr; hid_exit(); return true; } bool Device::connect() { - std::lock_guard lock(mex_dev_com); Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2); + std::lock_guard lock(mex_dev_com); + Log::instance()(std::string(__FUNCTION__) + std::string(" *IN* "), Loglevel::DEBUG_L2); // hid_init(); // done automatically on hid_open mp_devhandle = hid_open(m_vid, m_pid, nullptr); - return mp_devhandle != nullptr; + const auto success = mp_devhandle != nullptr; + Log::instance()(std::string("Connection success: ") + std::to_string(success), Loglevel::DEBUG_L2); + return success; } int Device::send(const void *packet) { - std::lock_guard lock(mex_dev_com); Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2); + std::lock_guard lock(mex_dev_com); + Log::instance()(std::string(__FUNCTION__) + std::string(" *IN* "), Loglevel::DEBUG_L2); if (mp_devhandle == nullptr) throw std::runtime_error("Attempted HID send on an invalid descriptor."); //TODO migrate except to library_error @@ -59,11 +68,12 @@ int Device::send(const void *packet) { } int Device::recv(void *packet) { + Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2); std::lock_guard lock(mex_dev_com); + Log::instance()(std::string(__FUNCTION__) + std::string(" *IN* "), Loglevel::DEBUG_L2); int status; int retry_count = 0; - Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2); if (mp_devhandle == nullptr) throw std::runtime_error("Attempted HID receive on an invalid descriptor."); //TODO migrate except to library_error -- cgit v1.2.1 From deed3e733e7158859fd48ef51b64d924ea4e8184 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 13 Jan 2017 12:04:52 +0100 Subject: Decrease retry count to 20 Update log message to be more readable Signed-off-by: Szczepan Zalega --- device.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'device.cc') diff --git a/device.cc b/device.cc index e6fe98e..532ba2e 100644 --- a/device.cc +++ b/device.cc @@ -107,7 +107,7 @@ int Device::recv(void *packet) { } Stick10::Stick10(): - Device(0x20a0, 0x4108, DeviceModel::PRO, 100ms, 100, 100ms) + Device(0x20a0, 0x4108, DeviceModel::PRO, 100ms, 20, 100ms) {} -- cgit v1.2.1 From d69cf0b866fa3cc5afda2bb1a321a900520fbcc1 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 31 Jan 2017 18:15:48 +0100 Subject: Add method for checking is the device listed as connected in OS Signed-off-by: Szczepan Zalega --- device.cc | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'device.cc') diff --git a/device.cc b/device.cc index 532ba2e..42c6883 100644 --- a/device.cc +++ b/device.cc @@ -106,6 +106,24 @@ int Device::recv(void *packet) { return status; } +bool Device::is_connected() { + Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2); + std::lock_guard lock(mex_dev_com); + if (mp_devhandle==nullptr){ + return false; + } + auto pInfo = hid_enumerate(m_vid, m_pid); + if (pInfo != nullptr){ + hid_free_enumeration(pInfo); + return true; + } + return false; + +// alternative: +// unsigned char buf[1]; +// return hid_read_timeout(mp_devhandle, buf, sizeof(buf), 20) != -1; +} + Stick10::Stick10(): Device(0x20a0, 0x4108, DeviceModel::PRO, 100ms, 20, 100ms) {} -- cgit v1.2.1 From 9bc6b85e12d73a43b8d85ba109acff8778f4c08a Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 31 Jan 2017 18:17:58 +0100 Subject: Throw before communicating with device if it is not initialized Signed-off-by: Szczepan Zalega --- device.cc | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'device.cc') diff --git a/device.cc b/device.cc index 42c6883..ce1eeb6 100644 --- a/device.cc +++ b/device.cc @@ -7,6 +7,7 @@ #include "include/device.h" #include "include/log.h" #include +#include "DeviceCommunicationExceptions.h" std::mutex mex_dev_com; @@ -60,8 +61,10 @@ int Device::send(const void *packet) { std::lock_guard lock(mex_dev_com); Log::instance()(std::string(__FUNCTION__) + std::string(" *IN* "), Loglevel::DEBUG_L2); - if (mp_devhandle == nullptr) - throw std::runtime_error("Attempted HID send on an invalid descriptor."); //TODO migrate except to library_error + if (mp_devhandle == nullptr) { + Log::instance()(std::string("Connection fail") , Loglevel::DEBUG_L2); + throw DeviceNotConnected("Attempted HID send on an invalid descriptor."); + } return (hid_send_feature_report( mp_devhandle, (const unsigned char *)(packet), HID_REPORT_SIZE)); @@ -75,8 +78,10 @@ int Device::recv(void *packet) { int retry_count = 0; - if (mp_devhandle == nullptr) - throw std::runtime_error("Attempted HID receive on an invalid descriptor."); //TODO migrate except to library_error + if (mp_devhandle == nullptr){ + Log::instance()(std::string("Connection fail") , Loglevel::DEBUG_L2); + throw DeviceNotConnected("Attempted HID receive on an invalid descriptor."); + } // FIXME extract error handling and repeating to parent function in // device_proto:192 -- cgit v1.2.1 From 3fcbb90176fb02816cc47ad7172b7b89bf6cb67d Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Sat, 4 Feb 2017 16:13:02 +0100 Subject: Make statistics about device's connection Signed-off-by: Szczepan Zalega --- device.cc | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'device.cc') diff --git a/device.cc b/device.cc index ce1eeb6..d904fd9 100644 --- a/device.cc +++ b/device.cc @@ -101,6 +101,9 @@ int Device::recv(void *packet) { Log::instance()( "Maximum retry count reached" + std::to_string(retry_count), Loglevel::WARNING); + Log::instance()( + std::string("Counter stats") + m_counters.get_as_string(), + Loglevel::DEBUG); break; } Log::instance()("Retrying... " + std::to_string(retry_count), @@ -129,6 +132,11 @@ bool Device::is_connected() { // return hid_read_timeout(mp_devhandle, buf, sizeof(buf), 20) != -1; } +void Device::show_stats() { + auto s = m_counters.get_as_string(); + Log::instance()(s, Loglevel::DEBUG_L2); +} + Stick10::Stick10(): Device(0x20a0, 0x4108, DeviceModel::PRO, 100ms, 20, 100ms) {} @@ -137,3 +145,19 @@ Stick10::Stick10(): Stick20::Stick20(): Device(0x20a0, 0x4109, DeviceModel::STORAGE, 200ms, 40, 200ms) {} + +#include +#define p(x) ss << #x << " " << x << ", "; +std::string Device::ErrorCounters::get_as_string() { + std::stringstream ss; + p(wrong_CRC); + p(CRC_other_than_awaited); + p(busy); + p(total_retries); + p(sending_error); + p(receiving_error); + p(total_comm_runs); + p(storage_commands); + p(successful ); + return ss.str(); +} -- cgit v1.2.1 From adbc664125142c434294bfa795666c90c7608429 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Thu, 9 Mar 2017 18:49:24 +0100 Subject: Adjust for compilation on Visual Studio 2017 Building works however tests are not. Possibly linking with original hidapi solution would work. --- device.cc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'device.cc') diff --git a/device.cc b/device.cc index d904fd9..0111572 100644 --- a/device.cc +++ b/device.cc @@ -2,7 +2,7 @@ #include #include #include -#include +#include "hidapi/hidapi.h" #include "include/misc.h" #include "include/device.h" #include "include/log.h" @@ -31,7 +31,7 @@ Device::Device(const uint16_t vid, const uint16_t pid, const DeviceModel model, bool Device::disconnect() { //called in object's destructor - Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2); + Log::instance()(__FUNCTION__, Loglevel::DEBUG_L2); std::lock_guard lock(mex_dev_com); Log::instance()(std::string(__FUNCTION__) + std::string(m_model==DeviceModel::PRO?"PRO":"STORAGE"), Loglevel::DEBUG_L2); Log::instance()(std::string(__FUNCTION__) + std::string(" *IN* "), Loglevel::DEBUG_L2); @@ -45,7 +45,7 @@ bool Device::disconnect() { return true; } bool Device::connect() { - Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2); + Log::instance()(__FUNCTION__, Loglevel::DEBUG_L2); std::lock_guard lock(mex_dev_com); Log::instance()(std::string(__FUNCTION__) + std::string(" *IN* "), Loglevel::DEBUG_L2); @@ -57,7 +57,7 @@ bool Device::connect() { } int Device::send(const void *packet) { - Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2); + Log::instance()(__FUNCTION__, Loglevel::DEBUG_L2); std::lock_guard lock(mex_dev_com); Log::instance()(std::string(__FUNCTION__) + std::string(" *IN* "), Loglevel::DEBUG_L2); @@ -71,7 +71,7 @@ int Device::send(const void *packet) { } int Device::recv(void *packet) { - Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2); + Log::instance()(__FUNCTION__, Loglevel::DEBUG_L2); std::lock_guard lock(mex_dev_com); Log::instance()(std::string(__FUNCTION__) + std::string(" *IN* "), Loglevel::DEBUG_L2); int status; @@ -115,7 +115,7 @@ int Device::recv(void *packet) { } bool Device::is_connected() { - Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2); + Log::instance()(__FUNCTION__, Loglevel::DEBUG_L2); std::lock_guard lock(mex_dev_com); if (mp_devhandle==nullptr){ return false; -- cgit v1.2.1 From 3d96df28fe95deb096a19e3886381ef360b978d8 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Thu, 16 Feb 2017 17:48:13 +0100 Subject: Correct device counters Signed-off-by: Szczepan Zalega --- device.cc | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'device.cc') diff --git a/device.cc b/device.cc index 0111572..facdc3e 100644 --- a/device.cc +++ b/device.cc @@ -150,14 +150,23 @@ Stick20::Stick20(): #define p(x) ss << #x << " " << x << ", "; std::string Device::ErrorCounters::get_as_string() { std::stringstream ss; - p(wrong_CRC); - p(CRC_other_than_awaited); - p(busy); + p(total_comm_runs); + p(communication_successful); + ss << "("; + p(command_successful_recv); + p(command_result_not_equal_0_recv); + ss << "), "; + p(sends_executed); + p(recv_executed); + p(successful_storage_commands); p(total_retries); + ss << "("; + p(busy); + p(busy_progressbar); + p(CRC_other_than_awaited); + p(wrong_CRC); + ss << "), "; p(sending_error); p(receiving_error); - p(total_comm_runs); - p(storage_commands); - p(successful ); return ss.str(); } -- cgit v1.2.1 From bb72ba6d7850d46df3a9959fa2598e7aadf0fd89 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Thu, 16 Feb 2017 21:47:36 +0100 Subject: Do not call hid_exit on each device disconnection Signed-off-by: Szczepan Zalega --- device.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'device.cc') diff --git a/device.cc b/device.cc index facdc3e..dcba735 100644 --- a/device.cc +++ b/device.cc @@ -41,7 +41,8 @@ bool Device::disconnect() { hid_close(mp_devhandle); mp_devhandle = nullptr; - hid_exit(); + //FIXME hidexit should not be called if some devices are still active - use static active devices counter + // hid_exit(); return true; } bool Device::connect() { -- cgit v1.2.1 From 5a5205a0591757919a1ff4cbe31cda3b427fa19f Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Thu, 16 Feb 2017 21:47:57 +0100 Subject: Fix typos Signed-off-by: Szczepan Zalega --- device.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'device.cc') diff --git a/device.cc b/device.cc index dcba735..32869ae 100644 --- a/device.cc +++ b/device.cc @@ -100,10 +100,10 @@ int Device::recv(void *packet) { if (status > 0) break; // success if (retry_count++ >= m_retry_receiving_count) { Log::instance()( - "Maximum retry count reached" + std::to_string(retry_count), + "Maximum retry count reached: " + std::to_string(retry_count), Loglevel::WARNING); Log::instance()( - std::string("Counter stats") + m_counters.get_as_string(), + std::string("Counter stats: ") + m_counters.get_as_string(), Loglevel::DEBUG); break; } -- cgit v1.2.1 From bf63972b2f9c90180d491f5ad314702fa7b8051f Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Thu, 16 Feb 2017 21:48:25 +0100 Subject: Decrease retry count in the lowest level Signed-off-by: Szczepan Zalega --- device.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'device.cc') diff --git a/device.cc b/device.cc index 32869ae..8fdf99a 100644 --- a/device.cc +++ b/device.cc @@ -139,12 +139,12 @@ void Device::show_stats() { } Stick10::Stick10(): - Device(0x20a0, 0x4108, DeviceModel::PRO, 100ms, 20, 100ms) + Device(0x20a0, 0x4108, DeviceModel::PRO, 100ms, 5, 100ms) {} Stick20::Stick20(): - Device(0x20a0, 0x4109, DeviceModel::STORAGE, 200ms, 40, 200ms) + Device(0x20a0, 0x4109, DeviceModel::STORAGE, 200ms, 5, 200ms) {} #include -- cgit v1.2.1 From 6061ee1af573147e41a0834d1c6628eda2fa2f7c Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 17 Feb 2017 17:49:20 +0100 Subject: Rename is_connected to be more specific Signed-off-by: Szczepan Zalega --- device.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'device.cc') diff --git a/device.cc b/device.cc index 8fdf99a..fcc3ba7 100644 --- a/device.cc +++ b/device.cc @@ -115,7 +115,7 @@ int Device::recv(void *packet) { return status; } -bool Device::is_connected() { +bool Device::could_be_enumerated() { Log::instance()(__FUNCTION__, Loglevel::DEBUG_L2); std::lock_guard lock(mex_dev_com); if (mp_devhandle==nullptr){ -- cgit v1.2.1 From 29fc4839b7aaf76c3587cf0d268546fd1d1390c4 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 21 Feb 2017 14:56:07 +0100 Subject: Build debug-log-free library for increased security Signed-off-by: Szczepan Zalega --- device.cc | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) (limited to 'device.cc') diff --git a/device.cc b/device.cc index fcc3ba7..7201087 100644 --- a/device.cc +++ b/device.cc @@ -31,12 +31,12 @@ Device::Device(const uint16_t vid, const uint16_t pid, const DeviceModel model, bool Device::disconnect() { //called in object's destructor - Log::instance()(__FUNCTION__, Loglevel::DEBUG_L2); + LOG(__FUNCTION__, Loglevel::DEBUG_L2); std::lock_guard lock(mex_dev_com); - Log::instance()(std::string(__FUNCTION__) + std::string(m_model==DeviceModel::PRO?"PRO":"STORAGE"), Loglevel::DEBUG_L2); - Log::instance()(std::string(__FUNCTION__) + std::string(" *IN* "), Loglevel::DEBUG_L2); + LOG(std::string(__FUNCTION__) + std::string(m_model==DeviceModel::PRO?"PRO":"STORAGE"), Loglevel::DEBUG_L2); + LOG(std::string(__FUNCTION__) + std::string(" *IN* "), Loglevel::DEBUG_L2); - Log::instance()(std::string("Disconnection success: ") + std::to_string(mp_devhandle == nullptr), Loglevel::DEBUG_L2); + LOG(std::string("Disconnection success: ") + std::to_string(mp_devhandle == nullptr), Loglevel::DEBUG_L2); if(mp_devhandle == nullptr) return false; hid_close(mp_devhandle); @@ -46,24 +46,24 @@ bool Device::disconnect() { return true; } bool Device::connect() { - Log::instance()(__FUNCTION__, Loglevel::DEBUG_L2); + LOG(__FUNCTION__, Loglevel::DEBUG_L2); std::lock_guard lock(mex_dev_com); - Log::instance()(std::string(__FUNCTION__) + std::string(" *IN* "), Loglevel::DEBUG_L2); + LOG(std::string(__FUNCTION__) + std::string(" *IN* "), Loglevel::DEBUG_L2); // hid_init(); // done automatically on hid_open mp_devhandle = hid_open(m_vid, m_pid, nullptr); const auto success = mp_devhandle != nullptr; - Log::instance()(std::string("Connection success: ") + std::to_string(success), Loglevel::DEBUG_L2); + LOG(std::string("Connection success: ") + std::to_string(success), Loglevel::DEBUG_L2); return success; } int Device::send(const void *packet) { - Log::instance()(__FUNCTION__, Loglevel::DEBUG_L2); + LOG(__FUNCTION__, Loglevel::DEBUG_L2); std::lock_guard lock(mex_dev_com); - Log::instance()(std::string(__FUNCTION__) + std::string(" *IN* "), Loglevel::DEBUG_L2); + LOG(std::string(__FUNCTION__) + std::string(" *IN* "), Loglevel::DEBUG_L2); if (mp_devhandle == nullptr) { - Log::instance()(std::string("Connection fail") , Loglevel::DEBUG_L2); + LOG(std::string("Connection fail") , Loglevel::DEBUG_L2); throw DeviceNotConnected("Attempted HID send on an invalid descriptor."); } @@ -72,15 +72,15 @@ int Device::send(const void *packet) { } int Device::recv(void *packet) { - Log::instance()(__FUNCTION__, Loglevel::DEBUG_L2); + LOG(__FUNCTION__, Loglevel::DEBUG_L2); std::lock_guard lock(mex_dev_com); - Log::instance()(std::string(__FUNCTION__) + std::string(" *IN* "), Loglevel::DEBUG_L2); + LOG(std::string(__FUNCTION__) + std::string(" *IN* "), Loglevel::DEBUG_L2); int status; int retry_count = 0; if (mp_devhandle == nullptr){ - Log::instance()(std::string("Connection fail") , Loglevel::DEBUG_L2); + LOG(std::string("Connection fail") , Loglevel::DEBUG_L2); throw DeviceNotConnected("Attempted HID receive on an invalid descriptor."); } @@ -94,20 +94,20 @@ int Device::recv(void *packet) { auto pwherr = hid_error(mp_devhandle); std::wstring wherr = (pwherr != nullptr) ? pwherr : L"No error message"; std::string herr(wherr.begin(), wherr.end()); - Log::instance()(std::string("libhid error message: ") + herr, + LOG(std::string("libhid error message: ") + herr, Loglevel::DEBUG_L2); if (status > 0) break; // success if (retry_count++ >= m_retry_receiving_count) { - Log::instance()( + LOG( "Maximum retry count reached: " + std::to_string(retry_count), Loglevel::WARNING); - Log::instance()( + LOG( std::string("Counter stats: ") + m_counters.get_as_string(), Loglevel::DEBUG); break; } - Log::instance()("Retrying... " + std::to_string(retry_count), + LOG("Retrying... " + std::to_string(retry_count), Loglevel::DEBUG); std::this_thread::sleep_for(m_retry_timeout); } @@ -116,7 +116,7 @@ int Device::recv(void *packet) { } bool Device::could_be_enumerated() { - Log::instance()(__FUNCTION__, Loglevel::DEBUG_L2); + LOG(__FUNCTION__, Loglevel::DEBUG_L2); std::lock_guard lock(mex_dev_com); if (mp_devhandle==nullptr){ return false; @@ -135,7 +135,7 @@ bool Device::could_be_enumerated() { void Device::show_stats() { auto s = m_counters.get_as_string(); - Log::instance()(s, Loglevel::DEBUG_L2); + LOG(s, Loglevel::DEBUG_L2); } Stick10::Stick10(): -- cgit v1.2.1 From 802dd4543b7634f498bd44909461eae6d7975abb Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 28 Feb 2017 21:02:30 +0100 Subject: Make device-level reconnect on problem with sending Make it 3 times before throwing exception Call hid_exit on last device disconnection Signed-off-by: Szczepan Zalega --- device.cc | 56 +++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 49 insertions(+), 7 deletions(-) (limited to 'device.cc') diff --git a/device.cc b/device.cc index 7201087..44ec5c3 100644 --- a/device.cc +++ b/device.cc @@ -15,6 +15,8 @@ using namespace nitrokey::device; using namespace nitrokey::log; using namespace std::chrono; +std::atomic_int Device::instances_count{0}; + Device::Device(const uint16_t vid, const uint16_t pid, const DeviceModel model, const milliseconds send_receive_delay, const int retry_receiving_count, const milliseconds retry_timeout) @@ -27,13 +29,19 @@ Device::Device(const uint16_t vid, const uint16_t pid, const DeviceModel model, last_command_status(0), m_model(model), m_send_receive_delay(send_receive_delay) -{} +{ + instances_count++; +} bool Device::disconnect() { //called in object's destructor LOG(__FUNCTION__, Loglevel::DEBUG_L2); std::lock_guard lock(mex_dev_com); - LOG(std::string(__FUNCTION__) + std::string(m_model==DeviceModel::PRO?"PRO":"STORAGE"), Loglevel::DEBUG_L2); + return _disconnect(); +} + +bool Device::_disconnect() { + LOG(std::string(__FUNCTION__) + std::string(m_model == DeviceModel::PRO ? "PRO" : "STORAGE"), Loglevel::DEBUG_L2); LOG(std::string(__FUNCTION__) + std::string(" *IN* "), Loglevel::DEBUG_L2); LOG(std::string("Disconnection success: ") + std::to_string(mp_devhandle == nullptr), Loglevel::DEBUG_L2); @@ -41,14 +49,21 @@ bool Device::disconnect() { hid_close(mp_devhandle); mp_devhandle = nullptr; - //FIXME hidexit should not be called if some devices are still active - use static active devices counter - // hid_exit(); + if (instances_count == 1){ + LOG(std::string("Calling hid_exit"), Loglevel::DEBUG_L2); + hid_exit(); + } return true; } + bool Device::connect() { LOG(__FUNCTION__, Loglevel::DEBUG_L2); std::lock_guard lock(mex_dev_com); - LOG(std::string(__FUNCTION__) + std::string(" *IN* "), Loglevel::DEBUG_L2); + return _connect(); +} + +bool Device::_connect() { + LOG(std::string(__FUNCTION__) + std::string(" *IN* "), Loglevel::DEBUG_L2); // hid_init(); // done automatically on hid_open mp_devhandle = hid_open(m_vid, m_pid, nullptr); @@ -67,8 +82,16 @@ int Device::send(const void *packet) { throw DeviceNotConnected("Attempted HID send on an invalid descriptor."); } - return (hid_send_feature_report( - mp_devhandle, (const unsigned char *)(packet), HID_REPORT_SIZE)); + int send_feature_report = -1; + + for (int i = 0; i < 3 && send_feature_report < 0; ++i) { + send_feature_report = hid_send_feature_report( + mp_devhandle, (const unsigned char *)(packet), HID_REPORT_SIZE); + if (send_feature_report < 0) _reconnect(); + //add thread sleep? + LOG(std::string("Sending attempt: ")+std::to_string(i) + " / 3" , Loglevel::DEBUG_L2); + } + return send_feature_report; } int Device::recv(void *packet) { @@ -138,6 +161,23 @@ void Device::show_stats() { LOG(s, Loglevel::DEBUG_L2); } +void Device::_reconnect() { + if (mex_dev_com.try_lock()){ + throw std::runtime_error("mutex should be locked before entering this function"); + } + LOG(__FUNCTION__, Loglevel::DEBUG_L2); + ++m_counters.low_level_reconnect; + _disconnect(); + _connect(); + +} + +Device::~Device() { + show_stats(); + disconnect(); + instances_count--; +} + Stick10::Stick10(): Device(0x20a0, 0x4108, DeviceModel::PRO, 100ms, 5, 100ms) {} @@ -167,7 +207,9 @@ std::string Device::ErrorCounters::get_as_string() { p(CRC_other_than_awaited); p(wrong_CRC); ss << "), "; + p(low_level_reconnect); p(sending_error); p(receiving_error); return ss.str(); } +#undef p \ No newline at end of file -- cgit v1.2.1 From 7ab1b59b13477310d0ff631dd9e6686d6ebf4e91 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 28 Feb 2017 22:41:27 +0100 Subject: Remove checking for mutex being locked Signed-off-by: Szczepan Zalega --- device.cc | 4 ---- 1 file changed, 4 deletions(-) (limited to 'device.cc') diff --git a/device.cc b/device.cc index 44ec5c3..940a654 100644 --- a/device.cc +++ b/device.cc @@ -162,14 +162,10 @@ void Device::show_stats() { } void Device::_reconnect() { - if (mex_dev_com.try_lock()){ - throw std::runtime_error("mutex should be locked before entering this function"); - } LOG(__FUNCTION__, Loglevel::DEBUG_L2); ++m_counters.low_level_reconnect; _disconnect(); _connect(); - } Device::~Device() { -- cgit v1.2.1 From 29375e7953041766de7c44ea5574cf80d5916e41 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 28 Feb 2017 22:50:45 +0100 Subject: Do reconnect also on receiving failure Signed-off-by: Szczepan Zalega --- device.cc | 1 + 1 file changed, 1 insertion(+) (limited to 'device.cc') diff --git a/device.cc b/device.cc index 940a654..ddbfe85 100644 --- a/device.cc +++ b/device.cc @@ -130,6 +130,7 @@ int Device::recv(void *packet) { Loglevel::DEBUG); break; } + _reconnect(); LOG("Retrying... " + std::to_string(retry_count), Loglevel::DEBUG); std::this_thread::sleep_for(m_retry_timeout); -- cgit v1.2.1 From e4ff28a31b0cfd1821d0a7418aba5f71a1cffb8c Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 28 Feb 2017 22:55:16 +0100 Subject: Remove obsolete comments Signed-off-by: Szczepan Zalega --- device.cc | 3 --- 1 file changed, 3 deletions(-) (limited to 'device.cc') diff --git a/device.cc b/device.cc index ddbfe85..35e7a78 100644 --- a/device.cc +++ b/device.cc @@ -107,13 +107,10 @@ int Device::recv(void *packet) { throw DeviceNotConnected("Attempted HID receive on an invalid descriptor."); } - // FIXME extract error handling and repeating to parent function in - // device_proto:192 for (;;) { status = (hid_get_feature_report(mp_devhandle, (unsigned char *)(packet), HID_REPORT_SIZE)); - // FIXME handle getting libhid error message somewhere else auto pwherr = hid_error(mp_devhandle); std::wstring wherr = (pwherr != nullptr) ? pwherr : L"No error message"; std::string herr(wherr.begin(), wherr.end()); -- cgit v1.2.1 From 257062b9a5130cf25dfd26d4ec93880abde1c9ce Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 7 Mar 2017 11:37:41 +0100 Subject: Check for null device reference in case of failed reconnection Signed-off-by: Szczepan Zalega --- device.cc | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) (limited to 'device.cc') diff --git a/device.cc b/device.cc index 35e7a78..9a64813 100644 --- a/device.cc +++ b/device.cc @@ -77,14 +77,13 @@ int Device::send(const void *packet) { std::lock_guard lock(mex_dev_com); LOG(std::string(__FUNCTION__) + std::string(" *IN* "), Loglevel::DEBUG_L2); - if (mp_devhandle == nullptr) { - LOG(std::string("Connection fail") , Loglevel::DEBUG_L2); - throw DeviceNotConnected("Attempted HID send on an invalid descriptor."); - } - int send_feature_report = -1; for (int i = 0; i < 3 && send_feature_report < 0; ++i) { + if (mp_devhandle == nullptr) { + LOG(std::string("Connection fail") , Loglevel::DEBUG_L2); + throw DeviceNotConnected("Attempted HID send on an invalid descriptor."); + } send_feature_report = hid_send_feature_report( mp_devhandle, (const unsigned char *)(packet), HID_REPORT_SIZE); if (send_feature_report < 0) _reconnect(); @@ -101,13 +100,12 @@ int Device::recv(void *packet) { int status; int retry_count = 0; - - if (mp_devhandle == nullptr){ - LOG(std::string("Connection fail") , Loglevel::DEBUG_L2); - throw DeviceNotConnected("Attempted HID receive on an invalid descriptor."); - } - for (;;) { + if (mp_devhandle == nullptr){ + LOG(std::string("Connection fail") , Loglevel::DEBUG_L2); + throw DeviceNotConnected("Attempted HID receive on an invalid descriptor."); + } + status = (hid_get_feature_report(mp_devhandle, (unsigned char *)(packet), HID_REPORT_SIZE)); -- cgit v1.2.1 From a3303c491a6f3b9980a91cabe81d5fb643ec8d9a Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Thu, 9 Mar 2017 12:07:27 +0100 Subject: Set turbo mode Signed-off-by: Szczepan Zalega --- device.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'device.cc') diff --git a/device.cc b/device.cc index 9a64813..66877da 100644 --- a/device.cc +++ b/device.cc @@ -176,7 +176,7 @@ Stick10::Stick10(): Stick20::Stick20(): - Device(0x20a0, 0x4109, DeviceModel::STORAGE, 200ms, 5, 200ms) + Device(0x20a0, 0x4109, DeviceModel::STORAGE, 20ms, 20, 20ms) {} #include -- cgit v1.2.1