diff --git a/host-macros/src/server.rs b/host-macros/src/server.rs index f5698404..0cf8058c 100644 --- a/host-macros/src/server.rs +++ b/host-macros/src/server.rs @@ -171,7 +171,7 @@ impl ServerBuilder { self.server.table().get(characteristic) } - #visibility fn set(&self, characteristic: &trouble_host::attribute::Characteristic, input: &T) -> Result<(), trouble_host::Error> { + #visibility fn set(&self, characteristic: &trouble_host::attribute::Characteristic, input: &T) -> Result<(), trouble_host::Error> { self.server.table().set(characteristic, input) } } diff --git a/host-macros/src/service.rs b/host-macros/src/service.rs index c57695fa..16821653 100644 --- a/host-macros/src/service.rs +++ b/host-macros/src/service.rs @@ -193,10 +193,10 @@ impl ServiceBuilder { self.code_build_chars.extend(quote_spanned! {characteristic.span=> let #char_name = { - static #name_screaming: static_cell::StaticCell<[u8; <#ty as trouble_host::types::gatt_traits::ToGatt>::MAX_SIZE]> = static_cell::StaticCell::new(); + static #name_screaming: static_cell::StaticCell<[u8; <#ty as trouble_host::types::gatt_traits::AsGatt>::MAX_SIZE]> = static_cell::StaticCell::new(); let mut val = <#ty>::default(); // constrain the type of the value here val = #default_value; // update the temporary value with our new default - let store = #name_screaming.init([0; <#ty as trouble_host::types::gatt_traits::ToGatt>::MAX_SIZE]); + let store = #name_screaming.init([0; <#ty as trouble_host::types::gatt_traits::AsGatt>::MAX_SIZE]); let mut builder = service .add_characteristic(#uuid, &[#(#properties),*], val, store); #descriptors @@ -300,7 +300,7 @@ impl ServiceBuilder { const CAPACITY: u8 = if (#capacity) < 16 { 16 } else { #capacity }; // minimum capacity is 16 bytes static #name_screaming: static_cell::StaticCell<[u8; CAPACITY as usize]> = static_cell::StaticCell::new(); let store = #name_screaming.init([0; CAPACITY as usize]); - let value = trouble_host::types::gatt_traits::ToGatt::to_gatt(&value); + let value = trouble_host::types::gatt_traits::AsGatt::as_gatt(&value); store[..value.len()].copy_from_slice(value); builder.add_descriptor( #uuid, diff --git a/host/src/attribute.rs b/host/src/attribute.rs index e96ca56d..57549ae5 100644 --- a/host/src/attribute.rs +++ b/host/src/attribute.rs @@ -12,7 +12,7 @@ use crate::att::AttErrorCode; use crate::attribute_server::AttributeServer; use crate::cursor::{ReadCursor, WriteCursor}; use crate::prelude::Connection; -use crate::types::gatt_traits::{FromGatt, ToGatt}; +use crate::types::gatt_traits::{AsGatt, FromGatt}; pub use crate::types::uuid::Uuid; use crate::Error; use heapless::Vec; @@ -400,8 +400,8 @@ impl<'d, M: RawMutex, const MAX: usize> AttributeTable<'d, M, MAX> { /// /// If the characteristic for the handle cannot be found, or the shape of the data does not match the type of the characterstic, /// an error is returned - pub fn set(&self, characteristic: &Characteristic, input: &T) -> Result<(), Error> { - let gatt_value = input.to_gatt(); + pub fn set(&self, characteristic: &Characteristic, input: &T) -> Result<(), Error> { + let gatt_value = input.as_gatt(); self.set_raw(characteristic.handle, gatt_value) } @@ -434,7 +434,7 @@ impl<'d, M: RawMutex, const MAX: usize> AttributeTable<'d, M, MAX> { /// Return the characteristic which corresponds to the supplied value handle /// /// If no characteristic corresponding to the given value handle was found, returns an error - pub fn find_characteristic_by_value_handle(&self, handle: u16) -> Result, Error> { + pub fn find_characteristic_by_value_handle(&self, handle: u16) -> Result, Error> { self.iterate(|mut it| { while let Some(att) = it.next() { if att.handle == handle { @@ -492,7 +492,7 @@ pub struct ServiceBuilder<'r, 'd, M: RawMutex, const MAX: usize> { } impl<'d, M: RawMutex, const MAX: usize> ServiceBuilder<'_, 'd, M, MAX> { - fn add_characteristic_internal( + fn add_characteristic_internal( &mut self, uuid: Uuid, props: CharacteristicProps, @@ -547,7 +547,7 @@ impl<'d, M: RawMutex, const MAX: usize> ServiceBuilder<'_, 'd, M, MAX> { } /// Add a characteristic to this service with a refererence to a mutable storage buffer. - pub fn add_characteristic>( + pub fn add_characteristic>( &mut self, uuid: U, props: &[CharacteristicProp], @@ -555,7 +555,7 @@ impl<'d, M: RawMutex, const MAX: usize> ServiceBuilder<'_, 'd, M, MAX> { store: &'d mut [u8], ) -> CharacteristicBuilder<'_, 'd, T, M, MAX> { let props = props.into(); - let bytes = value.to_gatt(); + let bytes = value.as_gatt(); store[..bytes.len()].copy_from_slice(bytes); let variable_len = T::MAX_SIZE != T::MIN_SIZE; let len = bytes.len() as u16; @@ -572,7 +572,7 @@ impl<'d, M: RawMutex, const MAX: usize> ServiceBuilder<'_, 'd, M, MAX> { } /// Add a characteristic to this service with a refererence to an immutable storage buffer. - pub fn add_characteristic_ro>( + pub fn add_characteristic_ro>( &mut self, uuid: U, value: &'d T, @@ -583,7 +583,7 @@ impl<'d, M: RawMutex, const MAX: usize> ServiceBuilder<'_, 'd, M, MAX> { props, AttributeData::ReadOnlyData { props, - value: value.to_gatt(), + value: value.as_gatt(), }, ) } @@ -611,7 +611,7 @@ impl Drop for ServiceBuilder<'_, '_, M, MAX> { /// A characteristic in the attribute table. #[cfg_attr(feature = "defmt", derive(defmt::Format))] #[derive(Clone, Copy, Debug, PartialEq)] -pub struct Characteristic { +pub struct Characteristic { /// Handle value assigned to the Client Characteristic Configuration Descriptor (if any) pub cccd_handle: Option, /// Handle value assigned to this characteristic when it is added to the Gatt Attribute Table @@ -631,7 +631,7 @@ impl Characteristic { connection: &Connection<'_>, value: &T, ) -> Result<(), Error> { - let value = value.to_gatt(); + let value = value.as_gatt(); server.table().set_raw(self.handle, value)?; let cccd_handle = self.cccd_handle.ok_or(Error::NotFound)?; @@ -663,7 +663,7 @@ impl Characteristic { server: &AttributeServer<'_, M, MAX>, value: &T, ) -> Result<(), Error> { - let value = value.to_gatt(); + let value = value.as_gatt(); server.table().set_raw(self.handle, value)?; Ok(()) } @@ -678,12 +678,12 @@ impl Characteristic { } /// Builder for characteristics. -pub struct CharacteristicBuilder<'r, 'd, T: ToGatt, M: RawMutex, const MAX: usize> { +pub struct CharacteristicBuilder<'r, 'd, T: AsGatt, M: RawMutex, const MAX: usize> { handle: Characteristic, table: &'r mut AttributeTable<'d, M, MAX>, } -impl<'d, T: ToGatt, M: RawMutex, const MAX: usize> CharacteristicBuilder<'_, 'd, T, M, MAX> { +impl<'d, T: AsGatt, M: RawMutex, const MAX: usize> CharacteristicBuilder<'_, 'd, T, M, MAX> { fn add_descriptor_internal( &mut self, uuid: Uuid, diff --git a/host/src/gatt.rs b/host/src/gatt.rs index e1a8cc32..133a2252 100644 --- a/host/src/gatt.rs +++ b/host/src/gatt.rs @@ -20,7 +20,7 @@ use crate::attribute_server::{AttributeServer, DynamicAttributeServer}; use crate::connection::Connection; use crate::cursor::{ReadCursor, WriteCursor}; use crate::pdu::Pdu; -use crate::types::gatt_traits::{FromGatt, FromGattError, ToGatt}; +use crate::types::gatt_traits::{AsGatt, FromGatt, FromGattError}; use crate::types::l2cap::L2capHeader; use crate::{config, BleHostError, Error, Stack}; @@ -516,7 +516,7 @@ impl<'reference, C: Controller, const MAX_SERVICES: usize, const L2CAP_MTU: usiz } /// Discover characteristics in a given service using a UUID. - pub async fn characteristic_by_uuid( + pub async fn characteristic_by_uuid( &self, service: &ServiceHandle, uuid: &Uuid, @@ -603,7 +603,7 @@ impl<'reference, C: Controller, const MAX_SERVICES: usize, const L2CAP_MTU: usiz /// Read a characteristic described by a handle. /// /// The number of bytes copied into the provided buffer is returned. - pub async fn read_characteristic( + pub async fn read_characteristic( &self, characteristic: &Characteristic, dest: &mut [u8], @@ -679,7 +679,7 @@ impl<'reference, C: Controller, const MAX_SERVICES: usize, const L2CAP_MTU: usiz /// Subscribe to indication/notification of a given Characteristic /// /// A listener is returned, which has a `next()` method - pub async fn subscribe( + pub async fn subscribe( &self, characteristic: &Characteristic, indication: bool, @@ -711,7 +711,7 @@ impl<'reference, C: Controller, const MAX_SERVICES: usize, const L2CAP_MTU: usiz } /// Unsubscribe from a given Characteristic - pub async fn unsubscribe( + pub async fn unsubscribe( &self, characteristic: &Characteristic, ) -> Result<(), BleHostError> { diff --git a/host/src/lib.rs b/host/src/lib.rs index d6c6bb7e..bc367341 100644 --- a/host/src/lib.rs +++ b/host/src/lib.rs @@ -97,7 +97,7 @@ pub mod prelude { #[cfg(feature = "scan")] pub use crate::scan::*; #[cfg(feature = "gatt")] - pub use crate::types::gatt_traits::{FixedGattValue, FromGatt, ToGatt}; + pub use crate::types::gatt_traits::{AsGatt, FixedGattValue, FromGatt}; pub use crate::Address; } diff --git a/host/src/types/gatt_traits.rs b/host/src/types/gatt_traits.rs index 51c90622..5d8c2ece 100644 --- a/host/src/types/gatt_traits.rs +++ b/host/src/types/gatt_traits.rs @@ -29,20 +29,20 @@ pub trait FixedGattValue: FromGatt { } /// Trait to allow conversion of a type to gatt bytes -pub trait ToGatt: Sized { +pub trait AsGatt: Sized { /// The minimum size the type might be const MIN_SIZE: usize; /// The maximum size the type might be const MAX_SIZE: usize; /// Converts to gatt bytes. /// Must return a slice of len in MIN_SIZE..=MAX_SIZE - fn to_gatt(&self) -> &[u8]; + fn as_gatt(&self) -> &[u8]; } /// Trait to allow conversion of gatt bytes into a type /// -/// Requires that the type implements ToGatt -pub trait FromGatt: ToGatt { +/// Requires that the type implements AsGatt +pub trait FromGatt: AsGatt { /// Converts from gatt bytes. /// Must return FromGattError::InvalidLength if data.len not in MIN_SIZE..=MAX_SIZE fn from_gatt(data: &[u8]) -> Result; @@ -54,11 +54,11 @@ impl FromGatt for T { } } -impl ToGatt for T { +impl AsGatt for T { const MIN_SIZE: usize = Self::SIZE; const MAX_SIZE: usize = Self::SIZE; - fn to_gatt(&self) -> &[u8] { + fn as_gatt(&self) -> &[u8] { ::to_gatt(self) } } @@ -124,11 +124,11 @@ impl FromGatt for Vec { } } -impl ToGatt for Vec { +impl AsGatt for Vec { const MIN_SIZE: usize = 0; const MAX_SIZE: usize = N; - fn to_gatt(&self) -> &[u8] { + fn as_gatt(&self) -> &[u8] { self } } @@ -145,11 +145,11 @@ impl FromGatt for [u8; N] { } } -impl ToGatt for [u8; N] { +impl AsGatt for [u8; N] { const MIN_SIZE: usize = 0; const MAX_SIZE: usize = N; - fn to_gatt(&self) -> &[u8] { + fn as_gatt(&self) -> &[u8] { self.as_slice() } } @@ -161,20 +161,20 @@ impl FromGatt for String { } } -impl ToGatt for String { +impl AsGatt for String { const MIN_SIZE: usize = 0; const MAX_SIZE: usize = N; - fn to_gatt(&self) -> &[u8] { + fn as_gatt(&self) -> &[u8] { self.as_ref() } } -impl ToGatt for &'static str { +impl AsGatt for &'static str { const MIN_SIZE: usize = 0; const MAX_SIZE: usize = usize::MAX; - fn to_gatt(&self) -> &[u8] { + fn as_gatt(&self) -> &[u8] { self.as_bytes() } }