From 8d1a2bccbb2f89706c02148a6dd8eb94a52a6620 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Thu, 11 Jan 2024 10:25:56 +0100 Subject: [PATCH] fix(eppp_link): Updated per iperf --- components/eppp_link/eppp_link.c | 54 +++++++- .../examples/rpc/client/main/app_main.c | 118 ++++++++++++++---- .../examples/rpc/client/main/client.cpp | 16 +++ .../examples/rpc/client/sdkconfig.defaults | 8 +- .../examples/rpc/common/esp_wifi_remote.h | 8 ++ .../eppp_link/examples/rpc/common/rpc.hpp | 3 +- .../examples/rpc/server/main/server.cpp | 19 ++- .../rpc/server/main/station_example_main.c | 36 +++++- .../examples/rpc/server/sdkconfig.defaults | 3 - components/eppp_link/include/eppp_link.h | 4 + 10 files changed, 234 insertions(+), 35 deletions(-) diff --git a/components/eppp_link/eppp_link.c b/components/eppp_link/eppp_link.c index 1a0813e5e3..13d10a275e 100644 --- a/components/eppp_link/eppp_link.c +++ b/components/eppp_link/eppp_link.c @@ -30,6 +30,7 @@ static EventGroupHandle_t s_event_group = NULL; static const char *TAG = "eppp_link"; static int s_retry_num = 0; static int s_eppp_netif_count = 0; // used as a suffix for the netif key +static eppp_channel_fn_t s_rx = NULL; struct eppp_handle { #if CONFIG_EPPP_LINK_DEVICE_SPI @@ -52,8 +53,28 @@ struct eppp_handle { struct packet { size_t len; uint8_t *data; + int channel; }; +static esp_err_t transmit_channel(void *netif, void *buffer, size_t len) +{ + struct eppp_handle *h = esp_netif_get_io_driver(netif); + struct packet buf = { .len = len }; + buf.channel = 1; + buf.data = malloc(len); + if (buf.data == NULL) { + ESP_LOGE(TAG, "Failed to allocate packet"); + return ESP_FAIL; + } + memcpy(buf.data, buffer, len); + BaseType_t ret = xQueueSend(h->out_queue, &buf, pdMS_TO_TICKS(10)); + if (ret != pdTRUE) { + ESP_LOGE(TAG, "Failed to queue packet to slave!"); + return ESP_FAIL; + } + return ESP_OK; +} + static esp_err_t transmit(void *h, void *buffer, size_t len) { struct eppp_handle *handle = h; @@ -230,6 +251,10 @@ static void on_ip_event(void *arg, esp_event_base_t base, int32_t event_id, void { ip_event_got_ip_t *event = (ip_event_got_ip_t *)data; esp_netif_t *netif = event->esp_netif; + if (event_id == IP_EVENT_STA_GOT_IP) { + ESP_LOGI(TAG, "Got IPv4 event: Interface \"%s(%s)\" address: " IPSTR, esp_netif_get_desc(netif), + esp_netif_get_ifkey(netif), IP2STR(&event->ip_info.ip)); + } int netif_cnt = get_netif_num(netif); if (netif_cnt < 0) { return; @@ -275,6 +300,7 @@ struct header { } __attribute__((packed)); }; uint8_t magic; + uint8_t channel; uint8_t checksum; } __attribute__((packed)); @@ -452,6 +478,13 @@ static esp_err_t perform_transaction_slave(union transaction *t, struct eppp_han return spi_slave_transmit(h->spi_host, &t->slave, portMAX_DELAY); } +esp_err_t eppp_add_channel(int nr, eppp_channel_fn_t *tx, const eppp_channel_fn_t rx) +{ + *tx = transmit_channel; + s_rx = rx; + return ESP_OK; +} + esp_err_t eppp_perform(esp_netif_t *netif) { static WORD_ALIGNED_ATTR uint8_t out_buf[TRANSFER_SIZE] = {}; @@ -482,8 +515,10 @@ esp_err_t eppp_perform(esp_netif_t *netif) head->magic = FRAME_OUT_CTRL_EX; head->size = 0; head->checksum = 0; + head->channel = 0; BaseType_t tx_queue_stat = xQueueReceive(h->out_queue, &buf, 0); if (tx_queue_stat == pdTRUE && buf.data) { + head->channel = buf.channel; if (buf.len > SHORT_PAYLOAD) { head->magic = FRAME_OUT_CTRL; head->size = buf.len; @@ -521,7 +556,14 @@ esp_err_t eppp_perform(esp_netif_t *netif) return ESP_FAIL; } if (head->magic == FRAME_IN_CTRL_EX && head->short_size > 0) { - esp_netif_receive(netif, in_buf + sizeof(struct header), head->short_size, NULL); + if (head->channel == 0) { + esp_netif_receive(netif, in_buf + sizeof(struct header), head->short_size, NULL); + } else { +// ESP_LOGE(TAG, "Got channel %d size %d", head->channel, head->short_size); + if (s_rx != NULL) { + s_rx(netif, in_buf + sizeof(struct header), head->short_size); + } + } } size_t in_long_payload = 0; if (head->magic == FRAME_IN_CTRL) { @@ -537,6 +579,7 @@ esp_err_t eppp_perform(esp_netif_t *netif) head->magic = FRAME_OUT_DATA; head->size = out_long_payload; head->checksum = 0; + head->channel = buf.channel; for (int i = 0; i < sizeof(struct header) - 1; ++i) { head->checksum += out_buf[i]; } @@ -570,7 +613,14 @@ esp_err_t eppp_perform(esp_netif_t *netif) if (head->size > 0) { ESP_LOG_BUFFER_HEXDUMP(TAG, in_buf + sizeof(struct header), head->size, ESP_LOG_VERBOSE); - esp_netif_receive(netif, in_buf + sizeof(struct header), head->size, NULL); + if (head->channel == 0) { + esp_netif_receive(netif, in_buf + sizeof(struct header), head->size, NULL); + } else { +// ESP_LOGE(TAG, "Got channel %d size %d", head->channel, head->size); + if (s_rx != NULL) { + s_rx(netif, in_buf + sizeof(struct header), head->size); + } + } } return ESP_OK; } diff --git a/components/eppp_link/examples/rpc/client/main/app_main.c b/components/eppp_link/examples/rpc/client/main/app_main.c index 83493f1fe6..5a5357c1db 100644 --- a/components/eppp_link/examples/rpc/client/main/app_main.c +++ b/components/eppp_link/examples/rpc/client/main/app_main.c @@ -130,6 +130,91 @@ static void test_on_ping_end(esp_ping_handle_t hdl, void *args) } #endif // PING +static esp_netif_t *s_wifi_netif; +static esp_netif_t *s_ppp_netif; +static eppp_channel_fn_t s_tx; + +static esp_err_t remote_wifi_receive(void *h, void *buffer, size_t len) +{ + if (s_wifi_netif) { +// printf("recv %d\n", len); + return esp_netif_receive(s_wifi_netif, buffer, len, NULL); + } + return ESP_OK; +} + +esp_err_t remote_wifi_transmit_wrap(void *h, void *buffer, size_t len, void *netstack_buffer) +{ + if (s_tx) { +// printf("send %d\n", len); + return s_tx(s_ppp_netif, buffer, len); + } + return ESP_OK; +} + +static esp_err_t remote_wifi_transmit(void *h, void *buffer, size_t len) +{ + if (s_tx) { + return s_tx(s_ppp_netif, buffer, len); + } + return ESP_OK; +} + +static void wifi_free(void *h, void *buffer) +{ +// printf("wifi_free %p\n", buffer); +} + +static void remote_wifi_netif(void) +{ + esp_netif_driver_ifconfig_t driver_cfg = { + .handle = (void *)1, + .transmit = remote_wifi_transmit, + .transmit_wrap = remote_wifi_transmit_wrap, + .driver_free_rx_buffer = wifi_free + + }; + const esp_netif_driver_ifconfig_t *wifi_driver_cfg = &driver_cfg; + esp_netif_config_t netif_config = { + .base = ESP_NETIF_BASE_DEFAULT_WIFI_STA, + .driver = wifi_driver_cfg, + .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP + }; + s_wifi_netif = esp_netif_new(&netif_config); +} + +static void wifi_init(void *ctx) +{ + client_init(); + + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_remote_init(&cfg)); + + wifi_config_t wifi_config = { + .sta = { + .ssid = CONFIG_ESP_WIFI_SSID, + .password = CONFIG_ESP_WIFI_PASSWORD, + }, + }; + + esp_err_t err = esp_wifi_remote_set_mode(WIFI_MODE_STA); + ESP_LOGI(TAG, "esp_wifi_remote_set_mode() returned = %x", err); + ESP_ERROR_CHECK(esp_wifi_remote_set_config(WIFI_IF_STA, &wifi_config) ); + ESP_ERROR_CHECK(esp_wifi_remote_start() ); + vTaskDelay(pdMS_TO_TICKS(1000)); + uint8_t mac[6]; + ESP_ERROR_CHECK(esp_wifi_remote_get_mac(WIFI_IF_STA, mac) ); + + esp_netif_set_mac(s_wifi_netif, mac); + vTaskDelay(pdMS_TO_TICKS(1000)); + + esp_netif_action_start(s_wifi_netif, 0, 0, 0); + ESP_ERROR_CHECK(esp_wifi_remote_connect() ); + + esp_netif_action_connected(s_wifi_netif, 0, 0, 0); + vTaskDelete(NULL); +} + void app_main(void) { ESP_LOGI(TAG, "[APP] Startup.."); @@ -145,39 +230,28 @@ void app_main(void) eppp_config_t config = EPPP_DEFAULT_CLIENT_CONFIG(); #if CONFIG_EPPP_LINK_DEVICE_SPI config.transport = EPPP_TRANSPORT_SPI; - config.task.priority = 5; + config.task.priority = 14; + config.spi.freq = 28000000; #else config.transport = EPPP_TRANSPORT_UART; config.uart.tx_io = 10; config.uart.rx_io = 11; config.uart.baud = 2000000; #endif - esp_netif_t *eppp_netif = eppp_connect(&config); - if (eppp_netif == NULL) { + s_ppp_netif = eppp_connect(&config); + if (s_ppp_netif == NULL) { ESP_LOGE(TAG, "Failed to connect"); return ; } + eppp_add_channel(1, &s_tx, remote_wifi_receive); + remote_wifi_netif(); + if (s_wifi_netif == NULL) { + ESP_LOGE(TAG, "Failed to create wifi netif"); + return ; + } - client_init(); - - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_LOG_BUFFER_HEXDUMP("cfg", &cfg, sizeof(cfg), ESP_LOG_WARN); - ESP_ERROR_CHECK(esp_wifi_remote_init(&cfg)); - - wifi_config_t wifi_config = { - .sta = { - .ssid = CONFIG_ESP_WIFI_SSID, - .password = CONFIG_ESP_WIFI_PASSWORD, - }, - }; - - esp_err_t err = esp_wifi_remote_set_mode(WIFI_MODE_STA); - ESP_LOGI(TAG, "esp_wifi_remote_set_mode() returned = %x", err); - ESP_ERROR_CHECK(esp_wifi_remote_set_config(WIFI_IF_STA, &wifi_config) ); - ESP_ERROR_CHECK(esp_wifi_remote_start() ); - vTaskDelay(pdMS_TO_TICKS(1000)); - ESP_ERROR_CHECK(esp_wifi_remote_connect() ); + xTaskCreate(&wifi_init, "initwifi", 8192, NULL, 18, NULL); // // Setup global DNS // esp_netif_dns_info_t dns; // dns.ip.u_addr.ip4.addr = esp_netif_htonl(CONFIG_EXAMPLE_GLOBAL_DNS); diff --git a/components/eppp_link/examples/rpc/client/main/client.cpp b/components/eppp_link/examples/rpc/client/main/client.cpp index feae29cfae..c06242a928 100644 --- a/components/eppp_link/examples/rpc/client/main/client.cpp +++ b/components/eppp_link/examples/rpc/client/main/client.cpp @@ -174,3 +174,19 @@ extern "C" esp_err_t esp_wifi_remote_connect(void) auto header = rpc.get_header(); return rpc.get_payload(CONNECT, header); } + +extern "C" esp_err_t esp_wifi_remote_get_mac(wifi_interface_t ifx, uint8_t mac[6]) +{ + RpcEngine rpc(tls); + + if (rpc.send(GET_MAC, &ifx) != ESP_OK) { + return ESP_FAIL; + } + auto header = rpc.get_header(); + auto ret = rpc.get_payload(GET_MAC, header); + ESP_LOG_BUFFER_HEXDUMP("MAC", ret.mac, 6, ESP_LOG_INFO); + + memcpy(mac, ret.mac, 6); + return ret.err; + +} diff --git a/components/eppp_link/examples/rpc/client/sdkconfig.defaults b/components/eppp_link/examples/rpc/client/sdkconfig.defaults index 2857f7ac8e..b4b1b3a8fc 100644 --- a/components/eppp_link/examples/rpc/client/sdkconfig.defaults +++ b/components/eppp_link/examples/rpc/client/sdkconfig.defaults @@ -1,8 +1,12 @@ # This file was generated using idf.py save-defconfig. It can be edited manually. # Espressif IoT Development Framework (ESP-IDF) 5.3.0 Project Minimal Configuration # -CONFIG_UART_ISR_IN_IRAM=y +CONFIG_IDF_TARGET="esp32s3" +CONFIG_ESP_TLS_INSECURE=y +CONFIG_ESP_TLS_SKIP_SERVER_CERT_VERIFY=y +CONFIG_ESP_SYSTEM_PANIC_PRINT_HALT=y CONFIG_LWIP_PPP_SUPPORT=y +CONFIG_LWIP_PPP_SERVER_SUPPORT=y CONFIG_LWIP_PPP_VJ_HEADER_COMPRESSION=n -CONFIG_LWIP_PPP_DEBUG_ON=y CONFIG_EPPP_LINK_DEVICE_SPI=y +CONFIG_LWIP_TCPIP_TASK_STACK_SIZE=4096 diff --git a/components/eppp_link/examples/rpc/common/esp_wifi_remote.h b/components/eppp_link/examples/rpc/common/esp_wifi_remote.h index e290d916d1..2c59d65aff 100644 --- a/components/eppp_link/examples/rpc/common/esp_wifi_remote.h +++ b/components/eppp_link/examples/rpc/common/esp_wifi_remote.h @@ -16,6 +16,12 @@ struct esp_wifi_remote_config { wifi_config_t conf; }; +struct esp_wifi_remote_mac_t { + esp_err_t err; + uint8_t mac[6]; +}; + + esp_err_t esp_wifi_remote_set_config(wifi_interface_t interface, wifi_config_t *conf); esp_err_t esp_wifi_remote_set_mode(wifi_mode_t mode); @@ -26,6 +32,8 @@ esp_err_t esp_wifi_remote_start(void); esp_err_t esp_wifi_remote_connect(void); +esp_err_t esp_wifi_remote_get_mac(wifi_interface_t ifx, uint8_t mac[6]); + #ifdef __cplusplus } #endif diff --git a/components/eppp_link/examples/rpc/common/rpc.hpp b/components/eppp_link/examples/rpc/common/rpc.hpp index dd0fb3ac30..b0d2f8160a 100644 --- a/components/eppp_link/examples/rpc/common/rpc.hpp +++ b/components/eppp_link/examples/rpc/common/rpc.hpp @@ -11,6 +11,7 @@ typedef enum api_id { SET_CONFIG, START, CONNECT, + GET_MAC } api_id_t; struct RpcHeader { @@ -50,7 +51,7 @@ class RpcEngine { size_t size; auto buf = req.marshall(t, size); ESP_LOGI("rpc", "Sending API id:%d", (int)id); - ESP_LOG_BUFFER_HEXDUMP("rpc", buf, size, ESP_LOG_INFO); + ESP_LOG_BUFFER_HEXDUMP("rpc", buf, size, ESP_LOG_VERBOSE); int len = esp_tls_conn_write(tls_, buf, size); if (len <= 0) { ESP_LOGE("rpc", "Failed to write data to the connection"); diff --git a/components/eppp_link/examples/rpc/server/main/server.cpp b/components/eppp_link/examples/rpc/server/main/server.cpp index 6a007428f0..f8fc61ea8e 100644 --- a/components/eppp_link/examples/rpc/server/main/server.cpp +++ b/components/eppp_link/examples/rpc/server/main/server.cpp @@ -119,9 +119,11 @@ static esp_err_t perform() } case INIT: { auto req = rpc.get_payload(INIT, header); - ESP_LOG_BUFFER_HEXDUMP("cfg", &req, sizeof(req), ESP_LOG_WARN); - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_LOG_BUFFER_HEXDUMP("cfg", &cfg, sizeof(cfg), ESP_LOG_WARN); + req.osi_funcs = &g_wifi_osi_funcs; + req.wpa_crypto_funcs = g_wifi_default_wpa_crypto_funcs; +// ESP_LOG_BUFFER_HEXDUMP("cfg", &req, sizeof(req), ESP_LOG_WARN); +// wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); +// ESP_LOG_BUFFER_HEXDUMP("cfg", &cfg, sizeof(cfg), ESP_LOG_WARN); auto ret = esp_wifi_init(&req); if (rpc.send(INIT, &ret) != ESP_OK) { @@ -141,6 +143,7 @@ static esp_err_t perform() if (header.size != 0) { return ESP_FAIL; } + auto ret = esp_wifi_start(); if (rpc.send(START, &ret) != ESP_OK) { return ESP_FAIL; @@ -151,12 +154,22 @@ static esp_err_t perform() if (header.size != 0) { return ESP_FAIL; } + auto ret = esp_wifi_connect(); if (rpc.send(CONNECT, &ret) != ESP_OK) { return ESP_FAIL; } break; } + case GET_MAC: { + auto req = rpc.get_payload(GET_MAC, header); + esp_wifi_remote_mac_t resp = {}; + resp.err = esp_wifi_get_mac(req, resp.mac); + if (rpc.send(GET_MAC, &resp) != ESP_OK) { + return ESP_FAIL; + } + break; + } } diff --git a/components/eppp_link/examples/rpc/server/main/station_example_main.c b/components/eppp_link/examples/rpc/server/main/station_example_main.c index bdba2283d9..310c02d30b 100644 --- a/components/eppp_link/examples/rpc/server/main/station_example_main.c +++ b/components/eppp_link/examples/rpc/server/main/station_example_main.c @@ -5,6 +5,7 @@ */ #include +#include #include "freertos/FreeRTOS.h" #include "freertos/event_groups.h" #include "esp_system.h" @@ -113,6 +114,29 @@ void wifi_init_sta(void) esp_err_t server_init(void); +static eppp_channel_fn_t s_tx; +static esp_netif_t *s_ppp_netif; + +static esp_err_t netif_recv(void *h, void *buffer, size_t len) +{ +// printf("recv %d\n", len); +// ESP_LOG_BUFFER_HEXDUMP("cfg", buffer, len, ESP_LOG_WARN); + return esp_wifi_internal_tx(WIFI_IF_STA, buffer, len); +} + +static esp_err_t wifi_recv(void *buffer, uint16_t len, void *eb) +{ +// printf("send %d\n", len); + if (s_tx) { +// printf("send %d\n", len); + esp_err_t ret = s_tx(s_ppp_netif, buffer, len); + esp_wifi_internal_free_rx_buffer(eb); + return ret; + } + return ESP_OK; +} + + void app_main(void) { @@ -129,11 +153,19 @@ void app_main(void) ESP_ERROR_CHECK(esp_event_loop_create_default()); eppp_config_t config = EPPP_DEFAULT_SERVER_CONFIG(); config.transport = EPPP_TRANSPORT_SPI; - esp_netif_t *eppp_netif = eppp_listen(&config); - if (eppp_netif == NULL) { + s_ppp_netif = eppp_listen(&config); + if (s_ppp_netif == NULL) { ESP_LOGE(TAG, "Failed to setup connection"); return ; } +// esp_wifi_internal_reg_rxcb(WIFI_IF_STA, wifi_recv); +// esp_wifi_internal_reg_netstack_buf_cb(esp_netif_netstack_buf_ref, esp_netif_netstack_buf_free); + eppp_add_channel(1, &s_tx, netif_recv); + server_init(); + vTaskDelay(pdMS_TO_TICKS(10000)); + esp_wifi_internal_reg_rxcb(WIFI_IF_STA, wifi_recv); + esp_wifi_internal_reg_netstack_buf_cb(esp_netif_netstack_buf_ref, esp_netif_netstack_buf_free); + } diff --git a/components/eppp_link/examples/rpc/server/sdkconfig.defaults b/components/eppp_link/examples/rpc/server/sdkconfig.defaults index ec1a1a43b0..2eb96884e3 100644 --- a/components/eppp_link/examples/rpc/server/sdkconfig.defaults +++ b/components/eppp_link/examples/rpc/server/sdkconfig.defaults @@ -1,6 +1,3 @@ -CONFIG_UART_ISR_IN_IRAM=y -CONFIG_LWIP_IP_FORWARD=y -CONFIG_LWIP_IPV4_NAPT=y CONFIG_LWIP_TCPIP_TASK_STACK_SIZE=4096 CONFIG_LWIP_PPP_SUPPORT=y CONFIG_LWIP_PPP_SERVER_SUPPORT=y diff --git a/components/eppp_link/include/eppp_link.h b/components/eppp_link/include/eppp_link.h index aba1f41c28..bb7f498992 100644 --- a/components/eppp_link/include/eppp_link.h +++ b/components/eppp_link/include/eppp_link.h @@ -103,3 +103,7 @@ esp_err_t eppp_netif_stop(esp_netif_t *netif, TickType_t stop_timeout); esp_err_t eppp_netif_start(esp_netif_t *netif); esp_err_t eppp_perform(esp_netif_t *netif); + +typedef esp_err_t (*eppp_channel_fn_t)(void *h, void *buffer, size_t len); + +esp_err_t eppp_add_channel(int nr, eppp_channel_fn_t *tx, const eppp_channel_fn_t rx);