summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDario Nieuwenhuis <dirbaio@dirbaio.net>2021-11-21 20:49:42 +0100
committerGitHub <noreply@github.com>2021-11-21 20:49:42 +0100
commite8ee3a7cdc28eac8ce57a811ac01c75ccadbd839 (patch)
treed9d010d9510a789480bf936ee76a2ac87db9ea00
parentab4d4f9de306e8d3040e2b5d6e05709d72891721 (diff)
parent1d9e9ff82e713c2ecf6eb75eafc5a898692695ed (diff)
downloadnrf-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.rs34
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()
+ }
+}