bacnet-endpoint-core 0.15.0

Shared BACnet endpoint ownership and transaction coordination
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};

use bacnet_encoding::apdu::{Apdu, ConfirmedRequest, UnconfirmedRequest, decode_apdu, encode_apdu};
use bacnet_encoding::npdu::{NpduAddress, decode_npdu};
use bacnet_npdu::NetworkPriority;
use bacnet_transport::port::{DataAttribute, ReceivedNpdu, TransportPort};
use bacnet_types::MacAddr;
use bacnet_types::enums::{ConfirmedServiceChoice, UnconfirmedServiceChoice};
use bacnet_types::error::Error;
use bytes::{Bytes, BytesMut};
use tokio::sync::mpsc;
use tokio::time::{Duration, timeout};

use super::*;

const WAIT: Duration = Duration::from_secs(1);
const EFFECTIVE_GROUP_ERROR: &str =
    "effective group destination requires a valid unconfirmed request APDU";

#[derive(Debug, PartialEq, Eq)]
enum LinkDestination {
    Unicast(MacAddr),
    Broadcast,
}

struct CapturedSend {
    npdu: Bytes,
    destination: LinkDestination,
    data_attributes: Vec<DataAttribute>,
}

struct CaptureTransport {
    receiver: Option<mpsc::Receiver<ReceivedNpdu>>,
    sent: mpsc::Sender<CapturedSend>,
    stops: Arc<AtomicUsize>,
    local_mac: MacAddr,
}

struct CaptureHandle {
    _sender: mpsc::Sender<ReceivedNpdu>,
    sent: mpsc::Receiver<CapturedSend>,
    stops: Arc<AtomicUsize>,
}

fn capture_transport() -> (CaptureTransport, CaptureHandle) {
    let (sender, receiver) = mpsc::channel(1);
    let (sent_tx, sent_rx) = mpsc::channel(8);
    let stops = Arc::new(AtomicUsize::new(0));
    (
        CaptureTransport {
            receiver: Some(receiver),
            sent: sent_tx,
            stops: Arc::clone(&stops),
            local_mac: MacAddr::from_slice(&[0xaa]),
        },
        CaptureHandle {
            _sender: sender,
            sent: sent_rx,
            stops,
        },
    )
}

impl CaptureTransport {
    async fn capture(
        &self,
        npdu: &[u8],
        destination: LinkDestination,
        data_attributes: &[DataAttribute],
    ) -> Result<(), Error> {
        self.sent
            .send(CapturedSend {
                npdu: Bytes::copy_from_slice(npdu),
                destination,
                data_attributes: data_attributes.to_vec(),
            })
            .await
            .map_err(|_| Error::Encoding("capture receiver closed".into()))
    }
}

impl TransportPort for CaptureTransport {
    async fn start(&mut self) -> Result<mpsc::Receiver<ReceivedNpdu>, Error> {
        self.receiver
            .take()
            .ok_or_else(|| Error::Encoding("capture transport already started".into()))
    }

    async fn stop(&mut self) -> Result<(), Error> {
        self.stops.fetch_add(1, Ordering::SeqCst);
        Ok(())
    }

    async fn send_unicast(&self, npdu: &[u8], mac: &[u8]) -> Result<(), Error> {
        self.capture(
            npdu,
            LinkDestination::Unicast(MacAddr::from_slice(mac)),
            &[],
        )
        .await
    }

    async fn send_unicast_with_data_attributes(
        &self,
        npdu: &[u8],
        mac: &[u8],
        data_attributes: &[DataAttribute],
    ) -> Result<(), Error> {
        self.capture(
            npdu,
            LinkDestination::Unicast(MacAddr::from_slice(mac)),
            data_attributes,
        )
        .await
    }

    async fn send_broadcast(&self, npdu: &[u8]) -> Result<(), Error> {
        self.capture(npdu, LinkDestination::Broadcast, &[]).await
    }

    async fn send_broadcast_with_data_attributes(
        &self,
        npdu: &[u8],
        data_attributes: &[DataAttribute],
    ) -> Result<(), Error> {
        self.capture(npdu, LinkDestination::Broadcast, data_attributes)
            .await
    }

    fn local_receive_apdu_capacity(&self) -> u16 {
        1476
    }

