From 8a759f01f5958c25260fcd93b3d6811f78cfe08c Mon Sep 17 00:00:00 2001 From: Heiko Schaefer Date: Fri, 21 Jan 2022 15:54:03 +0100 Subject: [PATCH] Try opening cards three times before failing --- card-functionality/src/cards.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/card-functionality/src/cards.rs b/card-functionality/src/cards.rs index e3dc308..34b5025 100644 --- a/card-functionality/src/cards.rs +++ b/card-functionality/src/cards.rs @@ -9,6 +9,7 @@ use pcsc::ShareMode; use serde_derive::Deserialize; use std::collections::BTreeMap; +use openpgp_card::Error; use openpgp_card_pcsc::PcscCard; use openpgp_card_scdc::ScdClient; @@ -99,7 +100,23 @@ impl TestCard { let res = ScdClient::shutdown_scd(None); log::trace!(" Attempt to shutdown scd: {:?}", res); - Ok(Box::new(PcscCard::open_by_ident(ident, SHARE_MODE)?)) + // Make three attempts to open the card before failing + // (this can be useful in ShareMode::Exclusive) + let mut i = 1; + let card: Result = loop { + let res = PcscCard::open_by_ident(ident, SHARE_MODE); + + if i == 3 || res.is_ok() { + break res; + } + + // sleep for 100ms + std::thread::sleep(std::time::Duration::from_millis(100)); + + i += 1; + }; + + Ok(Box::new(card?)) } Self::Scdc(serial) => { unimplemented!();