22
33use super :: gpio:: EspControlInterface ;
44use super :: protocol:: {
5- NinaCommand , NinaParam , NinaProtocolHandler , NinaSmallArrayParam , ProtocolInterface ,
5+ NinaByteParam , NinaCommand , NinaParam , NinaProtocolHandler , NinaSmallArrayParam ,
6+ ProtocolInterface ,
67} ;
78use super :: { Error , FirmwareVersion , WifiCommon , ARRAY_LENGTH_PLACEHOLDER } ;
89
@@ -51,11 +52,16 @@ where
5152 self . common . firmware_version ( )
5253 }
5354
54- /// Joins a WiFi network given an SSID and a Passphrase
55+ /// Joins a WiFi network given an SSID and a Passphrase.
5556 pub fn join ( & mut self , ssid : & str , passphrase : & str ) -> Result < ( ) , Error > {
5657 self . common . join ( ssid, passphrase)
5758 }
5859
60+ /// Disconnects from a joined WiFi network.
61+ pub fn leave ( & mut self ) -> Result < ( ) , Error > {
62+ self . common . leave ( )
63+ }
64+
5965 pub fn get_connection_status ( & mut self ) -> Result < u8 , Error > {
6066 self . common . get_connection_status ( )
6167 }
@@ -118,6 +124,29 @@ where
118124 Ok ( ( ) )
119125 }
120126
127+ fn disconnect ( & mut self ) -> Result < ( ) , self :: Error > {
128+ self . control_pins . wait_for_esp_select ( ) ;
129+
130+ self . send_cmd ( NinaCommand :: Disconnect , 1 ) . ok ( ) . unwrap ( ) ;
131+
132+ let dummy_param = NinaByteParam :: from_bytes ( & [ ControlByte :: Dummy as u8 ] ) ;
133+ self . send_param ( dummy_param) ;
134+
135+ self . send_end_cmd ( ) ;
136+
137+ // Pad byte stream to multiple of 4
138+ self . get_byte ( ) . ok ( ) . unwrap ( ) ;
139+ self . get_byte ( ) . ok ( ) . unwrap ( ) ;
140+
141+ self . control_pins . esp_deselect ( ) ;
142+ self . control_pins . wait_for_esp_select ( ) ;
143+
144+ self . wait_response_cmd ( NinaCommand :: Disconnect , 1 ) ;
145+
146+ self . control_pins . esp_deselect ( ) ;
147+ Ok ( ( ) )
148+ }
149+
121150 fn get_conn_status ( & mut self ) -> Result < u8 , self :: Error > {
122151 self . control_pins . wait_for_esp_select ( ) ;
123152
@@ -171,7 +200,7 @@ where
171200 //return Err(SPIError::Misc);
172201 }
173202
174- let num_params_to_read = self . get_param ( ) ? as usize ;
203+ let num_params_to_read = self . get_byte ( ) ? as usize ;
175204
176205 if num_params_to_read > 8 {
177206 return Ok ( [ 0x31 , 0x2e , 0x37 , 0x2e , 0x34 , 0x0 , 0x0 , 0x0 ] ) ;
@@ -180,7 +209,7 @@ where
180209
181210 let mut params: [ u8 ; ARRAY_LENGTH_PLACEHOLDER ] = [ 0 ; 8 ] ;
182211 for i in 0 ..num_params_to_read {
183- params[ i] = self . get_param ( ) . ok ( ) . unwrap ( )
212+ params[ i] = self . get_byte ( ) . ok ( ) . unwrap ( )
184213 }
185214
186215 self . read_and_check_byte ( ControlByte :: End as u8 ) ?;
@@ -194,7 +223,7 @@ where
194223 Ok ( ( ) )
195224 }
196225
197- fn get_param ( & mut self ) -> Result < u8 , self :: Error > {
226+ fn get_byte ( & mut self ) -> Result < u8 , self :: Error > {
198227 // Blocking read, don't return until we've read a byte successfully
199228 loop {
200229 let word_out = & mut [ ControlByte :: Dummy as u8 ] ;
@@ -214,7 +243,7 @@ where
214243 let mut timeout: u16 = 1000u16 ;
215244
216245 loop {
217- match self . get_param ( ) {
246+ match self . get_byte ( ) {
218247 Ok ( byte_read) => {
219248 if byte_read == ControlByte :: Error as u8 {
220249 return Ok ( false ) ;
@@ -239,7 +268,7 @@ where
239268 }
240269
241270 fn read_and_check_byte ( & mut self , check_byte : u8 ) -> Result < bool , self :: Error > {
242- match self . get_param ( ) {
271+ match self . get_byte ( ) {
243272 Ok ( byte_out) => {
244273 return Ok ( byte_out == check_byte) ;
245274 }
@@ -267,7 +296,7 @@ where
267296
268297 fn pad_to_multiple_of_4 ( & mut self , mut command_size : u16 ) {
269298 while command_size % 4 == 0 {
270- self . get_param ( ) . ok ( ) . unwrap ( ) ;
299+ self . get_byte ( ) . ok ( ) . unwrap ( ) ;
271300 command_size += 1 ;
272301 }
273302 }
0 commit comments