-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice_info.cpp
More file actions
200 lines (164 loc) · 6.8 KB
/
device_info.cpp
File metadata and controls
200 lines (164 loc) · 6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "device_info.h"
#include <Windows.h>
#include <SetupAPI.h>
#include <devguid.h>
#include <cfgmgr32.h>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <regex>
// Link SetupAPI
#pragma comment(lib, "setupapi.lib")
std::wstring USBDeviceInfo::getVidPidString() const {
std::wostringstream oss;
oss << L"VID:" << std::uppercase << std::hex << std::setfill(L'0') << std::setw(4) << vendorId
<< L" PID:" << std::setw(4) << productId;
return oss.str();
}
std::wstring USBDeviceInfo::getDisplayString() const {
std::wostringstream oss;
oss << name;
if (vendorId != 0 || productId != 0) {
oss << L" (" << getVidPidString() << L")";
}
if (!locationInfo.empty()) {
oss << L" " << locationInfo;
}
if (maxPowerMa > 0) {
oss << L" Power:" << std::dec << maxPowerMa << L"mA";
}
return oss.str();
}
std::wstring AudioDeviceInfo::getDisplayString() const {
std::wostringstream oss;
oss << (isOutput ? L"[OUT] " : L"[IN] ");
oss << name;
if (isDefault) {
oss << L" (Default)";
}
oss << L" - " << (isActive ? L"Active" : L"Disabled");
return oss.str();
}
bool parseVidPid(const std::wstring& devicePath, uint16_t& vid, uint16_t& pid) {
// Pattern: VID_XXXX&PID_XXXX or vid_xxxx&pid_xxxx
std::wstring upper = devicePath;
std::transform(upper.begin(), upper.end(), upper.begin(), ::towupper);
size_t vidPos = upper.find(L"VID_");
size_t pidPos = upper.find(L"PID_");
if (vidPos != std::wstring::npos && pidPos != std::wstring::npos) {
try {
vid = static_cast<uint16_t>(std::stoul(upper.substr(vidPos + 4, 4), nullptr, 16));
pid = static_cast<uint16_t>(std::stoul(upper.substr(pidPos + 4, 4), nullptr, 16));
return true;
} catch (...) {
return false;
}
}
return false;
}
std::wstring getDeviceProperty(HDEVINFO deviceInfoSet, SP_DEVINFO_DATA& deviceInfoData, DWORD property) {
DWORD dataType = 0;
DWORD requiredSize = 0;
SetupDiGetDeviceRegistryPropertyW(deviceInfoSet, &deviceInfoData, property,
&dataType, nullptr, 0, &requiredSize);
if (requiredSize == 0) return L"";
std::vector<BYTE> buffer(requiredSize);
if (SetupDiGetDeviceRegistryPropertyW(deviceInfoSet, &deviceInfoData, property,
&dataType, buffer.data(), requiredSize, nullptr)) {
return std::wstring(reinterpret_cast<wchar_t*>(buffer.data()));
}
return L"";
}
std::vector<USBDeviceInfo> enumerateUSBDevices() {
std::vector<USBDeviceInfo> devices;
// Get USB device class
HDEVINFO deviceInfoSet = SetupDiGetClassDevsW(
nullptr, L"USB", nullptr,
DIGCF_PRESENT | DIGCF_ALLCLASSES);
if (deviceInfoSet == INVALID_HANDLE_VALUE) {
return devices;
}
SP_DEVINFO_DATA deviceInfoData;
deviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (DWORD i = 0; SetupDiEnumDeviceInfo(deviceInfoSet, i, &deviceInfoData); i++) {
USBDeviceInfo device;
// Get device description
device.description = getDeviceProperty(deviceInfoSet, deviceInfoData, SPDRP_DEVICEDESC);
device.name = getDeviceProperty(deviceInfoSet, deviceInfoData, SPDRP_FRIENDLYNAME);
if (device.name.empty()) {
device.name = device.description;
}
// Skip if no name
if (device.name.empty()) continue;
// Get hardware ID for VID/PID
std::wstring hardwareId = getDeviceProperty(deviceInfoSet, deviceInfoData, SPDRP_HARDWAREID);
parseVidPid(hardwareId, device.vendorId, device.productId);
// Get location info
device.locationInfo = getDeviceProperty(deviceInfoSet, deviceInfoData, SPDRP_LOCATION_INFORMATION);
// Get device instance path
WCHAR instanceId[MAX_DEVICE_ID_LEN];
if (CM_Get_Device_IDW(deviceInfoData.DevInst, instanceId, MAX_DEVICE_ID_LEN, 0) == CR_SUCCESS) {
device.devicePath = instanceId;
}
// Try to get power info from registry
// USB devices report max power in their configuration descriptor
// This is stored in registry under device parameters
HKEY hKey = SetupDiOpenDevRegKey(deviceInfoSet, &deviceInfoData,
DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_READ);
if (hKey != INVALID_HANDLE_VALUE) {
DWORD powerValue = 0;
DWORD size = sizeof(powerValue);
// Some USB devices store power in registry, but it's not standardized
RegCloseKey(hKey);
}
// Skip hub devices and root hubs
if (device.name.find(L"Hub") != std::wstring::npos ||
device.name.find(L"Root") != std::wstring::npos) {
continue;
}
devices.push_back(device);
}
SetupDiDestroyDeviceInfoList(deviceInfoSet);
return devices;
}
USBDeviceInfo getUSBDeviceFromPath(const std::wstring& devicePath) {
USBDeviceInfo device;
device.devicePath = devicePath;
parseVidPid(devicePath, device.vendorId, device.productId);
// Try to find more info about this device
HDEVINFO deviceInfoSet = SetupDiGetClassDevsW(
nullptr, L"USB", nullptr,
DIGCF_PRESENT | DIGCF_ALLCLASSES);
if (deviceInfoSet == INVALID_HANDLE_VALUE) {
device.name = L"USB Device";
return device;
}
SP_DEVINFO_DATA deviceInfoData;
deviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (DWORD i = 0; SetupDiEnumDeviceInfo(deviceInfoSet, i, &deviceInfoData); i++) {
WCHAR instanceId[MAX_DEVICE_ID_LEN];
if (CM_Get_Device_IDW(deviceInfoData.DevInst, instanceId, MAX_DEVICE_ID_LEN, 0) == CR_SUCCESS) {
std::wstring currentPath = instanceId;
// Check if this device matches (case insensitive partial match)
std::wstring upperPath = devicePath;
std::wstring upperCurrent = currentPath;
std::transform(upperPath.begin(), upperPath.end(), upperPath.begin(), ::towupper);
std::transform(upperCurrent.begin(), upperCurrent.end(), upperCurrent.begin(), ::towupper);
if (upperPath.find(upperCurrent) != std::wstring::npos ||
upperCurrent.find(upperPath) != std::wstring::npos) {
device.description = getDeviceProperty(deviceInfoSet, deviceInfoData, SPDRP_DEVICEDESC);
device.name = getDeviceProperty(deviceInfoSet, deviceInfoData, SPDRP_FRIENDLYNAME);
if (device.name.empty()) {
device.name = device.description;
}
device.locationInfo = getDeviceProperty(deviceInfoSet, deviceInfoData, SPDRP_LOCATION_INFORMATION);
break;
}
}
}
SetupDiDestroyDeviceInfoList(deviceInfoSet);
if (device.name.empty()) {
device.name = L"USB Device (" + device.getVidPidString() + L")";
}
return device;
}