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
751
752
753
754
755
756
757
/*
 *
 *    Copyright (c) 2024-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.
 */

//! An implementation of the Matter BTP protocol over BLE, using GATT as the underlying transport.

use core::future::Future;

use embassy_futures::select::select;
use embassy_time::{Instant, Timer};

use session::{BTP_ACK_TIMEOUT_SECS, BTP_CONN_IDLE_TIMEOUT_SECS};

use crate::error::{Error, ErrorCode};
use crate::transport::network::btp::session::Session;
use crate::transport::network::{Address, BtAddr, NetworkReceive, NetworkSend, MAX_TX_PACKET_SIZE};
use crate::utils::cell::RefCell;
use crate::utils::init::{init, Init};
use crate::utils::storage::Vec;
use crate::utils::sync::blocking::Mutex;
use crate::utils::sync::Notification;

pub use gatt::*;

mod gatt;
mod session;

/// The maximum size of a BTP segment.
pub(crate) const MAX_BTP_SEGMENT_SIZE: usize = 244;
/// The size of the GATT header. `MAX_BTP_SEGMENT_SIZE` + `GATT_HEADER_SIZE` is 247 bytes, which is the maximum ATT MTU size supported by the BTP protocol.
pub(crate) const GATT_HEADER_SIZE: usize = 3;

/// The minimum MTU that can be used as per specification.
pub(crate) const MIN_MTU: u16 = (20 + GATT_HEADER_SIZE) as u16;
/// The maximum MTU that can be used as per specification.
pub(crate) const MAX_MTU: u16 = (MAX_BTP_SEGMENT_SIZE + GATT_HEADER_SIZE) as u16;

/// An implementation of the Matter BTP protocol.
/// This is a low-level protocol that is used to send and receive Matter messages over BLE.
///
/// For the BTP protocol to function, it is expected that a GATT Peripheral (or a GATT Central, when rs-matter plays the Controller role)
/// is setup in some OS-specific way by the user, where the GATT peripheral app is configured to contain the C1 and C2 characteristics
/// on a service with the the Matter Service UUID.
///
/// The OS-specific GATT Peripheral or Central is supposed to call the following two methods:
///
/// - For the case where we take the GATT Peripheral role (i.e. rs-matter is the Accessory/Device, and BTP is initialized with `Btp::set_initiator(false)`):
///   - `Btp::process_incoming` - when a GATT Write request is received on the C1 characteristic
///   - `Btp::process_outgoing` - periodically and when `Btp::wait_outgoing` is notified, to check if there is any data to send to the peer
///     The data is to be send via a GATT indication on the C2 characteristic.
///
/// - For the case where we take the GATT Central role (i.e. rs-matter is the Controller, and BTP is initialized with `Btp::set_initiator(true)`):
///   - `Btp::process_incoming` - when a GATT indication is received on the C2 characteristic
///   - `Btp::process_outgoing` - periodically and when `Btp::wait_outgoing` is notified, to check if there is any data to send to the peer
///     The data is to be send via a GATT Write request on the C1 characteristic.
pub struct Btp {
    /// The inner state of the BTP protocol, containing the session state, the outgoing SDU buffer, and the timeouts configuration.
    inner: Mutex<RefCell<BtpInner>>,
    /// Notification triggered when (potentially!) a new Matter packet (BTP SDU) is assembled and available for processing.
    recv_notif: Notification,
    /// Notification triggered when (potentially!) there is now space for buffering a new outgoing Matter packet (BTP SDU).
    send_notif: Notification,
    /// Notification triggered when (potentially!) there is new outgoing data to be sent to the peer, which can be either a handshake packet or a BTP SDU segment.
    outg_notif: Notification,
}

impl Btp {
    /// Construct a new BTP instance.
    #[inline(always)]
    pub const fn new() -> Self {
        Self {
            inner: Mutex::new(RefCell::new(BtpInner::new())),
            recv_notif: Notification::new(),
            send_notif: Notification::new(),
            outg_notif: Notification::new(),
        }
    }

    /// Create an in-place initializer for a BTP instance.
    pub fn init() -> impl Init<Self> {
        init!(Self {
            inner <- Mutex::init(RefCell::init(BtpInner::init())),
            recv_notif: Notification::new(),
            send_notif: Notification::new(),
            outg_notif: Notification::new(),
        })
    }

