bacnet-network 0.15.0

BACnet network layer: routing, router tables, priority channels
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
//! RB-03 discovery tests: Who-Is origin, Init envelopes, directed routing.
//!
//! Clause map (ASHRAE 135-2020, supplied PDF): 6.5.4 (retain routed
//! SNET/SADR, else add ingress net + SA; forward data attributes), 6.6.3.2
//! (Who-Is discovery relay; scoped queries never promote to global scans),
//! 6.4.7/6.6.3.8 (Initialize-Routing-Table query vs. update envelopes).
//!
//! Shares [`super::envelope_harness`] with the control-envelope module.

use bacnet_encoding::npdu::{Npdu, NpduAddress, decode_npdu};
use bacnet_npdu::{NetworkMessageType, RejectMessageReason};
use bacnet_types::MacAddr;
use bytes::Bytes;

use super::SendRequest;
use super::envelope_harness::*;
use crate::router_table::{PortId, Reachability};

// --- Who-Is discovery origin (Clause 6.6.3.2) ---

#[tokio::test]
async fn who_is_local_origin_adds_ingress_source_and_attributes_on_forward() {
    let mut h = Harness::two_port();
    let mut npdu = control_npdu(NetworkMessageType::WHO_IS_ROUTER_TO_NETWORK, &[0x0b, 0xb8]);
    let mut ctx = h.ctx(0, &[7], npdu.clone());
    ctx.data_attributes = attributes();
    npdu = ctx.npdu.clone();
    h.handle(ctx).await;

    assert!(h.drain(0).is_empty());
    let forwarded = broadcast_data(h.drain(1));
    assert_eq!(forwarded.len(), 1);
    let relayed = decode_npdu(forwarded[0].clone()).unwrap();
    // Locally originated: ingress network + immediate source MAC added.
    assert_eq!(
        relayed.source,
        Some(NpduAddress {
            network: 1000,
            mac_address: MacAddr::from_slice(&[7]),
        })
    );
    assert!(relayed.destination.is_none());
    assert_eq!(relayed.payload, npdu.payload);
    assert_eq!(
        relayed.message_type,
        Some(NetworkMessageType::WHO_IS_ROUTER_TO_NETWORK)
    );
    // Next hop stays separate: the relay is a link broadcast, not addressed.
    assert_eq!(h.table().len(), 2);
}

#[tokio::test]
async fn who_is_routed_origin_retains_source() {
    let mut h = Harness::two_port();
    let mut npdu = control_npdu(NetworkMessageType::WHO_IS_ROUTER_TO_NETWORK, &[0x0b, 0xb8]);
    npdu.source = Some(NpduAddress {
        network: 900,
        mac_address: MacAddr::from_slice(&[9]),
    });
    let mut ctx = h.ctx(0, &[7], npdu);
    ctx.data_attributes = attributes();
    h.handle(ctx).await;

    let forwarded = broadcast_data(h.drain(1));
    assert_eq!(forwarded.len(), 1);
    let relayed = decode_npdu(forwarded[0].clone()).unwrap();
    // Already routed: SNET/SADR retained, ingress MAC not merged in.
    assert_eq!(
        relayed.source,
        Some(NpduAddress {
            network: 900,
            mac_address: MacAddr::from_slice(&[9]),
        })
    );
}

