Skip to content

Commit

Permalink
Merge pull request #187 from embassy-rs/fix-178
Browse files Browse the repository at this point in the history
Make example panic if advertise fails
  • Loading branch information
lulf authored Nov 29, 2024
2 parents b470ede + 69781b9 commit 11dc195
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
13 changes: 5 additions & 8 deletions examples/apps/src/ble_bas_peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ where
// then return to advertising state.
select(connection_task, counter_task).await;
}
Err(_) => info!("[adv] error"),
Err(e) => {
#[cfg(feature = "defmt")]
let e = defmt::Debug2Format(&e);
panic!("[adv] error: {:?}", e);
}
}
}
};
Expand Down Expand Up @@ -142,13 +146,6 @@ async fn advertise<'a, C: Controller>(
name: &'a str,
peripheral: &mut Peripheral<'a, C>,
) -> Result<Connection<'a>, BleHostError<C::Error>> {
let name = if name.len() > 22 {
let truncated_name = &name[..22];
info!("Name truncated to {}", truncated_name);
truncated_name
} else {
name
};
let mut advertiser_data = [0; 31];
AdStructure::encode_slice(
&[
Expand Down
19 changes: 19 additions & 0 deletions host/src/advertise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,3 +502,22 @@ impl<'d> Iterator for AdStructureIter<'d> {
Some(self.read())
}
}

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

#[test]
fn adv_name_truncate() {
let mut adv_data = [0; 31];
assert!(AdStructure::encode_slice(
&[
AdStructure::Flags(LE_GENERAL_DISCOVERABLE | BR_EDR_NOT_SUPPORTED),
AdStructure::ServiceUuids16(&[Uuid::Uuid16([0x0f, 0x18])]),
AdStructure::CompleteLocalName(b"12345678901234567890123"),
],
&mut adv_data[..],
)
.is_err());
}
}

0 comments on commit 11dc195

Please sign in to comment.