tcp-uart
This commit is contained in:
parent
1cfcd9d879
commit
abbb99a008
|
@ -0,0 +1,168 @@
|
|||
#include <string.h>
|
||||
#include <sys/param.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
#include <freertos/stream_buffer.h>
|
||||
#include <esp_system.h>
|
||||
#include <esp_wifi.h>
|
||||
#include <esp_event.h>
|
||||
#include <esp_log.h>
|
||||
#include <nvs_flash.h>
|
||||
#include <esp_netif.h>
|
||||
|
||||
#include <lwip/err.h>
|
||||
#include <lwip/sockets.h>
|
||||
#include <lwip/sys.h>
|
||||
#include <lwip/netdb.h>
|
||||
|
||||
#include "led.h"
|
||||
#include "delay.h"
|
||||
#include "network-uart.h"
|
||||
|
||||
#define PORT 2346
|
||||
#define KEEPALIVE_IDLE 5
|
||||
#define KEEPALIVE_INTERVAL 5
|
||||
#define KEEPALIVE_COUNT 3
|
||||
#define TAG "network-uart"
|
||||
|
||||
typedef struct {
|
||||
bool connected;
|
||||
int socket_id;
|
||||
} NetworkUART;
|
||||
|
||||
static NetworkUART network_uart;
|
||||
|
||||
|
||||
void network_uart_send(uint8_t* buffer, size_t size) {
|
||||
int to_write = size;
|
||||
while(to_write > 0) {
|
||||
int written = send(network_uart.socket_id, buffer + (size - to_write), to_write, 0);
|
||||
to_write -= written;
|
||||
}
|
||||
};
|
||||
|
||||
void receive_and_send_to_uart(void) {
|
||||
size_t rx_size = SIZE_MAX;
|
||||
size_t uart_packet_size = uart_glue_get_packet_size();
|
||||
uint8_t* buffer_rx = malloc(uart_packet_size);
|
||||
|
||||
do {
|
||||
if(uart_glue_can_receive()) {
|
||||
size_t max_len = uart_glue_get_free_size();
|
||||
if(max_len > uart_packet_size) max_len = uart_packet_size;
|
||||
rx_size = recv(network_uart.socket_id, buffer_rx, max_len, 0);
|
||||
if(rx_size > 0) {
|
||||
uart_glue_receive(buffer_rx, rx_size);
|
||||
}
|
||||
} else {
|
||||
delay(10);
|
||||
}
|
||||
} while(rx_size > 0);
|
||||
|
||||
free(buffer_rx);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void network_uart_server_task(void* pvParameters) {
|
||||
char addr_str[128];
|
||||
int addr_family = (int)pvParameters;
|
||||
int ip_protocol = 0;
|
||||
int keepAlive = 1;
|
||||
int keepIdle = KEEPALIVE_IDLE;
|
||||
int keepInterval = KEEPALIVE_INTERVAL;
|
||||
int keepCount = KEEPALIVE_COUNT;
|
||||
network_uart.connected = false;
|
||||
struct sockaddr_storage dest_addr;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
int listen_sock = socket(addr_family, 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));
|
||||
|
||||
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);
|
||||
|
||||
err = listen(listen_sock, 1);
|
||||
if(err != 0) {
|
||||
ESP_LOGE(TAG, "Error occurred during listen: errno %d", errno);
|
||||
goto CLEAN_UP;
|
||||
}
|
||||
|
||||
while(1) {
|
||||
ESP_LOGI(TAG, "Socket listening");
|
||||
|
||||
struct sockaddr_storage source_addr; // Large enough for both IPv4 or IPv6
|
||||
socklen_t addr_len = sizeof(source_addr);
|
||||
int sock = accept(listen_sock, (struct sockaddr*)&source_addr, &addr_len);
|
||||
if(sock < 0) {
|
||||
ESP_LOGE(TAG, "Unable to accept connection: errno %d", errno);
|
||||
break;
|
||||
}
|
||||
|
||||
// Set tcp keepalive option
|
||||
setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &keepAlive, sizeof(int));
|
||||
setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &keepIdle, sizeof(int));
|
||||
setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &keepIdle, sizeof(int));
|
||||
setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &keepInterval, sizeof(int));
|
||||
setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, &keepCount, sizeof(int));
|
||||
|
||||
// Convert ip address to string
|
||||
if(source_addr.ss_family == PF_INET) {
|
||||
inet_ntoa_r(
|
||||
((struct sockaddr_in*)&source_addr)->sin_addr, addr_str, sizeof(addr_str) - 1);
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "Socket accepted ip address: %s", addr_str);
|
||||
|
||||
led_set_green(255);
|
||||
delay(10);
|
||||
led_set_green(0);
|
||||
|
||||
network_uart.socket_id = sock;
|
||||
network_uart.connected = true;
|
||||
|
||||
receive_and_send_to_uart();
|
||||
|
||||
network_uart.connected = false;
|
||||
network_uart.socket_id = -1;
|
||||
|
||||
led_set_blue(255);
|
||||
delay(10);
|
||||
led_set_blue(0);
|
||||
|
||||
shutdown(sock, 0);
|
||||
close(sock);
|
||||
}
|
||||
|
||||
CLEAN_UP:
|
||||
close(listen_sock);
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
void network_uart_server_init(void) {
|
||||
network_uart.connected = false;
|
||||
network_uart.socket_id = -1;
|
||||
|
||||
esp_wifi_set_ps(WIFI_PS_NONE);
|
||||
xTaskCreate(network_uzart_server_task, "network_uart_server", 4096, (void*)AF_INET, 5, NULL);
|
||||
}
|
|
@ -12,3 +12,16 @@
|
|||
* Start UART server
|
||||
*/
|
||||
void network_uart_server_init(void);
|
||||
|
||||
/**
|
||||
* Checks if someone is connected to the UART server
|
||||
* @return bool
|
||||
*/
|
||||
bool network_uart_connected(void);
|
||||
|
||||
/**
|
||||
* Send data
|
||||
* @param buffer data
|
||||
* @param size data size
|
||||
*/
|
||||
void network_uart_send(uint8_t* buffer, size_t size);
|
27
sdkconfig
27
sdkconfig
|
@ -226,6 +226,13 @@ CONFIG_ADC_DISABLE_DAC=y
|
|||
#
|
||||
CONFIG_UART_ISR_IN_IRAM=y
|
||||
# end of UART configuration
|
||||
|
||||
#
|
||||
# GDMA Configuration
|
||||
#
|
||||
# CONFIG_GDMA_CTRL_FUNC_IN_IRAM is not set
|
||||
# CONFIG_GDMA_ISR_IRAM_SAFE is not set
|
||||
# end of GDMA Configuration
|
||||
# end of Driver configurations
|
||||
|
||||
#
|
||||
|
@ -277,7 +284,6 @@ CONFIG_ESP32S2_TRACEMEM_RESERVE_DRAM=0x0
|
|||
# CONFIG_ESP32S2_ULP_COPROC_ENABLED is not set
|
||||
CONFIG_ESP32S2_ULP_COPROC_RESERVE_MEM=0
|
||||
CONFIG_ESP32S2_DEBUG_OCDAWARE=y
|
||||
# CONFIG_ESP32S2_DEBUG_STUBS_ENABLE is not set
|
||||
CONFIG_ESP32S2_BROWNOUT_DET=y
|
||||
CONFIG_ESP32S2_BROWNOUT_DET_LVL_SEL_7=y
|
||||
# CONFIG_ESP32S2_BROWNOUT_DET_LVL_SEL_6 is not set
|
||||
|
@ -390,6 +396,11 @@ CONFIG_ESP_SLEEP_RTC_BUS_ISO_WORKAROUND=y
|
|||
# CONFIG_ESP_SLEEP_GPIO_RESET_WORKAROUND is not set
|
||||
# CONFIG_ESP_SLEEP_FLASH_LEAKAGE_WORKAROUND is not set
|
||||
# end of Sleep Config
|
||||
|
||||
#
|
||||
# RTC Clock Config
|
||||
#
|
||||
# end of RTC Clock Config
|
||||
# end of Hardware Settings
|
||||
|
||||
#
|
||||
|
@ -425,6 +436,7 @@ CONFIG_ESP_PHY_CALIBRATION_AND_DATA_STORAGE=y
|
|||
# CONFIG_ESP_PHY_INIT_DATA_IN_PARTITION is not set
|
||||
CONFIG_ESP_PHY_MAX_WIFI_TX_POWER=20
|
||||
CONFIG_ESP_PHY_MAX_TX_POWER=20
|
||||
# CONFIG_ESP_PHY_ENABLE_USB is not set
|
||||
# end of PHY
|
||||
|
||||
#
|
||||
|
@ -471,6 +483,7 @@ CONFIG_ESP_CONSOLE_UART_NUM=-1
|
|||
# CONFIG_ESP_INT_WDT is not set
|
||||
# CONFIG_ESP_TASK_WDT is not set
|
||||
# CONFIG_ESP_PANIC_HANDLER_IRAM is not set
|
||||
# CONFIG_ESP_DEBUG_STUBS_ENABLE is not set
|
||||
CONFIG_ESP_SYSTEM_CHECK_INT_LEVEL_4=y
|
||||
# end of ESP System Settings
|
||||
|
||||
|
@ -510,6 +523,7 @@ CONFIG_ESP32_WIFI_ENABLE_WPA3_SAE=y
|
|||
# CONFIG_ESP_WIFI_STA_DISCONNECTED_PM_ENABLE is not set
|
||||
# CONFIG_ESP_WIFI_EXTERNAL_COEXIST_ENABLE is not set
|
||||
# CONFIG_ESP_WIFI_GMAC_SUPPORT is not set
|
||||
CONFIG_ESP_WIFI_SOFTAP_SUPPORT=y
|
||||
# end of Wi-Fi
|
||||
|
||||
#
|
||||
|
@ -837,6 +851,15 @@ CONFIG_MBEDTLS_SSL_OUT_CONTENT_LEN=4096
|
|||
# CONFIG_MBEDTLS_DYNAMIC_BUFFER is not set
|
||||
# CONFIG_MBEDTLS_DEBUG is not set
|
||||
|
||||
#
|
||||
# mbedTLS v2.28.x related
|
||||
#
|
||||
# CONFIG_MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH is not set
|
||||
# CONFIG_MBEDTLS_X509_TRUSTED_CERT_CALLBACK is not set
|
||||
# CONFIG_MBEDTLS_SSL_CONTEXT_SERIALIZATION is not set
|
||||
CONFIG_MBEDTLS_SSL_KEEP_PEER_CERTIFICATE=y
|
||||
# end of mbedTLS v2.28.x related
|
||||
|
||||
#
|
||||
# Certificate Bundle
|
||||
#
|
||||
|
@ -1043,6 +1066,7 @@ CONFIG_SPI_FLASH_SUPPORT_MXIC_CHIP=y
|
|||
CONFIG_SPI_FLASH_SUPPORT_GD_CHIP=y
|
||||
CONFIG_SPI_FLASH_SUPPORT_WINBOND_CHIP=y
|
||||
CONFIG_SPI_FLASH_SUPPORT_BOYA_CHIP=y
|
||||
CONFIG_SPI_FLASH_SUPPORT_TH_CHIP=y
|
||||
# end of Auto-detect flash chips
|
||||
|
||||
CONFIG_SPI_FLASH_ENABLE_ENCRYPTED_READ_WRITE=y
|
||||
|
@ -1233,6 +1257,7 @@ CONFIG_ESP_CONSOLE_UART_NONE=y
|
|||
CONFIG_CONSOLE_UART_NUM=-1
|
||||
# CONFIG_INT_WDT is not set
|
||||
# CONFIG_TASK_WDT is not set
|
||||
# CONFIG_ESP32_DEBUG_STUBS_ENABLE is not set
|
||||
CONFIG_TIMER_TASK_STACK_SIZE=3584
|
||||
# CONFIG_EXTERNAL_COEX_ENABLE is not set
|
||||
# CONFIG_ESP32_ENABLE_COREDUMP_TO_FLASH is not set
|
||||
|
|
Loading…
Reference in New Issue