// SPDX-FileCopyrightText: Copyright 2026 Gloria Goertz, Mireille Meyer // SPDX-License-Identifier: BSD-2-Clause #include "Internal.hh" #include #include #include #include #include #include #include #include #include "../../StringTools.hh" namespace atom::IO { struct DeviceProperties { public: bool isValid; u16 vendorId; u16 productId; bool isHID; union { bool hidIsBluetooth; u8 usbInterface; }; }; struct HIDDeviceData { public: HANDLE handle; OVERLAPPED overlapped; }; template T HexStringToInt(const std::string& string) requires std::is_integral_v { T value = 0; const auto [_, ec] = std::from_chars(string.c_str(), string.c_str() + string.size(), value, 16); ATOM_ASSERT(ec == std::errc(), "std::from_chars failed"); return value; } DeviceProperties ParseDevicePath(const wchar_t* ATOM_NONNULL data) { DeviceProperties props = { false, 0, 0, false, 0 }; const size_t pidOffset = StringLengthFast("_PID&"); const size_t usbInterfaceOffset = StringLengthFast("&REV_0000&MI_"); auto path = WCHARToStdString(data).Unwrap(); if (path.starts_with("HID\\VID")) { // HID - USB const size_t offset = StringLengthFast("HID\\VID_"); props.vendorId = HexStringToInt(path.substr(offset, 4)); props.productId = HexStringToInt(path.substr(offset + 4 + pidOffset, 4)); props.isValid = true; props.isHID = true; props.hidIsBluetooth = false; } else if (path.starts_with("HID\\{")) { // HID - Bluetooth const size_t baseOffset = StringLengthFast("HID\\{00000000-0000-0000-0000-000000000000}"); if (path.substr(baseOffset, 4) == "_Dev") { const size_t offset = baseOffset + StringLengthFast("_Dev_VID&00"); props.vendorId = HexStringToInt(path.substr(offset, 4)); props.productId = HexStringToInt(path.substr(offset + 4 + pidOffset, 4)); } else { const size_t offset = baseOffset + StringLengthFast("_VID&0000"); props.vendorId = HexStringToInt(path.substr(offset, 4)); props.productId = HexStringToInt(path.substr(offset + 4 + pidOffset, 4)); } props.isValid = true; props.isHID = true; props.hidIsBluetooth = true; } else if (path.starts_with("USB\\VID")) { const size_t offset = StringLengthFast("USB\\VID_"); props.vendorId = HexStringToInt(path.substr(offset, 4)); props.productId = HexStringToInt(path.substr(offset + 4 + pidOffset, 4)); props.isValid = true; props.hidIsBluetooth = false; if (path.size() >= offset + pidOffset + usbInterfaceOffset + 8 + 2) { props.usbInterface = HexStringToInt(path.substr(offset + pidOffset + usbInterfaceOffset + 8, 2)); } } else if (path.starts_with("USB\\{")) { const size_t offset = StringLengthFast("USB\\{00000000-0000-0000-0000-000000000000}_VID&0000"); props.vendorId = HexStringToInt(path.substr(offset, 4)); props.productId = HexStringToInt(path.substr(offset + 4 + pidOffset, 4)); props.isValid = true; props.hidIsBluetooth = false; if (path.size() >= offset + pidOffset + usbInterfaceOffset + 8 + 2) { props.usbInterface = HexStringToInt(path.substr(offset + pidOffset + usbInterfaceOffset + 8, 2)); } } return props; } Result FindDevice(const GUID* interfaceClassGUID, const GUID& classGUID, const GUID& interfaceGUID, const u16 vendorId, const u16 productId, const bool isHID, const u8 usbInterface) { HDEVINFO info = SetupDiGetClassDevsW(interfaceClassGUID, nullptr, nullptr, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE | (interfaceClassGUID == nullptr ? DIGCF_ALLCLASSES : 0)); const DWORD error = GetLastError(); if (info == INVALID_HANDLE_VALUE || info == nullptr) { ATOM_PANIC("SetupDiGetClassDevsW failed"); } SP_DEVINFO_DATA data = { .cbSize = sizeof(SP_DEVINFO_DATA), .ClassGuid = { 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0 } }, .DevInst = 0, .Reserved = 0 }; SP_DEVICE_INTERFACE_DATA interfaceData = { .cbSize = sizeof(SP_DEVICE_INTERFACE_DATA), .InterfaceClassGuid = { 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0 } }, .Flags = 0, .Reserved = 0 }; SP_DEVICE_INTERFACE_DETAIL_DATA_W* interfaceDetailData; DWORD interfaceDetailDataSize = 0; std::unique_ptr path = nullptr; bool isHidBluetooth = false; for (u32 index = 0; index < UINT32_MAX; index++) { BOOL result = SetupDiEnumDeviceInfo(info, index, &data); if (result == FALSE) { const DWORD error = GetLastError(); if (error == ERROR_NO_MORE_ITEMS) { break; } switch (error) { default: { ATOM_PANIC("SetupDiEnumDeviceInfo failed"); } } } if (!IsEqualGUID(data.ClassGuid, classGUID)) { continue; } DEVPROPTYPE propertyType = 0; DWORD size = 0; result = SetupDiGetDevicePropertyW(info, &data, &DEVPKEY_Device_HardwareIds, &propertyType, nullptr, 0, &size, 0); if (result == FALSE) { switch (GetLastError()) { case ERROR_INSUFFICIENT_BUFFER: { break; } default: { ATOM_PANIC("SetupDiGetDevicePropertyW failed"); } } } ATOM_ASSERT(propertyType == DEVPROP_TYPE_STRING_LIST, "propertyType != DEVPROP_TYPE_STRING_LIST ({}) (is {})", DEVPROP_TYPE_STRING_LIST, propertyType); auto hardwareIDs = std::make_unique(size + 1); result = SetupDiGetDevicePropertyW(info, &data, &DEVPKEY_Device_HardwareIds, &propertyType, reinterpret_cast(hardwareIDs.get()), size, &size, 0); if (result == FALSE) { ATOM_PANIC("SetupDiGetDevicePropertyW failed"); } const DeviceProperties props = ParseDevicePath(hardwareIDs.get()); if (!props.isValid || props.vendorId != vendorId || props.productId != productId || props.isHID != isHID) { continue; } result = SetupDiEnumDeviceInterfaces(info, &data, &interfaceGUID, 0, &interfaceData); if (result == FALSE) { ATOM_PANIC("SetupDiEnumDeviceInterfaces failed"); } result = SetupDiGetDeviceInterfaceDetailW(info, &interfaceData, nullptr, 0, &interfaceDetailDataSize, nullptr); if (result == FALSE) { if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) { ATOM_PANIC("SetupDiGetDeviceInterfaceDetailW failed"); } } interfaceDetailData = reinterpret_cast(new u8[interfaceDetailDataSize]); interfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_W); result = SetupDiGetDeviceInterfaceDetailW(info, &interfaceData, interfaceDetailData, interfaceDetailDataSize, &interfaceDetailDataSize, nullptr); if (result == FALSE) { ATOM_PANIC("SetupDiGetDeviceInterfaceDetailW failed"); } usize pathSize = wcslen(interfaceDetailData->DevicePath); path = std::make_unique(pathSize + sizeof(wchar_t)); std::memcpy(path.get(), interfaceDetailData->DevicePath, pathSize * sizeof(wchar_t)); if (props.isHID) { isHidBluetooth = props.hidIsBluetooth; } delete interfaceDetailData; break; } SetupDiDestroyDeviceInfoList(info); if (path == nullptr) { return Status::NotFound; } HANDLE device = CreateFileW(path.get(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, nullptr); if (device == INVALID_HANDLE_VALUE || device == nullptr) { switch (GetLastError()) { case ERROR_SHARING_VIOLATION: { return Status::Locked; } default: { ATOM_PANIC("CreateFileW failed"); } } } return DeviceResult { device, isHidBluetooth }; } }