@@ -176,6 +176,81 @@ pub async fn get_cpu_serial_number_suffix() -> Result<String> {
176176 Ok ( serial)
177177}
178178
179+ async fn send_message (
180+ stream : & mut Stream ,
181+ stage : u8 ,
182+ id : MessageId ,
183+ message : impl Message ,
184+ ) -> Result < usize > {
185+ let mut packet: Vec < u8 > = vec ! [ ] ;
186+ let mut data = message. write_to_bytes ( ) ?;
187+
188+ // create header: 2 bytes message length + 2 bytes MessageID
189+ packet. write_u16 ( data. len ( ) as u16 ) . await ?;
190+ packet. write_u16 ( id. clone ( ) as u16 ) . await ?;
191+
192+ // append data and send
193+ packet. append ( & mut data) ;
194+
195+ info ! (
196+ "{} 📨 stage #{} of {}: Sending <yellow>{:?}</> frame to phone..." ,
197+ NAME , stage, STAGES , id
198+ ) ;
199+
200+ Ok ( stream. write ( & packet) . await ?)
201+ }
202+
203+ async fn read_message (
204+ stream : & mut Stream ,
205+ stage : u8 ,
206+ id : MessageId ,
207+ started : Instant ,
208+ ) -> Result < usize > {
209+ let mut buf = vec ! [ 0 ; HEADER_LEN ] ;
210+ let n = stream. read_exact ( & mut buf) . await ?;
211+ debug ! ( "received {} bytes: {:02X?}" , n, buf) ;
212+ let elapsed = started. elapsed ( ) ;
213+
214+ let len: usize = u16:: from_be_bytes ( buf[ 0 ..=1 ] . try_into ( ) ?) . into ( ) ;
215+ let message_id = u16:: from_be_bytes ( buf[ 2 ..=3 ] . try_into ( ) ?) ;
216+ debug ! ( "MessageID = {}, len = {}" , message_id, len) ;
217+
218+ if message_id != id. clone ( ) as u16 {
219+ warn ! (
220+ "Received data has invalid MessageID: got: {:?}, expected: {:?}" ,
221+ message_id, id
222+ ) ;
223+ }
224+ info ! (
225+ "{} 📨 stage #{} of {}: Received <yellow>{:?}</> frame from phone (⏱️ {} ms)" ,
226+ NAME ,
227+ stage,
228+ STAGES ,
229+ id,
230+ ( elapsed. as_secs( ) * 1_000 ) + ( elapsed. subsec_nanos( ) / 1_000_000 ) as u64 ,
231+ ) ;
232+
233+ // read and discard the remaining bytes
234+ if len > 0 {
235+ let mut buf = vec ! [ 0 ; len] ;
236+ let n = stream. read_exact ( & mut buf) . await ?;
237+ debug ! ( "remaining {} bytes: {:02X?}" , n, buf) ;
238+
239+ // analyzing WifiConnectStatus
240+ // this is a frame where phone cannot connect to WiFi:
241+ // [08, FD, FF, FF, FF, FF, FF, FF, FF, FF, 01] -> which is -i64::MAX
242+ // and this is where all is fine:
243+ // [08, 00]
244+ if id == MessageId :: WifiConnectStatus && n >= 2 {
245+ if buf[ 1 ] != 0 {
246+ return Err ( "phone cannot connect to our WiFi AP..." . into ( ) ) ;
247+ }
248+ }
249+ }
250+
251+ Ok ( HEADER_LEN + len)
252+ }
253+
179254impl Bluetooth {
180255 pub async fn start_ble ( & mut self , state : AppState , enable_btle : bool ) -> Result < ( ) > {
181256 let mut success;
@@ -358,17 +433,6 @@ impl Bluetooth {
358433 Ok ( ( addr, stream) )
359434 }
360435
361- async fn cleanup_failed_bluetooth_connect ( device : & Device ) -> Result < ( ) > {
362- let cleanup_delay = Duration :: from_secs ( 2 ) ;
363- let _ = timeout ( cleanup_delay, device. disconnect ( ) ) . await ;
364- debug ! (
365- "{} Cleaned up bluetooth connection for device: {:?}" ,
366- NAME ,
367- device. name( ) . await
368- ) ;
369- Ok ( ( ) )
370- }
371-
372436 async fn try_connect_bluetooth_addresses (
373437 adapter : & Adapter ,
374438 dongle_mode : bool ,
@@ -458,84 +522,18 @@ impl Bluetooth {
458522 }
459523 Err ( anyhow ! ( "Unable to connect to the provided addresses" ) . into ( ) )
460524 }
461- }
462-
463- async fn send_message (
464- stream : & mut Stream ,
465- stage : u8 ,
466- id : MessageId ,
467- message : impl Message ,
468- ) -> Result < usize > {
469- let mut packet: Vec < u8 > = vec ! [ ] ;
470- let mut data = message. write_to_bytes ( ) ?;
471-
472- // create header: 2 bytes message length + 2 bytes MessageID
473- packet. write_u16 ( data. len ( ) as u16 ) . await ?;
474- packet. write_u16 ( id. clone ( ) as u16 ) . await ?;
475-
476- // append data and send
477- packet. append ( & mut data) ;
478-
479- info ! (
480- "{} 📨 stage #{} of {}: Sending <yellow>{:?}</> frame to phone..." ,
481- NAME , stage, STAGES , id
482- ) ;
483-
484- Ok ( stream. write ( & packet) . await ?)
485- }
486-
487- async fn read_message (
488- stream : & mut Stream ,
489- stage : u8 ,
490- id : MessageId ,
491- started : Instant ,
492- ) -> Result < usize > {
493- let mut buf = vec ! [ 0 ; HEADER_LEN ] ;
494- let n = stream. read_exact ( & mut buf) . await ?;
495- debug ! ( "received {} bytes: {:02X?}" , n, buf) ;
496- let elapsed = started. elapsed ( ) ;
497-
498- let len: usize = u16:: from_be_bytes ( buf[ 0 ..=1 ] . try_into ( ) ?) . into ( ) ;
499- let message_id = u16:: from_be_bytes ( buf[ 2 ..=3 ] . try_into ( ) ?) ;
500- debug ! ( "MessageID = {}, len = {}" , message_id, len) ;
501525
502- if message_id != id. clone ( ) as u16 {
503- warn ! (
504- "Received data has invalid MessageID: got: {:?}, expected: {:?}" ,
505- message_id, id
526+ async fn cleanup_failed_bluetooth_connect ( device : & Device ) -> Result < ( ) > {
527+ let cleanup_delay = Duration :: from_secs ( 2 ) ;
528+ let _ = timeout ( cleanup_delay, device. disconnect ( ) ) . await ;
529+ debug ! (
530+ "{} Cleaned up bluetooth connection for device: {:?}" ,
531+ NAME ,
532+ device. name( ) . await
506533 ) ;
507- }
508- info ! (
509- "{} 📨 stage #{} of {}: Received <yellow>{:?}</> frame from phone (⏱️ {} ms)" ,
510- NAME ,
511- stage,
512- STAGES ,
513- id,
514- ( elapsed. as_secs( ) * 1_000 ) + ( elapsed. subsec_nanos( ) / 1_000_000 ) as u64 ,
515- ) ;
516-
517- // read and discard the remaining bytes
518- if len > 0 {
519- let mut buf = vec ! [ 0 ; len] ;
520- let n = stream. read_exact ( & mut buf) . await ?;
521- debug ! ( "remaining {} bytes: {:02X?}" , n, buf) ;
522-
523- // analyzing WifiConnectStatus
524- // this is a frame where phone cannot connect to WiFi:
525- // [08, FD, FF, FF, FF, FF, FF, FF, FF, FF, 01] -> which is -i64::MAX
526- // and this is where all is fine:
527- // [08, 00]
528- if id == MessageId :: WifiConnectStatus && n >= 2 {
529- if buf[ 1 ] != 0 {
530- return Err ( "phone cannot connect to our WiFi AP..." . into ( ) ) ;
531- }
532- }
534+ Ok ( ( ) )
533535 }
534536
535- Ok ( HEADER_LEN + len)
536- }
537-
538- impl Bluetooth {
539537 pub async fn bluetooth_stop ( & mut self ) -> Result < ( ) > {
540538 //info!("{} 🥷 Unregistering default agent", NAME);
541539 //drop(state.handle_agent);
0 commit comments