bacnet-client 0.15.0

BACnet async client: TSM, segmentation, discovery, and service APIs
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
//! Once the client has learned its network's number, a routed request or a
//! network broadcast naming that number goes out as local traffic (#1358):
//! a unicast to the destination MAC, or a local broadcast, with no DNET. A
//! routed confirmed request is then answered from that MAC with no SNET, or
//! relayed back by a router with this network as its SNET and that MAC as
//! its SADR, and either answer completes it (#1465). The peer's limits still
//! come from its routed device-table row, and a request past them is refused
//! or segmented locally. Another network keeps its DNET, and so does every
//! network while the number is unknown.
//!
//! The peer is `[3]`, behind router `[9]` when routed. The number is learned
//! through the client's own Network-Number-Is intake.
use super::*;
use crate::client::BACnetClient;
use crate::discovery::RoutedDeviceConfig;
use bacnet_encoding::{
    apdu::{Apdu, ConfirmedRequest, SegmentAck, SimpleAck, decode_apdu, encode_apdu},
    npdu::{Npdu, NpduAddress, decode_npdu, encode_npdu},
};
use bacnet_transport::port::ReceivedNpdu;
use bacnet_types::MacAddr;
use bacnet_types::enums::{ConfirmedServiceChoice, ObjectType, PropertyIdentifier, Segmentation};
use bacnet_types::error::Error;
use bacnet_types::primitives::ObjectIdentifier;
use bytes::BytesMut;
use tokio::sync::mpsc;
use tokio::time::Duration;

/// The number of the network this client is attached to.
const THIS_NETWORK: u16 = 77;
const REMOTE_NETWORK: u16 = 5;
const PEER: [u8; 1] = [3];
const ROUTER: [u8; 1] = [9];

/// Where a request went: the link MAC and the NPDU's DNET, if any.
#[derive(Debug, PartialEq, Eq)]
struct Route {
    link: MacAddr,
    dnet: Option<u16>,
}

fn local() -> Route {
    Route {
        link: MacAddr::from_slice(&PEER),
        dnet: None,
    }
}

fn routed(network: u16) -> Route {
    Route {
        link: MacAddr::from_slice(&ROUTER),
        dnet: Some(network),
    }
}

/// A started client that has learned `THIS_NETWORK`, or no number at all.
async fn client(
    learned: bool,
) -> (
    BACnetClient<Capture>,
    mpsc::Sender<ReceivedNpdu>,
    mpsc::Receiver<Sent>,
) {
    let (client, inbound, mut outbound, _) = harness(false).await;
    if learned {
        inject(&inbound, &number(THIS_NETWORK, 0), true).await;
        // The worker takes controls in order: once it answers, it holds the
        // number the announcement gave.
        inject(&inbound, QUERY, false).await;
        reply(&mut outbound, THIS_NETWORK).await;
    }
    (client, inbound, outbound)
}

/// Take the confirmed request sent next and answer it with a SimpleACK the
/// way it went: from the peer's MAC when sent locally, from the router with
/// the peer's SNET when routed. Where it went.
async fn answer(
    inbound: &mpsc::Sender<ReceivedNpdu>,
    outbound: &mut mpsc::Receiver<Sent>,
) -> Route {
    let sent = bounded(outbound.recv()).await.unwrap();
    let npdu = decode_npdu(sent.npdu).unwrap();
    let Apdu::ConfirmedRequest(request) = decode_apdu(npdu.payload).unwrap() else {
        panic!("a confirmed request")
    };
    let source = npdu.destination.map(|to| {
        assert_eq!(to.mac_address.as_slice(), PEER, "the DADR");
        NpduAddress {
            network: to.network,
            mac_address: MacAddr::from_slice(&PEER),
        }
    });
    let route = Route {
        link: sent.destination.clone(),
        dnet: source.as_ref().map(|from| from.network),
    };
    send_to_client(inbound, &simple_ack(&request), &sent.destination, source).await;
    route
}

/// The SimpleACK that completes `request`.
fn simple_ack(request: &ConfirmedRequest) -> Apdu {
    Apdu::SimpleAck(SimpleAck {
        invoke_id: request.invoke_id,
        service_choice: request.service_choice,
    })
}

/// Hand the client `apdu` from link MAC `link`, with `source` as its SNET/SADR
/// when routed.
async fn send_to_client(
    inbound: &mpsc::Sender<ReceivedNpdu>,
    apdu: &Apdu,
    link: &[u8],
    source: Option<NpduAddress>,
) {
    let mut payload = BytesMut::new();
    encode_apdu(&mut payload, apdu).unwrap();
    let mut npdu = BytesMut::new();
    encode_npdu(
        &mut npdu,
        &Npdu {
            source,
            payload: payload.freeze(),
            ..Npdu::default()
        },
    )
    .unwrap();
    inject_from(inbound, &npdu, link).await;
}

