Skip to content

Commit

Permalink
Fix device discovery in NitrokeyManager::connect_with_path
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
d-e-s-o committed Mar 21, 2021
1 parent a9e2ac0 commit 0da3469
Showing 1 changed file with 31 additions and 31 deletions.
62 changes: 31 additions & 31 deletions NitrokeyManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<DeviceModel> 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<DeviceModel> 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())
auto p = Device::create(model.value());
if (!p)
return false;
p->set_path(path);

auto p = Device::create(model.value());
if (!p)
return false;
p->set_path(path);
if(!p->connect()) return false;

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() {
Expand Down

0 comments on commit 0da3469

Please sign in to comment.