diff --git a/src/device.rs b/src/device.rs index 0558dbc..61547e7 100644 --- a/src/device.rs +++ b/src/device.rs @@ -99,6 +99,11 @@ impl Default for DeviceConfig { } pub struct Device { + /// Flag set when the device has received the Core Device Reset command. + /// The first command received by the device is expected to be Core Device + /// Reset, receiving any other command before this is indicative of a + /// bad host state. + is_reset: bool, pub handle: usize, pub mac_address: MacAddress, config: DeviceConfig, @@ -108,7 +113,6 @@ pub struct Device { pub tx: mpsc::UnboundedSender, pica_tx: mpsc::Sender, country_code: [u8; 2], - pub n_active_sessions: usize, } @@ -122,6 +126,7 @@ impl Device { Device { handle, mac_address, + is_reset: false, config: Default::default(), state: DeviceState::DeviceStateError, // Will be overwitten sessions: Default::default(), @@ -216,18 +221,19 @@ impl Device { log::debug!("[{}] DeviceReset", self.handle); log::debug!(" reset_config={:?}", reset_config); - let status = match reset_config { - ResetConfig::UwbsReset => uci::Status::Ok, - }; *self = Device::new( self.handle, self.mac_address, self.tx.clone(), self.pica_tx.clone(), ); + self.is_reset = true; self.init(); - CoreDeviceResetRspBuilder { status }.build() + CoreDeviceResetRspBuilder { + status: uci::Status::Ok, + } + .build() } fn core_get_device_info(&self, _cmd: CoreGetDeviceInfoCmd) -> CoreGetDeviceInfoRsp { @@ -969,6 +975,17 @@ impl Device { use SessionConfigPacketChild::*; use SessionControlPacketChild::*; + // Check whether the first command received is the Core Device + // Reset command. The controller responds with Device Status + // Notification with DEVICE_STATE_ERROR otherwise. + if !self.is_reset && !cmd.is_core_device_reset_cmd() { + return CoreDeviceStatusNtfBuilder { + device_state: DeviceState::DeviceStateError, + } + .build() + .into(); + } + match cmd.specialize() { CorePacket(cmd) => match cmd.specialize() { CoreDeviceResetCmd(cmd) => self.core_device_reset(cmd).into(), diff --git a/src/packets.rs b/src/packets.rs index 16e7b4f..08b3a82 100644 --- a/src/packets.rs +++ b/src/packets.rs @@ -25,6 +25,18 @@ pub mod uci { include!(concat!(env!("OUT_DIR"), "/uci_packets.rs")); + impl ControlPacket { + pub fn is_core_device_reset_cmd(&self) -> bool { + matches!( + self.controlpacket.child, + ControlPacketDataChild::CorePacket(CorePacketData { + child: CorePacketDataChild::CoreDeviceResetCmd(_), + .. + }) + ) + } + } + /// Size of common UCI packet header. pub const COMMON_HEADER_SIZE: usize = 1; /// Size of UCI packet headers.