rs-matter 0.2.0

Native Rust implementation of the Matter (Smart-Home) ecosystem
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
/*
 *
 *    Copyright (c) 2026 Project CHIP Authors
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

//! Implementation of the Matter Push AV Stream Transport cluster (0x0555).
//!
//! Lets a camera push audio/video streams to an external endpoint (e.g.
//! a cloud recorder or NVR) using CMAF/DASH over TLS, as an alternative
//! to the pull-style WebRTC transport.
//!
//! # Architecture (Pattern B1 — "Hooks")
//!
//! [`PushAvStreamHandler`] owns the spec-defined state — the
//! `CurrentConnections` table, stable connection IDs, the
//! `SupportedFormats` catalogue — and performs all spec validation
//! before delegating the side-effecting bits (open / modify / close
//! the actual outgoing TLS push) to a user-supplied
//! [`PushAvStreamHooks`] implementation.
//!
//! # Storage
//!
//! `TransportOptionsStruct` is large (11 fields including 2 nested
//! structs and 2 nullable arrays). Rather than mirror every leaf in
//! a dedicated owned type, the handler captures the request's TLV
//! bytes verbatim into a per-connection
//! [`Vec<u8, MAX_TRANSPORT_OPTIONS_BYTES>`] and replays them on
//! attribute reads / `FindTransport`. This keeps the in-memory
//! footprint predictable and faithful to whatever the controller
//! sent without round-tripping through Rust types we'd have to keep
//! in sync with codegen.
//!
//! # Const generics
//!
//! * `NC` — maximum number of concurrently allocated push connections.
//!   Spec MinLimit is 1 for any device advertising the cluster;
//!   typical values 2..=8.
//!
//! # Scope of v1
//!
//! * Full validation, allocation, modification, deallocation,
//!   set-status, manually-trigger, find of push transport
//!   connections.
//! * `SupportedFormats` advertises the `(ContainerFormat, IngestMethod)`
//!   pairs the device can produce.
//! * Round-trip of `TransportOptionsStruct` is byte-for-byte (raw
//!   TLV pass-through) on `CurrentConnections` reads and
//!   `FindTransport` responses.
//! * NOT in scope (left to the application via hooks):
//!   `PerZoneSensitivity` / `Metadata` features beyond echoing the
//!   client-supplied bits in stored options. The cluster declares
//!   neither feature in [`Self::CLUSTER`]; build a custom `Cluster`
//!   value if you need them.

use core::cell::{Cell, RefCell};

use crate::dm::FabricIndex;
use crate::dm::{ArrayAttributeRead, Cluster, Dataver, EndptId, InvokeContext, ReadContext};
use crate::error::{Error, ErrorCode};
use crate::tlv::{TLVBuilderParent, TLVElement, TLVTag, ToTLV};
use crate::utils::storage::{Vec, WriteBuf};
use crate::utils::sync::blocking::Mutex;
use crate::with;

#[allow(unused_imports)]
pub use crate::dm::clusters::decl::push_av_stream_transport::*;

use super::super::decl::push_av_stream_transport as decl;

/// Maximum size, in bytes, of a single connection's serialized
/// `TransportOptionsStruct`. Sized to fit the worst-case CMAF-with-CENC
/// shape (URL up to 256 chars, two CENC keys, motion zones). Increase
/// at the call site by re-sizing the [`Vec`] type if your deployment
/// pushes the limit.
pub const MAX_TRANSPORT_OPTIONS_BYTES: usize = 768;

/// Errors a [`PushAvStreamHooks`] implementation can surface back to
/// the cluster. Each maps to a Matter cluster-status code (most via
/// the generic `ErrorCode` set; cluster-specific codes are emitted
/// directly by the handler from [`Self::CLUSTER`]).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum PushAvError {
    /// `FAILURE` — generic hooks-level failure.
    Failure,
    /// `NOT_FOUND` — referenced connection ID does not exist.
    NotFound,
    /// `RESOURCE_EXHAUSTED` — application cannot accept another
    /// concurrent push.
    ResourceExhausted,
    /// `CONSTRAINT_ERROR` — combination of params is unsupported at
    /// runtime (e.g. URL host unreachable, codec unavailable on the
    /// requested stream).
    DynamicConstraint,
    /// `INVALID_IN_STATE` — cluster-specific (e.g. trigger before
    /// allocate).
    InvalidInState,
}

impl From<PushAvError> for Error {
    fn from(e: PushAvError) -> Self {
        match e {
            PushAvError::Failure => ErrorCode::Failure.into(),
            PushAvError::NotFound => ErrorCode::NotFound.into(),
            PushAvError::ResourceExhausted => ErrorCode::ResourceExhausted.into(),
            PushAvError::DynamicConstraint => ErrorCode::ConstraintError.into(),
            PushAvError::InvalidInState => ErrorCode::InvalidAction.into(),
        }
    }
}

/// One row in the `SupportedFormats` attribute.
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct SupportedFormat {
    pub container_format: ContainerFormatEnum,
    pub ingest_method: IngestMethodsEnum,
}

/// One row in the `CurrentConnections` attribute. Stored verbatim by
/// [`PushAvStreamHandler`].
#[derive(Debug, Clone)]
pub struct PushConnection {
    /// Stable, server-assigned ID handed back in
    /// `AllocatePushTransportResponse`.
    pub connection_id: u16,
    /// Fabric the connection belongs to (taken from the invoking
    /// command's accessing fabric — fabric-scoped per spec).
    pub fabric_index: FabricIndex,
    /// Mutable status flipped by `SetTransportStatus` /
    /// `ManuallyTriggerTransport`.
    pub status: TransportStatusEnum,
    /// Raw TLV bytes of the `TransportOptionsStruct` as received in
    /// the original `AllocatePushTransport` (or, after a successful
    /// `ModifyPushTransport`, the new options). Replayed on
    /// attribute reads.
    pub transport_options: Vec<u8, MAX_TRANSPORT_OPTIONS_BYTES>,
}

/// Static configuration for a [`PushAvStreamHandler`]. Currently just
/// the supported-formats catalogue; expand here when you need
/// per-deployment knobs.
#[derive(Debug, Clone, Copy)]
pub struct PushAvStreamConfig<'a> {
    pub supported_formats: &'a [SupportedFormat],
}

/// Application hooks. All spec validation (URL non-empty, stream
/// reference checks if you wire them up via the cam-av-stream
/// reference-counting helpers, capacity, fabric scoping) is done by
/// [`PushAvStreamHandler`] before any of these methods run.
///
/// Implementors only need to interact with their actual upload
/// pipeline (open the TLS connection, push CMAF segments, etc.).
pub trait PushAvStreamHooks {
    /// Called when a new push connection has been validated and
    /// assigned a stable ID. The implementation should provision the
    /// upload pipeline; returning `Err` aborts the allocation (the
    /// connection is NOT added to `CurrentConnections` and no ID is
    /// returned to the controller).
    fn on_allocate(
        &self,
        connection_id: u16,
        fabric_index: FabricIndex,
        request: &AllocatePushTransportRequest<'_>,
    ) -> impl core::future::Future<Output = Result<(), PushAvError>>;

    /// Called when a controller requests `DeallocatePushTransport`
    /// AND the handler has confirmed the connection exists and is
    /// owned by the accessing fabric. On `Err` the row is left in
    /// place.
    fn on_deallocate(
        &self,
        _connection_id: u16,
    ) -> impl core::future::Future<Output = Result<(), PushAvError>> {
        async { Ok(()) }
    }

    /// Called on `ModifyPushTransport`. The handler has already
    /// confirmed existence and fabric scoping; on success the stored
    /// raw options are replaced.
    fn on_modify(
        &self,
        _connection_id: u16,
        _request: &ModifyPushTransportRequest<'_>,
    ) -> impl core::future::Future<Output = Result<(), PushAvError>> {
        async { Ok(()) }
    }

    /// Called on `SetTransportStatus`. `connection_id == None` means
    /// "all connections of the accessing fabric" per spec. Status is
    /// applied to the cluster table on `Ok`.
    fn on_set_status(
        &self,
        _connection_id: Option<u16>,
        _status: TransportStatusEnum,
    ) -> impl core::future::Future<Output = Result<(), PushAvError>> {
        async { Ok(()) }
    }

    /// Called on `ManuallyTriggerTransport` — request the upload
    /// pipeline to begin (or extend) a push. `time_control` carries
    /// the optional motion-trigger envelope; `user_defined` carries
    /// the optional opaque blob from the spec's `userDefined` field.
    fn on_manually_trigger(
        &self,
        _connection_id: u16,
        _activation_reason: TriggerActivationReasonEnum,
        _time_control: Option<TransportMotionTriggerTimeControlStruct<'_>>,
        _user_defined: Option<&[u8]>,
    ) -> impl core::future::Future<Output = Result<(), PushAvError>> {
        async { Ok(()) }
    }
}

impl<T> PushAvStreamHooks for &T
where
    T: PushAvStreamHooks,
{
    fn on_allocate(
        &self,
        connection_id: u16,
        fabric_index: FabricIndex,
        request: &AllocatePushTransportRequest<'_>,
    ) -> impl core::future::Future<Output = Result<(), PushAvError>> {
        (*self).on_allocate(connection_id, fabric_index, request)
    }

    fn on_deallocate(
        &self,
        connection_id: u16,
    ) -> impl core::future::Future<Output = Result<(), PushAvError>> {
        (*self).on_deallocate(connection_id)
    }

    fn on_modify(
        &self,
        connection_id: u16,
        request: &ModifyPushTransportRequest<'_>,
    ) -> impl core::future::Future<Output = Result<(), PushAvError>> {
        (*self).on_modify(connection_id, request)
    }

    fn on_set_status(
        &self,
        connection_id: Option<u16>,
        status: TransportStatusEnum,
    ) -> impl core::future::Future<Output = Result<(), PushAvError>> {
        (*self).on_set_status(connection_id, status)
    }

    fn on_manually_trigger(
        &self,
        connection_id: u16,
        activation_reason: TriggerActivationReasonEnum,
        time_control: Option<TransportMotionTriggerTimeControlStruct<'_>>,
        user_defined: Option<&[u8]>,
    ) -> impl core::future::Future<Output = Result<(), PushAvError>> {
        (*self).on_manually_trigger(connection_id, activation_reason, time_control, user_defined)
    }
}

struct State<const NC: usize> {
    connections: Vec<PushConnection, NC>,
}

impl<const NC: usize> State<NC> {
    const fn new() -> Self {
        Self {
            connections: Vec::new(),
        }
    }
}

/// Handler for the Push AV Stream Transport cluster (0x0555).
pub struct PushAvStreamHandler<'a, H, const NC: usize>
where
    H: PushAvStreamHooks,
{
    dataver: Dataver,
    endpoint_id: EndptId,
    config: PushAvStreamConfig<'a>,
    hooks: H,
    state: Mutex<RefCell<State<NC>>>,
    next_id: Mutex<Cell<u16>>,
}

impl<'a, H, const NC: usize> PushAvStreamHandler<'a, H, NC>
where
    H: PushAvStreamHooks,
{
    /// Cluster metadata advertising the mandatory attribute / command
    /// set without optional features. Build a custom [`Cluster`]
    /// value via `decl::FULL_CLUSTER.with_features(...)` for
    /// `PER_ZONE_SENSITIVITY` / `METADATA`.
    pub const CLUSTER: Cluster<'static> = decl::FULL_CLUSTER
        .with_revision(2)
        .with_attrs(with!(
            required;
            AttributeId::SupportedFormats | AttributeId::CurrentConnections
        ))
        .with_cmds(with!(
            decl::CommandId::AllocatePushTransport
                | decl::CommandId::DeallocatePushTransport
                | decl::CommandId::ModifyPushTransport
                | decl::CommandId::SetTransportStatus
                | decl::CommandId::ManuallyTriggerTransport
                | decl::CommandId::FindTransport
        ));

    /// Construct a new handler.
    pub const fn new(
        dataver: Dataver,
        endpoint_id: EndptId,
        config: PushAvStreamConfig<'a>,
        hooks: H,
    ) -> Self {
        Self {
            dataver,
            endpoint_id,
            config,
            hooks,
            state: Mutex::new(RefCell::new(State::new())),
            next_id: Mutex::new(Cell::new(1)),
        }
    }

    /// Wrap in the generic async adaptor for registration with a
    /// `rs-matter` `Node`.
    pub const fn adapt(self) -> decl::HandlerAsyncAdaptor<Self> {
        decl::HandlerAsyncAdaptor(self)
    }

    /// Endpoint this handler is mounted on.
    pub const fn endpoint_id(&self) -> EndptId {
        self.endpoint_id
    }

    /// Snapshot the current connections. Useful for diagnostics.
    pub fn connections(&self) -> Vec<PushConnection, NC> {
        self.state.lock(|cell| cell.borrow().connections.clone())
    }

    /// Allocate the next free `connection_id` (wraps to 1 on `u16`
    /// overflow).
    fn alloc_id(&self) -> u16 {
        self.next_id.lock(|cell| {
            let mut id = cell.get();
            if id == 0 {
                id = 1;
            }
            cell.set(id.wrapping_add(1).max(1));
            id
        })
    }

    /// Capture `options` (a request-side `TransportOptionsStruct`)
    /// into a fresh raw-TLV buffer.
    fn capture_options(
        &self,
        options: &TransportOptionsStruct<'_>,
    ) -> Result<Vec<u8, MAX_TRANSPORT_OPTIONS_BYTES>, Error> {
        let mut buf = [0u8; MAX_TRANSPORT_OPTIONS_BYTES];
        let mut wb = WriteBuf::new(&mut buf);
        options.to_tlv(&TLVTag::Anonymous, &mut wb)?;
        let bytes = wb.as_slice();
        let mut stored: Vec<u8, MAX_TRANSPORT_OPTIONS_BYTES> = Vec::new();
        stored
            .extend_from_slice(bytes)
            .map_err(|_| Error::from(ErrorCode::ResourceExhausted))?;
        Ok(stored)
    }
}

impl<'a, H, const NC: usize> ClusterAsyncHandler for PushAvStreamHandler<'a, H, NC>
where
    H: PushAvStreamHooks,
{
    const CLUSTER: Cluster<'static> = Self::CLUSTER;

    fn dataver(&self) -> u32 {
        self.dataver.get()
    }

    fn dataver_changed(&self) {
        self.dataver.changed();
    }

    // ----- Attributes -----

    async fn supported_formats<P: TLVBuilderParent>(
        &self,
        _ctx: impl ReadContext,
        builder: ArrayAttributeRead<
            SupportedFormatStructArrayBuilder<P>,
            SupportedFormatStructBuilder<P>,
        >,
    ) -> Result<P, Error> {
        match builder {
            ArrayAttributeRead::ReadAll(mut b) => {
                for f in self.config.supported_formats {
                    b = write_supported_format(b.push()?, f)?;
                }
                b.end()
            }
            ArrayAttributeRead::ReadOne(idx, b) => {
                let Some(f) = self.config.supported_formats.get(idx as usize) else {
                    return Err(ErrorCode::ConstraintError.into());
                };
                write_supported_format(b, f)
            }
            ArrayAttributeRead::ReadNone(b) => b.end(),
        }
    }

    async fn current_connections<P: TLVBuilderParent>(
        &self,
        ctx: impl ReadContext,
        builder: ArrayAttributeRead<
            TransportConfigurationStructArrayBuilder<P>,
            TransportConfigurationStructBuilder<P>,
        >,
    ) -> Result<P, Error> {
        let attr = ctx.attr();
        let mut snapshot: Vec<PushConnection, NC> = Vec::new();
        self.state.lock(|cell| {
            for c in cell.borrow().connections.iter() {
                if !attr.fab_filter || c.fabric_index == attr.fab_idx {
                    let _ = snapshot.push(c.clone());
                }
            }
        });

        match builder {
            ArrayAttributeRead::ReadAll(mut b) => {
                for c in snapshot.iter() {
                    b = write_connection(b.push()?, c)?;
                }
                b.end()
            }
            ArrayAttributeRead::ReadOne(idx, b) => {
                let Some(c) = snapshot.get(idx as usize) else {
                    return Err(ErrorCode::ConstraintError.into());
                };
                write_connection(b, c)
            }
            ArrayAttributeRead::ReadNone(b) => b.end(),
        }
    }

    // ----- Commands -----

    async fn handle_allocate_push_transport<P: TLVBuilderParent>(
        &self,
        ctx: impl InvokeContext,
        request: AllocatePushTransportRequest<'_>,
        response: AllocatePushTransportResponseBuilder<P>,
    ) -> Result<P, Error> {
        let cmd = ctx.cmd();
        let fab_idx = cmd.fab_idx;

        let options = request.transport_options()?;

        // Spec: URL must be non-empty.
        let url = options.url()?;
        if url.is_empty() {
            return Err(ErrorCode::ConstraintError.into());
        }

        // Capacity.
        let full = self
            .state
            .lock(|cell| cell.borrow().connections.len() >= NC);
        if full {
            return Err(ErrorCode::ResourceExhausted.into());
        }

        // Capture raw options before crossing the await boundary so
        // we don't re-borrow the request after the hook returns.
        let stored_options = self.capture_options(&options)?;

        let connection_id = self.alloc_id();
        self.hooks
            .on_allocate(connection_id, fab_idx, &request)
            .await?;

        let pushed = self.state.lock(|cell| {
            let mut state = cell.borrow_mut();
            state
                .connections
                .push(PushConnection {
                    connection_id,
                    fabric_index: fab_idx,
                    status: TransportStatusEnum::Inactive,
                    transport_options: stored_options,
                })
                .is_ok()
        });
        if !pushed {
            let _ = self.hooks.on_deallocate(connection_id).await;
            return Err(ErrorCode::ResourceExhausted.into());
        }
        ctx.notify_own_attr_changed(AttributeId::CurrentConnections as _);

        // Build the response: echo the just-stored connection.
        let snapshot = self
            .state
            .lock(|cell| cell.borrow().connections.last().cloned())
            .ok_or(Error::from(ErrorCode::Failure))?;
        let cfg = response.transport_configuration()?;
        write_connection(cfg, &snapshot)?.end()
    }

    async fn handle_deallocate_push_transport(
        &self,
        ctx: impl InvokeContext,
        request: DeallocatePushTransportRequest<'_>,
    ) -> Result<(), Error> {
        let cmd = ctx.cmd();
        let fab_idx = cmd.fab_idx;
        let connection_id = request.connection_id()?;

        let exists = self.state.lock(|cell| {
            cell.borrow()
                .connections
                .iter()
                .any(|c| c.connection_id == connection_id && c.fabric_index == fab_idx)
        });
        if !exists {
            return Err(ErrorCode::NotFound.into());
        }

        self.hooks.on_deallocate(connection_id).await?;

        self.state.lock(|cell| {
            let mut state = cell.borrow_mut();
            state
                .connections
                .retain(|c| !(c.connection_id == connection_id && c.fabric_index == fab_idx));
        });
        ctx.notify_own_attr_changed(AttributeId::CurrentConnections as _);
        Ok(())
    }

    async fn handle_modify_push_transport(
        &self,
        ctx: impl InvokeContext,
        request: ModifyPushTransportRequest<'_>,
    ) -> Result<(), Error> {
        let cmd = ctx.cmd();
        let fab_idx = cmd.fab_idx;
        let connection_id = request.connection_id()?;
        let options = request.transport_options()?;

        let url = options.url()?;
        if url.is_empty() {
            return Err(ErrorCode::ConstraintError.into());
        }

        let exists = self.state.lock(|cell| {
            cell.borrow()
                .connections
                .iter()
                .any(|c| c.connection_id == connection_id && c.fabric_index == fab_idx)
        });
        if !exists {
            return Err(ErrorCode::NotFound.into());
        }

        let stored_options = self.capture_options(&options)?;

        self.hooks.on_modify(connection_id, &request).await?;

        self.state.lock(|cell| {
            let mut state = cell.borrow_mut();
            if let Some(row) = state
                .connections
                .iter_mut()
                .find(|c| c.connection_id == connection_id && c.fabric_index == fab_idx)
            {
                row.transport_options = stored_options;
            }
        });
        ctx.notify_own_attr_changed(AttributeId::CurrentConnections as _);
        Ok(())
    }

    async fn handle_set_transport_status(
        &self,
        ctx: impl InvokeContext,
        request: SetTransportStatusRequest<'_>,
    ) -> Result<(), Error> {
        let cmd = ctx.cmd();
        let fab_idx = cmd.fab_idx;
        let connection_id = request.connection_id()?.into_option();
        let status = request.transport_status()?;

        // If a specific connection is targeted, validate existence + ownership.
        if let Some(id) = connection_id {
            let owned = self.state.lock(|cell| {
                cell.borrow()
                    .connections
                    .iter()
                    .any(|c| c.connection_id == id && c.fabric_index == fab_idx)
            });
            if !owned {
                return Err(ErrorCode::NotFound.into());
            }
        }

        self.hooks.on_set_status(connection_id, status).await?;

        self.state.lock(|cell| {
            let mut state = cell.borrow_mut();
            for c in state.connections.iter_mut() {
                if c.fabric_index != fab_idx {
                    continue;
                }
                match connection_id {
                    Some(id) if c.connection_id != id => continue,
                    _ => {}
                }
                c.status = status;
            }
        });
        ctx.notify_own_attr_changed(AttributeId::CurrentConnections as _);
        Ok(())
    }

    async fn handle_manually_trigger_transport(
        &self,
        ctx: impl InvokeContext,
        request: ManuallyTriggerTransportRequest<'_>,
    ) -> Result<(), Error> {
        let cmd = ctx.cmd();
        let fab_idx = cmd.fab_idx;
        let connection_id = request.connection_id()?;
        let reason = request.activation_reason()?;
        let time_control = request.time_control()?;
        let user_defined = request.user_defined()?;

        let owned = self.state.lock(|cell| {
            cell.borrow()
                .connections
                .iter()
                .any(|c| c.connection_id == connection_id && c.fabric_index == fab_idx)
        });
        if !owned {
            return Err(ErrorCode::NotFound.into());
        }

        self.hooks
            .on_manually_trigger(
                connection_id,
                reason,
                time_control,
                user_defined.map(|s| s.0),
            )
            .await?;
        Ok(())
    }

    async fn handle_find_transport<P: TLVBuilderParent>(
        &self,
        ctx: impl InvokeContext,
        request: FindTransportRequest<'_>,
        response: FindTransportResponseBuilder<P>,
    ) -> Result<P, Error> {
        let cmd = ctx.cmd();
        let fab_idx = cmd.fab_idx;
        let connection_id = request.connection_id()?.into_option();

        // Snapshot matching rows.
        let mut snapshot: Vec<PushConnection, NC> = Vec::new();
        self.state.lock(|cell| {
            for c in cell.borrow().connections.iter() {
                if c.fabric_index != fab_idx {
                    continue;
                }
                if let Some(id) = connection_id {
                    if c.connection_id != id {
                        continue;
                    }
                }
                let _ = snapshot.push(c.clone());
            }
        });

        // Spec: NOT_FOUND if no matching transport.
        if snapshot.is_empty() {
            return Err(ErrorCode::NotFound.into());
        }

        let mut arr = response.transport_configurations()?;
        for c in snapshot.iter() {
            arr = write_connection(arr.push()?, c)?;
        }
        arr.end()?.end()
    }
}

fn write_supported_format<P: TLVBuilderParent>(
    builder: SupportedFormatStructBuilder<P>,
    f: &SupportedFormat,
) -> Result<P, Error> {
    builder
        .container_format(f.container_format)?
        .ingest_method(f.ingest_method)?
        .end()
}

/// Replay a stored [`PushConnection`] into a
/// `TransportConfigurationStructBuilder<P>`.
///
/// Slot 0 (`connection_id`) and slot 1 (`transport_status`) are
/// written via the typed builder; slot 2 (`transport_options`,
/// optional) is sidestepped via `OptionalBuilder::none()` and
/// the stored raw TLV bytes are spliced in directly under
/// `Context(2)` — this lets us round-trip arbitrarily complex
/// `TransportOptionsStruct` values without reconstructing every
/// nested struct field-by-field.
fn write_connection<P: TLVBuilderParent>(
    builder: TransportConfigurationStructBuilder<P>,
    c: &PushConnection,
) -> Result<P, Error> {
    let b = builder
        .connection_id(c.connection_id)?
        .transport_status(c.status)?;
    // Skip the typed builder for slot 2 and write the raw stored TLV
    // directly under Context(2). The const-generic state advances to
    // 254 either way, preserving slot ordering for `fabric_index`.
    let mut b = b.transport_options()?.none();
    if !c.transport_options.is_empty() {
        let element = TLVElement::new(c.transport_options.as_slice());
        element.to_tlv(&TLVTag::Context(2), b.writer())?;
    }
    b.fabric_index(Some(c.fabric_index))?.end()
}