diff options
| author | Yuval Adam <_@yuv.al> | 2018-07-23 22:03:37 +0300 |
|---|---|---|
| committer | Yuval Adam <_@yuv.al> | 2018-07-23 22:03:37 +0300 |
| commit | 39ea216984f4b7b5aa861c9605ccf5de720bee7e (patch) | |
| tree | 5cc7007b1622421a01bd4617728c2a2aa397eab1 | |
| parent | 7092c0f2d29d8b7e2d9af23dac4b87e08ab1dc02 (diff) | |
Lifetime problems with opening the device
| -rw-r--r-- | src/lib.rs | 25 |
1 files changed, 12 insertions, 13 deletions
@@ -5,32 +5,31 @@ const KNOWN_DEVICES: [(u16, u16, &str); 2] = [ (0x0bda, 0x2838, "Generic RTL2832U OEM") ]; -struct Usb { +struct Usb<'a> { ctx: libusb::Context, - desc: Option<libusb::DeviceDescriptor> - // dev: Option<libusb::Device> + dev: Option<libusb::DeviceHandle<'a>> } -pub struct RtlSdr { - usb: Usb +pub struct RtlSdr<'a> { + usb: Usb<'a> } -impl RtlSdr { +impl<'a> RtlSdr<'a> { - pub fn new() -> RtlSdr { + pub fn new() -> RtlSdr<'a> { RtlSdr { usb: Usb { ctx: libusb::Context::new().unwrap(), - desc: None + dev: None } } } pub fn init(&mut self) { - self.usb.desc = self.find_device(); + self.usb.dev = self.find_device(); } - pub fn find_device(&self) -> Option<libusb::DeviceDescriptor> { + pub fn find_device(&self) -> Option<libusb::DeviceHandle<'a>> { for mut dev in self.usb.ctx.devices().unwrap().iter() { let desc = dev.device_descriptor().unwrap(); let vid = desc.vendor_id(); @@ -38,7 +37,7 @@ impl RtlSdr { for kd in KNOWN_DEVICES.iter() { if kd.0 == vid && kd.1 == pid { - return Some(desc) + return Some(dev.open().unwrap()) } } } @@ -53,8 +52,8 @@ mod tests { #[test] fn test_init() { let mut rtlsdr = RtlSdr::new(); - assert!(rtlsdr.usb.desc.is_none()); + assert!(rtlsdr.usb.dev.is_none()); rtlsdr.init(); - assert!(rtlsdr.usb.desc.is_some()); + assert!(rtlsdr.usb.dev.is_some()); } } |