    fn local_mac(&self) -> &[u8] {
        &self.local_mac
    }

    fn is_broadcast_mac(&self, mac: &[u8]) -> bool {
        mac == BROADCAST_MAC
    }

    /// Its broadcast MAC, and every group form a B/IP link with broadcast
    /// address 192.168.1.255 recognises (#1479).
    fn is_group_destination(&self, mac: &[u8]) -> bool {
        mac == BROADCAST_MAC
            || bacnet_transport::bip::BipTransport::new(
                std::net::Ipv4Addr::LOCALHOST,
                0xBAC0,
                std::net::Ipv4Addr::new(192, 168, 1, 255),
            )
            .is_group_destination(mac)
    }
}

/// The capture link's broadcast MAC, as on MS/TP.
const BROADCAST_MAC: [u8; 1] = [0xff];

/// The B/IP group forms the capture link recognises: the limited broadcast
/// and the configured broadcast IP at this port and another, and IPv4
/// multicast.
const BIP_GROUPS: [[u8; 6]; 5] = [
    [255, 255, 255, 255, 0xBA, 0xC0],
    [255, 255, 255, 255, 0xBA, 0xC1],
    [192, 168, 1, 255, 0xBA, 0xC1],
    [224, 0, 0, 1, 0xBA, 0xC0],
    [239, 255, 255, 250, 0x07, 0x6C],
];

fn encoded_unconfirmed_request() -> Vec<u8> {
    let mut encoded = BytesMut::new();
    encode_apdu(
        &mut encoded,
        &Apdu::UnconfirmedRequest(UnconfirmedRequest {
            service_choice: UnconfirmedServiceChoice::WHO_IS,
            service_request: Bytes::new(),
        }),
    )
    .unwrap();
    encoded.to_vec()
}

fn encoded_confirmed_request() -> Vec<u8> {
    let mut encoded = BytesMut::new();
    encode_apdu(
        &mut encoded,
        &Apdu::ConfirmedRequest(ConfirmedRequest {
            segmented: false,
            more_follows: false,
            segmented_response_accepted: false,
            max_segments: None,
            max_apdu_length: 480,
            invoke_id: 0x41,
            sequence_number: None,
            proposed_window_size: None,
            service_choice: ConfirmedServiceChoice::READ_PROPERTY,
            service_request: Bytes::new(),
        }),
    )
    .unwrap();
    encoded.to_vec()
}

/// One send through the endpoint egress and the wire it must produce.
struct CommandCase {
    destination: EndpointApduDestination,
    expected_link_destination: LinkDestination,
    expected_npdu_destination: Option<NpduAddress>,
    attribute_type: u8,
    expecting_reply: bool,
    priority: NetworkPriority,
}

async fn assert_command(egress: &EndpointEgress, handle: &mut CaptureHandle, case: CommandCase) {
    let CommandCase {
        destination,
        expected_link_destination,
        expected_npdu_destination,
        attribute_type,
        expecting_reply,
        priority,
    } = case;
    let apdu = encoded_unconfirmed_request();
    let data_attributes = vec![DataAttribute {
        option_type: attribute_type,
        must_understand: attribute_type.is_multiple_of(2),
        data: vec![attribute_type, attribute_type.wrapping_add(1)],
    }];

    egress
        .send_apdu(
            apdu.clone(),
            destination,
            expecting_reply,
            priority,
            data_attributes.clone(),
        )
        .await
        .unwrap();

    let captured = timeout(WAIT, handle.sent.recv())
        .await
        .expect("network-service send timed out")
        .expect("capture channel closed");
    assert_eq!(captured.destination, expected_link_destination);
    assert_eq!(captured.data_attributes, data_attributes);
    let decoded = decode_npdu(captured.npdu).unwrap();
    assert_eq!(decoded.destination, expected_npdu_destination);
    assert_eq!(decoded.expecting_reply, expecting_reply);
    assert_eq!(decoded.priority, priority);
    assert_eq!(decoded.payload.as_ref(), apdu);
    assert!(matches!(
        decode_apdu(decoded.payload),
        Ok(Apdu::UnconfirmedRequest(_))
    ));
}

