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
use super::{Error, Instant, Result, RingBuffer, Socket, SocketHandle, SocketMeta};
use embedded_nal::SocketAddr;
use fugit::{ExtU32, SecsDurationU32};
/// A TCP socket ring buffer.
pub type SocketBuffer<const N: usize> = RingBuffer<u8, N>;
#[derive(Debug, PartialEq, Eq)]
pub enum State<const TIMER_HZ: u32> {
/// Freshly created, unsullied
Created,
WaitingForConnect(SocketAddr),
/// TCP connected or UDP has an address
Connected(SocketAddr),
/// Block all writes (Socket is closed by remote)
ShutdownForWrite(Instant<TIMER_HZ>),
}
#[cfg(feature = "defmt")]
impl<const TIMER_HZ: u32> defmt::Format for State<TIMER_HZ> {
fn format(&self, fmt: defmt::Formatter) {
match self {
State::Created => defmt::write!(fmt, "State::Created"),
State::WaitingForConnect(_) => defmt::write!(fmt, "State::WaitingForConnect"),
State::Connected(_) => defmt::write!(fmt, "State::Connected"),
State::ShutdownForWrite(_) => defmt::write!(fmt, "State::ShutdownForWrite"),
}
}
}
impl<const TIMER_HZ: u32> Default for State<TIMER_HZ> {
fn default() -> Self {
State::Created
}
}
/// A Transmission Control Protocol socket.
///
/// A TCP socket may passively listen for connections or actively connect to another endpoint.
/// Note that, for listening sockets, there is no "backlog"; to be able to simultaneously
/// accept several connections, as many sockets must be allocated, or any new connection
/// attempts will be reset.
#[derive(Debug)]
pub struct TcpSocket<const TIMER_HZ: u32, const L: usize> {
pub(crate) meta: SocketMeta,
state: State<TIMER_HZ>,
check_interval: SecsDurationU32,
read_timeout: Option<SecsDurationU32>,
available_data: usize,
rx_buffer: SocketBuffer<L>,
last_check_time: Option<Instant<TIMER_HZ>>,
}
impl<const TIMER_HZ: u32, const L: usize> TcpSocket<TIMER_HZ, L> {
/// Create a socket using the given buffers.
pub fn new(socket_id: u8) -> TcpSocket<TIMER_HZ, L> {
TcpSocket {
meta: SocketMeta {
handle: SocketHandle(socket_id),
},
state: State::default(),
rx_buffer: SocketBuffer::new(),
available_data: 0,
check_interval: 15.secs(),
read_timeout: Some(15.secs()),
last_check_time: None,
}
}
/// Return the socket handle.
pub fn handle(&self) -> SocketHandle {
self.meta.handle
}
pub fn update_handle(&mut self, handle: SocketHandle) {
debug!(
"[TCP Socket] [{:?}] Updating handle {:?}",
self.handle(),
handle
);
self.meta.update(handle)
}
/// Return the bound endpoint.
pub fn endpoint(&self) -> Option<SocketAddr> {
match self.state {
State::Connected(s) | State::WaitingForConnect(s) => Some(s),
_ => None,
}
}
/// Return the connection state, in terms of the TCP state machine.
pub fn state(&self) -> &State<TIMER_HZ> {
&self.state
}
pub fn reset(&mut self) {
self.set_state(State::default());
self.rx_buffer.clear();
self.set_available_data(0);
self.last_check_time = None;
}
pub fn should_update_available_data(&mut self, ts: Instant<TIMER_HZ>) -> bool {
// Cannot request available data on a socket that is closed by the
// module
if !self.is_connected() {
return false;
}
let should_update = self
.last_check_time
.and_then(|last_check_time| ts.checked_duration_since(last_check_time))
.map(|dur| dur >= self.check_interval)
.unwrap_or(true);
if should_update {
self.last_check_time.replace(ts);
}
should_update
}
pub fn recycle(&self, ts: Instant<TIMER_HZ>) -> bool {
if let Some(read_timeout) = self.read_timeout {
match self.state {
State::Created | State::WaitingForConnect(_) | State::Connected(_) => false,
State::ShutdownForWrite(closed_time) => ts
.checked_duration_since(closed_time)
.map(|dur| dur >= read_timeout)
.unwrap_or(false),
}
} else {
false
}
}
pub fn closed_by_remote(&mut self, ts: Instant<TIMER_HZ>) {
self.set_state(State::ShutdownForWrite(ts));
self.set_available_data(0);
}
/// Set available data.
pub fn set_available_data(&mut self, available_data: usize) {
self.available_data = available_data;
}
/// Get the number of bytes available to ingress.
pub fn get_available_data(&self) -> usize {
self.available_data
}
/// Return whether a connection is active.
///
/// This function returns true if the socket is actively exchanging packets
/// with a remote endpoint. Note that this does not mean that it is possible
/// to send or receive data through the socket; for that, use
/// [can_recv](#method.can_recv).
pub fn is_connected(&self) -> bool {
// trace!("[{:?}] State: {:?}", self.handle(), self.state);
matches!(self.state, State::Connected(_))
}
/// Return whether the receive half of the full-duplex connection is open.
///
/// This function returns true if it's possible to receive data from the remote endpoint.
/// It will return true while there is data in the receive buffer, and if there isn't,
/// as long as the remote endpoint has not closed the connection.
///
/// In terms of the TCP state machine, the socket must be in the `Connected`,
/// `FIN-WAIT-1`, or `FIN-WAIT-2` state, or have data in the receive buffer instead.
pub fn may_recv(&self) -> bool {
match self.state {
State::Connected(_) | State::ShutdownForWrite(_) => true,
// If we have something in the receive buffer, we can receive that.
_ if !self.rx_buffer.is_empty() => true,
_ => false,
}
}
/// Check whether the receive half of the full-duplex connection buffer is open
/// (see [may_recv](#method.may_recv), and the receive buffer is not full.
pub fn can_recv(&self) -> bool {
if !self.may_recv() {
return false;
}
!self.rx_buffer.is_full()
}
fn recv_impl<'b, F, R>(&'b mut self, f: F) -> Result<R>
where
F: FnOnce(&'b mut SocketBuffer<L>) -> (usize, R),
{
// We may have received some data inside the initial SYN, but until the connection
// is fully open we must not dequeue any data, as it may be overwritten by e.g.
// another (stale) SYN. (We do not support TCP Fast Open.)
if !self.may_recv() {
return Err(Error::Illegal);
}
let (_size, result) = f(&mut self.rx_buffer);
Ok(result)
}
/// Call `f` with the largest contiguous slice of octets in the receive buffer,
/// and dequeue the amount of elements returned by `f`.
///
/// This function returns `Err(Error::Illegal) if the receive half of
/// the connection is not open; see [may_recv](#method.may_recv).
pub fn recv<'b, F, R>(&'b mut self, f: F) -> Result<R>
where
F: FnOnce(&'b mut [u8]) -> (usize, R),
{
self.recv_impl(|rx_buffer| rx_buffer.dequeue_many_with(f))
}
/// Call `f` with a slice of octets in the receive buffer, and dequeue the
/// amount of elements returned by `f`.
///
/// If the buffer read wraps around, the second argument of `f` will be
/// `Some()` with the remainder of the buffer, such that the combined slice
/// of the two arguments, makes up the full buffer.
///
/// This function returns `Err(Error::Illegal) if the receive half of the
/// connection is not open; see [may_recv](#method.may_recv).
pub fn recv_wrapping<'b, F>(&'b mut self, f: F) -> Result<usize>
where
F: FnOnce(&'b [u8], Option<&'b [u8]>) -> usize,
{
self.recv_impl(|rx_buffer| {
rx_buffer.dequeue_many_with_wrapping(|a, b| {
let len = f(a, b);
(len, len)
})
})
}
/// Dequeue a sequence of received octets, and fill a slice from it.
///
/// This function returns the amount of bytes actually dequeued, which is limited
/// by the amount of free space in the transmit buffer; down to zero.
///
/// See also [recv](#method.recv).
pub fn recv_slice(&mut self, data: &mut [u8]) -> Result<usize> {
self.recv_impl(|rx_buffer| {
let size = rx_buffer.dequeue_slice(data);
(size, size)
})
}
/// Peek at a sequence of received octets without removing them from
/// the receive buffer, and return a pointer to it.
///
/// This function otherwise behaves identically to [recv](#method.recv).
pub fn peek(&mut self, size: usize) -> Result<&[u8]> {
// See recv() above.
if !self.may_recv() {
return Err(Error::Illegal);
}
Ok(self.rx_buffer.get_allocated(0, size))
}
pub fn rx_window(&self) -> usize {
self.rx_buffer.window()
}
/// Peek at a sequence of received octets without removing them from
/// the receive buffer, and fill a slice from it.
///
/// This function otherwise behaves identically to [recv_slice](#method.recv_slice).
pub fn peek_slice(&mut self, data: &mut [u8]) -> Result<usize> {
let buffer = self.peek(data.len())?;
let data = &mut data[..buffer.len()];
data.copy_from_slice(buffer);
Ok(buffer.len())
}
pub fn rx_enqueue_slice(&mut self, data: &[u8]) -> usize {
self.rx_buffer.enqueue_slice(data)
}
/// Return the amount of octets queued in the receive buffer.
///
/// Note that the Berkeley sockets interface does not have an equivalent of this API.
pub fn recv_queue(&self) -> usize {
self.rx_buffer.len()
}
pub fn set_state(&mut self, state: State<TIMER_HZ>) {
debug!(
"[TCP Socket] [{:?}] state change: {:?} -> {:?}",
self.handle(),
self.state,
state
);
self.state = state
}
}
impl<const TIMER_HZ: u32, const L: usize> Into<Socket<TIMER_HZ, L>> for TcpSocket<TIMER_HZ, L> {
fn into(self) -> Socket<TIMER_HZ, L> {
Socket::Tcp(self)
}
}