/// The confirmed request in a frame the client sent, checked to have gone to
/// the peer's MAC with no DNET.
fn local_request(sent: Sent) -> ConfirmedRequest {
    assert_eq!(sent.destination.as_slice(), PEER, "sent to the peer itself");
    let npdu = decode_npdu(sent.npdu).unwrap();
    assert_eq!(npdu.destination, None, "sent with no DNET");
    let Apdu::ConfirmedRequest(request) = decode_apdu(npdu.payload).unwrap() else {
        panic!("a confirmed request")
    };
    request
}

/// Send a routed WriteProperty to the peer on `network` and return where it
/// went, once its answer has completed it.
async fn request(learned: bool, network: u16) -> Route {
    let (mut client, inbound, mut outbound) = client(learned).await;
    let (result, route) = bounded(async {
        tokio::join!(
            client.confirmed_request_routed(
                &ROUTER,
                network,
                &PEER,
                ConfirmedServiceChoice::WRITE_PROPERTY,
                &[0x0C, 0x00, 0x80, 0x00, 0x01],
            ),
            answer(&inbound, &mut outbound),
        )
    })
    .await;
    assert!(result.unwrap().is_empty());
    client.stop().await.unwrap();
    route
}

#[tokio::test]
async fn a_routed_request_naming_this_network_goes_local_and_its_direct_answer_completes_it() {
    assert_eq!(request(true, THIS_NETWORK).await, local());
    assert_eq!(request(true, REMOTE_NETWORK).await, routed(REMOTE_NETWORK));
    assert_eq!(request(false, THIS_NETWORK).await, routed(THIS_NETWORK));
}

#[tokio::test]
async fn a_device_added_as_routed_on_this_network_is_written_locally() {
    for (learned, network, expected) in [
        (true, THIS_NETWORK, local()),
        (true, REMOTE_NETWORK, routed(REMOTE_NETWORK)),
        (false, THIS_NETWORK, routed(THIS_NETWORK)),
    ] {
        let (mut client, inbound, mut outbound) = client(learned).await;
        client
            .add_routed_device(RoutedDeviceConfig {
                instance: 9,
                router_mac: ROUTER.to_vec(),
                remote_network: network,
                remote_mac: PEER.to_vec(),
                max_apdu_length: 480,
                segmentation_supported: Segmentation::NONE,
                max_segments_accepted: None,
            })
            .await
            .unwrap();
        let (result, route) = bounded(async {
            tokio::join!(
                client.write_property_to_device(
                    9,
                    ObjectIdentifier::new(ObjectType::ANALOG_VALUE, 1).unwrap(),
                    PropertyIdentifier::PRESENT_VALUE,
                    None,
                    vec![0x44, 0x42, 0xA0, 0x00, 0x00],
                    None,
                ),
                answer(&inbound, &mut outbound),
            )
        })
        .await;
        result.unwrap();
        assert_eq!(route, expected, "learned {learned}, network {network}");
        client.stop().await.unwrap();
    }
}

#[tokio::test]
async fn a_who_is_on_this_network_by_number_goes_as_a_local_broadcast() {
    for (learned, network, dnet) in [
        (true, THIS_NETWORK, None),
        (true, REMOTE_NETWORK, Some(REMOTE_NETWORK)),
        (false, THIS_NETWORK, Some(THIS_NETWORK)),
    ] {
        let (mut client, _inbound, mut outbound) = client(learned).await;
        client.who_is_network(network, None).await.unwrap();
        let sent = bounded(outbound.recv()).await.unwrap();
        assert!(sent.destination.is_empty(), "a broadcast");
        let destination = decode_npdu(sent.npdu).unwrap().destination;
        assert_eq!(
            destination.map(|to| (to.network, to.mac_address.is_empty())),
            dnet.map(|network| (network, true)),
            "learned {learned}, network {network}"
        );
        client.stop().await.unwrap();
    }
}

/// A WriteProperty longer than one 128-octet APDU.
const LONG_WRITE: [u8; 300] = [0x44; 300];

/// A client that has learned `THIS_NETWORK` and holds Device 9 as routed
/// through `ROUTER` on it, accepting APDUs of at most 128 octets and
/// `segmentation`.
async fn client_with_routed_row(
    segmentation: Segmentation,
) -> (
    BACnetClient<Capture>,
    mpsc::Sender<ReceivedNpdu>,
    mpsc::Receiver<Sent>,
) {
    let (client, inbound, outbound) = client(true).await;
    client
        .add_routed_device(RoutedDeviceConfig {
            instance: 9,
            router_mac: ROUTER.to_vec(),
            remote_network: THIS_NETWORK,
            remote_mac: PEER.to_vec(),
            max_apdu_length: 128,
            segmentation_supported: segmentation,
            max_segments_accepted: None,
        })
        .await
        .unwrap();
    (client, inbound, outbound)
}

