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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
use super::dispatch_context::{DispatchContext, InboundApdu};
use super::response_admission::{
TerminalDispatchOutcome, TerminalResponse, admit_terminal_during_reassembly,
complete_terminal_response, current_reassembly_owner, log_coordinated_mismatch,
take_current_reassembly,
};
use super::segmentation_context::InboundSegmentSource;
use super::*;
use crate::tsm::{CompletionOutcome, CoordinatedCompletion};
impl<T: TransportPort + 'static> BACnetClient<T> {
/// Dispatch a received APDU to the appropriate handler.
pub(super) async fn dispatch_apdu(
context: DispatchContext<'_, T>,
seg_state: &mut HashMap<SegKey, SegmentedReceiveState>,
inbound: InboundApdu<'_>,
apdu: Apdu,
limits: ResponseLimits,
) {
let DispatchContext {
tsm,
device_table,
network,
cov_tx,
event_tx,
confirmed_cov_ack_policy,
device_tx,
device_collision_tx,
i_have_tx,
seg_ack_senders,
decode_warning,
response_warning,
} = context;
let InboundApdu {
source_mac,
source_network,
provenance,
response_route,
is_group,
reply_tx,
} = inbound;
let TransactionPeer {
tsm_mac,
canonical: canonical_peer,
} = TransactionPeer::of_answer_in(
tsm,
source_mac,
source_network.as_ref(),
network.local_network_number().get(),
&apdu,
)
.await;
match apdu {
Apdu::SimpleAck(ack) => {
// Mid-reassembly, a SimpleACK is in Clause 5.4.4.4
// UnexpectedPDU_Received's list, exactly like the Error and
// Reject arms below — the segmented response under
// reassembly IS this transaction's acknowledgment, so a
// second one is not answering it. Checked before the TSM
// lock: `abort_reassembly` takes that lock itself (#367).
let key = (tsm_mac.clone(), ack.invoke_id, provenance);
let coordinator_apdu = Apdu::SimpleAck(ack.clone());
admit_terminal_during_reassembly(
tsm,
seg_state,
&key,
&canonical_peer,
&coordinator_apdu,
)
.await;
if let Some(state) = take_current_reassembly(tsm, seg_state, &key).await {
debug!(
invoke_id = ack.invoke_id,
"Aborting reassembly on a SimpleAck"
);
Self::abort_reassembly(
tsm,
network,
state.abort_target(&tsm_mac),
ack.invoke_id,
bacnet_types::enums::AbortReason::INVALID_APDU_IN_THIS_STATE,
)
.await;
return;
}
debug!(invoke_id = ack.invoke_id, "Received SimpleAck");
match complete_terminal_response(
tsm,
TerminalResponse {
tsm_mac: &tsm_mac,
invoke_id: ack.invoke_id,
peer: &canonical_peer,
apdu: &coordinator_apdu,
response: TsmResponse::SimpleAck,
phase_gate: true,
owner: None,
},
)
.await
{
TerminalDispatchOutcome::Completion(outcome) => {
log_coordinated_mismatch(
&outcome,
ack.invoke_id,
"SimpleAck",
response_warning,
);
}
TerminalDispatchOutcome::PrematureSegmentedRequestAborted => {
Self::send_client_abort(
network,
source_mac,
source_network,
ack.invoke_id,
bacnet_types::enums::AbortReason::INVALID_APDU_IN_THIS_STATE,
)
.await;
}
}
}
Apdu::ComplexAck(ack) => {
if ack.segmented {
Self::handle_segmented_complex_ack(
tsm,
network,
seg_state,
InboundSegmentSource {
mac: source_mac,
network: source_network,
provenance,
warning: response_warning,
},
ack,
limits,
)
.await;
} else {
// An unsegmented BACnet-ComplexACK-PDU is in the same
// Clause 5.4.4.4
// UnexpectedPDU_Received list: the transaction's answer
// is the segmented ComplexACK already under reassembly.
let key = (tsm_mac.clone(), ack.invoke_id, provenance);
let coordinator_apdu = Apdu::ComplexAck(ack.clone());
admit_terminal_during_reassembly(
tsm,
seg_state,
&key,
&canonical_peer,
&coordinator_apdu,
)
.await;
if let Some(state) = take_current_reassembly(tsm, seg_state, &key).await {
debug!(
invoke_id = ack.invoke_id,
"Aborting reassembly on an unsegmented ComplexAck"
);
Self::abort_reassembly(
tsm,
network,
state.abort_target(&tsm_mac),
ack.invoke_id,
bacnet_types::enums::AbortReason::INVALID_APDU_IN_THIS_STATE,
)
.await;
return;
}
debug!(invoke_id = ack.invoke_id, "Received ComplexAck");
match complete_terminal_response(
tsm,
TerminalResponse {
tsm_mac: &tsm_mac,
invoke_id: ack.invoke_id,
peer: &canonical_peer,
apdu: &coordinator_apdu,
response: TsmResponse::ComplexAck {
service_data: ack.service_ack,
},
phase_gate: true,
owner: None,
},
)
.await
{
TerminalDispatchOutcome::Completion(outcome) => {
log_coordinated_mismatch(
&outcome,
ack.invoke_id,
"ComplexAck",
response_warning,
);
}
TerminalDispatchOutcome::PrematureSegmentedRequestAborted => {
Self::send_client_abort(
network,
source_mac,
source_network,
ack.invoke_id,
bacnet_types::enums::AbortReason::INVALID_APDU_IN_THIS_STATE,
)
.await;
}
}
}
}
Apdu::Error(err) => {
// Mid-reassembly, an Error PDU is Clause 5.4.4.4
// UnexpectedPDU_Received — "BACnet-Error-PDU" is in its list —
// so the local surface is an ABORT.indication with
// INVALID_APDU_IN_THIS_STATE, not the error content (#367).
// Checked before the TSM lock below: `abort_reassembly` takes
// that lock itself, and tokio's Mutex is not reentrant.
let key = (tsm_mac.clone(), err.invoke_id, provenance);
let coordinator_apdu = Apdu::Error(err.clone());
admit_terminal_during_reassembly(
tsm,
seg_state,
&key,
&canonical_peer,
&coordinator_apdu,
)
.await;
if let Some(state) = take_current_reassembly(tsm, seg_state, &key).await {
debug!(
invoke_id = err.invoke_id,
"Aborting reassembly on an Error PDU"
);
Self::abort_reassembly(
tsm,
network,
state.abort_target(&tsm_mac),
err.invoke_id,
bacnet_types::enums::AbortReason::INVALID_APDU_IN_THIS_STATE,
)
.await;
return;
}
debug!(invoke_id = err.invoke_id, "Received Error PDU");
// Correlated by invoke ID alone. Unlike 20.1.4.2 and 20.1.5.6,
// Clause 20.1.7.2 imposes no correspondence to the requested
// service: error-choice identifies the selected BACnet-Error
// alternative by tag, and Clause 21 opens that production with
// `other [127] Error`. Nothing in the Standard forbids a peer
// from answering through that tag, so an exact-match gate here
// would reject conformant responses.
match complete_terminal_response(
tsm,
TerminalResponse {
tsm_mac: &tsm_mac,
invoke_id: err.invoke_id,
peer: &canonical_peer,
apdu: &coordinator_apdu,
response: TsmResponse::from_error_pdu(&err),
phase_gate: true,
owner: None,
},
)
.await
{
TerminalDispatchOutcome::Completion(_) => {}
TerminalDispatchOutcome::PrematureSegmentedRequestAborted => {
Self::send_client_abort(
network,
source_mac,
source_network,
err.invoke_id,
bacnet_types::enums::AbortReason::INVALID_APDU_IN_THIS_STATE,
)
.await;
}
}
}
Apdu::Reject(rej) => {
// Same Clause 5.4.4.4 UnexpectedPDU_Received diversion as the
// Error arm above ("BACnet-Reject-PDU" is in the same list),
// with the same lock-ordering constraint (#367).
let key = (tsm_mac.clone(), rej.invoke_id, provenance);
let coordinator_apdu = Apdu::Reject(rej.clone());
admit_terminal_during_reassembly(
tsm,
seg_state,
&key,
&canonical_peer,
&coordinator_apdu,
)
.await;
if let Some(state) = take_current_reassembly(tsm, seg_state, &key).await {
debug!(
invoke_id = rej.invoke_id,
"Aborting reassembly on a Reject PDU"
);
Self::abort_reassembly(
tsm,
network,
state.abort_target(&tsm_mac),
rej.invoke_id,
bacnet_types::enums::AbortReason::INVALID_APDU_IN_THIS_STATE,
)
.await;
return;
}
debug!(invoke_id = rej.invoke_id, "Received Reject PDU");
// Carries no service choice (Clause 20.1.8); invoke ID is the
// only correlation the Standard provides.
let _ = complete_terminal_response(
tsm,
TerminalResponse {
tsm_mac: &tsm_mac,
invoke_id: rej.invoke_id,
peer: &canonical_peer,
apdu: &coordinator_apdu,
response: TsmResponse::remote(rej.remote_error()),
phase_gate: false,
owner: None,
},
)
.await;
}
Apdu::Abort(abt) => {
// Clause 5.4.4.3 AbortPDU_Received fires only for an Abort
// with `server` = TRUE. An Abort this client
// itself sent — 5.4.4.4 emits them with 'server' = FALSE —
// must not, echoed back or spoofed, complete the very
// transaction it was trying to abort.
if !abt.sent_by_server {
debug!(
invoke_id = abt.invoke_id,
"Ignoring Abort PDU with server=FALSE"
);
return;
}
// Clause 5.4.4.4 AbortPDU_Received delivers ABORT.indication
// locally and returns to IDLE —
// ending the reassembly session is the IDLE half, and no
// reply PDU is prescribed (#367). This removal must stay
// below the server=FALSE guard above, or an echoed copy of
// this client's own Abort would tear down a healthy
// reassembly. Behaviorally redundant with the per-segment
// pending gate in `handle_segmented_complex_ack` — a later
// segment would find no transaction and clear the session
// anyway — so no test can pin this line alone; it is kept so
// the slot frees at the Abort, not at the peer's next move.
let key = (tsm_mac.clone(), abt.invoke_id, provenance);
let reassembly_owner = current_reassembly_owner(tsm, seg_state, &key).await;
debug!(invoke_id = abt.invoke_id, "Received Abort PDU");
// Carries no service choice (Clause 20.1.9).
let response = TsmResponse::remote(abt.remote_error());
let coordinator_apdu = Apdu::Abort(abt.clone());
let outcome = complete_terminal_response(
tsm,
TerminalResponse {
tsm_mac: &tsm_mac,
invoke_id: abt.invoke_id,
peer: &canonical_peer,
apdu: &coordinator_apdu,
response,
phase_gate: false,
owner: reassembly_owner.clone(),
},
)
.await;
if matches!(
outcome,
TerminalDispatchOutcome::Completion(CoordinatedCompletion::Completed(
CompletionOutcome::Delivered
))
) && let Some(owner) = reassembly_owner
&& seg_state
.get(&key)
.is_some_and(|state| state.owner.same_as(&owner))
{
seg_state.remove(&key);
}
}
Apdu::ConfirmedRequest(req) => {
let reply = InboundReply::new(response_route, reply_tx);
if is_group {
debug!(
invoke_id = req.invoke_id,
service = req.service_choice.to_raw(),
"Ignoring group-addressed ConfirmedRequest"
);
return;
}
if req.segmented {
Self::send_server_abort(
network,
source_mac,
source_network,
reply,
req.invoke_id,
bacnet_types::enums::AbortReason::SEGMENTATION_NOT_SUPPORTED,
)
.await;
return;
}
if req.service_choice == ConfirmedServiceChoice::CONFIRMED_COV_NOTIFICATION {
match COVNotificationRequest::decode_detailed(&req.service_request) {
Ok(notification) => {
debug!(
object = ?notification.monitored_object_identifier,
"Received ConfirmedCOVNotification"
);
let received = ReceivedCOVNotification::new(
notification,
source_mac,
source_network,
COVNotificationDelivery::Confirmed,
);
let response = confirmed_cov_ack_policy(&received);
let _ = cov_tx.send(received);
Self::send_confirmed_cov_notification_response(
network,
source_mac,
source_network,
req.invoke_id,
req.service_choice,
response,
reply,
)
.await;
}
Err(e) => {
let reject_reason = e.reject_reason();
let e = e.into_error();
warn_throttled!(
decode_warning,
error = %e,
"Failed to decode ConfirmedCOVNotification"
);
Self::send_confirmed_request_reject(
network,
source_mac,
source_network,
reply,
req.invoke_id,
reject_reason,
)
.await;
}
}
} else if req.service_choice == ConfirmedServiceChoice::CONFIRMED_EVENT_NOTIFICATION
{
Self::receive_confirmed_event_notification(
network,
event_tx,
source_mac,
source_network,
reply,
req,
decode_warning,
)
.await;
} else {
debug!(
service = req.service_choice.to_raw(),
"Rejecting unsupported ConfirmedRequest (client mode)"
);
Self::send_confirmed_request_reject(
network,
source_mac,
source_network,
reply,
req.invoke_id,
RejectReason::UNRECOGNIZED_SERVICE,
)
.await;
}
}
Apdu::UnconfirmedRequest(req) => {
if req.service_choice == UnconfirmedServiceChoice::I_AM {
match bacnet_services::who_is::IAmRequest::decode(&req.service_request) {
Ok(i_am) => {
debug!(
device = i_am.object_identifier.instance_number(),
vendor = i_am.vendor_id,
"Received IAm"
);
let (src_net, src_addr) = match source_network {
Some(npdu_addr) if !npdu_addr.mac_address.is_empty() => {
(Some(npdu_addr.network), Some(npdu_addr.mac_address.clone()))
}
_ => (None, None),
};
let device = DiscoveredDevice {
object_identifier: i_am.object_identifier,
mac_address: MacAddr::from_slice(source_mac),
max_apdu_length: i_am.max_apdu_length,
segmentation_supported: i_am.segmentation_supported,
max_segments_accepted: None,
vendor_id: i_am.vendor_id,
last_seen: std::time::Instant::now(),
source_network: src_net,
source_address: src_addr,
};
let status = {
let mut table = device_table.lock().await;
table.upsert_with_result(device.clone())
};
match status {
DeviceUpsertResult::Inserted => {
let _ = device_tx.send(DeviceEvent {
kind: DeviceEventKind::Discovered,
device,
});
}
DeviceUpsertResult::Updated => {
let _ = device_tx.send(DeviceEvent {
kind: DeviceEventKind::Updated,
device,
});
}
DeviceUpsertResult::Collision { retained } => {
let _ = device_collision_tx.send(DeviceCollisionEvent {
retained,
incoming: device,
});
}
DeviceUpsertResult::Dropped => {}
}
}
Err(e) => {
warn_throttled!(decode_warning, error = %e, "Failed to decode IAm");
}
}
} else if req.service_choice
== UnconfirmedServiceChoice::UNCONFIRMED_COV_NOTIFICATION
{
match COVNotificationRequest::decode(&req.service_request) {
Ok(notification) => {
debug!(
object = ?notification.monitored_object_identifier,
"Received UnconfirmedCOVNotification"
);
let received = ReceivedCOVNotification::new(
notification,
source_mac,
source_network,
COVNotificationDelivery::Unconfirmed,
);
let _ = cov_tx.send(received);
}
Err(e) => {
warn_throttled!(
decode_warning,
error = %e,
"Failed to decode UnconfirmedCOVNotification"
);
}
}
} else if req.service_choice
== UnconfirmedServiceChoice::UNCONFIRMED_EVENT_NOTIFICATION
{
event_notifications::receive_unconfirmed_event_notification(
event_tx,
source_mac,
source_network,
&req.service_request,
decode_warning,
);
} else if req.service_choice == UnconfirmedServiceChoice::I_HAVE {
i_have::deliver(
i_have_tx,
source_mac,
source_network,
&req.service_request,
decode_warning,
);
} else {
debug!(
service = req.service_choice.to_raw(),
"Ignoring unconfirmed service in client dispatch"
);
}
}
Apdu::SegmentAck(sa) => {
// Clause 5.4.4.2 gates DuplicateACK_Received, NewACK_Received
// and FinalACK_Received on the 'server' parameter being TRUE.
// A SegmentACK this client sent while receiving a segmented
// response carries server=FALSE and must not be fed back into
// its own segmented send.
//
// This check must stay ahead of the Abort below. A client
// emits SegmentACKs with 'server' = FALSE, so if the order
// were reversed, two of these clients exchanging a segmented
// ConfirmedCOVNotification would answer each other's
// SegmentACKs with Aborts indefinitely. Dropping them here is
// what keeps the Abort reachable only by a *server's* PDU.
if !sa.sent_by_server {
debug!(
invoke_id = sa.invoke_id,
"Ignoring SegmentAck with server=FALSE"
);
return;
}
let invoke_id = sa.invoke_id;
let key = (tsm_mac.clone(), invoke_id, provenance);
// SEGMENTED_CONF (5.4.4.4) `UnexpectedPDU_Received` lists
// a server-side BACnet-SegmentACK-PDU among the inappropriate
// PDUs. The response has three parts: send a client-side
// BACnet-Abort-PDU (`server` = FALSE), raise ABORT.indication to
// the local user carrying `abort-reason` INVALID_APDU_IN_THIS_STATE
// (again `server` = FALSE), and go back to IDLE.
//
// Receive state is checked before the outgoing phase because
// SEGMENTED_CONF gives this PDU the opposite disposition.
if let Some(state) = take_current_reassembly(tsm, seg_state, &key).await {
debug!(invoke_id, "Aborting reassembly on an unexpected SegmentAck");
// The stored identity, not this PDU's source. A key match
// already forces the inbound SNET/SADR to equal the
// stored pair (the key is derived from it), but the
// immediate MAC can diverge — a second router to the same
// peer — and reading both from the state keeps the
// (reply_mac, reply_network) pair consistent.
Self::abort_reassembly(
tsm,
network,
state.abort_target(&tsm_mac),
invoke_id,
bacnet_types::enums::AbortReason::INVALID_APDU_IN_THIS_STATE,
)
.await;
return;
}
// Hold the TSM lock through the route lookup and try_send.
// Terminal completion uses this same lock, so a raw sender
// entry cannot outlive the authoritative phase for delivery.
let tsm_guard = tsm.lock().await;
let coordinator_apdu = Apdu::SegmentAck(sa.clone());
match tsm_guard.coordinated_segment_ack_phase(
&tsm_mac,
invoke_id,
&canonical_peer,
&coordinator_apdu,
) {
SegmentAckPhase::SegmentedRequest(owner) => {
// Lock order is TSM then SegmentACK routes. Setup and
// cleanup never hold both locks. Compat mode: SegmentACK
// delivery stays provenance-agnostic (RB-09 later).
let send_key = (tsm_mac.clone(), invoke_id);
let senders = seg_ack_senders.lock().await;
if let Some(route) = senders
.get(&send_key)
.filter(|route| route.owner.same_as(&owner))
{
let _ = route.sender.try_send(sa);
}
return;
}
SegmentAckPhase::Outstanding => {
debug!(
invoke_id,
"Discarding duplicate SegmentAck for an outstanding transaction"
);
return;
}
SegmentAckPhase::CoordinatorRejected => return,
SegmentAckPhase::Idle => {}
}
drop(tsm_guard);
// IDLE (5.4.4.1) `UnexpectedSegmentInfoReceived`: nothing is
// outstanding, so a server SegmentACK earns a client Abort.
debug!(invoke_id, "Aborting SegmentAck received while idle");
Self::send_client_abort(
network,
source_mac,
source_network,
invoke_id,
bacnet_types::enums::AbortReason::INVALID_APDU_IN_THIS_STATE,
)
.await;
}
}
}
/// Send an ordinarily addressed reply or outgoing-client transaction control.
///
/// A PDU from a routed peer carries the peer's SNET/SADR; the reply must
/// carry that pair as DNET/DADR, unicast to the router that delivered the
/// original, or the router treats it as locally addressed and never
/// forwards it. Inbound direct confirmed-request replies instead use
/// the checked original-socket path in `inbound_replies`.
pub(super) async fn send_reply_apdu(
network: &Arc<NetworkLayer<T>>,
buf: &[u8],
reply_mac: &[u8],
reply_network: &Option<NpduAddress>,
) -> Result<(), Error> {
match reply_network {
Some(address) if !address.mac_address.is_empty() => {
network
.send_apdu_routed(
buf,
address.network,
&address.mac_address,
reply_mac,
false,
NetworkPriority::Normal,
)
.await
}
_ => {
network
.send_apdu(buf, reply_mac, false, NetworkPriority::Normal)
.await
}
}
}
}