diff options
Diffstat (limited to 'nrf-softdevice')
-rw-r--r-- | nrf-softdevice/src/ble/gatt_traits.rs | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/nrf-softdevice/src/ble/gatt_traits.rs b/nrf-softdevice/src/ble/gatt_traits.rs index 9fc7475..4b52a92 100644 --- a/nrf-softdevice/src/ble/gatt_traits.rs +++ b/nrf-softdevice/src/ble/gatt_traits.rs @@ -1,9 +1,12 @@ +use core::convert::TryInto; use core::mem; use core::slice; -use heapless::Vec; +use heapless::{String, Vec}; +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum FromGattError { InvalidLength, + InvalidCharacter, } pub trait FixedGattValue: Sized { @@ -83,3 +86,31 @@ impl<const N: usize> GattValue for Vec<u8, N> { &self } } + +impl<const N: usize> GattValue for [u8; N] { + const MIN_SIZE: usize = 0; + const MAX_SIZE: usize = N; + + fn from_gatt(data: &[u8]) -> Self { + unwrap!(data.try_into()) + } + + fn to_gatt(&self) -> &[u8] { + self.as_slice() + } +} + +impl<const N: usize> GattValue for String<N> { + const MIN_SIZE: usize = 0; + const MAX_SIZE: usize = N; + + fn from_gatt(data: &[u8]) -> Self { + String::from(unwrap!( + core::str::from_utf8(data).map_err(|_| FromGattError::InvalidCharacter) + )) + } + + fn to_gatt(&self) -> &[u8] { + self.as_ref() + } +} |