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.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/device.h') diff --git a/include/device.h b/include/device.h index 62c4073..f686fbd 100644 --- a/include/device.h +++ b/include/device.h @@ -2,7 +2,7 @@ #define DEVICE_H #include #include -#include "inttypes.h" +#include #define HID_REPORT_SIZE 65 -- cgit v1.2.1 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 --- include/device.h | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) (limited to 'include/device.h') diff --git a/include/device.h b/include/device.h index f686fbd..938fc75 100644 --- a/include/device.h +++ b/include/device.h @@ -8,9 +8,12 @@ // TODO !! SEMAPHORE +#include + namespace nitrokey { namespace device { using namespace std::chrono_literals; + using std::chrono::milliseconds; struct EnumClassHash { @@ -22,6 +25,7 @@ namespace device { }; enum class DeviceModel{ + UNKNOWN, PRO, STORAGE }; @@ -29,8 +33,11 @@ enum class DeviceModel{ class Device { public: - Device(); - virtual ~Device(){disconnect();} + 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); + + virtual ~Device(){disconnect();} // lack of device is not actually an error, // so it doesn't throw @@ -51,31 +58,32 @@ public: int get_retry_receiving_count() const { return m_retry_receiving_count; }; int get_retry_sending_count() const { return m_retry_sending_count; }; std::chrono::milliseconds get_retry_timeout() const { return m_retry_timeout; }; - std::chrono::milliseconds get_send_receive_delay() const {return m_send_receive_delay;} + std::chrono::milliseconds get_send_receive_delay() const {return m_send_receive_delay;} - int get_last_command_status() {auto a = last_command_status; last_command_status = 0; return a;}; - 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;} + int get_last_command_status() {int a = last_command_status; last_command_status = 0; return a;}; + 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;} private: - uint8_t last_command_status; + std::atomic last_command_status; - protected: - uint16_t m_vid; - uint16_t m_pid; - DeviceModel m_model; +protected: + const uint16_t m_vid; + const uint16_t m_pid; + const DeviceModel m_model; /* * While the project uses Signal11 portable HIDAPI * library, there's no way of doing it asynchronously, * hence polling. */ - int m_retry_sending_count; - int m_retry_receiving_count; - std::chrono::milliseconds m_retry_timeout; - std::chrono::milliseconds m_send_receive_delay; + 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::atomicmp_devhandle; - hid_device *mp_devhandle; }; class Stick10 : public Device { -- cgit v1.2.1 From fc986f87fd256e2cfdd5edcc5b0cf14faf694441 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Wed, 11 Jan 2017 16:52:33 +0100 Subject: Use atomic_exchange for atomic operations Signed-off-by: Szczepan Zalega --- include/device.h | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'include/device.h') diff --git a/include/device.h b/include/device.h index 938fc75..42865ee 100644 --- a/include/device.h +++ b/include/device.h @@ -6,8 +6,6 @@ #define HID_REPORT_SIZE 65 -// TODO !! SEMAPHORE - #include namespace nitrokey { @@ -60,7 +58,7 @@ public: std::chrono::milliseconds get_retry_timeout() const { return m_retry_timeout; }; std::chrono::milliseconds get_send_receive_delay() const {return m_send_receive_delay;} - int get_last_command_status() {int a = last_command_status; last_command_status = 0; return a;}; + int get_last_command_status() {int a = std::atomic_exchange(&last_command_status, static_cast(0)); return a;}; 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;} -- cgit v1.2.1 From 02189413ce463116694478fcdcb418fed7e42027 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Wed, 11 Jan 2017 20:09:53 +0100 Subject: Remove UNKNOWN type of the device Signed-off-by: Szczepan Zalega --- include/device.h | 1 - 1 file changed, 1 deletion(-) (limited to 'include/device.h') diff --git a/include/device.h b/include/device.h index 42865ee..4001d45 100644 --- a/include/device.h +++ b/include/device.h @@ -23,7 +23,6 @@ namespace device { }; enum class DeviceModel{ - UNKNOWN, PRO, STORAGE }; -- 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 --- include/device.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'include/device.h') diff --git a/include/device.h b/include/device.h index 4001d45..5965f99 100644 --- a/include/device.h +++ b/include/device.h @@ -52,6 +52,8 @@ public: */ virtual int recv(void *packet); + bool is_connected(); + int get_retry_receiving_count() const { return m_retry_receiving_count; }; int get_retry_sending_count() const { return m_retry_sending_count; }; std::chrono::milliseconds get_retry_timeout() const { return m_retry_timeout; }; -- 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.h | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'include/device.h') diff --git a/include/device.h b/include/device.h index 5965f99..a23e1b3 100644 --- a/include/device.h +++ b/include/device.h @@ -2,7 +2,8 @@ #define DEVICE_H #include #include -#include +#include +#include #define HID_REPORT_SIZE 65 @@ -30,11 +31,26 @@ enum class DeviceModel{ class Device { public: + + struct ErrorCounters{ + int wrong_CRC; + int CRC_other_than_awaited; + int busy; + int total_retries; + int sending_error; + int receiving_error; + int total_comm_runs; + int storage_commands; + int successful; + std::string get_as_string(); + } m_counters = {}; + + 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); - virtual ~Device(){disconnect();} + virtual ~Device(){show_stats(); disconnect();} // lack of device is not actually an error, // so it doesn't throw @@ -54,6 +70,8 @@ public: bool is_connected(); + void show_stats(); + ErrorCounters get_stats(){ return m_counters; } int get_retry_receiving_count() const { return m_retry_receiving_count; }; int get_retry_sending_count() const { return m_retry_sending_count; }; std::chrono::milliseconds get_retry_timeout() const { return m_retry_timeout; }; -- cgit v1.2.1 From 6ee68fa294d1d9ab8fa8e61a009845dc31a9b771 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 14 Feb 2017 11:53:25 +0100 Subject: Compiles on MXE, but not working on Windows Signed-off-by: Szczepan Zalega --- include/device.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/device.h') diff --git a/include/device.h b/include/device.h index a23e1b3..281c4d9 100644 --- a/include/device.h +++ b/include/device.h @@ -1,7 +1,7 @@ #ifndef DEVICE_H #define DEVICE_H #include -#include +#include "hidapi/hidapi.h" #include #include -- 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.h | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) (limited to 'include/device.h') diff --git a/include/device.h b/include/device.h index 281c4d9..40eb376 100644 --- a/include/device.h +++ b/include/device.h @@ -28,21 +28,30 @@ enum class DeviceModel{ STORAGE }; +#include + class Device { public: struct ErrorCounters{ - int wrong_CRC; - int CRC_other_than_awaited; - int busy; - int total_retries; - int sending_error; - int receiving_error; - int total_comm_runs; - int storage_commands; - int successful; + using cnt = std::atomic_int; + cnt wrong_CRC; + cnt CRC_other_than_awaited; + cnt busy; + cnt total_retries; + cnt sending_error; + cnt receiving_error; + cnt total_comm_runs; + cnt successful_storage_commands; + cnt command_successful_recv; + cnt recv_executed; + cnt sends_executed; + cnt busy_progressbar; + cnt command_result_not_equal_0_recv; + cnt communication_successful; std::string get_as_string(); + } m_counters = {}; @@ -71,7 +80,7 @@ public: bool is_connected(); void show_stats(); - ErrorCounters get_stats(){ return m_counters; } +// ErrorCounters get_stats(){ return m_counters; } int get_retry_receiving_count() const { return m_retry_receiving_count; }; int get_retry_sending_count() const { return m_retry_sending_count; }; std::chrono::milliseconds get_retry_timeout() const { return m_retry_timeout; }; -- 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 --- include/device.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'include/device.h') diff --git a/include/device.h b/include/device.h index 40eb376..5d7ee12 100644 --- a/include/device.h +++ b/include/device.h @@ -77,7 +77,12 @@ public: */ virtual int recv(void *packet); - bool is_connected(); + /*** + * Returns true if some device is visible by OS with given VID and PID + * whether the device is connected through HID API or not. + * @return true if visible by OS + */ + bool could_be_enumerated(); void show_stats(); // ErrorCounters get_stats(){ return m_counters; } -- 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 --- include/device.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'include/device.h') diff --git a/include/device.h b/include/device.h index 5d7ee12..7b300e5 100644 --- a/include/device.h +++ b/include/device.h @@ -50,6 +50,7 @@ public: cnt busy_progressbar; cnt command_result_not_equal_0_recv; cnt communication_successful; + cnt low_level_reconnect; std::string get_as_string(); } m_counters = {}; @@ -59,7 +60,7 @@ public: const milliseconds send_receive_delay, const int retry_receiving_count, const milliseconds retry_timeout); - virtual ~Device(){show_stats(); disconnect();} + virtual ~Device(); // lack of device is not actually an error, // so it doesn't throw @@ -97,6 +98,9 @@ public: DeviceModel get_device_model() const {return m_model;} private: std::atomic last_command_status; + void _reconnect(); + bool _connect(); + bool _disconnect(); protected: const uint16_t m_vid; @@ -115,6 +119,8 @@ protected: std::atomicmp_devhandle; + + static std::atomic_int instances_count; }; class Stick10 : public Device { -- cgit v1.2.1