@@ -316,11 +316,13 @@ where
316316 ) -> Result < [ u8 ; MAX_NINA_RESPONSE_LENGTH ] , Error > {
317317 self . control_pins . wait_for_esp_select ( ) ;
318318
319- let result = self . wait_response_cmd ( & operation. command , expected_num_params) ;
319+ self . check_response_ready ( & operation. command , expected_num_params) ?;
320+
321+ let result = self . read_response ( ) ?;
320322
321323 self . control_pins . esp_deselect ( ) ;
322324
323- result
325+ Ok ( result)
324326 }
325327
326328 fn send_cmd ( & mut self , cmd : & NinaCommand , num_params : u8 ) -> Result < ( ) , Error > {
@@ -341,11 +343,27 @@ where
341343 Ok ( ( ) )
342344 }
343345
344- fn wait_response_cmd (
345- & mut self ,
346- cmd : & NinaCommand ,
347- num_params : u8 ,
348- ) -> Result < [ u8 ; MAX_NINA_RESPONSE_LENGTH ] , Error > {
346+ fn read_response ( & mut self ) -> Result < [ u8 ; MAX_NINA_RESPONSE_LENGTH ] , Error > {
347+ let response_length_in_bytes = self . get_byte ( ) . ok ( ) . unwrap ( ) as usize ;
348+
349+ if response_length_in_bytes > MAX_NINA_PARAMS {
350+ return Err ( ProtocolError :: TooManyParameters . into ( ) ) ;
351+ }
352+
353+ let mut response_param_buffer: [ u8 ; MAX_NINA_RESPONSE_LENGTH ] =
354+ [ 0 ; MAX_NINA_RESPONSE_LENGTH ] ;
355+ if response_length_in_bytes > 0 {
356+ response_param_buffer =
357+ self . read_response_bytes ( response_param_buffer, response_length_in_bytes) ?;
358+ }
359+
360+ let control_byte: u8 = ControlByte :: End as u8 ;
361+ self . read_and_check_byte ( & control_byte) . ok ( ) ;
362+
363+ Ok ( response_param_buffer)
364+ }
365+
366+ fn check_response_ready ( & mut self , cmd : & NinaCommand , num_params : u8 ) -> Result < ( ) , Error > {
349367 self . check_start_cmd ( ) ?;
350368 let byte_to_check: u8 = * cmd as u8 | ControlByte :: Reply as u8 ;
351369 let result = self . read_and_check_byte ( & byte_to_check) . ok ( ) . unwrap ( ) ;
@@ -359,24 +377,17 @@ where
359377 if !result {
360378 return Err ( ProtocolError :: InvalidNumberOfParameters . into ( ) ) ;
361379 }
380+ Ok ( ( ) )
381+ }
362382
363- let number_of_params_to_read = self . get_byte ( ) . ok ( ) . unwrap ( ) as usize ;
364-
365- if number_of_params_to_read > MAX_NINA_PARAMS {
366- return Err ( ProtocolError :: TooManyParameters . into ( ) ) ;
367- }
368-
369- let mut response_param_buffer: [ u8 ; MAX_NINA_RESPONSE_LENGTH ] =
370- [ 0 ; MAX_NINA_RESPONSE_LENGTH ] ;
371- if number_of_params_to_read > 0 {
372- for i in 0 ..number_of_params_to_read {
373- response_param_buffer[ i] = self . get_byte ( ) . ok ( ) . unwrap ( )
374- }
383+ fn read_response_bytes (
384+ & mut self ,
385+ mut response_param_buffer : [ u8 ; MAX_NINA_RESPONSE_LENGTH ] ,
386+ response_length_in_bytes : usize ,
387+ ) -> Result < [ u8 ; MAX_NINA_RESPONSE_LENGTH ] , Error > {
388+ for i in 0 ..response_length_in_bytes {
389+ response_param_buffer[ i] = self . get_byte ( ) . ok ( ) . unwrap ( )
375390 }
376-
377- let control_byte: u8 = ControlByte :: End as u8 ;
378- self . read_and_check_byte ( & control_byte) . ok ( ) ;
379-
380391 Ok ( response_param_buffer)
381392 }
382393
@@ -398,6 +409,9 @@ where
398409 for _ in 0 ..retry_limit {
399410 let byte_read = self . get_byte ( ) . ok ( ) . unwrap ( ) ;
400411 if byte_read == ControlByte :: Error as u8 {
412+ // consume remaining bytes after error: 0x00, 0xEE
413+ self . get_byte ( ) . ok ( ) ;
414+ self . get_byte ( ) . ok ( ) ;
401415 return Err ( ProtocolError :: NinaProtocolVersionMismatch . into ( ) ) ;
402416 } else if byte_read == wait_byte {
403417 return Ok ( true ) ;
0 commit comments