From 3faa00097b6b9c391f56f68d9a948f2780572522 Mon Sep 17 00:00:00 2001 From: Daniel Mueller Date: Sat, 20 Mar 2021 17:42:03 -0700 Subject: [PATCH] Fix device discovery in NitrokeyManager::connect_with_path The device discovery in NitrokeyManager's connect_with_path method is broken. It tries to statically determine the vendor ID by using the first working HID pointer. That's obviously not working when a both a Nitrokey device and Librem Key are connected and the path is describing a Librem Key. In such a case the vendor ID used would be that for the Nitrokey and we would not be able to find the Librem Key at all. This change fixes up the logic. --- NitrokeyManager.cc | 64 +++++++++++++++++++++++----------------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc index 329d155e..80b3537d 100644 --- a/NitrokeyManager.cc +++ b/NitrokeyManager.cc @@ -217,44 +217,44 @@ using nitrokey::misc::strcpyT; } } - auto vendor_id = NITROKEY_VID; - auto info_ptr = hid_enumerate(vendor_id, 0); - if (!info_ptr) { - vendor_id = PURISM_VID; - info_ptr = hid_enumerate(vendor_id, 0); - } - auto first_info_ptr = info_ptr; - if (!info_ptr) - return false; + auto vendor_ids = { NITROKEY_VID, PURISM_VID }; + for (auto vendor_id : vendor_ids) { + auto info_ptr = hid_enumerate(vendor_id, 0); + if (!info_ptr) { + continue; + } + auto first_info_ptr = info_ptr; + + misc::Option model; + while (info_ptr && !model.has_value()) { + if (path == std::string(info_ptr->path)) { + model = product_id_to_model(info_ptr->vendor_id, info_ptr->product_id); + } + info_ptr = info_ptr->next; + } + hid_free_enumeration(first_info_ptr); - misc::Option model; - while (info_ptr && !model.has_value()) { - if (path == std::string(info_ptr->path)) { - model = product_id_to_model(info_ptr->vendor_id, info_ptr->product_id); - } - info_ptr = info_ptr->next; - } - hid_free_enumeration(first_info_ptr); + if (!model.has_value()) + continue; - if (!model.has_value()) - return false; + auto p = Device::create(model.value()); + if (!p) + continue; + p->set_path(path); - auto p = Device::create(model.value()); - if (!p) - return false; - p->set_path(path); + if(!p->connect()) continue; - if(!p->connect()) return false; + if(cache_connections){ + connected_devices [path] = p; + } - if(cache_connections){ - connected_devices [path] = p; + device = p; //previous device will be disconnected automatically + current_device_id = path; + nitrokey::log::Log::setPrefix(path); + LOGD1("Device successfully changed"); + return true; } - - device = p; //previous device will be disconnected automatically - current_device_id = path; - nitrokey::log::Log::setPrefix(path); - LOGD1("Device successfully changed"); - return true; + return false; } bool NitrokeyManager::connect() {