mabi-bacnet 1.5.0

Mabinogion - BACnet/IP simulator
Documentation
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
//! BVLC (BACnet Virtual Link Control) protocol implementation.
//!
//! BVLC provides the virtual link layer for BACnet/IP communications,
//! encapsulating NPDUs for transmission over UDP.

use bytes::{Buf, BufMut, Bytes, BytesMut};
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};

/// BVLC type identifier (always 0x81 for BACnet/IP).
pub const BVLC_TYPE: u8 = 0x81;

/// Minimum BVLC header size.
pub const BVLC_HEADER_SIZE: usize = 4;

/// BACnet/IP port.
pub const BACNET_IP_PORT: u16 = 47808;

/// BVLC Function codes per ASHRAE 135.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum BvlcFunction {
    /// BVLC-Result
    Result = 0x00,
    /// Write-Broadcast-Distribution-Table
    WriteBroadcastDistributionTable = 0x01,
    /// Read-Broadcast-Distribution-Table
    ReadBroadcastDistributionTable = 0x02,
    /// Read-Broadcast-Distribution-Table-Ack
    ReadBroadcastDistributionTableAck = 0x03,
    /// Forwarded-NPDU
    ForwardedNpdu = 0x04,
    /// Register-Foreign-Device
    RegisterForeignDevice = 0x05,
    /// Read-Foreign-Device-Table
    ReadForeignDeviceTable = 0x06,
    /// Read-Foreign-Device-Table-Ack
    ReadForeignDeviceTableAck = 0x07,
    /// Delete-Foreign-Device-Table-Entry
    DeleteForeignDeviceTableEntry = 0x08,
    /// Distribute-Broadcast-To-Network
    DistributeBroadcastToNetwork = 0x09,
    /// Original-Unicast-NPDU
    OriginalUnicastNpdu = 0x0A,
    /// Original-Broadcast-NPDU
    OriginalBroadcastNpdu = 0x0B,
    /// Secure-BVLL
    SecureBvll = 0x0C,
}

impl BvlcFunction {
    /// Try to create from a raw byte value.
    pub fn from_u8(value: u8) -> Option<Self> {
        match value {
            0x00 => Some(Self::Result),
            0x01 => Some(Self::WriteBroadcastDistributionTable),
            0x02 => Some(Self::ReadBroadcastDistributionTable),
            0x03 => Some(Self::ReadBroadcastDistributionTableAck),
            0x04 => Some(Self::ForwardedNpdu),
            0x05 => Some(Self::RegisterForeignDevice),
            0x06 => Some(Self::ReadForeignDeviceTable),
            0x07 => Some(Self::ReadForeignDeviceTableAck),
            0x08 => Some(Self::DeleteForeignDeviceTableEntry),
            0x09 => Some(Self::DistributeBroadcastToNetwork),
            0x0A => Some(Self::OriginalUnicastNpdu),
            0x0B => Some(Self::OriginalBroadcastNpdu),
            0x0C => Some(Self::SecureBvll),
            _ => None,
        }
    }

    /// Check if this function carries an NPDU.
    pub fn has_npdu(&self) -> bool {
        matches!(
            self,
            Self::ForwardedNpdu | Self::OriginalUnicastNpdu | Self::OriginalBroadcastNpdu
        )
    }

    /// Check if this function is a broadcast type.
    pub fn is_broadcast(&self) -> bool {
        matches!(
            self,
            Self::OriginalBroadcastNpdu | Self::DistributeBroadcastToNetwork
        )
    }
}

impl TryFrom<u8> for BvlcFunction {
    type Error = BvlcError;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        Self::from_u8(value).ok_or(BvlcError::UnknownFunction(value))
    }
}

/// BVLC Result codes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u16)]
pub enum BvlcResultCode {
    /// Successful completion
    Success = 0x0000,
    /// Write-Broadcast-Distribution-Table NAK
    WriteBdtNak = 0x0010,
    /// Read-Broadcast-Distribution-Table NAK
    ReadBdtNak = 0x0020,
    /// Register-Foreign-Device NAK
    RegisterForeignDeviceNak = 0x0030,
    /// Read-Foreign-Device-Table NAK
    ReadFdtNak = 0x0040,
    /// Delete-Foreign-Device-Table-Entry NAK
    DeleteFdtEntryNak = 0x0050,
    /// Distribute-Broadcast-To-Network NAK
    DistributeBroadcastNak = 0x0060,
}

impl BvlcResultCode {
    pub fn from_u16(value: u16) -> Option<Self> {
        match value {
            0x0000 => Some(Self::Success),
            0x0010 => Some(Self::WriteBdtNak),
            0x0020 => Some(Self::ReadBdtNak),
            0x0030 => Some(Self::RegisterForeignDeviceNak),
            0x0040 => Some(Self::ReadFdtNak),
            0x0050 => Some(Self::DeleteFdtEntryNak),
            0x0060 => Some(Self::DistributeBroadcastNak),
            _ => None,
        }
    }
}

