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
use core::convert::TryInto;

use super::ssl::SecurityProfileId;
use super::DataService;
use super::{
    socket::{Error as SocketError, SocketHandle, TcpSocket, TcpState},
    Error, EGRESS_CHUNK_SIZE,
};
use crate::command::ip_transport_layer::{
    types::{SocketProtocol, SslTlsStatus},
    CloseSocket, ConnectSocket, CreateSocket, PrepareWriteSocketDataBinary, SetSocketSslState,
    WriteSocketDataBinary,
};
use embedded_nal::{SocketAddr, TcpClientStack};
use embedded_time::{
    duration::{Generic, Milliseconds},
    Clock,
};

impl<'a, C, CLK, const N: usize, const L: usize> TcpClientStack for DataService<'a, C, CLK, N, L>
where
    C: atat::AtatClient,
    CLK: Clock,
    Generic<CLK::T>: TryInto<Milliseconds>,
{
    type Error = Error;

    // Only return a SocketHandle to reference into the SocketSet owned by the GsmClient,
    // as the Socket object itself provides no value without accessing it though the client.
    type TcpSocket = SocketHandle;

    /// Open a new TCP socket to the given address and port. The socket starts in the unconnected state.
    fn socket(&mut self) -> Result<Self::TcpSocket, Self::Error> {
        // Check if there are any unused sockets available
        if self.sockets.len() >= self.sockets.capacity() {
            if let Ok(ts) = self.network.status.timer.try_now() {
                // Check if there are any sockets closed by remote, and close it
                // if it has exceeded its timeout, in order to recycle it.
                if !self.sockets.recycle(&ts) {
                    return Err(Error::Socket(SocketError::SocketSetFull));
                }
            } else {
                return Err(Error::Socket(SocketError::SocketSetFull));
            }
        }

        let socket_resp = self.network.send_internal(
            &CreateSocket {
                protocol: SocketProtocol::TCP,
                local_port: None,
            },
            true,
        )?;

        Ok(self.sockets.add(TcpSocket::new(socket_resp.socket.0))?)
    }

    /// Connect to the given remote host and port.
    fn connect(
        &mut self,
        socket: &mut Self::TcpSocket,
        remote: SocketAddr,
    ) -> nb::Result<(), Self::Error> {
        let mut tcp = self
            .sockets
            .get::<TcpSocket<CLK, L>>(*socket)
            .map_err(Self::Error::from)?;

        if matches!(tcp.state(), TcpState::Created) {
            self.network
                .send_internal(
                    &SetSocketSslState {
                        socket: *socket,
                        ssl_tls_status: SslTlsStatus::Enabled(SecurityProfileId(0)),
                    },
                    true,
                )
                .map_err(Self::Error::from)?;

            self.network
                .send_internal(
                    &ConnectSocket {
                        socket: *socket,
                        remote_addr: remote.ip(),
                        remote_port: remote.port(),
                    },
                    false,
                )
                .map_err(Self::Error::from)?;

            tcp.set_state(TcpState::Connected);
            Ok(())
        } else {
            defmt::error!(
                "Cannot connect socket! Socket: {} is in state: {}",
                socket,
                tcp.state()
            );
            Err(Error::Socket(SocketError::Illegal).into())
        }
    }

    /// Check if this socket is still connected
    fn is_connected(&mut self, socket: &Self::TcpSocket) -> Result<bool, Self::Error> {
        Ok(self
            .sockets
            .get::<TcpSocket<CLK, L>>(*socket)?
            .is_connected())
    }

    /// Write to the stream. Returns the number of bytes written is returned
    /// (which may be less than `buffer.len()`), or an error.
    fn send(
        &mut self,
        socket: &mut Self::TcpSocket,
        buffer: &[u8],
    ) -> nb::Result<usize, Self::Error> {
        if !self.is_connected(&socket)? {
            return Err(Error::SocketClosed.into());
        }

        for chunk in buffer.chunks(EGRESS_CHUNK_SIZE) {
            defmt::trace!("Sending: {} bytes", chunk.len());
            self.network
                .send_internal(
                    &PrepareWriteSocketDataBinary {
                        socket: *socket,
                        length: chunk.len(),
                    },
                    false,
                )
                .map_err(Self::Error::from)?;

            let response = self
                .network
                .send_internal(
                    &WriteSocketDataBinary {
                        data: serde_bytes::Bytes::new(chunk),
                    },
                    false,
                )
                .map_err(Self::Error::from)?;

            if response.length != chunk.len() {
                return Err(Error::BadLength.into());
            }
            if &response.socket != socket {
                return Err(Error::WrongSocketType.into());
            }
        }

        Ok(buffer.len())
    }

    /// Read from the stream. Returns `Ok(n)`, which means `n` bytes of
    /// data have been received and they have been placed in
    /// `&buffer[0..n]`, or an error.
    fn receive(
        &mut self,
        socket: &mut Self::TcpSocket,
        buffer: &mut [u8],
    ) -> nb::Result<usize, Self::Error> {
        let mut tcp = self
            .sockets
            .get::<TcpSocket<CLK, L>>(*socket)
            .map_err(Self::Error::from)?;

        Ok(tcp.recv_slice(buffer).map_err(Self::Error::from)?)
    }

    /// Close an existing TCP socket.
    fn close(&mut self, socket: Self::TcpSocket) -> Result<(), Self::Error> {
        self.network.send_internal(&CloseSocket { socket }, false)?;
        self.sockets.remove(socket)?;
        Ok(())
    }
}