From 25402a14c284c2cb9f05d93034ae5caa1ede448f Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Wed, 25 Nov 2020 22:04:59 +0100 Subject: Add scan API for scanning for advertisements. --- examples/src/bin/ble_scan.rs | 104 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 examples/src/bin/ble_scan.rs (limited to 'examples/src') diff --git a/examples/src/bin/ble_scan.rs b/examples/src/bin/ble_scan.rs new file mode 100644 index 0000000..970b4ef --- /dev/null +++ b/examples/src/bin/ble_scan.rs @@ -0,0 +1,104 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +#[path = "../example_common.rs"] +mod example_common; +use example_common::*; + +use core::mem; +use core::slice; +use cortex_m_rt::entry; +use defmt::{panic, *}; +use embassy::executor::{task, Executor}; +use embassy::util::Forever; + +use nrf_softdevice::ble::central; +use nrf_softdevice::raw; +use nrf_softdevice::Softdevice; + +static EXECUTOR: Forever = Forever::new(); + +#[task] +async fn softdevice_task(sd: &'static Softdevice) { + sd.run().await; +} + +#[task] +async fn ble_task(sd: &'static Softdevice) { + let config = central::ScanConfig { whitelist: None }; + let res = central::scan(sd, config, |params| unsafe { + info!("AdvReport!"); + info!( + "type: connectable={:u16} scannable={:u16} directed={:u16} scan_response={:u16} extended_pdu={:u16} status={:u16}", + params.type_.connectable(), + params.type_.scannable(), + params.type_.directed(), + params.type_.scan_response(), + params.type_.extended_pdu(), + params.type_.status() + ); + info!( + "addr: resolved={:u8} type={:u8} addr={:[u8]}", + params.peer_addr.addr_id_peer(), + params.peer_addr.addr_type(), + params.peer_addr.addr + ); + info!("data: {:[u8]}", slice::from_raw_parts(params.data.p_data, params.data.len as usize)); + None + }) + .await; + unwrap!(res); + info!("Scan returned"); +} + +#[entry] +fn main() -> ! { + info!("Hello World!"); + + let config = nrf_softdevice::Config { + clock: Some(raw::nrf_clock_lf_cfg_t { + source: raw::NRF_CLOCK_LF_SRC_XTAL as u8, + rc_ctiv: 0, + rc_temp_ctiv: 0, + accuracy: 7, + }), + conn_gap: Some(raw::ble_gap_conn_cfg_t { + conn_count: 6, + event_length: 6, + }), + conn_gatt: Some(raw::ble_gatt_conn_cfg_t { att_mtu: 128 }), + gatts_attr_tab_size: Some(raw::ble_gatts_cfg_attr_tab_size_t { + attr_tab_size: 32768, + }), + gap_role_count: Some(raw::ble_gap_cfg_role_count_t { + adv_set_count: 1, + periph_role_count: 3, + central_role_count: 3, + central_sec_count: 0, + _bitfield_1: raw::ble_gap_cfg_role_count_t::new_bitfield_1(0), + }), + gap_device_name: Some(raw::ble_gap_cfg_device_name_t { + p_value: b"HelloRust" as *const u8 as _, + current_len: 9, + max_len: 9, + write_perm: unsafe { mem::zeroed() }, + _bitfield_1: raw::ble_gap_cfg_device_name_t::new_bitfield_1( + raw::BLE_GATTS_VLOC_STACK as u8, + ), + }), + ..Default::default() + }; + + let (sdp, p) = take_peripherals(); + let sd = Softdevice::enable(sdp, &config); + + let executor = EXECUTOR.put(Executor::new(cortex_m::asm::sev)); + unwrap!(executor.spawn(softdevice_task(sd))); + unwrap!(executor.spawn(ble_task(sd))); + + loop { + executor.run(); + cortex_m::asm::wfe(); + } +} -- cgit v1.2.3