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
//! Bulk Data Exchange (Core §11.22): moving a file between two nodes.
//!
//! Matter's messages are small — §4.4.4 caps a datagram at 1280 octets — and a firmware image
//! is not. BDX is how anything larger moves: a negotiation, then a run of numbered blocks, then
//! an acknowledgement that ends the session. It is what an OTA image travels over, what
//! `RetrieveLogsRequest` (§11.12) hands a diagnostic log to, and the reason
//! [`ImageUri`](crate::clusters::ota_provider::ImageUri) spells `bdx://`.
//!
//! The protocol "has some semantic elements influenced by the Trivial File Transfer Protocol
//! (TFTP) RFC 1350", with one large difference: BDX does not retransmit. §11.22.4 requires a
//! reliable transport underneath — MRP, BTP or TCP — so `BlockAck` and `BlockQuery` are
//! *flow control*, not reliability. That is what lets a battery-powered node pace a download
//! it could not otherwise stay awake for.
//!
//! # Four roles, two of which are the same question
//!
//! §11.22.2 names the ends twice over. **Sender** and **Receiver** say which way the data
//! goes; **Initiator** and **Responder** say who spoke first. A download — an OTA Requestor
//! fetching an image — is an Initiator that is the Receiver, so it opens with `ReceiveInit`;
//! an upload opens with `SendInit`. Either way, exactly one end is the **Driver**: it paces
//! the transfer, and the other follows. [`Sender`] and [`Receiver`] are this module's two
//! halves, and [`Parameters::control`] is what says which of them drives.
//!
//! # Errors are status codes, because that is what goes on the wire
//!
//! Everything that inspects a peer's message returns [`Rejected`], whose error is the
//! [`StatusCode`] of §11.22.3.2 — `BAD_BLOCK_COUNTER` for a block out of order,
//! `LENGTH_MISMATCH` for a transfer that ended short. [`report`] turns one into the
//! `StatusReport` to send, and §11.22.3.2 is clear about what happens next: "the receiving peer
//! SHALL terminate its processing of the transfer and invalidate the exchange." Encoding, which
//! can only fail locally, keeps the crate's ordinary [`Error`].
//!
//! # A download, end to end
//!
//! ```
//! use matter_kit::bdx::{
//! Direction, Init, Limits, MessageType, Parameters, Receiver, Sender, negotiate,
//! };
//!
//! // The Requestor proposes: whole file, 256-octet blocks, either drive mode.
//! let mut proposal = Init::new(b"firmware.ota", 256);
//! proposal.definite_length = Some(600);
//!
//! // The Provider answers with what it will actually do.
//! let limits = Limits { available: Some(600), ..Limits::default() };
//! let agreed = negotiate(Direction::Download, &proposal, &limits)?;
//! assert!(agreed.sender_drives()); // §11.22.5.4.1 prefers it
//!
//! let accept = agreed.receive_accept(&[]);
//! let mut provider = Sender::new(agreed);
//! let mut requestor = Receiver::new(Parameters::from_receive_accept(&proposal, &accept)?);
//!
//! // 600 octets in blocks of 256: 256, 256, then an 88-octet BlockEOF.
//! for (len, eof) in [(256, false), (256, false), (88, true)] {
//! let counter = provider.block(len, eof)?;
//! requestor.on_block(counter, len, eof)?;
//! let (opcode, counter) = requestor.ack()?;
//! provider.on_ack(counter, opcode == MessageType::BlockAckEof)?;
//! }
//! assert!(provider.is_complete() && requestor.is_complete());
//! assert_eq!(requestor.received(), 600);
//! # Ok::<(), matter_kit::bdx::StatusCode>(())
//! ```
pub use ;
pub use ;
use crate;
use crateProtocolId;
use crate;
/// The result of anything that judges a peer's message: the failure is the status code the
/// peer must be told.
///
/// Not [`crate::Result`], and deliberately: §11.22.3.2's codes are the entire content of a BDX
/// failure, and folding them into a general error would throw away the only thing the caller
/// needs in order to answer.
pub type Rejected<T> = Result;
/// §11.22.3.2's status codes, as they appear in a `StatusReport`'s ProtocolCode field.
///
/// Exhaustive: the specification closes the set.
/// Writes the `StatusReport` that ends a transfer (§11.22.3.2).
///
/// "StatusReport(GeneralCode: FAILURE, ProtocolId: {VendorID=0x0000, ProtocolId=BDX},
/// ProtocolCode: `<value>`)" — sent on the same exchange, which is then invalidated.
/// Reads a `StatusReport` that arrived on a BDX exchange.
///
/// Returns the reason the transfer ended, or [`None`] if the report is not a BDX failure — a
/// `SUCCESS` report, or one belonging to another protocol, neither of which ends the transfer.