Skip to content

Commit 1032dfc

Browse files
committed
bluetooth: show response times during handshake
1 parent 1323cbd commit 1032dfc

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

src/bluetooth.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use bluer::{
99
use futures::StreamExt;
1010
use simplelog::*;
1111
use std::sync::Arc;
12-
use std::time::Duration;
12+
use std::time::{Duration, Instant};
1313
use tokio::io::AsyncReadExt;
1414
use tokio::io::AsyncWriteExt;
1515
use tokio::sync::Notify;
@@ -250,10 +250,16 @@ async fn send_message(
250250
Ok(stream.write(&packet).await?)
251251
}
252252

253-
async fn read_message(stream: &mut Stream, stage: u8, id: MessageId) -> Result<usize> {
253+
async fn read_message(
254+
stream: &mut Stream,
255+
stage: u8,
256+
id: MessageId,
257+
started: Instant,
258+
) -> Result<usize> {
254259
let mut buf = vec![0; HEADER_LEN];
255260
let n = stream.read_exact(&mut buf).await?;
256261
debug!("received {} bytes: {:02X?}", n, buf);
262+
let elapsed = started.elapsed();
257263

258264
let len: usize = u16::from_be_bytes(buf[0..=1].try_into()?).into();
259265
let message_id = u16::from_be_bytes(buf[2..=3].try_into()?);
@@ -267,8 +273,12 @@ async fn read_message(stream: &mut Stream, stage: u8, id: MessageId) -> Result<u
267273
.into());
268274
}
269275
info!(
270-
"{} 📨 stage #{} of {}: Received <yellow>{:?}</> frame from phone",
271-
NAME, stage, STAGES, id
276+
"{} 📨 stage #{} of {}: Received <yellow>{:?}</> frame from phone (⏱️ {} ms)",
277+
NAME,
278+
stage,
279+
STAGES,
280+
id,
281+
(elapsed.as_secs() * 1_000) + (elapsed.subsec_nanos() / 1_000_000) as u64,
272282
);
273283

274284
// read and discard the remaining bytes
@@ -333,6 +343,7 @@ pub async fn bluetooth_setup_connection(
333343
use WifiInfoResponse::WifiInfoResponse;
334344
use WifiStartRequest::WifiStartRequest;
335345
let mut stage = 1;
346+
let mut started;
336347

337348
let (state, mut stream) = power_up_and_wait_for_connection(advertise, connect).await?;
338349

@@ -342,7 +353,8 @@ pub async fn bluetooth_setup_connection(
342353
start_req.set_port(TCP_SERVER_PORT);
343354
send_message(&mut stream, stage, MessageId::WifiStartRequest, start_req).await?;
344355
stage += 1;
345-
read_message(&mut stream, stage, MessageId::WifiInfoRequest).await?;
356+
started = Instant::now();
357+
read_message(&mut stream, stage, MessageId::WifiInfoRequest, started).await?;
346358

347359
let mut info = WifiInfoResponse::new();
348360
info.set_ssid(String::from(WLAN_SSID));
@@ -357,9 +369,11 @@ pub async fn bluetooth_setup_connection(
357369
stage += 1;
358370
send_message(&mut stream, stage, MessageId::WifiInfoResponse, info).await?;
359371
stage += 1;
360-
read_message(&mut stream, stage, MessageId::WifiStartResponse).await?;
372+
started = Instant::now();
373+
read_message(&mut stream, stage, MessageId::WifiStartResponse, started).await?;
361374
stage += 1;
362-
read_message(&mut stream, stage, MessageId::WifiConnectStatus).await?;
375+
started = Instant::now();
376+
read_message(&mut stream, stage, MessageId::WifiConnectStatus, started).await?;
363377
tcp_start.notify_one();
364378
let _ = stream.shutdown().await?;
365379

0 commit comments

Comments
 (0)