#[tokio::test]
async fn network_service_delegates_every_apdu_destination_with_attributes() {
    let (transport, mut handle) = capture_transport();
    let mut endpoint = EndpointIngress::new(transport, 8);
    let ingress = endpoint.start().await.unwrap();
    let egress = ingress.egress;

    assert_command(
        &egress,
        &mut handle,
        CommandCase {
            destination: EndpointApduDestination::Direct {
                destination_mac: MacAddr::from_slice(&[0x10]),
            },
            expected_link_destination: LinkDestination::Unicast(MacAddr::from_slice(&[0x10])),
            expected_npdu_destination: None,
            attribute_type: 1,
            expecting_reply: true,
            priority: NetworkPriority::Urgent,
        },
    )
    .await;
    let routed_destination = NpduAddress {
        network: 200,
        mac_address: MacAddr::from_slice(&[0x20]),
    };
    assert_command(
        &egress,
        &mut handle,
        CommandCase {
            destination: EndpointApduDestination::Routed {
                destination_network: routed_destination.network,
                destination_mac: routed_destination.mac_address.clone(),
                router_mac: MacAddr::from_slice(&[0x21]),
            },
            expected_link_destination: LinkDestination::Unicast(MacAddr::from_slice(&[0x21])),
            expected_npdu_destination: Some(routed_destination),
            attribute_type: 2,
            expecting_reply: false,
            priority: NetworkPriority::CriticalEquipment,
        },
    )
    .await;
    let unknown_router_destination = NpduAddress {
        network: 300,
        mac_address: MacAddr::from_slice(&[0x30]),
    };
    assert_command(
        &egress,
        &mut handle,
        CommandCase {
            destination: EndpointApduDestination::RoutedViaLocalBroadcast {
                destination_network: unknown_router_destination.network,
                destination_mac: unknown_router_destination.mac_address.clone(),
            },
            expected_link_destination: LinkDestination::Broadcast,
            expected_npdu_destination: Some(unknown_router_destination),
            attribute_type: 3,
            expecting_reply: true,
            priority: NetworkPriority::LifeSafety,
        },
    )
    .await;
    assert_command(
        &egress,
        &mut handle,
        CommandCase {
            destination: EndpointApduDestination::LocalBroadcast,
            expected_link_destination: LinkDestination::Broadcast,
            expected_npdu_destination: None,
            attribute_type: 4,
            expecting_reply: false,
            priority: NetworkPriority::Normal,
        },
    )
    .await;
    assert_command(
        &egress,
        &mut handle,
        CommandCase {
            destination: EndpointApduDestination::RemoteBroadcast {
                destination_network: 400,
            },
            expected_link_destination: LinkDestination::Broadcast,
            expected_npdu_destination: Some(NpduAddress {
                network: 400,
                mac_address: MacAddr::new(),
            }),
            attribute_type: 5,
            expecting_reply: true,
            priority: NetworkPriority::Urgent,
        },
    )
    .await;
    assert_command(
        &egress,
        &mut handle,
        CommandCase {
            destination: EndpointApduDestination::GlobalBroadcast,
            expected_link_destination: LinkDestination::Broadcast,
            expected_npdu_destination: Some(NpduAddress {
                network: 0xffff,
                mac_address: MacAddr::new(),
            }),
            attribute_type: 6,
            expecting_reply: false,
            priority: NetworkPriority::CriticalEquipment,
        },
    )
    .await;

    endpoint.stop().await.unwrap();
    assert_eq!(handle.stops.load(Ordering::SeqCst), 1);
}

