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
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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
//! Transaction State Machine (TSM) per ASHRAE 135-2020 Clause 5.4.
//!
//! Tracks in-flight confirmed requests. Each request gets a unique invoke_id
//! (0-255) scoped per destination MAC. Responses are delivered via oneshot channels.

use bacnet_types::MacAddr;
use bacnet_types::enums::{AbortReason, ConfirmedServiceChoice};
use bacnet_types::error::RemoteError;
use bacnet_types::map_hash::BuildMapHasher;
use bytes::Bytes;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::oneshot;

mod final_segment;
#[cfg(test)]
pub(crate) use final_segment::TerminalResponseAdmission;
pub(crate) use final_segment::{FinalSegmentIssue, FinalSegmentSendToken};
mod segmented_response;
pub(crate) use segmented_response::SegmentedResponseAdmission;
mod completion;
mod coordinated;
mod progress;
pub(crate) use coordinated::{
    CoordinatedCompletion, CoordinatedTerminalPhase, SegmentedAckArrival,
};
use coordinated::{PendingLease, PendingRelease};
use progress::TransactionShared;
pub(crate) use progress::{ProgressReceiver, TransactionProgress};

/// TSM configuration.
#[derive(Debug, Clone)]
pub struct TsmConfig {
    /// APDU timeout in milliseconds (default 6000).
    pub apdu_timeout_ms: u64,
    /// APDU segment timeout in milliseconds (default = apdu_timeout_ms).
    pub apdu_segment_timeout_ms: u64,
    /// Number of APDU retries (default 3).
    pub apdu_retries: u8,
}

impl Default for TsmConfig {
    fn default() -> Self {
        Self {
            apdu_timeout_ms: 6000,
            apdu_segment_timeout_ms: 6000,
            apdu_retries: 3,
        }
    }
}

/// Response types that complete a transaction.
///
/// Non-exhaustive: the TSM gains completion reasons as more of Clause 5.4's
/// state machine is implemented, and those should be additive for callers.
#[derive(Debug)]
#[non_exhaustive]
pub enum TsmResponse {
    /// SimpleACK — confirmed service completed with no return data.
    SimpleAck,
    /// ComplexACK — confirmed service returned data.
    ComplexAck {
        /// Encoded service ACK parameters, without the APDU header.
        service_data: Bytes,
    },
    /// An Error, Reject or Abort PDU: the peer turned the request down.
    Remote {
        /// What the PDU reports.
        error: RemoteError,
        /// What an Error PDU's structured Clause 21 body adds to the class
        /// and code (ChangeList-Error, CreateObject-Error, WPM-Error and the
        /// others); `None` for a plain error, a Reject or an Abort.
        detail: Option<bacnet_types::error::ErrorDetail>,
    },
    /// A router rejected the active message as too long for its routed path.
    NetworkPathTooLong {
        /// Destination network named by the network-layer rejection.
        dnet: u16,
    },
}

impl TsmResponse {
    /// The completion an Error PDU produces. A structured body keeps its
    /// detail, such as a ChangeList-Error's First Failed Element Number or
    /// WritePropertyMultiple's first failed write attempt.
    pub(crate) fn from_error_pdu(pdu: &bacnet_encoding::apdu::ErrorPdu) -> Self {
        Self::Remote {
            error: pdu.remote_error(),
            detail: bacnet_services::structured_error::detail(pdu),
        }
    }

    /// The completion a Reject or Abort PDU produces, or an Error PDU
    /// without a structured body: `error` without a detail.
    pub const fn remote(error: RemoteError) -> Self {
        Self::Remote {
            error,
            detail: None,
        }
    }
}

/// Invoke ID allocator scoped to a single destination MAC.
struct InvokeIdAllocator {
    next_id: u8,
    in_use: [bool; 256],
}

impl InvokeIdAllocator {
    fn new() -> Self {
        Self {
            next_id: 0,
            in_use: [false; 256],
        }
    }

    fn allocate(&mut self) -> Option<u8> {
        let start = self.next_id;
        loop {
            let id = self.next_id;
            self.next_id = self.next_id.wrapping_add(1);
            if !self.in_use[id as usize] {
                self.in_use[id as usize] = true;
                return Some(id);
            }
            if self.next_id == start {
                return None;
            }
        }
    }

