summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorAlbert Skog <mail@albertskog.se>2021-10-15 12:34:47 +0200
committerAlbert Skog <mail@albertskog.se>2021-10-15 12:34:47 +0200
commit182b9001189deca80e3840b6c989cbc37623991d (patch)
tree358d1d5ed6f9aaef0a6b622bb9b36f65b4a115d5 /examples
parentfd48489c0ffc58c8ae02d32d9904162a2777df3c (diff)
parentbf4f7a143d38713922d9bd5ff1a983c4c7290c10 (diff)
downloadnrf-softdevice-182b9001189deca80e3840b6c989cbc37623991d.zip
Merge branch 'master' into feature/indications
Diffstat (limited to 'examples')
-rw-r--r--examples/src/bin/ble_bas_peripheral.rs61
-rw-r--r--examples/src/bin/ble_peripheral_onoff.rs17
2 files changed, 46 insertions, 32 deletions
diff --git a/examples/src/bin/ble_bas_peripheral.rs b/examples/src/bin/ble_bas_peripheral.rs
index 8d76ec3..e3e6233 100644
--- a/examples/src/bin/ble_bas_peripheral.rs
+++ b/examples/src/bin/ble_bas_peripheral.rs
@@ -24,12 +24,16 @@ async fn softdevice_task(sd: &'static Softdevice) {
sd.run().await;
}
-#[nrf_softdevice::gatt_server(uuid = "180f")]
+#[nrf_softdevice::gatt_service(uuid = "180f")]
struct BatteryService {
- #[characteristic(uuid = "2a19", read, write, notify)]
+ #[characteristic(uuid = "2a19", read, notify)]
battery_level: u8,
+}
+
+#[nrf_softdevice::gatt_service(uuid = "9e7312e0-2354-11eb-9f10-fbc30a62cf38")]
+struct FooService {
#[characteristic(
- uuid = "3a4a1f7e-22d8-11eb-a3aa-1b3b1d4e4a0d",
+ uuid = "9e7312e0-2354-11eb-9f10-fbc30a63cf38",
read,
write,
notify,
@@ -38,9 +42,16 @@ struct BatteryService {
foo: u16,
}
+#[nrf_softdevice::gatt_server]
+struct Server {
+ bas: BatteryService,
+ foo: FooService,
+}
+
#[embassy::task]
async fn bluetooth_task(sd: &'static Softdevice) {
- let server: BatteryService = 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,
@@ -64,30 +75,28 @@ async fn bluetooth_task(sd: &'static Softdevice) {
// Run the GATT server on the connection. This returns when the connection gets disconnected.
let res = gatt_server::run(&conn, &server, |e| match e {
- BatteryServiceEvent::BatteryLevelWrite(val) => {
- info!("wrote battery level: {}", val);
- if let Err(e) = server.battery_level_notify(&conn, val + 1) {
- info!("send notification error: {:?}", e);
+ ServerEvent::Bas(e) => match e {
+ BatteryServiceEvent::BatteryLevelCccdWrite { notifications } => {
+ info!("battery notifications: {}", notifications)
+ }
+ },
+ ServerEvent::Foo(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);
+ }
}
- }
- BatteryServiceEvent::FooWrite(val) => {
- info!("wrote battery level: {}", val);
- if let Err(e) = server.foo_notify(&conn, val + 1) {
- info!("send notification error: {:?}", e);
+ FooServiceEvent::FooCccdWrite {
+ indications,
+ notifications,
+ } => {
+ info!(
+ "foo indications: {}, notifications: {}",
+ indications, notifications
+ )
}
- }
- BatteryServiceEvent::BatteryLevelCccdWrite { notifications } => {
- info!("battery notifications: {}", notifications)
- }
- BatteryServiceEvent::FooCccdWrite {
- indications,
- notifications,
- } => {
- info!(
- "foo indications: {}, notifications: {}",
- indications, notifications
- )
- }
+ },
})
.await;
diff --git a/examples/src/bin/ble_peripheral_onoff.rs b/examples/src/bin/ble_peripheral_onoff.rs
index 47b9dfd..32eac93 100644
--- a/examples/src/bin/ble_peripheral_onoff.rs
+++ b/examples/src/bin/ble_peripheral_onoff.rs
@@ -28,13 +28,18 @@ async fn softdevice_task(sd: &'static Softdevice) {
sd.run().await;
}
-#[nrf_softdevice::gatt_server(uuid = "9e7312e0-2354-11eb-9f10-fbc30a62cf38")]
+#[nrf_softdevice::gatt_service(uuid = "9e7312e0-2354-11eb-9f10-fbc30a62cf38")]
struct FooService {
#[characteristic(uuid = "9e7312e0-2354-11eb-9f10-fbc30a63cf38", read, write, notify)]
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,
@@ -57,13 +62,13 @@ async fn run_bluetooth(sd: &'static Softdevice, server: &FooService) {
info!("advertising done!");
let res = gatt_server::run(&conn, server, |e| match e {
- FooServiceEvent::FooWrite(val) => {
+ ServerEvent::Foo(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);
}
}
- FooServiceEvent::FooCccdWrite { notifications } => {
+ ServerEvent::Foo(FooServiceEvent::FooCccdWrite { notifications }) => {
info!("foo notifications: {}", notifications)
}
})
@@ -77,7 +82,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");