From 19a68aa9aea4f03596092e3a6b55477add2b6a2e Mon Sep 17 00:00:00 2001 From: ivmarkov Date: Thu, 6 Mar 2025 09:27:31 +0000 Subject: [PATCH] Restore the GattData::process public API --- host-macros/src/server.rs | 4 +- host/src/gatt.rs | 91 +++++++++++++++++++++------------------ 2 files changed, 51 insertions(+), 44 deletions(-) diff --git a/host-macros/src/server.rs b/host-macros/src/server.rs index ed58738b..2c9adf27 100644 --- a/host-macros/src/server.rs +++ b/host-macros/src/server.rs @@ -198,11 +198,11 @@ impl ServerBuilder { }) } - #visibility fn get, V: FromGatt>(&self, attribute_handle: &T) -> Result { + #visibility fn get, V: FromGatt>(&self, attribute_handle: &T) -> Result { self.server.table().get(attribute_handle) } - #visibility fn set(&self, attribute_handle: &T, input: &T::Value) -> Result<(), Error> { + #visibility fn set(&self, attribute_handle: &T, input: &T::Value) -> Result<(), trouble_host::Error> { self.server.table().set(attribute_handle, input) } diff --git a/host/src/gatt.rs b/host/src/gatt.rs index 29e0a0f6..8ffa72d6 100644 --- a/host/src/gatt.rs +++ b/host/src/gatt.rs @@ -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, @@ -82,47 +82,6 @@ impl<'stack, 'server> GattConnection<'stack, 'server> { } } - async fn process(&self, mut data: GattData<'_>) -> Result>, 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 @@ -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>, 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.