summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorUlf Lilleengen <lulf@redhat.com>2021-10-11 11:41:24 +0200
committerUlf Lilleengen <lulf@redhat.com>2021-10-11 11:41:24 +0200
commit1f73a93a6ddce925ff749d63f4528e50e9efae02 (patch)
treed0fa96325b5d7ec3a6bbcc472b6df06fb7c1a86d /examples
parentc9a0fbf3b7d9537463756efdad7a819cfff6756e (diff)
downloadnrf-softdevice-1f73a93a6ddce925ff749d63f4528e50e9efae02.zip
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.
Diffstat (limited to 'examples')
-rw-r--r--examples/src/bin/ble_bas_peripheral.rs41
-rw-r--r--examples/src/bin/ble_peripheral_onoff.rs29
2 files changed, 38 insertions, 32 deletions
diff --git a/examples/src/bin/ble_bas_peripheral.rs b/examples/src/bin/ble_bas_peripheral.rs
index 31a258e..ff30375 100644
--- a/examples/src/bin/ble_bas_peripheral.rs
+++ b/examples/src/bin/ble_bas_peripheral.rs
@@ -14,10 +14,7 @@ use defmt::*;
use embassy::executor::Executor;
use embassy::util::Forever;
-use nrf_softdevice::ble::{
- gatt_server::{self, Service},
- peripheral,
-};
+use nrf_softdevice::ble::{gatt_server, peripheral};
use nrf_softdevice::{raw, Softdevice};
static EXECUTOR: Forever<Executor> = Forever::new();
@@ -39,10 +36,16 @@ struct FooService {
foo: u16,
}
+#[nrf_softdevice::gatt_server]
+struct Server {
+ bas: BatteryService,
+ foo: FooService,
+}
+
#[embassy::task]
async fn bluetooth_task(sd: &'static Softdevice) {
- let battery_service: BatteryService = unwrap!(gatt_server::register(sd));
- let foo_service: FooService = unwrap!(gatt_server::register(sd));
+ let server: Server = unwrap!(gatt_server::register(sd));
+
#[rustfmt::skip]
let adv_data = &[
0x02, 0x01, raw::BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE as u8,
@@ -65,31 +68,29 @@ async fn bluetooth_task(sd: &'static Softdevice) {
info!("advertising done!");
// Run the GATT server on the connection. This returns when the connection gets disconnected.
- let res = gatt_server::run(&conn, |e| {
- match battery_service.on_write(e.clone()) {
- Some(BatteryServiceEvent::BatteryLevelNotificationsEnabled) => {
+ let res = gatt_server::run(&conn, &server, |e| match e {
+ ServerEvent::BatteryService(e) => match e {
+ BatteryServiceEvent::BatteryLevelNotificationsEnabled => {
info!("battery notifications enabled")
}
- Some(BatteryServiceEvent::BatteryLevelNotificationsDisabled) => {
+ BatteryServiceEvent::BatteryLevelNotificationsDisabled => {
info!("battery notifications disabled")
}
- None => {}
- }
- match foo_service.on_write(e) {
- Some(FooServiceEvent::FooWrite(val)) => {
- info!("wrote battery level: {}", val);
- if let Err(e) = foo_service.foo_notify(&conn, val + 1) {
+ },
+ ServerEvent::FooService(e) => match e {
+ FooServiceEvent::FooWrite(val) => {
+ info!("wrote foo: {}", val);
+ if let Err(e) = server.foo.foo_notify(&conn, val + 1) {
info!("send notification error: {:?}", e);
}
}
- Some(FooServiceEvent::FooNotificationsEnabled) => {
+ FooServiceEvent::FooNotificationsEnabled => {
info!("foo notifications enabled")
}
- Some(FooServiceEvent::FooNotificationsDisabled) => {
+ FooServiceEvent::FooNotificationsDisabled => {
info!("foo notifications disabled")
}
- None => {}
- }
+ },
})
.await;
diff --git a/examples/src/bin/ble_peripheral_onoff.rs b/examples/src/bin/ble_peripheral_onoff.rs
index 3666055..82d0599 100644
--- a/examples/src/bin/ble_peripheral_onoff.rs
+++ b/examples/src/bin/ble_peripheral_onoff.rs
@@ -18,10 +18,7 @@ use embassy_nrf::gpiote::PortInput;
use embassy_nrf::interrupt::Priority;
use futures::pin_mut;
-use nrf_softdevice::ble::{
- gatt_server::{self, Service},
- peripheral,
-};
+use nrf_softdevice::ble::{gatt_server, peripheral};
use nrf_softdevice::{raw, Softdevice};
static EXECUTOR: Forever<Executor> = Forever::new();
@@ -37,7 +34,12 @@ struct FooService {
foo: u16,
}
-async fn run_bluetooth(sd: &'static Softdevice, server: &FooService) {
+#[nrf_softdevice::gatt_server]
+struct Server {
+ foo: FooService,
+}
+
+async fn run_bluetooth(sd: &'static Softdevice, server: &Server) {
#[rustfmt::skip]
let adv_data = &[
0x02, 0x01, raw::BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE as u8,
@@ -59,16 +61,19 @@ async fn run_bluetooth(sd: &'static Softdevice, server: &FooService) {
info!("advertising done!");
- let res = gatt_server::run(&conn, |e| match server.on_write(e) {
- Some(FooServiceEvent::FooWrite(val)) => {
+ let res = gatt_server::run(&conn, server, |e| match e {
+ ServerEvent::FooService(FooServiceEvent::FooWrite(val)) => {
info!("wrote foo level: {}", val);
- if let Err(e) = server.foo_notify(&conn, val + 1) {
+ if let Err(e) = server.foo.foo_notify(&conn, val + 1) {
info!("send notification error: {:?}", e);
}
}
- Some(FooServiceEvent::FooNotificationsEnabled) => info!("notifications enabled"),
- Some(FooServiceEvent::FooNotificationsDisabled) => info!("notifications disabled"),
- None => {}
+ ServerEvent::FooService(FooServiceEvent::FooNotificationsEnabled) => {
+ info!("notifications enabled")
+ }
+ ServerEvent::FooService(FooServiceEvent::FooNotificationsDisabled) => {
+ info!("notifications disabled")
+ }
})
.await;
@@ -80,7 +85,7 @@ async fn run_bluetooth(sd: &'static Softdevice, server: &FooService) {
#[embassy::task]
async fn bluetooth_task(sd: &'static Softdevice, button1: AnyPin, button2: AnyPin) {
- let server: FooService = unwrap!(gatt_server::register(sd));
+ let server: Server = unwrap!(gatt_server::register(sd));
info!("Bluetooth is OFF");
info!("Press nrf52840-dk button 1 to enable, button 2 to disable");