1 /******************************************************************************** 2 * @file sl_si91x_socket_types.h 3 ******************************************************************************* 4 * # License 5 * <b>Copyright 2024 Silicon Laboratories Inc. www.silabs.com</b> 6 ******************************************************************************* 7 * 8 * SPDX-License-Identifier: Zlib 9 * 10 * The licensor of this software is Silicon Laboratories Inc. 11 * 12 * This software is provided 'as-is', without any express or implied 13 * warranty. In no event will the authors be held liable for any damages 14 * arising from the use of this software. 15 * 16 * Permission is granted to anyone to use this software for any purpose, 17 * including commercial applications, and to alter it and redistribute it 18 * freely, subject to the following restrictions: 19 * 20 * 1. The origin of this software must not be misrepresented; you must not 21 * claim that you wrote the original software. If you use this software 22 * in a product, an acknowledgment in the product documentation would be 23 * appreciated but is not required. 24 * 2. Altered source versions must be plainly marked as such, and must not be 25 * misrepresented as being the original software. 26 * 3. This notice may not be removed or altered from any source distribution. 27 * 28 ******************************************************************************/ 29 30 #pragma once 31 32 #include <stdint.h> 33 #include <sys/socket.h> 34 35 #include "sl_si91x_types.h" 36 #include "cmsis_os2.h" // CMSIS RTOS2 37 #include "sl_si91x_protocol_types.h" 38 39 /** 40 * @addtogroup SI91X_SOCKET_FUNCTIONS 41 * @{ 42 */ 43 /** 44 * @brief Structure for socket metadata associated with read event. 45 * 46 * @details 47 * The structure holds the metadata information for the socket's read event. 48 * It includes the following details such as: IP version, socket ID, length of data received, 49 * offset within the buffer, destination port, and the IP address of the sender device. 50 * The IP address can be either IPv4 or IPv6, determined by the 51 * `ip_version` field. 52 */ 53 typedef struct { 54 uint16_t 55 ip_version; ///< Two bytes for the IP version of the IP address, four bytes for the IPv4, and six bytes for the IPv6. 56 57 uint16_t socket_id; ///< The socket number associated with the read event is two bytes. 58 59 uint32_t length; ///< Four bytes. Length of received data. 60 61 uint16_t offset; ///< Two bytes. Offset data from the start of the buffer. 62 63 uint16_t dest_port; ///< Two bytes. Port number of the device which sends data to the destination. 64 65 union { 66 uint8_t ipv4_address[4]; ///< Four bytes. IPv4 address of the device which sends data. Used if ip_version is four. 67 68 uint8_t ipv6_address[16]; ///< 16 bytes. IPv6 address of the device which sends data. Used if ip_version is six. 69 } dest_ip_addr; ///< Union for IPv4 or IPv6 address, depending on ip_version. 70 } sl_si91x_socket_metadata_t; 71 72 /** 73 * @typedef sl_si91x_socket_receive_data_callback_t 74 * @brief Callback function reads asynchronous data from the socket. 75 * 76 * @details 77 * The callback function reads asynchronous data from the socket when the sl_si91x_socket_async 78 * API is registered and called. The callback provides the following details: 79 * socket ID, pointer to the buffer which contains receiver data, size of the buffer, 80 * and metadata of the receiver packet (such as IP address, and port number). 81 * 82 * 83 * @param socket 84 * Socket ID. 85 * 86 * @param buffer 87 * Pointer to the buffer which stores the receiver data. 88 * 89 * @param length 90 * Buffer size. 91 * 92 * @param firmware_socket_response 93 * Pointer to sl_si91x_socket_metadata_t structure contains receiver packet metadata information. 94 * The metadata information consists of IP address (either, Ipv4 or IPV6), and port number. 95 * 96 * @return 97 * N/A 98 */ 99 typedef void (*sl_si91x_socket_receive_data_callback_t)(uint32_t socket, 100 uint8_t *buffer, 101 uint32_t length, 102 const sl_si91x_socket_metadata_t *firmware_socket_response); 103 104 /** 105 * @typedef sl_si91x_socket_accept_callback_t 106 * @brief Callback functions for new asynchronous accepted connection. 107 * 108 * @details 109 * The callback provides paramenters for new accepted connection when the sl_si91x_accept_async API is registered and called. 110 * The callback provides the following details: socket ID of the accepted connection, address of remoter peer, 111 * and IP version of connection. 112 * 113 * @param socket 114 * Socket ID of the accepted connection. 115 * 116 * @param addr 117 * Pointer to `struct sockaddr` contains remote peer address. 118 * 119 * @param ip_version 120 * IP version of the connection (for example, four bytes for IPv4, and six bytes for IPv6). 121 * 122 * @return 123 * N/A 124 */ 125 typedef void (*sl_si91x_socket_accept_callback_t)(int32_t socket, struct sockaddr *addr, uint8_t ip_version); 126 127 /** 128 * @typedef sl_si91x_socket_data_transfer_complete_handler_t 129 * @brief Callback function indicates data transfer status. 130 * 131 * @details 132 * The callback indicates the data transfer completion status when either of the sl_si91x_send_async or sl_si91x_sendto_async API is registered and called. 133 * The callback provides the socket ID, and the number of bytes that are successfully transfer. 134 * 135 * @param socket 136 * Socket ID. 137 * 138 * @param length 139 * Number of bytes transferred. 140 * 141 * @return 142 * N/A 143 */ 144 typedef void (*sl_si91x_socket_data_transfer_complete_handler_t)(int32_t socket, uint16_t length); 145 146 /** 147 * @typedef sl_si91x_socket_select_callback_t 148 * @brief Callback function indicates asynchronous select request result. 149 * 150 * @details 151 * The callback indicates asynchronous response reaches the select request when the sl_si91x_select API is registered and called. 152 * The callback provides the following details: file descriptor sets for read, write, and exception conditions, and status of the selected request. 153 * 154 * @param fd_read 155 * File descriptor pointer sets for read operations. 156 * 157 * @param fd_write 158 * File descriptor pointer sets for write operations. 159 * 160 * @param fd_except 161 * File descriptor pointer sets for exception condition. 162 * 163 * @param status 164 * Select request status. 165 * 166 * @return 167 * N/A 168 */ 169 typedef void (*sl_si91x_socket_select_callback_t)(sl_si91x_fd_set *fd_read, sl_si91x_fd_set *fd_write, sl_si91x_fd_set *fd_except, int32_t status); 170 171 /** 172 * @typedef sl_si91x_socket_remote_termination_callback_t 173 * @brief Callback function indicates termination of the remote socket. 174 * 175 * @details 176 * The callback function notifies on the termination of the remote socket when the sl_si91x_set_remote_termination_callback API is registered and called. 177 * The callback provides the following details: socket ID, remote socket port number, and number of bytes sent before termination of the remote socket. 178 * 179 * @param socket 180 * Socket ID. 181 * 182 * @param port 183 * Remote socket port number. 184 * 185 * @param bytes_sent 186 * Number of bytes sent before termination. 187 * 188 * @return 189 * The callback does not returns value. 190 */ 191 typedef void (*sl_si91x_socket_remote_termination_callback_t)(int socket, uint16_t port, uint32_t bytes_sent); 192 193 /** @} */ 194 195 /// Internal si91x BSD socket status 196 typedef enum { 197 RESET = 0, // State of unallocated socket. 198 INITIALIZED, // Socket attains this state when socket() has been executed successfully. 199 BOUND, // Socket attains this state when bind() has been executed successfully. 200 LISTEN, // (TCP ONLY STATE) Socket attains this state when listen() has been executed successfully. 201 UDP_UNCONNECTED_READY, // (UDP ONLY STATE) Socket attains this state when sendto() or recvfrom() has been executed successfully prior connect. 202 CONNECTED, // Socket attains this state when connect() has been executed successfully. 203 DISCONNECTED // Socket attains this state when underlying connection is lost 204 } sli_si91x_bsd_socket_state_t; 205 206 #define SI91X_MAX_SIZE_OF_EXTENSION_DATA 256 207 208 #pragma pack() 209 /// Internal si91x TLS extensions 210 typedef struct { 211 uint8_t buffer[SI91X_MAX_SIZE_OF_EXTENSION_DATA]; ///< Buffer 212 uint16_t total_extensions; ///< Total extensions 213 uint16_t current_size_of_extensions; ///< Current size of extensions 214 } sli_si91x_tls_extensions_t; 215 216 /// Structure to hold WebSocket host and resource information 217 typedef struct { 218 uint8_t host_length; ///< Length of WebSocket host name 219 uint8_t resource_length; ///< Length of WebSocket resource name 220 uint8_t websocket_data[]; ///< WebSocket resource name and host name 221 } sli_si91x_websocket_info_t; 222 223 #pragma pack() 224 225 /// Internal si91x socket handle 226 typedef struct { 227 int32_t id; ///< Socket ID 228 int32_t type; ///< Socket type 229 int32_t index; ///< Socket index 230 int role; ///< Socket role 231 int32_t protocol; ///< Protocol 232 uint16_t tcp_keepalive_initial_time; ///< TCP keepalive intial time 233 uint8_t max_tcp_retries; ///< MAX TCOP retries 234 uint16_t read_timeout; ///< Read timeout 235 uint8_t certificate_index; ///< Certificate Index 236 uint8_t vap_id; ///< Virtual AP ID 237 uint16_t mss; ///< Maximum segment size (MSS) value 238 struct sockaddr_in6 local_address; ///< Using sockaddr_in6 to hold either IPV4 or IPV6. 239 struct sockaddr_in6 remote_address; ///< Using sockaddr_in6 to hold either IPV4 or IPV6. 240 sli_si91x_bsd_socket_state_t state; ///< BSD socket state (used for internal tracking) 241 sli_si91x_tls_extensions_t tls_extensions; ///< TLS Extension 242 bool is_waiting_on_ack; ///< Boolean flag to check if socket is waiting for an ack. 243 #if defined(SLI_SI917) || defined(SLI_SI915) 244 uint32_t ssl_bitmap; ///< SSL bitmap 245 uint32_t max_retransmission_timeout_value; ///< Max retransmission timeout value 246 uint32_t tos; ///< TOS 247 #else 248 uint8_t ssl_bitmap; ///< SSL Bitmap 249 #endif 250 uint8_t opcode; ///< Opcode used in websocket 251 sli_si91x_websocket_info_t *websocket_info; ///< Pointer to WebSocket info 252 sl_si91x_socket_receive_data_callback_t recv_data_callback; ///< Receive data callback 253 sl_si91x_socket_data_transfer_complete_handler_t data_transfer_callback; ///< Data transfer callback 254 sl_si91x_socket_accept_callback_t user_accept_callback; ///< Async Accept callback 255 osEventFlagsId_t socket_events; ///< Event Flags for sockets 256 int32_t client_id; ///< Client Socket Id for accept 257 uint8_t socket_bitmap; ///< Socket Bitmap 258 uint8_t data_buffer_count; ///< Number of queued data buffers allocated by this socket 259 uint8_t data_buffer_limit; ///< Maximum number of queued data buffers permitted for this socket 260 sli_si91x_command_queue_t command_queue; ///< Command queue 261 sl_si91x_buffer_queue_t tx_data_queue; ///< Transmit data queue 262 sl_si91x_buffer_queue_t rx_data_queue; ///< Receive data queue 263 } sli_si91x_socket_t; 264