summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlbert Skog <mail@albertskog.se>2021-10-15 11:24:36 +0200
committerAlbert Skog <mail@albertskog.se>2021-10-15 11:24:36 +0200
commitfd48489c0ffc58c8ae02d32d9904162a2777df3c (patch)
treef86a630279aeaf89d57a2151e19872c41f75d205
parentae537dcbf67dc2366574c583740b2c7823c733d1 (diff)
downloadnrf-softdevice-fd48489c0ffc58c8ae02d32d9904162a2777df3c.zip
make CCCD write a single event
-rw-r--r--examples/src/bin/ble_bas_peripheral.rs24
-rw-r--r--examples/src/bin/ble_peripheral_onoff.rs5
-rw-r--r--nrf-softdevice-macro/src/lib.rs54
3 files changed, 40 insertions, 43 deletions
diff --git a/examples/src/bin/ble_bas_peripheral.rs b/examples/src/bin/ble_bas_peripheral.rs
index 8793c82..8d76ec3 100644
--- a/examples/src/bin/ble_bas_peripheral.rs
+++ b/examples/src/bin/ble_bas_peripheral.rs
@@ -28,7 +28,13 @@ 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)]
+ #[characteristic(
+ uuid = "3a4a1f7e-22d8-11eb-a3aa-1b3b1d4e4a0d",
+ read,
+ write,
+ notify,
+ indicate
+ )]
foo: u16,
}
@@ -70,14 +76,18 @@ async fn bluetooth_task(sd: &'static Softdevice) {
info!("send notification error: {:?}", e);
}
}
- BatteryServiceEvent::BatteryLevelNotificationsEnabled => {
- info!("battery notifications enabled")
+ BatteryServiceEvent::BatteryLevelCccdWrite { notifications } => {
+ info!("battery notifications: {}", notifications)
}
- BatteryServiceEvent::BatteryLevelNotificationsDisabled => {
- info!("battery notifications disabled")
+ BatteryServiceEvent::FooCccdWrite {
+ indications,
+ notifications,
+ } => {
+ info!(
+ "foo indications: {}, notifications: {}",
+ indications, notifications
+ )
}
- BatteryServiceEvent::FooNotificationsEnabled => info!("foo notifications enabled"),
- BatteryServiceEvent::FooNotificationsDisabled => info!("foo notifications disabled"),
})
.await;
diff --git a/examples/src/bin/ble_peripheral_onoff.rs b/examples/src/bin/ble_peripheral_onoff.rs
index e3f3943..47b9dfd 100644
--- a/examples/src/bin/ble_peripheral_onoff.rs
+++ b/examples/src/bin/ble_peripheral_onoff.rs
@@ -63,8 +63,9 @@ async fn run_bluetooth(sd: &'static Softdevice, server: &FooService) {
info!("send notification error: {:?}", e);
}
}
- FooServiceEvent::FooNotificationsEnabled => info!("notifications enabled"),
- FooServiceEvent::FooNotificationsDisabled => info!("notifications disabled"),
+ FooServiceEvent::FooCccdWrite { notifications } => {
+ info!("foo notifications: {}", notifications)
+ }
})
.await;
diff --git a/nrf-softdevice-macro/src/lib.rs b/nrf-softdevice-macro/src/lib.rs
index 2f4b766..e1cec67 100644
--- a/nrf-softdevice-macro/src/lib.rs
+++ b/nrf-softdevice-macro/src/lib.rs
@@ -212,19 +212,17 @@ pub fn gatt_server(args: TokenStream, item: TokenStream) -> TokenStream {
));
if !indicate {
- let case_enabled = format_ident!("{}NotificationsEnabled", name_pascal);
- let case_disabled = format_ident!("{}NotificationsDisabled", name_pascal);
+ let case_cccd_write = format_ident!("{}CccdWrite", name_pascal);
code_event_enum.extend(quote_spanned!(ch.span=>
- #case_enabled,
- #case_disabled,
+ #case_cccd_write{notifications: bool},
));
code_on_write.extend(quote_spanned!(ch.span=>
- if handle == self.#cccd_handle {
- if data.len() != 0 && data[0] & 0x01 != 0 {
- return Some(#event_enum_name::#case_enabled);
- } else {
- return Some(#event_enum_name::#case_disabled);
+ if handle == self.#cccd_handle && data.len() != 0 {
+ match data[0] & 0x01 {
+ 0x00 => return Some(#event_enum_name::#case_cccd_write{notifications: false}),
+ 0x01 => return Some(#event_enum_name::#case_cccd_write{notifications: true}),
+ _ => {},
}
}
));
@@ -244,19 +242,17 @@ pub fn gatt_server(args: TokenStream, item: TokenStream) -> TokenStream {
));
if !notify {
- let case_enabled = format_ident!("{}IndicationsEnabled", name_pascal);
- let case_disabled = format_ident!("{}IndicationsDisabled", name_pascal);
+ let case_cccd_write = format_ident!("{}CccdWrite", name_pascal);
code_event_enum.extend(quote_spanned!(ch.span=>
- #case_enabled,
- #case_disabled,
+ #case_cccd_write{indications: bool},
));
code_on_write.extend(quote_spanned!(ch.span=>
- if handle == self.#cccd_handle {
- if data.len() != 0 && data[0] & 0x02 != 0 {
- return Some(#event_enum_name::#case_enabled);
- } else {
- return Some(#event_enum_name::#case_disabled);
+ if handle == self.#cccd_handle && data.len() != 0 {
+ match data[0] & 0x02 {
+ 0x00 => return Some(#event_enum_name::#case_cccd_write{indications: false}),
+ 0x02 => return Some(#event_enum_name::#case_cccd_write{indications: true}),
+ _ => {},
}
}
));
@@ -264,28 +260,18 @@ pub fn gatt_server(args: TokenStream, item: TokenStream) -> TokenStream {
}
if indicate && notify {
- let case_disabled_disabled =
- format_ident!("{}IndicationsDisabledNotificationsDisabled", name_pascal);
- let case_disabled_enabled =
- format_ident!("{}IndicationsDisabledNotificationsEnabled", name_pascal);
- let case_enabled_disabled =
- format_ident!("{}IndicationsEnabledNotificationsDisabled", name_pascal);
- let case_enabled_enabled =
- format_ident!("{}IndicationsEnabledNotificationsEnabled", name_pascal);
+ let case_cccd_write = format_ident!("{}CccdWrite", name_pascal);
code_event_enum.extend(quote_spanned!(ch.span=>
- #case_disabled_disabled,
- #case_disabled_enabled,
- #case_enabled_disabled,
- #case_enabled_enabled,
+ #case_cccd_write{indications: bool, notifications: bool},
));
code_on_write.extend(quote_spanned!(ch.span=>
if handle == self.#cccd_handle && data.len() != 0 {
match data[0] & 0x03 {
- 0x00 => return Some(#event_enum_name::#case_disabled_disabled),
- 0x01 => return Some(#event_enum_name::#case_disabled_enabled),
- 0x02 => return Some(#event_enum_name::#case_enabled_disabled),
- 0x03 => return Some(#event_enum_name::#case_enabled_enabled),
+ 0x00 => return Some(#event_enum_name::#case_cccd_write{indications: false, notifications: false}),
+ 0x01 => return Some(#event_enum_name::#case_cccd_write{indications: false, notifications: true}),
+ 0x02 => return Some(#event_enum_name::#case_cccd_write{indications: true, notifications: false}),
+ 0x03 => return Some(#event_enum_name::#case_cccd_write{indications: true, notifications: true}),
_ => {},
}
}