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

Restore the GattData::process public API #311

Merged
merged 1 commit into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions host-macros/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,11 @@ impl ServerBuilder {
})
}

#visibility fn get<T: trouble_host::attribute::AttributeHandle<Value = V>, V: FromGatt>(&self, attribute_handle: &T) -> Result<T::Value, Error> {
#visibility fn get<T: trouble_host::attribute::AttributeHandle<Value = V>, V: FromGatt>(&self, attribute_handle: &T) -> Result<T::Value, trouble_host::Error> {
self.server.table().get(attribute_handle)
}

#visibility fn set<T: trouble_host::attribute::AttributeHandle>(&self, attribute_handle: &T, input: &T::Value) -> Result<(), Error> {
#visibility fn set<T: trouble_host::attribute::AttributeHandle>(&self, attribute_handle: &T, input: &T::Value) -> Result<(), trouble_host::Error> {
self.server.table().set(attribute_handle, input)

}
Expand Down
91 changes: 49 additions & 42 deletions host/src/gatt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl<'stack, 'server> GattConnection<'stack, 'server> {
loop {
match self.connection.next().await {
ConnectionEvent::Disconnected { reason } => return GattConnectionEvent::Disconnected { reason },
ConnectionEvent::Gatt { data } => match self.process(data).await {
ConnectionEvent::Gatt { data } => match data.process(self.server).await {
Ok(event) => match event {
Some(event) => return GattConnectionEvent::Gatt { event: Ok(event) },
None => continue,
Expand All @@ -82,47 +82,6 @@ impl<'stack, 'server> GattConnection<'stack, 'server> {
}
}

async fn process(&self, mut data: GattData<'_>) -> Result<Option<GattEvent<'stack, 'server>>, Error> {
let att = data.incoming();
match att {
AttClient::Request(AttReq::Write { handle, data: _ }) => Ok(Some(GattEvent::Write(WriteEvent {
value_handle: handle,
pdu: data.pdu.take(),
connection: self.connection.clone(),
server: self.server,
}))),

AttClient::Command(AttCmd::Write { handle, data: _ }) => Ok(Some(GattEvent::Write(WriteEvent {
value_handle: handle,
pdu: data.pdu.take(),
connection: self.connection.clone(),
server: self.server,
}))),

AttClient::Request(AttReq::Read { handle }) => Ok(Some(GattEvent::Read(ReadEvent {
value_handle: handle,
pdu: data.pdu.take(),
connection: self.connection.clone(),
server: self.server,
}))),

AttClient::Request(AttReq::ReadBlob { handle, offset }) => Ok(Some(GattEvent::Read(ReadEvent {
value_handle: handle,
pdu: data.pdu.take(),
connection: self.connection.clone(),
server: self.server,
}))),
_ => {
// Process it now since the user will not
if let Some(pdu) = data.pdu.as_ref() {
let reply = process_accept(pdu, &self.connection, self.server)?;
reply.send().await;
}
Ok(None)
}
}
}

/// Get a reference to the underlying BLE connection.
pub fn raw(&self) -> &Connection<'stack> {
&self.connection
Expand Down Expand Up @@ -176,6 +135,54 @@ impl<'stack> GattData<'stack> {
connection.send(pdu).await;
Ok(())
}

/// Handle the GATT data.
///
/// May return an event that should be replied/processed. Uses the provided
/// attribute server to handle the protocol.
pub async fn process<'m>(
mut self,
server: &'m dyn DynamicAttributeServer,
) -> Result<Option<GattEvent<'stack, 'm>>, Error> {
let att = self.incoming();
match att {
AttClient::Request(AttReq::Write { handle, data: _ }) => Ok(Some(GattEvent::Write(WriteEvent {
value_handle: handle,
pdu: self.pdu.take(),
connection: self.connection.clone(),
server,
}))),

AttClient::Command(AttCmd::Write { handle, data: _ }) => Ok(Some(GattEvent::Write(WriteEvent {
value_handle: handle,
pdu: self.pdu.take(),
connection: self.connection.clone(),
server,
}))),

AttClient::Request(AttReq::Read { handle }) => Ok(Some(GattEvent::Read(ReadEvent {
value_handle: handle,
pdu: self.pdu.take(),
connection: self.connection.clone(),
server,
}))),

AttClient::Request(AttReq::ReadBlob { handle, offset }) => Ok(Some(GattEvent::Read(ReadEvent {
value_handle: handle,
pdu: self.pdu.take(),
connection: self.connection.clone(),
server,
}))),
_ => {
// Process it now since the user will not
if let Some(pdu) = self.pdu.as_ref() {
let reply = process_accept(pdu, &self.connection, server)?;
reply.send().await;
}
Ok(None)
}
}
}
}

/// An event returned while processing GATT requests.
Expand Down