Skip to content

Commit

Permalink
Update NUS peripheral example to latest API changes
Browse files Browse the repository at this point in the history
  • Loading branch information
plaes committed Oct 23, 2024
1 parent 8a5bcee commit 585208e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
30 changes: 13 additions & 17 deletions examples/apps/src/ble_nus_peripheral.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* Nordic Uart Service (NUS) peripheral example */
use embassy_futures::join::join3;
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
use embassy_time::{Duration, Timer};
use heapless::Vec;
use trouble_host::prelude::*;
Expand Down Expand Up @@ -50,18 +49,13 @@ where
.set_random_address(address)
.build();

let mut table: AttributeTable<'_, NoopRawMutex, MAX_ATTRIBUTES> = AttributeTable::new();

// Generic Access Service (mandatory)
let id = b"Trouble Example Device";
let mut svc = table.add_service(Service::new(0x1800));
let _ = svc.add_characteristic_ro(0x2a00, id);
svc.build();

// Generic attribute service (mandatory)
table.add_service(Service::new(0x1801));

let server = Server::new(stack, &mut table);
let server = Server::new_with_config(
stack,
GapConfig::Peripheral(PeripheralConfig {
name: "TrouBLE",
appearance: &appearance::GENERIC_UNKNOWN,
}),
);

info!("Starting advertising and GATT service");
let _ = join3(
Expand All @@ -76,7 +70,7 @@ async fn ble_task<C: Controller>(mut runner: Runner<'_, C>) -> Result<(), BleHos
runner.run().await
}

async fn gatt_task<C: Controller>(server: &Server<'_, '_, C>) {
async fn gatt_task<C: Controller>(server: &Server<'_, C>) {
loop {
match server.next().await {
Ok(GattEvent::Write { handle, connection: _ }) => {
Expand All @@ -96,13 +90,13 @@ async fn gatt_task<C: Controller>(server: &Server<'_, '_, C>) {

async fn advertise_task<C: Controller>(
mut peripheral: Peripheral<'_, C>,
server: &Server<'_, '_, C>,
server: &Server<'_, C>,
) -> Result<(), BleHostError<C::Error>> {
let mut adv_data = [0; 31];
AdStructure::encode_slice(
&[
AdStructure::Flags(LE_GENERAL_DISCOVERABLE | BR_EDR_NOT_SUPPORTED),
AdStructure::CompleteLocalName(b"Trouble NRF UART"),
AdStructure::CompleteLocalName(b"Trouble NRF UART (NUS) Service"),
],
&mut adv_data[..],
)?;
Expand All @@ -120,8 +114,10 @@ async fn advertise_task<C: Controller>(
let conn = advertiser.accept().await?;

/* TODO: Implement "echo" and push rx bytes back to tx? */
let mut tx = [0; ATT_MTU];
// XXX: I have to manually "fix" the size from 123 -> 128
let mut tx = [0; 128];
let mut tick: u8 = 0;
// Keep connection alive
while conn.is_connected() {
Timer::after(Duration::from_secs(2)).await;
tick = tick.wrapping_add(1);
Expand Down
1 change: 0 additions & 1 deletion examples/nrf-sdc/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 585208e

Please sign in to comment.