#[tokio::test]
async fn who_is_scoped_known_network_answered_locally_not_forwarded() {
    let mut h = Harness::two_port();
    let mut npdu = control_npdu(NetworkMessageType::WHO_IS_ROUTER_TO_NETWORK, &[0x07, 0xd0]);
    let mut ctx = h.ctx(0, &[7], npdu.clone());
    ctx.data_attributes = attributes();
    npdu = ctx.npdu.clone();
    h.handle(ctx).await;

    // I-Am reply broadcast on the ingress port only, carrying attributes.
    assert!(h.drain(1).is_empty());
    let mut replies = h.drain(0);
    assert_eq!(replies.len(), 1);
    let reply = replies.pop().unwrap();
    let (data, attrs) = match reply {
        SendRequest::Broadcast {
            npdu,
            data_attributes,
            ..
        } => (npdu, data_attributes),
        SendRequest::Unicast { .. } => panic!("expected broadcast I-Am reply"),
    };
    let decoded = decode_npdu(data).unwrap();
    assert_eq!(
        decoded.message_type,
        Some(NetworkMessageType::I_AM_ROUTER_TO_NETWORK)
    );
    assert_eq!(decoded.payload.as_ref(), &[0x07, 0xd0]);
    assert_eq!(attrs, attributes());
    assert_eq!(npdu.payload.as_ref(), &[0x07, 0xd0]);
}

#[tokio::test]
async fn who_is_unscoped_lists_reachable_not_ingress() {
    let mut h = Harness::two_port();
    h.handle(h.ctx(
        0,
        &[7],
        control_npdu(NetworkMessageType::WHO_IS_ROUTER_TO_NETWORK, &[]),
    ))
    .await;

    assert!(h.drain(1).is_empty());
    let replies = broadcast_data(h.drain(0));
    assert_eq!(replies.len(), 1);
    let decoded = decode_npdu(replies[0].clone()).unwrap();
    // 2000 reachable via another port; the ingress 1000 is excluded.
    assert_eq!(decoded.payload.as_ref(), &[0x07, 0xd0]);
}

#[tokio::test]
async fn who_is_truncated_tail_never_promotes_to_global_scan() {
    let mut h = Harness::two_port();
    // A 1-octet tail was previously answered as an unscoped global query.
    for payload in [&[0x0b][..], &[0x0b, 0xb8, 0x00][..], &[0, 0][..]] {
        h.handle(h.ctx(
            0,
            &[7],
            control_npdu(NetworkMessageType::WHO_IS_ROUTER_TO_NETWORK, payload),
        ))
        .await;
        h.assert_quiet();
    }
    assert_eq!(h.table().len(), 2);
}

// --- Initialize-Routing-Table transactional envelope ---

#[tokio::test]
async fn init_update_query_and_garbage_envelopes() {
    let mut h = Harness::two_port();

    // Truncated entry: no table change, no ACK (previously ACK-on-partial).
    h.handle(h.ctx(
        1,
        &[2],
        control_npdu(
            NetworkMessageType::INITIALIZE_ROUTING_TABLE,
            &[1, 0x0b, 0xb8],
        ),
    ))
    .await;
    h.assert_quiet();
    assert_eq!(h.table().len(), 2);

    // Trailing garbage after a zero count: not a query, no ACK.
    h.handle(h.ctx(
        1,
        &[2],
        control_npdu(NetworkMessageType::INITIALIZE_ROUTING_TABLE, &[0, 0xff]),
    ))
    .await;
    h.assert_quiet();

    // Valid update: Port ID 1 names local port 0 (the wire mapping, not the
    // ingress port 1). Learns 3000 there; the ACK carries no table data and
    // is routed to the origin.
    h.handle(h.ctx(
        1,
        &[2],
        control_npdu(
            NetworkMessageType::INITIALIZE_ROUTING_TABLE,
            &[1, 0x0b, 0xb8, 1, 0],
        ),
    ))
    .await;
    assert!(h.drain(0).is_empty());
    let mut acks = h.drain(1);
    assert_eq!(acks.len(), 1);
    match acks.pop().unwrap() {
        SendRequest::Unicast { npdu, mac, .. } => {
            assert_eq!(mac.as_slice(), &[2]);
            let decoded = decode_npdu(npdu).unwrap();
            assert_eq!(
                decoded.message_type,
                Some(NetworkMessageType::INITIALIZE_ROUTING_TABLE_ACK)
            );
            assert!(decoded.payload.is_empty());
        }
        SendRequest::Broadcast { .. } => panic!("expected unicast ACK"),
    }
    let route = h.table().lookup(3000).unwrap();
    assert_eq!(route.port(), PortId(0));
    assert!(!route.is_direct());
    assert_eq!(route.next_hop().as_slice(), &[2]);

    // Valid query: one ACK carries the full table — ascending DNETs with
    // stable nonzero wire Port IDs (1000->1, 2000->2, 3000->1).
    h.handle(h.ctx(
        1,
        &[2],
        control_npdu(NetworkMessageType::INITIALIZE_ROUTING_TABLE, &[0]),
    ))
    .await;
    let mut acks = h.drain(1);
    assert_eq!(acks.len(), 1);
    match acks.pop().unwrap() {
        SendRequest::Unicast { npdu, .. } => {
            let decoded = decode_npdu(npdu).unwrap();
            assert_eq!(
                decoded.payload.as_ref(),
                &[3, 0x03, 0xe8, 1, 0, 0x07, 0xd0, 2, 0, 0x0b, 0xb8, 1, 0]
            );
        }
        SendRequest::Broadcast { .. } => panic!("expected unicast ACK"),
    }
}

