From 0d179c7499cdd39a9c3951d65e8932ebae0ea87d Mon Sep 17 00:00:00 2001 From: petekubiak <37507920+petekubiak@users.noreply.github.com> Date: Mon, 20 Jan 2025 13:02:43 +0000 Subject: [PATCH] Add paths to types in server and service macros (#254) --- host-macros/src/characteristic.rs | 2 +- host-macros/src/server.rs | 28 ++++++++++----------- host-macros/src/service.rs | 42 +++++++++++++++++++++---------- 3 files changed, 44 insertions(+), 28 deletions(-) diff --git a/host-macros/src/characteristic.rs b/host-macros/src/characteristic.rs index 05200fd3..68c37448 100644 --- a/host-macros/src/characteristic.rs +++ b/host-macros/src/characteristic.rs @@ -106,7 +106,7 @@ pub fn parse_uuid(meta: &ParseNestedMeta<'_>) -> Result { let span = expr.span(); // span will highlight if the value does not impl Into Ok(quote::quote_spanned! { span => { - let uuid: Uuid = #expr.into(); + let uuid: trouble_host::types::uuid::Uuid = #expr.into(); uuid } }) diff --git a/host-macros/src/server.rs b/host-macros/src/server.rs index c0f7b121..bb33d9ca 100644 --- a/host-macros/src/server.rs +++ b/host-macros/src/server.rs @@ -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 } @@ -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 } } @@ -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 { - 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 }) } @@ -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 { - let mut table: AttributeTable<'_, #mutex_type, _ATTRIBUTE_TABLE_SIZE> = AttributeTable::new(); + #visibility fn new_with_config(gap: trouble_host::gap::GapConfig<'values>) -> Result { + 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(&self, characteristic: &Characteristic) -> Result { + #visibility fn get(&self, characteristic: &trouble_host::attribute::Characteristic) -> Result { self.server.table().get(characteristic) } - #visibility fn set(&self, characteristic: &Characteristic, input: &T) -> Result<(), Error> { + #visibility fn set(&self, characteristic: &trouble_host::attribute::Characteristic, 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 diff --git a/host-macros/src/service.rs b/host-macros/src/service.rs index 13e5ca7d..d9bc6e65 100644 --- a/host-macros/src/service.rs +++ b/host-macros/src/service.rs @@ -51,7 +51,7 @@ fn parse_arg_uuid(value: &Expr) -> Result { let span = other.span(); // span will highlight if the value does not impl Into Ok(quote::quote_spanned! { span => { - let uuid: Uuid = #other.into(); + let uuid: trouble_host::types::uuid::Uuid = #other.into(); uuid } }) @@ -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(table: &mut AttributeTable<'_, M, MAX_ATTRIBUTES>) -> Self + #visibility fn new(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 { @@ -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 @@ -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(), @@ -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, @@ -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 { 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 }