btle/le/advertisement_structures/
tx_power_level.rs1use crate::le::advertisement::{
2 AdStructureType, AdType, ConstAdStructType, UnpackableAdStructType,
3};
4use crate::PackError;
5use core::fmt::Formatter;
6
7#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Default, Debug, Hash)]
8pub struct TxPowerLevel {
9 pub dbm: i8,
10}
11impl core::fmt::Display for TxPowerLevel {
12 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
13 write!(f, "{}dbm", self.dbm)
14 }
15}
16impl TxPowerLevel {
17 pub const AD_TYPE: AdType = AdType::TxPowerLevel;
18
19 pub const BYTE_LEN: usize = 1;
20 pub fn new(dbm: i8) -> TxPowerLevel {
21 TxPowerLevel { dbm }
22 }
23}
24
25impl AdStructureType for TxPowerLevel {
26 fn ad_type(&self) -> AdType {
27 Self::AD_TYPE
28 }
29
30 fn byte_len(&self) -> usize {
31 Self::BYTE_LEN
32 }
33
34 fn pack_into(&self, buf: &mut [u8]) -> Result<(), PackError> {
35 PackError::expect_length(Self::BYTE_LEN, buf)?;
36 buf[0] = self.dbm as u8;
37 Ok(())
38 }
39}
40impl UnpackableAdStructType for TxPowerLevel {
41 fn unpack_from(ad_type: AdType, buf: &[u8]) -> Result<Self, PackError>
42 where
43 Self: Sized,
44 {
45 if ad_type != Self::AD_TYPE {
46 Err(PackError::InvalidFields)
47 } else {
48 PackError::expect_length(Self::BYTE_LEN, buf)?;
49 Ok(Self::new(buf[0] as i8))
50 }
51 }
52}
53impl ConstAdStructType for TxPowerLevel {
54 const AD_TYPE: AdType = AdType::TxPowerLevel;
55}