Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
olegv142 committed Mar 21, 2024
1 parent d2f721f commit 1442faf
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 59 deletions.
2 changes: 1 addition & 1 deletion src/components/ethernet_init/ethernet_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "driver/spi_master.h"
#endif // CONFIG_ETH_USE_SPI_ETHERNET

static const char *TAG = "example_eth_init";
static const char *TAG = "bridge_eth_init";

#if CONFIG_EXAMPLE_SPI_ETHERNETS_NUM
#define SPI_ETHERNETS_NUM CONFIG_EXAMPLE_SPI_ETHERNETS_NUM
Expand Down
73 changes: 63 additions & 10 deletions src/main/Kconfig.projbuild
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@ menu "Eth-UART Bridge Configuration"
help
GPIO number (IOxx) for 50MHz oscillator enable.

config EXAMPLE_IPV4
bool "IPV4"
default y
depends on LWIP_IPV4

config EXAMPLE_IPV6
bool "IPV6"
default n
select EXAMPLE_CONNECT_IPV6
config BRIDGE_PORT
int "Echo port"
range 0 65535
default 3142
help
Local port the UART bridge will listen on.

config EXAMPLE_PORT
config ECHO_PORT
int "Echo port"
range 0 65535
default 3333
Expand All @@ -46,4 +43,60 @@ menu "Eth-UART Bridge Configuration"
help
Keep-alive probe packet retry count.

config UART_TX_GPIO
int "UART TX GPIO number"
range 0 34
default 2
help
GPIO number (IOxx) for serial data TX output.

config UART_RX_GPIO
int "UART RX GPIO number"
range 0 34
default 4
help
GPIO number (IOxx) for serial data RX input.

config UART_RTS_GPIO
int "UART RTS GPIO number"
range 0 34
default 12
help
GPIO number (IOxx) for serial data RTS output. Low level enables data reception from RX line.

config UART_CTS_GPIO
depends on UART_CTS_EN
int "UART CTS GPIO number"
range 0 34
default 14
help
GPIO number (IOxx) for serial data CTS input. Low level enables data transmission to TX line.

config UART_CTS_EN
bool "UART CTS enable"
default n
help
Enable using CTS input. Low level on this pin enables data transmission to TX line.

config UART_BITRATE
int "UART baud rate"
range 9600 1843200
default 921600
help
UART data transfer rate in bits per second.

config UART_TX_BUFF_SIZE
int "UART transmit buffer size (KB)"
range 0 64
default 17
help
UART transmit data buffer size in kilobytes.

config UART_RX_BUFF_SIZE
int "UART receive buffer size (KB)"
range 1 64
default 17
help
UART receive data buffer size in kilobytes.

endmenu
2 changes: 1 addition & 1 deletion src/main/ethernet_example_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "sdkconfig.h"
#include "tcp_server.h"

static const char *TAG = "eth_example";
static const char *TAG = "bridge";

