|  | // Copyright 2023 Google LLC | 
|  | // Use of this source code is governed by a BSD-style license that can be | 
|  | // found in the LICENSE file. | 
|  |  | 
|  | use crate::packet_encoding::{Encodable, Error as PacketError}; | 
|  |  | 
|  | /// Represents the Advertising Set ID which is 1 byte long. | 
|  | /// Follows little endian encoding. | 
|  | #[derive(Debug, Clone, Copy, PartialEq)] | 
|  | pub struct AdvertisingSetId(pub u8); | 
|  |  | 
|  | impl AdvertisingSetId { | 
|  | // Byte size if this is to be encoded. | 
|  | pub const BYTE_SIZE: usize = 1; | 
|  | } | 
|  |  | 
|  | /// Represents the SyncInfo Interval value which is 2 bytes long. | 
|  | /// Follows little endian encoding. | 
|  | #[derive(Debug, Clone, Copy, PartialEq)] | 
|  | pub struct PaInterval(pub u16); | 
|  |  | 
|  | impl PaInterval { | 
|  | pub const BYTE_SIZE: usize = 2; | 
|  | pub const UNKNOWN_VALUE: u16 = 0xFFFF; | 
|  |  | 
|  | pub const fn unknown() -> Self { | 
|  | Self(Self::UNKNOWN_VALUE) | 
|  | } | 
|  | } | 
|  |  | 
|  | impl Encodable for PaInterval { | 
|  | type Error = PacketError; | 
|  |  | 
|  | /// Encodees the PaInterval to 2 byte value using little endian encoding. | 
|  | fn encode(&self, buf: &mut [u8]) -> core::result::Result<(), Self::Error> { | 
|  | if buf.len() < Self::BYTE_SIZE { | 
|  | return Err(PacketError::BufferTooSmall); | 
|  | } | 
|  | buf[0..Self::BYTE_SIZE].copy_from_slice(&self.0.to_le_bytes()); | 
|  | Ok(()) | 
|  | } | 
|  |  | 
|  | fn encoded_len(&self) -> core::primitive::usize { | 
|  | Self::BYTE_SIZE | 
|  | } | 
|  | } | 
|  |  | 
|  | #[cfg(test)] | 
|  | mod tests { | 
|  | use super::*; | 
|  |  | 
|  | #[test] | 
|  | fn encode_pa_interval() { | 
|  | let mut buf = [0; PaInterval::BYTE_SIZE]; | 
|  | let interval = PaInterval(0x1004); | 
|  |  | 
|  | interval.encode(&mut buf[..]).expect("should succeed"); | 
|  | assert_eq!(buf, [0x04, 0x10]); | 
|  | } | 
|  |  | 
|  | #[test] | 
|  | fn encode_pa_interval_fails() { | 
|  | let mut buf = [0; 1];  // Not enough buffer space. | 
|  | let interval = PaInterval(0x1004); | 
|  |  | 
|  | interval.encode(&mut buf[..]).expect_err("should fail"); | 
|  | } | 
|  | } |