Skip to content

Commit 770bceb

Browse files
committed
wip: use tokio rwlock
1 parent eb22538 commit 770bceb

File tree

1 file changed

+38
-32
lines changed

1 file changed

+38
-32
lines changed

crates/improv-wifi/src/lib.rs

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@
1919
//! [improv-wifi]: https://www.improv-wifi.com
2020
//! [NetworkManager]: https://www.networkmanager.dev
2121
22-
use std::{
23-
sync::{Arc, RwLock},
24-
time::Duration,
25-
};
22+
use std::{sync::Arc, time::Duration};
2623

2724
use bluer::{
2825
gatt::local::{
@@ -34,7 +31,10 @@ use bluer::{
3431
};
3532
use error::Error;
3633
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+
};
3838

3939
mod error;
4040
mod status;
@@ -75,20 +75,20 @@ impl State {
7575
Self(Arc::new(RwLock::new(state)))
7676
}
7777

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
8080
}
8181

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
8484
}
8585

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;
8888
}
8989

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;
9292
}
9393
}
9494

@@ -115,47 +115,47 @@ pub trait WifiConfigurator {
115115
}
116116

117117
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;
120120
self.status_change_notifier.send(()).ok();
121121
// TODO: pro-actively write to the client???
122122
}
123123

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;
126126
self.error_change_notifier.send(()).ok();
127127
// TODO: pro-actively write to the client???
128128
}
129129

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;
132132
self.error_change_notifier.send(()).ok();
133133
// TODO: pro-actively write to the client???
134134
}
135135

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;
139139
}
140140
}
141141

142142
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;
145145
return;
146146
}
147147

148-
self.clear_error();
148+
self.clear_error().await;
149149

150-
self.modify_status(Status::Provisioning);
150+
self.modify_status(Status::Provisioning).await;
151151

152152
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;
155155
return;
156156
}
157157

158-
self.modify_status(Status::Provisioned);
158+
self.modify_status(Status::Provisioned).await;
159159
}
160160

161161
pub async fn install(adapter: &Adapter, handler: T) -> Result<Self> {
@@ -207,7 +207,9 @@ impl<T: WifiConfigurator> ImprovWifi<T> {
207207
let state = state.clone();
208208
move |_| {
209209
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+
)
211213
}
212214
}),
213215
..Default::default()
@@ -225,7 +227,7 @@ impl<T: WifiConfigurator> ImprovWifi<T> {
225227
tokio::spawn(async move {
226228
while let Ok(()) = status_change_receiver.recv().await {
227229
notifier
228-
.notify(vec![state.status().as_byte()])
230+
.notify(vec![state.status().await.as_byte()])
229231
.await
230232
.ok();
231233
}
@@ -247,7 +249,10 @@ impl<T: WifiConfigurator> ImprovWifi<T> {
247249
move |_| {
248250
let state = state.clone();
249251
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())])
251256
})
252257
}
253258
}),
@@ -268,6 +273,7 @@ impl<T: WifiConfigurator> ImprovWifi<T> {
268273
notifier
269274
.notify(vec![state
270275
.last_error()
276+
.await
271277
.map_or(0x00, |s| s.as_byte())])
272278
.await
273279
.ok();

0 commit comments

Comments
 (0)