#[tokio::test]
async fn effective_group_destinations_reject_confirmed_and_malformed_apdus_without_emission() {
    let (transport, mut handle) = capture_transport();
    let mut endpoint = EndpointIngress::new(transport, 8);
    let ingress = endpoint.start().await.unwrap();
    let egress = ingress.egress;

    for destination in [
        EndpointApduDestination::LocalBroadcast,
        EndpointApduDestination::RemoteBroadcast {
            destination_network: 400,
        },
        EndpointApduDestination::GlobalBroadcast,
    ]
    .into_iter()
    // With no DNET, the link's broadcast MAC, or any other group address, is
    // a local broadcast (#1479).
    .chain(
        std::iter::once(&BROADCAST_MAC[..])
            .chain(BIP_GROUPS.iter().map(|mac| &mac[..]))
            .map(|mac| EndpointApduDestination::Direct {
                destination_mac: MacAddr::from_slice(mac),
            }),
    ) {
        for apdu in [encoded_confirmed_request(), vec![0xff]] {
            assert!(matches!(
                egress
                    .send_apdu(
                        apdu,
                        destination.clone(),
                        false,
                        NetworkPriority::Normal,
                        Vec::new(),
                    )
                    .await,
                Err(Error::Encoding(message)) if message == EFFECTIVE_GROUP_ERROR
            ));
        }
    }
    assert!(matches!(
        handle.sent.try_recv(),
        Err(mpsc::error::TryRecvError::Empty)
    ));

    // An Unconfirmed-Request still goes to the broadcast MAC: this link
    // gives no group rule for the network layer to judge by (#1677).
    egress
        .send_apdu(
            encoded_unconfirmed_request(),
            EndpointApduDestination::Direct {
                destination_mac: MacAddr::from_slice(&BROADCAST_MAC),
            },
            false,
            NetworkPriority::Normal,
            Vec::new(),
        )
        .await
        .unwrap();
    let captured = timeout(WAIT, handle.sent.recv()).await.unwrap().unwrap();
    assert_eq!(
        captured.destination,
        LinkDestination::Unicast(MacAddr::from_slice(&BROADCAST_MAC))
    );

    endpoint.stop().await.unwrap();
    assert_eq!(handle.stops.load(Ordering::SeqCst), 1);
}

/// A routed destination with no DADR is a remote broadcast, so a confirmed
/// request to it is refused by the network layer, naming its PDU type, and
/// nothing goes out (#1479). The routed form through a known router still
/// sends an Unconfirmed-Request there; the local-broadcast form refuses an
/// empty DADR outright and points to the remote-broadcast send.
#[tokio::test]
async fn routed_destinations_without_a_dadr_refuse_all_but_an_unconfirmed_request() {
    let (transport, mut handle) = capture_transport();
    let mut endpoint = EndpointIngress::new(transport, 8);
    let ingress = endpoint.start().await.unwrap();
    let egress = ingress.egress;
    let routed = EndpointApduDestination::Routed {
        destination_network: 400,
        destination_mac: MacAddr::new(),
        router_mac: MacAddr::from_slice(&[0x40]),
    };
    let via_broadcast = EndpointApduDestination::RoutedViaLocalBroadcast {
        destination_network: 400,
        destination_mac: MacAddr::new(),
    };
    let send = |apdu, destination| {
        egress.send_apdu(
            apdu,
            destination,
            false,
            NetworkPriority::Normal,
            Vec::new(),
        )
    };
    for (apdu, destination, refusal) in [
        (
            encoded_confirmed_request(),
            routed.clone(),
            "not PDU type CONFIRMED_REQUEST",
        ),
        (
            encoded_confirmed_request(),
            via_broadcast.clone(),
            "use broadcast_to_network",
        ),
        (
            encoded_unconfirmed_request(),
            via_broadcast,
            "use broadcast_to_network",
        ),
    ] {
        let message = send(apdu, destination).await.unwrap_err().to_string();
        assert!(message.contains(refusal), "{message}");
    }
    assert!(matches!(
        handle.sent.try_recv(),
        Err(mpsc::error::TryRecvError::Empty)
    ));

    send(encoded_unconfirmed_request(), routed).await.unwrap();
    let captured = timeout(WAIT, handle.sent.recv()).await.unwrap().unwrap();
    assert_eq!(
        captured.destination,
        LinkDestination::Unicast(MacAddr::from_slice(&[0x40]))
    );
    let npdu = decode_npdu(captured.npdu).unwrap();
    assert_eq!(
        npdu.destination,
        Some(NpduAddress {
            network: 400,
            mac_address: MacAddr::new(),
        })
    );

    endpoint.stop().await.unwrap();
}

#[tokio::test]
async fn routed_via_local_broadcast_accepts_confirmed_request_for_ultimate_unicast() {
    let (transport, mut handle) = capture_transport();
    let mut endpoint = EndpointIngress::new(transport, 2);
    let ingress = endpoint.start().await.unwrap();
    let egress = ingress.egress;
    let apdu = encoded_confirmed_request();
    let destination = NpduAddress {
        network: 300,
        mac_address: MacAddr::from_slice(&[0x30]),
    };

    egress
        .send_apdu(
            apdu.clone(),
            EndpointApduDestination::RoutedViaLocalBroadcast {
                destination_network: destination.network,
                destination_mac: destination.mac_address.clone(),
            },
            true,
            NetworkPriority::LifeSafety,
            Vec::new(),
        )
        .await
        .unwrap();

    let captured = timeout(WAIT, handle.sent.recv())
        .await
        .expect("routed local-broadcast send timed out")
        .expect("capture channel closed");
    assert_eq!(captured.destination, LinkDestination::Broadcast);
    let npdu = decode_npdu(captured.npdu).unwrap();
    assert_eq!(npdu.destination, Some(destination));
    assert!(npdu.expecting_reply);
    assert_eq!(npdu.priority, NetworkPriority::LifeSafety);
    assert_eq!(npdu.payload.as_ref(), apdu);
    assert!(matches!(
        decode_apdu(npdu.payload),
        Ok(Apdu::ConfirmedRequest(_))
    ));

    endpoint.stop().await.unwrap();
    assert_eq!(handle.stops.load(Ordering::SeqCst), 1);
}

#[test]
fn only_destinations_on_the_known_local_network_are_localized() {
    let station = || MacAddr::from_slice(&[0x30]);
    let named = |network| {
        [
            EndpointApduDestination::Routed {
                destination_network: network,
                destination_mac: station(),
                router_mac: MacAddr::from_slice(&[0x09]),
            },
            EndpointApduDestination::RoutedViaLocalBroadcast {
                destination_network: network,
                destination_mac: station(),
            },
            EndpointApduDestination::RemoteBroadcast {
                destination_network: network,
            },
        ]
    };
    let direct = EndpointApduDestination::Direct {
        destination_mac: station(),
    };
    let [unicast, via_broadcast, broadcast] = named(300);
    assert_eq!(unicast.localized(Some(300)), direct);
    assert_eq!(via_broadcast.localized(Some(300)), direct);
    assert_eq!(
        broadcast.localized(Some(300)),
        EndpointApduDestination::LocalBroadcast
    );
    // Another network, and every network while the number is unknown, keeps
    // its DNET; destinations naming no network never change.
    for local_network in [Some(301), None] {
        for destination in named(300) {
            assert_eq!(destination.clone().localized(local_network), destination);
        }
    }
    for destination in [
        direct.clone(),
        EndpointApduDestination::LocalBroadcast,
        EndpointApduDestination::GlobalBroadcast,
    ] {
        assert_eq!(destination.clone().localized(Some(300)), destination);
    }
}

/// The egress frames each destination as its caller names it, even one
/// naming the published local number: answers keep the route their request
/// arrived by, and only the senders that start traffic localize it (#1403).
#[tokio::test]
async fn the_egress_sends_a_destination_as_named_once_a_number_is_published() {
    let (transport, mut handle) = capture_transport();
    let mut endpoint = EndpointIngress::new(transport, 2);
    let ingress = endpoint.start().await.unwrap();
    let egress = ingress.egress;
    let slot = egress.local_network_number().clone();
    slot.publish(bacnet_types::network_number::NetworkNumber::configured(300).unwrap());
    assert_eq!(egress.local_network_number().get(), Some(300));
    let destination = NpduAddress {
        network: 300,
        mac_address: MacAddr::from_slice(&[0x30]),
    };
    egress
        .send_apdu(
            encoded_confirmed_request(),
            EndpointApduDestination::Routed {
                destination_network: destination.network,
                destination_mac: destination.mac_address.clone(),
                router_mac: MacAddr::from_slice(&[0x09]),
            },
            false,
            NetworkPriority::Normal,
            Vec::new(),
        )
        .await
        .unwrap();
    let captured = timeout(WAIT, handle.sent.recv())
        .await
        .expect("routed send timed out")
        .expect("capture channel closed");
    assert_eq!(
        captured.destination,
        LinkDestination::Unicast(MacAddr::from_slice(&[0x09]))
    );
    assert_eq!(
        decode_npdu(captured.npdu).unwrap().destination,
        Some(destination)
    );

    endpoint.stop().await.unwrap();
}