Skip to content

Commit 6550409

Browse files
HaoboGululf
authored andcommitted
feat(att): support set cccd table
Signed-off-by: Haobo Gu <[email protected]>
1 parent 85e31dc commit 6550409

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

host-macros/src/server.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,12 @@ impl ServerBuilder {
206206
self.server.table().set(attribute_handle, input)
207207
}
208208

209-
#visibility fn cccd_tables(&self, connection: &trouble_host::connection::Connection) -> Option<trouble_host::prelude::CccdTable<_CCCD_TABLE_SIZE>> {
210-
self.server.cccd_tables(connection)
209+
#visibility fn get_cccd_table(&self, connection: &trouble_host::connection::Connection) -> Option<trouble_host::prelude::CccdTable<_CCCD_TABLE_SIZE>> {
210+
self.server.get_cccd_table(connection)
211+
}
212+
213+
#visibility fn set_cccd_table(&self, connection: &trouble_host::connection::Connection, table: trouble_host::prelude::CccdTable<_CCCD_TABLE_SIZE>) {
214+
self.server.set_cccd_table(connection, table);
211215
}
212216
}
213217

host/src/attribute.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ pub enum CCCDFlag {
874874

875875
/// CCCD flag.
876876
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
877-
#[derive(Clone, Copy, Default)]
877+
#[derive(Clone, Copy, Default, Debug, PartialEq)]
878878
pub struct CCCD(pub(crate) u16);
879879

880880
impl<const T: usize> From<[CCCDFlag; T]> for CCCD {
@@ -887,6 +887,12 @@ impl<const T: usize> From<[CCCDFlag; T]> for CCCD {
887887
}
888888
}
889889

890+
impl From<u16> for CCCD {
891+
fn from(value: u16) -> Self {
892+
CCCD(value)
893+
}
894+
}
895+
890896
impl CCCD {
891897
/// Get raw value
892898
pub fn raw(&self) -> u16 {

host/src/attribute_server.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct Client {
1919

2020
/// A table of CCCD values.
2121
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
22-
#[derive(Clone)]
22+
#[derive(Clone, Debug)]
2323
pub struct CccdTable<const ENTRIES: usize> {
2424
inner: [(u16, CCCD); ENTRIES],
2525
}
@@ -33,6 +33,16 @@ impl<const ENTRIES: usize> Default for CccdTable<ENTRIES> {
3333
}
3434

3535
impl<const ENTRIES: usize> CccdTable<ENTRIES> {
36+
/// Create a new CCCD table from an array of (handle, cccd) pairs.
37+
pub fn new(cccd_values: [(u16, CCCD); ENTRIES]) -> Self {
38+
Self { inner: cccd_values }
39+
}
40+
41+
/// Get the inner array of (handle, cccd) pairs.
42+
pub fn inner(&self) -> &[(u16, CCCD); ENTRIES] {
43+
&self.inner
44+
}
45+
3646
fn add_handle(&mut self, cccd_handle: u16) {
3747
for (handle, _) in self.inner.iter_mut() {
3848
if *handle == 0 {
@@ -198,6 +208,19 @@ impl<M: RawMutex, const CCCD_MAX: usize, const CONN_MAX: usize> CccdTables<M, CC
198208
None
199209
})
200210
}
211+
212+
fn set_cccd_table(&self, peer_address: &BdAddr, table: CccdTable<CCCD_MAX>) {
213+
self.state.lock(|n| {
214+
let mut n = n.borrow_mut();
215+
for (client, t) in n.iter_mut() {
216+
if client.address == *peer_address {
217+
trace!("Setting cccd table {:?} for {:?}", table, peer_address);
218+
*t = table;
219+
break;
220+
}
221+
}
222+
})
223+
}
201224
}
202225

203226
/// A GATT server capable of processing the GATT protocol using the provided table of attributes.
@@ -700,8 +723,13 @@ impl<'values, M: RawMutex, const ATT_MAX: usize, const CCCD_MAX: usize, const CO
700723
&self.att_table
701724
}
702725

703-
/// Get a reference to the CCCD tables
704-
pub fn cccd_tables(&self, connection: &Connection) -> Option<CccdTable<CCCD_MAX>> {
726+
/// Get the CCCD table for a connection
727+
pub fn get_cccd_table(&self, connection: &Connection) -> Option<CccdTable<CCCD_MAX>> {
705728
self.cccd_tables.get_cccd_table(&connection.peer_address())
706729
}
730+
731+
/// Set the CCCD table for a connection
732+
pub fn set_cccd_table(&self, connection: &Connection, table: CccdTable<CCCD_MAX>) {
733+
self.cccd_tables.set_cccd_table(&connection.peer_address(), table);
734+
}
707735
}

0 commit comments

Comments
 (0)