/** Event handler for Ethernet events */
static void eth_event_handler(void *arg, esp_event_base_t event_base,
Expand Down
66 changes: 22 additions & 44 deletions src/main/tcp_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,21 @@
#include <lwip/netdb.h>


#define PORT CONFIG_EXAMPLE_PORT
#define ECHO_PORT CONFIG_ECHO_PORT
#define KEEPALIVE_IDLE CONFIG_EXAMPLE_KEEPALIVE_IDLE
#define KEEPALIVE_INTERVAL CONFIG_EXAMPLE_KEEPALIVE_INTERVAL
#define KEEPALIVE_COUNT CONFIG_EXAMPLE_KEEPALIVE_COUNT

static const char *TAG = "example";
static const char *TAG = "bridge_eth";

static void do_retransmit(const int sock)
typedef void (*sock_handler_t)(int);

struct server_port {
uint16_t port;
sock_handler_t handler;
};

static void do_echo(int sock)
{
int len;
static char rx_buffer[2048];
Expand Down Expand Up @@ -61,56 +68,37 @@ static void do_retransmit(const int sock)
static void tcp_server_task(void *pvParameters)
{
char addr_str[128];
int addr_family = (int)pvParameters;
struct server_port const* srv = pvParameters;
int ip_protocol = 0;
int keepAlive = 1;
int keepIdle = KEEPALIVE_IDLE;
int keepInterval = KEEPALIVE_INTERVAL;
int keepCount = KEEPALIVE_COUNT;
struct sockaddr_storage dest_addr;

#ifdef CONFIG_EXAMPLE_IPV4
if (addr_family == AF_INET) {
struct sockaddr_in *dest_addr_ip4 = (struct sockaddr_in *)&dest_addr;
dest_addr_ip4->sin_addr.s_addr = htonl(INADDR_ANY);
dest_addr_ip4->sin_family = AF_INET;
dest_addr_ip4->sin_port = htons(PORT);
ip_protocol = IPPROTO_IP;
}
#endif
#ifdef CONFIG_EXAMPLE_IPV6
if (addr_family == AF_INET6) {
struct sockaddr_in6 *dest_addr_ip6 = (struct sockaddr_in6 *)&dest_addr;
bzero(&dest_addr_ip6->sin6_addr.un, sizeof(dest_addr_ip6->sin6_addr.un));
dest_addr_ip6->sin6_family = AF_INET6;
dest_addr_ip6->sin6_port = htons(PORT);
ip_protocol = IPPROTO_IPV6;
}
#endif
struct sockaddr_in *dest_addr_ip4 = (struct sockaddr_in *)&dest_addr;
dest_addr_ip4->sin_addr.s_addr = htonl(INADDR_ANY);
dest_addr_ip4->sin_family = AF_INET;
dest_addr_ip4->sin_port = htons(srv->port);
ip_protocol = IPPROTO_IP;

int listen_sock = socket(addr_family, SOCK_STREAM, ip_protocol);
int listen_sock = socket(AF_INET, SOCK_STREAM, ip_protocol);
if (listen_sock < 0) {
ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
vTaskDelete(NULL);
return;
}
int opt = 1;
setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
#if defined(CONFIG_EXAMPLE_IPV4) && defined(CONFIG_EXAMPLE_IPV6)
// Note that by default IPV6 binds to both protocols, it is must be disabled
// if both protocols used at the same time (used in CI)
setsockopt(listen_sock, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof(opt));
#endif

ESP_LOGI(TAG, "Socket created");

int err = bind(listen_sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
if (err != 0) {
ESP_LOGE(TAG, "Socket unable to bind: errno %d", errno);
ESP_LOGE(TAG, "IPPROTO: %d", addr_family);
goto CLEAN_UP;
}
ESP_LOGI(TAG, "Socket bound, port %d", PORT);
ESP_LOGI(TAG, "Socket bound, port %d", srv->port);

err = listen(listen_sock, 1);
if (err != 0) {
Expand All @@ -136,19 +124,12 @@ static void tcp_server_task(void *pvParameters)
setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &keepInterval, sizeof(int));
setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, &keepCount, sizeof(int));
// Convert ip address to string
#ifdef CONFIG_EXAMPLE_IPV4
if (source_addr.ss_family == PF_INET) {
inet_ntoa_r(((struct sockaddr_in *)&source_addr)->sin_addr, addr_str, sizeof(addr_str) - 1);
}
#endif
#ifdef CONFIG_EXAMPLE_IPV6
if (source_addr.ss_family == PF_INET6) {
inet6_ntoa_r(((struct sockaddr_in6 *)&source_addr)->sin6_addr, addr_str, sizeof(addr_str) - 1);
}
#endif
ESP_LOGI(TAG, "Socket accepted ip address: %s", addr_str);

do_retransmit(sock);
(srv->handler)(sock);

shutdown(sock, 0);
close(sock);
Expand All @@ -159,12 +140,9 @@ static void tcp_server_task(void *pvParameters)
vTaskDelete(NULL);
}

static struct server_port echo_server = {ECHO_PORT, do_echo};

void tcp_server_create(void)
{
#ifdef CONFIG_EXAMPLE_IPV4
xTaskCreate(tcp_server_task, "tcp_server", 4096, (void*)AF_INET, 5, NULL);
#endif
#ifdef CONFIG_EXAMPLE_IPV6
xTaskCreate(tcp_server_task, "tcp_server", 4096, (void*)AF_INET6, 5, NULL);
#endif
xTaskCreate(tcp_server_task, "tcp_server", 4096, (void*)&echo_server, 5, NULL);
}
12 changes: 9 additions & 3 deletions src/sdkconfig
Original file line number Diff line number Diff line change
Expand Up @@ -375,12 +375,18 @@ CONFIG_PARTITION_TABLE_MD5=y
#
CONFIG_HAS_CLK_EN_PIN=y
CONFIG_CLK_EN_GPIO=16
CONFIG_EXAMPLE_IPV4=y
# CONFIG_EXAMPLE_IPV6 is not set
CONFIG_EXAMPLE_PORT=3333
CONFIG_BRIDGE_PORT=3142
CONFIG_ECHO_PORT=3333
CONFIG_EXAMPLE_KEEPALIVE_IDLE=5
CONFIG_EXAMPLE_KEEPALIVE_INTERVAL=5
CONFIG_EXAMPLE_KEEPALIVE_COUNT=3
CONFIG_UART_TX_GPIO=2
CONFIG_UART_RX_GPIO=4
CONFIG_UART_RTS_GPIO=12
# CONFIG_UART_CTS_EN is not set
CONFIG_UART_BITRATE=921600
CONFIG_UART_TX_BUFF_SIZE=17
CONFIG_UART_RX_BUFF_SIZE=17
# end of Eth-UART Bridge Configuration

#
Expand Down

0 comments on commit 1442faf

Please sign in to comment.