#[tokio::test]
async fn init_ack_truncated_entry_learns_nothing() {
    let mut h = Harness::two_port();
    h.handle(h.ctx(
        1,
        &[2],
        control_npdu(
            NetworkMessageType::INITIALIZE_ROUTING_TABLE_ACK,
            &[2, 0x0b, 0xb8, 0, 0, 0x0f],
        ),
    ))
    .await;
    h.assert_quiet();
    let table = h.table();
    assert_eq!(table.len(), 2);
    assert!(table.lookup(3000).is_none());
}

// --- Init query/ACK response path (relocated from tests.rs, RB-03) ---

#[tokio::test]
async fn initialize_routing_table_ack() {
    let mut table = TableSeed::new();
    table.add_direct(1000, 0);
    table.add_direct(2000, 1);
    let mut h = Harness::with_table(table);

    // RB-03: a table query is Number of Ports == 0 (one octet), not a
    // missing field. An empty payload is a truncated envelope: no ACK.
    let npdu = Npdu {
        message_type: Some(NetworkMessageType::INITIALIZE_ROUTING_TABLE),
        payload: Bytes::from_static(&[0]),
        ..Npdu::default()
    };
    h.handle(h.ctx(0, &[0x0A], npdu)).await;

    let sent = h.rxs[0].try_recv().unwrap();
    match sent {
        SendRequest::Unicast {
            npdu: data, mac, ..
        } => {
            assert_eq!(mac.as_slice(), &[0x0A]);
            let decoded = decode_npdu(data.clone()).unwrap();
            assert!(decoded.is_network_message());
            assert_eq!(
                decoded.message_type,
                Some(NetworkMessageType::INITIALIZE_ROUTING_TABLE_ACK)
            );
            assert_eq!(decoded.payload.len(), 9);
            assert_eq!(decoded.payload[0], 2);
            // Stable nonzero wire Port IDs in ascending-DNET order.
            assert_eq!(
                decoded.payload.as_ref(),
                &[2, 0x03, 0xe8, 1, 0, 0x07, 0xd0, 2, 0]
            );
        }
        _ => panic!("Expected Unicast response for Init-Routing-Table"),
    }
}

#[tokio::test]
async fn initialize_routing_table_empty_payload_sends_no_ack() {
    // RB-03: the Number of Ports octet is absent, so this is a truncated
    // envelope, not a query. No table change, no ACK.
    let mut table = TableSeed::new();
    table.add_direct(1000, 0);
    let mut h = Harness::with_table(table);

    let npdu = Npdu {
        message_type: Some(NetworkMessageType::INITIALIZE_ROUTING_TABLE),
        payload: Bytes::new(),
        ..Npdu::default()
    };
    h.handle(h.ctx(0, &[0x0A], npdu)).await;

    h.assert_quiet();
    assert_eq!(h.table().len(), 1);
}

// --- Routing-first dispatcher for directed controls ---

