esp-netif: SLIP interface refactor to isolate interface from drivers

* Original commit: espressif/esp-idf@1a41545c3e
This commit is contained in:
David Cermak
2020-06-12 16:43:34 +02:00
parent 061885ad23
commit 3bf488eb86
8 changed files with 134 additions and 105 deletions

View File

@@ -21,12 +21,18 @@ menu "Example Configuration"
help
Baud rate for UART communication
config EXAMPLE_UDP_PORT
int "Port for UDP echo server"
default 5678
help
Port for UDP echo server in example
endmenu
config EXAMPLE_UDP_PORT
int "Port for UDP echo server"
default 5678
help
Port for UDP echo server in example
config EXAMPLE_IPV4
bool "Test with IPv4 address"
default n
help
Test interface using IPv4
endmenu

View File

@@ -9,8 +9,6 @@
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_log.h"
@@ -19,8 +17,6 @@
#include "esp_netif_slip.h"
#include "lwip/sockets.h"
#include "lwip/dns.h"
#include "lwip/netdb.h"
#include "slip_modem.h"
@@ -29,14 +25,12 @@ static const char *TAG = "SLIP_EXAMPLE";
#define STACK_SIZE (10 * 1024)
#define PRIORITY 10
TaskHandle_t udp_rx_tx_handle;
static void udp_rx_tx_task(void *arg)
{
char addr_str[128];
uint8_t rx_buff[1024];
int sock = *(int *)arg;
int sock = (int)arg;
struct sockaddr_in6 source_addr;
socklen_t socklen = sizeof(source_addr);
@@ -53,7 +47,11 @@ static void udp_rx_tx_task(void *arg)
}
// Parse out address to string
inet6_ntoa_r(source_addr.sin6_addr, addr_str, sizeof(addr_str) - 1);
if (source_addr.sin6_family == PF_INET) {
inet_ntoa_r(((struct sockaddr_in *)&source_addr)->sin_addr.s_addr, addr_str, sizeof(addr_str) - 1);
} else if (source_addr.sin6_family == PF_INET6) {
inet6_ntoa_r(source_addr.sin6_addr, addr_str, sizeof(addr_str) - 1);
}
// Force null termination of received data and print
rx_buff[len] = 0;
@@ -70,37 +68,51 @@ static void udp_rx_tx_task(void *arg)
vTaskDelete(NULL);
}
esp_err_t udp_rx_tx_init()
esp_err_t udp_rx_tx_init(void)
{
// Setup bind address
struct sockaddr_in6 dest_addr;
#if CONFIG_EXAMPLE_IPV4
sa_family_t family = AF_INET;
int ip_protocol = IPPROTO_IP;
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(CONFIG_EXAMPLE_UDP_PORT);
ip_protocol = IPPROTO_IP;
#else
sa_family_t family = AF_INET6;
int ip_protocol = IPPROTO_IPV6;
bzero(&dest_addr.sin6_addr.un, sizeof(dest_addr.sin6_addr.un));
dest_addr.sin6_family = AF_INET6;
dest_addr.sin6_family = family;
dest_addr.sin6_port = htons(CONFIG_EXAMPLE_UDP_PORT);
#endif
// Create socket
int sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_IPV6);
int sock = socket(family, SOCK_DGRAM, ip_protocol);
if (sock < 0) {
ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
return -1;
return ESP_FAIL;
}
// Disable IPv4 and reuse address
int opt = 1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
#if !CONFIG_EXAMPLE_IPV4
setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof(opt));
#endif
// Bind socket
int err = bind(sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
if (err < 0) {
ESP_LOGE(TAG, "Socket unable to bind: errno %d", errno);
return -2;
return ESP_FAIL;
}
ESP_LOGI(TAG, "Socket bound, port %d", CONFIG_EXAMPLE_UDP_PORT);
// Start UDP rx thread
xTaskCreate(udp_rx_tx_task, "udp_rx_tx", STACK_SIZE, &sock, PRIORITY, &udp_rx_tx_handle);
xTaskCreate(udp_rx_tx_task, "udp_rx_tx", STACK_SIZE, (void *)sock, PRIORITY, NULL);
return ESP_OK;
}
@@ -111,7 +123,7 @@ static void slip_set_prefix(esp_netif_t *slip_netif)
uint8_t buff[10] = {0};
// Fetch the slip interface IP
const ip6_addr_t *addr = esp_slip_get_ip6(slip_netif);
const esp_ip6_addr_t *addr = esp_slip_get_ip6(slip_netif);
ESP_LOGI(TAG, "%s: prefix set (%08x:%08x)", __func__,
lwip_ntohl(addr->addr[0]), lwip_ntohl(addr->addr[1]));
@@ -129,7 +141,7 @@ static void slip_set_prefix(esp_netif_t *slip_netif)
esp_netif_lwip_slip_raw_output(slip_netif, buff, 2 + 8);
}
// slip_rx_filter filters incomming commands from the slip interface
// slip_rx_filter filters incoming commands from the slip interface
// this implementation is designed for use with contiki slip devices
bool slip_rx_filter(void *ctx, uint8_t *data, uint32_t len)
{
@@ -162,23 +174,34 @@ bool slip_rx_filter(void *ctx, uint8_t *data, uint32_t len)
return false;
}
#if CONFIG_EXAMPLE_IPV4
static const esp_netif_ip_info_t s_slip_ip4 = {
.ip = { .addr = ESP_IP4TOADDR( 10, 0, 0, 2) },
};
#endif
// Initialise the SLIP interface
esp_netif_t *slip_if_init()
esp_netif_t *slip_if_init(void)
{
ESP_LOGI(TAG, "Initialising SLIP interface");
esp_netif_config_t cfg = ESP_NETIF_DEFAULT_SLIP();
esp_netif_inherent_config_t base_cfg = ESP_NETIF_INHERENT_DEFAULT_SLIP()
#if CONFIG_EXAMPLE_IPV4
base_cfg.ip_info = &s_slip_ip4;
#endif
esp_netif_config_t cfg = { .base = &base_cfg,
.driver = NULL,
.stack = ESP_NETIF_NETSTACK_DEFAULT_SLIP };
esp_netif_t *slip_netif = esp_netif_new(&cfg);
esp_netif_slip_config_t slip_config = {
.uart_dev = UART_NUM_2,
};
esp_netif_slip_config_t slip_config;
IP6_ADDR(&slip_config.addr,
IP6_ADDR(&slip_config.ip6_addr,
lwip_htonl(0xfd000000),
lwip_htonl(0x00000000),
lwip_htonl(0x00000000),
lwip_htonl(0x000000001)
lwip_htonl(0x00000001)
);
esp_netif_slip_set_params(slip_netif, &slip_config);
@@ -186,7 +209,7 @@ esp_netif_t *slip_if_init()
ESP_LOGI(TAG, "Initialising SLIP modem");
esp_slip_modem_config_t modem_cfg = {
.uart_dev = UART_NUM_2,
.uart_dev = UART_NUM_1,
.uart_tx_pin = CONFIG_EXAMPLE_UART_TX_PIN,
.uart_rx_pin = CONFIG_EXAMPLE_UART_RX_PIN,
@@ -209,7 +232,7 @@ esp_netif_t *slip_if_init()
void app_main(void)
{
// Setup networking
tcpip_adapter_init();
esp_netif_init();
esp_log_level_set("*", ESP_LOG_DEBUG);
@@ -221,9 +244,4 @@ void app_main(void)
// Setup UDP loopback service
udp_rx_tx_init();
// Run
while (1) {
vTaskDelay(portTICK_PERIOD_MS * 10);
}
}