/// The routed row the device was added with still sets the peer's limits
/// when the request goes locally: a peer that takes no segmented request
/// gets nothing too long for it.
#[tokio::test]
async fn a_routed_row_on_this_network_still_bounds_a_local_request() {
    let (mut client, _inbound, mut outbound) = client_with_routed_row(Segmentation::NONE).await;
    let result = bounded(client.confirmed_request_routed(
        &ROUTER,
        THIS_NETWORK,
        &PEER,
        ConfirmedServiceChoice::WRITE_PROPERTY,
        &LONG_WRITE,
    ))
    .await;
    assert!(matches!(result, Err(Error::Segmentation(_))), "{result:?}");
    assert!(outbound.try_recv().is_err(), "nothing was sent");
    client.stop().await.unwrap();
}

/// A local request longer than the routed row allows goes to the peer in
/// segments, each with no DNET, and the peer's own SegmentACKs and SimpleACK,
/// with no SNET, carry it through.
#[tokio::test]
async fn a_long_request_to_this_network_is_segmented_to_the_peer() {
    let (mut client, inbound, mut outbound) = client_with_routed_row(Segmentation::BOTH).await;
    let peer = async {
        let mut segments = 0;
        loop {
            let request = local_request(bounded(outbound.recv()).await.unwrap());
            assert!(request.segmented, "frame {segments} is a segment");
            segments += 1;
            let ack = Apdu::SegmentAck(SegmentAck {
                negative_ack: false,
                sent_by_server: true,
                invoke_id: request.invoke_id,
                sequence_number: request.sequence_number.unwrap(),
                actual_window_size: 1,
            });
            send_to_client(&inbound, &ack, &PEER, None).await;
            if !request.more_follows {
                send_to_client(&inbound, &simple_ack(&request), &PEER, None).await;
                return segments;
            }
        }
    };
    let (result, segments) = bounded(async {
        tokio::join!(
            client.confirmed_request_routed(
                &ROUTER,
                THIS_NETWORK,
                &PEER,
                ConfirmedServiceChoice::WRITE_PROPERTY,
                &LONG_WRITE,
            ),
            peer,
        )
    })
    .await;
    assert!(result.unwrap().is_empty());
    assert!(segments > 1, "{segments} segments");
    client.stop().await.unwrap();
}

/// `station` on `network`, as the SNET/SADR a router adds when it relays.
fn relayed_from(network: u16, station: &[u8]) -> Option<NpduAddress> {
    Some(NpduAddress {
        network,
        mac_address: MacAddr::from_slice(station),
    })
}

/// Send a WriteProperty to the peer, routed to this network or straight to
/// its MAC, while `peer` answers it, and return how long it took to complete.
async fn write_answered_by(
    client: &BACnetClient<Capture>,
    routed: bool,
    peer: impl std::future::Future<Output = ()>,
) -> Duration {
    const WRITE: [u8; 5] = [0x0C, 0x00, 0x80, 0x00, 0x01];
    let service = ConfirmedServiceChoice::WRITE_PROPERTY;
    let started = tokio::time::Instant::now();
    let request = async {
        if routed {
            client
                .confirmed_request_routed(&ROUTER, THIS_NETWORK, &PEER, service, &WRITE)
                .await
        } else {
            client.confirmed_request(&PEER, service, &WRITE).await
        }
    };
    let (result, ()) = tokio::join!(request, peer);
    assert!(result.unwrap().is_empty());
    started.elapsed()
}

/// The request sent again after the APDU timeout, checked to have gone to
/// the peer's MAC with the first one's invoke ID.
async fn retry_of(
    outbound: &mut mpsc::Receiver<Sent>,
    first: &ConfirmedRequest,
) -> ConfirmedRequest {
    let retry = tokio::time::timeout(Duration::from_secs(10), outbound.recv())
        .await
        .expect("the request goes again")
        .unwrap();
    let retry = local_request(retry);
    assert_eq!(retry.invoke_id, first.invoke_id);
    retry
}

