19
19
//! [improv-wifi]: https://www.improv-wifi.com
20
20
//! [NetworkManager]: https://www.networkmanager.dev
21
21
22
- use std:: {
23
- sync:: { Arc , RwLock } ,
24
- time:: Duration ,
25
- } ;
22
+ use std:: { sync:: Arc , time:: Duration } ;
26
23
27
24
use bluer:: {
28
25
gatt:: local:: {
@@ -34,7 +31,10 @@ use bluer::{
34
31
} ;
35
32
use error:: Error ;
36
33
use status:: Status ;
37
- use tokio:: sync:: broadcast:: { channel as broadcast_channel, Sender } ;
34
+ use tokio:: sync:: {
35
+ broadcast:: { channel as broadcast_channel, Sender } ,
36
+ RwLock ,
37
+ } ;
38
38
39
39
mod error;
40
40
mod status;
@@ -75,20 +75,20 @@ impl State {
75
75
Self ( Arc :: new ( RwLock :: new ( state) ) )
76
76
}
77
77
78
- pub fn status ( & self ) -> Status {
79
- self . 0 . read ( ) . unwrap ( ) . status
78
+ pub async fn status ( & self ) -> Status {
79
+ self . 0 . read ( ) . await . status
80
80
}
81
81
82
- pub fn last_error ( & self ) -> Option < Error > {
83
- self . 0 . read ( ) . unwrap ( ) . last_error
82
+ pub async fn last_error ( & self ) -> Option < Error > {
83
+ self . 0 . read ( ) . await . last_error
84
84
}
85
85
86
- pub fn set_status ( & self , new : Status ) {
87
- self . 0 . write ( ) . unwrap ( ) . status = new;
86
+ pub async fn set_status ( & self , new : Status ) {
87
+ self . 0 . write ( ) . await . status = new;
88
88
}
89
89
90
- pub fn set_last_error ( & self , new : Option < Error > ) {
91
- self . 0 . write ( ) . unwrap ( ) . last_error = new;
90
+ pub async fn set_last_error ( & self , new : Option < Error > ) {
91
+ self . 0 . write ( ) . await . last_error = new;
92
92
}
93
93
}
94
94
@@ -115,47 +115,47 @@ pub trait WifiConfigurator {
115
115
}
116
116
117
117
impl < T : WifiConfigurator > ImprovWifi < T > {
118
- fn modify_status ( & mut self , status : Status ) {
119
- self . state . set_status ( status) ;
118
+ async fn modify_status ( & mut self , status : Status ) {
119
+ self . state . set_status ( status) . await ;
120
120
self . status_change_notifier . send ( ( ) ) . ok ( ) ;
121
121
// TODO: pro-actively write to the client???
122
122
}
123
123
124
- pub fn set_error ( & mut self , error : Error ) {
125
- self . state . set_last_error ( Some ( error) ) ;
124
+ pub async fn set_error ( & mut self , error : Error ) {
125
+ self . state . set_last_error ( Some ( error) ) . await ;
126
126
self . error_change_notifier . send ( ( ) ) . ok ( ) ;
127
127
// TODO: pro-actively write to the client???
128
128
}
129
129
130
- pub fn clear_error ( & mut self ) {
131
- self . state . set_last_error ( None ) ;
130
+ pub async fn clear_error ( & mut self ) {
131
+ self . state . set_last_error ( None ) . await ;
132
132
self . error_change_notifier . send ( ( ) ) . ok ( ) ;
133
133
// TODO: pro-actively write to the client???
134
134
}
135
135
136
- pub fn set_authorized ( & mut self ) {
137
- if self . state . status ( ) == Status :: AuthorizationRequired {
138
- self . modify_status ( Status :: Authorized ) ;
136
+ pub async fn set_authorized ( & mut self ) {
137
+ if self . state . status ( ) . await == Status :: AuthorizationRequired {
138
+ self . modify_status ( Status :: Authorized ) . await ;
139
139
}
140
140
}
141
141
142
142
pub async fn provision ( & mut self ) {
143
- if self . state . status ( ) != Status :: Authorized {
144
- self . set_error ( Error :: NotAuthorized ) ;
143
+ if self . state . status ( ) . await != Status :: Authorized {
144
+ self . set_error ( Error :: NotAuthorized ) . await ;
145
145
return ;
146
146
}
147
147
148
- self . clear_error ( ) ;
148
+ self . clear_error ( ) . await ;
149
149
150
- self . modify_status ( Status :: Provisioning ) ;
150
+ self . modify_status ( Status :: Provisioning ) . await ;
151
151
152
152
if let Err ( err) = self . handler . provision ( ) . await {
153
- self . set_error ( err) ;
154
- self . modify_status ( Status :: Authorized ) ;
153
+ self . set_error ( err) . await ;
154
+ self . modify_status ( Status :: Authorized ) . await ;
155
155
return ;
156
156
}
157
157
158
- self . modify_status ( Status :: Provisioned ) ;
158
+ self . modify_status ( Status :: Provisioned ) . await ;
159
159
}
160
160
161
161
pub async fn install ( adapter : & Adapter , handler : T ) -> Result < Self > {
@@ -207,7 +207,9 @@ impl<T: WifiConfigurator> ImprovWifi<T> {
207
207
let state = state. clone( ) ;
208
208
move |_| {
209
209
let state = state. clone( ) ;
210
- Box :: pin( async move { Ok ( vec![ state. status( ) . as_byte( ) ] ) } )
210
+ Box :: pin(
211
+ async move { Ok ( vec![ state. status( ) . await . as_byte( ) ] ) } ,
212
+ )
211
213
}
212
214
} ) ,
213
215
..Default :: default ( )
@@ -225,7 +227,7 @@ impl<T: WifiConfigurator> ImprovWifi<T> {
225
227
tokio:: spawn( async move {
226
228
while let Ok ( ( ) ) = status_change_receiver. recv( ) . await {
227
229
notifier
228
- . notify( vec![ state. status( ) . as_byte( ) ] )
230
+ . notify( vec![ state. status( ) . await . as_byte( ) ] )
229
231
. await
230
232
. ok( ) ;
231
233
}
@@ -247,7 +249,10 @@ impl<T: WifiConfigurator> ImprovWifi<T> {
247
249
move |_| {
248
250
let state = state. clone( ) ;
249
251
Box :: pin( async move {
250
- Ok ( vec![ state. last_error( ) . map_or( 0x00 , |s| s. as_byte( ) ) ] )
252
+ Ok ( vec![ state
253
+ . last_error( )
254
+ . await
255
+ . map_or( 0x00 , |s| s. as_byte( ) ) ] )
251
256
} )
252
257
}
253
258
} ) ,
@@ -268,6 +273,7 @@ impl<T: WifiConfigurator> ImprovWifi<T> {
268
273
notifier
269
274
. notify( vec![ state
270
275
. last_error( )
276
+ . await
271
277
. map_or( 0x00 , |s| s. as_byte( ) ) ] )
272
278
. await
273
279
. ok( ) ;
0 commit comments