Skip to content

Commit

Permalink
Reworked devcon::restart_bth_usb_device
Browse files Browse the repository at this point in the history
  • Loading branch information
nefarius committed Aug 7, 2024
1 parent 83e13f4 commit 5d68eb9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 64 deletions.
110 changes: 47 additions & 63 deletions src/Devcon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,94 +259,78 @@ std::expected<void, Win32Error> devcon::update(const std::wstring& hardwareId, c
return std::unexpected(Win32Error(ERROR_INTERNAL_ERROR));
}

bool devcon::restart_bth_usb_device()
std::expected<void, Win32Error> devcon::restart_bth_usb_device(int instance)
{
DWORD i, err;
bool found = false, succeeded = false;

HDEVINFO hDevInfo;
bool found = false;
SP_DEVINFO_DATA spDevInfoData;

hDevInfo = SetupDiGetClassDevs(
HDEVINFO hDevInfo = SetupDiGetClassDevs(
&GUID_DEVCLASS_BLUETOOTH,
nullptr,
nullptr,
DIGCF_PRESENT
);

const auto guard = sg::make_scope_guard([hDevInfo]() noexcept
{
if (hDevInfo != INVALID_HANDLE_VALUE)
{
SetupDiDestroyDeviceInfoList(hDevInfo);
}
});

if (hDevInfo == INVALID_HANDLE_VALUE)
{
return succeeded;
return std::unexpected(Win32Error(GetLastError(), "SetupDiGetClassDevs"));
}

spDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &spDevInfoData); i++)

if (!SetupDiEnumDeviceInfo(hDevInfo, instance, &spDevInfoData))
{
DWORD DataT;
LPTSTR p, buffer = nullptr;
DWORD buffersize = 0;
std::unexpected(Win32Error(GetLastError(), "SetupDiEnumDeviceInfo"));
}

// get all devices info
while (!SetupDiGetDeviceRegistryProperty(hDevInfo,
&spDevInfoData,
SPDRP_ENUMERATOR_NAME,
&DataT,
(PBYTE)buffer,
buffersize,
&buffersize))
{
if (GetLastError() == ERROR_INVALID_DATA)
{
break;
}
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
if (buffer)
LocalFree(buffer);
buffer = static_cast<wchar_t*>(LocalAlloc(LPTR, buffersize));
}
else
{
goto cleanup_DeviceInfo;
}
}
DWORD bufferSize = 0;
const auto enumeratorProperty = GetDeviceRegistryProperty(
hDevInfo,
&spDevInfoData,
SPDRP_ENUMERATOR_NAME,
NULL,
&bufferSize
);

if (GetLastError() == ERROR_INVALID_DATA)
continue;
if (!enumeratorProperty)
{
return std::unexpected(enumeratorProperty.error());
}

//find device with enumerator name "USB"
for (p = buffer; *p && (p < &buffer[buffersize]); p += lstrlen(p) + sizeof(TCHAR))
const LPTSTR buffer = (LPTSTR)enumeratorProperty.value();

// find device with enumerator name "USB"
for (LPTSTR p = buffer; p && *p && (p < &buffer[bufferSize]); p += lstrlen(p) + sizeof(TCHAR))
{
if (!_tcscmp(TEXT("USB"), p))
{
if (!_tcscmp(TEXT("USB"), p))
{
found = true;
break;
}
found = true;
break;
}
}

if (buffer)
LocalFree(buffer);
LocalFree(buffer);

// if device found restart
if (found)
// if device found restart
if (found)
{
if (!SetupDiRestartDevices(hDevInfo, &spDevInfoData))
{
if (!SetupDiRestartDevices(hDevInfo, &spDevInfoData))
{
err = GetLastError();
break;
}

succeeded = true;

break;
std::unexpected(Win32Error(GetLastError(), "SetupDiRestartDevices"));
}
}

cleanup_DeviceInfo:
err = GetLastError();
SetupDiDestroyDeviceInfoList(hDevInfo);
SetLastError(err);
return {};
}

return succeeded;
return std::unexpected(Win32Error(ERROR_NOT_FOUND));
}

bool devcon::enable_disable_bth_usb_device(bool state)
Expand Down
2 changes: 1 addition & 1 deletion src/Devcon.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace devcon
const std::wstring& fullInfPath, bool* rebootRequired,
bool force = false);

bool restart_bth_usb_device();
std::expected<void, nefarius::util::Win32Error> restart_bth_usb_device(int instance = 0);

bool enable_disable_bth_usb_device(bool state);

Expand Down

0 comments on commit 5d68eb9

Please sign in to comment.