/// Once the number is known, an answer a router relays back with this
/// network as its SNET and the peer's MAC as its SADR completes a request
/// sent to that MAC as the direct answer would (#1465): network numbers are
/// unique, so both name the same station. That holds for a request routed to
/// this network and for one sent straight to the MAC, and the answer
/// completes it at once, with nothing sent again.
#[tokio::test(start_paused = true)]
async fn an_answer_relayed_with_this_networks_snet_completes_a_local_request() {
    let (mut client, inbound, mut outbound) = client(true).await;
    for routed in [true, false] {
        let peer = async {
            let request = local_request(outbound.recv().await.unwrap());
            let relayed = relayed_from(THIS_NETWORK, &PEER);
            send_to_client(&inbound, &simple_ack(&request), &ROUTER, relayed).await;
        };
        let took = write_answered_by(&client, routed, peer).await;
        // The harness's APDU timeout is 5 s.
        assert!(took < Duration::from_secs(5), "routed {routed}: {took:?}");
        assert!(outbound.try_recv().is_err(), "routed {routed}: sent once");
    }
    client.stop().await.unwrap();
}

/// A relayed answer completes a local request only when it names this
/// network, the station the request went to and the request's invoke ID.
/// Another SNET, another SADR or another invoke ID completes nothing, so the
/// request goes again after the APDU timeout, and the station's relayed
/// answer to that completes it.
#[tokio::test(start_paused = true)]
async fn a_relayed_answer_naming_another_network_station_or_invoke_id_leaves_the_request_open() {
    let (mut client, inbound, mut outbound) = client(true).await;
    let peer = async {
        let request = local_request(outbound.recv().await.unwrap());
        let ack = simple_ack(&request);
        for source in [
            relayed_from(REMOTE_NETWORK, &PEER),
            relayed_from(THIS_NETWORK, &[4]),
        ] {
            send_to_client(&inbound, &ack, &ROUTER, source).await;
        }
        let another_invoke_id = Apdu::SimpleAck(SimpleAck {
            invoke_id: request.invoke_id.wrapping_add(1),
            service_choice: request.service_choice,
        });
        let relayed = relayed_from(THIS_NETWORK, &PEER);
        send_to_client(&inbound, &another_invoke_id, &ROUTER, relayed.clone()).await;
        let retry = retry_of(&mut outbound, &request).await;
        send_to_client(&inbound, &simple_ack(&retry), &ROUTER, relayed).await;
    };
    let took = write_answered_by(&client, true, peer).await;
    assert!(took >= Duration::from_secs(5), "{took:?}");
    client.stop().await.unwrap();
}

/// While the number is unknown nothing changes: an answer relayed with SNET
/// 77 is not from the MAC a request went to, so only that MAC's own answer
/// to the retry completes it.
#[tokio::test(start_paused = true)]
async fn with_the_number_unknown_a_relayed_answer_leaves_a_local_request_open() {
    let (mut client, inbound, mut outbound) = client(false).await;
    let peer = async {
        let request = local_request(outbound.recv().await.unwrap());
        let relayed = relayed_from(THIS_NETWORK, &PEER);
        send_to_client(&inbound, &simple_ack(&request), &ROUTER, relayed).await;
        let retry = retry_of(&mut outbound, &request).await;
        send_to_client(&inbound, &simple_ack(&retry), &PEER, None).await;
    };
    let took = write_answered_by(&client, false, peer).await;
    assert!(took >= Duration::from_secs(5), "{took:?}");
    client.stop().await.unwrap();
}

/// A request routed to this network while its number was unknown goes
/// through the router with the DNET and keeps that routed key. When the
/// number is learned before its answer comes back, the answer the router
/// relays with this network as its SNET still completes it at once, as it
/// would have before the number was known (#1465).
#[tokio::test(start_paused = true)]
async fn a_request_routed_before_the_number_was_learned_completes_on_its_relayed_answer() {
    let (mut client, inbound, mut outbound) = client(false).await;
    let peer = async {
        let sent = outbound.recv().await.unwrap();
        assert_eq!(sent.destination.as_slice(), ROUTER, "through the router");
        let npdu = decode_npdu(sent.npdu).unwrap();
        assert_eq!(npdu.destination.map(|to| to.network), Some(THIS_NETWORK));
        let Apdu::ConfirmedRequest(request) = decode_apdu(npdu.payload).unwrap() else {
            panic!("a confirmed request")
        };
        inject(&inbound, &number(THIS_NETWORK, 0), true).await;
        inject(&inbound, QUERY, false).await;
        reply(&mut outbound, THIS_NETWORK).await;
        let relayed = relayed_from(THIS_NETWORK, &PEER);
        send_to_client(&inbound, &simple_ack(&request), &ROUTER, relayed).await;
    };
    let took = write_answered_by(&client, true, peer).await;
    assert!(took < Duration::from_secs(5), "{took:?}");
    assert!(outbound.try_recv().is_err(), "sent once");
    client.stop().await.unwrap();
}