Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions dispatch/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
//!
//! Apps need to implement the App trait to be managed.
//!
use core::mem;

use crate::response::SIZE as ResponseSize;
use crate::App;
use crate::{
Expand Down Expand Up @@ -381,14 +383,6 @@ impl<'pipe> ApduDispatch<'pipe> {
// not necessarily the case for other apps

// if there is a selected app with a different AID, deselect it
if let Some(current_aid) = self.current_aid {
if current_aid != aid {
let app = Self::find_app(self.current_aid.as_ref(), apps).unwrap();
// for now all apps will be happy with this.
app.deselect();
self.current_aid = None;
}
}

// select specified app in any case
if let Some(app) = Self::find_app(Some(&aid), apps) {
Expand All @@ -401,7 +395,14 @@ impl<'pipe> ApduDispatch<'pipe> {
_ => panic!("Unexpected buffer state."),
};

self.current_aid = Some(aid);
let old_aid = mem::replace(&mut self.current_aid, Some(aid));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Can we use Option::replace instead? IMHO easier to read.

Suggested change
let old_aid = mem::replace(&mut self.current_aid, Some(aid));
let old_aid = self.current_aid.replace(aid);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Probably should be a clippy lint.

if let Some(old_aid) = old_aid {
if old_aid != aid {
let app = Self::find_app(self.current_aid.as_ref(), apps).unwrap();
// for now all apps will be happy with this.
app.deselect();
}
}

self.handle_app_response(&result, &response);
} else {
Expand Down
19 changes: 19 additions & 0 deletions dispatch/tests/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,25 @@ fn select_not_found() {
])
}

#[test]
#[serial]
fn select_not_found_still_selected() {
run_apdus(&[
// Select
&hex!("00A40400 05 0A01000001"),
// Ok
&hex!("9000"),
// Select
&hex!("00A40400 05 0A01000100"),
// Not found
&hex!("6A82"),
// Echo app is still selected
&hex!("80100000 05 0102030405 00"),
// Echo + Ok
&hex!("0000000000 0102030405 9000"),
])
}

#[test]
#[serial]
fn select_bad_aid() {
Expand Down
Loading