Skip to main content

doip_definitions/doip_payload/
routing_activation_response.rs

1use crate::{
2    definitions::{
3        DOIP_ROUTING_ACTIVATION_RES_ENTITY_LEN, DOIP_ROUTING_ACTIVATION_RES_ISO_LEN,
4        DOIP_ROUTING_ACTIVATION_RES_TESTER_LEN,
5    },
6    error::{Error, Result},
7    payload::ActivationCode,
8};
9
10/// A response to the `RoutingActivationRequest`.
11///
12/// Contains the logical address of the recieved `DoIP` entity along with the activation code.
13#[cfg_attr(feature = "python-bindings", pyo3::pyclass)]
14#[derive(Copy, Clone, Debug, PartialEq)]
15pub struct RoutingActivationResponse {
16    /// Logical address of requested entity
17    pub logical_address: [u8; DOIP_ROUTING_ACTIVATION_RES_TESTER_LEN],
18
19    /// Source address of response entity
20    pub source_address: [u8; DOIP_ROUTING_ACTIVATION_RES_ENTITY_LEN],
21
22    /// Activation Code
23    pub activation_code: ActivationCode,
24
25    /// ISO reserved buffer
26    pub buffer: [u8; DOIP_ROUTING_ACTIVATION_RES_ISO_LEN],
27}
28
29impl From<RoutingActivationResponse>
30    for [u8; DOIP_ROUTING_ACTIVATION_RES_TESTER_LEN
31        + DOIP_ROUTING_ACTIVATION_RES_ENTITY_LEN
32        + 1
33        + DOIP_ROUTING_ACTIVATION_RES_ISO_LEN]
34{
35    fn from(value: RoutingActivationResponse) -> Self {
36        let mut buffer = [0u8; DOIP_ROUTING_ACTIVATION_RES_TESTER_LEN
37            + DOIP_ROUTING_ACTIVATION_RES_ENTITY_LEN
38            + 1
39            + DOIP_ROUTING_ACTIVATION_RES_ISO_LEN];
40
41        let mut offset = 0;
42
43        buffer[offset..offset + DOIP_ROUTING_ACTIVATION_RES_TESTER_LEN]
44            .copy_from_slice(&value.logical_address);
45        offset += DOIP_ROUTING_ACTIVATION_RES_TESTER_LEN;
46
47        buffer[offset..offset + DOIP_ROUTING_ACTIVATION_RES_ENTITY_LEN]
48            .copy_from_slice(&value.source_address);
49        offset += DOIP_ROUTING_ACTIVATION_RES_ENTITY_LEN;
50
51        buffer[offset] = value.activation_code.into();
52        offset += 1;
53
54        buffer[offset..offset + DOIP_ROUTING_ACTIVATION_RES_ISO_LEN].copy_from_slice(&value.buffer);
55
56        buffer
57    }
58}
59
60impl TryFrom<&[u8]> for RoutingActivationResponse {
61    type Error = Error;
62
63    fn try_from(value: &[u8]) -> Result<Self> {
64        let mut offset = 0;
65
66        let logical_address = value
67            .get(offset..DOIP_ROUTING_ACTIVATION_RES_TESTER_LEN)
68            .ok_or(Error::OutOfBounds {
69                source: "RoutingActivationResponse",
70                variable: "Logical Address",
71            })?
72            .try_into()?;
73
74        offset += DOIP_ROUTING_ACTIVATION_RES_TESTER_LEN;
75
76        let source_address = value
77            .get(offset..offset + DOIP_ROUTING_ACTIVATION_RES_ENTITY_LEN)
78            .ok_or(Error::OutOfBounds {
79                source: "RoutingActivationResponse",
80                variable: "Source Address",
81            })?
82            .try_into()?;
83
84        offset += DOIP_ROUTING_ACTIVATION_RES_ENTITY_LEN;
85
86        let activation_code = value
87            .get(offset)
88            .ok_or(Error::OutOfBounds {
89                source: "RoutingActivationResponse",
90                variable: "Activation Code",
91            })?
92            .try_into()?;
93
94        offset += 1;
95
96        let buffer = value
97            .get(offset..offset + DOIP_ROUTING_ACTIVATION_RES_ISO_LEN)
98            .ok_or(Error::OutOfBounds {
99                source: "RoutingActivationResponse",
100                variable: "Buffer",
101            })?
102            .try_into()?;
103
104        Ok(RoutingActivationResponse {
105            logical_address,
106            source_address,
107            activation_code,
108            buffer,
109        })
110    }
111}