commit b06a845dd6f58f59bca5ba923fa9e6c8f4940c70 from: Thomas Böhler date: Wed Nov 16 16:39:32 2022 UTC Throughput prototype commit - 0e42fcae1d0d628ac79a76bf12c93f422f30584f commit + b06a845dd6f58f59bca5ba923fa9e6c8f4940c70 blob - 3cb3a65422354188e3aef9a3777458049619df61 blob + af860d854abbf8a6d7fba21573fce9a7288741e0 --- Cargo.lock +++ Cargo.lock @@ -60,6 +60,7 @@ dependencies = [ "env_logger", "futures", "log", + "rand", "tokio", "uuid", ] @@ -388,6 +389,17 @@ dependencies = [ ] [[package]] +name = "getrandom" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] name = "hashbrown" version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -514,6 +526,16 @@ dependencies = [ ] [[package]] +name = "num_cpus" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19e64526ebdee182341572e50e9ad03965aa510cd94427a4549448f285e957a1" +dependencies = [ + "hermit-abi", + "libc", +] + +[[package]] name = "objc" version = "0.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -560,6 +582,12 @@ source = "registry+https://github.com/rust-lang/crates checksum = "6ac9a59f73473f1b8d852421e59e64809f025994837ef743615c6d0c5b305160" [[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + +[[package]] name = "proc-macro2" version = "1.0.47" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -578,6 +606,36 @@ dependencies = [ ] [[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] name = "redox_syscall" version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -730,6 +788,7 @@ dependencies = [ "autocfg", "libc", "mio", + "num_cpus", "pin-project-lite", "socket2", "tokio-macros", blob - c238e700ade0b5f78b177f46019ad6ded5f00748 blob + 8b708128304f72ccc81b1b3b18dc9f743b97395a --- Cargo.toml +++ Cargo.toml @@ -10,5 +10,6 @@ btleplug = "0.10.1" env_logger = "0.9.1" futures = "0.3.25" log = "0.4.17" -tokio = { version = "1.21.2", features = ["rt", "macros"] } +tokio = { version = "1.21.2", features = ["rt-multi-thread", "macros"] } uuid = "1.2.1" +rand = "0.8.5" blob - 47b4290f33bd3d2a5f2b36ab46ef73d7bcad3f5f blob + dbc73becf07d8b46149d62ef0e3b66cbd807a1e7 --- src/main.rs +++ src/main.rs @@ -1,4 +1,3 @@ -use futures::stream::StreamExt; use log::*; use std::time::Duration; use tokio::time; @@ -22,12 +21,16 @@ async fn find_device( "Scanning for device ({:?}) with adapter {:?}", mac_addr, adapter ); + // TODO: device should advertise service so that ScanFilter can be used with the service ID adapter.start_scan(ScanFilter::default()).await?; info!("Scanning for 10 seconds"); time::sleep(Duration::from_secs(10)).await; let peripherals = adapter.peripherals().await?; - info!("Found peripherals: {:?}", peripherals); + info!( + "Found peripherals: {:?}", + peripherals.iter().map(|p| p.address()).collect::>() + ); for p in peripherals { let props = p.properties().await?.unwrap(); @@ -50,14 +53,19 @@ async fn handle_buffer_ready( ) { debug!("In handle_buffer_ready"); if !ready { - if let Err(e) = peripheral.unsubscribe(characteristic).await { - error!( - "Unable to unsubscribe from characteristic {:?} of periheral {:?}: {:?}", - characteristic.uuid, - peripheral.address(), - e - ); - } + //info!( + // "Unsubscribing from characteristic {:?}", + // characteristic.uuid + //); + //if let Err(e) = peripheral.unsubscribe(characteristic).await { + // error!( + // "Unable to unsubscribe from characteristic {:?} of periheral {:?}: {:?}", + // characteristic.uuid, + // peripheral.address(), + // e + // ); + //} + info!("Peripheral not ready, exiting handle_buffer_ready"); return; } @@ -73,6 +81,8 @@ async fn handle_buffer_ready( e ); } + + debug!("Done with handle_buffer_ready"); } async fn get_buffer_len( @@ -92,7 +102,7 @@ async fn get_buffer_len( Ok(usize::from_be_bytes(buf)) } -#[tokio::main(flavor = "current_thread")] +#[tokio::main] async fn main() -> anyhow::Result<()> { env_logger::init(); @@ -144,7 +154,7 @@ async fn main() -> anyhow::Result<()> { .iter() .find(|c| c.uuid == BUFFER_READY_UUID) .unwrap(); - esp.subscribe(buffer_ready_char).await?; + //esp.subscribe(buffer_ready_char).await?; // get the buffer length the esp set to know how big the buffer is let buffer_len_char = service @@ -155,15 +165,21 @@ async fn main() -> anyhow::Result<()> { let buffer_len = get_buffer_len(&esp, buffer_len_char).await?; info!("ESP buffer length: {}", buffer_len); - // jumpstart the connection - let buffer_ready_value = esp.read(buffer_ready_char).await?[0] == 1; - handle_buffer_ready(&esp, buffer_ready_char, buffer_ready_value, buffer_len).await; + let data_buffer_char = service + .characteristics + .iter() + .find(|c| c.uuid == DATA_BUFFER_UUID) + .unwrap(); - let mut ns = esp.notifications().await?; - while let Some(n) = ns.next().await { - if n.uuid == BUFFER_READY_UUID { - handle_buffer_ready(&esp, buffer_ready_char, n.value[0] == 1, buffer_len).await; - } + loop { + let buffer: Vec = std::iter::repeat_with(rand::random) + .take(buffer_len) + .collect(); + + // calls to `write` with a `WriteType` of `WithResponse` *should* wait for a response + // before continuing + esp.write(data_buffer_char, &buffer, WriteType::WithResponse) + .await?; } info!("Nothing else to do. Disconnecting");