Skip to content

Commit 6401ca4

Browse files
committed
Merge branch 'topic/fix-device-discovery'
Correct logic for the device discovery Fixes #194
2 parents ecf993b + 3faa000 commit 6401ca4

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

NitrokeyManager.cc

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -217,44 +217,44 @@ using nitrokey::misc::strcpyT;
217217
}
218218
}
219219

220-
auto vendor_id = NITROKEY_VID;
221-
auto info_ptr = hid_enumerate(vendor_id, 0);
222-
if (!info_ptr) {
223-
vendor_id = PURISM_VID;
224-
info_ptr = hid_enumerate(vendor_id, 0);
225-
}
226-
auto first_info_ptr = info_ptr;
227-
if (!info_ptr)
228-
return false;
220+
auto vendor_ids = { NITROKEY_VID, PURISM_VID };
221+
for (auto vendor_id : vendor_ids) {
222+
auto info_ptr = hid_enumerate(vendor_id, 0);
223+
if (!info_ptr) {
224+
continue;
225+
}
226+
auto first_info_ptr = info_ptr;
227+
228+
misc::Option<DeviceModel> model;
229+
while (info_ptr && !model.has_value()) {
230+
if (path == std::string(info_ptr->path)) {
231+
model = product_id_to_model(info_ptr->vendor_id, info_ptr->product_id);
232+
}
233+
info_ptr = info_ptr->next;
234+
}
235+
hid_free_enumeration(first_info_ptr);
229236

230-
misc::Option<DeviceModel> model;
231-
while (info_ptr && !model.has_value()) {
232-
if (path == std::string(info_ptr->path)) {
233-
model = product_id_to_model(info_ptr->vendor_id, info_ptr->product_id);
234-
}
235-
info_ptr = info_ptr->next;
236-
}
237-
hid_free_enumeration(first_info_ptr);
237+
if (!model.has_value())
238+
continue;
238239

239-
if (!model.has_value())
240-
return false;
240+
auto p = Device::create(model.value());
241+
if (!p)
242+
continue;
243+
p->set_path(path);
241244

242-
auto p = Device::create(model.value());
243-
if (!p)
244-
return false;
245-
p->set_path(path);
245+
if(!p->connect()) continue;
246246

247-
if(!p->connect()) return false;
247+
if(cache_connections){
248+
connected_devices [path] = p;
249+
}
248250

249-
if(cache_connections){
250-
connected_devices [path] = p;
251+
device = p; //previous device will be disconnected automatically
252+
current_device_id = path;
253+
nitrokey::log::Log::setPrefix(path);
254+
LOGD1("Device successfully changed");
255+
return true;
251256
}
252-
253-
device = p; //previous device will be disconnected automatically
254-
current_device_id = path;
255-
nitrokey::log::Log::setPrefix(path);
256-
LOGD1("Device successfully changed");
257-
return true;
257+
return false;
258258
}
259259

260260
bool NitrokeyManager::connect() {

0 commit comments

Comments
 (0)