From 585208e638c73b9c98986732260a41ab78e51f57 Mon Sep 17 00:00:00 2001 From: Priit Laes Date: Wed, 23 Oct 2024 13:49:20 +0300 Subject: [PATCH] Update NUS peripheral example to latest API changes --- examples/apps/src/ble_nus_peripheral.rs | 30 +++++++++++-------------- examples/nrf-sdc/Cargo.lock | 1 - 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/examples/apps/src/ble_nus_peripheral.rs b/examples/apps/src/ble_nus_peripheral.rs index 4f8a6570..703e2089 100644 --- a/examples/apps/src/ble_nus_peripheral.rs +++ b/examples/apps/src/ble_nus_peripheral.rs @@ -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::*; @@ -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( @@ -76,7 +70,7 @@ async fn ble_task(mut runner: Runner<'_, C>) -> Result<(), BleHos runner.run().await } -async fn gatt_task(server: &Server<'_, '_, C>) { +async fn gatt_task(server: &Server<'_, C>) { loop { match server.next().await { Ok(GattEvent::Write { handle, connection: _ }) => { @@ -96,13 +90,13 @@ async fn gatt_task(server: &Server<'_, '_, C>) { async fn advertise_task( mut peripheral: Peripheral<'_, C>, - server: &Server<'_, '_, C>, + server: &Server<'_, C>, ) -> Result<(), BleHostError> { 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[..], )?; @@ -120,8 +114,10 @@ async fn advertise_task( 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); diff --git a/examples/nrf-sdc/Cargo.lock b/examples/nrf-sdc/Cargo.lock index 8db0ff44..0e4fc0d2 100644 --- a/examples/nrf-sdc/Cargo.lock +++ b/examples/nrf-sdc/Cargo.lock @@ -1310,7 +1310,6 @@ dependencies = [ "panic-probe", "static_cell", "trouble-example-apps", - "trouble-host", ] [[package]]