diff options
author | Jacob Rosenthal <jacobrosenthal@gmail.com> | 2021-11-20 22:03:37 -0700 |
---|---|---|
committer | Jacob Rosenthal <jacobrosenthal@gmail.com> | 2021-11-20 22:03:37 -0700 |
commit | 15a212d23ee1a9985f2d4f69f0b4f5f6c23559fc (patch) | |
tree | 14ae56902acde232d8cbb4cf8d5161c729e12f40 /nrf-softdevice | |
parent | ab4d4f9de306e8d3040e2b5d6e05709d72891721 (diff) | |
download | nrf-softdevice-15a212d23ee1a9985f2d4f69f0b4f5f6c23559fc.zip |
gatt traits: add const array and heapless string
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() + } +} |