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
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};

use bacnet_encoding::npdu::{Npdu, NpduAddress, decode_npdu, encode_npdu};
use bacnet_npdu::NetworkPriority;
use bacnet_transport::loopback::LoopbackTransport;
use bacnet_transport::port::{DataAttribute, ReceivedNpdu, TransportPort, TransportProvenance};
use bacnet_types::MacAddr;
use bacnet_types::error::Error;
use bytes::{Bytes, BytesMut};
use tokio::sync::{Notify, mpsc, oneshot};
use tokio::time::{Duration, timeout};

use super::*;

const WAIT: Duration = Duration::from_secs(1);

struct TestTransport {
    receiver: Option<mpsc::Receiver<ReceivedNpdu>>,
    sent: mpsc::Sender<(Vec<u8>, Vec<u8>)>,
    starts: Arc<AtomicUsize>,
    stops: Arc<AtomicUsize>,
    local_mac: MacAddr,
}

struct TestTransportHandle {
    sender: mpsc::Sender<ReceivedNpdu>,
    sent: mpsc::Receiver<(Vec<u8>, Vec<u8>)>,
    starts: Arc<AtomicUsize>,
    stops: Arc<AtomicUsize>,
}

fn test_transport() -> (TestTransport, TestTransportHandle) {
    let (sender, receiver) = mpsc::channel(32);
    let (sent_tx, sent_rx) = mpsc::channel(32);
    let starts = Arc::new(AtomicUsize::new(0));
    let stops = Arc::new(AtomicUsize::new(0));
    (
        TestTransport {
            receiver: Some(receiver),
            sent: sent_tx,
            starts: Arc::clone(&starts),
            stops: Arc::clone(&stops),
            local_mac: MacAddr::from_slice(&[0xaa]),
        },
        TestTransportHandle {
            sender,
            sent: sent_rx,
            starts,
            stops,
        },
    )
}