    fn release(&mut self, id: u8) {
        self.in_use[id as usize] = false;
    }

    fn all_free(&self) -> bool {
        !self.in_use.iter().any(|&used| used)
    }
}

/// Maximum number of distinct destination MACs tracked by the TSM.
/// Prevents unbounded memory growth from spoofed source addresses.
const MAX_TSM_DESTINATIONS: usize = 1024;

/// What `complete_transaction` did with a response.
#[derive(Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum CompletionOutcome {
    /// The response matched a pending transaction and was delivered.
    Delivered,
    /// No transaction was pending for this source and invoke ID.
    NoTransaction,
    /// A transaction was pending, but the response was labelled for a
    /// different confirmed service. The transaction is left pending and the
    /// invoke ID stays allocated, so the legitimate response can still arrive.
    ServiceChoiceMismatch {
        /// The service the pending request asked for.
        expected: ConfirmedServiceChoice,
        /// The service the response claimed to answer.
        observed: ConfirmedServiceChoice,
    },
}

#[derive(Debug)]
pub(crate) enum SegmentAckPhase {
    SegmentedRequest(TransactionOwner),
    Outstanding,
    CoordinatorRejected,
    Idle,
}

pub(crate) struct TransactionRegistration {
    pub(crate) response: oneshot::Receiver<TsmResponse>,
    pub(crate) progress: ProgressReceiver,
    pub(crate) owner: TransactionOwner,
}

/// Identity of one registration, independent of its reusable wire key.
///
/// Pointer identity is stable while delayed work retains a clone. The
/// allocation therefore cannot be reused for a replacement transaction until
/// every stale owner reference has gone away.
///
/// The same allocation carries the request-side timer progress (#1727), so
/// a transaction costs one shared allocation rather than two.
#[derive(Clone, Debug)]
pub(crate) struct TransactionOwner(Arc<TransactionShared>);

impl TransactionOwner {
    fn new() -> Self {
        Self(Arc::new(TransactionShared::new()))
    }

