You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was attempting to send a string as a characteristic with the following code:
#[gatt_service(uuid = DEVICE_INFO_SERVICE_UUID)]structDeviceInfoService{#[characteristic(uuid = "00002a25-0000-1000-8000-00805f9b34fb", value = "1337", read)]serial_number:&'staticstr,}
I could see the characteristic but it had an odd nonsense value that was not valid utf8
Looking at the code for the impl<T: Primitive> FixedGattValue for T it seems that SIZE is set as mem::size_of::<Self>(); and in the case of T = &str this wont actually be the size of the str slice, just the size of the reference.
Having realized this I switched to trying to use heapless::String (as shown below) and that worked as expected.
#[gatt_service(uuid = DEVICE_INFO_SERVICE_UUID)]structDeviceInfoService{#[characteristic(uuid = "00002a25-0000-1000-8000-00805f9b34fb", value = heapless::String::from_str("1337").unwrap(), read)]serial_number: heapless::String<8>,}
The text was updated successfully, but these errors were encountered:
For completeness here is the current code that seems to be flawed for &str
traitPrimitive:Copy{}implPrimitiveforu8{}implPrimitiveforu16{}implPrimitiveforu32{}implPrimitiveforu64{}implPrimitivefori8{}implPrimitivefori16{}implPrimitivefori32{}implPrimitivefori64{}implPrimitiveforf32{}implPrimitiveforf64{}implPrimitiveforBluetoothUuid16{}// ok as this is just a NewType(u16)implPrimitivefor&'_str{}impl<T:Primitive>FixedGattValueforT{constSIZE:usize = mem::size_of::<Self>();fnfrom_gatt(data:&[u8]) -> Result<Self,FromGattError>{if data.len() != Self::SIZE{Err(FromGattError::InvalidLength)}else{// SAFETY// - Pointer is considered "valid" as per the rules outlined for validity in std::ptr v1.82.0// - Pointer was generated from a slice of bytes matching the size of the type implementing Primitive, and all types implementing Primitive are valid for all possible configurations of bits// - Primitive trait is constrained to require Copyunsafe{Ok((data.as_ptr()as*constSelf).read_unaligned())}}}fnto_gatt(&self) -> &[u8]{// SAFETY// - Slice is of type u8 so data is guaranteed valid for reads of any length// - Data and len are tied to the address and size of the typeunsafe{ slice::from_raw_parts(selfas*constSelfas*constu8,Self::SIZE)}}}
I was attempting to send a string as a characteristic with the following code:
I could see the characteristic but it had an odd nonsense value that was not valid utf8
Looking at the code for the
impl<T: Primitive> FixedGattValue for T
it seems thatSIZE
is set asmem::size_of::<Self>();
and in the case ofT = &str
this wont actually be the size of the str slice, just the size of the reference.Having realized this I switched to trying to use
heapless::String
(as shown below) and that worked as expected.The text was updated successfully, but these errors were encountered: