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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
use crate::{
    application_protocol::application_pdu::ApplicationPdu,
    common::{
        error::Error,
        io::{Reader, Writer},
    },
};

// Network Layer Protocol Data Unit
#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct NetworkPdu<'a> {
    pub src: Option<SourceAddress>,
    pub dst: Option<DestinationAddress>,
    pub expect_reply: bool,
    pub message_priority: MessagePriority,
    pub network_message: NetworkMessage<'a>,
}

// NOTE: this is actually a control flag
#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[repr(u8)]
pub enum MessagePriority {
    Normal = 0,
    Urgent = 1,
    CriticalEquipment = 2,
    LifeSafety = 3,
}

impl From<u8> for MessagePriority {
    fn from(value: u8) -> Self {
        const MASK: u8 = 0b0000_0011;
        let value = value & MASK;

        match value {
            0 => MessagePriority::Normal,
            1 => MessagePriority::Urgent,
            2 => MessagePriority::CriticalEquipment,
            3 => MessagePriority::LifeSafety,
            _ => unreachable!(), // because of mask
        }
    }
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[repr(u8)]
enum ControlFlags {
    NetworkLayerMessage = 1 << 7,
    HasDestination = 1 << 5,
    HasSource = 1 << 3,
    ExpectingReply = 1 << 2,
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum NetworkMessage<'a> {
    Apdu(ApplicationPdu<'a>),
    MessageType(MessageType),
    CustomMessageType(u8),
}

// Network Layer Message Type
#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[repr(u8)]
pub enum MessageType {
    WhoIsRouterToNetwork = 0,
    IAmRouterToNetwork = 1,
    ICouldBeRouterToNetwork = 2,
    RejectMessageToNetwork = 3,
    RouterBusyToNetwork = 4,
    RouterAvailableToNetwork = 5,
    InitRtTable = 6,
    InitRtTableAck = 7,
    EstablishConnectionToNetwork = 8,
    DisconnectConnectionToNetwork = 9,
    ChallengeRequest = 10,
    SecurityPayload = 11,
    SecurityResponse = 12,
    RequestKeyUpdate = 13,
    UpdateKeySet = 14,
    UpdateDistributionKey = 15,
    RequestMasterKey = 16,
    SetMasterKey = 17,
    WhatIsNetworkNumber = 18,
    NetworkNumberIs = 19,
    // X'14' to X'7F': Reserved for use by ASHRAE
    // X'80' to X'FF': Available for vendor proprietary messages
}

impl TryFrom<u8> for MessageType {
    type Error = u8;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            0 => Ok(Self::WhoIsRouterToNetwork),
            1 => Ok(Self::IAmRouterToNetwork),
            2 => Ok(Self::ICouldBeRouterToNetwork),
            3 => Ok(Self::RejectMessageToNetwork),
            4 => Ok(Self::RouterBusyToNetwork),
            5 => Ok(Self::RouterAvailableToNetwork),
            6 => Ok(Self::InitRtTable),
            7 => Ok(Self::InitRtTableAck),
            8 => Ok(Self::EstablishConnectionToNetwork),
            9 => Ok(Self::DisconnectConnectionToNetwork),
            10 => Ok(Self::ChallengeRequest),
            11 => Ok(Self::SecurityPayload),
            12 => Ok(Self::SecurityResponse),
            13 => Ok(Self::RequestKeyUpdate),
            14 => Ok(Self::UpdateKeySet),
            15 => Ok(Self::UpdateDistributionKey),
            16 => Ok(Self::RequestMasterKey),
            17 => Ok(Self::SetMasterKey),
            18 => Ok(Self::WhatIsNetworkNumber),
            19 => Ok(Self::NetworkNumberIs),
            _ => Err(value),
        }
    }
}

impl<'a> NetworkPdu<'a> {
    const VERSION: u8 = 0x01; // ASHRAE 135-1995
    pub fn new(
        src: Option<SourceAddress>,
        dst: Option<DestinationAddress>,
        expect_reply: bool,
        message_priority: MessagePriority,
        message: NetworkMessage<'a>,
    ) -> Self {
        Self {
            src,
            dst,
            expect_reply,
            message_priority,
            network_message: message,
        }
    }

    pub fn encode(&self, writer: &mut Writer) {
        writer.push(Self::VERSION);
        writer.push(self.calculate_control());

        if let Some(dst) = self.dst.as_ref() {
            dst.network_address.encode(writer);
        }

        if let Some(src) = self.src.as_ref() {
            src.encode(writer);
        }

        // hop count comes after src
        if let Some(dst) = self.dst.as_ref() {
            writer.push(dst.hop_count);
        }

        match &self.network_message {
            NetworkMessage::Apdu(adpu) => adpu.encode(writer),
            NetworkMessage::MessageType(message_type) => {
                writer.push(message_type.clone() as u8);
            }
            NetworkMessage::CustomMessageType(message_type) => {
                writer.push(*message_type);
            }
        };
    }

