Skip to content

Commit

Permalink
Use AsGatt rather than ToGatt (#290)
Browse files Browse the repository at this point in the history
* Use AsGatt rather than ToGatt
  • Loading branch information
petekubiak authored Feb 21, 2025
1 parent 23cb851 commit c1f002b
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 38 deletions.
2 changes: 1 addition & 1 deletion host-macros/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl ServerBuilder {
self.server.table().get(characteristic)
}

#visibility fn set<T: trouble_host::types::gatt_traits::ToGatt>(&self, characteristic: &trouble_host::attribute::Characteristic<T>, input: &T) -> Result<(), trouble_host::Error> {
#visibility fn set<T: trouble_host::types::gatt_traits::AsGatt>(&self, characteristic: &trouble_host::attribute::Characteristic<T>, input: &T) -> Result<(), trouble_host::Error> {
self.server.table().set(characteristic, input)
}
}
Expand Down
6 changes: 3 additions & 3 deletions host-macros/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
28 changes: 14 additions & 14 deletions host/src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<T: ToGatt>(&self, characteristic: &Characteristic<T>, input: &T) -> Result<(), Error> {
let gatt_value = input.to_gatt();
pub fn set<T: AsGatt>(&self, characteristic: &Characteristic<T>, input: &T) -> Result<(), Error> {
let gatt_value = input.as_gatt();
self.set_raw(characteristic.handle, gatt_value)
}

Expand Down Expand Up @@ -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<T: ToGatt>(&self, handle: u16) -> Result<Characteristic<T>, Error> {
pub fn find_characteristic_by_value_handle<T: AsGatt>(&self, handle: u16) -> Result<Characteristic<T>, Error> {
self.iterate(|mut it| {
while let Some(att) = it.next() {
if att.handle == handle {
Expand Down Expand Up @@ -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<T: ToGatt>(
fn add_characteristic_internal<T: AsGatt>(
&mut self,
uuid: Uuid,
props: CharacteristicProps,
Expand Down Expand Up @@ -547,15 +547,15 @@ 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<T: ToGatt, U: Into<Uuid>>(
pub fn add_characteristic<T: AsGatt, U: Into<Uuid>>(
&mut self,
uuid: U,
props: &[CharacteristicProp],
value: T,
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;
Expand All @@ -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<T: ToGatt, U: Into<Uuid>>(
pub fn add_characteristic_ro<T: AsGatt, U: Into<Uuid>>(
&mut self,
uuid: U,
value: &'d T,
Expand All @@ -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(),
},
)
}
Expand Down Expand Up @@ -611,7 +611,7 @@ impl<M: RawMutex, const MAX: usize> 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<T: ToGatt> {
pub struct Characteristic<T: AsGatt> {
/// Handle value assigned to the Client Characteristic Configuration Descriptor (if any)
pub cccd_handle: Option<u16>,
/// Handle value assigned to this characteristic when it is added to the Gatt Attribute Table
Expand All @@ -631,7 +631,7 @@ impl<T: FromGatt> Characteristic<T> {
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)?;
Expand Down Expand Up @@ -663,7 +663,7 @@ impl<T: FromGatt> Characteristic<T> {
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(())
}
Expand All @@ -678,12 +678,12 @@ impl<T: FromGatt> Characteristic<T> {
}

/// 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<T>,
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,
Expand Down
10 changes: 5 additions & 5 deletions host/src/gatt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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<T: ToGatt>(
pub async fn characteristic_by_uuid<T: AsGatt>(
&self,
service: &ServiceHandle,
uuid: &Uuid,
Expand Down Expand Up @@ -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<T: ToGatt>(
pub async fn read_characteristic<T: AsGatt>(
&self,
characteristic: &Characteristic<T>,
dest: &mut [u8],
Expand Down Expand Up @@ -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<T: ToGatt>(
pub async fn subscribe<T: AsGatt>(
&self,
characteristic: &Characteristic<T>,
indication: bool,
Expand Down Expand Up @@ -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<T: ToGatt>(
pub async fn unsubscribe<T: AsGatt>(
&self,
characteristic: &Characteristic<T>,
) -> Result<(), BleHostError<C::Error>> {
Expand Down
2 changes: 1 addition & 1 deletion host/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
28 changes: 14 additions & 14 deletions host/src/types/gatt_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, FromGattError>;
Expand All @@ -54,11 +54,11 @@ impl<T: FixedGattValue> FromGatt for T {
}
}

impl<T: FixedGattValue> ToGatt for T {
impl<T: FixedGattValue> 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] {
<Self as FixedGattValue>::to_gatt(self)
}
}
Expand Down Expand Up @@ -124,11 +124,11 @@ impl<const N: usize> FromGatt for Vec<u8, N> {
}
}

impl<const N: usize> ToGatt for Vec<u8, N> {
impl<const N: usize> AsGatt for Vec<u8, N> {
const MIN_SIZE: usize = 0;
const MAX_SIZE: usize = N;

fn to_gatt(&self) -> &[u8] {
fn as_gatt(&self) -> &[u8] {
self
}
}
Expand All @@ -145,11 +145,11 @@ impl<const N: usize> FromGatt for [u8; N] {
}
}

impl<const N: usize> ToGatt for [u8; N] {
impl<const N: usize> 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()
}
}
Expand All @@ -161,20 +161,20 @@ impl<const N: usize> FromGatt for String<N> {
}
}

impl<const N: usize> ToGatt for String<N> {
impl<const N: usize> AsGatt for String<N> {
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()
}
}

0 comments on commit c1f002b

Please sign in to comment.