@@ -22,6 +22,7 @@ use protobuf::Message;
22
22
use WifiInfoResponse :: AccessPointType ;
23
23
use WifiInfoResponse :: SecurityMode ;
24
24
const HEADER_LEN : usize = 4 ;
25
+ const STAGES : u8 = 5 ;
25
26
26
27
// module name for logging engine
27
28
const NAME : & str = "<i><bright-black> bluetooth: </>" ;
@@ -225,7 +226,12 @@ async fn power_up_and_wait_for_connection(
225
226
Ok ( ( state, stream) )
226
227
}
227
228
228
- async fn send_message ( stream : & mut Stream , id : MessageId , message : impl Message ) -> Result < usize > {
229
+ async fn send_message (
230
+ stream : & mut Stream ,
231
+ stage : u8 ,
232
+ id : MessageId ,
233
+ message : impl Message ,
234
+ ) -> Result < usize > {
229
235
let mut packet: Vec < u8 > = vec ! [ ] ;
230
236
let mut data = message. write_to_bytes ( ) ?;
231
237
@@ -236,12 +242,15 @@ async fn send_message(stream: &mut Stream, id: MessageId, message: impl Message)
236
242
// append data and send
237
243
packet. append ( & mut data) ;
238
244
239
- info ! ( "{} 📨 Sending <yellow>{:?}</> frame to phone..." , NAME , id) ;
245
+ info ! (
246
+ "{} 📨 stage #{} of {}: Sending <yellow>{:?}</> frame to phone..." ,
247
+ NAME , stage, STAGES , id
248
+ ) ;
240
249
241
250
Ok ( stream. write ( & packet) . await ?)
242
251
}
243
252
244
- async fn read_message ( stream : & mut Stream , id : MessageId ) -> Result < usize > {
253
+ async fn read_message ( stream : & mut Stream , stage : u8 , id : MessageId ) -> Result < usize > {
245
254
let mut buf = vec ! [ 0 ; HEADER_LEN ] ;
246
255
let n = stream. read_exact ( & mut buf) . await ?;
247
256
debug ! ( "received {} bytes: {:02X?}" , n, buf) ;
@@ -257,7 +266,10 @@ async fn read_message(stream: &mut Stream, id: MessageId) -> Result<usize> {
257
266
)
258
267
. into ( ) ) ;
259
268
}
260
- info ! ( "{} 📨 Received <yellow>{:?}</> frame from phone" , NAME , id) ;
269
+ info ! (
270
+ "{} 📨 stage #{} of {}: Received <yellow>{:?}</> frame from phone" ,
271
+ NAME , stage, STAGES , id
272
+ ) ;
261
273
262
274
// read and discard the remaining bytes
263
275
if len > 0 {
@@ -320,15 +332,17 @@ pub async fn bluetooth_setup_connection(
320
332
) -> Result < BluetoothState > {
321
333
use WifiInfoResponse :: WifiInfoResponse ;
322
334
use WifiStartRequest :: WifiStartRequest ;
335
+ let mut stage = 1 ;
323
336
324
337
let ( state, mut stream) = power_up_and_wait_for_connection ( advertise, connect) . await ?;
325
338
326
339
info ! ( "{} 📲 Sending parameters via bluetooth to phone..." , NAME ) ;
327
340
let mut start_req = WifiStartRequest :: new ( ) ;
328
341
start_req. set_ip_address ( String :: from ( WLAN_IP_ADDR ) ) ;
329
342
start_req. set_port ( TCP_SERVER_PORT ) ;
330
- send_message ( & mut stream, MessageId :: WifiStartRequest , start_req) . await ?;
331
- read_message ( & mut stream, MessageId :: WifiInfoRequest ) . await ?;
343
+ send_message ( & mut stream, stage, MessageId :: WifiStartRequest , start_req) . await ?;
344
+ stage += 1 ;
345
+ read_message ( & mut stream, stage, MessageId :: WifiInfoRequest ) . await ?;
332
346
333
347
let mut info = WifiInfoResponse :: new ( ) ;
334
348
info. set_ssid ( String :: from ( WLAN_SSID ) ) ;
@@ -340,9 +354,12 @@ pub async fn bluetooth_setup_connection(
340
354
info. set_bssid ( bssid) ;
341
355
info. set_security_mode ( SecurityMode :: WPA2_PERSONAL ) ;
342
356
info. set_access_point_type ( AccessPointType :: DYNAMIC ) ;
343
- send_message ( & mut stream, MessageId :: WifiInfoResponse , info) . await ?;
344
- read_message ( & mut stream, MessageId :: WifiStartResponse ) . await ?;
345
- read_message ( & mut stream, MessageId :: WifiConnectStatus ) . await ?;
357
+ stage += 1 ;
358
+ send_message ( & mut stream, stage, MessageId :: WifiInfoResponse , info) . await ?;
359
+ stage += 1 ;
360
+ read_message ( & mut stream, stage, MessageId :: WifiStartResponse ) . await ?;
361
+ stage += 1 ;
362
+ read_message ( & mut stream, stage, MessageId :: WifiConnectStatus ) . await ?;
346
363
tcp_start. notify_one ( ) ;
347
364
let _ = stream. shutdown ( ) . await ?;
348
365
0 commit comments