summaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: 41aede90d53bcb0eb2967815705ebe7a5021e383 (plain)
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
extern crate rusb;

mod devices;
mod usb;

use devices::*;
use usb::RtlSdrDeviceHandle;

const INTERFACE_ID: u8 = 0;

const KNOWN_DEVICES: [(u16, u16, &str); 2] = [
    (0x0bda, 0x2832, "Generic RTL2832U"),
    (0x0bda, 0x2838, "Generic RTL2832U OEM"),
];

const KNOWN_TUNERS: [DeviceInfo; 2] = [r820t::DEVICE_INFO, fc0013::DEVICE_INFO];

pub struct RtlSdr {
    // ctx: rusb::Context,
    // device: UsbDevice,
    iface_id: u8,
}

impl RtlSdr {
    pub fn new() -> RtlSdr {
        RtlSdr {
            // ctx: rusb::Context::new().unwrap(),
            iface_id: INTERFACE_ID,
        }
    }

    pub fn open(&mut self) -> bool {
        let mut found = false;
        for dev in rusb::devices().unwrap().iter() {
            let desc = dev.device_descriptor().unwrap();
            let vid = desc.vendor_id();
            let pid = desc.product_id();

            for kd in KNOWN_DEVICES.iter() {
                if kd.0 == vid && kd.1 == pid {
                    println!("Found USB device with vid {} and pid {}", vid, pid);
                    let usb_handle = dev.open().unwrap();
                    let mut handle = RtlSdrDeviceHandle::new(usb_handle, self.iface_id);
                    // let kernel_driver_attached = handle.detach_kernel_driver();

                    let _iface = handle.claim_interface();

                    handle.test_write();
                    // reset device if write didn't succeed

                    handle.init_baseband();
                    handle.set_i2c_repeater(true);

                    for tuner_info in KNOWN_TUNERS.iter() {
                        match handle.i2c_read_reg(tuner_info.i2c_addr, tuner_info.check_addr) {
                            Ok(val) => {
                                if val == tuner_info.check_val {
                                    println!("Found {} tuner\n", tuner_info.name);
                                    found = true;
                                    break;
                                } else {
                                    println!(
                                        "Mismatch in  {} tuner expected {} found {}\n",
                                        tuner_info.name, tuner_info.check_val, val
                                    );
                                }
                            }
                            Err(_) => {}
                        };
                    }

                    // {
                    //     let d = r820t::R820T::new(&handle);
                    //     d.init();
                    // }

                    // handle.deinit_baseband();

                    // if kernel_driver_attached {
                    //     handle.attach_kernel_driver();
                    // }
                } else {
                    println!("No match for vid {} and pid {}", vid, pid);
                }
            }
        }
        found
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_init() {
        let mut rtlsdr = RtlSdr::new();
        let found = rtlsdr.open();
        assert_eq!(found, true);
    }
}