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
mod args;
pub use args::Args;

mod device;
pub use device::Device;
pub use device::DeviceTrait;
pub use device::GenericDevice;

pub mod impls;

mod range;
pub use range::Range;
pub use range::RangeItem;

mod streamer;
pub use streamer::RxStreamer;
pub use streamer::TxStreamer;

use serde::{Deserialize, Serialize};

use std::str::FromStr;
use thiserror::Error;

/// Seify Error
#[derive(Debug, Error)]
pub enum Error {
    #[error("DeviceError")]
    DeviceError,
    #[error("Value ({1}) out of range ({0:?})")]
    OutOfRange(Range, f64),
    #[error("Value Error")]
    ValueError,
    #[error("Not Found")]
    NotFound,
    #[error("corresponding feature not enabled")]
    FeatureNotEnabled,
    #[error("Not Supported")]
    NotSupported,
    #[error("Overflow")]
    Overflow,
    #[error("Inactive")]
    Inactive,
    #[error("Json")]
    Json(#[from] serde_json::Error),
    #[error("Misc")]
    Misc(String),
    #[error("Io")]
    Io(#[from] std::io::Error),
    #[cfg(all(feature = "soapy", not(target_arch = "wasm32")))]
    #[error("Soapy")]
    Soapy(soapysdr::Error),
    #[cfg(all(feature = "aaronia_http", not(target_arch = "wasm32")))]
    #[error("Ureq")]
    Ureq(Box<ureq::Error>),
    #[cfg(all(feature = "rtlsdr", not(target_arch = "wasm32")))]
    #[error("RtlSdr")]
    RtlSdr(#[from] seify_rtlsdr::error::RtlsdrError),
}

#[cfg(all(feature = "aaronia_http", not(target_arch = "wasm32")))]
impl From<ureq::Error> for Error {
    fn from(value: ureq::Error) -> Self {
        Error::Ureq(Box::new(value))
    }
}

/// Supported hardware drivers.
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum Driver {
    Aaronia,
    AaroniaHttp,
    RtlSdr,
    Soapy,
}

impl FromStr for Driver {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let s = s.to_lowercase();
        if s == "aaronia" {
            return Ok(Driver::Aaronia);
        }
        if s == "aaronia_http" || s == "aaronia-http" || s == "aaroniahttp" {
            return Ok(Driver::AaroniaHttp);
        }
        if s == "rtlsdr" || s == "rtl-sdr" || s == "rtl" {
            return Ok(Driver::RtlSdr);
        }
        if s == "soapy" || s == "soapysdr" {
            return Ok(Driver::Soapy);
        }
        Err(Error::ValueError)
    }
}

/// Direction (Rx/TX)
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum Direction {
    Rx,
    Tx,
}

/// Enumerate devices.
///
/// ## Returns
///
/// A vector or [`Args`] that provide information about the device and can be used to identify it
/// uniquely, i.e., passing the [`Args`] to [`Device::from_args`](crate::Device::from_args) will
/// open this particular device.
pub fn enumerate() -> Result<Vec<Args>, Error> {
    enumerate_with_args(Args::new())
}

/// Enumerate devices with given [`Args`].
///
/// ## Returns
///
/// A vector or [`Args`] that provide information about the device and can be used to identify it
/// uniquely, i.e., passing the [`Args`] to [`Device::from_args`](crate::Device::from_args) will
/// open this particular device.
pub fn enumerate_with_args<A: TryInto<Args>>(a: A) -> Result<Vec<Args>, Error> {
    let args: Args = a.try_into().or(Err(Error::ValueError))?;
    let mut devs = Vec::new();
    let driver = match args.get::<String>("driver") {
        Ok(s) => Some(s.parse::<Driver>()?),
        Err(_) => None,
    };

    #[cfg(all(feature = "aaronia", any(target_os = "linux", target_os = "windows")))]
    {
        if driver.is_none() || matches!(driver, Some(Driver::Aaronia)) {
            devs.append(&mut impls::Aaronia::probe(&args)?)
        }
    }
    #[cfg(not(all(feature = "aaronia", any(target_os = "linux", target_os = "windows"))))]
    {
        if matches!(driver, Some(Driver::Aaronia)) {
            return Err(Error::FeatureNotEnabled);
        }
    }

    #[cfg(all(feature = "aaronia_http", not(target_arch = "wasm32")))]
    {
        if driver.is_none() || matches!(driver, Some(Driver::AaroniaHttp)) {
            devs.append(&mut impls::AaroniaHttp::probe(&args)?)
        }
    }
    #[cfg(not(all(feature = "aaronia_http", not(target_arch = "wasm32"))))]
    {
        if matches!(driver, Some(Driver::AaroniaHttp)) {
            return Err(Error::FeatureNotEnabled);
        }
    }

    #[cfg(all(feature = "rtlsdr", not(target_arch = "wasm32")))]
    {
        if driver.is_none() || matches!(driver, Some(Driver::RtlSdr)) {
            devs.append(&mut impls::RtlSdr::probe(&args)?)
        }
    }
    #[cfg(not(all(feature = "rtlsdr", not(target_arch = "wasm32"))))]
    {
        if matches!(driver, Some(Driver::RtlSdr)) {
            return Err(Error::FeatureNotEnabled);
        }
    }

    #[cfg(all(feature = "soapy", not(target_arch = "wasm32")))]
    {
        if driver.is_none() || matches!(driver, Some(Driver::Soapy)) {
            devs.append(&mut impls::Soapy::probe(&args)?)
        }
    }
    #[cfg(not(all(feature = "soapy", not(target_arch = "wasm32"))))]
    {
        if matches!(driver, Some(Driver::Soapy)) {
            return Err(Error::FeatureNotEnabled);
        }
    }

    let _ = &mut devs;
    Ok(devs)
}