modality_trace_recorder_plugin/streaming/
command.rs

1use derive_more::Display;
2
3/// Trace recorder control-plane commands
4#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Display)]
5pub enum Command {
6    /// CMD_SET_ACTIVE with param1 set to 1, start tracing
7    #[display(fmt = "Start")]
8    Start,
9    /// CMD_SET_ACTIVE with param1 set to 0, stop tracing
10    #[display(fmt = "Stop")]
11    Stop,
12}
13
14impl Command {
15    /// Wire size of a command, equivalent to `sizeof(TracealyzerCommandType)`
16    pub const WIRE_SIZE: usize = 8;
17
18    /// param1 = 1 means start, param1 = 0 means stop
19    const CMD_SET_ACTIVE: u8 = 1;
20
21    fn param1(&self) -> u8 {
22        match self {
23            Command::Start => 1,
24            Command::Stop => 0,
25        }
26    }
27
28    fn checksum(&self) -> u16 {
29        let sum: u16 = u16::from(Self::CMD_SET_ACTIVE)
30              // param2..=param5 are always zero
31              + u16::from(self.param1());
32        0xFFFF_u16.wrapping_sub(sum)
33    }
34
35    /// Convert a command to its `TracealyzerCommandType` wire representation
36    pub fn to_le_bytes(&self) -> [u8; Self::WIRE_SIZE] {
37        let checksum_bytes = self.checksum().to_le_bytes();
38        [
39            Self::CMD_SET_ACTIVE,
40            self.param1(),
41            0,
42            0,
43            0,
44            0,
45            checksum_bytes[0],
46            checksum_bytes[1],
47        ]
48    }
49}