/// BVLC message header.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BvlcHeader {
    /// BVLC function code.
    pub function: BvlcFunction,
    /// Total message length (including header).
    pub length: u16,
}

impl BvlcHeader {
    /// Create a new BVLC header.
    pub fn new(function: BvlcFunction, length: u16) -> Self {
        Self { function, length }
    }

    /// Encode the header to bytes.
    pub fn encode(&self, buf: &mut BytesMut) {
        buf.put_u8(BVLC_TYPE);
        buf.put_u8(self.function as u8);
        buf.put_u16(self.length);
    }

    /// Decode header from bytes.
    pub fn decode(data: &[u8]) -> Result<Self, BvlcError> {
        if data.len() < BVLC_HEADER_SIZE {
            return Err(BvlcError::TooShort {
                expected: BVLC_HEADER_SIZE,
                actual: data.len(),
            });
        }

        let bvlc_type = data[0];
        if bvlc_type != BVLC_TYPE {
            return Err(BvlcError::InvalidType(bvlc_type));
        }

        let function = BvlcFunction::try_from(data[1])?;
        let length = u16::from_be_bytes([data[2], data[3]]);

        Ok(Self { function, length })
    }
}

/// BVLC message containing header and payload.
#[derive(Debug, Clone)]
pub struct BvlcMessage {
    /// Message header.
    pub header: BvlcHeader,
    /// NPDU payload (for messages that carry NPDUs).
    pub npdu: Vec<u8>,
    /// Original source address (for Forwarded-NPDU).
    pub original_source: Option<SocketAddrV4>,
    /// Result code (for BVLC-Result messages).
    pub result_code: Option<BvlcResultCode>,
}

impl BvlcMessage {
    /// Create a new Original-Unicast-NPDU message.
    pub fn original_unicast(npdu: Vec<u8>) -> Self {
        let length = (BVLC_HEADER_SIZE + npdu.len()) as u16;
        Self {
            header: BvlcHeader::new(BvlcFunction::OriginalUnicastNpdu, length),
            npdu,
            original_source: None,
            result_code: None,
        }
    }

    /// Create a new Original-Broadcast-NPDU message.
    pub fn original_broadcast(npdu: Vec<u8>) -> Self {
        let length = (BVLC_HEADER_SIZE + npdu.len()) as u16;
        Self {
            header: BvlcHeader::new(BvlcFunction::OriginalBroadcastNpdu, length),
            npdu,
            original_source: None,
            result_code: None,
        }
    }

    /// Create a new Forwarded-NPDU message.
    pub fn forwarded_npdu(npdu: Vec<u8>, original_source: SocketAddrV4) -> Self {
        // Forwarded-NPDU includes 6 bytes for original address (4 IP + 2 port)
        let length = (BVLC_HEADER_SIZE + 6 + npdu.len()) as u16;
        Self {
            header: BvlcHeader::new(BvlcFunction::ForwardedNpdu, length),
            npdu,
            original_source: Some(original_source),
            result_code: None,
        }
    }

    /// Create a BVLC-Result message.
    pub fn result(code: BvlcResultCode) -> Self {
        Self {
            header: BvlcHeader::new(BvlcFunction::Result, 6), // 4 header + 2 result code
            npdu: Vec::new(),
            original_source: None,
            result_code: Some(code),
        }
    }

    /// Encode the message to bytes.
    pub fn encode(&self) -> Bytes {
        let mut buf = BytesMut::with_capacity(self.header.length as usize);

        self.header.encode(&mut buf);

        match self.header.function {
            BvlcFunction::Result => {
                if let Some(code) = self.result_code {
                    buf.put_u16(code as u16);
                }
            }
            BvlcFunction::ForwardedNpdu => {
                if let Some(addr) = &self.original_source {
                    buf.put_slice(&addr.ip().octets());
                    buf.put_u16(addr.port());
                }
                buf.put_slice(&self.npdu);
            }
            _ => {
                buf.put_slice(&self.npdu);
            }
        }

        buf.freeze()
    }

