mirror of
https://github.com/espressif/esp-protocols.git
synced 2025-08-01 03:34:40 +02:00
esp-netif: SLIP interface refactor to isolate interface from drivers
* Original commit: espressif/esp-idf@1a41545c3e
This commit is contained in:
@@ -2,11 +2,5 @@
|
||||
# in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
set(EXTRA_COMPONENT_DIRS
|
||||
../components/slip_modem/
|
||||
)
|
||||
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(slip_client)
|
||||
|
||||
|
@@ -4,7 +4,7 @@
|
||||
|
||||
## Overview
|
||||
|
||||
This provides SLIP support for connection to Contiki gateway devices, allowing the ESP32 to be used to bridge between low-power networks and IP (Wifi / Ethernet).
|
||||
This provides SLIP support for connection to Contiki gateway devices, allowing the ESP platform board to be used to bridge between low-power networks and IP (Wifi / Ethernet).
|
||||
|
||||
## How to use example
|
||||
|
||||
@@ -12,7 +12,33 @@ This provides SLIP support for connection to Contiki gateway devices, allowing t
|
||||
|
||||
To run this example, you need an ESP32 dev board (e.g. ESP32-WROVER Kit) or ESP32 core board (e.g. ESP32-DevKitC).
|
||||
For test purpose, you also need a SLIP capable gateway device, such as anything running [Contiki](https://github.com/contiki-os/contiki) gateway firmware.
|
||||
You can also try other modules as long as they implement the SLIP protocol.
|
||||
You can also try other modules as long as they implement the SLIP protocol (e.g. linux device with slip module loaded)
|
||||
|
||||
#### Setup a test SLIP device
|
||||
|
||||
It is possible to configure any device with linux and a serial interface
|
||||
(e.g. raspberry PI or a PC with USB to serial bridge) to enable SLIP interface.
|
||||
|
||||
To test this example with such device, please follow these steps:
|
||||
|
||||
- Configure IPv4 mode in the example configuration menu
|
||||
|
||||
- Setup SLIP interface
|
||||
```
|
||||
slattach -v -L -s 115200 -p slip /dev/ttyAMA0
|
||||
```
|
||||
where the `/dev/ttyAMA0` is the device's serial port
|
||||
|
||||
- Configure IP addresses
|
||||
```
|
||||
ifconfig sl0 10.0.0.1 dstaddr 10.0.0.2
|
||||
```
|
||||
where the `10.0.0.2` is IPv4 address of the ESP platform board
|
||||
|
||||
- Send and receive back UDP packets, as the example implements UDP echo server
|
||||
```
|
||||
nc -u 10.0.0.2 5678
|
||||
```
|
||||
|
||||
#### Pin Assignment
|
||||
|
||||
@@ -30,7 +56,7 @@ You can also try other modules as long as they implement the SLIP protocol.
|
||||
Open the project configuration menu (`idf.py menuconfig`). Then go into `Example Configuration` menu.
|
||||
|
||||
- Choose the RX and TX pins
|
||||
|
||||
- Choose port number and IP protocol for socket udp server
|
||||
For use in external projects `SLIP support` must be enabled under the `components/lwip` menu.
|
||||
|
||||
|
||||
|
@@ -0,0 +1,8 @@
|
||||
# SLIP Modem Component
|
||||
|
||||
idf_component_register(
|
||||
SRCS "library/slip_modem.c"
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES esp_netif
|
||||
)
|
||||
|
@@ -0,0 +1,3 @@
|
||||
COMPONENT_ADD_INCLUDEDIRS := include
|
||||
|
||||
COMPONENT_SRCDIRS := library
|
@@ -0,0 +1,68 @@
|
||||
// Copyright 2020 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "esp_netif.h"
|
||||
#include "esp_netif_slip.h"
|
||||
|
||||
#include "driver/uart.h"
|
||||
|
||||
// Forward declare modem object
|
||||
typedef struct esp_slip_modem esp_slip_modem_t;
|
||||
|
||||
// Filter callbacks for handling application specific slip messages
|
||||
typedef bool slip_rx_filter_cb_t(void *ctx, uint8_t *data, uint32_t len);
|
||||
|
||||
|
||||
/** @brief Configuration structure for SLIP modem interface
|
||||
*
|
||||
*/
|
||||
typedef struct {
|
||||
uart_port_t uart_dev; /* UART device for reading and writing SLIP information, this must be initialised externally */
|
||||
|
||||
int uart_tx_pin; /* UART TX pin number */
|
||||
int uart_rx_pin; /* UART TX pin number */
|
||||
|
||||
uint32_t uart_baud; /* UART baud rate */
|
||||
|
||||
uint32_t rx_buffer_len; /* Length of buffer for RX messages */
|
||||
|
||||
slip_rx_filter_cb_t *rx_filter; /* Filter for parsing out non-SLIP messages from incoming SLIP stream */
|
||||
void *rx_filter_ctx; /* Context to be passed to SLIP filter function */
|
||||
|
||||
} esp_slip_modem_config_t;
|
||||
|
||||
|
||||
/** @brief Create a slip modem
|
||||
*
|
||||
* @param[in] slip configured esp netif
|
||||
* @param[in] configuration for the slip modem
|
||||
*
|
||||
* @returns
|
||||
* - slip modem driver glue object
|
||||
*/
|
||||
void *esp_slip_modem_create(esp_netif_t *slip_netif, esp_slip_modem_config_t *modem_config);
|
||||
|
||||
/** @brief Destroy a slip modem
|
||||
*
|
||||
* @param[in] slip modem object for destruction
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK on success
|
||||
*/
|
||||
esp_err_t esp_slip_modem_destroy(esp_slip_modem_t *slip_modem);
|
@@ -0,0 +1,272 @@
|
||||
// Copyright 2020 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "slip_modem.h"
|
||||
|
||||
#include "esp_netif.h"
|
||||
#include "esp_netif_slip.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#define SLIP_RX_TASK_PRIORITY 10
|
||||
#define SLIP_RX_TASK_STACK_SIZE (4 * 1024)
|
||||
|
||||
static const char *TAG = "esp-slip_modem";
|
||||
|
||||
|
||||
// UART container object
|
||||
typedef struct {
|
||||
// UART device number for SIO use
|
||||
uart_port_t uart_dev;
|
||||
|
||||
// UART baud rate for configuration
|
||||
uint32_t uart_baud;
|
||||
|
||||
// UART TX pin for configuration
|
||||
int uart_tx_pin;
|
||||
|
||||
// UART RX pin for configuration
|
||||
int uart_rx_pin;
|
||||
|
||||
// QueueHandle for uart driver
|
||||
QueueHandle_t uart_queue;
|
||||
|
||||
// TaskHandle for receive task
|
||||
TaskHandle_t uart_rx_task;
|
||||
} esp_slip_uart_t;
|
||||
|
||||
|
||||
// Modem object, implements glue logic for slip_driver and esp_netif
|
||||
struct esp_slip_modem {
|
||||
// ESP base netif driver
|
||||
esp_netif_driver_base_t base;
|
||||
|
||||
// Uart for use with slip
|
||||
esp_slip_uart_t uart;
|
||||
|
||||
// Buffer for incoming messages
|
||||
uint8_t *buffer;
|
||||
uint32_t buffer_len;
|
||||
|
||||
// Filter callbacks for application-specific slip message handling
|
||||
slip_rx_filter_cb_t *rx_filter;
|
||||
void *rx_filter_ctx;
|
||||
|
||||
// Running flag
|
||||
bool running;
|
||||
};
|
||||
|
||||
|
||||
// Forward function declaration
|
||||
static void esp_slip_modem_uart_rx_task(void *arg);
|
||||
static esp_err_t esp_slip_modem_post_attach(esp_netif_t *esp_netif, void *args);
|
||||
|
||||
// Create a new slip netif
|
||||
void *esp_slip_modem_create(esp_netif_t *slip_netif, esp_slip_modem_config_t *modem_config)
|
||||
{
|
||||
ESP_LOGI(TAG, "%s: Creating slip modem (netif: %p)", __func__, slip_netif);
|
||||
|
||||
ESP_LOGD(TAG, "%s (netif: %p)", __func__, slip_netif);
|
||||
|
||||
esp_slip_modem_t *slip_modem = calloc(1, sizeof(esp_slip_modem_t));
|
||||
if (!slip_modem) {
|
||||
ESP_LOGE(TAG, "create netif glue failed");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Attach driver and post_attach callbacks
|
||||
slip_modem->base.post_attach = esp_slip_modem_post_attach;
|
||||
|
||||
// Attach config
|
||||
slip_modem->buffer_len = modem_config->rx_buffer_len;
|
||||
|
||||
slip_modem->rx_filter = modem_config->rx_filter;
|
||||
slip_modem->rx_filter_ctx = modem_config->rx_filter_ctx;
|
||||
|
||||
slip_modem->uart.uart_dev = modem_config->uart_dev;
|
||||
slip_modem->uart.uart_baud = modem_config->uart_baud;
|
||||
slip_modem->uart.uart_rx_pin = modem_config->uart_rx_pin;
|
||||
slip_modem->uart.uart_tx_pin = modem_config->uart_tx_pin;
|
||||
|
||||
// Return new modem, with a cast to the first item
|
||||
return &slip_modem->base;
|
||||
}
|
||||
|
||||
// Internal handler called on driver start
|
||||
static esp_err_t esp_slip_driver_start(esp_slip_modem_t *slip_modem)
|
||||
{
|
||||
ESP_LOGD(TAG, "%s: Starting SLIP modem (modem %p)", __func__, slip_modem);
|
||||
|
||||
// Allocate RX buffer if one does not exist
|
||||
if (slip_modem->buffer == NULL) {
|
||||
slip_modem->buffer = malloc(slip_modem->buffer_len);
|
||||
}
|
||||
if (slip_modem->buffer == NULL) {
|
||||
ESP_LOGE(TAG, "error allocating rx buffer");
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
|
||||
// Build configuration
|
||||
uart_config_t uart_config = {
|
||||
.baud_rate = slip_modem->uart.uart_baud,
|
||||
.data_bits = UART_DATA_8_BITS,
|
||||
.parity = UART_PARITY_DISABLE,
|
||||
.stop_bits = UART_STOP_BITS_1,
|
||||
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
|
||||
};
|
||||
|
||||
// Initialise uart
|
||||
ESP_ERROR_CHECK(uart_param_config(slip_modem->uart.uart_dev, &uart_config));
|
||||
|
||||
// Set UART pins
|
||||
ESP_ERROR_CHECK(uart_set_pin(slip_modem->uart.uart_dev, slip_modem->uart.uart_tx_pin, slip_modem->uart.uart_rx_pin, 0, 0));
|
||||
|
||||
// Install UART driver
|
||||
ESP_ERROR_CHECK(uart_driver_install(slip_modem->uart.uart_dev, slip_modem->buffer_len, slip_modem->buffer_len, 10, &slip_modem->uart.uart_queue, 0));
|
||||
|
||||
// Start slip RX task
|
||||
slip_modem->running = true;
|
||||
xTaskCreate(esp_slip_modem_uart_rx_task, "slip_modem_uart_rx_task", SLIP_RX_TASK_STACK_SIZE, slip_modem, SLIP_RX_TASK_PRIORITY, &slip_modem->uart.uart_rx_task);
|
||||
|
||||
// Finally, initialise slip network interface
|
||||
esp_netif_action_start(slip_modem->base.netif, 0, 0, 0);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
|
||||
esp_err_t esp_slip_modem_destroy(esp_slip_modem_t *slip_modem)
|
||||
{
|
||||
// Stop slip driver
|
||||
esp_netif_action_stop(slip_modem->base.netif, 0, 0, 0);
|
||||
|
||||
// Stop uart rx task
|
||||
vTaskDelete(slip_modem->uart.uart_rx_task);
|
||||
|
||||
// Delete driver
|
||||
uart_driver_delete(slip_modem->uart.uart_dev);
|
||||
|
||||
// Free slip interface
|
||||
free(slip_modem);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
// Modem transmit for glue logic
|
||||
static esp_err_t esp_slip_modem_transmit(void *slip_driver, void *buffer, size_t len)
|
||||
{
|
||||
ESP_LOGD(TAG, "%s", __func__);
|
||||
ESP_LOG_BUFFER_HEXDUMP(TAG, buffer, len, ESP_LOG_DEBUG);
|
||||
esp_slip_modem_t *slip_modem = (esp_slip_modem_t *) slip_driver;
|
||||
|
||||
int32_t res = uart_write_bytes(slip_modem->uart.uart_dev, (char *)buffer, len);
|
||||
if (res < 0) {
|
||||
// Handle errors
|
||||
ESP_LOGE(TAG, "%s: uart_write_bytes error %i", __func__, res);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
// Post-attach handler for netif
|
||||
static esp_err_t esp_slip_modem_post_attach(esp_netif_t *esp_netif, void *args)
|
||||
{
|
||||
esp_slip_modem_t *slip_modem = (esp_slip_modem_t *) args;
|
||||
|
||||
ESP_LOGD(TAG, "%s (netif: %p args: %p)", __func__, esp_netif, args);
|
||||
|
||||
const esp_netif_driver_ifconfig_t driver_ifconfig = {
|
||||
.driver_free_rx_buffer = NULL,
|
||||
.transmit = esp_slip_modem_transmit,
|
||||
.handle = slip_modem,
|
||||
};
|
||||
|
||||
slip_modem->base.netif = esp_netif;
|
||||
ESP_ERROR_CHECK(esp_netif_set_driver_config(esp_netif, &driver_ifconfig));
|
||||
|
||||
esp_slip_driver_start(slip_modem);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t esp_slip_modem_set_default_handlers(esp_netif_t *esp_netif) {
|
||||
esp_err_t ret;
|
||||
|
||||
if (esp_netif == NULL) {
|
||||
ESP_LOGE(TAG, "esp-netif handle can't be null");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
ret = esp_event_handler_register(SLIP_EVENT, SLIP_EVENT_START, esp_netif_action_start, esp_netif);
|
||||
if (ret != ESP_OK) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
ret = esp_event_handler_register(SLIP_EVENT, SLIP_EVENT_STOP, esp_netif_action_stop, esp_netif);
|
||||
if (ret != ESP_OK) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
fail:
|
||||
esp_eth_clear_default_handlers(esp_netif);
|
||||
return ret;
|
||||
}
|
||||
|
||||
esp_err_t esp_slip_modem_clear_default_handlers(void *esp_netif)
|
||||
{
|
||||
if (!esp_netif) {
|
||||
ESP_LOGE(TAG, "esp-netif handle can't be null");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
esp_event_handler_unregister(SLIP_EVENT, SLIP_EVENT_START, esp_netif_action_start);
|
||||
esp_event_handler_unregister(SLIP_EVENT, SLIP_EVENT_STOP, esp_netif_action_stop);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
|
||||
static void esp_slip_modem_uart_rx_task(void *arg)
|
||||
{
|
||||
esp_slip_modem_t *slip_modem = (esp_slip_modem_t *) arg;
|
||||
|
||||
ESP_LOGD(TAG, "Start SLIP modem RX task (slip_modem %p filter: %p)", slip_modem, slip_modem->rx_filter);
|
||||
ESP_LOGD(TAG, "Uart: %d, buffer: %p (%d bytes)", slip_modem->uart.uart_dev, slip_modem->buffer, slip_modem->buffer_len);
|
||||
|
||||
while (slip_modem->running == true) {
|
||||
// Read data from the UART
|
||||
int len = uart_read_bytes(slip_modem->uart.uart_dev, slip_modem->buffer, slip_modem->buffer_len, 1 / portTICK_RATE_MS);
|
||||
|
||||
if (len > 0) {
|
||||
|
||||
// Log slip RX data
|
||||
ESP_LOGD(TAG, "rx %d bytes", len);
|
||||
ESP_LOG_BUFFER_HEX_LEVEL(TAG, slip_modem->buffer, len, ESP_LOG_DEBUG);
|
||||
|
||||
// Ensure null termination
|
||||
slip_modem->buffer[len] = '\0';
|
||||
|
||||
// Filter if provided
|
||||
if ((slip_modem->rx_filter != NULL) && slip_modem->rx_filter(slip_modem->rx_filter_ctx, slip_modem->buffer, len)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Pass received bytes in to slip interface
|
||||
esp_netif_receive(slip_modem->base.netif, slip_modem->buffer, len, NULL);
|
||||
}
|
||||
|
||||
// Yeild to allow other tasks to progress
|
||||
vTaskDelay(1 * portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
@@ -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
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user