#[repr(u8)]
#[allow(dead_code)]
pub(crate) enum Request {
SetTransceiverMode = 1,
Max2837Write = 2,
Max2837Read = 3,
Si5351CWrite = 4,
Si5351CRead = 5,
SampleRateSet = 6,
BasebandFilterBandwidthSet = 7,
Rffc5071Write = 8,
Rffc5071Read = 9,
SpiflashErase = 10,
SpiflashWrite = 11,
SpiflashRead = 12,
BoardIdRead = 14,
VersionStringRead = 15,
SetFreq = 16,
AmpEnable = 17,
BoardPartidSerialnoRead = 18,
SetLnaGain = 19,
SetVgaGain = 20,
SetTxvgaGain = 21,
AntennaEnable = 23,
SetFreqExplicit = 24,
UsbWcidVendorReq = 25,
InitSweep = 26,
OperacakeGetBoards = 27,
OperacakeSetPorts = 28,
SetHwSyncMode = 29,
Reset = 30,
OperacakeSetRanges = 31,
ClkoutEnable = 32,
SpiflashStatus = 33,
SpiflashClearStatus = 34,
OperacakeGpioTest = 35,
CpldChecksum = 36,
UiEnable = 37,
}
#[derive(Copy, Clone, Debug, PartialEq)]
#[repr(u16)]
pub enum Mode {
Off = 0,
Receive = 1,
Transmit = 2,
}
#[derive(Debug)]
pub struct Config {
pub vga_db: u16,
pub txvga_db: u16,
pub lna_db: u16,
pub amp_enable: bool,
pub antenna_enable: bool,
pub frequency_hz: u64,
pub sample_rate_hz: u32,
pub sample_rate_div: u32,
}
impl Config {
pub fn tx_default() -> Self {
Self {
vga_db: 0,
lna_db: 0,
txvga_db: 40,
amp_enable: false,
antenna_enable: false,
frequency_hz: 908_000_000,
sample_rate_hz: 2_500_000,
sample_rate_div: 1,
}
}
pub fn rx_default() -> Self {
Self {
vga_db: 24,
lna_db: 0,
txvga_db: 0,
amp_enable: false,
antenna_enable: false,
frequency_hz: 908_000_000,
sample_rate_hz: 2_500_000,
sample_rate_div: 1,
}
}
}
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("io")]
Io(#[from] std::io::Error),
#[error("transfer")]
Transfer(#[from] nusb::transfer::TransferError),
#[error("transfer truncated: got {actual} bytes, expected {expected}")]
TransferTruncated {
actual: usize,
expected: usize,
},
#[error("no api: device version {device}, minimum required {min}")]
NoApi {
device: UsbVersion,
min: UsbVersion,
},
#[error("{0}")]
Argument(&'static str),
#[error("HackRF in invalid mode. Required: {required:?}, actual: {actual:?}")]
WrongMode {
required: Mode,
actual: Mode,
},
#[error("Device not found")]
NotFound,
#[error("Streamer instance already created")]
StreamerExists,
}
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, PartialOrd, Ord)]
pub struct UsbVersion(pub u8, pub u8, pub u8);
impl UsbVersion {
pub fn from_bcd(mut raw: u16) -> Self {
let sub_minor: u8 = (raw & 0x000F) as u8;
raw >>= 4;
let minor: u8 = (raw & 0x000F) as u8;
raw >>= 4;
let mut major: u8 = (raw & 0x000F) as u8;
raw >>= 4;
major += (10 * raw) as u8;
UsbVersion(major, minor, sub_minor)
}
pub fn major(self) -> u8 {
let UsbVersion(major, _, _) = self;
major
}
pub fn minor(self) -> u8 {
let UsbVersion(_, minor, _) = self;
minor
}
pub fn sub_minor(self) -> u8 {
let UsbVersion(_, _, sub_minor) = self;
sub_minor
}
}
impl std::fmt::Display for UsbVersion {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}.{}.{}", self.major(), self.minor(), self.sub_minor())
}
}