#[tokio::test]
async fn directed_control_routed_not_consumed() {
    let mut table = TableSeed::new();
    table.add_direct(1000, 0);
    table.add_direct(2000, 1);
    table.add_learned(3000, 1, MacAddr::from_slice(&[5]));
    let mut h = Harness::with_table(table);

    // I-Am addressed to a remote network: forwarded toward it, never learned.
    let mut npdu = control_npdu(NetworkMessageType::I_AM_ROUTER_TO_NETWORK, &[0x0f, 0xa0]);
    npdu.destination = Some(NpduAddress {
        network: 3000,
        mac_address: MacAddr::from_slice(&[9]),
    });
    npdu.hop_count = 255;
    let mut ctx = h.ctx(0, &[7], npdu);
    ctx.data_attributes = attributes();
    h.dispatch(ctx).await;

    assert!(h.drain(0).is_empty());
    let mut forwarded = h.drain(1);
    assert_eq!(forwarded.len(), 1);
    match forwarded.pop().unwrap() {
        SendRequest::Unicast {
            npdu,
            mac,
            data_attributes,
            ..
        } => {
            assert_eq!(mac.as_slice(), &[5]);
            let decoded = decode_npdu(npdu).unwrap();
            assert_eq!(
                decoded.message_type,
                Some(NetworkMessageType::I_AM_ROUTER_TO_NETWORK)
            );
            assert_eq!(
                decoded.destination,
                Some(NpduAddress {
                    network: 3000,
                    mac_address: MacAddr::from_slice(&[9]),
                })
            );
            assert_eq!(decoded.hop_count, 254);
            assert_eq!(decoded.payload.as_ref(), &[0x0f, 0xa0]);
            assert_eq!(data_attributes, attributes());
        }
        SendRequest::Broadcast { .. } => panic!("expected unicast route"),
    }
    // The payload network was forwarded, not learned.
    let table = h.table();
    assert!(table.lookup(4000).is_none());
    assert_eq!(table.lookup(3000).unwrap().port(), PortId(1));
}

#[tokio::test]
async fn directed_control_to_own_network_handled_locally() {
    let mut h = Harness::two_port();

    // Who-Is for 2000 sent to our own network 1000: answered here, not routed.
    let mut npdu = control_npdu(NetworkMessageType::WHO_IS_ROUTER_TO_NETWORK, &[0x07, 0xd0]);
    npdu.destination = Some(NpduAddress {
        network: 1000,
        mac_address: MacAddr::from_slice(&[1]),
    });
    npdu.hop_count = 255;
    h.dispatch(h.ctx(0, &[7], npdu)).await;

    assert!(h.drain(1).is_empty());
    let replies = broadcast_data(h.drain(0));
    assert_eq!(replies.len(), 1);
    assert_eq!(
        decode_npdu(replies[0].clone()).unwrap().payload.as_ref(),
        &[0x07, 0xd0]
    );
}

#[tokio::test]
async fn directed_control_to_unknown_network_rejected() {
    let mut h = Harness::two_port();

    let mut npdu = control_npdu(NetworkMessageType::WHO_IS_ROUTER_TO_NETWORK, &[0x27, 0x0f]);
    npdu.destination = Some(NpduAddress {
        network: 9999,
        mac_address: MacAddr::from_slice(&[9]),
    });
    npdu.hop_count = 255;
    h.dispatch(h.ctx(0, &[7], npdu)).await;

    // RB-06: unknown first solicits (bounded Who-Is on the other port) then
    // fails the caller with the honest retryable reject; nothing is buffered.
    let solicited = broadcast_data(h.drain(1));
    assert_eq!(solicited.len(), 1);
    let who_is = decode_npdu(solicited[0].clone()).unwrap();
    assert_eq!(
        who_is.message_type,
        Some(NetworkMessageType::WHO_IS_ROUTER_TO_NETWORK)
    );
    assert_eq!(who_is.payload.as_ref(), &[0x27, 0x0f]);
    let mut rejects = h.drain(0);
    assert_eq!(rejects.len(), 1);
    match rejects.pop().unwrap() {
        SendRequest::Unicast { npdu, mac, .. } => {
            assert_eq!(mac.as_slice(), &[7]);
            let decoded = decode_npdu(npdu).unwrap();
            assert_eq!(
                decoded.message_type,
                Some(NetworkMessageType::REJECT_MESSAGE_TO_NETWORK)
            );
            assert_eq!(
                decoded.payload.as_ref(),
                &[
                    RejectMessageReason::NOT_DIRECTLY_CONNECTED.to_raw(),
                    0x27,
                    0x0f
                ]
            );
        }
        SendRequest::Broadcast { .. } => panic!("expected unicast reject"),
    }
    assert_eq!(h.table().len(), 2);
}

