diff options
author | Ulf Lilleengen <lulf@redhat.com> | 2021-10-08 08:30:14 +0200 |
---|---|---|
committer | Ulf Lilleengen <lulf@redhat.com> | 2021-10-08 08:30:14 +0200 |
commit | 2ee4c87061004a1c4e59f1445b5e9976c6d83e9b (patch) | |
tree | 9c70cccd478f2c3a29f8e74ec1ce91a21c58a09c /examples/src/bin/ble_bas_peripheral.rs | |
parent | 40c9680b46580009feaa5c8b54477eb48e7ee038 (diff) | |
download | nrf-softdevice-2ee4c87061004a1c4e59f1445b5e9976c6d83e9b.zip |
Split battery and foo service
Diffstat (limited to 'examples/src/bin/ble_bas_peripheral.rs')
-rw-r--r-- | examples/src/bin/ble_bas_peripheral.rs | 59 |
1 files changed, 35 insertions, 24 deletions
diff --git a/examples/src/bin/ble_bas_peripheral.rs b/examples/src/bin/ble_bas_peripheral.rs index 4fc773d..b63d578 100644 --- a/examples/src/bin/ble_bas_peripheral.rs +++ b/examples/src/bin/ble_bas_peripheral.rs @@ -31,13 +31,18 @@ async fn softdevice_task(sd: &'static Softdevice) { struct BatteryService { #[characteristic(uuid = "2a19", read, write, notify)] battery_level: u8, - #[characteristic(uuid = "3a4a1f7e-22d8-11eb-a3aa-1b3b1d4e4a0d", read, write, notify)] +} + +#[nrf_softdevice::gatt_server(uuid = "9e7312e0-2354-11eb-9f10-fbc30a62cf38")] +struct FooService { + #[characteristic(uuid = "9e7312e0-2354-11eb-9f10-fbc30a63cf38", read, write, notify)] foo: u16, } #[embassy::task] async fn bluetooth_task(sd: &'static Softdevice) { - let server: BatteryService = unwrap!(gatt_server::register(sd)); + let battery_service: BatteryService = unwrap!(gatt_server::register(sd)); + let foo_service: FooService = unwrap!(gatt_server::register(sd)); #[rustfmt::skip] let adv_data = &[ 0x02, 0x01, raw::BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE as u8, @@ -60,32 +65,38 @@ 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 server.on_write(e) { - Some(BatteryServiceEvent::BatteryLevelWrite(val)) => { - info!("wrote battery level: {}", val); - if let Err(e) = server.battery_level_notify(&conn, val + 1) { - info!("send notification error: {:?}", e); + let res = gatt_server::run(&conn, |e| { + match battery_service.on_write(e.clone()) { + Some(BatteryServiceEvent::BatteryLevelWrite(val)) => { + info!("wrote battery level: {}", val); + if let Err(e) = battery_service.battery_level_notify(&conn, val + 1) { + info!("send notification error: {:?}", e); + } } - } - Some(BatteryServiceEvent::FooWrite(val)) => { - info!("wrote battery level: {}", val); - if let Err(e) = server.foo_notify(&conn, val + 1) { - info!("send notification error: {:?}", e); + + Some(BatteryServiceEvent::BatteryLevelNotificationsEnabled) => { + info!("battery notifications enabled") } + Some(BatteryServiceEvent::BatteryLevelNotificationsDisabled) => { + info!("battery notifications disabled") + } + None => {} } - Some(BatteryServiceEvent::BatteryLevelNotificationsEnabled) => { - info!("battery notifications enabled") - } - Some(BatteryServiceEvent::BatteryLevelNotificationsDisabled) => { - info!("battery notifications disabled") - } - Some(BatteryServiceEvent::FooNotificationsEnabled) => { - info!("foo notifications enabled") - } - Some(BatteryServiceEvent::FooNotificationsDisabled) => { - info!("foo notifications disabled") + 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) { + info!("send notification error: {:?}", e); + } + } + Some(FooServiceEvent::FooNotificationsEnabled) => { + info!("foo notifications enabled") + } + Some(FooServiceEvent::FooNotificationsDisabled) => { + info!("foo notifications disabled") + } + None => {} } - None => {} }) .await; |