/ firmware / src / networking / tcp.rs
tcp.rs
 1  use embassy_net::tcp::TcpSocket;
 2  
 3  pub async fn read_exact(
 4      socket: &mut TcpSocket<'_>,
 5      buf: &mut [u8],
 6  ) -> Result<(), embassy_net::tcp::Error> {
 7      let mut offset = 0;
 8      while offset < buf.len() {
 9          match socket.read(&mut buf[offset..]).await {
10              Ok(0) => return Err(embassy_net::tcp::Error::ConnectionReset),
11              Ok(bytes_read) => {
12                  offset += bytes_read;
13              }
14              Err(error) => return Err(error),
15          }
16      }
17  
18      Ok(())
19  }
20  
21  pub async fn write_all(
22      socket: &mut TcpSocket<'_>,
23      payload: &[u8],
24  ) -> Result<(), embassy_net::tcp::Error> {
25      let mut bytes_written_total = 0;
26  
27      while bytes_written_total < payload.len() {
28          let bytes_written_this_iteration = socket.write(&payload[bytes_written_total..]).await?;
29          if bytes_written_this_iteration == 0 {
30              return Err(embassy_net::tcp::Error::ConnectionReset);
31          }
32  
33          bytes_written_total += bytes_written_this_iteration;
34      }
35  
36      Ok(())
37  }