From f291131f082a37a4a36eca84e5322b9ea1718749 Mon Sep 17 00:00:00 2001 From: Ulf Lilleengen Date: Tue, 5 Oct 2021 13:25:06 +0200 Subject: Refactor GATT handling to support multiple services * Make run() return all GATT events and leave the responsibility of calling the service handler to the application * Generate code for each service that converts from the generic event into the type-specific event. --- nrf-softdevice/src/ble/gatt_server.rs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'nrf-softdevice') diff --git a/nrf-softdevice/src/ble/gatt_server.rs b/nrf-softdevice/src/ble/gatt_server.rs index 58663cc..2f65d5c 100644 --- a/nrf-softdevice/src/ble/gatt_server.rs +++ b/nrf-softdevice/src/ble/gatt_server.rs @@ -28,15 +28,23 @@ pub struct CharacteristicHandles { pub sccd_handle: u16, } +pub struct GattEvent<'a> { + pub handle: u16, + pub data: &'a [u8], +} + pub trait Server: Sized { type Event; fn uuid() -> Uuid; + fn register(service_handle: u16, register_char: F) -> Result where F: FnMut(Characteristic, &[u8]) -> Result; - fn on_write(&self, handle: u16, data: &[u8]) -> Option; + fn on_event<'m, F>(&self, event: &GattEvent<'m>, f: F) + where + F: FnMut(Self::Event); } #[derive(Debug, PartialEq, Eq, Clone, Copy)] @@ -139,9 +147,9 @@ impl From for RunError { } } -pub async fn run(conn: &Connection, server: &S, mut f: F) -> Result<(), RunError> +pub async fn run<'m, F>(conn: &Connection, mut f: F) -> Result<(), RunError> where - F: FnMut(S::Event), + F: FnMut(GattEvent<'m>), { let conn_handle = conn.with_state(|state| state.check_connected())?; portal(conn_handle) @@ -163,7 +171,10 @@ where panic!("gatt_server auth_required not yet supported"); } - server.on_write(params.handle, v).map(|e| f(e)); + f(GattEvent { + handle: params.handle, + data: &v, + }); } _ => {} } -- cgit v1.2.3 From 8d78ed20f1313d00d8727f41383413a13c23d457 Mon Sep 17 00:00:00 2001 From: Ulf Lilleengen Date: Wed, 6 Oct 2021 18:43:55 +0200 Subject: Simplify API --- nrf-softdevice/src/ble/gatt_server.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'nrf-softdevice') diff --git a/nrf-softdevice/src/ble/gatt_server.rs b/nrf-softdevice/src/ble/gatt_server.rs index 2f65d5c..4200c22 100644 --- a/nrf-softdevice/src/ble/gatt_server.rs +++ b/nrf-softdevice/src/ble/gatt_server.rs @@ -28,9 +28,10 @@ pub struct CharacteristicHandles { pub sccd_handle: u16, } +#[derive(Clone)] pub struct GattEvent<'a> { - pub handle: u16, - pub data: &'a [u8], + handle: u16, + data: &'a [u8], } pub trait Server: Sized { @@ -42,9 +43,7 @@ pub trait Server: Sized { where F: FnMut(Characteristic, &[u8]) -> Result; - fn on_event<'m, F>(&self, event: &GattEvent<'m>, f: F) - where - F: FnMut(Self::Event); + fn on_write<'m>(&self, event: GattEvent<'m>) -> Option; } #[derive(Debug, PartialEq, Eq, Clone, Copy)] -- cgit v1.2.3 From ab48ad7f39900bdc9865ea65e800c16e6e128185 Mon Sep 17 00:00:00 2001 From: Ulf Lilleengen Date: Wed, 6 Oct 2021 18:45:40 +0200 Subject: Simplify further --- nrf-softdevice/src/ble/gatt_server.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'nrf-softdevice') diff --git a/nrf-softdevice/src/ble/gatt_server.rs b/nrf-softdevice/src/ble/gatt_server.rs index 4200c22..ba6bc9c 100644 --- a/nrf-softdevice/src/ble/gatt_server.rs +++ b/nrf-softdevice/src/ble/gatt_server.rs @@ -30,8 +30,8 @@ pub struct CharacteristicHandles { #[derive(Clone)] pub struct GattEvent<'a> { - handle: u16, - data: &'a [u8], + pub handle: u16, + pub data: &'a [u8], } pub trait Server: Sized { -- cgit v1.2.3 From ded290c788d79a9c90059c8eceae2e442116ad3c Mon Sep 17 00:00:00 2001 From: Ulf Lilleengen Date: Wed, 6 Oct 2021 18:47:57 +0200 Subject: Rename to on_event --- nrf-softdevice/src/ble/gatt_server.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'nrf-softdevice') diff --git a/nrf-softdevice/src/ble/gatt_server.rs b/nrf-softdevice/src/ble/gatt_server.rs index ba6bc9c..124c3ff 100644 --- a/nrf-softdevice/src/ble/gatt_server.rs +++ b/nrf-softdevice/src/ble/gatt_server.rs @@ -43,7 +43,7 @@ pub trait Server: Sized { where F: FnMut(Characteristic, &[u8]) -> Result; - fn on_write<'m>(&self, event: GattEvent<'m>) -> Option; + fn on_event<'m>(&self, event: GattEvent<'m>) -> Option; } #[derive(Debug, PartialEq, Eq, Clone, Copy)] -- cgit v1.2.3 From 714f3ad657a65315a70437fe133ff088fa2bac11 Mon Sep 17 00:00:00 2001 From: Ulf Lilleengen Date: Thu, 7 Oct 2021 08:29:36 +0200 Subject: Rename back to on_write --- nrf-softdevice/src/ble/gatt_server.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'nrf-softdevice') diff --git a/nrf-softdevice/src/ble/gatt_server.rs b/nrf-softdevice/src/ble/gatt_server.rs index 124c3ff..ba6bc9c 100644 --- a/nrf-softdevice/src/ble/gatt_server.rs +++ b/nrf-softdevice/src/ble/gatt_server.rs @@ -43,7 +43,7 @@ pub trait Server: Sized { where F: FnMut(Characteristic, &[u8]) -> Result; - fn on_event<'m>(&self, event: GattEvent<'m>) -> Option; + fn on_write<'m>(&self, event: GattEvent<'m>) -> Option; } #[derive(Debug, PartialEq, Eq, Clone, Copy)] -- cgit v1.2.3 From c9a0fbf3b7d9537463756efdad7a819cfff6756e Mon Sep 17 00:00:00 2001 From: Ulf Lilleengen Date: Mon, 11 Oct 2021 09:53:42 +0200 Subject: Rename gatt_server to gatt_service --- nrf-softdevice/src/ble/gatt_server.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'nrf-softdevice') diff --git a/nrf-softdevice/src/ble/gatt_server.rs b/nrf-softdevice/src/ble/gatt_server.rs index ba6bc9c..aa66324 100644 --- a/nrf-softdevice/src/ble/gatt_server.rs +++ b/nrf-softdevice/src/ble/gatt_server.rs @@ -34,7 +34,7 @@ pub struct GattEvent<'a> { pub data: &'a [u8], } -pub trait Server: Sized { +pub trait Service: Sized { type Event; fn uuid() -> Uuid; @@ -58,7 +58,7 @@ impl From for RegisterError { } } -pub fn register(_sd: &Softdevice) -> Result { +pub fn register(_sd: &Softdevice) -> Result { let mut service_handle: u16 = 0; let ret = unsafe { raw::sd_ble_gatts_service_add( -- cgit v1.2.3 From 1f73a93a6ddce925ff749d63f4528e50e9efae02 Mon Sep 17 00:00:00 2001 From: Ulf Lilleengen Date: Mon, 11 Oct 2021 11:41:24 +0200 Subject: Add gatt_server macro for registering a GATT server Generate event type and on_write handling for server type, which in turn dispatches events to its services. Register and run now takes a valid Server instance as argument. --- nrf-softdevice/src/ble/gatt_server.rs | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) (limited to 'nrf-softdevice') diff --git a/nrf-softdevice/src/ble/gatt_server.rs b/nrf-softdevice/src/ble/gatt_server.rs index aa66324..b744d9b 100644 --- a/nrf-softdevice/src/ble/gatt_server.rs +++ b/nrf-softdevice/src/ble/gatt_server.rs @@ -28,10 +28,10 @@ pub struct CharacteristicHandles { pub sccd_handle: u16, } -#[derive(Clone)] -pub struct GattEvent<'a> { - pub handle: u16, - pub data: &'a [u8], +pub trait Server: Sized { + type Event; + fn register(sd: &Softdevice) -> Result; + fn on_write(&self, handle: u16, data: &[u8]) -> Option; } pub trait Service: Sized { @@ -43,7 +43,7 @@ pub trait Service: Sized { where F: FnMut(Characteristic, &[u8]) -> Result; - fn on_write<'m>(&self, event: GattEvent<'m>) -> Option; + fn on_write(&self, handle: u16, data: &[u8]) -> Option; } #[derive(Debug, PartialEq, Eq, Clone, Copy)] @@ -58,12 +58,17 @@ impl From for RegisterError { } } -pub fn register(_sd: &Softdevice) -> Result { +pub fn register(sd: &Softdevice) -> Result { + S::register(sd) +} + +pub fn register_service(_sd: &Softdevice) -> Result { + let uuid = S::uuid(); let mut service_handle: u16 = 0; let ret = unsafe { raw::sd_ble_gatts_service_add( raw::BLE_GATTS_SRVC_TYPE_PRIMARY as u8, - S::uuid().as_raw_ptr(), + uuid.as_raw_ptr(), &mut service_handle as _, ) }; @@ -146,9 +151,10 @@ impl From for RunError { } } -pub async fn run<'m, F>(conn: &Connection, mut f: F) -> Result<(), RunError> +pub async fn run<'m, F, S>(conn: &Connection, server: &S, mut f: F) -> Result<(), RunError> where - F: FnMut(GattEvent<'m>), + F: FnMut(S::Event), + S: Server, { let conn_handle = conn.with_state(|state| state.check_connected())?; portal(conn_handle) @@ -170,10 +176,7 @@ where panic!("gatt_server auth_required not yet supported"); } - f(GattEvent { - handle: params.handle, - data: &v, - }); + server.on_write(params.handle, &v).map(|e| f(e)); } _ => {} } -- cgit v1.2.3 From bf4f7a143d38713922d9bd5ff1a983c4c7290c10 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Wed, 13 Oct 2021 21:31:44 +0200 Subject: Allow using cortex-m-rt 0.6 or 0.7 --- nrf-softdevice/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'nrf-softdevice') diff --git a/nrf-softdevice/Cargo.toml b/nrf-softdevice/Cargo.toml index 1034aad..143e1d0 100644 --- a/nrf-softdevice/Cargo.toml +++ b/nrf-softdevice/Cargo.toml @@ -43,7 +43,7 @@ critical-section = { version = "0.2.1" } num_enum = { version = "0.5.1", default-features = false } embassy = { version = "0.1.0" } cortex-m = "0.7.2" -cortex-m-rt = "0.6.13" +cortex-m-rt = ">=0.6.15,<0.8" heapless = "0.7.1" fixed = "1.5.0" -- cgit v1.2.3