    pub(crate) fn same_as(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.0, &other.0)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum RequestTimerExpiration {
    Retry,
    SegmentedResponse { generation: u64 },
    TimedOut,
    NoTransaction,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum SegmentTimerExpiration {
    Activity { generation: u64 },
    AwaitingResponse,
    TimedOut,
    NoTransaction,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum TransactionPhase {
    AwaitingResponse,
    SegmentedRequest { sent_all_segments: bool },
    SegmentedResponse,
}

/// A confirmed request awaiting its response.
struct PendingTransaction {
    responder: oneshot::Sender<TsmResponse>,
    /// Also publishes this transaction's timer progress.
    owner: TransactionOwner,
    phase: TransactionPhase,
    final_segment_issue: Option<FinalSegmentIssue>,
    /// Monotonic token used to reject a SegmentTimer expiry observed before
    /// newer segment activity acquired the TSM lock.
    segment_generation: u64,
    /// The service this request asked for. Clause 20.1.4.2 and 20.1.5.6 both
    /// require service-ack-choice to match the BACnetConfirmedServiceChoice
    /// of the request being acknowledged. A different choice therefore
    /// cannot identify this transaction's response.
    expected_service_choice: ConfirmedServiceChoice,
    lease: PendingLease,
}

/// Transaction State Machine.
///
/// Tracks pending confirmed requests and correlates responses by
/// `(destination_mac, invoke_id)` and by the confirmed service each request
/// asked for.
pub struct Tsm {
    config: TsmConfig,
    // Only this client inserts: see bacnet_types::map_hash (#1713).
    allocators: HashMap<MacAddr, InvokeIdAllocator, BuildMapHasher>,
    pending: HashMap<(MacAddr, u8), PendingTransaction, BuildMapHasher>,
    coordinator: Option<Arc<bacnet_endpoint_core::coordinator::OutboundTransactionCoordinator>>,
}

impl Tsm {
    /// Create a transaction state machine with the given timing and retry configuration.
    pub fn new(config: TsmConfig) -> Self {
        Self {
            config,
            allocators: HashMap::default(),
            pending: HashMap::default(),
            coordinator: None,
        }
    }

    /// Timing and retry configuration in force.
    pub fn config(&self) -> &TsmConfig {
        &self.config
    }

    /// Allocate an invoke ID for the given destination MAC.
    /// Returns `None` if all 256 IDs are in use for this destination,
    /// or if the maximum number of tracked destinations has been reached.
    pub fn allocate_invoke_id(&mut self, destination_mac: &[u8]) -> Option<u8> {
        let key = MacAddr::from_slice(destination_mac);
        if !self.allocators.contains_key(&key) && self.allocators.len() >= MAX_TSM_DESTINATIONS {
            return None;
        }
        let allocator = self
            .allocators
            .entry(key)
            .or_insert_with(InvokeIdAllocator::new);
        allocator.allocate()
    }

    /// Release an invoke ID back to the pool for the given destination.
    /// Removes the allocator entry if all IDs are now free (prevents unbounded growth).
    pub fn release_invoke_id(&mut self, destination_mac: &[u8], invoke_id: u8) {
        let key = MacAddr::from_slice(destination_mac);
        if let Some(allocator) = self.allocators.get_mut(&key) {
            allocator.release(invoke_id);
            if allocator.all_free() {
                self.allocators.remove(&key);
            }
        }
    }

    /// Register a pending transaction. Returns a receiver that will deliver
    /// the response when it arrives.
    ///
    /// `service_choice` is the confirmed service being requested; a response
    /// labelled for any other service will not complete this transaction.
    pub fn register_transaction(
        &mut self,
        destination_mac: MacAddr,
        invoke_id: u8,
        service_choice: ConfirmedServiceChoice,
    ) -> oneshot::Receiver<TsmResponse> {
        self.register_transaction_with_progress(destination_mac, invoke_id, service_choice)
            .response
    }

    pub(crate) fn register_transaction_with_progress(
        &mut self,
        destination_mac: MacAddr,
        invoke_id: u8,
        service_choice: ConfirmedServiceChoice,
    ) -> TransactionRegistration {
        self.register_transaction_in_phase(
            destination_mac,
            invoke_id,
            service_choice,
            TransactionPhase::AwaitingResponse,
            PendingLease::Legacy,
        )
    }

    #[cfg(test)]
    pub(crate) fn register_segmented_transaction_with_progress(
        &mut self,
        destination_mac: MacAddr,
        invoke_id: u8,
        service_choice: ConfirmedServiceChoice,
    ) -> TransactionRegistration {
        self.register_transaction_in_phase(
            destination_mac,
            invoke_id,
            service_choice,
            TransactionPhase::SegmentedRequest {
                sent_all_segments: false,
            },
            PendingLease::Legacy,
        )
    }

    fn register_transaction_in_phase(
        &mut self,
        destination_mac: MacAddr,
        invoke_id: u8,
        service_choice: ConfirmedServiceChoice,
        phase: TransactionPhase,
        lease: PendingLease,
    ) -> TransactionRegistration {
        let (pending, registration) = Self::new_pending_transaction(service_choice, phase, lease);
        debug_assert!(
            !self
                .pending
                .contains_key(&(destination_mac.clone(), invoke_id)),
            "duplicate TSM registration for invoke_id {}",
            invoke_id
        );
        self.pending.insert((destination_mac, invoke_id), pending);
        registration
    }

    fn new_pending_transaction(
        service_choice: ConfirmedServiceChoice,
        phase: TransactionPhase,
        lease: PendingLease,
    ) -> (PendingTransaction, TransactionRegistration) {
        let (tx, rx) = oneshot::channel();
        let owner = TransactionOwner::new();
        let final_segment_issue =
            matches!(phase, TransactionPhase::SegmentedRequest { .. }).then(FinalSegmentIssue::new);
        (
            PendingTransaction {
                responder: tx,
                owner: owner.clone(),
                phase,
                final_segment_issue,
                segment_generation: 0,
                expected_service_choice: service_choice,
                lease,
            },
            TransactionRegistration {
                response: rx,
                progress: ProgressReceiver::new(owner.clone()),
                owner,
            },
        )
    }

    /// Reserve the first poll of the final N-UNITDATA.request. A terminal PDU
    /// that reaches dispatch while this token is live waits for that poll to
    /// resolve instead of being admitted or rejected early.
    pub(crate) fn begin_final_segment_send(
        &mut self,
        destination_mac: &[u8],
        invoke_id: u8,
        owner: &TransactionOwner,
    ) -> Option<FinalSegmentSendToken> {
        let key = (MacAddr::from_slice(destination_mac), invoke_id);
        let pending = self.pending.get(&key)?;
        if !pending.owner.same_as(owner)
            || !matches!(
                pending.phase,
                TransactionPhase::SegmentedRequest {
                    sent_all_segments: false
                }
            )
        {
            return None;
        }
        let issue = pending.final_segment_issue.as_ref()?.clone();
        issue.begin().then_some(FinalSegmentSendToken {
            issue,
            resolved: false,
        })
    }

    /// Publish `SentAllSegments` after the final send future has been polled.
    /// The caller must not hold the TSM lock while performing that poll.
    pub(crate) fn mark_final_segment_issued(
        &mut self,
        destination_mac: &[u8],
        invoke_id: u8,
        owner: &TransactionOwner,
        token: &mut FinalSegmentSendToken,
    ) -> bool {
        let key = (MacAddr::from_slice(destination_mac), invoke_id);
        let Some(pending) = self.pending.get_mut(&key) else {
            return false;
        };
        if !pending.owner.same_as(owner) {
            return false;
        }
        let TransactionPhase::SegmentedRequest { sent_all_segments } = &mut pending.phase else {
            return false;
        };
        if !pending
            .final_segment_issue
            .as_ref()
            .is_some_and(|issue| issue.same_as(&token.issue))
            || !token.issue.is_polling()
        {
            return false;
        }
        *sent_all_segments = true;
        token.issued();
        true
    }

    /// FinalACK_Received moves the same owner into AWAIT_CONFIRMATION.
    pub(crate) fn finish_segmented_request(
        &mut self,
        destination_mac: &[u8],
        invoke_id: u8,
        owner: &TransactionOwner,
    ) -> bool {
        let key = (MacAddr::from_slice(destination_mac), invoke_id);
        let Some(pending) = self.pending.get_mut(&key) else {
            return false;
        };
        if !pending.owner.same_as(owner)
            || !matches!(
                pending.phase,
                TransactionPhase::SegmentedRequest {
                    sent_all_segments: true
                }
            )
        {
            return false;
        }
        pending.phase = TransactionPhase::AwaitingResponse;
        pending
            .owner
            .publish_progress(TransactionProgress::AwaitingResponse);
        true
    }

    pub(crate) fn segment_ack_phase(&self, source_mac: &[u8], invoke_id: u8) -> SegmentAckPhase {
        let key = (MacAddr::from_slice(source_mac), invoke_id);
        match self.pending.get(&key) {
            Some(pending) if matches!(pending.phase, TransactionPhase::SegmentedRequest { .. }) => {
                SegmentAckPhase::SegmentedRequest(pending.owner.clone())
            }
            Some(_) => SegmentAckPhase::Outstanding,
            None => SegmentAckPhase::Idle,
        }
    }

    #[cfg(test)]
    pub(crate) fn admit_terminal_response(
        &mut self,
        source_mac: &[u8],
        invoke_id: u8,
        owner: Option<&TransactionOwner>,
    ) -> TerminalResponseAdmission {
        let key = (MacAddr::from_slice(source_mac), invoke_id);
        let Some(pending) = self.pending.get(&key) else {
            return TerminalResponseAdmission::NoTransaction;
        };
        if owner.is_some_and(|owner| !pending.owner.same_as(owner)) {
            return TerminalResponseAdmission::NoTransaction;
        }
        let current_owner = pending.owner.clone();
        if matches!(
            pending.phase,
            TransactionPhase::SegmentedRequest {
                sent_all_segments: false
            }
        ) {
            if pending
                .final_segment_issue
                .as_ref()
                .is_some_and(|issue| issue.is_polling())
            {
                return TerminalResponseAdmission::FinalSegmentSendPolling;
            }
            self.abort_invalid_apdu_in_current_state(source_mac, invoke_id, &current_owner);
            return TerminalResponseAdmission::PrematureSegmentedRequestAborted;
        }
        TerminalResponseAdmission::Active(current_owner)
    }

    /// Enter SEGMENTED_CONF after the first segment has been saved.
    ///
    /// The transition and RequestTimer retry authorization use the same TSM
    /// lock. Whichever changes or observes the phase first wins that decision;
    /// an authorized retry performs transport I/O only after releasing it.
    pub(crate) fn begin_segmented_response(
        &mut self,
        source_mac: &[u8],
        invoke_id: u8,
        owner: &TransactionOwner,
    ) -> Option<u64> {
        let key = (MacAddr::from_slice(source_mac), invoke_id);
        let pending = self.pending.get_mut(&key)?;
        if !pending.owner.same_as(owner) {
            return None;
        }
        if matches!(
            pending.phase,
            TransactionPhase::SegmentedRequest {
                sent_all_segments: false
            }
        ) {
            return None;
        }
        pending.segment_generation = pending.segment_generation.wrapping_add(1);
        pending.phase = TransactionPhase::SegmentedResponse;
        let generation = pending.segment_generation;
        pending
            .owner
            .publish_progress(TransactionProgress::SegmentedResponse { generation });
        Some(generation)
    }

    /// Restart SegmentTimer for a segment handled in SEGMENTED_CONF.
    pub(crate) fn record_segmented_response_activity(
        &mut self,
        source_mac: &[u8],
        invoke_id: u8,
        owner: &TransactionOwner,
    ) -> Option<u64> {
        let key = (MacAddr::from_slice(source_mac), invoke_id);
        let pending = self.pending.get_mut(&key)?;
        if !pending.owner.same_as(owner) || pending.phase != TransactionPhase::SegmentedResponse {
            return None;
        }
        pending.segment_generation = pending.segment_generation.wrapping_add(1);
        let generation = pending.segment_generation;
        pending
            .owner
            .publish_progress(TransactionProgress::SegmentedResponse { generation });
        Some(generation)
    }

    /// Resume AWAIT_CONFIRMATION when a completed reassembly is not this
    /// transaction's response or cannot be delivered.
    pub(crate) fn reset_segmented_response(
        &mut self,
        source_mac: &[u8],
        invoke_id: u8,
        owner: &TransactionOwner,
    ) -> bool {
        let key = (MacAddr::from_slice(source_mac), invoke_id);
        let Some(pending) = self.pending.get_mut(&key) else {
            return false;
        };
        if !pending.owner.same_as(owner) || pending.phase != TransactionPhase::SegmentedResponse {
            return false;
        }
        pending.phase = TransactionPhase::AwaitingResponse;
        pending
            .owner
            .publish_progress(TransactionProgress::AwaitingResponse);
        true
    }

    /// Arbitrate RequestTimer against the receive-side phase transition.
    ///
    /// Returning [`RequestTimerExpiration::Retry`] authorizes one retry. The
    /// caller then releases the enclosing TSM lock before transport I/O; a
    /// segmented-response transition that follows does not revoke that send.
    pub(crate) fn expire_request_timer(
        &mut self,
        destination_mac: &[u8],
        invoke_id: u8,
        owner: &TransactionOwner,
        final_timeout: bool,
    ) -> RequestTimerExpiration {
        let key = (MacAddr::from_slice(destination_mac), invoke_id);
        let Some(pending) = self.pending.get(&key) else {
            return RequestTimerExpiration::NoTransaction;
        };
        if !pending.owner.same_as(owner) {
            return RequestTimerExpiration::NoTransaction;
        }
        if pending.phase == TransactionPhase::SegmentedResponse {
            return RequestTimerExpiration::SegmentedResponse {
                generation: pending.segment_generation,
            };
        }
        if !final_timeout {
            return RequestTimerExpiration::Retry;
        }
        let pending = self
            .pending
            .remove(&key)
            .expect("pending transaction exists");
        self.release_pending(destination_mac, invoke_id, pending, PendingRelease::Release);
        RequestTimerExpiration::TimedOut
    }

    /// Cancel only if no segment activity has advanced past `generation`.
    pub(crate) fn expire_segment_timer(
        &mut self,
        destination_mac: &[u8],
        invoke_id: u8,
        owner: &TransactionOwner,
        generation: u64,
    ) -> SegmentTimerExpiration {
        let key = (MacAddr::from_slice(destination_mac), invoke_id);
        let Some(pending) = self.pending.get(&key) else {
            return SegmentTimerExpiration::NoTransaction;
        };
        if !pending.owner.same_as(owner) {
            return SegmentTimerExpiration::NoTransaction;
        }
        if pending.phase == TransactionPhase::AwaitingResponse {
            return SegmentTimerExpiration::AwaitingResponse;
        }
        if pending.segment_generation != generation {
            return SegmentTimerExpiration::Activity {
                generation: pending.segment_generation,
            };
        }
        let pending = self
            .pending
            .remove(&key)
            .expect("pending transaction exists");
        self.release_pending(destination_mac, invoke_id, pending, PendingRelease::Release);
        SegmentTimerExpiration::TimedOut
    }

    /// The confirmed service a pending transaction is waiting on, if any.
    ///
    /// Doubles as the "is a transaction pending" predicate: an inbound
    /// segmented response for an invoke ID nobody is waiting on should not be
    /// allocated a reassembly session.
    pub fn expected_service_choice(
        &self,
        source_mac: &[u8],
        invoke_id: u8,
    ) -> Option<ConfirmedServiceChoice> {
        let key = (MacAddr::from_slice(source_mac), invoke_id);
        self.pending.get(&key).map(|p| p.expected_service_choice)
    }

    pub(crate) fn owner_is_current(
        &self,
        source_mac: &[u8],
        invoke_id: u8,
        owner: &TransactionOwner,
    ) -> bool {
        let key = (MacAddr::from_slice(source_mac), invoke_id);
        self.pending
            .get(&key)
            .is_some_and(|pending| pending.owner.same_as(owner))
    }

    fn abort_invalid_apdu_in_current_state(
        &mut self,
        source_mac: &[u8],
        invoke_id: u8,
        owner: &TransactionOwner,
    ) {
        let _ = self.complete_transaction_for_owner(
            source_mac,
            invoke_id,
            owner,
            None,
            TsmResponse::remote(RemoteError::Abort {
                reason: AbortReason::INVALID_APDU_IN_THIS_STATE,
            }),
        );
    }

    /// Cancel a pending transaction. Returns `true` if found.
    pub fn cancel_transaction(&mut self, destination_mac: &[u8], invoke_id: u8) -> bool {
        self.cancel_transaction_inner(destination_mac, invoke_id, None)
    }

    pub(crate) fn cancel_transaction_for_owner(
        &mut self,
        destination_mac: &[u8],
        invoke_id: u8,
        owner: &TransactionOwner,
    ) -> bool {
        self.cancel_transaction_inner(destination_mac, invoke_id, Some(owner))
    }

    fn cancel_transaction_inner(
        &mut self,
        destination_mac: &[u8],
        invoke_id: u8,
        owner: Option<&TransactionOwner>,
    ) -> bool {
        let key = (MacAddr::from_slice(destination_mac), invoke_id);
        if owner.is_some_and(|owner| {
            self.pending
                .get(&key)
                .is_none_or(|pending| !pending.owner.same_as(owner))
        }) {
            return false;
        }
        match self.pending.remove(&key) {
            Some(pending) => {
                self.release_pending(destination_mac, invoke_id, pending, PendingRelease::Cancel);
                true
            }
            _ => false,
        }
    }

    /// Number of confirmed transactions currently awaiting a response.
    pub fn pending_count(&self) -> usize {
        self.pending.len()
    }

    /// Whether a transaction keyed to `tsm_mac` with `invoke_id` is pending.
    pub(crate) fn has_transaction(&self, tsm_mac: &[u8], invoke_id: u8) -> bool {
        self.pending
            .contains_key(&(MacAddr::from_slice(tsm_mac), invoke_id))
    }
}

#[cfg(test)]
#[path = "tsm/coordinated_tests.rs"]
mod coordinated_tests;
#[cfg(test)]
mod tests;