wincwifi 0.3.0

WINC1500 Wifi chip embedded driver
Documentation
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/// Default receive timeout (in milliseconds) for both TCP and UDP socket operations.
const DEFAULT_SOCKET_RECEIVE_TIMEOUT: u32 = 10_000;

/// SSL Socket Options
#[cfg(feature = "ssl")]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Default, PartialEq, Debug, Clone, Copy)]
struct SslSockInfo {
    pub cfg: u8,
    pub data_offset: u16,
}

/// Network Socket.
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(PartialEq, Debug, Clone, Copy)]
pub struct Socket {
    /// Socket Identifier.
    pub v: u8, // todo make this not public
    /// Session Id.
    pub s: u16,
    /// Receive Timeout.
    receive_timeout: u32,
    /// SSL Sockets Information.
    #[cfg(feature = "ssl")]
    ssl: SslSockInfo,
}

/// Implementation of `Socket` to create new instance and managing the receive timeout.
impl Socket {
    /// Creates a new instance of `Socket` with a default receive timeout (10,000 msecs).
    ///
    /// # Arguments
    ///
    /// * `v` - Socket identifier.
    /// * `s` - Session identifier.
    ///
    /// # Returns
    ///
    /// * `Socket` - A new `Socket` instance with default configuration.
    pub fn new(v: u8, s: u16) -> Self {
        Socket {
            v,
            s,
            receive_timeout: DEFAULT_SOCKET_RECEIVE_TIMEOUT,
            #[cfg(feature = "ssl")]
            ssl: SslSockInfo::default(),
        }
    }

    /// Sets the receive timeout for the socket.
    ///
    /// # Arguments
    ///
    /// * `timeout` - Timeout duration in milliseconds.
    pub(crate) fn set_recv_timeout(&mut self, timeout: u32) {
        self.receive_timeout = timeout;
    }

    /// Returns the receive timeout of the socket, in milliseconds.
    pub(crate) fn get_recv_timeout(&self) -> u32 {
        self.receive_timeout
    }

    /// Set the SSL Options
    ///
    /// # Arguments
    ///
    /// * `ssl_opt` - SSL socket options.
    #[cfg(feature = "ssl")]
    pub(crate) fn set_ssl_cfg(&mut self, ssl_opt: u8, enable: bool) {
        if enable {
            self.ssl.cfg |= ssl_opt;
        } else {
            self.ssl.cfg &= !ssl_opt;
        }
    }

    /// Returns the SSL options value set on a socket.
    pub(crate) fn get_ssl_cfg(&self) -> u8 {
        #[cfg(feature = "ssl")]
        {
            self.ssl.cfg
        }
        #[cfg(not(feature = "ssl"))]
        {
            0
        }
    }

    /// Set the data offset for SSL operations.
    ///
    /// # Arguments
    ///
    /// * `offset` - Data offset received from SSL connect command.
    #[cfg(feature = "ssl")]
    pub(crate) fn set_ssl_data_offset(&mut self, offset: u16) {
        self.ssl.data_offset = offset
    }

    /// Returns the data offset for SSL operations.
    #[cfg(feature = "ssl")]
    pub(crate) fn get_ssl_data_offset(&self) -> u16 {
        self.ssl.data_offset
    }
}

/// Creates a new `Socket` instance from a tuple of `(Socket ID, Session ID)`, with a default receive timeout (10,000 msecs).
impl From<(u8, u16)> for Socket {
    fn from(val: (u8, u16)) -> Self {
        Socket {
            v: val.0,
            s: val.1,
            receive_timeout: DEFAULT_SOCKET_RECEIVE_TIMEOUT,
            #[cfg(feature = "ssl")]
            ssl: SslSockInfo::default(),
        }
    }
}

/// Converts a `Socket` into a tuple of `(Socket ID, Session ID)`.
impl From<Socket> for (u8, u16) {
    fn from(sock: Socket) -> Self {
        (sock.v, sock.s)
    }
}