impl TransportPort for TestTransport {
    async fn start(&mut self) -> Result<mpsc::Receiver<ReceivedNpdu>, Error> {
        self.starts.fetch_add(1, Ordering::SeqCst);
        self.receiver
            .take()
            .ok_or_else(|| Error::Encoding("test 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.sent
            .send((npdu.to_vec(), mac.to_vec()))
            .await
            .map_err(|_| Error::Encoding("test sent-frame receiver closed".into()))
    }

    async fn send_broadcast(&self, _npdu: &[u8]) -> Result<(), Error> {
        Ok(())
    }

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

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

    /// MAC 0xFF reaches a group of nodes.
    fn is_group_destination(&self, mac: &[u8]) -> bool {
        mac == [0xFF]
    }
}

struct BlockingTransport {
    receiver: Option<mpsc::Receiver<ReceivedNpdu>>,
    entered_send: Arc<Notify>,
    stops: Arc<AtomicUsize>,
    local_mac: MacAddr,
}

struct BlockingTransportHandle {
    sender: mpsc::Sender<ReceivedNpdu>,
    entered_send: Arc<Notify>,
    stops: Arc<AtomicUsize>,
}

fn blocking_transport() -> (BlockingTransport, BlockingTransportHandle) {
    let (sender, receiver) = mpsc::channel(1);
    let entered_send = Arc::new(Notify::new());
    let stops = Arc::new(AtomicUsize::new(0));
    (
        BlockingTransport {
            receiver: Some(receiver),
            entered_send: Arc::clone(&entered_send),
            stops: Arc::clone(&stops),
            local_mac: MacAddr::from_slice(&[0xaa]),
        },
        BlockingTransportHandle {
            sender,
            entered_send,
            stops,
        },
    )
}

impl TransportPort for BlockingTransport {
    async fn start(&mut self) -> Result<mpsc::Receiver<ReceivedNpdu>, Error> {
        self.receiver
            .take()
            .ok_or_else(|| Error::Encoding("blocking 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.entered_send.notify_one();
        std::future::pending::<()>().await;
        unreachable!("blocking test send is cancelled by endpoint stop")
    }

    async fn send_broadcast(&self, _npdu: &[u8]) -> Result<(), Error> {
        Ok(())
    }

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

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

fn npdu_bytes(apdu: &[u8]) -> Bytes {
    let npdu = Npdu {
        expecting_reply: false,
        priority: NetworkPriority::Normal,
        payload: Bytes::copy_from_slice(apdu),
        ..Npdu::default()
    };
    let mut buffer = BytesMut::new();
    encode_npdu(&mut buffer, &npdu).unwrap();
    buffer.freeze()
}

fn received_npdu(apdu: &[u8]) -> ReceivedNpdu {
    ReceivedNpdu {
        direct_response: None,
        npdu: npdu_bytes(apdu),
        source_mac: MacAddr::from_slice(&[0x11]),
        link_layer_group: false,
        data_attributes: Vec::new(),
        provenance: TransportProvenance::unverified(),
        reply_tx: None,
    }
}

async fn inject(handle: &TestTransportHandle, apdu: &[u8]) {
    handle.sender.send(received_npdu(apdu)).await.unwrap();
}

async fn receive<T>(receiver: &mut mpsc::Receiver<T>) -> T {
    timeout(WAIT, receiver.recv())
        .await
        .expect("ingress receive timed out")
        .expect("ingress route closed")
}

#[tokio::test]
async fn loopback_routes_all_apdu_classes_once_and_in_order() {
    let (transport, peer) = LoopbackTransport::pair(vec![0x01], vec![0x02]);
    let mut endpoint = EndpointIngress::new(transport, 8);
    let mut ingress = endpoint.start().await.unwrap();
    let apdus: [&[u8]; 8] = [
        &[0x00, 0x05, 0x11, 0x0c],
        &[0x20, 0x12, 0x0c],
        &[0x10, 0x08],
        &[0x30, 0x13, 0x0c],
        &[0x50, 0x14, 0x0c, 0x91, 0x00, 0x91, 0x00],
        &[0x60, 0x15, 0x00],
        &[0x70, 0x16, 0x00],
        &[0x40, 0x17, 0x00, 0x01],
    ];

    for apdu in apdus {
        peer.send_unicast(&npdu_bytes(apdu), &[0x01]).await.unwrap();
    }

    let mut inbound_types = Vec::new();
    for _ in 0..2 {
        inbound_types.push(receive(&mut ingress.inbound_requests).await.apdu[0] >> 4);
    }
    let mut terminal_types = Vec::new();
    for _ in 0..6 {
        terminal_types.push(receive(&mut ingress.terminal_or_segment).await.apdu[0] >> 4);
    }

    assert_eq!(inbound_types, [0, 1]);
    assert_eq!(terminal_types, [2, 3, 5, 6, 7, 4]);
    assert!(ingress.inbound_requests.try_recv().is_err());
    assert!(ingress.terminal_or_segment.try_recv().is_err());
    assert!(ingress.policy_outcomes.try_recv().is_err());
    assert!(matches!(
        endpoint.stop().await.unwrap(),
        ClassifierExit::Cancelled
    ));
}

#[tokio::test]
async fn direct_egress_preserves_npdu_flags_priority_and_destination() {
    let (transport, mut handle) = test_transport();
    let mut endpoint = EndpointIngress::new(transport, 2);
    let ingress = endpoint.start().await.unwrap();
    let egress = ingress.egress.clone();

    egress
        .send_direct(
            vec![0x20, 0x2a, 0x0c],
            MacAddr::from_slice(&[0x44, 0x55]),
            true,
            NetworkPriority::Urgent,
        )
        .await
        .unwrap();

    let (npdu, destination) = timeout(WAIT, handle.sent.recv()).await.unwrap().unwrap();
    let decoded = decode_npdu(Bytes::from(npdu)).unwrap();
    assert_eq!(destination, [0x44, 0x55]);
    assert!(decoded.expecting_reply);
    assert_eq!(decoded.priority, NetworkPriority::Urgent);
    assert_eq!(decoded.payload.as_ref(), &[0x20, 0x2a, 0x0c]);

    endpoint.stop().await.unwrap();
    assert!(matches!(
        egress
            .send_direct(
                vec![0x20, 0x2b, 0x0c],
                MacAddr::from_slice(&[0x44]),
                false,
                NetworkPriority::Normal,
            )
            .await,
        Err(Error::Encoding(message)) if message == "endpoint shutdown"
    ));
}

#[tokio::test]
async fn saturated_egress_is_bounded_and_stop_cancels_accepted_commands() {
    let (transport, handle) = blocking_transport();
    let mut endpoint = EndpointIngress::new(transport, 1);
    let mut ingress = endpoint.start().await.unwrap();
    let egress = ingress.egress.clone();

    let first_egress = egress.clone();
    let first = tokio::spawn(async move {
        first_egress
            .send_direct(
                vec![0x20, 0x01, 0x0c],
                MacAddr::from_slice(&[0x01]),
                false,
                NetworkPriority::Normal,
            )
            .await
    });
    timeout(WAIT, handle.entered_send.notified()).await.unwrap();

    let second_egress = egress.clone();
    let second = tokio::spawn(async move {
        second_egress
            .send_direct(
                vec![0x20, 0x02, 0x0c],
                MacAddr::from_slice(&[0x01]),
                false,
                NetworkPriority::Normal,
            )
            .await
    });
    tokio::task::yield_now().await;

    assert!(matches!(
        egress
            .send_direct(
                vec![0x20, 0x03, 0x0c],
                MacAddr::from_slice(&[0x01]),
                false,
                NetworkPriority::Normal,
            )
            .await,
        Err(Error::Encoding(message)) if message == "endpoint egress queue is full"
    ));

    handle
        .sender
        .send(received_npdu(&[0x20, 0x44, 0x0c]))
        .await
        .unwrap();
    let terminal = timeout(WAIT, ingress.terminal_or_segment.recv())
        .await
        .expect("blocked egress starved terminal classification")
        .expect("terminal route closed while egress was blocked");
    assert_eq!(terminal.apdu.as_ref(), &[0x20, 0x44, 0x0c]);

    assert!(matches!(
        timeout(WAIT, endpoint.stop()).await.unwrap().unwrap(),
        ClassifierExit::Cancelled
    ));
    assert_eq!(handle.stops.load(Ordering::SeqCst), 1);
    for result in [first.await.unwrap(), second.await.unwrap()] {
        assert!(matches!(
            result,
            Err(Error::Encoding(message)) if message == "endpoint shutdown"
        ));
    }
}

#[tokio::test]
async fn malformed_and_unsupported_apdus_have_policy_outcomes() {
    let (transport, handle) = test_transport();
    let mut endpoint = EndpointIngress::new(transport, 4);
    let mut ingress = endpoint.start().await.unwrap();

    inject(&handle, &[]).await;
    inject(&handle, &[0x80]).await;

    let malformed = receive(&mut ingress.policy_outcomes).await;
    assert_eq!(malformed.reason, PolicyReason::MalformedApdu);
    assert!(malformed.received.apdu.is_empty());
    let unsupported = receive(&mut ingress.policy_outcomes).await;
    assert_eq!(unsupported.reason, PolicyReason::UnsupportedPduType(8));
    assert_eq!(unsupported.received.apdu.as_ref(), &[0x80]);
    assert!(ingress.inbound_requests.try_recv().is_err());
    assert!(ingress.terminal_or_segment.try_recv().is_err());

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

/// A confirmed request from a group address goes to policy, not to the
/// server role, whose answer would reach every node in the group (#1504).
#[tokio::test]
async fn a_confirmed_request_from_a_group_source_goes_to_policy() {
    let (transport, handle) = test_transport();
    let mut endpoint = EndpointIngress::new(transport, 4);
    let mut ingress = endpoint.start().await.unwrap();
    // ReadProperty, and Who-Is.
    let confirmed = [
        0x00, 0x05, 0x01, 0x0C, 0x0C, 0x02, 0x00, 0x00, 0x01, 0x19, 0x4D,
    ];
    let unconfirmed = [0x10, 0x08];
    for apdu in [&confirmed[..], &unconfirmed] {
        let mut from_group = received_npdu(apdu);
        from_group.source_mac = MacAddr::from_slice(&[0xFF]);
        handle.sender.send(from_group).await.unwrap();
    }
    inject(&handle, &confirmed).await;

    let refused = receive(&mut ingress.policy_outcomes).await;
    assert_eq!(refused.reason, PolicyReason::GroupSource);
    assert_eq!(refused.received.source_mac[..], [0xFF]);
    // The group's unconfirmed request, and a station's confirmed one, still
    // reach the role.
    let who_is = receive(&mut ingress.inbound_requests).await;
    assert_eq!(who_is.apdu.as_ref(), unconfirmed);
    let request = receive(&mut ingress.inbound_requests).await;
    assert_eq!(request.source_mac[..], [0x11]);
    assert!(ingress.policy_outcomes.try_recv().is_err());

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

#[tokio::test]
async fn request_route_preserves_the_complete_envelope_and_reply_sender() {
    let (transport, handle) = test_transport();
    let mut endpoint = EndpointIngress::new(transport, 2);
    let mut ingress = endpoint.start().await.unwrap();
    // A global broadcast carries only an Unconfirmed-Request (#1491).
    let apdu = [0x10, 0x08];
    let source_network = NpduAddress {
        network: 77,
        mac_address: MacAddr::from_slice(&[0x44, 0x55]),
    };
    let destination = NpduAddress {
        network: 0xffff,
        mac_address: MacAddr::new(),
    };
    let npdu = Npdu {
        expecting_reply: true,
        priority: NetworkPriority::Urgent,
        destination: Some(destination),
        source: Some(source_network.clone()),
        hop_count: 255,
        payload: Bytes::copy_from_slice(&apdu),
        ..Npdu::default()
    };
    let mut buffer = BytesMut::new();
    encode_npdu(&mut buffer, &npdu).unwrap();
    let attributes = vec![DataAttribute {
        option_type: 9,
        must_understand: true,
        data: vec![1, 2, 3],
    }];
    let (reply_tx, reply_rx) = oneshot::channel();
    handle
        .sender
        .send(ReceivedNpdu {
            direct_response: None,
            npdu: buffer.freeze(),
            source_mac: MacAddr::from_slice(&[0xde, 0xad]),
            link_layer_group: true,
            data_attributes: attributes.clone(),
            provenance: TransportProvenance::unverified(),
            reply_tx: Some(reply_tx),
        })
        .await
        .unwrap();

    let mut received = receive(&mut ingress.inbound_requests).await;
    assert_eq!(received.apdu.as_ref(), apdu);
    assert_eq!(received.source_mac.as_slice(), &[0xde, 0xad]);
    assert_eq!(received.source_network, Some(source_network));
    assert!(received.link_layer_group);
    assert!(received.is_group);
    assert_eq!(received.data_attributes, attributes);
    received
        .reply_tx
        .take()
        .expect("reply sender was lost")
        .send(Bytes::from_static(b"reply"))
        .unwrap();
    assert_eq!(reply_rx.await.unwrap().as_ref(), b"reply");
    assert!(ingress.terminal_or_segment.try_recv().is_err());
    assert!(ingress.policy_outcomes.try_recv().is_err());

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

#[tokio::test]
async fn duplicate_start_is_rejected_before_transport_and_stop_is_single_owner() {
    let (transport, handle) = test_transport();
    let mut endpoint = EndpointIngress::new(transport, 1);
    let mut ingress = endpoint.start().await.unwrap();
    assert_eq!(handle.starts.load(Ordering::SeqCst), 1);

    assert!(endpoint.start().await.is_err());
    assert_eq!(handle.starts.load(Ordering::SeqCst), 1);
    assert!(matches!(
        endpoint.stop().await.unwrap(),
        ClassifierExit::Cancelled
    ));
    assert_eq!(handle.stops.load(Ordering::SeqCst), 1);
    assert!(endpoint.stop().await.is_err());
    assert_eq!(handle.stops.load(Ordering::SeqCst), 1);
    assert!(ingress.inbound_requests.recv().await.is_none());
    assert!(ingress.terminal_or_segment.recv().await.is_none());
    assert!(ingress.policy_outcomes.recv().await.is_none());
}

#[tokio::test]
async fn zero_capacity_is_rejected_before_transport_start() {
    let (transport, handle) = test_transport();
    let mut endpoint = EndpointIngress::new(transport, 0);

    assert!(endpoint.start().await.is_err());
    assert_eq!(handle.starts.load(Ordering::SeqCst), 0);
    assert_eq!(handle.stops.load(Ordering::SeqCst), 0);
}

#[tokio::test]
async fn full_and_closed_role_routes_are_explicit_and_stop_does_not_block() {
    let (transport, handle) = test_transport();
    let mut endpoint = EndpointIngress::new(transport, 1);
    let ingress = endpoint.start().await.unwrap();
    let mut inbound = ingress.inbound_requests;
    let terminal = ingress.terminal_or_segment;
    let mut policies = ingress.policy_outcomes;

    inject(&handle, &[0x00, 0x05, 0x01, 0x0c]).await;
    inject(&handle, &[0x00, 0x05, 0x02, 0x0c]).await;
    let full = receive(&mut policies).await;
    assert_eq!(
        full.reason,
        PolicyReason::RouteFull(IngressRoute::InboundRequest)
    );
    assert_eq!(full.received.apdu[2], 2);

    drop(terminal);
    inject(&handle, &[0x20, 0x03, 0x0c]).await;
    let closed = receive(&mut policies).await;
    assert_eq!(
        closed.reason,
        PolicyReason::RouteClosed(IngressRoute::TerminalOrSegment)
    );
    assert_eq!(closed.received.apdu[1], 3);

    timeout(WAIT, endpoint.stop())
        .await
        .expect("stop blocked on a saturated role route")
        .unwrap();
    assert_eq!(handle.stops.load(Ordering::SeqCst), 1);
    assert_eq!(inbound.recv().await.unwrap().apdu[2], 1);
    assert!(inbound.recv().await.is_none());
    assert!(policies.recv().await.is_none());
}

#[tokio::test]
async fn full_policy_route_returns_the_unrouted_envelope_on_reclaim() {
    let (transport, handle) = test_transport();
    let mut endpoint = EndpointIngress::new(transport, 1);
    let mut ingress = endpoint.start().await.unwrap();

    inject(&handle, &[0x00, 0x05, 0x01, 0x0c]).await;
    inject(&handle, &[0x00, 0x05, 0x02, 0x0c]).await;
    inject(&handle, &[0x00, 0x05, 0x03, 0x0c]).await;

    assert!(
        timeout(WAIT, ingress.terminal_or_segment.recv())
            .await
            .expect("classifier did not stop when its policy route filled")
            .is_none()
    );

    let exit = timeout(WAIT, endpoint.stop()).await.unwrap().unwrap();
    match exit {
        ClassifierExit::PolicyRouteFull(outcome) => {
            assert_eq!(
                outcome.reason,
                PolicyReason::RouteFull(IngressRoute::InboundRequest)
            );
            assert_eq!(outcome.received.apdu[2], 3);
        }
        other => panic!("unexpected classifier exit: {other:?}"),
    }
    assert_eq!(handle.stops.load(Ordering::SeqCst), 1);
    drop(ingress);
}

#[path = "endpoint_ingress_lifecycle_tests.rs"]
mod lifecycle;