From 4a7ce051bd4004fb62f1c7022d92efa2ce42b6ab Mon Sep 17 00:00:00 2001
From: Robin Krahl <me@robin-krahl.de>
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<std::string> to std::vector<DeviceInfo> to expose the
additional information contained in the DeviceInfo struct.
---
 NitrokeyManager.cc                | 10 ++++++++--
 device.cc                         | 11 +++++++----
 libnitrokey/device.h              |  8 +++++++-
 unittest/test_multiple_devices.cc |  6 ++++--
 4 files changed, 26 insertions(+), 9 deletions(-)

diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc
index a950e4b..8825fce 100644
--- a/NitrokeyManager.cc
+++ b/NitrokeyManager.cc
@@ -109,7 +109,12 @@ using nitrokey::misc::strcpyT;
         std::lock_guard<std::mutex> lock(mex_dev_com_manager);
 
         auto p = make_shared<Stick20>();
-        return p->enumerate(); // make static
+        auto device_infos = p->enumerate();
+        std::vector<std::string> strings;
+        strings.resize(device_infos.size());
+        std::transform(device_infos.begin(), device_infos.end(), strings.begin(),
+            [](auto device_info) { return device_info.m_path; });
+        return strings;
     }
 
     std::vector<std::string> NitrokeyManager::list_devices_by_cpuID(){
@@ -130,7 +135,8 @@ using nitrokey::misc::strcpyT;
         auto d = make_shared<Stick20>();
         const auto v = d->enumerate();
         LOGD1("Discovering IDs");
-        for (auto & p: v){
+        for (auto & i: v){
+            auto p = i.m_path;
             d = make_shared<Stick20>();
             LOGD1( std::string("Found: ") + p );
             d->set_path(p);
diff --git a/device.cc b/device.cc
index 80e4b38..506a68c 100644
--- a/device.cc
+++ b/device.cc
@@ -171,14 +171,17 @@ int Device::recv(void *packet) {
   return status;
 }
 
-std::vector<std::string> Device::enumerate(){
+std::vector<DeviceInfo> Device::enumerate(){
   //TODO make static
   auto pInfo = hid_enumerate(m_vid, m_pid);
   auto pInfo_ = pInfo;
-  std::vector<std::string> res;
+  std::vector<DeviceInfo> res;
   while (pInfo != nullptr){
-    std::string a (pInfo->path);
-    res.push_back(a);
+    std::string path(pInfo->path);
+    std::wstring serialNumber(pInfo->serial_number);
+    auto deviceModel = this->get_device_model();
+    DeviceInfo info = { deviceModel, path, serialNumber };
+    res.push_back(info);
     pInfo = pInfo->next;
   }
 
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<std::string> 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<DeviceInfo> enumerate();
 
 
         void show_stats();
diff --git a/unittest/test_multiple_devices.cc b/unittest/test_multiple_devices.cc
index cd78681..b224653 100644
--- a/unittest/test_multiple_devices.cc
+++ b/unittest/test_multiple_devices.cc
@@ -37,7 +37,8 @@ TEST_CASE("List devices", "[BASIC]") {
     shared_ptr<Stick20> d = make_shared<Stick20>();
     auto v = d->enumerate();
     REQUIRE(v.size() > 0);
-    for (auto a : v){
+    for (auto i : v){
+        auto a = i.m_path;
         std::cout << a;
         d->set_path(a);
         d->connect();
@@ -57,7 +58,8 @@ TEST_CASE("Regenerate AES keys", "[BASIC]") {
     REQUIRE(v.size() > 0);
 
     std::vector<shared_ptr<Stick20>> devices;
-    for (auto a : v){
+    for (auto i : v){
+        auto a = i.m_path;
         std::cout << a << endl;
         d = make_shared<Stick20>();
         d->set_path(a);
-- 
cgit v1.2.3