    /// Reset the BTP state, by clearing all sessions and buffers, and resetting the timeouts to their default values.
    pub fn reset(&self) {
        self.inner.lock(|inner| {
            inner.borrow_mut().reset();

            self.recv_notif.notify();
            self.send_notif.notify();
            self.outg_notif.notify();
        });
    }

    /// Set the relaxed MTU negotiation mode, which changes the behavior of the MTU negotiation in the handshake phase
    /// when there is a mismatch between the GATT MTU and the peer-reported MTU.
    pub fn set_relaxed_mtu_nego(&self, relaxed_mtu_nego: bool) {
        self.inner
            .lock(|inner| inner.borrow_mut().set_relaxed_mtu_nego(relaxed_mtu_nego));
    }

    /// Set the BTP timeouts, by configuring the ACK timeout and the connection idle timeout.
    ///
    /// Only used by the unit tests.
    #[allow(dead_code)]
    pub(crate) fn set_timeouts(&self, ack_timeout_secs: u8, conn_idle_timeout_secs: u8) {
        self.inner.lock(|inner| {
            inner
                .borrow_mut()
                .set_timeouts(ack_timeout_secs, conn_idle_timeout_secs);

            self.recv_notif.notify();
            self.send_notif.notify();
            self.outg_notif.notify();
        });
    }

    /// Check if the session has timed out due to inactivity (connection timeout).
    pub fn timeout(&self) -> bool {
        self.inner.lock(|inner| inner.borrow().timeout())
    }

    /// Wait until the session has timed out due to inactivity (connection timeout).
    pub async fn wait_timeout(&self) {
        // Check every 2 seconds
        // The connection timeout should be at least 1 second, but in production
        // use cases is anyway hard-coded to `BTP_CONN_IDLE_TIMEOUT_SECS`
        //
        // And even for testing, timeouting an extra second late is not a prob
        const TIMEOUT_CHECK_SECS: u64 = 2;

        while !self.timeout() {
            Timer::after_secs(TIMEOUT_CHECK_SECS).await;
        }
    }

    /// Process an incoming BLE packet
    ///
    /// This method is expected to be called by the OS-specific GATT Peripheral or Central when a packet is received from the peer.
    ///
    /// # Arguments
    /// - `gatt_mtu`: the GATT MTU (if known) to be used for processing the incoming packet.
    ///   This is needed in order to properly process the BTP handshake packets, which contain the peer's supported MTU.
    ///   If `None` is provided, the processing will assume that the GATT MTU is unknown, and will use the minimum MTU for processing the handshake packets.
    /// - `addr`: the address of the peer from where the packet originates
    /// - `data`: the incoming packet data, which is expected to be the payload of a GATT Write request (when we are the Peripheral) or a GATT indication (when we are the Central)
    pub fn process_incoming(
        &self,
        gatt_mtu: Option<u16>,
        addr: BtAddr,
        data: &[u8],
    ) -> Result<(), Error> {
        self.inner.lock(|inner| {
            inner.borrow_mut().process_incoming(gatt_mtu, addr, data)?;

            self.recv_notif.notify();
            self.outg_notif.notify();

            Ok(())
        })
    }

    /// Process outgoing data and prepare it to be sent to the peer.
    ///
    /// This method is expected to be called by the OS-specific GATT Peripheral or Central periodically and when `Btp::wait_outgoing` is notified,
    /// in order to check if there is any data to send to the peer.
    ///
    /// The data to be sent is expected to be sent by the OS-specific GATT Peripheral or Central via a GATT indication (when we are the Peripheral)
    /// or a GATT Write request (when we are the Central).
    ///
    /// # Arguments
    /// - `gatt_mtu`: the GATT MTU (if known) to be used for processing the outgoing packet.
    ///   This is needed in order to properly process the BTP handshake packets, which require to know the MTU in order to properly segment the outgoing data.
    ///   If `None` is provided, the processing will assume that the GATT MTU is unknown, and will use the minimum MTU for processing the handshake packets.
    /// - `buf`: the buffer to be used for preparing the outgoing packet data, which is expected to be the payload of a GATT indication (when we are the Peripheral)
    ///   or a GATT Write request (when we are the Central). A size of 512 (max MTU) should be enough.
    ///
    /// # Returns
    /// - `Ok(len)` if there is data to be sent to the peer, where `len` is the size of the prepared packet data to be sent.
    ///   The prepared packet data will be written to the provided `buf` buffer.
    /// - `Ok(0)` if there is no data to be sent to the peer at the moment.
    /// - `Err` if there was an error during processing the outgoing data.
    pub fn process_outgoing(&self, gatt_mtu: Option<u16>, buf: &mut [u8]) -> Result<usize, Error> {
        self.inner.lock(|inner| {
            let mut inner = inner.borrow_mut();

            let len = inner.process_outgoing(gatt_mtu, buf)?;

            if inner.outgoing_sdu.buf.is_empty() {
                self.send_notif.notify();
            }

            Ok(len)
        })
    }

    /// Wait until there is at least one packet to be sent to the peer,
    /// by waiting for a notification that is triggered when there is new outgoing data to be sent.
    pub async fn wait_outgoing(&self) {
        // Check every second
        // The ack timeout should be at least 1 second, but in production
        // use cases is anyway hard-coded to `BTP_ACK_TIMEOUT_SECS`
        const ACK_TIMEOUT_CHECK_SECS: u64 = 1;

        select(
            self.outg_notif.wait(),
            Timer::after_secs(ACK_TIMEOUT_CHECK_SECS),
        )
        .await;
    }

    /// Wait until there is at least one Matter (a.k.a. BTP SDU) packet available for consumption.
    pub async fn wait_available(&self) -> Result<(), Error> {
        loop {
            let available = self.inner.lock(|inner| inner.borrow().available());

            if available {
                break;
            }

            self.recv_notif.wait().await;
        }

        Ok(())
    }

    /// Receive a Matter (a.k.a. BTP SDU) packet.
    ///
    /// If there is no packet available, this method will block asynchronously until a packet is available.
    /// Returns the size of the received packet, as well as the address of the BLE peer from where the packet originates.
    pub async fn recv(&self, buf: &mut [u8]) -> Result<(usize, BtAddr), Error> {
        loop {
            let result = self.inner.lock(|inner| {
                let result = inner.borrow_mut().recv(buf)?;

                if result.is_some() {
                    self.outg_notif.notify();
                }

                Ok::<_, Error>(result)
            })?;

            if let Some(result) = result {
                break Ok(result);
            }

            self.recv_notif.wait().await;
        }
    }

    /// Send a Matter (a.k.a. BTP SDU) packet to the specified BLE peer.
    pub async fn send(&self, data: &[u8], addr: BtAddr) -> Result<(), Error> {
        loop {
            let sent = self.inner.lock(|inner| {
                let sent = inner.borrow_mut().send(data, addr)?;

                if sent {
                    self.outg_notif.notify();
                }

                Ok::<_, Error>(sent)
            })?;

            if sent {
                break Ok(());
            }

            self.send_notif.wait().await;
        }
    }
}

impl Default for Btp {
    fn default() -> Self {
        Self::new()
    }
}

impl NetworkSend for &Btp {
    async fn send_to(&mut self, data: &[u8], addr: Address) -> Result<(), Error> {
        (*self)
            .send(data, addr.btp().ok_or(ErrorCode::NoNetworkInterface)?)
            .await
    }
}

impl NetworkReceive for &Btp {
    fn wait_available(&mut self) -> impl Future<Output = Result<(), Error>> {
        (*self).wait_available()
    }

    async fn recv_from(&mut self, buffer: &mut [u8]) -> Result<(usize, Address), Error> {
        (*self)
            .recv(buffer)
            .await
            .map(|(len, addr)| (len, Address::Btp(addr)))
    }
}

impl NetworkSend for Btp {
    async fn send_to(&mut self, data: &[u8], addr: Address) -> Result<(), Error> {
        (&*self).send_to(data, addr).await
    }
}

impl NetworkReceive for Btp {
    fn wait_available(&mut self) -> impl Future<Output = Result<(), Error>> {
        (*self).wait_available()
    }

    async fn recv_from(&mut self, buffer: &mut [u8]) -> Result<(usize, Address), Error> {
        (&*self).recv_from(buffer).await
    }
}

/// The inner state of the BTP protocol, containing the session state, the outgoing SDU buffer, and the timeouts configuration.
struct BtpInner {
    session: Session,
    outgoing_sdu: OutgoingSdu,
    ack_timeout_secs: u8,
    conn_idle_timeout_secs: u8,
}

impl BtpInner {
    /// Construct a new BtpInner instance with default values.
    const fn new() -> Self {
        Self {
            session: Session::new(),
            outgoing_sdu: OutgoingSdu::new(),
            ack_timeout_secs: BTP_ACK_TIMEOUT_SECS,
            conn_idle_timeout_secs: BTP_CONN_IDLE_TIMEOUT_SECS,
        }
    }

    /// Create an in-place initializer for a BtpInner instance.
    fn init() -> impl Init<Self> {
        init!(Self {
            session <- Session::init(),
            outgoing_sdu <- OutgoingSdu::init(),
            ack_timeout_secs: BTP_ACK_TIMEOUT_SECS,
            conn_idle_timeout_secs: BTP_CONN_IDLE_TIMEOUT_SECS,
        })
    }

    /// Reset the BtpInner state, by resetting the session, clearing the outgoing SDU buffer, and resetting the timeouts to their default values.
    fn reset(&mut self) {
        self.session.reset();
        self.outgoing_sdu.reset();
        self.ack_timeout_secs = BTP_ACK_TIMEOUT_SECS;
        self.conn_idle_timeout_secs = BTP_CONN_IDLE_TIMEOUT_SECS;
    }

    /// Set the relaxed MTU negotiation mode, which changes the behavior of the MTU negotiation in the handshake phase
    /// when there is a mismatch between the GATT MTU and the peer-reported MTU.
    fn set_relaxed_mtu_nego(&mut self, relaxed_mtu_nego: bool) {
        self.session.set_relaxed_mtu_nego(relaxed_mtu_nego);
    }

    /// Set the BTP timeouts, by configuring the ACK timeout and the connection idle timeout.
    /// Only used by the unit tests.
    fn set_timeouts(&mut self, ack_timeout_secs: u8, conn_idle_timeout_secs: u8) {
        self.ack_timeout_secs = ack_timeout_secs;
        self.conn_idle_timeout_secs = conn_idle_timeout_secs;
    }

    /// Process an incoming BLE packet
    fn process_incoming(
        &mut self,
        gatt_mtu: Option<u16>,
        addr: BtAddr,
        data: &[u8],
    ) -> Result<(), Error> {
        self.session.process_rx(gatt_mtu, addr, data)
    }

    /// Process outgoing data and prepare it to be sent to the peer.
    fn process_outgoing(&mut self, gatt_mtu: Option<u16>, buf: &mut [u8]) -> Result<usize, Error> {
        let len = self.session.prep_tx_handshake(gatt_mtu, buf)?;
        if len > 0 {
            return Ok(len);
        }

        if !self.outgoing_sdu.buf.is_empty() {
            if self.outgoing_sdu.address == self.session.address() {
                let len = self.session.prep_tx_data(
                    &self.outgoing_sdu.buf,
                    &mut self.outgoing_sdu.buf_offset,
                    buf,
                )?;
                if len > 0 {
                    if self.outgoing_sdu.buf_offset == self.outgoing_sdu.buf.len() {
                        self.outgoing_sdu.reset();
                    }

                    return Ok(len);
                }
            } else {
                self.outgoing_sdu.reset();
            }
        }

        if self
            .session
            .is_ack_due(Instant::now(), self.ack_timeout_secs as _)
        {
            let len = self.session.prep_tx_data(&[], &mut 0, buf)?;
            assert!(len > 0);

            return Ok(len);
        }

        Ok(0)
    }

    /// Check if there is at least one Matter (a.k.a. BTP SDU) packet available for consumption.
    fn available(&self) -> bool {
        self.session.message_available()
    }

    /// Receive a Matter (a.k.a. BTP SDU) packet.
    ///
    /// Returns the size of the received packet, as well as the address of the BLE peer from where the packet originates,
    /// or 0 if there is no packet available for reception.
    fn recv(&mut self, buf: &mut [u8]) -> Result<Option<(usize, BtAddr)>, Error> {
        if self.session.message_available() {
            let len = self.session.fetch_message(buf)?;

            Ok(Some((len, self.session.address())))
        } else {
            Ok(None)
        }
    }

    /// Send a Matter (a.k.a. BTP SDU) packet to the specified BLE peer.
    ///
    /// Returns `Ok(true)` if the packet was successfully buffered for sending,
    /// `Ok(false)` if there is already an outgoing packet being buffered (i.e. the caller should retry later),
    /// or an error if there was an error during buffering the packet for sending.
    fn send(&mut self, data: &[u8], addr: BtAddr) -> Result<bool, Error> {
        if data.is_empty() || data.len() > MAX_TX_PACKET_SIZE {
            Err(ErrorCode::InvalidArgument.into())
        } else if self.outgoing_sdu.buf.is_empty() {
            self.outgoing_sdu.address = addr;
            self.outgoing_sdu.buf_offset = 0;
            unwrap!(self.outgoing_sdu.buf.extend_from_slice(data));

            Ok(true)
        } else {
            Ok(false)
        }
    }

    /// Check if the session has timed out due to inactivity (connection timeout).
    fn timeout(&self) -> bool {
        self.session
            .is_timed_out(Instant::now(), self.conn_idle_timeout_secs as _)
    }
}

/// The state of an outgoing BTP SDU, containing the peer address, the SDU data buffer, and the current offset in the buffer for sending.
struct OutgoingSdu {
    address: BtAddr,
    buf: Vec<u8, MAX_TX_PACKET_SIZE>,
    buf_offset: usize,
}

impl OutgoingSdu {
    const fn new() -> Self {
        Self {
            address: BtAddr([0; 6]),
            buf: Vec::new(),
            buf_offset: 0,
        }
    }

    fn init() -> impl Init<Self> {
        init!(Self {
            address: BtAddr([0; 6]),
            buf <- crate::utils::storage::Vec::init(),
            buf_offset: 0,
        })
    }

    fn reset(&mut self) {
        self.address = BtAddr([0; 6]);
        self.buf.clear();
        self.buf_offset = 0;
    }
}

#[cfg(test)]
mod test {
    use super::*;

    const PEER_ADDR: BtAddr = BtAddr([1, 2, 3, 4, 5, 6]);

    fn incoming(btp: &Btp, data: &[u8]) {
        incoming_mtu(btp, None, data)
    }

    fn expect_outgoing(btp: &Btp, data: &[u8]) {
        expect_outgoing_mtu(btp, None, data)
    }

    /// Generate `GattPeripheralEvent::Write` event for the peer
    fn incoming_mtu(btp: &Btp, gatt_mtu: Option<u16>, data: &[u8]) {
        btp.process_incoming(gatt_mtu, PEER_ADDR, data).unwrap();
    }

    /// Expect to receive the provided data from the peer as if the BTP protocol
    /// did call `indicate`
    fn expect_outgoing_mtu(btp: &Btp, gatt_mtu: Option<u16>, data: &[u8]) {
        let mut buf = [0; 512];

        let len = btp.process_outgoing(gatt_mtu, &mut buf).unwrap();

        assert_eq!(&buf[..len], data);
    }

    fn send(btp: &Btp, data: &[u8]) {
        embassy_futures::block_on(btp.send(data, PEER_ADDR)).unwrap();
    }

    fn expect_recv(btp: &Btp, data: &[u8]) {
        let mut buf = [0; 2048];

        let (len, addr) = embassy_futures::block_on(btp.recv(&mut buf)).unwrap();

        assert_eq!(addr, PEER_ADDR);
        assert_eq!(&buf[..len], data);
    }

    #[test]
    fn test_mtu_timeout() {
        #[cfg(all(feature = "std", not(target_os = "espidf")))]
        {
            let _ = env_logger::try_init_from_env(
                env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "info"),
            );
        }

        let btp = Btp::new();
        btp.set_timeouts(1, 2);

        incoming_mtu(
            &btp,
            Some(0xc8),
            &[0x65, 0x6c, 0x54, 0x00, 0x00, 0x00, 0xc8, 0x00, 0x05],
        );

        // Expected MTU in response is 0xc8 - 3 = 0xc5
        expect_outgoing(&btp, &[0x65, 0x6c, 0x05, 0xc5, 0x00, 0x05]);

        embassy_futures::block_on(Timer::after_secs(3));

        assert!(btp.timeout());

        /////////////////////////////////

        btp.reset();

        // GATT MTU is unknown
        incoming_mtu(
            &btp,
            None,
            &[0x65, 0x6c, 0x54, 0x00, 0x00, 0x00, 0xc8, 0x00, 0x05],
        );

        // Expected MTU is the minimum one (0x14)
        expect_outgoing(&btp, &[0x65, 0x6c, 0x05, 0x14, 0x00, 0x05]);
    }

    // Utility to do the negotiation phase with a minumum MTU
    fn nego_min_mtu() -> Btp {
        let btp = Btp::new();

        incoming(
            &btp,
            &[0x65, 0x6c, 0x54, 0x00, 0x00, 0x00, 0xc8, 0x00, 0x05],
        );

        // Peer window = 1 because of this handshake resp
        expect_outgoing(&btp, &[0x65, 0x6c, 0x05, 0x14, 0x00, 0x05]);

        btp
    }

    #[test]
    fn test_short_read() {
        let btp = nego_min_mtu();

        send(&btp, &[0, 1, 2, 3]);

        expect_outgoing(&btp, &[5, 1, 4, 0, 0, 1, 2, 3]);
    }

    #[test]
    fn test_short_write() {
        let btp = nego_min_mtu();

        incoming(&btp, &[5, 0, 3, 0, 1, 2, 3]);

        expect_recv(&btp, &[1, 2, 3]);
    }

    #[test]
    fn test_long_read() {
        let btp = nego_min_mtu();

        send(&btp, &[0; 52]);

        // Long msg beginning
        expect_outgoing(
            &btp,
            &[1, 1, 52, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        );

        // Long msg continue
        expect_outgoing(
            &btp,
            &[2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        );

        // Long msg end
        expect_outgoing(
            &btp,
            &[6, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        );
    }

    #[test]
    fn test_long_write() {
        let btp = nego_min_mtu();

        // Beginning
        incoming(
            &btp,
            &[
                1, 0, 30, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
            ],
        );

        // End
        incoming(
            &btp,
            &[4, 1, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30],
        );

        expect_recv(
            &btp,
            &[
                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,
            ],
        );
    }

    #[test]
    fn test_long_read_ack() {
        let btp = nego_min_mtu();

        // A short message, to pump up the ack window
        send(&btp, &[0, 1, 2, 3]);

        // Peer window = 2
        expect_outgoing(&btp, &[5, 1, 4, 0, 0, 1, 2, 3]);

        send(&btp, &[0; 100]);

        // Long msg beginning
        // Peer window = 3
        expect_outgoing(
            &btp,
            &[1, 2, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        );

        // Long msg continue
        // Peer window = 4
        expect_outgoing(
            &btp,
            &[2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        );

        // Send ACK from the peer as its window is full by now (5 - 1) = 4
        incoming(&btp, &[8, 3, 0]);

        // Long msg end + ACK
        // Peer window = 0, final packet
        expect_outgoing(
            &btp,
            &[10, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        );
    }

    #[test]
    fn test_long_write_ack() {
        let btp = nego_min_mtu();

        // Beginning
        incoming(
            &btp,
            &[1, 0, 44, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        );

        // Continue
        incoming(
            &btp,
            &[2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        );

        // End
        incoming(&btp, &[4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);

        expect_recv(&btp, &[0; 44]);
    }

    #[test]
    fn test_idle_ping_pong() {
        let btp = nego_min_mtu();

        btp.set_timeouts(1, 10);

        // The peripheral should send the first ACK for the handshake response
        incoming(&btp, &[8, 0, 0]);

        embassy_futures::block_on(Timer::after_secs(1));

        // BTP should - in X seconds - ACK our message so that the session does not timeout
        expect_outgoing(&btp, &[8, 0, 1]);

        // The peripheral should ACK it
        incoming(&btp, &[8, 1, 1]);

        embassy_futures::block_on(Timer::after_secs(1));

        // BTP should - in X seconds - ACK again
        expect_outgoing(&btp, &[8, 1, 2]);
    }
}