Skip to main content

doip_definitions/doip_payload/
entity_status_response.rs

1use crate::{
2    definitions::{
3        DOIP_ENTITY_STATUS_RESPONSE_MCTS_LEN, DOIP_ENTITY_STATUS_RESPONSE_MDS_LEN,
4        DOIP_ENTITY_STATUS_RESPONSE_NCTS_LEN,
5    },
6    error::{Error, Result},
7    payload::NodeType,
8};
9
10/// Expected reponse from `EntityStatusRequest`.
11///
12/// Containing details of the target of the `EntityStatusRequest`, the
13/// `EntityStatusResponse` provides the program with details pertaining to the
14/// active status of the entity.
15#[cfg_attr(feature = "python-bindings", pyo3::pyclass)]
16#[derive(Copy, Clone, Debug, PartialEq)]
17pub struct EntityStatusResponse {
18    /// The type of entity, either a `Gateway` or `Node`
19    pub node_type: NodeType,
20
21    /// The number of maximum concurrent TCP sockets allowed on this entity
22    pub max_concurrent_sockets: [u8; DOIP_ENTITY_STATUS_RESPONSE_MCTS_LEN],
23
24    /// The number of currently open TCP sockets on the entity
25    pub currently_open_sockets: [u8; DOIP_ENTITY_STATUS_RESPONSE_NCTS_LEN],
26
27    /// The max data size allowed to be sent to the entity
28    pub max_data_size: [u8; DOIP_ENTITY_STATUS_RESPONSE_MDS_LEN],
29}
30
31impl From<EntityStatusResponse>
32    for [u8; 1
33        + DOIP_ENTITY_STATUS_RESPONSE_MCTS_LEN
34        + DOIP_ENTITY_STATUS_RESPONSE_NCTS_LEN
35        + DOIP_ENTITY_STATUS_RESPONSE_MDS_LEN]
36{
37    fn from(value: EntityStatusResponse) -> Self {
38        let mut buffer = [0u8; 1
39            + DOIP_ENTITY_STATUS_RESPONSE_MCTS_LEN
40            + DOIP_ENTITY_STATUS_RESPONSE_NCTS_LEN
41            + DOIP_ENTITY_STATUS_RESPONSE_MDS_LEN];
42        let mut offset = 0;
43
44        buffer[offset] = value.node_type.into();
45        offset += 1;
46
47        #[allow(clippy::range_plus_one)]
48        buffer[offset..offset + DOIP_ENTITY_STATUS_RESPONSE_MCTS_LEN]
49            .copy_from_slice(&value.max_concurrent_sockets);
50        offset += DOIP_ENTITY_STATUS_RESPONSE_MCTS_LEN;
51
52        #[allow(clippy::range_plus_one)]
53        buffer[offset..offset + DOIP_ENTITY_STATUS_RESPONSE_NCTS_LEN]
54            .copy_from_slice(&value.currently_open_sockets);
55        offset += DOIP_ENTITY_STATUS_RESPONSE_NCTS_LEN;
56
57        buffer[offset..offset + DOIP_ENTITY_STATUS_RESPONSE_MDS_LEN]
58            .copy_from_slice(&value.max_data_size);
59
60        buffer
61    }
62}
63
64impl TryFrom<&[u8]> for EntityStatusResponse {
65    type Error = Error;
66
67    fn try_from(value: &[u8]) -> Result<Self> {
68        let mut offset = 0;
69
70        let node_type = value
71            .get(offset)
72            .ok_or(Error::OutOfBounds {
73                source: "EntityStatusResponse",
74                variable: "Node Type",
75            })?
76            .try_into()?;
77
78        let max_concurrent_sockets = value
79            .get(offset..DOIP_ENTITY_STATUS_RESPONSE_MCTS_LEN)
80            .ok_or(Error::OutOfBounds {
81                source: "EntityStatusResponse",
82                variable: "Max Concurrent Sockets",
83            })?
84            .try_into()?;
85
86        offset += DOIP_ENTITY_STATUS_RESPONSE_MCTS_LEN;
87
88        #[allow(clippy::range_plus_one)]
89        let currently_open_sockets = value
90            .get(offset..offset + DOIP_ENTITY_STATUS_RESPONSE_NCTS_LEN)
91            .ok_or(Error::OutOfBounds {
92                source: "EntityStatusResponse",
93                variable: "Currently Open Sockets",
94            })?
95            .try_into()?;
96
97        offset += DOIP_ENTITY_STATUS_RESPONSE_NCTS_LEN;
98
99        let max_data_size = value
100            .get(offset..offset + DOIP_ENTITY_STATUS_RESPONSE_MDS_LEN)
101            .ok_or(Error::OutOfBounds {
102                source: "EntityStatusResponse",
103                variable: "Max Data Size",
104            })?
105            .try_into()?;
106
107        Ok(EntityStatusResponse {
108            node_type,
109            max_concurrent_sockets,
110            currently_open_sockets,
111            max_data_size,
112        })
113    }
114}