Skip to content

Commit

Permalink
Make cancellable between name reads, disable cancel button during inq…
Browse files Browse the repository at this point in the history
…uiry.
  • Loading branch information
capitalistspz committed Oct 21, 2024
1 parent d53e4fe commit a6bab48
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/gui/PairingDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,27 +244,37 @@ void PairingDialog::WorkerThread()

// Search for device
inquiry_info* infos = nullptr;
m_cancelButton->Disable();
const auto respCount = hci_inquiry(hostId, 5, 4, liacLap, &infos, IREQ_CACHE_FLUSH);
m_cancelButton->Enable();
if (respCount <= 0)
{
UpdateCallback(PairingState::SearchFailed);
return;
}
stdx::scope_exit infoFree([&]() { bt_free(infos);});

if (m_threadShouldQuit)
return;

// Open dev to read name
const auto hostDevDesc = hci_open_dev(hostId);
const auto hostDev = hci_open_dev(hostId);
stdx::scope_exit devClose([&]() { hci_close_dev(hostDev);});

char nameBuffer[HCI_MAX_NAME_LENGTH] = {};

bool foundADevice = false;
// Get device name and compare. Would use product and vendor id from SDP, but many third-party Wiimotes don't store them
for (const auto& devInfo : std::span(infos, respCount))
{
const auto& addr = devInfo.bdaddr;
if (hci_read_remote_name(hostDevDesc, &addr, HCI_MAX_NAME_LENGTH, nameBuffer,
2000) != 0 || !isWiimoteName(nameBuffer))
{
const auto err = hci_read_remote_name(hostDev, &addr, HCI_MAX_NAME_LENGTH, nameBuffer,
2000);
if (m_threadShouldQuit)
return;
if (err || !isWiimoteName(nameBuffer))
continue;
}

L2CapWiimote::AddCandidateAddress(addr);
foundADevice = true;
const auto& b = addr.b;
Expand All @@ -275,9 +285,6 @@ void PairingDialog::WorkerThread()
UpdateCallback(PairingState::Finished);
else
UpdateCallback(PairingState::SearchFailed);

bt_free(infos);
hci_close_dev(hostDevDesc);
}
#else
void PairingDialog::WorkerThread()
Expand Down

0 comments on commit a6bab48

Please sign in to comment.