Skip to content

Commit e23aeb9

Browse files
committed
fix(controller): cancel nfc poll when selecting other future
1 parent 4c2fe54 commit e23aeb9

File tree

5 files changed

+26
-68
lines changed

5 files changed

+26
-68
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ nfc1 = "0.4.0"
2323
serde_json = "1.0.85"
2424
wpactrl = "0.5.1"
2525
tokio-rustls = "0.23.4"
26-
webpki = "0.22.0"
2726
webpki-roots = "0.22.5"
2827
tokio-io-timeout = "1.2.0"
2928
thiserror = "1.0.37"

src/client.rs

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/nfc/thread.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,19 @@ use crate::nfc::reader::{NfcReader, Uid};
1111
pub enum NfcCommand {
1212
Poll {
1313
responder: oneshot::Sender<Uid>,
14+
cancel_rx: oneshot::Receiver<()>,
1415
},
1516
Read {
1617
uid: Uid,
1718
responder: oneshot::Sender<Option<String>>,
1819
},
1920
Release {
2021
responder: oneshot::Sender<()>,
22+
cancel_rx: oneshot::Receiver<()>,
2123
},
2224
}
2325

24-
pub fn start_nfc_listener(
25-
mut nfc_rx: mpsc::Receiver<NfcCommand>,
26-
mut cancel_rx: oneshot::Receiver<()>,
27-
) {
26+
pub fn start_nfc_listener(mut nfc_rx: mpsc::Receiver<NfcCommand>) {
2827
thread::spawn(move || {
2928
let mut context = nfc1::Context::new().unwrap();
3029
let device = context.open().unwrap();
@@ -34,7 +33,10 @@ pub fn start_nfc_listener(
3433
use NfcCommand::*;
3534

3635
match command {
37-
Poll { responder } => {
36+
Poll {
37+
responder,
38+
mut cancel_rx,
39+
} => {
3840
let uid = loop {
3941
if cancel_rx.try_recv() != Err(TryRecvError::Empty) {
4042
return;
@@ -57,7 +59,10 @@ pub fn start_nfc_listener(
5759
_ => responder.send(None).unwrap(),
5860
}
5961
}
60-
Release { responder } => {
62+
Release {
63+
responder,
64+
mut cancel_rx,
65+
} => {
6166
loop {
6267
if cancel_rx.try_recv() != Err(TryRecvError::Empty) {
6368
return;

src/subsystems/controller.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,12 @@ impl Controller {
6666
self.set_idle_led().await?;
6767

6868
let (uid_tx, uid_rx) = oneshot::channel();
69-
nfc.send(NfcCommand::Poll { responder: uid_tx }).await?;
69+
let (_cancel_tx, cancel_rx) = oneshot::channel::<()>();
70+
nfc.send(NfcCommand::Poll {
71+
responder: uid_tx,
72+
cancel_rx,
73+
})
74+
.await?;
7075

7176
tokio::select! {
7277
result = uid_rx => {
@@ -249,8 +254,10 @@ impl Controller {
249254

250255
async fn wait_for_release(&self, nfc: &mpsc::Sender<NfcCommand>) -> Result<()> {
251256
let (released_tx, released_rx) = oneshot::channel();
257+
let (_cancel_tx, cancel_rx) = oneshot::channel::<()>();
252258
nfc.send(NfcCommand::Release {
253259
responder: released_tx,
260+
cancel_rx,
254261
})
255262
.await?;
256263
released_rx.await?;
@@ -266,7 +273,12 @@ impl Controller {
266273
self.led.send(LedState::Blink { color: MAGENTA }).await?;
267274

268275
let (uid_tx, uid_rx) = oneshot::channel();
269-
nfc.send(NfcCommand::Poll { responder: uid_tx }).await?;
276+
let (_cancel_tx, cancel_rx) = oneshot::channel::<()>();
277+
nfc.send(NfcCommand::Poll {
278+
responder: uid_tx,
279+
cancel_rx,
280+
})
281+
.await?;
270282
let uid = uid_rx.await?;
271283

272284
if !config_uids.contains(&uid) {
@@ -290,9 +302,7 @@ impl Controller {
290302
impl IntoSubsystem<Error> for Controller {
291303
async fn run(mut self, subsys: SubsystemHandle) -> Result<()> {
292304
let (nfc_tx, nfc_rx) = mpsc::channel(1);
293-
let (_cancel_nfc_tx, cancel_nfc_rx) = oneshot::channel::<()>();
294-
295-
start_nfc_listener(nfc_rx, cancel_nfc_rx);
305+
start_nfc_listener(nfc_rx);
296306

297307
tokio::select! {
298308
_ = subsys.on_shutdown_requested() => {

0 commit comments

Comments
 (0)