|
| 1 | +/***************************************************************************/ /** |
| 2 | + * @file sl_websocket_client_types.h |
| 3 | + ******************************************************************************* |
| 4 | + * # License |
| 5 | + * <b>Copyright 2025 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 | +#ifndef SL_WEBSOCKET_CLIENT_TYPES_H |
| 30 | +#define SL_WEBSOCKET_CLIENT_TYPES_H |
| 31 | + |
| 32 | +#include "sl_status.h" |
| 33 | +#include "sl_wifi_types.h" |
| 34 | +#include "sl_ip_types.h" |
| 35 | +#include "sl_net_constants.h" |
| 36 | +#include "cmsis_os2.h" |
| 37 | +#include "sl_si91x_socket_types.h" |
| 38 | +#include "sl_si91x_protocol_types.h" |
| 39 | +#include <stdbool.h> |
| 40 | +#include <stdint.h> |
| 41 | + |
| 42 | +/****************************************************** |
| 43 | + * Constants |
| 44 | + ******************************************************/ |
| 45 | +/** |
| 46 | + * @addtogroup SERVICE_WEBSOCKET_CLIENT_CONSTANTS |
| 47 | + * @{ |
| 48 | + */ |
| 49 | + |
| 50 | +#define SL_WEBSOCKET_FIN_BIT 0x80 /**< Final frame bit. Not to be configured by the user. */ |
| 51 | + |
| 52 | +#define SL_SI91X_WEBSOCKET_MAX_HOST_LENGTH 51 /**< Websocket max host length. Not to be configured by the user. */ |
| 53 | + |
| 54 | +#define SL_SI91X_WEBSOCKET_MAX_RESOURCE_LENGTH \ |
| 55 | + 51 /**< Websocket max resource length. Not to be configured by the user. */ |
| 56 | + |
| 57 | +/****************************************************** |
| 58 | + * Enumerations |
| 59 | + ******************************************************/ |
| 60 | + |
| 61 | +/** |
| 62 | + * @brief WebSocket opcodes for different frame types. |
| 63 | + * |
| 64 | + * @details This enumeration defines the opcodes used in WebSocket frames to indicate the type of frame being sent or received. |
| 65 | + */ |
| 66 | +typedef enum { |
| 67 | + SL_WEBSOCKET_OPCODE_CONTINUE = 0x0, /**< Continuation frame */ |
| 68 | + SL_WEBSOCKET_OPCODE_TEXT = 0x1, /**< Text frame */ |
| 69 | + SL_WEBSOCKET_OPCODE_BINARY = 0x2, /**< Binary frame */ |
| 70 | + SL_WEBSOCKET_OPCODE_CLOSE = |
| 71 | + 0x8, /**< Connection close frame. The close frame payload contains a close code (first 2 bytes) and optional reason text, with a maximum length of 125 bytes.*/ |
| 72 | + SL_WEBSOCKET_OPCODE_PING = 0x9, /**< Ping frame */ |
| 73 | + SL_WEBSOCKET_OPCODE_PONG = 0xA, /**< Pong frame */ |
| 74 | +} sl_websocket_opcode_t; |
| 75 | + |
| 76 | +/** |
| 77 | + * @brief Error codes for WebSocket operations. |
| 78 | + * |
| 79 | + * @details This enumeration defines the error codes that can be returned by WebSocket operations to indicate the result of the operation. |
| 80 | + */ |
| 81 | +typedef enum { |
| 82 | + SL_WEBSOCKET_SUCCESS = 0, /**< Operation successful */ |
| 83 | + SL_WEBSOCKET_ERR_SOCKET_CREATION = -1, /**< Error creating socket */ |
| 84 | + SL_WEBSOCKET_ERR_SOCKET_BIND = -2, /**< Error binding socket */ |
| 85 | + SL_WEBSOCKET_ERR_SOCKET_CONNECT = -3, /**< Error connecting socket */ |
| 86 | + SL_WEBSOCKET_ERR_SEND_FRAME = -4, /**< Error sending frame */ |
| 87 | + SL_WEBSOCKET_ERR_RECEIVE_FRAME = -5, /**< Error receiving frame */ |
| 88 | + SL_WEBSOCKET_ERR_CLOSE_FRAME = -6, /**< Error closing frame */ |
| 89 | + SL_WEBSOCKET_ERR_SSL_SETSOCKOPT = -7, /**< Error setting socket options for SSL */ |
| 90 | + SL_WEBSOCKET_ERR_INVALID_PARAMETER = -8 /**< Invalid input parameter */ |
| 91 | +} sl_websocket_error_t; |
| 92 | + |
| 93 | +/** |
| 94 | + * @brief WebSocket connection states. |
| 95 | + * |
| 96 | + * @details This enumeration defines the possible states of a WebSocket connection. |
| 97 | + */ |
| 98 | +typedef enum { |
| 99 | + SL_WEBSOCKET_STATE_DISCONNECTED, /**< The WebSocket client is not connected to the server. This is the initial state before any connection attempt is made or after a connection has been closed or failed. */ |
| 100 | + SL_WEBSOCKET_STATE_CONNECTING, /**< The WebSocket client is in the process of establishing a connection to the server. This state is set when the connection process begins. */ |
| 101 | + SL_WEBSOCKET_STATE_CONNECTED, /**< The WebSocket client is successfully connected to the server. This state indicates that the client can now send and receive WebSocket frames. */ |
| 102 | + SL_WEBSOCKET_STATE_CLOSING, /**< The WebSocket client is in the process of closing the connection. This state is set when the client initiates a close operation. */ |
| 103 | + SL_WEBSOCKET_STATE_CLOSED /**< The WebSocket connection has been closed. This state indicates that the client is no longer connected to the server. */ |
| 104 | +} sl_websocket_state_t; |
| 105 | + |
| 106 | +/** |
| 107 | + * @brief WebSocket predefined status codes for Close frames. |
| 108 | + * |
| 109 | + * @details This enumeration defines the pre-defined status codes that endpoints may use when sending a Close frame. |
| 110 | + */ |
| 111 | +typedef enum { |
| 112 | + SL_WEBSOCKET_CLOSE_NORMAL = 1000, /**< Normal closure */ |
| 113 | + SL_WEBSOCKET_CLOSE_GOING_AWAY = 1001, /**< Endpoint is going away */ |
| 114 | + SL_WEBSOCKET_CLOSE_PROTOCOL_ERROR = 1002, /**< Protocol error */ |
| 115 | + SL_WEBSOCKET_CLOSE_UNSUPPORTED_DATA = 1003, /**< Received data type not supported */ |
| 116 | + SL_WEBSOCKET_CLOSE_RESERVED = 1004, /**< Reserved */ |
| 117 | + SL_WEBSOCKET_CLOSE_NO_STATUS = 1005, /**< No status code present */ |
| 118 | + SL_WEBSOCKET_CLOSE_ABNORMAL = 1006, /**< Abnormal closure */ |
| 119 | + SL_WEBSOCKET_CLOSE_INVALID_PAYLOAD = 1007, /**< Invalid payload data */ |
| 120 | + SL_WEBSOCKET_CLOSE_POLICY_VIOLATION = 1008, /**< Policy violation */ |
| 121 | + SL_WEBSOCKET_CLOSE_MESSAGE_TOO_BIG = 1009, /**< Message too big */ |
| 122 | + SL_WEBSOCKET_CLOSE_MISSING_EXTENSION = 1010, /**< Missing required extension */ |
| 123 | + SL_WEBSOCKET_CLOSE_INTERNAL_ERROR = 1011, /**< Internal server error */ |
| 124 | + SL_WEBSOCKET_CLOSE_TLS_HANDSHAKE_FAILURE = 1015 /**< TLS handshake failure */ |
| 125 | +} sl_websocket_close_status_code_t; |
| 126 | + |
| 127 | +/** @} */ |
| 128 | +/****************************************************** |
| 129 | + * Type Definitions |
| 130 | + ******************************************************/ |
| 131 | +// Forward declaration of the type |
| 132 | +typedef struct sl_websocket_client_s sl_websocket_client_t; |
| 133 | + |
| 134 | +/****************************************************** |
| 135 | + * Structures |
| 136 | + ******************************************************/ |
| 137 | + |
| 138 | +/** |
| 139 | + * @addtogroup SERVICE_WEBSOCKET_CLIENT_TYPES |
| 140 | + * @{ |
| 141 | + */ |
| 142 | + |
| 143 | +/** |
| 144 | + * @brief WebSocket client configuration structure. |
| 145 | + * |
| 146 | + * @details This structure holds the configuration parameters for initializing a WebSocket client. |
| 147 | + */ |
| 148 | +typedef struct { |
| 149 | + char *host; /**< WebSocket server host (for example, "example.com"). */ |
| 150 | + char *resource; /**< WebSocket resource path (for example, "/chat"). */ |
| 151 | + uint16_t server_port; /**< WebSocket server port number. */ |
| 152 | + uint16_t client_port; /**< Local client port number. */ |
| 153 | + char *ip_address; /**< WebSocket server IP address. */ |
| 154 | + sl_si91x_socket_receive_data_callback_t data_cb; /**< Data receive callback function. */ |
| 155 | + sl_si91x_socket_remote_termination_callback_t |
| 156 | + remote_terminate_cb; /**< Callback function for remote termination event. */ |
| 157 | + bool enable_ssl; /**< Enable SSL for WebSocket connection. */ |
| 158 | +} sl_websocket_config_t; |
| 159 | + |
| 160 | +/** |
| 161 | + * @brief WebSocket client structure. |
| 162 | + * |
| 163 | + * @details This structure holds the state and configuration of a WebSocket client, including socket descriptors, connection state, and callback functions. This is not a user configurable structure. |
| 164 | + */ |
| 165 | +typedef struct sl_websocket_client_s { |
| 166 | + int socket_fd; /**< BSD socket file descriptor. */ |
| 167 | + char host[SL_SI91X_WEBSOCKET_MAX_HOST_LENGTH]; /**< WebSocket server host (for example, "example.com"). */ |
| 168 | + char resource[SL_SI91X_WEBSOCKET_MAX_RESOURCE_LENGTH]; /**< WebSocket resource path (for example, "/chat"). */ |
| 169 | + char subprotocol[SLI_WEBS_MAX_SUBPROTOCOL_LENGTH]; /**< WebSocket subprotocol (for example, "mqtt"). */ |
| 170 | + uint16_t server_port; /**< WebSocket server port number. */ |
| 171 | + uint16_t client_port; /**< Local client port number. */ |
| 172 | + sl_ip_address_t ip_address; /**< WebSocket server IP address. */ |
| 173 | + uint8_t mask_key[4]; /**< Masking key for client-to-server frames (used in hosted mode). */ |
| 174 | + sl_websocket_state_t state; /**< WebSocket connection state. */ |
| 175 | + sl_si91x_socket_receive_data_callback_t data_cb; /**< Data receive callback function. */ |
| 176 | + sl_si91x_socket_remote_termination_callback_t |
| 177 | + remote_terminate_cb; /**< Callback function for remote termination event. */ |
| 178 | + bool enable_ssl; /**< Enable SSL for WebSocket connection. */ |
| 179 | + void *user_context; /**< User-defined context (for future reference). */ |
| 180 | +} sl_websocket_client_t; |
| 181 | + |
| 182 | +/** |
| 183 | + * @brief WebSocket send request structure. |
| 184 | + * |
| 185 | + * @details This structure holds the parameters for sending a WebSocket frame, including the opcode, payload buffer, and payload length. |
| 186 | + */ |
| 187 | +typedef struct { |
| 188 | + sl_websocket_opcode_t opcode; /**< Opcode (TEXT, BINARY, and so on.). */ |
| 189 | + const uint8_t *buffer; /**< Pointer to the payload. */ |
| 190 | + size_t length; /**< Length of the payload. */ |
| 191 | +} sl_websocket_send_request_t; |
| 192 | + |
| 193 | +/** @} */ |
| 194 | + |
| 195 | +#endif //SL_WEBSOCKET_CLIENT_TYPES_H |
0 commit comments