From 965dd146872e41e6da21cfbad3331e5e87990f7b Mon Sep 17 00:00:00 2001 From: ziyiw Date: Tue, 19 Dec 2023 21:19:24 +0000 Subject: [PATCH] Send DeviceResetCmd after task handle is dropped. Previously the command was sent before the task handle is dropped. This may cause race condition that the DeviceResetRsp may be consumed by the reader created in open(). In this case, the consume_device_reset_rsp_and_ntf will wait until timeout. Bug: 316421259 Test: manual Change-Id: I680d3ea79344f9b5f07a36877837bfc1b8e0532a --- uwb/aidl/default/src/uwb_chip.rs | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/uwb/aidl/default/src/uwb_chip.rs b/uwb/aidl/default/src/uwb_chip.rs index d749147d15..d1c3c67f76 100644 --- a/uwb/aidl/default/src/uwb_chip.rs +++ b/uwb/aidl/default/src/uwb_chip.rs @@ -61,6 +61,20 @@ impl State { callbacks.as_binder().unlink_to_death(death_recipient)?; token.cancel(); handle.await.unwrap(); + let packet: UciControlPacket = DeviceResetCmdBuilder { + reset_config: ResetConfig::UwbsReset, + } + .build() + .into(); + // DeviceResetCmd need to be send to reset the device to stop all running + // activities on UWBS. + let packet_vec: Vec = packet.into(); + for hal_packet in packet_vec.into_iter() { + serial + .write(&hal_packet.to_vec()) + .map(|written| written as i32) + .map_err(|_| binder::StatusCode::UNKNOWN_ERROR)?; + } consume_device_reset_rsp_and_ntf( &mut serial .try_clone() @@ -238,21 +252,7 @@ impl IUwbChipAsyncServer for UwbChip { let mut state = self.state.lock().await; - if let State::Opened { ref mut serial, .. } = *state { - let packet: UciControlPacket = DeviceResetCmdBuilder { - reset_config: ResetConfig::UwbsReset, - } - .build() - .into(); - // DeviceResetCmd need to be send to reset the device to stop all running - // activities on UWBS. - let packet_vec: Vec = packet.into(); - for hal_packet in packet_vec.into_iter() { - serial - .write(&hal_packet.to_vec()) - .map(|written| written as i32) - .map_err(|_| binder::StatusCode::UNKNOWN_ERROR)?; - } + if let State::Opened { .. } = *state { state.close().await } else { Err(binder::ExceptionCode::ILLEGAL_STATE.into())