From 15a212d23ee1a9985f2d4f69f0b4f5f6c23559fc Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Sat, 20 Nov 2021 22:03:37 -0700 Subject: gatt traits: add const array and heapless string --- nrf-softdevice/src/ble/gatt_traits.rs | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) (limited to 'nrf-softdevice') 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 GattValue for Vec { &self } } + +impl 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 GattValue for String { + 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() + } +} -- cgit v1.2.3