From c6ba90ba1ca606b63373caaba16cb4fcc65d00f9 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Mon, 9 Jan 2017 18:30:29 +0100 Subject: Remove unused inttypes Signed-off-by: Szczepan Zalega --- include/device_proto.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/device_proto.h') diff --git a/include/device_proto.h b/include/device_proto.h index 0953566..667b8db 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -8,7 +8,7 @@ #include #include // a local version for compatibility with Windows -#include "inttypes.h" +#include #include "cxx_semantics.h" #include "device.h" #include "misc.h" -- cgit v1.2.1 From 90e626b52c75e9e117494f15f57177aaf912468c Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 13 Jan 2017 10:43:15 +0100 Subject: Add lock_guard for complete send-receive cycle Signed-off-by: Szczepan Zalega --- include/device_proto.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'include/device_proto.h') diff --git a/include/device_proto.h b/include/device_proto.h index 667b8db..31bfe11 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -32,6 +32,8 @@ #define PWS_SEND_TAB 2 #define PWS_SEND_CR 3 +#include + namespace nitrokey { namespace proto { /* @@ -204,13 +206,15 @@ namespace nitrokey { bzero(&st, sizeof(st)); } - static ClearingProxy run(device::Device &dev, const command_payload &payload) { using namespace ::nitrokey::device; using namespace ::nitrokey::log; using namespace std::chrono_literals; + static std::mutex send_receive_mtx; + std::lock_guard guard(send_receive_mtx); + Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2); int status; -- 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 --- include/device_proto.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/device_proto.h') diff --git a/include/device_proto.h b/include/device_proto.h index 31bfe11..2105f30 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -296,7 +296,7 @@ namespace nitrokey { successful_communication = true; break; } - Log::instance()(std::string("Retry status - dev status, equal crc, correct CRC: ") + Log::instance()(std::string("Retry status - dev status, awaited cmd crc, correct packet CRC: ") + std::to_string(resp.device_status) + " " + std::to_string(resp.last_command_crc == outp.crc) + " " + std::to_string(resp.isCRCcorrect()), Loglevel::DEBUG_L2); -- cgit v1.2.1 From 0503db5b47f247568b78504fa781e083e108eab9 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 31 Jan 2017 18:12:31 +0100 Subject: Pass devices shared pointer to methods instead of ref Signed-off-by: Szczepan Zalega --- include/device_proto.h | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'include/device_proto.h') diff --git a/include/device_proto.h b/include/device_proto.h index 2105f30..ba314f4 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -206,7 +206,7 @@ namespace nitrokey { bzero(&st, sizeof(st)); } - static ClearingProxy run(device::Device &dev, + static ClearingProxy run(std::shared_ptr dev, const command_payload &payload) { using namespace ::nitrokey::device; using namespace ::nitrokey::log; @@ -235,22 +235,22 @@ namespace nitrokey { bool successful_communication = false; int receiving_retry_counter = 0; - int sending_retry_counter = dev.get_retry_sending_count(); + int sending_retry_counter = dev->get_retry_sending_count(); while (sending_retry_counter-- > 0) { - status = dev.send(&outp); + status = dev->send(&outp); if (status <= 0) - throw std::runtime_error( + throw DeviceSendingFailure( std::string("Device error while sending command ") + std::to_string(status)); - std::this_thread::sleep_for(dev.get_send_receive_delay()); + std::this_thread::sleep_for(dev->get_send_receive_delay()); // FIXME make checks done in device:recv here - receiving_retry_counter = dev.get_retry_receiving_count(); + receiving_retry_counter = dev->get_retry_receiving_count(); while (receiving_retry_counter-- > 0) { - status = dev.recv(&resp); + status = dev->recv(&resp); - if (dev.get_device_model() == DeviceModel::STORAGE && + if (dev->get_device_model() == DeviceModel::STORAGE && resp.command_id >= stick20::CMD_START_VALUE && resp.command_id < stick20::CMD_END_VALUE ) { Log::instance()(std::string("Detected storage device cmd, status: ") + @@ -306,7 +306,7 @@ namespace nitrokey { Loglevel::DEBUG); Log::instance()("Invalid incoming HID packet:", Loglevel::DEBUG_L2); Log::instance()(static_cast(resp), Loglevel::DEBUG_L2); - std::this_thread::sleep_for(dev.get_retry_timeout()); + std::this_thread::sleep_for(dev->get_retry_timeout()); continue; } if (successful_communication) break; @@ -315,7 +315,7 @@ namespace nitrokey { Loglevel::DEBUG); } - dev.set_last_command_status(resp.last_command_status); // FIXME should be handled on device.recv + dev->set_last_command_status(resp.last_command_status); // FIXME should be handled on device.recv clear_packet(outp); @@ -348,7 +348,7 @@ namespace nitrokey { return resp; } - static ClearingProxy run(device::Device &dev) { + static ClearingProxy run(std::shared_ptr dev) { command_payload empty_payload; return run(dev, empty_payload); } -- 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 --- include/device_proto.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'include/device_proto.h') diff --git a/include/device_proto.h b/include/device_proto.h index ba314f4..9401428 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -33,6 +33,7 @@ #define PWS_SEND_CR 3 #include +#include "DeviceCommunicationExceptions.h" namespace nitrokey { namespace proto { @@ -217,6 +218,10 @@ namespace nitrokey { Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2); + if (dev == nullptr){ + throw DeviceNotConnected("Device not initialized"); + } + int status; OutgoingPacket outp; ResponsePacket resp; @@ -320,7 +325,7 @@ namespace nitrokey { clear_packet(outp); if (status <= 0) - throw std::runtime_error( //FIXME replace with CriticalErrorException + throw DeviceReceivingFailure( //FIXME replace with CriticalErrorException std::string("Device error while executing command ") + std::to_string(status)); -- cgit v1.2.1 From 767e24572db2bbc4b9837c32ffc0bab4e1ad0b81 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 3 Feb 2017 17:07:32 +0100 Subject: Disconnect device as soon as the communication issue appears Signed-off-by: Szczepan Zalega --- include/device_proto.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'include/device_proto.h') diff --git a/include/device_proto.h b/include/device_proto.h index 9401428..1e07277 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -243,10 +243,13 @@ namespace nitrokey { int sending_retry_counter = dev->get_retry_sending_count(); while (sending_retry_counter-- > 0) { status = dev->send(&outp); - if (status <= 0) + if (status <= 0){ + Log::instance()("Encountered communication error, disconnecting device", Loglevel::DEBUG_L2); + dev->disconnect(); throw DeviceSendingFailure( std::string("Device error while sending command ") + std::to_string(status)); + } std::this_thread::sleep_for(dev->get_send_receive_delay()); -- 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 --- include/device_proto.h | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'include/device_proto.h') diff --git a/include/device_proto.h b/include/device_proto.h index 1e07277..e3a217d 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -221,6 +221,7 @@ namespace nitrokey { if (dev == nullptr){ throw DeviceNotConnected("Device not initialized"); } + dev->m_counters.total_comm_runs++; int status; OutgoingPacket outp; @@ -246,6 +247,7 @@ namespace nitrokey { if (status <= 0){ Log::instance()("Encountered communication error, disconnecting device", Loglevel::DEBUG_L2); dev->disconnect(); + dev->m_counters.sending_error++; throw DeviceSendingFailure( std::string("Device error while sending command ") + std::to_string(status)); @@ -263,6 +265,7 @@ namespace nitrokey { resp.command_id < stick20::CMD_END_VALUE ) { Log::instance()(std::string("Detected storage device cmd, status: ") + std::to_string(resp.storage_status.device_status), Loglevel::DEBUG_L2); + dev->m_counters.storage_commands++; resp.last_command_status = static_cast(stick10::command_status::ok); switch (static_cast(resp.storage_status.device_status)) { @@ -288,8 +291,9 @@ namespace nitrokey { //SENDPASSWORD gives wrong CRC , for now rely on !=0 (TODO report) // if (resp.device_status == 0 && resp.last_command_crc == outp.crc && resp.isCRCcorrect()) break; + auto CRC_equal_awaited = resp.last_command_crc == outp.crc; if (resp.device_status == static_cast(stick10::device_status::ok) && - resp.last_command_crc == outp.crc && resp.isValid()){ + CRC_equal_awaited && resp.isValid()){ successful_communication = true; break; } @@ -306,14 +310,19 @@ namespace nitrokey { } Log::instance()(std::string("Retry status - dev status, awaited cmd crc, correct packet CRC: ") + std::to_string(resp.device_status) + " " + - std::to_string(resp.last_command_crc == outp.crc) + + std::to_string(CRC_equal_awaited) + " " + std::to_string(resp.isCRCcorrect()), Loglevel::DEBUG_L2); + if (!resp.isCRCcorrect()) dev->m_counters.wrong_CRC++; + if (!CRC_equal_awaited) dev->m_counters.CRC_other_than_awaited++; + + Log::instance()( "Device is not ready or received packet's last CRC is not equal to sent CRC packet, retrying...", Loglevel::DEBUG); Log::instance()("Invalid incoming HID packet:", Loglevel::DEBUG_L2); Log::instance()(static_cast(resp), Loglevel::DEBUG_L2); + dev->m_counters.total_retries++; std::this_thread::sleep_for(dev->get_retry_timeout()); continue; } @@ -327,10 +336,12 @@ namespace nitrokey { clear_packet(outp); - if (status <= 0) + if (status <= 0) { + dev->m_counters.receiving_error++; throw DeviceReceivingFailure( //FIXME replace with CriticalErrorException std::string("Device error while executing command ") + std::to_string(status)); + } Log::instance()("Incoming HID packet:", Loglevel::DEBUG); Log::instance()(static_cast(resp), Loglevel::DEBUG); @@ -351,6 +362,7 @@ namespace nitrokey { if (resp.last_command_status != static_cast(stick10::command_status::ok)) throw CommandFailedException(resp.command_id, resp.last_command_status); + dev->m_counters.successful++; // See: DeviceResponse return resp; -- cgit v1.2.1 From 832ae0720e7ce2a9666baaf9b86c1c9b9c150697 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Sat, 4 Feb 2017 16:14:55 +0100 Subject: Allow device to reply 10 times with busy status in a try Signed-off-by: Szczepan Zalega --- include/device_proto.h | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'include/device_proto.h') diff --git a/include/device_proto.h b/include/device_proto.h index e3a217d..ea740b3 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -298,9 +298,17 @@ namespace nitrokey { break; } if (resp.device_status == static_cast(stick10::device_status::busy)) { - receiving_retry_counter++; - Log::instance()("Status busy, not decresing receiving_retry_counter counter: " + - std::to_string(receiving_retry_counter), Loglevel::DEBUG_L2); + static int busy_counter = 0; + if (busy_counter++<10) { + receiving_retry_counter++; + dev->m_counters.busy++; + Log::instance()("Status busy, not decreasing receiving_retry_counter counter: " + + std::to_string(receiving_retry_counter), Loglevel::DEBUG_L2); + } else { + busy_counter = 0; + Log::instance()("Status busy, decreasing receiving_retry_counter counter: " + + std::to_string(receiving_retry_counter), Loglevel::DEBUG); + } } if (resp.device_status == static_cast(stick10::device_status::busy) && static_cast(resp.storage_status.device_status) -- cgit v1.2.1 From b16667ba57ef301ef961801676de66cce30d4c52 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Sat, 4 Feb 2017 16:16:03 +0100 Subject: Disable early device disconnection on communication error Signed-off-by: Szczepan Zalega --- include/device_proto.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'include/device_proto.h') diff --git a/include/device_proto.h b/include/device_proto.h index ea740b3..cd91952 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -245,8 +245,9 @@ namespace nitrokey { while (sending_retry_counter-- > 0) { status = dev->send(&outp); if (status <= 0){ - Log::instance()("Encountered communication error, disconnecting device", Loglevel::DEBUG_L2); - dev->disconnect(); + //FIXME early disconnection not yet working properly +// Log::instance()("Encountered communication error, disconnecting device", Loglevel::DEBUG_L2); +// dev->disconnect(); dev->m_counters.sending_error++; throw DeviceSendingFailure( std::string("Device error while sending command ") + -- cgit v1.2.1 From c3aec5d004968c103a65398a1fd66f0d5ced63af Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 10 Feb 2017 12:39:25 +0100 Subject: Count all busy status Signed-off-by: Szczepan Zalega --- include/device_proto.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/device_proto.h') diff --git a/include/device_proto.h b/include/device_proto.h index cd91952..cbb74fb 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -300,9 +300,9 @@ namespace nitrokey { } if (resp.device_status == static_cast(stick10::device_status::busy)) { static int busy_counter = 0; + dev->m_counters.busy++; if (busy_counter++<10) { receiving_retry_counter++; - dev->m_counters.busy++; Log::instance()("Status busy, not decreasing receiving_retry_counter counter: " + std::to_string(receiving_retry_counter), Loglevel::DEBUG_L2); } else { -- 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. --- include/device_proto.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'include/device_proto.h') diff --git a/include/device_proto.h b/include/device_proto.h index cbb74fb..1e381dd 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -6,7 +6,6 @@ #include #include #include -#include // a local version for compatibility with Windows #include #include "cxx_semantics.h" @@ -34,6 +33,7 @@ #include #include "DeviceCommunicationExceptions.h" +#define bzero(b,len) (memset((b), '\0', (len)), (void) 0) namespace nitrokey { namespace proto { @@ -43,7 +43,7 @@ namespace nitrokey { * * TODO (future) support for Big Endian */ - +#pragma pack (push,1) /* * Every packet is a USB HID report (check USB spec) */ @@ -181,8 +181,10 @@ namespace nitrokey { typedef command_payload CommandPayload; typedef response_payload ResponsePayload; + typedef struct HIDReport OutgoingPacket; typedef struct DeviceResponse ResponsePacket; +#pragma pack (pop) static_assert(std::is_pod::value, "outgoingpacket must be a pod type"); @@ -216,7 +218,7 @@ namespace nitrokey { static std::mutex send_receive_mtx; std::lock_guard guard(send_receive_mtx); - Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2); + Log::instance()(__FUNCTION__, Loglevel::DEBUG_L2); if (dev == nullptr){ throw DeviceNotConnected("Device not initialized"); -- 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 --- include/device_proto.h | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'include/device_proto.h') diff --git a/include/device_proto.h b/include/device_proto.h index 1e381dd..3519123 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -245,6 +245,7 @@ namespace nitrokey { int receiving_retry_counter = 0; int sending_retry_counter = dev->get_retry_sending_count(); while (sending_retry_counter-- > 0) { + dev->m_counters.sends_executed++; status = dev->send(&outp); if (status <= 0){ //FIXME early disconnection not yet working properly @@ -261,6 +262,7 @@ namespace nitrokey { // FIXME make checks done in device:recv here receiving_retry_counter = dev->get_retry_receiving_count(); while (receiving_retry_counter-- > 0) { + dev->m_counters.recv_executed++; status = dev->recv(&resp); if (dev->get_device_model() == DeviceModel::STORAGE && @@ -268,7 +270,6 @@ namespace nitrokey { resp.command_id < stick20::CMD_END_VALUE ) { Log::instance()(std::string("Detected storage device cmd, status: ") + std::to_string(resp.storage_status.device_status), Loglevel::DEBUG_L2); - dev->m_counters.storage_commands++; resp.last_command_status = static_cast(stick10::command_status::ok); switch (static_cast(resp.storage_status.device_status)) { @@ -362,6 +363,7 @@ namespace nitrokey { if (resp.device_status == static_cast(stick10::device_status::busy) && static_cast(resp.storage_status.device_status) == stick20::device_status::busy_progressbar){ + dev->m_counters.busy_progressbar++; throw LongOperationInProgressException( resp.command_id, resp.device_status, resp.storage_status.progress_bar_value); } @@ -370,10 +372,20 @@ namespace nitrokey { if (receiving_retry_counter <= 0) throw std::runtime_error( "Maximum receiving_retry_counter count reached for receiving response from the device!"); - if (resp.last_command_status != static_cast(stick10::command_status::ok)) + dev->m_counters.communication_successful++; + + if (resp.last_command_status != static_cast(stick10::command_status::ok)){ + dev->m_counters.command_result_not_equal_0_recv++; throw CommandFailedException(resp.command_id, resp.last_command_status); + } - dev->m_counters.successful++; + dev->m_counters.command_successful_recv++; + + if (dev->get_device_model() == DeviceModel::STORAGE && + resp.command_id >= stick20::CMD_START_VALUE && + resp.command_id < stick20::CMD_END_VALUE ) { + dev->m_counters.successful_storage_commands++; + } // See: DeviceResponse return resp; -- 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 --- include/device_proto.h | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'include/device_proto.h') diff --git a/include/device_proto.h b/include/device_proto.h index 3519123..ea8f136 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -218,7 +218,7 @@ namespace nitrokey { static std::mutex send_receive_mtx; std::lock_guard guard(send_receive_mtx); - Log::instance()(__FUNCTION__, Loglevel::DEBUG_L2); + LOG(__FUNCTION__, Loglevel::DEBUG_L2); if (dev == nullptr){ throw DeviceNotConnected("Device not initialized"); @@ -236,8 +236,8 @@ namespace nitrokey { outp.payload = payload; outp.update_CRC(); - Log::instance()("Outgoing HID packet:", Loglevel::DEBUG); - Log::instance()(static_cast(outp), Loglevel::DEBUG); + LOG("Outgoing HID packet:", Loglevel::DEBUG); + LOG(static_cast(outp), Loglevel::DEBUG); if (!outp.isValid()) throw std::runtime_error("Invalid outgoing packet"); @@ -249,7 +249,7 @@ namespace nitrokey { status = dev->send(&outp); if (status <= 0){ //FIXME early disconnection not yet working properly -// Log::instance()("Encountered communication error, disconnecting device", Loglevel::DEBUG_L2); +// LOG("Encountered communication error, disconnecting device", Loglevel::DEBUG_L2); // dev->disconnect(); dev->m_counters.sending_error++; throw DeviceSendingFailure( @@ -268,7 +268,7 @@ namespace nitrokey { if (dev->get_device_model() == DeviceModel::STORAGE && resp.command_id >= stick20::CMD_START_VALUE && resp.command_id < stick20::CMD_END_VALUE ) { - Log::instance()(std::string("Detected storage device cmd, status: ") + + LOG(std::string("Detected storage device cmd, status: ") + std::to_string(resp.storage_status.device_status), Loglevel::DEBUG_L2); resp.last_command_status = static_cast(stick10::command_status::ok); @@ -286,7 +286,7 @@ namespace nitrokey { resp.device_status = static_cast(stick10::device_status::ok); break; default: - Log::instance()(std::string("Unknown storage device status, cannot translate: ") + + LOG(std::string("Unknown storage device status, cannot translate: ") + std::to_string(resp.storage_status.device_status), Loglevel::DEBUG); resp.device_status = resp.storage_status.device_status; break; @@ -306,11 +306,11 @@ namespace nitrokey { dev->m_counters.busy++; if (busy_counter++<10) { receiving_retry_counter++; - Log::instance()("Status busy, not decreasing receiving_retry_counter counter: " + + LOG("Status busy, not decreasing receiving_retry_counter counter: " + std::to_string(receiving_retry_counter), Loglevel::DEBUG_L2); } else { busy_counter = 0; - Log::instance()("Status busy, decreasing receiving_retry_counter counter: " + + LOG("Status busy, decreasing receiving_retry_counter counter: " + std::to_string(receiving_retry_counter), Loglevel::DEBUG); } } @@ -320,7 +320,7 @@ namespace nitrokey { successful_communication = true; break; } - Log::instance()(std::string("Retry status - dev status, awaited cmd crc, correct packet CRC: ") + LOG(std::string("Retry status - dev status, awaited cmd crc, correct packet CRC: ") + std::to_string(resp.device_status) + " " + std::to_string(CRC_equal_awaited) + " " + std::to_string(resp.isCRCcorrect()), Loglevel::DEBUG_L2); @@ -329,18 +329,18 @@ namespace nitrokey { if (!CRC_equal_awaited) dev->m_counters.CRC_other_than_awaited++; - Log::instance()( + LOG( "Device is not ready or received packet's last CRC is not equal to sent CRC packet, retrying...", Loglevel::DEBUG); - Log::instance()("Invalid incoming HID packet:", Loglevel::DEBUG_L2); - Log::instance()(static_cast(resp), Loglevel::DEBUG_L2); + LOG("Invalid incoming HID packet:", Loglevel::DEBUG_L2); + LOG(static_cast(resp), Loglevel::DEBUG_L2); dev->m_counters.total_retries++; std::this_thread::sleep_for(dev->get_retry_timeout()); continue; } if (successful_communication) break; - Log::instance()(std::string("Resending (outer loop) "), Loglevel::DEBUG_L2); - Log::instance()(std::string("sending_retry_counter count: ") + std::to_string(sending_retry_counter), + LOG(std::string("Resending (outer loop) "), Loglevel::DEBUG_L2); + LOG(std::string("sending_retry_counter count: ") + std::to_string(sending_retry_counter), Loglevel::DEBUG); } @@ -355,9 +355,9 @@ namespace nitrokey { std::to_string(status)); } - Log::instance()("Incoming HID packet:", Loglevel::DEBUG); - Log::instance()(static_cast(resp), Loglevel::DEBUG); - Log::instance()(std::string("receiving_retry_counter count: ") + std::to_string(receiving_retry_counter), + LOG("Incoming HID packet:", Loglevel::DEBUG); + LOG(static_cast(resp), Loglevel::DEBUG); + LOG(std::string("receiving_retry_counter count: ") + std::to_string(receiving_retry_counter), Loglevel::DEBUG); if (resp.device_status == static_cast(stick10::device_status::busy) && -- cgit v1.2.1 From 019d86a056ecb036da243a3304a3220b15e0bfd5 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 7 Mar 2017 17:31:46 +0100 Subject: Add TODO comment Signed-off-by: Szczepan Zalega --- include/device_proto.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/device_proto.h') diff --git a/include/device_proto.h b/include/device_proto.h index ea8f136..1eb637f 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -370,7 +370,7 @@ namespace nitrokey { if (!resp.isValid()) throw std::runtime_error("Invalid incoming packet"); if (receiving_retry_counter <= 0) - throw std::runtime_error( + throw std::runtime_error( //TODO change to other kind to handle correctly by caller, communication exception? "Maximum receiving_retry_counter count reached for receiving response from the device!"); dev->m_counters.communication_successful++; -- cgit v1.2.1 From 6c2e2c8177ff6bf5731ea25f3211fc6d889628d6 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Thu, 9 Mar 2017 17:35:39 +0100 Subject: Use own exception types instead of general runtime_error Signed-off-by: Szczepan Zalega --- include/device_proto.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'include/device_proto.h') diff --git a/include/device_proto.h b/include/device_proto.h index 1eb637f..f1f52d6 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -239,7 +239,7 @@ namespace nitrokey { LOG("Outgoing HID packet:", Loglevel::DEBUG); LOG(static_cast(outp), Loglevel::DEBUG); - if (!outp.isValid()) throw std::runtime_error("Invalid outgoing packet"); + if (!outp.isValid()) throw DeviceSendingFailure("Invalid outgoing packet"); bool successful_communication = false; int receiving_retry_counter = 0; @@ -368,9 +368,9 @@ namespace nitrokey { resp.command_id, resp.device_status, resp.storage_status.progress_bar_value); } - if (!resp.isValid()) throw std::runtime_error("Invalid incoming packet"); + if (!resp.isValid()) throw DeviceReceivingFailure("Invalid incoming packet"); if (receiving_retry_counter <= 0) - throw std::runtime_error( //TODO change to other kind to handle correctly by caller, communication exception? + throw DeviceReceivingFailure( "Maximum receiving_retry_counter count reached for receiving response from the device!"); dev->m_counters.communication_successful++; -- cgit v1.2.1 From 077aa887ba94e93006feadc28a06c13c829d318a Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Thu, 9 Mar 2017 17:36:39 +0100 Subject: Dynamically increase delay between retries on busy status Signed-off-by: Szczepan Zalega --- include/device_proto.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'include/device_proto.h') diff --git a/include/device_proto.h b/include/device_proto.h index f1f52d6..b137689 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -261,6 +261,8 @@ namespace nitrokey { // FIXME make checks done in device:recv here receiving_retry_counter = dev->get_retry_receiving_count(); + int busy_counter = 0; + auto retry_timeout = dev->get_retry_timeout(); while (receiving_retry_counter-- > 0) { dev->m_counters.recv_executed++; status = dev->recv(&resp); @@ -302,16 +304,17 @@ namespace nitrokey { break; } if (resp.device_status == static_cast(stick10::device_status::busy)) { - static int busy_counter = 0; dev->m_counters.busy++; if (busy_counter++<10) { receiving_retry_counter++; LOG("Status busy, not decreasing receiving_retry_counter counter: " + std::to_string(receiving_retry_counter), Loglevel::DEBUG_L2); } else { + retry_timeout *= 2; busy_counter = 0; LOG("Status busy, decreasing receiving_retry_counter counter: " + - std::to_string(receiving_retry_counter), Loglevel::DEBUG); + std::to_string(receiving_retry_counter) + ", current delay:" + + std::to_string(retry_timeout.count()), Loglevel::DEBUG); } } if (resp.device_status == static_cast(stick10::device_status::busy) && @@ -335,7 +338,7 @@ namespace nitrokey { LOG("Invalid incoming HID packet:", Loglevel::DEBUG_L2); LOG(static_cast(resp), Loglevel::DEBUG_L2); dev->m_counters.total_retries++; - std::this_thread::sleep_for(dev->get_retry_timeout()); + std::this_thread::sleep_for(retry_timeout); continue; } if (successful_communication) break; -- cgit v1.2.1