Skip to content

Commit

Permalink
dp: Report optional master address as an Option
Browse files Browse the repository at this point in the history
When a peripheral does not have a master it is tied to, then it reports
the master address 255.  Don't forward this implementation detail to the
user - instead report an untied peripheral with master address `None`.
  • Loading branch information
Rahix committed Nov 14, 2024
1 parent 6fe0709 commit 5572997
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
3 changes: 1 addition & 2 deletions examples/dp-scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ fn main() -> ! {
.slot_bits(2500)
// For generating the live-list as fast as possible, set GAP factor to 1.
.gap_wait_rotations(1)
.max_retry_limit(3)
.build(),
);
// We must not poll() too often or to little. T_slot / 2 seems to be a good compromise.
Expand All @@ -45,7 +44,7 @@ fn main() -> ! {
Some(dp::scan::DpScanEvent::PeripheralFound(desc)) => {
log::info!("Discovered peripheral #{}:", desc.address);
log::info!(" - Ident: 0x{:04x}", desc.ident);
log::info!(" - Master: {:?}", desc.tied_to_master);
log::info!(" - Master: {:?}", desc.master_address);
}
Some(dp::scan::DpScanEvent::PeripheralLost(address)) => {
log::info!("Lost peripheral #{}.", address);
Expand Down
12 changes: 9 additions & 3 deletions src/dp/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub struct PeripheralDiagnostics<'a> {
/// This ident number must match the one passed in [`PeripheralOptions`].
pub ident_number: u16,
/// Address of the DP master this peripheral is locked to (if any)
pub master_address: u8,
pub master_address: Option<u8>,
/// Extended diagnostics blocks
pub extended_diagnostics: &'a crate::dp::ExtendedDiagnostics<'a>,
}
Expand All @@ -105,7 +105,7 @@ pub struct PeripheralDiagnostics<'a> {
pub(crate) struct DiagnosticsInfo {
pub flags: DiagnosticFlags,
pub ident_number: u16,
pub master_address: u8,
pub master_address: Option<u8>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
Expand Down Expand Up @@ -640,11 +640,17 @@ impl<'a> Peripheral<'a> {
return None;
}

let master_address = if t.pdu[3] == 255 {
None
} else {
Some(t.pdu[3])
};

let mut diag = DiagnosticsInfo {
flags: DiagnosticFlags::from_bits_retain(u16::from_le_bytes(
t.pdu[0..2].try_into().unwrap(),
)),
master_address: t.pdu[3],
master_address,
ident_number: u16::from_be_bytes(t.pdu[4..6].try_into().unwrap()),
};

Expand Down
13 changes: 9 additions & 4 deletions src/dp/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pub struct DpPeripheralDescription {
pub address: crate::Address,
pub ident: u16,
pub tied_to_master: Option<crate::Address>,
pub master_address: Option<crate::Address>,
}

#[derive(Debug, PartialEq, Eq, Clone)]
Expand Down Expand Up @@ -46,11 +46,17 @@ impl DpScanner {
return None;
}

let master_address = if t.pdu[3] == 255 {
None
} else {
Some(t.pdu[3])
};

let mut diag = crate::dp::DiagnosticsInfo {
flags: crate::dp::DiagnosticFlags::from_bits_retain(u16::from_le_bytes(
t.pdu[0..2].try_into().unwrap(),
)),
master_address: t.pdu[3],
master_address,
ident_number: u16::from_be_bytes(t.pdu[4..6].try_into().unwrap()),
};

Expand Down Expand Up @@ -128,8 +134,7 @@ impl crate::fdl::FdlApplication for DpScanner {
let desc = DpPeripheralDescription {
address,
ident: diag.ident_number,
// TODO: Not always Some()
tied_to_master: Some(diag.master_address),
master_address: diag.master_address,
};

if station_unknown {
Expand Down

0 comments on commit 5572997

Please sign in to comment.