Skip to content

Commit da839f3

Browse files
get_metadata is unreliable and can fail to parse the response. Retry a few times
1 parent 3ac5c3a commit da839f3

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/lib.rs

+21-2
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,31 @@ impl Ppk2 {
9797
Ok(response)
9898
}
9999

100-
/// Get the device metadata.
101-
pub fn get_metadata(&mut self) -> Result<Metadata> {
100+
fn try_get_metadata(&mut self) -> Result<Metadata> {
102101
let response = self.send_command(Command::GetMetaData)?;
103102
Metadata::from_bytes(&response)
104103
}
105104

105+
/// Get the device metadata.
106+
pub fn get_metadata(&mut self) -> Result<Metadata> {
107+
let mut result: Result<Metadata> = Err(Error::Parse("Metadata".to_string()));
108+
109+
// Retry a few times, as the metadata command sometimes fails
110+
for _ in 0..3 {
111+
match self.try_get_metadata() {
112+
Ok(metadata) => {
113+
result = Ok(metadata);
114+
break;
115+
}
116+
Err(e) => {
117+
tracing::warn!("Error fetching metadata: {:?}. Retrying..", e);
118+
}
119+
}
120+
}
121+
122+
result
123+
}
124+
106125
/// Enable or disable the device power.
107126
pub fn set_device_power(&mut self, power: DevicePower) -> Result<()> {
108127
self.send_command(Command::DeviceRunningSet(power))?;

0 commit comments

Comments
 (0)