Подключены ESP32|Linux, обновлен интерфейс

This commit is contained in:
2026-09-11 20:53:38 +03:00
parent 9fce687690
commit e7a01aaeda
23 changed files with 477 additions and 647 deletions
File diff suppressed because it is too large Load Diff
+75
View File
@@ -0,0 +1,75 @@
"""Prepare the known-working lwIP component layout for ESP-IDF v6.0.2."""
import filecmp
from pathlib import Path
import shutil
import sys
def replace_once(text, old, new, label):
if text.count(old) != 1:
raise RuntimeError(f"Unexpected {label}; expected ESP-IDF v6.0.2 sources")
return text.replace(old, new, 1)
def write_changed(path, data):
path.parent.mkdir(parents=True, exist_ok=True)
if not path.exists() or path.read_bytes() != data:
path.write_bytes(data)
def prepare(source, destination, arp):
source, destination, arp = source.resolve(), destination.resolve(), arp.resolve()
if not arp.is_file():
raise RuntimeError(f"Missing modified ARP source: {arp}")
if source == destination or source in destination.parents or destination in source.parents:
raise RuntimeError("Source and generated directories must be separate")
if any(c in arp.as_posix() for c in ('"', ';', '$')):
raise RuntimeError("ARP path contains characters that require CMake escaping")
cmake = (source / "CMakeLists.txt").read_text()
cmake = replace_once(cmake, '"lwip/src/core/ipv4/etharp.c"',
f'"{arp.as_posix()}"', "lwIP CMakeLists.txt")
cmake = replace_once(cmake,
'if(NOT ${target} STREQUAL "linux")\n set(priv_requires vfs)\nendif()',
'set(priv_requires)\n\nif(NOT ${target} STREQUAL "linux")\n list(APPEND priv_requires vfs)\nendif()',
"lwIP dependencies")
sockets_path = "lwip/src/include/lwip/sockets.h"
sockets = (source / sockets_path).read_text()
if "#ifndef SIOCATMARK" not in sockets:
line = "#define SIOCATMARK _IOR('s', 7, unsigned long) /* at oob mark? */"
sockets = replace_once(sockets, line, "#endif\n#ifndef SIOCATMARK\n" + line,
"SIOCATMARK definition")
fcntl_path = "port/linux/include/sys/fcntl.h"
fcntl = (source / fcntl_path).read_text()
if "#define F_GETFD" not in fcntl:
fcntl = fcntl.rstrip("\n") + "\n\n#ifndef F_GETFD\n#define F_GETFD 1\n#endif\n"
changed = {"CMakeLists.txt": cmake.encode(), sockets_path: sockets.encode(),
fcntl_path: fcntl.encode()}
# Validate all substitutions before writing. Do not copy or compile stock ARP.
destination.mkdir(parents=True, exist_ok=True)
for path in source.rglob("*"):
relative = path.relative_to(source)
if ".git" in relative.parts or not path.is_file():
continue
name = relative.as_posix()
if name in changed or name == "lwip/src/core/ipv4/etharp.c":
continue
target = destination / relative
target.parent.mkdir(parents=True, exist_ok=True)
if not target.is_file() or not filecmp.cmp(path, target, shallow=False):
shutil.copy2(path, target)
for name, data in changed.items():
write_changed(destination / name, data)
stock_arp = destination / "lwip/src/core/ipv4/etharp.c"
if stock_arp.exists():
stock_arp.unlink()
if __name__ == "__main__":
try:
if len(sys.argv) != 4:
raise RuntimeError("Usage: prepare.py SDK_LWIP BUILD_LWIP PATCHED_ETHARP")
prepare(*(Path(value) for value in sys.argv[1:]))
except (OSError, RuntimeError) as error:
sys.exit(str(error))
+104
View File
@@ -0,0 +1,104 @@
/*
* SPDX-FileCopyrightText: 2001-2003 Swedish Institute of Computer Science
*
* SPDX-License-Identifier: BSD-3-Clause
*
* SPDX-FileContributor: 2022-2023 Espressif Systems (Shanghai) CO LTD
*/
#include <string.h>
#include "lwip/opt.h"
#include "lwip/pbuf.h"
#include "lwip/snmp.h"
#include "lwip/ethip6.h"
#include "netif/etharp.h"
#include "esp_netif.h"
#include "esp_netif_net_stack.h"
#include "tapif.h"
#define IFNAME0 't'
#define IFNAME1 'p'
static err_t
low_level_output(struct netif *netif, struct pbuf *p)
{
esp_netif_t *esp_netif = esp_netif_get_handle_from_netif_impl(netif);
char buf[1518]; /* max packet size including VLAN excluding CRC */
if (p->tot_len > sizeof(buf)) {
MIB2_STATS_NETIF_INC(netif, ifoutdiscards);
LWIP_DEBUGF(NETIF_DEBUG, ("tapif: packet too large"));
return ERR_IF;
}
/* initiate transfer(); */
pbuf_copy_partial(p, buf, p->tot_len, 0);
int ret = esp_netif_transmit(esp_netif, buf, p->tot_len);
/* Check error */
if (likely(ret == ESP_OK)) {
return ERR_OK;
}
if (ret == ESP_ERR_NO_MEM) {
return ERR_MEM;
}
return ERR_IF;
}
static void
low_level_init(struct netif *netif)
{
/* Obtain MAC address from network interface. */
netif->hwaddr[0] = 0x02;
netif->hwaddr[1] = 0x12;
netif->hwaddr[2] = 0x34;
netif->hwaddr[3] = 0x56;
netif->hwaddr[4] = 0x78;
netif->hwaddr[5] = 0xab;
netif->hwaddr_len = 6;
/* device capabilities */
netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP;
}
err_t lwip_tapif_init(struct netif *netif)
{
LWIP_ASSERT("Tried to initialize tapif with NULL netif", netif != NULL);
netif->name[0] = IFNAME0;
netif->name[1] = IFNAME1;
#if LWIP_IPV4
netif->output = etharp_output;
#endif /* LWIP_IPV4 */
#if LWIP_IPV6
netif->output_ip6 = ethip6_output;
#endif /* LWIP_IPV6 */
netif->linkoutput = low_level_output;
netif->mtu = 1500;
low_level_init(netif);
netif_set_link_up(netif);
return ERR_OK;
}
esp_err_t lwip_tapif_input(void *h, void *buffer, size_t len, void *l2_buff)
{
struct netif *netif = h;
struct pbuf *p;
LWIP_ASSERT("running tapif input with NULL netif", netif != NULL);
p = pbuf_alloc(PBUF_RAW, len, PBUF_RAM);
if (p == NULL) {
return ESP_ERR_NO_MEM;
}
memcpy(p->payload, buffer, len);
/* full packet send to tcpip_thread to process */
if (unlikely(netif->input(p, netif) != ERR_OK)) {
LWIP_DEBUGF(NETIF_DEBUG, ("tapif_input: IP input error\n"));
pbuf_free(p);
return ESP_FAIL;
}
return ESP_OK;
}
@@ -0,0 +1,26 @@
/*
* SPDX-FileCopyrightText: 2001-2003 Swedish Institute of Computer Science
*
* SPDX-License-Identifier: BSD-3-Clause
*
* SPDX-FileContributor: 2022-2023 Espressif Systems (Shanghai) CO LTD
*/
#pragma once
#include "esp_err.h"
#include "lwip/esp_netif_net_stack.h"
/**
* @brief lwip netif init API
* @param netif pointer to lwip's netif
* @return ERR_OK on success
*/
err_t lwip_tapif_init(struct netif *netif);
/**
* @brief Input data path
* @param h pointer to network stack handle (stuct netif* in our case)
* @param buffer Data
* @param len Data size
* @param l2_buff Data L2 buffer
*/
esp_err_t lwip_tapif_input(void *h, void *buffer, size_t len, void *l2_buff);