Skip to content

Commit e10a2f5

Browse files
committed
wiseconnect: Websockets NetStack Application protocol
Origin: Silicon Labs WiseConnect SDK License: Zlib URL: https://github.com/siliconlabs/wiseconnect Commit: 05ef77e82bb473fdf157d33fb9cbf6e1d9a11a9e Version: v3.5.0-rc3 Purpose: To support the SiWx917 Websockets Network Stack Application protocol offloading, we need to add the Silicon Labs APIs,so adding the corresponding source files and include files Signed-off-by: Rahul Gurram <[email protected]>
1 parent e654c05 commit e10a2f5

File tree

6 files changed

+1006
-0
lines changed

6 files changed

+1006
-0
lines changed

wiseconnect/components/device/silabs/si91x/wireless/socket/inc/sl_si91x_socket_constants.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@
8787

8888
#define SLI_MAX_RETRANSMISSION_TIME_VALUE 32
8989

90+
91+
#define SOL_TCP 0x0006 ///< This is used to denote that the options are applicable at the TCP level.
92+
#define TCP_ULP 0x001f ///< Attach a ULP (Upper Layer Protocol) to a TCP connection.
93+
#define TLS "tls" ///< Option value for default TLS version.
94+
#define TLS_1_0 "tls_1_0" ///< Option value for TLS 1.0.
95+
#define TLS_1_1 "tls_1_1" ///< Option value for TLS 1.1.
96+
#define TLS_1_2 "tls_1_2" ///< Option value for TLS 1.2.
97+
#define TLS_1_3 "tls_1_3" ///< Option value for TLS 1.3.
9098
/**
9199
* @addtogroup SI91X_SOCKET_OPTION_NAME SiWx91x Socket Option Name
92100
* @ingroup SI91X_SOCKET_FUNCTIONS
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/***************************************************************************/ /**
2+
* @file sl_websocket_client.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_H
30+
#define SL_WEBSOCKET_CLIENT_H
31+
32+
#include <stdbool.h>
33+
#include <stdint.h>
34+
#include "cmsis_os2.h"
35+
#include "sl_status.h"
36+
#include "sl_wifi_types.h"
37+
#include "sl_ip_types.h"
38+
#include "sl_net_constants.h"
39+
#include "sl_si91x_socket_types.h"
40+
#include "sl_websocket_client_types.h"
41+
42+
/******************************************************
43+
* Function Declarations
44+
******************************************************/
45+
46+
/**
47+
* @addtogroup SERVICE_WEBSOCKET_CLIENT_FUNCTIONS
48+
* @{
49+
*/
50+
51+
/***************************************************************************/ /**
52+
* @brief Initialize the WebSocket client.
53+
*
54+
* @details
55+
* This function initializes the WebSocket client with the provided configuration.
56+
* It allocates necessary resources and sets up the client for connection.
57+
*
58+
* @param[out] handle
59+
* Pointer to the WebSocket client structure. Must not be NULL.
60+
*
61+
* @param[in] config
62+
* Pointer to the WebSocket client configuration constant structure. Must not be NULL.
63+
*
64+
* @return
65+
* sl_websocket_error_t - Error code indicating the result of the operation.
66+
*/
67+
sl_websocket_error_t sl_websocket_init(sl_websocket_client_t *handle, const sl_websocket_config_t *config);
68+
69+
/***************************************************************************/ /**
70+
* @brief Connect to a WebSocket server.
71+
*
72+
* @details
73+
* This function creates a socket, binds it, and connects to the specified WebSocket server.
74+
*
75+
* @pre
76+
* The WebSocket handle should be initialized using @ref sl_websocket_init before calling this function.
77+
*
78+
* @param[in] handle
79+
* Pointer to the WebSocket client structure. Must not be NULL.
80+
*
81+
* @return
82+
* sl_websocket_error_t - Error code indicating the result of the operation.
83+
*/
84+
sl_websocket_error_t sl_websocket_connect(sl_websocket_client_t *handle);
85+
86+
/***************************************************************************/ /**
87+
* @brief Send a WebSocket frame.
88+
*
89+
* @details
90+
* This function sends a WebSocket frame to the server. Masking is taken care of by the firmware.
91+
*
92+
* @pre
93+
* The WebSocket handle should be initialized using @ref sl_websocket_init and connected using @ref sl_websocket_connect before calling this function.
94+
*
95+
* @param[in] handle
96+
* Pointer to the WebSocket client structure. Must not be NULL.
97+
*
98+
* @param[in] send_request
99+
* Pointer to the send request constant structure containing the frame details. Must not be NULL.
100+
*
101+
* @return
102+
* sl_websocket_error_t - Error code indicating the result of the operation.
103+
*
104+
* @note Masking refers to applying a random 32-bit mask to the data sent to the server to ensure data integrity and security.
105+
*
106+
* @note The following table lists the maximum individual chunk of data that can be sent over each supported protocol.
107+
* The length of the payload is specified in the `length` field of the `sl_websocket_send_request_t` structure.
108+
*
109+
* Protocol | Maximum data chunk (bytes)
110+
* --------------------|---------------------------
111+
* WebSocket | 1450 bytes
112+
* WebSocket over SSL | 1362 bytes
113+
*/
114+
sl_websocket_error_t sl_websocket_send_frame(sl_websocket_client_t *handle,
115+
const sl_websocket_send_request_t *send_request);
116+
117+
/***************************************************************************/ /**
118+
* @brief Close the WebSocket connection.
119+
*
120+
* @details
121+
* This function closes the WebSocket connection and cleans up resources.
122+
*
123+
* @pre
124+
* The WebSocket handle should be initialized using @ref sl_websocket_init and connected using @ref sl_websocket_connect before calling this function.
125+
*
126+
* @param[in] handle
127+
* Pointer to the WebSocket client structure. Must not be NULL.
128+
*
129+
* @return
130+
* sl_websocket_error_t - Error code indicating the result of the operation.
131+
*/
132+
sl_websocket_error_t sl_websocket_close(sl_websocket_client_t *handle);
133+
134+
/***************************************************************************/ /**
135+
* @brief Deinitialize the WebSocket client.
136+
*
137+
* @details
138+
* This function deinitializes the WebSocket client by freeing allocated resources and resetting the state.
139+
* It should be called only after the WebSocket connection has been closed because it also attempts to close the socket as part of its cleanup process if the socket remains open after the @ref sl_websocket_close is invoked.
140+
* Therefore, calling this function is mandatory whenever a WebSocket connection is terminated, ensuring proper cleanup.
141+
*
142+
* @pre
143+
* The WebSocket handle should be initialized using @ref sl_websocket_init before calling this function.
144+
*
145+
* @param[in] handle
146+
* Pointer to the WebSocket client structure. Must not be NULL.
147+
*
148+
* @return
149+
* sl_websocket_error_t - Error code indicating the result of the operation.
150+
*/
151+
sl_websocket_error_t sl_websocket_deinit(sl_websocket_client_t *handle);
152+
153+
/***************************************************************************/ /**
154+
* @brief Extracts the WebSocket opcode from a given socket ID.
155+
*
156+
* @details
157+
* This function determines the opcode of the WebSocket from the socket ID.
158+
* The socket_id is obtained from the recv_data_callback from the sl_si91x_socket_metadata_t parameter.
159+
*
160+
* @param[in] socket_id
161+
* The 16-bit socket ID from which to extract the opcode.
162+
*
163+
* @return
164+
* sl_websocket_opcode_t - The extracted WebSocket opcode.
165+
*/
166+
sl_websocket_opcode_t sl_si91x_get_opcode_from_socket_id(uint16_t socket_id);
167+
/** @} */
168+
169+
#endif //SL_WEBSOCKET_CLIENT_H
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
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

Comments
 (0)