    /// Decode a message from bytes.
    pub fn decode(data: &[u8]) -> Result<Self, BvlcError> {
        let header = BvlcHeader::decode(data)?;

        if data.len() < header.length as usize {
            return Err(BvlcError::TooShort {
                expected: header.length as usize,
                actual: data.len(),
            });
        }

        let mut cursor = &data[BVLC_HEADER_SIZE..];

        match header.function {
            BvlcFunction::Result => {
                if cursor.len() < 2 {
                    return Err(BvlcError::TooShort {
                        expected: 2,
                        actual: cursor.len(),
                    });
                }
                let code_value = cursor.get_u16();
                let result_code = BvlcResultCode::from_u16(code_value);

                Ok(Self {
                    header,
                    npdu: Vec::new(),
                    original_source: None,
                    result_code,
                })
            }
            BvlcFunction::ForwardedNpdu => {
                if cursor.len() < 6 {
                    return Err(BvlcError::TooShort {
                        expected: 6,
                        actual: cursor.len(),
                    });
                }

                let ip = Ipv4Addr::new(
                    cursor.get_u8(),
                    cursor.get_u8(),
                    cursor.get_u8(),
                    cursor.get_u8(),
                );
                let port = cursor.get_u16();
                let original_source = SocketAddrV4::new(ip, port);

                Ok(Self {
                    header,
                    npdu: cursor.to_vec(),
                    original_source: Some(original_source),
                    result_code: None,
                })
            }
            BvlcFunction::OriginalUnicastNpdu | BvlcFunction::OriginalBroadcastNpdu => Ok(Self {
                header,
                npdu: cursor.to_vec(),
                original_source: None,
                result_code: None,
            }),
            _ => Ok(Self {
                header,
                npdu: cursor.to_vec(),
                original_source: None,
                result_code: None,
            }),
        }
    }

    /// Get the NPDU payload if present.
    pub fn npdu(&self) -> Option<&[u8]> {
        if self.header.function.has_npdu() && !self.npdu.is_empty() {
            Some(&self.npdu)
        } else {
            None
        }
    }

    /// Check if this is a broadcast message.
    pub fn is_broadcast(&self) -> bool {
        self.header.function.is_broadcast()
    }

    /// Get the effective source address (original source for forwarded, or None).
    pub fn effective_source(&self) -> Option<SocketAddr> {
        self.original_source.map(SocketAddr::V4)
    }
}

/// BVLC protocol errors.
#[derive(Debug, thiserror::Error)]
pub enum BvlcError {
    #[error("Message too short: expected {expected}, got {actual}")]
    TooShort { expected: usize, actual: usize },

    #[error("Invalid BVLC type: 0x{0:02X} (expected 0x81)")]
    InvalidType(u8),

    #[error("Unknown BVLC function: 0x{0:02X}")]
    UnknownFunction(u8),

    #[error("Invalid message format")]
    InvalidFormat,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_bvlc_function_from_u8() {
        assert_eq!(
            BvlcFunction::from_u8(0x0A),
            Some(BvlcFunction::OriginalUnicastNpdu)
        );
        assert_eq!(
            BvlcFunction::from_u8(0x0B),
            Some(BvlcFunction::OriginalBroadcastNpdu)
        );
        assert_eq!(BvlcFunction::from_u8(0xFF), None);
    }

    #[test]
    fn test_bvlc_header_encode_decode() {
        let header = BvlcHeader::new(BvlcFunction::OriginalUnicastNpdu, 10);
        let mut buf = BytesMut::new();
        header.encode(&mut buf);

        let decoded = BvlcHeader::decode(&buf).unwrap();
        assert_eq!(decoded.function, BvlcFunction::OriginalUnicastNpdu);
        assert_eq!(decoded.length, 10);
    }

    #[test]
    fn test_original_unicast_encode_decode() {
        let npdu = vec![0x01, 0x04, 0x00, 0x05]; // Simple NPDU
        let msg = BvlcMessage::original_unicast(npdu.clone());

        let encoded = msg.encode();
        let decoded = BvlcMessage::decode(&encoded).unwrap();

        assert_eq!(decoded.header.function, BvlcFunction::OriginalUnicastNpdu);
        assert_eq!(decoded.npdu, npdu);
    }

    #[test]
    fn test_original_broadcast_encode_decode() {
        let npdu = vec![0x01, 0x04, 0x00, 0x05];
        let msg = BvlcMessage::original_broadcast(npdu.clone());

        let encoded = msg.encode();
        let decoded = BvlcMessage::decode(&encoded).unwrap();

        assert_eq!(decoded.header.function, BvlcFunction::OriginalBroadcastNpdu);
        assert!(decoded.is_broadcast());
    }

    #[test]
    fn test_forwarded_npdu_encode_decode() {
        let npdu = vec![0x01, 0x04, 0x00, 0x05];
        let source = SocketAddrV4::new(Ipv4Addr::new(192, 168, 1, 100), 47808);
        let msg = BvlcMessage::forwarded_npdu(npdu.clone(), source);

        let encoded = msg.encode();
        let decoded = BvlcMessage::decode(&encoded).unwrap();

        assert_eq!(decoded.header.function, BvlcFunction::ForwardedNpdu);
        assert_eq!(decoded.original_source, Some(source));
        assert_eq!(decoded.npdu, npdu);
    }

    #[test]
    fn test_bvlc_result_encode_decode() {
        let msg = BvlcMessage::result(BvlcResultCode::Success);

        let encoded = msg.encode();
        let decoded = BvlcMessage::decode(&encoded).unwrap();

        assert_eq!(decoded.header.function, BvlcFunction::Result);
        assert_eq!(decoded.result_code, Some(BvlcResultCode::Success));
    }
}