diff options
Diffstat (limited to 'rust/daemon/src/echip/bluetooth.rs')
-rw-r--r-- | rust/daemon/src/echip/bluetooth.rs | 66 |
1 files changed, 31 insertions, 35 deletions
diff --git a/rust/daemon/src/echip/bluetooth.rs b/rust/daemon/src/echip/bluetooth.rs index f9228f57..218ba15f 100644 --- a/rust/daemon/src/echip/bluetooth.rs +++ b/rust/daemon/src/echip/bluetooth.rs @@ -37,12 +37,12 @@ static ECHIP_BT_MUTEX: Mutex<()> = Mutex::new(()); pub type RootcanalIdentifier = u32; -// BLUETOOTH_ECHIPS is a singleton that contains a hash map from -// RootcanalIdentifier to SharedEmulatedChip +// BLUETOOTH_INVALID_PACKETS is a singleton that contains a hash map from +// RootcanalIdentifier to Vec<InvalidPacket> // This singleton is only used when Rootcanal reports invalid // packets by rootcanal_id and we add those to Bluetooth struct. lazy_static! { - static ref BLUETOOTH_ECHIPS: Arc<Mutex<BTreeMap<RootcanalIdentifier, SharedEmulatedChip>>> = + static ref BLUETOOTH_INVALID_PACKETS: Arc<Mutex<BTreeMap<RootcanalIdentifier, Vec<InvalidPacket>>>> = Arc::new(Mutex::new(BTreeMap::new())); } @@ -59,7 +59,6 @@ pub struct Bluetooth { rootcanal_id: RootcanalIdentifier, low_energy_enabled: ProtoState, classic_enabled: ProtoState, - invalid_packets: Vec<InvalidPacket>, } fn patch_state( @@ -135,14 +134,21 @@ impl EmulatedChip for Bluetooth { // Lock to protect id_to_chip_info_ table in C++ let _unused = ECHIP_BT_MUTEX.lock().expect("Failed to acquire lock on ECHIP_BT_MUTEX"); ffi_bluetooth::bluetooth_remove(self.rootcanal_id); + BLUETOOTH_INVALID_PACKETS.lock().expect("invalid packets").remove(&self.rootcanal_id); } fn get_stats(&self, duration_secs: u64) -> Vec<ProtoRadioStats> { // Construct NetsimRadioStats for BLE and Classic. let mut ble_stats_proto = ProtoRadioStats::new(); ble_stats_proto.set_duration_secs(duration_secs); - for invalid_packet in &self.invalid_packets { - ble_stats_proto.invalid_packets.push(invalid_packet.clone()); + if let Some(v) = BLUETOOTH_INVALID_PACKETS + .lock() + .expect("Failed to acquire lock on BLUETOOTH_INVALID_PACKETS") + .get(&self.rootcanal_id) + { + for invalid_packet in v { + ble_stats_proto.invalid_packets.push(invalid_packet.clone()); + } } let mut classic_stats_proto = ble_stats_proto.clone(); @@ -160,24 +166,6 @@ impl EmulatedChip for Bluetooth { } vec![ble_stats_proto, classic_stats_proto] } - - fn report_invalid_packet( - &mut self, - reason: InvalidPacketReason, - description: String, - packet: Vec<u8>, - ) { - // Remove the earliest reported packet if length greater than 5 - if self.invalid_packets.len() >= 5 { - self.invalid_packets.remove(0); - } - // append error packet - let mut invalid_packet = InvalidPacket::new(); - invalid_packet.set_reason(reason); - invalid_packet.set_description(description); - invalid_packet.set_packet(packet); - self.invalid_packets.push(invalid_packet); - } } /// Create a new Emulated Bluetooth Chip @@ -197,11 +185,9 @@ pub fn new(create_params: &CreateParams, chip_id: ChipIdentifier) -> SharedEmula rootcanal_id, low_energy_enabled: ProtoState::ON, classic_enabled: ProtoState::ON, - invalid_packets: Vec::new(), }; - let shared_echip = SharedEmulatedChip(Arc::new(Mutex::new(Box::new(echip)))); - BLUETOOTH_ECHIPS.lock().unwrap().insert(rootcanal_id, shared_echip.clone()); - shared_echip + BLUETOOTH_INVALID_PACKETS.lock().expect("invalid packets").insert(rootcanal_id, Vec::new()); + SharedEmulatedChip(Arc::new(Mutex::new(Box::new(echip)))) } /// Starts the Bluetooth service. @@ -222,13 +208,23 @@ pub fn report_invalid_packet_cxx( description: &CxxString, packet: &CxxVector<u8>, ) { - let reason_enum = InvalidPacketReason::from_i32(reason).unwrap_or(InvalidPacketReason::UNKNOWN); - match BLUETOOTH_ECHIPS.lock().unwrap().get(&rootcanal_id) { - Some(echip) => echip.lock().report_invalid_packet( - reason_enum, - description.to_string(), - packet.as_slice().to_vec(), - ), + match BLUETOOTH_INVALID_PACKETS.lock().unwrap().get_mut(&rootcanal_id) { + Some(v) => { + // Remove the earliest reported packet if length greater than 5 + if v.len() >= 5 { + v.remove(0); + } + // append error packet + let mut invalid_packet = InvalidPacket::new(); + invalid_packet.set_reason( + InvalidPacketReason::from_i32(reason).unwrap_or(InvalidPacketReason::UNKNOWN), + ); + invalid_packet.set_description(description.to_string()); + invalid_packet.set_packet(packet.as_slice().to_vec()); + v.push(invalid_packet); + // Log the report + info!("Reported Invalid Packet for Bluetooth EmulatedChip with rootcanal_id: {rootcanal_id}, reason:{reason}, description: {description:?}, packet: {packet:?}"); + } None => error!("Bluetooth EmulatedChip not created for rootcanal_id: {rootcanal_id}"), } } |