| 1 |
use std::{ffi::OsString, iter, mem, ptr}; |
| 2 |
|
| 3 |
use log::debug; |
| 4 |
use windows_sys::{ |
| 5 |
core::GUID, |
| 6 |
Win32::{ |
| 7 |
Devices::{ |
| 8 |
DeviceAndDriverInstallation::{ |
| 9 |
CM_Get_Child, CM_Get_DevNode_PropertyW, CM_Get_Device_Interface_ListW, |
| 10 |
CM_Get_Device_Interface_List_SizeW, CM_Get_Device_Interface_PropertyW, |
| 11 |
CM_Get_Parent, CM_Get_Sibling, CM_Locate_DevNodeW, CM_Open_DevNode_Key, |
| 12 |
RegDisposition_OpenExisting, CM_GET_DEVICE_INTERFACE_LIST_PRESENT, |
| 13 |
CM_LOCATE_DEVNODE_PHANTOM, CM_REGISTRY_HARDWARE, CR_BUFFER_SMALL, CR_SUCCESS, |
| 14 |
}, |
| 15 |
Properties::{ |
| 16 |
DEVPKEY_Device_InstanceId, DEVPROPKEY, DEVPROPTYPE, DEVPROP_TYPE_STRING, |
| 17 |
DEVPROP_TYPE_STRING_LIST, DEVPROP_TYPE_UINT32, |
| 18 |
}, |
| 19 |
}, |
| 20 |
Foundation::INVALID_HANDLE_VALUE, |
| 21 |
System::Registry::KEY_READ, |
| 22 |
}, |
| 23 |
}; |
| 24 |
|
| 25 |
use super::{ |
| 26 |
registry::RegKey, |
| 27 |
util::{NulSepList, NulSepListIter, WCStr, WCString}, |
| 28 |
}; |
| 29 |
|
| 30 |
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] |
| 31 |
pub struct DevInst(u32); |
| 32 |
|
| 33 |
impl DevInst { |
| 34 |
pub fn from_instance_id(id: &WCStr) -> Option<DevInst> { |
| 35 |
let mut devinst = 0; |
| 36 |
let c = unsafe { CM_Locate_DevNodeW(&mut devinst, id.as_ptr(), CM_LOCATE_DEVNODE_PHANTOM) }; |
| 37 |
if c == CR_SUCCESS { |
| 38 |
Some(DevInst(devinst)) |
| 39 |
} else { |
| 40 |
None |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
pub fn get_property<T: PropertyType>(&self, pkey: DEVPROPKEY) -> Option<T> { |
| 45 |
let mut property_type: DEVPROPTYPE = 0; |
| 46 |
let mut buffer: T::Buffer = T::empty_buffer(); |
| 47 |
let mut size: u32 = mem::size_of_val(&buffer) as u32; |
| 48 |
|
| 49 |
let r = unsafe { |
| 50 |
CM_Get_DevNode_PropertyW( |
| 51 |
self.0, |
| 52 |
&pkey, |
| 53 |
&mut property_type, |
| 54 |
&mut buffer as *mut _ as *mut u8, |
| 55 |
&mut size, |
| 56 |
0, |
| 57 |
) |
| 58 |
}; |
| 59 |
|
| 60 |
if r == CR_SUCCESS && property_type == T::PROPTYPE { |
| 61 |
Some(T::from_buffer(&buffer)) |
| 62 |
} else { |
| 63 |
None |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
pub fn instance_id(&self) -> WCString { |
| 68 |
self.get_property(DEVPKEY_Device_InstanceId) |
| 69 |
.expect("device should always have instance ID") |
| 70 |
} |
| 71 |
|
| 72 |
pub fn parent(&self) -> Option<DevInst> { |
| 73 |
let mut out = 0; |
| 74 |
let cr = unsafe { CM_Get_Parent(&mut out, self.0, 0) }; |
| 75 |
if cr == CR_SUCCESS { |
| 76 |
Some(DevInst(out)) |
| 77 |
} else { |
| 78 |
None |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
pub fn first_child(&self) -> Option<DevInst> { |
| 83 |
let mut out = 0; |
| 84 |
let cr = unsafe { CM_Get_Child(&mut out, self.0, 0) }; |
| 85 |
if cr == CR_SUCCESS { |
| 86 |
Some(DevInst(out)) |
| 87 |
} else { |
| 88 |
None |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
pub fn next_sibling(&self) -> Option<DevInst> { |
| 93 |
let mut out = 0; |
| 94 |
let cr = unsafe { CM_Get_Sibling(&mut out, self.0, 0) }; |
| 95 |
if cr == CR_SUCCESS { |
| 96 |
Some(DevInst(out)) |
| 97 |
} else { |
| 98 |
None |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
pub fn children(&self) -> impl Iterator<Item = DevInst> { |
| 103 |
let mut node = self.first_child(); |
| 104 |
iter::from_fn(move || { |
| 105 |
if let Some(n) = node { |
| 106 |
node = n.next_sibling(); |
| 107 |
Some(n) |
| 108 |
} else { |
| 109 |
None |
| 110 |
} |
| 111 |
}) |
| 112 |
} |
| 113 |
|
| 114 |
pub fn registry_key(&self) -> Option<RegKey> { |
| 115 |
let mut hkey = INVALID_HANDLE_VALUE; |
| 116 |
let cr = unsafe { |
| 117 |
CM_Open_DevNode_Key( |
| 118 |
self.0, |
| 119 |
KEY_READ, |
| 120 |
0, |
| 121 |
RegDisposition_OpenExisting, |
| 122 |
&mut hkey, |
| 123 |
CM_REGISTRY_HARDWARE, |
| 124 |
) |
| 125 |
}; |
| 126 |
|
| 127 |
if cr == CR_SUCCESS && hkey != INVALID_HANDLE_VALUE { |
| 128 |
Some(unsafe { RegKey::new(hkey) }) |
| 129 |
} else { |
| 130 |
None |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
pub fn interfaces(&self, interface: GUID) -> NulSepList { |
| 139 |
let id = self.instance_id(); |
| 140 |
list_interfaces(interface, Some(&id)) |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
pub trait PropertyType { |
| 145 |
const PROPTYPE: DEVPROPTYPE; |
| 146 |
type Buffer; |
| 147 |
|
| 148 |
fn empty_buffer() -> Self::Buffer; |
| 149 |
fn from_buffer(b: &Self::Buffer) -> Self; |
| 150 |
} |
| 151 |
|
| 152 |
impl PropertyType for u32 { |
| 153 |
const PROPTYPE: DEVPROPTYPE = DEVPROP_TYPE_UINT32; |
| 154 |
type Buffer = u32; |
| 155 |
fn empty_buffer() -> u32 { |
| 156 |
0 |
| 157 |
} |
| 158 |
fn from_buffer(b: &Self::Buffer) -> Self { |
| 159 |
*b |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
impl PropertyType for WCString { |
| 164 |
const PROPTYPE: DEVPROPTYPE = DEVPROP_TYPE_STRING; |
| 165 |
type Buffer = [u16; 1024]; |
| 166 |
fn empty_buffer() -> Self::Buffer { |
| 167 |
[0; 1024] |
| 168 |
} |
| 169 |
fn from_buffer(b: &Self::Buffer) -> Self { |
| 170 |
WCStr::from_slice_until_nul(b).to_owned() |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
impl PropertyType for Vec<WCString> { |
| 175 |
const PROPTYPE: DEVPROPTYPE = DEVPROP_TYPE_STRING_LIST; |
| 176 |
type Buffer = [u16; 1024]; |
| 177 |
fn empty_buffer() -> Self::Buffer { |
| 178 |
[0; 1024] |
| 179 |
} |
| 180 |
fn from_buffer(b: &Self::Buffer) -> Self { |
| 181 |
NulSepListIter(b).map(|s| s.to_owned()).collect() |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
impl PropertyType for OsString { |
| 186 |
const PROPTYPE: DEVPROPTYPE = DEVPROP_TYPE_STRING; |
| 187 |
type Buffer = [u16; 1024]; |
| 188 |
fn empty_buffer() -> Self::Buffer { |
| 189 |
[0; 1024] |
| 190 |
} |
| 191 |
fn from_buffer(b: &Self::Buffer) -> Self { |
| 192 |
WCStr::from_slice_until_nul(b).into() |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
impl PropertyType for Vec<OsString> { |
| 197 |
const PROPTYPE: DEVPROPTYPE = DEVPROP_TYPE_STRING_LIST; |
| 198 |
type Buffer = [u16; 1024]; |
| 199 |
fn empty_buffer() -> Self::Buffer { |
| 200 |
[0; 1024] |
| 201 |
} |
| 202 |
fn from_buffer(b: &Self::Buffer) -> Self { |
| 203 |
NulSepListIter(b).map(|s| s.into()).collect() |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
pub fn list_interfaces(interface: GUID, instance_id: Option<&WCStr>) -> NulSepList { |
| 208 |
let flags = CM_GET_DEVICE_INTERFACE_LIST_PRESENT; |
| 209 |
let mut buf: Vec<u16> = Vec::new(); |
| 210 |
loop { |
| 211 |
let mut len = 0; |
| 212 |
let cr = unsafe { |
| 213 |
CM_Get_Device_Interface_List_SizeW( |
| 214 |
&mut len, |
| 215 |
&interface, |
| 216 |
instance_id.map_or(ptr::null(), |x| x.as_ptr()), |
| 217 |
flags, |
| 218 |
) |
| 219 |
}; |
| 220 |
|
| 221 |
if cr != CR_SUCCESS { |
| 222 |
buf.clear(); |
| 223 |
debug!("CM_Get_Device_Interface_List_SizeW failed, status {cr}"); |
| 224 |
break; |
| 225 |
} |
| 226 |
|
| 227 |
buf.resize(len as usize, 0); |
| 228 |
|
| 229 |
let cr = unsafe { |
| 230 |
CM_Get_Device_Interface_ListW( |
| 231 |
&interface, |
| 232 |
instance_id.map_or(ptr::null(), |x| x.as_ptr()), |
| 233 |
buf.as_mut_ptr(), |
| 234 |
buf.len() as u32, |
| 235 |
flags, |
| 236 |
) |
| 237 |
}; |
| 238 |
|
| 239 |
if cr == CR_SUCCESS { |
| 240 |
break; |
| 241 |
} else if cr == CR_BUFFER_SMALL { |
| 242 |
continue; |
| 243 |
} else { |
| 244 |
buf.clear(); |
| 245 |
debug!("CM_Get_Device_Interface_ListW failed, status {cr}"); |
| 246 |
break; |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
NulSepList(buf) |
| 251 |
} |
| 252 |
|
| 253 |
pub fn get_device_interface_property<T: PropertyType>( |
| 254 |
interface: &WCStr, |
| 255 |
pkey: DEVPROPKEY, |
| 256 |
) -> Option<T> { |
| 257 |
let mut property_type: DEVPROPTYPE = 0; |
| 258 |
let mut buffer: T::Buffer = T::empty_buffer(); |
| 259 |
let mut size: u32 = mem::size_of_val(&buffer) as u32; |
| 260 |
|
| 261 |
let r = unsafe { |
| 262 |
CM_Get_Device_Interface_PropertyW( |
| 263 |
interface.as_ptr(), |
| 264 |
&pkey, |
| 265 |
&mut property_type, |
| 266 |
&mut buffer as *mut _ as *mut u8, |
| 267 |
&mut size, |
| 268 |
0, |
| 269 |
) |
| 270 |
}; |
| 271 |
|
| 272 |
if r == CR_SUCCESS && property_type == T::PROPTYPE { |
| 273 |
Some(T::from_buffer(&buffer)) |
| 274 |
} else { |
| 275 |
None |
| 276 |
} |
| 277 |
} |