From eb55579c1c0e03ea98372280a344c79bb52a1f1a Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Sun, 13 Jan 2019 12:03:06 +0100 Subject: Add DeviceInfo struct for enumeration This is a preparation for a future patch that will change the enumerate method to return a vector of DeviceInfo instances instead of a vector of strings. --- libnitrokey/device.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'libnitrokey/device.h') diff --git a/libnitrokey/device.h b/libnitrokey/device.h index f6d2380..1183c9c 100644 --- a/libnitrokey/device.h +++ b/libnitrokey/device.h @@ -50,6 +50,27 @@ enum class DeviceModel{ STORAGE }; +/** + * Information about a connected device. + * + * This struct contains the information about a connected device returned by + * hidapi when enumerating the connected devices. + */ +struct DeviceInfo { + /** + * The model of the connected device. + */ + DeviceModel m_deviceModel; + /** + * The USB connection path for the device. + */ + std::string m_path; + /** + * The serial number of the device. + */ + std::wstring m_serialNumber; +}; + #include class Device { -- cgit v1.2.1 From 4a7ce051bd4004fb62f1c7022d92efa2ce42b6ab Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Sun, 13 Jan 2019 12:03:34 +0100 Subject: Change Device::enumerate return type to use DeviceInfo The return type of Device::enumerate is changed from std::vector to std::vector to expose the additional information contained in the DeviceInfo struct. --- libnitrokey/device.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'libnitrokey/device.h') diff --git a/libnitrokey/device.h b/libnitrokey/device.h index 1183c9c..1a84402 100644 --- a/libnitrokey/device.h +++ b/libnitrokey/device.h @@ -127,7 +127,13 @@ public: * @return true if visible by OS */ bool could_be_enumerated(); - std::vector enumerate(); + /** + * Returns a vector with all connected Nitrokey devices of the same device + * type as this device. + * + * @return information about all connected devices + */ + std::vector enumerate(); void show_stats(); -- cgit v1.2.1 From 6772dcf38e275b2ab9f962a87e86f89541c13a92 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Sun, 13 Jan 2019 12:03:53 +0100 Subject: Extract vendor and product IDs into constants --- libnitrokey/device.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'libnitrokey/device.h') diff --git a/libnitrokey/device.h b/libnitrokey/device.h index 1a84402..477640f 100644 --- a/libnitrokey/device.h +++ b/libnitrokey/device.h @@ -50,6 +50,19 @@ enum class DeviceModel{ STORAGE }; +/** + * The USB vendor ID for Nitrokey devices. + */ +extern const uint16_t NITROKEY_VID; +/** + * The USB product ID for the Nitrokey Pro. + */ +extern const uint16_t NITROKEY_PRO_PID; +/** + * The USB product ID for the Nitrokey Storage. + */ +extern const uint16_t NITROKEY_STORAGE_PID; + /** * Information about a connected device. * -- cgit v1.2.1 From 6c52c50d59eafa5acc7ae650695f199b7a014841 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Sun, 13 Jan 2019 12:04:12 +0100 Subject: Add product_id_to_model function --- libnitrokey/device.h | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'libnitrokey/device.h') diff --git a/libnitrokey/device.h b/libnitrokey/device.h index 477640f..8fbb385 100644 --- a/libnitrokey/device.h +++ b/libnitrokey/device.h @@ -26,6 +26,7 @@ #include #include #include +#include "misc.h" #define HID_REPORT_SIZE 65 @@ -63,6 +64,12 @@ extern const uint16_t NITROKEY_PRO_PID; */ extern const uint16_t NITROKEY_STORAGE_PID; +/** + * Convert the given USB product ID to a Nitrokey model. If there is no model + * with that ID, return an absent value. + */ +misc::Option product_id_to_model(uint16_t product_id); + /** * Information about a connected device. * -- cgit v1.2.1 From 1751759356bd64cc78f8f71543c3edd5cdce8376 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Sun, 13 Jan 2019 12:04:22 +0100 Subject: Make Device::enumerate static Device::enumerate does not need any instance data, therefore it is made static. Note that this not only changes the public API by making the method static. We also return all connected Nitrokey devices instead of only Storage devices. The NitrokeyManager method list_devices_by_cpuID is changed to check the device type so that they still only return Storage devices. The list_device method now returns both Storage and Pro devices. --- libnitrokey/device.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'libnitrokey/device.h') diff --git a/libnitrokey/device.h b/libnitrokey/device.h index 8fbb385..418d335 100644 --- a/libnitrokey/device.h +++ b/libnitrokey/device.h @@ -148,12 +148,11 @@ public: */ bool could_be_enumerated(); /** - * Returns a vector with all connected Nitrokey devices of the same device - * type as this device. + * Returns a vector with all connected Nitrokey devices. * * @return information about all connected devices */ - std::vector enumerate(); + static std::vector enumerate(); void show_stats(); -- cgit v1.2.1 From 14635f49813df1699569d3b13456ea33955de54b Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Sun, 13 Jan 2019 12:04:31 +0100 Subject: Add Device::create static method The method makes it easier to create a std::shared_ptr from a model enum instance. --- libnitrokey/device.h | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'libnitrokey/device.h') diff --git a/libnitrokey/device.h b/libnitrokey/device.h index 418d335..eab3888 100644 --- a/libnitrokey/device.h +++ b/libnitrokey/device.h @@ -24,6 +24,7 @@ #include #include "hidapi/hidapi.h" #include +#include #include #include #include "misc.h" @@ -154,6 +155,11 @@ public: */ static std::vector enumerate(); + /** + * Create a Device of the given model. + */ + static std::shared_ptr create(DeviceModel model); + void show_stats(); // ErrorCounters get_stats(){ return m_counters; } -- cgit v1.2.1 From 66763febd7990f35d34345175257b2ad9401e829 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Sun, 13 Jan 2019 12:04:42 +0100 Subject: Implement operator<< for DeviceModel --- libnitrokey/device.h | 3 +++ 1 file changed, 3 insertions(+) (limited to 'libnitrokey/device.h') diff --git a/libnitrokey/device.h b/libnitrokey/device.h index eab3888..4b1c239 100644 --- a/libnitrokey/device.h +++ b/libnitrokey/device.h @@ -26,6 +26,7 @@ #include #include #include +#include #include #include "misc.h" @@ -52,6 +53,8 @@ enum class DeviceModel{ STORAGE }; +std::ostream& operator<<(std::ostream& stream, DeviceModel model); + /** * The USB vendor ID for Nitrokey devices. */ -- cgit v1.2.1 From a80378e0c770a503ddaafc0c7aacb78cac667b8f Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Sun, 13 Jan 2019 12:05:28 +0100 Subject: Change std::wstring to std::string in DeviceInfo For easier handling, we should use a std::string instead of std::wstring for the serial number in DeviceInfo. For the conversion, I assume that the serial number is valid UTF-8. As it should be alphanumeric and ASCII only, this should be true. --- libnitrokey/device.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'libnitrokey/device.h') diff --git a/libnitrokey/device.h b/libnitrokey/device.h index 4b1c239..d50080d 100644 --- a/libnitrokey/device.h +++ b/libnitrokey/device.h @@ -92,7 +92,7 @@ struct DeviceInfo { /** * The serial number of the device. */ - std::wstring m_serialNumber; + std::string m_serialNumber; }; #include -- cgit v1.2.1