Skip to content

Commit

Permalink
add rssi task to gatt periph example (embassy-rs#219)
Browse files Browse the repository at this point in the history
* add rssi task to gatt periph example

* add ReadRssi to Controller trait
  • Loading branch information
jamessizeland authored Jan 7, 2025
1 parent 5e55ac1 commit 44b58bd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
19 changes: 16 additions & 3 deletions examples/apps/src/ble_bas_peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ where
info!("Our address = {:?}", address);

let mut resources = Resources::new(PacketQos::None);
let (_stack, mut peripheral, _, runner) = trouble_host::new(controller, &mut resources)
let (stack, mut peripheral, _, runner) = trouble_host::new(controller, &mut resources)
.set_random_address(address)
.build();

Expand All @@ -61,7 +61,7 @@ where
Ok(conn) => {
// set up tasks when the connection is established to a central, so they don't run when no one is connected.
let connection_task = conn_task(&server, &conn);
let counter_task = counter_task(&server, &conn);
let counter_task = counter_task(&server, &conn, stack);
// run until any task ends (usually because the connection has been closed),
// then return to advertising state.
select(connection_task, counter_task).await;
Expand Down Expand Up @@ -103,6 +103,9 @@ async fn ble_task<C: Controller>(mut runner: Runner<'_, C>) {
}

/// Stream Events until the connection closes.
///
/// This function will handle the GATT events and process them.
/// This is how we interact with read and write requests.
async fn conn_task(server: &Server<'_>, conn: &Connection<'_>) -> Result<(), Error> {
let level = server.battery_service.level;
loop {
Expand Down Expand Up @@ -174,7 +177,10 @@ async fn advertise<'a, C: Controller>(
}

/// Example task to use the BLE notifier interface.
async fn counter_task(server: &Server<'_>, conn: &Connection<'_>) {
/// This task will notify the connected central of a counter value every 2 seconds.
/// It will also read the RSSI value every 2 seconds.
/// and will stop when the connection is closed by the central or an error occurs.
async fn counter_task<C: Controller>(server: &Server<'_>, conn: &Connection<'_>, stack: Stack<'_, C>) {
let mut tick: u8 = 0;
let level = server.battery_service.level;
loop {
Expand All @@ -184,6 +190,13 @@ async fn counter_task(server: &Server<'_>, conn: &Connection<'_>) {
info!("[adv] error notifying connection");
break;
};
// read RSSI (Received Signal Strength Indicator) of the connection.
if let Ok(rssi) = conn.rssi(stack).await {
info!("[gatt] RSSI: {:?}", rssi);
} else {
info!("[gatt] error getting RSSI");
break;
};
Timer::after_secs(2).await;
}
}
3 changes: 3 additions & 0 deletions host/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use core::mem::MaybeUninit;

use advertise::AdvertisementDataError;
use bt_hci::cmd::status::ReadRssi;
use bt_hci::cmd::{AsyncCmd, SyncCmd};
pub use bt_hci::param::{AddrKind, BdAddr, LeConnRole as Role};
use bt_hci::FromHciBytesError;
Expand Down Expand Up @@ -244,6 +245,7 @@ pub trait Controller:
+ ControllerCmdSync<LeReadFilterAcceptListSize>
+ ControllerCmdSync<SetControllerToHostFlowControl>
+ ControllerCmdSync<Reset>
+ ControllerCmdSync<ReadRssi>
+ ControllerCmdSync<LeCreateConnCancel>
+ ControllerCmdAsync<LeCreateConn>
+ ControllerCmdSync<LeClearFilterAcceptList>
Expand Down Expand Up @@ -274,6 +276,7 @@ impl<
+ ControllerCmdSync<LeAddDeviceToFilterAcceptList>
+ ControllerCmdSync<SetControllerToHostFlowControl>
+ ControllerCmdSync<Reset>
+ ControllerCmdSync<ReadRssi>
+ ControllerCmdSync<LeCreateConnCancel>
+ ControllerCmdAsync<LeCreateConn>
+ for<'t> ControllerCmdSync<LeSetAdvEnable>
Expand Down

0 comments on commit 44b58bd

Please sign in to comment.