44
55use :: log:: * ;
66
7+ use crate :: private:: common:: Newtype ;
8+ use crate :: private:: mutex;
9+ use crate :: wifi:: EspWifi ;
10+ use embedded_svc:: wifi:: { ClientConfiguration , Configuration , Wifi } ;
11+ use esp_idf_sys:: EspError ;
12+ use esp_idf_sys:: * ;
713use std:: ffi:: { c_char, CStr , CString } ;
814use std:: fmt:: Write ;
915use std:: ops:: Deref ;
1016use std:: ptr;
11- use std:: sync:: mpsc:: { Receiver , sync_channel, SyncSender } ;
12- use embedded_svc:: wifi:: { ClientConfiguration , Configuration , Wifi } ;
13- use esp_idf_sys:: * ;
14- use esp_idf_sys:: EspError ;
15- use crate :: private:: common:: Newtype ;
16- use crate :: private:: mutex;
17- use crate :: wifi:: EspWifi ;
17+ use std:: sync:: mpsc:: { sync_channel, Receiver , SyncSender } ;
1818
1919static EVENTS_TX : mutex:: Mutex < Option < SyncSender < DppEvent > > > =
2020 mutex:: Mutex :: wrap ( mutex:: RawMutex :: new ( ) , None ) ;
@@ -40,10 +40,7 @@ impl<'d, 'w> EspDppBootstrapper<'d, 'w> {
4040 * dpp_event_relay = Some ( events_tx) ;
4141 drop ( dpp_event_relay) ;
4242 esp ! ( unsafe { esp_supp_dpp_init( Some ( Self :: dpp_event_cb_unsafe) ) } ) ?;
43- Ok ( Self {
44- wifi,
45- events_rx,
46- } )
43+ Ok ( Self { wifi, events_rx } )
4744 }
4845
4946 /// Generate a QR code that can be scanned by a mobile phone or other configurator
@@ -64,46 +61,41 @@ impl<'d, 'w> EspDppBootstrapper<'d, 'w> {
6461 & ' b mut self ,
6562 channels : & [ u8 ] ,
6663 key : Option < & [ u8 ; 32 ] > ,
67- associated_data : Option < & [ u8 ] >
64+ associated_data : Option < & [ u8 ] > ,
6865 ) -> Result < EspDppBootstrapped < ' b , QrCode > , EspError > {
69- let mut channels_str = channels. into_iter ( )
70- . fold ( String :: new ( ) , |mut a, c| {
71- write ! ( a, "{c}," ) . unwrap ( ) ;
72- a
73- } ) ;
66+ let mut channels_str = channels. into_iter ( ) . fold ( String :: new ( ) , |mut a, c| {
67+ write ! ( a, "{c}," ) . unwrap ( ) ;
68+ a
69+ } ) ;
7470 channels_str. pop ( ) ;
7571 let channels_cstr = CString :: new ( channels_str) . unwrap ( ) ;
7672 let key_ascii_cstr = key. map ( |k| {
77- let result = k. iter ( )
78- . fold ( String :: new ( ) , |mut a, b| {
79- write ! ( a, "{b:02X}" ) . unwrap ( ) ;
80- a
81- } ) ;
73+ let result = k. iter ( ) . fold ( String :: new ( ) , |mut a, b| {
74+ write ! ( a, "{b:02X}" ) . unwrap ( ) ;
75+ a
76+ } ) ;
8277 CString :: new ( result) . unwrap ( )
8378 } ) ;
8479 let associated_data_cstr = match associated_data {
85- Some ( associated_data) => {
86- Some ( CString :: new ( associated_data)
87- . map_err ( |_| {
88- warn ! ( "associated data contains an embedded NUL character!" ) ;
89- EspError :: from_infallible :: < ESP_ERR_INVALID_ARG > ( )
90- } ) ?)
91- }
80+ Some ( associated_data) => Some ( CString :: new ( associated_data) . map_err ( |_| {
81+ warn ! ( "associated data contains an embedded NUL character!" ) ;
82+ EspError :: from_infallible :: < ESP_ERR_INVALID_ARG > ( )
83+ } ) ?) ,
9284 None => None ,
9385 } ;
9486 debug ! ( "dpp_bootstrap_gen..." ) ;
9587 esp ! ( unsafe {
96- esp_supp_dpp_bootstrap_gen(
97- channels_cstr. as_ptr( ) ,
98- dpp_bootstrap_type_DPP_BOOTSTRAP_QR_CODE,
99- key_ascii_cstr. map_or_else( ptr:: null, |x| x. as_ptr( ) ) ,
100- associated_data_cstr. map_or_else( ptr:: null, |x| x. as_ptr( ) ) )
101- } ) ? ;
102- let event = self . events_rx . recv ( )
103- . map_err ( |_| {
104- warn ! ( "Internal error receiving event!" ) ;
105- EspError :: from_infallible :: < ESP_ERR_INVALID_STATE > ( )
106- } ) ?;
88+ esp_supp_dpp_bootstrap_gen(
89+ channels_cstr. as_ptr( ) ,
90+ dpp_bootstrap_type_DPP_BOOTSTRAP_QR_CODE,
91+ key_ascii_cstr. map_or_else( ptr:: null, |x| x. as_ptr( ) ) ,
92+ associated_data_cstr. map_or_else( ptr:: null, |x| x. as_ptr( ) ) ,
93+ )
94+ } ) ? ;
95+ let event = self . events_rx . recv ( ) . map_err ( |_| {
96+ warn ! ( "Internal error receiving event!" ) ;
97+ EspError :: from_infallible :: < ESP_ERR_INVALID_STATE > ( )
98+ } ) ?;
10799 debug ! ( "dpp_bootstrap_gen got: {event:?}" ) ;
108100 match event {
109101 DppEvent :: UriReady ( qrcode) => {
@@ -117,7 +109,7 @@ impl<'d, 'w> EspDppBootstrapper<'d, 'w> {
117109 _ => {
118110 warn ! ( "Got unexpected event: {event:?}" ) ;
119111 Err ( EspError :: from_infallible :: < ESP_ERR_INVALID_STATE > ( ) )
120- } ,
112+ }
121113 }
122114 }
123115
@@ -126,9 +118,10 @@ impl<'d, 'w> EspDppBootstrapper<'d, 'w> {
126118 Configuration :: Client ( c) => c,
127119 _ => {
128120 let fallback_config = ClientConfiguration :: default ( ) ;
129- self . wifi . set_configuration ( & Configuration :: Client ( fallback_config. clone ( ) ) ) ?;
121+ self . wifi
122+ . set_configuration ( & Configuration :: Client ( fallback_config. clone ( ) ) ) ?;
130123 fallback_config
131- } ,
124+ }
132125 } ;
133126 if !self . wifi . is_started ( ) ? {
134127 self . wifi . start ( ) ?;
@@ -138,19 +131,19 @@ impl<'d, 'w> EspDppBootstrapper<'d, 'w> {
138131
139132 unsafe extern "C" fn dpp_event_cb_unsafe (
140133 evt : esp_supp_dpp_event_t ,
141- data : * mut :: core:: ffi:: c_void
134+ data : * mut :: core:: ffi:: c_void ,
142135 ) {
143136 debug ! ( "dpp_event_cb_unsafe: evt={evt}" ) ;
144137 let event = match evt {
145138 esp_supp_dpp_event_t_ESP_SUPP_DPP_URI_READY => {
146139 DppEvent :: UriReady ( CStr :: from_ptr ( data as * mut c_char ) . to_str ( ) . unwrap ( ) . into ( ) )
147- } ,
140+ }
148141 esp_supp_dpp_event_t_ESP_SUPP_DPP_CFG_RECVD => {
149142 let config = data as * mut wifi_config_t ;
150143 // TODO: We're losing pmf_cfg.required=true setting due to missing
151144 // information in ClientConfiguration.
152145 DppEvent :: ConfigurationReceived ( Newtype ( ( * config) . sta ) . into ( ) )
153- } ,
146+ }
154147 esp_supp_dpp_event_t_ESP_SUPP_DPP_FAIL => {
155148 DppEvent :: Fail ( EspError :: from ( data as esp_err_t ) . unwrap ( ) )
156149 }
@@ -172,7 +165,6 @@ fn dpp_event_cb(event: DppEvent) {
172165 }
173166}
174167
175-
176168#[ derive( Debug ) ]
177169enum DppEvent {
178170 UriReady ( String ) ,
@@ -197,11 +189,10 @@ pub struct QrCode(pub String);
197189impl < ' d , T > EspDppBootstrapped < ' d , T > {
198190 pub fn listen_once ( & self ) -> Result < ClientConfiguration , EspError > {
199191 esp ! ( unsafe { esp_supp_dpp_start_listen( ) } ) ?;
200- let event = self . events_rx . recv ( )
201- . map_err ( |e| {
202- warn ! ( "Internal receive error: {e}" ) ;
203- EspError :: from_infallible :: < ESP_ERR_INVALID_STATE > ( )
204- } ) ?;
192+ let event = self . events_rx . recv ( ) . map_err ( |e| {
193+ warn ! ( "Internal receive error: {e}" ) ;
194+ EspError :: from_infallible :: < ESP_ERR_INVALID_STATE > ( )
195+ } ) ?;
205196 match event {
206197 DppEvent :: ConfigurationReceived ( config) => Ok ( config) ,
207198 DppEvent :: Fail ( e) => Err ( e) ,
0 commit comments