Skip to content

Commit

Permalink
Add paths to types in server and service macros (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
petekubiak authored Jan 20, 2025
1 parent 90756d3 commit 0d179c7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 28 deletions.
2 changes: 1 addition & 1 deletion host-macros/src/characteristic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub fn parse_uuid(meta: &ParseNestedMeta<'_>) -> Result<TokenStream> {
let span = expr.span(); // span will highlight if the value does not impl Into<Uuid>
Ok(quote::quote_spanned! { span =>
{
let uuid: Uuid = #expr.into();
let uuid: trouble_host::types::uuid::Uuid = #expr.into();
uuid
}
})
Expand Down
28 changes: 14 additions & 14 deletions host-macros/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,19 @@ impl ServerBuilder {
let attribute_table_size = if let Some(value) = self.arguments.attribute_table_size {
value
} else {
parse_quote!(GAP_SERVICE_ATTRIBUTE_COUNT #code_attribute_summation)
parse_quote!(trouble_host::gap::GAP_SERVICE_ATTRIBUTE_COUNT #code_attribute_summation)
};

quote! {
const _ATTRIBUTE_TABLE_SIZE: usize = #attribute_table_size;
// This pattern causes the assertion to happen at compile time
const _: () = {
core::assert!(_ATTRIBUTE_TABLE_SIZE >= GAP_SERVICE_ATTRIBUTE_COUNT #code_attribute_summation, "Specified attribute table size is insufficient. Please increase attribute_table_size or remove the argument entirely to allow automatic sizing of the attribute table.");
core::assert!(_ATTRIBUTE_TABLE_SIZE >= trouble_host::gap::GAP_SERVICE_ATTRIBUTE_COUNT #code_attribute_summation, "Specified attribute table size is insufficient. Please increase attribute_table_size or remove the argument entirely to allow automatic sizing of the attribute table.");
};

#visibility struct #name<'values>
{
server: AttributeServer<'values, #mutex_type, _ATTRIBUTE_TABLE_SIZE>,
server: trouble_host::prelude::AttributeServer<'values, #mutex_type, _ATTRIBUTE_TABLE_SIZE>,
#code_service_definition
}

Expand All @@ -122,12 +122,12 @@ impl ServerBuilder {
/// Create a new Gatt Server instance.
///
/// Requires you to add your own GAP Service. Use `new_default(name)` or `new_with_config(name, gap_config)` if you want to add a GAP Service.
#visibility fn new(mut table: AttributeTable<'values, #mutex_type, _ATTRIBUTE_TABLE_SIZE>) -> Self {
#visibility fn new(mut table: trouble_host::attribute::AttributeTable<'values, #mutex_type, _ATTRIBUTE_TABLE_SIZE>) -> Self {

#code_service_init

Self {
server: AttributeServer::new(table),
server: trouble_host::prelude::AttributeServer::new(table),
#code_server_populate
}
}
Expand All @@ -137,14 +137,14 @@ impl ServerBuilder {
/// The maximum length which the name can be is 22 bytes (limited by the size of the advertising packet).
/// If a name longer than this is passed, Err() is returned.
#visibility fn new_default(name: &'values str) -> Result<Self, &'static str> {
let mut table: AttributeTable<'_, #mutex_type, _ATTRIBUTE_TABLE_SIZE> = AttributeTable::new();
let mut table: trouble_host::attribute::AttributeTable<'_, #mutex_type, _ATTRIBUTE_TABLE_SIZE> = trouble_host::attribute::AttributeTable::new();

GapConfig::default(name).build(&mut table)?;
trouble_host::gap::GapConfig::default(name).build(&mut table)?;

#code_service_init

Ok(Self {
server: AttributeServer::new(table),
server: trouble_host::prelude::AttributeServer::new(table),
#code_server_populate
})
}
Expand All @@ -154,31 +154,31 @@ impl ServerBuilder {
/// This function will add a GAP Service.
/// The maximum length which the device name can be is 22 bytes (limited by the size of the advertising packet).
/// If a name longer than this is passed, Err() is returned.
#visibility fn new_with_config(gap: GapConfig<'values>) -> Result<Self, &'static str> {
let mut table: AttributeTable<'_, #mutex_type, _ATTRIBUTE_TABLE_SIZE> = AttributeTable::new();
#visibility fn new_with_config(gap: trouble_host::gap::GapConfig<'values>) -> Result<Self, &'static str> {
let mut table: trouble_host::attribute::AttributeTable<'_, #mutex_type, _ATTRIBUTE_TABLE_SIZE> = trouble_host::attribute::AttributeTable::new();

gap.build(&mut table)?;

#code_service_init

Ok(Self {
server: AttributeServer::new(table),
server: trouble_host::prelude::AttributeServer::new(table),
#code_server_populate
})
}

#visibility fn get<T: trouble_host::types::gatt_traits::GattValue>(&self, characteristic: &Characteristic<T>) -> Result<T, Error> {
#visibility fn get<T: trouble_host::types::gatt_traits::GattValue>(&self, characteristic: &trouble_host::attribute::Characteristic<T>) -> Result<T, trouble_host::Error> {
self.server.table().get(characteristic)
}

#visibility fn set<T: trouble_host::types::gatt_traits::GattValue>(&self, characteristic: &Characteristic<T>, input: &T) -> Result<(), Error> {
#visibility fn set<T: trouble_host::types::gatt_traits::GattValue>(&self, characteristic: &trouble_host::attribute::Characteristic<T>, input: &T) -> Result<(), trouble_host::Error> {
self.server.table().set(characteristic, input)
}
}

impl<'values> core::ops::Deref for #name<'values>
{
type Target = AttributeServer<'values, #mutex_type, _ATTRIBUTE_TABLE_SIZE>;
type Target = trouble_host::prelude::AttributeServer<'values, #mutex_type, _ATTRIBUTE_TABLE_SIZE>;

fn deref(&self) -> &Self::Target {
&self.server
Expand Down
42 changes: 29 additions & 13 deletions host-macros/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn parse_arg_uuid(value: &Expr) -> Result<TokenStream2> {
let span = other.span(); // span will highlight if the value does not impl Into<Uuid>
Ok(quote::quote_spanned! { span =>
{
let uuid: Uuid = #other.into();
let uuid: trouble_host::types::uuid::Uuid = #other.into();
uuid
}
})
Expand Down Expand Up @@ -153,18 +153,18 @@ impl ServiceBuilder {
quote! {
#visibility struct #struct_name {
#fields
handle: AttributeHandle,
handle: trouble_host::attribute::AttributeHandle,
}

#[allow(unused)]
impl #struct_name {
#visibility const ATTRIBUTE_COUNT: usize = #attribute_count;

#visibility fn new<M, const MAX_ATTRIBUTES: usize>(table: &mut AttributeTable<'_, M, MAX_ATTRIBUTES>) -> Self
#visibility fn new<M, const MAX_ATTRIBUTES: usize>(table: &mut trouble_host::attribute::AttributeTable<'_, M, MAX_ATTRIBUTES>) -> Self
where
M: embassy_sync::blocking_mutex::raw::RawMutex,
{
let mut service = table.add_service(Service::new(#uuid));
let mut service = table.add_service(trouble_host::attribute::Service::new(#uuid));
#code_build_chars

Self {
Expand Down 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 GattValue>::MAX_SIZE]> = static_cell::StaticCell::new();
static #name_screaming: static_cell::StaticCell<[u8; <#ty as trouble_host::types::gatt_traits::GattValue>::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 GattValue>::MAX_SIZE]);
let store = #name_screaming.init([0; <#ty as trouble_host::types::gatt_traits::GattValue>::MAX_SIZE]);
let mut builder = service
.add_characteristic(#uuid, &[#(#properties),*], val, store);
#descriptors
Expand Down Expand Up @@ -235,7 +235,7 @@ impl ServiceBuilder {
// add fields for each characteristic value handle
fields.push(syn::Field {
ident: Some(char_name.clone()),
ty: syn::Type::Verbatim(quote!(Characteristic<#ty>)),
ty: syn::Type::Verbatim(quote!(trouble_host::attribute::Characteristic<#ty>)),
attrs: Vec::new(),
colon_token: Default::default(),
vis: ch.vis.clone(),
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 = GattValue::to_gatt(&value);
let value = trouble_host::types::gatt_traits::GattValue::to_gatt(&value);
store[..value.len()].copy_from_slice(value);
builder.add_descriptor(
#uuid,
Expand All @@ -323,14 +323,30 @@ fn parse_property_into_list(property: bool, variant: TokenStream2, properties: &
/// Parse the properties of a characteristic and return a list of properties
fn set_access_properties(args: &AccessArgs) -> Vec<TokenStream2> {
let mut properties = Vec::new();
parse_property_into_list(args.read, quote! {CharacteristicProp::Read}, &mut properties);
parse_property_into_list(args.write, quote! {CharacteristicProp::Write}, &mut properties);
parse_property_into_list(
args.read,
quote! {trouble_host::attribute::CharacteristicProp::Read},
&mut properties,
);
parse_property_into_list(
args.write,
quote! {trouble_host::attribute::CharacteristicProp::Write},
&mut properties,
);
parse_property_into_list(
args.write_without_response,
quote! {CharacteristicProp::WriteWithoutResponse},
quote! {trouble_host::attribute::CharacteristicProp::WriteWithoutResponse},
&mut properties,
);
parse_property_into_list(
args.notify,
quote! {trouble_host::attribute::CharacteristicProp::Notify},
&mut properties,
);
parse_property_into_list(
args.indicate,
quote! {trouble_host::attribute::CharacteristicProp::Indicate},
&mut properties,
);
parse_property_into_list(args.notify, quote! {CharacteristicProp::Notify}, &mut properties);
parse_property_into_list(args.indicate, quote! {CharacteristicProp::Indicate}, &mut properties);
properties
}

0 comments on commit 0d179c7

Please sign in to comment.