    fn calculate_control(&self) -> u8 {
        let is_network_layer_message = match &self.network_message {
            NetworkMessage::Apdu(_) => 0,
            NetworkMessage::MessageType(_) => ControlFlags::NetworkLayerMessage as u8,
            NetworkMessage::CustomMessageType(_) => ControlFlags::NetworkLayerMessage as u8,
        };

        let has_destination = match self.dst.as_ref() {
            Some(dst) => {
                if dst.network_address.net > 0 {
                    ControlFlags::HasDestination as u8
                } else {
                    0
                }
            }
            None => 0,
        };

        let has_source = match self.src.as_ref() {
            Some(src) => {
                if src.net > 0 && src.net != 0xFFFF {
                    ControlFlags::HasSource as u8
                } else {
                    0
                }
            }
            None => 0,
        };
        let expecting_reply = if self.expect_reply {
            ControlFlags::ExpectingReply as u8
        } else {
            0
        };
        let message_priority = self.message_priority.clone() as u8;

        is_network_layer_message | has_destination | has_source | expecting_reply | message_priority
    }

    pub fn decode(reader: &mut Reader, buf: &'a [u8]) -> Result<Self, Error> {
        // ignore version
        let _version = reader.read_byte(buf)?;

        // read and decode control byte
        let control = reader.read_byte(buf)?;
        let has_dst = (control & ControlFlags::HasDestination as u8) > 0;
        let has_src = (control & ControlFlags::HasSource as u8) > 0;
        let is_network_message = (control & ControlFlags::NetworkLayerMessage as u8) > 0;
        let expect_reply = (control & ControlFlags::ExpectingReply as u8) > 0;
        let message_priority: MessagePriority = control.into();

        let dst = if has_dst {
            Some(NetworkAddress::decode(reader, buf)?)
        } else {
            None
        };

        let src = if has_src {
            Some(NetworkAddress::decode(reader, buf)?)
        } else {
            None
        };

        // if dst exists then read the hop_count (it comes after src for some reason)
        let dst = if let Some(dst) = dst {
            let hop_count = reader.read_byte(buf)?;
            Some(DestinationAddress {
                network_address: dst,
                hop_count,
            })
        } else {
            None
        };

        let network_message = if is_network_message {
            let message_type = reader.read_byte(buf)?;
            match message_type.try_into() {
                Ok(message_type) => NetworkMessage::MessageType(message_type),
                Err(custom_message_type) => NetworkMessage::CustomMessageType(custom_message_type),
            }
        } else {
            let apdu = ApplicationPdu::decode(reader, buf)?;
            NetworkMessage::Apdu(apdu)
        };

        Ok(Self {
            dst,
            src,
            expect_reply,
            message_priority,
            network_message,
        })
    }
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Addr {
    pub ipv4: [u8; 4],
    pub port: u16,
}

const IPV4_ADDR_LEN: u8 = 6;

pub type SourceAddress = NetworkAddress;

#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct NetworkAddress {
    pub net: u16,
    pub addr: Option<Addr>,
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct DestinationAddress {
    pub network_address: NetworkAddress,
    pub hop_count: u8,
}

impl DestinationAddress {
    pub fn new(net: u16, addr: Option<Addr>) -> Self {
        Self {
            network_address: NetworkAddress { net, addr },
            hop_count: 255,
        }
    }
}

impl NetworkAddress {
    pub fn encode(&self, writer: &mut Writer) {
        writer.extend_from_slice(&self.net.to_be_bytes());
        match self.addr.as_ref() {
            Some(addr) => {
                writer.push(IPV4_ADDR_LEN);
                writer.extend_from_slice(&addr.ipv4);
                writer.extend_from_slice(&addr.port.to_be_bytes());
            }
            None => writer.push(0),
        }
    }

    pub fn decode(reader: &mut Reader, buf: &[u8]) -> Result<Self, Error> {
        let net = u16::from_be_bytes(reader.read_bytes(buf)?);
        let len = reader.read_byte(buf)?;
        match len {
            IPV4_ADDR_LEN => {
                let ipv4: [u8; 4] = reader.read_bytes(buf)?;
                let port = u16::from_be_bytes(reader.read_bytes(buf)?);

                Ok(Self {
                    net,
                    addr: Some(Addr { ipv4, port }),
                })
            }
            0 => Ok(Self { net, addr: None }),
            x => Err(Error::Length((
                "NetworkAddress decode ip len can only be 6 or 0",
                x as u32,
            ))),
        }
    }
}