From 7f6fcf3d032d15343951ce9ffa80d04dd7ccdc5f Mon Sep 17 00:00:00 2001 From: bobwzy <35530184+bobwzy@users.noreply.github.com> Date: Mon, 11 Dec 2023 16:58:39 -0800 Subject: [PATCH] Add support to clear device on Pica when host closes hal. (#48) * Add support to clear device on Pica when host closes hal. * Also reset the country code when receiving DeviceResetCmd. --- src/device.rs | 19 +++++++++++++------ src/session.rs | 2 +- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/device.rs b/src/device.rs index 919ffa1..fd18dc8 100644 --- a/src/device.rs +++ b/src/device.rs @@ -149,8 +149,8 @@ impl Device { let status = match reset_config { ResetConfig::UwbsReset => StatusCode::UciStatusOk, }; - *self = Device::new(self.handle, self.tx.clone(), self.pica_tx.clone()); + self.init(); DeviceResetRspBuilder { status }.build() } @@ -298,12 +298,19 @@ impl Device { println!("[{}] Session deinit", self.handle); println!(" session_id=0x{:x}", session_id); - let status = if self.sessions.remove(&session_id).is_some() { - StatusCode::UciStatusOk - } else { - StatusCode::UciStatusSessionNotExist + let status = match self.sessions.get_mut(&session_id) { + Some(session) => { + if session.state == SessionState::SessionStateActive { + self.n_active_sessions -= 1; + if self.n_active_sessions == 0 { + self.set_state(DeviceState::DeviceStateReady); + } + } + self.sessions.remove(&session_id); + StatusCode::UciStatusOk + } + None => StatusCode::UciStatusSessionNotExist, }; - SessionDeinitRspBuilder { status }.build() } diff --git a/src/session.rs b/src/session.rs index a14d7fa..06b97b6 100644 --- a/src/session.rs +++ b/src/session.rs @@ -607,7 +607,7 @@ impl AppConfig { pub struct Session { /// cf. [UCI] 7.1 - state: SessionState, + pub state: SessionState, /// cf. [UCI] 7.2 Table 13: 4 octets unique random number generated by application id: u32, device_handle: usize,