|
| 1 | +#![no_std] |
| 2 | +#![no_main] |
| 3 | + |
| 4 | +use {defmt_rtt as _, panic_probe as _}; |
| 5 | + |
| 6 | +use defmt::{error, info}; |
| 7 | +use embassy_executor::Spawner; |
| 8 | +use embassy_futures::join::join3; |
| 9 | +use embassy_time::{Duration, Timer}; |
| 10 | +use microbit_bsp::{ble::MultiprotocolServiceLayer, Config, Microbit}; |
| 11 | +use trouble_host::prelude::*; |
| 12 | + |
| 13 | +/// Size of L2CAP packets (ATT MTU is this - 4) |
| 14 | +const L2CAP_MTU: usize = 251; |
| 15 | + |
| 16 | +/// Max number of connections |
| 17 | +const CONNECTIONS_MAX: usize = 1; |
| 18 | + |
| 19 | +/// Max number of L2CAP channels. |
| 20 | +const L2CAP_CHANNELS_MAX: usize = 2; // Signal + att |
| 21 | + |
| 22 | +type Resources<C> = HostResources<C, CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, L2CAP_MTU>; |
| 23 | + |
| 24 | +// GATT Server definition |
| 25 | +#[gatt_server(attribute_data_size = 10)] |
| 26 | +struct Server { |
| 27 | + battery_service: BatteryService, |
| 28 | +} |
| 29 | + |
| 30 | +// Battery service |
| 31 | +#[gatt_service(uuid = "180f")] |
| 32 | +struct BatteryService { |
| 33 | + #[characteristic(uuid = "2a19", read, notify)] |
| 34 | + level: u8, |
| 35 | +} |
| 36 | + |
| 37 | +#[embassy_executor::task] |
| 38 | +async fn mpsl_task(mpsl: &'static MultiprotocolServiceLayer<'static>) -> ! { |
| 39 | + mpsl.run().await |
| 40 | +} |
| 41 | + |
| 42 | +#[embassy_executor::main] |
| 43 | +async fn main(spawner: Spawner) { |
| 44 | + let board = Microbit::new(Config::default()); |
| 45 | + let (sdc, mpsl) = board |
| 46 | + .ble |
| 47 | + .init(board.timer0, board.rng) |
| 48 | + .expect("BLE Stack failed to initialize"); |
| 49 | + spawner.must_spawn(mpsl_task(mpsl)); |
| 50 | + |
| 51 | + run(sdc).await; |
| 52 | +} |
| 53 | + |
| 54 | +pub async fn run<C>(controller: C) |
| 55 | +where |
| 56 | + C: Controller, |
| 57 | +{ |
| 58 | + let address = Address::random([0x41, 0x5A, 0xE3, 0x1E, 0x83, 0xE7]); |
| 59 | + info!("Our address = {:?}", address); |
| 60 | + |
| 61 | + let mut resources = Resources::new(PacketQos::None); |
| 62 | + let (stack, peripheral, _, runner) = trouble_host::new(controller, &mut resources) |
| 63 | + .set_random_address(address) |
| 64 | + .build(); |
| 65 | + |
| 66 | + let server = Server::new_with_config( |
| 67 | + stack, |
| 68 | + GapConfig::Peripheral(PeripheralConfig { |
| 69 | + name: "Trouble", |
| 70 | + appearance: &appearance::GENERIC_POWER, |
| 71 | + }), |
| 72 | + ) |
| 73 | + .expect("Failed to create GATT server"); |
| 74 | + |
| 75 | + info!("Starting advertising and GATT service"); |
| 76 | + let _ = join3( |
| 77 | + ble_task(runner), |
| 78 | + gatt_task(&server), |
| 79 | + advertise_task(peripheral, &server), |
| 80 | + ) |
| 81 | + .await; |
| 82 | +} |
| 83 | + |
| 84 | +async fn ble_task<C: Controller>(mut runner: Runner<'_, C>) -> Result<(), BleHostError<C::Error>> { |
| 85 | + runner.run().await |
| 86 | +} |
| 87 | + |
| 88 | +async fn gatt_task<C: Controller>(server: &Server<'_, '_, C>) { |
| 89 | + loop { |
| 90 | + match server.next().await { |
| 91 | + Ok(GattEvent::Write { |
| 92 | + value_handle, |
| 93 | + connection: _, |
| 94 | + }) => { |
| 95 | + info!("[gatt] Write event on {:?}", value_handle); |
| 96 | + } |
| 97 | + Ok(GattEvent::Read { |
| 98 | + value_handle, |
| 99 | + connection: _, |
| 100 | + }) => { |
| 101 | + info!("[gatt] Read event on {:?}", value_handle); |
| 102 | + } |
| 103 | + Err(e) => { |
| 104 | + error!("[gatt] Error processing GATT events: {:?}", e); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +async fn advertise_task<C: Controller>( |
| 111 | + mut peripheral: Peripheral<'_, C>, |
| 112 | + server: &Server<'_, '_, C>, |
| 113 | +) -> Result<(), BleHostError<C::Error>> { |
| 114 | + let mut adv_data = [0; 31]; |
| 115 | + AdStructure::encode_slice( |
| 116 | + &[ |
| 117 | + AdStructure::Flags(LE_GENERAL_DISCOVERABLE | BR_EDR_NOT_SUPPORTED), |
| 118 | + AdStructure::ServiceUuids16(&[Uuid::Uuid16([0x0f, 0x18])]), |
| 119 | + AdStructure::CompleteLocalName(b"Trouble"), |
| 120 | + ], |
| 121 | + &mut adv_data[..], |
| 122 | + )?; |
| 123 | + loop { |
| 124 | + info!("[adv] advertising"); |
| 125 | + let mut advertiser = peripheral |
| 126 | + .advertise( |
| 127 | + &Default::default(), |
| 128 | + Advertisement::ConnectableScannableUndirected { |
| 129 | + adv_data: &adv_data[..], |
| 130 | + scan_data: &[], |
| 131 | + }, |
| 132 | + ) |
| 133 | + .await?; |
| 134 | + let conn = advertiser.accept().await?; |
| 135 | + info!("[adv] connection established"); |
| 136 | + // Keep connection alive |
| 137 | + let mut tick: u8 = 0; |
| 138 | + while conn.is_connected() { |
| 139 | + Timer::after(Duration::from_secs(2)).await; |
| 140 | + tick = tick.wrapping_add(1); |
| 141 | + info!("[adv] notifying connection of tick {}", tick); |
| 142 | + let _ = server.notify(&server.battery_service.level, &conn, &tick).await; |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments