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
// Copyright 2024 Saorsa Labs Ltd.
//
// This Saorsa Network Software is licensed under the General Public License (GPL), version 3.
// Please see the file LICENSE-GPL, or visit <http://www.gnu.org/licenses/> for the full text.
//
// Full details available at https://saorsalabs.com/licenses
//! Constrained Protocol Engine for Low-Bandwidth Transports
//!
//! This module provides a lightweight protocol engine optimized for constrained
//! transports like BLE and LoRa that cannot run full QUIC. Unlike QUIC's 20+ byte
//! headers, the constrained protocol uses minimal 4-5 byte headers.
//!
//! # Architecture
//!
//! The constrained engine is organized into layers:
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────┐
//! │ Application Layer │
//! ├─────────────────────────────────────────────────────────────┤
//! │ ConstrainedTransport / ConstrainedHandle (transport.rs) │
//! │ - Thread-safe wrapper with handle pattern │
//! │ - Async channel-based packet I/O │
//! ├─────────────────────────────────────────────────────────────┤
//! │ ConstrainedEngineAdapter (adapter.rs) │
//! │ - TransportAddr ↔ SocketAddr mapping │
//! │ - Synthetic addresses for BLE/LoRa │
//! ├─────────────────────────────────────────────────────────────┤
//! │ ConstrainedEngine (engine.rs) │
//! │ - Multi-connection management │
//! │ - Packet routing and event generation │
//! ├─────────────────────────────────────────────────────────────┤
//! │ ConstrainedConnection (connection.rs) │
//! │ - Per-connection state and buffers │
//! │ - Send/receive with reliability │
//! ├─────────────────────────────────────────────────────────────┤
//! │ ARQ Layer (arq.rs) │
//! │ - SendWindow / ReceiveWindow │
//! │ - Retransmission and timeout handling │
//! ├─────────────────────────────────────────────────────────────┤
//! │ StateMachine (state.rs) │
//! │ - Connection lifecycle states │
//! │ - Valid transition enforcement │
//! ├─────────────────────────────────────────────────────────────┤
//! │ Header/Types (header.rs, types.rs) │
//! │ - 5-byte packet header format │
//! │ - Core type definitions │
//! └─────────────────────────────────────────────────────────────┘
//! ```
//!
//! # Design Goals
//!
//! - **Minimal overhead**: 4-5 byte headers vs QUIC's 20+ bytes
//! - **Simple reliability**: ARQ (Automatic Repeat Request) with cumulative ACKs
//! - **No congestion control**: Link layer handles congestion
//! - **Session resumption**: Integrates with BLE session cache
//! - **Low memory footprint**: Small window sizes (8-16 packets)
//! - **Transport agnostic**: Works with any `TransportAddr` type
//!
//! # Header Format
//!
//! ```text
//! 0 1 2 3 4
//! +-------+-------+-------+-------+-------+
//! | CID (16b) | SEQ | ACK | FLAGS |
//! +-------+-------+-------+-------+-------+
//! ```
//!
//! - **CID**: Connection ID (2 bytes) - identifies the connection
//! - **SEQ**: Sequence number (1 byte) - 0-255, wrapping
//! - **ACK**: Acknowledgment number (1 byte) - cumulative ACK
//! - **FLAGS**: Packet flags (1 byte) - SYN, ACK, FIN, RST, DATA, PING, PONG
//!
//! # Protocol Engine Selection
//!
//! Use [`ConstrainedTransport::should_use_constrained`](crate::constrained::ConstrainedTransport::should_use_constrained) to determine whether
//! to use the constrained engine based on transport capabilities:
//!
//! | Capability | QUIC | Constrained |
//! |------------|------|-------------|
//! | Bandwidth | >= 10 kbps | < 10 kbps |
//! | MTU | >= 1200 bytes | < 1200 bytes |
//! | RTT | < 2 seconds | Any |
//!
//! # State Machine
//!
//! ```text
//! SYN_SENT
//! ↓
//! CLOSED → SYN_RCVD → ESTABLISHED → FIN_WAIT → CLOSING → TIME_WAIT → CLOSED
//! ↑ ↓
//! └─────── RST ─────────┘
//! ```
//!
//! # Example: Using with TransportAddr
//!
//! ```rust,ignore
//! use ant_quic::constrained::{ConstrainedTransport, ConstrainedHandle};
//! use ant_quic::transport::TransportAddr;
//!
//! // Create transport for BLE
//! let transport = ConstrainedTransport::for_ble();
//! let handle = transport.handle();
//!
//! // Connect to a BLE device
//! let ble_addr = TransportAddr::Ble {
//! device_id: [0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF],
//! service_uuid: None,
//! };
//! let conn_id = handle.connect(&ble_addr)?;
//!
//! // Send data
//! handle.send(conn_id, b"Hello, BLE!")?;
//!
//! // Process incoming packets and check for events
//! handle.process_incoming(&ble_addr, &received_data)?;
//! while let Some(event) = handle.next_event() {
//! match event {
//! AdapterEvent::DataReceived { connection_id, data } => {
//! println!("Received: {:?}", data);
//! }
//! _ => {}
//! }
//! }
//! ```
//!
//! # Module Organization
//!
//! - `types` - Core types: ConnectionId, SequenceNumber, PacketFlags, ConstrainedError, ConstrainedAddr
//! - `header` - Packet header format and serialization
//! - `state` - Connection state machine
//! - `arq` - ARQ reliability layer (SendWindow, ReceiveWindow)
//! - `connection` - Connection management
//! - `engine` - Main protocol engine
//! - `adapter` - TransportAddr integration layer
//! - `transport` - Thread-safe transport wrapper
// Sub-modules
// Re-exports
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;