#[tokio::test]
async fn directed_who_is_remote_broadcast_forwarded_with_origin() {
    let mut h = Harness::two_port();

    // Locally-originated remote broadcast: origin added at forward time.
    let mut npdu = control_npdu(NetworkMessageType::WHO_IS_ROUTER_TO_NETWORK, &[0x0b, 0xb8]);
    npdu.destination = Some(NpduAddress {
        network: 2000,
        mac_address: MacAddr::new(),
    });
    npdu.hop_count = 255;
    h.dispatch(h.ctx(0, &[7], npdu)).await;
    assert!(h.drain(0).is_empty());
    let forwarded = broadcast_data(h.drain(1));
    assert_eq!(forwarded.len(), 1);
    let decoded = decode_npdu(forwarded[0].clone()).unwrap();
    assert!(decoded.destination.is_none());
    assert_eq!(
        decoded.source,
        Some(NpduAddress {
            network: 1000,
            mac_address: MacAddr::from_slice(&[7]),
        })
    );

    // Already-routed remote broadcast: origin retained.
    let mut npdu = control_npdu(NetworkMessageType::WHO_IS_ROUTER_TO_NETWORK, &[0x0b, 0xb8]);
    npdu.destination = Some(NpduAddress {
        network: 2000,
        mac_address: MacAddr::new(),
    });
    npdu.source = Some(NpduAddress {
        network: 900,
        mac_address: MacAddr::from_slice(&[9]),
    });
    npdu.hop_count = 254;
    h.dispatch(h.ctx(0, &[7], npdu)).await;
    let forwarded = broadcast_data(h.drain(1));
    assert_eq!(forwarded.len(), 1);
    assert_eq!(
        decode_npdu(forwarded[0].clone()).unwrap().source,
        Some(NpduAddress {
            network: 900,
            mac_address: MacAddr::from_slice(&[9]),
        })
    );
}

// --- Link-group provenance is not an effective-group verdict ---

#[tokio::test]
async fn link_broadcast_routed_unicast_control_stays_unicast() {
    let mut table = TableSeed::new();
    table.add_direct(1000, 0);
    table.add_direct(2000, 1);
    table.add_learned(3000, 1, MacAddr::from_slice(&[5]));
    let mut h = Harness::with_table(table);

    // A routed unicast arriving over a link broadcast still forwards unicast.
    let mut npdu = control_npdu(NetworkMessageType::ROUTER_BUSY_TO_NETWORK, &[0x0b, 0xb8]);
    npdu.destination = Some(NpduAddress {
        network: 3000,
        mac_address: MacAddr::from_slice(&[9]),
    });
    npdu.hop_count = 255;
    let mut ctx = h.ctx(0, &[7], npdu);
    ctx.link_layer_group = true;
    h.dispatch(ctx).await;

    assert!(h.drain(0).is_empty());
    match h.drain(1).pop() {
        Some(SendRequest::Unicast { .. }) => {}
        other => panic!("expected unicast forward, got {other:?}"),
    }
    // A transit control is never consumed: no busy mark from our own relay.
    assert_eq!(
        h.table().effective_reachability(3000, h.now()),
        Some(Reachability::Reachable)
    );
}