1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use crate::uavcan::node::port::io_statistics::IoStatistics;
use canadensis_core::ServiceId;
use canadensis_encoding::{
    DataType, Deserialize, DeserializeError, ReadCursor, Request, Response, Serialize, WriteCursor,
};

/// uavcan.node.GetTransportStatistics version 0.1 request
#[derive(Debug, Clone, Default)]
pub struct GetTransportStatisticsRequest;

impl GetTransportStatisticsRequest {
    pub const SERVICE: ServiceId = ServiceId::from_truncating(434);
}

impl DataType for GetTransportStatisticsRequest {
    // Sealed type
    const EXTENT_BYTES: Option<u32> = None;
}

impl Request for GetTransportStatisticsRequest {}

impl Serialize for GetTransportStatisticsRequest {
    fn size_bits(&self) -> usize {
        0
    }

    fn serialize(&self, _cursor: &mut WriteCursor<'_>) {
        // Nothing to do
    }
}

impl Deserialize for GetTransportStatisticsRequest {
    fn in_bit_length_set(bit_length: usize) -> bool {
        bit_length == 0
    }

    fn deserialize_in_place(
        &mut self,
        _cursor: &mut ReadCursor<'_>,
    ) -> Result<(), DeserializeError> {
        // Nothing to do
        Ok(())
    }

    fn deserialize(_cursor: &mut ReadCursor<'_>) -> Result<Self, DeserializeError>
    where
        Self: Sized,
    {
        // Nothing to do
        Ok(GetTransportStatisticsRequest)
    }
}

/// uavcan.node.GetTransportStatistics version 0.1 response
#[derive(Debug, Clone, Default)]
pub struct GetTransportStatisticsResponse {
    pub transfer_statistics: IoStatistics,
    pub network_interface_statistics: heapless::Vec<
        IoStatistics,
        { GetTransportStatisticsResponse::MAX_NETWORK_INTERFACES as usize },
    >,
}

impl GetTransportStatisticsResponse {
    pub const SERVICE: ServiceId = ServiceId::from_truncating(434);
    pub const MAX_NETWORK_INTERFACES: u8 = 3;
}

impl DataType for GetTransportStatisticsResponse {
    const EXTENT_BYTES: Option<u32> = Some(192);
}

impl Response for GetTransportStatisticsResponse {}

impl Serialize for GetTransportStatisticsResponse {
    fn size_bits(&self) -> usize {
        120 + self.network_interface_statistics.len() * 120
    }

    fn serialize(&self, cursor: &mut WriteCursor<'_>) {
        cursor.write_composite(&self.transfer_statistics);
        cursor.write_aligned_u8(self.network_interface_statistics.len() as u8);
        for interface_stats in &self.network_interface_statistics {
            cursor.write_composite(interface_stats);
        }
    }
}

impl Deserialize for GetTransportStatisticsResponse {
    fn in_bit_length_set(bit_length: usize) -> bool {
        bit_length == 120 + 8
            || bit_length == 120 + 8 + 120
            || bit_length == 120 + 8 + 120 * 2
            || bit_length == 120 + 8 + 120 * 3
    }

    fn deserialize_in_place(
        &mut self,
        cursor: &mut ReadCursor<'_>,
    ) -> Result<(), DeserializeError> {
        self.transfer_statistics = cursor.read_composite()?;
        self.network_interface_statistics.clear();
        let length_read = cursor.read_aligned_u8();
        if length_read <= GetTransportStatisticsResponse::MAX_NETWORK_INTERFACES {
            for _ in 0..length_read {
                self.network_interface_statistics
                    .push(cursor.read_composite()?)
                    .expect("Array too long");
            }
            Ok(())
        } else {
            Err(DeserializeError::ArrayLength)
        }
    }

    fn deserialize(cursor: &mut ReadCursor<'_>) -> Result<Self, DeserializeError>
    where
        Self: Sized,
    {
        let mut value = GetTransportStatisticsResponse::default();
        value.deserialize_in_place(cursor)?;
        Ok(value)
    }
}