diff options
author | Dario Nieuwenhuis <dirbaio@dirbaio.net> | 2021-11-21 20:49:42 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-21 20:49:42 +0100 |
commit | e8ee3a7cdc28eac8ce57a811ac01c75ccadbd839 (patch) | |
tree | d9d010d9510a789480bf936ee76a2ac87db9ea00 | |
parent | ab4d4f9de306e8d3040e2b5d6e05709d72891721 (diff) | |
parent | 1d9e9ff82e713c2ecf6eb75eafc5a898692695ed (diff) | |
download | nrf-softdevice-e8ee3a7cdc28eac8ce57a811ac01c75ccadbd839.zip |
Merge pull request #87 from jacobrosenthal/gatt_traits
gatt traits: add const array and heapless string
-rw-r--r-- | nrf-softdevice/src/ble/gatt_traits.rs | 34 |
1 files changed, 33 insertions, 1 deletions
diff --git a/nrf-softdevice/src/ble/gatt_traits.rs b/nrf-softdevice/src/ble/gatt_traits.rs index 9fc7475..1916687 100644 --- a/nrf-softdevice/src/ble/gatt_traits.rs +++ b/nrf-softdevice/src/ble/gatt_traits.rs @@ -1,9 +1,13 @@ +use core::convert::TryInto; use core::mem; use core::slice; -use heapless::Vec; +use heapless::{String, Vec}; +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum FromGattError { InvalidLength, + InvalidCharacter, } pub trait FixedGattValue: Sized { @@ -83,3 +87,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() + } +} |