Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow getting and setting all attributes #261

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
c94e4ec
Implement generic attribute handle for get and set to accept attribut…
petekubiak Jan 13, 2025
8371390
Merge remote-tracking branch 'origin/main' into allow-getting-and-set…
petekubiak Jan 14, 2025
22e1883
Remove AttributeHandle type
petekubiak Jan 14, 2025
f2e1139
Rename AttrHandle to AttributeHandle
petekubiak Jan 14, 2025
a525211
Fix handle type issue in service macro
petekubiak Jan 14, 2025
66e188d
Change server's get and set methods to accept a generic AttributeHandle
petekubiak Jan 14, 2025
1afb38e
Consolidate UUID from slice functionality into TryFrom implementation
petekubiak Jan 14, 2025
ee81ea2
Merge branch 'replace-uuid-from-slice-with-try-from' into allow-getti…
petekubiak Jan 14, 2025
d55ac4d
Implement GattValue on Uuid type
petekubiak Jan 14, 2025
2793596
Implement Attribute handle in service builder macro
petekubiak Jan 14, 2025
a315b77
Revert "Implement Attribute handle in service builder macro"
petekubiak Jan 20, 2025
1f39f4a
Allow getting and setting characteristic CCCD properties
petekubiak Jan 20, 2025
6870ac4
Swap mention of CCCD for characteristic properties in new attribute h…
petekubiak Jan 20, 2025
1066949
Add name as an argument for descriptor attribute
petekubiak Jan 23, 2025
1ab567d
Implement Primitive (and hence GattValue) for &[u8]
petekubiak Jan 23, 2025
57f8a5d
Implement AttributeHandle for Descriptor
petekubiak Jan 23, 2025
a2d3479
Add named descriptor handles to service to make them accessible
petekubiak Jan 23, 2025
7a05271
Merge remote-tracking branch 'origin/main' into allow-getting-and-set…
petekubiak Jan 23, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Consolidate UUID from slice functionality into TryFrom implementation
petekubiak committed Jan 14, 2025
commit 1afb38e89e00d92d7ab1b0a4bd515e112c9861d2
2 changes: 1 addition & 1 deletion host/src/attribute.rs
Original file line number Diff line number Diff line change
@@ -253,7 +253,7 @@ impl AttributeData<'_> {
Ok(Self::Declaration {
props: CharacteristicProps(r.read()?),
handle: r.read()?,
uuid: Uuid::from_slice(r.remaining()),
uuid: Uuid::try_from(r.remaining())?,
})
}
}
33 changes: 12 additions & 21 deletions host/src/types/uuid.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! UUID types.

use bt_hci::uuid::BluetoothUuid16;

use crate::codec::{Decode, Encode, Error, Type};
@@ -30,20 +31,6 @@ impl Uuid {
Self::Uuid128(val)
}

/// Create a UUID from a slice, either 2 or 16 bytes long.
pub fn from_slice(val: &[u8]) -> Self {
if val.len() == 2 {
Self::Uuid16([val[0], val[1]])
} else if val.len() == 16 {
Self::Uuid128([
val[0], val[1], val[2], val[3], val[4], val[5], val[6], val[7], val[8], val[9], val[10], val[11],
val[12], val[13], val[14], val[15],
])
} else {
panic!("unexpected input");
}
}

/// Copy the UUID bytes into a slice.
pub fn bytes(&self, data: &mut [u8]) {
match self {
@@ -90,15 +77,19 @@ impl From<u16> for Uuid {
}
}

impl From<&[u8]> for Uuid {
fn from(data: &[u8]) -> Self {
match data.len() {
2 => Uuid::Uuid16(data.try_into().unwrap()),
impl TryFrom<&[u8]> for Uuid {
type Error = crate::Error;

fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
match value.len() {
// Slice length has already been verified, so unwrap can be used
2 => Ok(Uuid::Uuid16(value.try_into().unwrap())),
16 => {
let bytes: [u8; 16] = data.try_into().unwrap();
Uuid::Uuid128(bytes)
let mut bytes = [0; 16];
bytes.copy_from_slice(value);
Ok(Uuid::Uuid128(bytes))
}
_ => panic!(),
_ => Err(crate::Error::InvalidValue),
}
}
}