Skip to content

Commit

Permalink
socket/cs_WebSocket: resolve wrong usage of strcat for the url
Browse files Browse the repository at this point in the history
  • Loading branch information
RicArch97 committed Jan 15, 2023
1 parent 07ee835 commit 10a248f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
12 changes: 9 additions & 3 deletions boards/esp32.overlay
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// / {
// aliases {
// w5500-ethernet = &w5500;
// };
// };

// Enable ESP32 Wifi
&wifi {
status = "okay";
Expand All @@ -11,11 +17,11 @@
// Enable W5500 Ethernet module
// &spi3 {
// status = "okay";
// clock-frequency = <1000000>;
// w5500: w5500@0 {
// compatible = "wiznet,w5500";
// reg = <0>;
// spi-max-frequency = <18000000>;
// int-gpios = <&gpio1 1 GPIO_ACTIVE_LOW>;
// reset-gpios = <&gpio0 27 GPIO_ACTIVE_LOW>;
// spi-max-frequency = <1000000>;
// int-gpios = <&gpio0 13 GPIO_ACTIVE_LOW>;
// };
// };
1 change: 1 addition & 0 deletions include/socket/cs_WebSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#define CS_WEBSOCKET_HTTP_HEADER_SIZE 30
#define CS_WEBSOCKET_RECV_TIMEOUT 3000
#define CS_WEBSOCKET_RECV_RETRY_TIMOUT 50
#define CS_WEBSOCKET_URL_MAX_LEN 32

#define CS_WEBSOCKET_CONNECTED_EVENT 0x001

Expand Down
8 changes: 5 additions & 3 deletions prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
CONFIG_POSIX_API=y

# Enables C++ support
CONFIG_CPLUSPLUS=y
CONFIG_CPP=y
CONFIG_CPP_MAIN=y

CONFIG_MAIN_STACK_SIZE=9472
Expand All @@ -32,6 +32,9 @@ CONFIG_NET_TCP=y
CONFIG_NET_IPV6=y
CONFIG_NET_IPV4=y
CONFIG_NET_DHCPV4=y
# 2 interfaces, 1 for ethernet and 1 for wifi
# CONFIG_NET_IF_MAX_IPV4_COUNT=2
# CONFIG_NET_IF_MAX_IPV6_COUNT=2

# Enable the network management API, and receive networking events
CONFIG_NET_MGMT=y
Expand Down Expand Up @@ -63,8 +66,7 @@ CONFIG_ESP32_WIFI_STA_RECONNECT=y

# Enable Ethernet
CONFIG_NET_L2_ETHERNET=y
# CONFIG_ETH_W5500=y
# CONFIG_ETH_W5500_TIMEOUT=1000
#CONFIG_ETH_W5500=y

# Enable DNS
CONFIG_DNS_RESOLVER=y
Expand Down
2 changes: 1 addition & 1 deletion src/drivers/cs_Wifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ cs_err_t Wifi::init(const char *ssid, const char *psk)
return CS_ERR_DEVICE_NOT_READY;
}

_iface = net_if_get_default();
_iface = net_if_lookup_by_dev(wifi_dev);
// Get the default network interface
if (_iface == NULL) {
LOG_ERR("No interface with device %s is configured", _iface->if_dev->dev->name);
Expand Down
20 changes: 13 additions & 7 deletions src/socket/cs_WebSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ LOG_MODULE_REGISTER(cs_WebSocket, LOG_LEVEL_INF);
#include <zephyr/net/websocket.h>

#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <limits.h>

Expand Down Expand Up @@ -175,7 +176,7 @@ cs_err_t WebSocket::init(cs_socket_opts *opts)
* @brief Open a websocket connection.
*
* @param url URL of the websocket, excluding the domain name or peer address and forward slash.
* NULL if not required.
* Max 64 characters, the rest is dropped. NULL if not required.
*
* @return CS_OK if connection is successful.
*/
Expand All @@ -187,7 +188,7 @@ cs_err_t WebSocket::connect(const char *url)
}

if (zsock_connect(_sock_id, _addr, _addr_len) < 0) {
LOG_ERR("Failed to connect to socket host: %d", -errno);
LOG_ERR("Failed to connect to socket host with errno: %d", -errno);
return CS_ERR_SOCKET_CONNECT_FAILED;
}

Expand All @@ -205,20 +206,25 @@ cs_err_t WebSocket::connect(const char *url)
return CS_ERR_SOCKET_WEBSOCKET_GET_IP_INFO_FAILED;
}

char url_buf[CS_WEBSOCKET_URL_MAX_LEN];
char url_prefix[] = "/";
strcat(url_buf, url_prefix);
// create url from forward slash + url if url provided
if (url != NULL) {
strncat(url_buf, url, (sizeof(url_buf) - sizeof(url_prefix)));
}

ws_req.host = addr;
ws_req.url = url == NULL ? url_prefix : strcat(url_prefix, url);
ws_req.url = url_buf;
ws_req.cb = handleWebsocketConnect;
ws_req.tmp_buf = _websocket_recv_temp_buf;
ws_req.tmp_buf_len = sizeof(_websocket_recv_temp_buf);

// perform http handshake for websocket connection

// perform http handshake for websocket connection, and open connection
_websock_id = websocket_connect(_sock_id, &ws_req, CS_WEBSOCKET_RECV_TIMEOUT, this);
if (_websock_id < 0) {
LOG_ERR("Failed to connect to websocket on %s", _host_name);
zsock_close(_sock_id);
LOG_ERR("Failed to connect to websocket on %s%s", addr, url_buf);
close();
return CS_ERR_SOCKET_WEBSOCKET_CONNECT_FAILED;
}

Expand Down

0 comments on commit 10a248f

Please sign in to comment.