Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions src/bladerf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,11 @@ pub trait BladeRF: Sized + Drop {
/// TODO: Get this moved over as a method to the streamer structs once we add the ability to do metadata
///
/// Related `libbladerf` docs: <https://www.nuand.com/libbladeRF-doc/v2.5.0/group___f_n___s_c_h_e_d_u_l_e_d___t_u_n_i_n_g.html#gad7bd11c5784e78af7ae8fab26f4605fa>
fn schedule_retune(
///
/// # Safety
/// At the moment, the conversion from the [QuickTune] struct to the [bladerf_quick_tune] only handles one of the two cases it could be.
/// This neeed to be fixed. Look at the source code and the bladerf docs as if you had to use the C ABI directly.
unsafe fn schedule_retune(
&self,
channel: Channel,
time: u64,
Expand Down Expand Up @@ -679,7 +683,10 @@ pub trait BladeRF: Sized + Drop {
/// </div>
///
/// Related `libbladerf` docs: <https://www.nuand.com/libbladeRF-doc/v2.5.0/group___f_n___s_c_h_e_d_u_l_e_d___t_u_n_i_n_g.html#ga5cb5018f2acc2b25e2690e96439a029c>
fn get_quick_tune(&self, channel: Channel) -> Result<QuickTune> {
/// # Safety
/// At the moment, the conversion from the [QuickTune] struct to the [bladerf_quick_tune] only handles one of the two cases it could be.
/// This neeed to be fixed. Look at the source code and the bladerf docs as if you had to use the C ABI directly.
unsafe fn get_quick_tune(&self, channel: Channel) -> Result<QuickTune> {
let mut quick_tune = QuickTune {
freqsel: 0,
vcocap: 0,
Expand Down Expand Up @@ -1063,15 +1070,9 @@ pub trait BladeRF: Sized + Drop {
///
/// Related `libbladerf` docs: <https://www.nuand.com/libbladeRF-doc/v2.5.0/group___f_n___t_r_i_g.html#ga14afff57873c8ae591a4142d7851a869>
unsafe fn trigger_arm(&self, trigger: &Trigger, arm: bool) -> Result<()> {
let res = unsafe {
bladerf_trigger_arm(
self.get_device_ptr(),
trigger as *const Trigger as *const bladerf_trigger,
arm,
0,
0,
)
};
let brf_trigger = trigger.into();
Comment thread
QuantumEF marked this conversation as resolved.

let res = unsafe { bladerf_trigger_arm(self.get_device_ptr(), &brf_trigger, arm, 0, 0) };
check_res!(res);
Ok(())
}
Expand All @@ -1088,12 +1089,9 @@ pub trait BladeRF: Sized + Drop {
///
/// Related `libbladerf` docs: <https://www.nuand.com/libbladeRF-doc/v2.5.0/group___f_n___t_r_i_g.html#gaaa2b932a3b810203952bb49c1673c124>
unsafe fn trigger_fire(&self, trigger: &Trigger) -> Result<()> {
let res = unsafe {
bladerf_trigger_fire(
self.get_device_ptr(),
trigger as *const Trigger as *const bladerf_trigger,
)
};
let brf_trigger = trigger.into();

let res = unsafe { bladerf_trigger_fire(self.get_device_ptr(), &brf_trigger) };
check_res!(res);
Ok(())
}
Expand All @@ -1112,10 +1110,13 @@ pub trait BladeRF: Sized + Drop {
let mut fire_requested = false;
let mut resv1 = 0u64;
let mut resv2 = 0u64;

let brf_trigger = trigger.into();

let res = unsafe {
bladerf_trigger_state(
self.get_device_ptr(),
trigger as *const Trigger as *const bladerf_trigger,
&brf_trigger,
&mut is_armed,
&mut has_fired,
&mut fire_requested,
Expand Down
46 changes: 46 additions & 0 deletions src/types/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,49 @@ impl From<Backend> for bladerf_backend {
value as i32 as bladerf_backend
}
}

#[cfg(test)]
mod test {
use libbladerf_sys::bladerf_backend;

use crate::Backend;

#[test]
/// Precautionary test since the From<Backend> impl has 2 casts as I believe the [bladerf_backend] type is not consistent across platforms.
fn backend_enum_conversion_test() {
Comment on lines +45 to +46

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate on this? What kind of failure mode are you trying to guard against?

The variants in the header file have ordinals 0..=100 so we shouldnt have any issues with representing that in our #[repr(i32)] enum Backend { ... } (even if enum bladerf_backend is sized differently on different platforms)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put this in primarily as a sanity check if there are future code changes. I don't think it should be needed, but because of the double as cast, I was thinking about going through and adding some sort of brute force tests to anything that is repr(C). I did not really put a lot of though into this specific item, it was more of an addition so I would not have to think

let bk = Backend::Any;
let bk_int: i64 = (bk as i32).into();
assert_eq!(
bk_int,
<bladerf_backend as std::convert::Into<i64>>::into(bk.into())
);

let bk = Backend::LibUsb;
let bk_int: i64 = (bk as i32).into();
assert_eq!(
bk_int,
<bladerf_backend as std::convert::Into<i64>>::into(bk.into())
);

let bk = Backend::Linux;
let bk_int: i64 = (bk as i32).into();
assert_eq!(
bk_int,
<bladerf_backend as std::convert::Into<i64>>::into(bk.into())
);

let bk = Backend::Cypress;
let bk_int: i64 = (bk as i32).into();
assert_eq!(
bk_int,
<bladerf_backend as std::convert::Into<i64>>::into(bk.into())
);

let bk = Backend::Dummy;
let bk_int: i64 = (bk as i32).into();
assert_eq!(
bk_int,
<bladerf_backend as std::convert::Into<i64>>::into(bk.into())
);
}
}
12 changes: 12 additions & 0 deletions src/types/trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ impl TryFrom<bladerf_trigger_signal> for TriggerSignal {
/// Trigger configuration
///
/// Related `libbladerf` docs: <https://www.nuand.com/libbladeRF-doc/v2.5.0/structbladerf__trigger.html>
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Trigger {
/// The channel associated with this trigger
pub channel: Channel,
Expand All @@ -86,6 +87,17 @@ pub struct Trigger {
pub options: u64,
}

impl From<&Trigger> for bladerf_trigger {
fn from(value: &Trigger) -> Self {
bladerf_trigger {
channel: value.channel as bladerf_channel,
role: value.role as bladerf_trigger_role,
signal: value.signal as bladerf_trigger_signal,
options: value.options,
}
}
}

impl TryFrom<bladerf_trigger> for Trigger {
type Error = Error;

Expand Down
Loading