Skip to content

Commit d69c9c3

Browse files
authored
Fix Static addresses not working after change to ESP_NETIF and network not running with VS debugger (#3133)
1 parent e8b977d commit d69c9c3

File tree

7 files changed

+274
-49
lines changed

7 files changed

+274
-49
lines changed

src/PAL/Lwip/lwIP_Sockets.cpp

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ extern "C"
2020
#include <lwip/apps/sntp.h>
2121
}
2222

23+
#if defined(PLATFORM_ESP32)
24+
#include "NF_ESP32_Network.h"
25+
#endif
26+
2327
int errorCode;
2428

2529
//--//
@@ -1288,6 +1292,7 @@ struct dhcp_client_id
12881292
uint8_t clientId[6];
12891293
};
12901294

1295+
#if !defined(PLATFORM_ESP32)
12911296
HRESULT LWIP_SOCKETS_Driver::UpdateAdapterConfiguration(
12921297
uint32_t interfaceIndex,
12931298
uint32_t updateFlags,
@@ -1389,6 +1394,135 @@ HRESULT LWIP_SOCKETS_Driver::UpdateAdapterConfiguration(
13891394

13901395
return S_OK;
13911396
}
1397+
#else
1398+
HRESULT LWIP_SOCKETS_Driver::UpdateAdapterConfiguration(
1399+
uint32_t interfaceIndex,
1400+
uint32_t updateFlags,
1401+
HAL_Configuration_NetworkInterface *config)
1402+
{
1403+
NATIVE_PROFILE_PAL_NETWORK();
1404+
bool enableDHCP = (config->StartupAddressMode == AddressMode_DHCP);
1405+
1406+
struct netif *networkInterface =
1407+
netif_find_interface(g_LWIP_SOCKETS_Driver.m_interfaces[interfaceIndex].m_interfaceNumber);
1408+
if (NULL == networkInterface)
1409+
{
1410+
return CLR_E_FAIL;
1411+
}
1412+
1413+
esp_netif_t *espNetif = NF_ESP32_GetEspNetif(networkInterface);
1414+
if (NULL == espNetif)
1415+
{
1416+
return CLR_E_FAIL;
1417+
}
1418+
1419+
#if LWIP_DNS
1420+
// when using DHCP do not use the static settings
1421+
if (0 != (updateFlags & NetworkInterface_UpdateOperation_Dns))
1422+
{
1423+
if (config->AutomaticDNS == 0)
1424+
{
1425+
// user defined DNS addresses
1426+
if (config->IPv4DNSAddress1 != 0)
1427+
{
1428+
esp_netif_dns_info_t dnsServer;
1429+
dnsServer.ip.u_addr.ip4.addr = config->IPv4DNSAddress1;
1430+
dnsServer.ip.type = IPADDR_TYPE_V4;
1431+
1432+
esp_netif_set_dns_info(espNetif, ESP_NETIF_DNS_MAIN, &dnsServer);
1433+
}
1434+
if (config->IPv4DNSAddress2 != 0)
1435+
{
1436+
// need to convert this first
1437+
esp_netif_dns_info_t dnsServer;
1438+
dnsServer.ip.u_addr.ip4.addr = config->IPv4DNSAddress2;
1439+
dnsServer.ip.type = IPADDR_TYPE_V4;
1440+
1441+
esp_netif_set_dns_info(espNetif, ESP_NETIF_DNS_FALLBACK, &dnsServer);
1442+
}
1443+
}
1444+
}
1445+
#endif
1446+
1447+
#if LWIP_DHCP
1448+
if (0 != (updateFlags & NetworkInterface_UpdateOperation_Dhcp))
1449+
{
1450+
if (enableDHCP)
1451+
{
1452+
// Make sure DHCP option is enabled for esp_netif
1453+
espNetif->flags = (esp_netif_flags_t)(espNetif->flags | ESP_NETIF_DHCP_CLIENT);
1454+
1455+
// Reset IP address on interface before enabling DHCP
1456+
ip_addr_t ipAddress, mask, gateway;
1457+
#if LWIP_IPV6
1458+
ipAddress.u_addr.ip4.addr = 0;
1459+
mask.u_addr.ip4.addr = 0;
1460+
gateway.u_addr.ip4.addr = 0;
1461+
#else
1462+
ipAddress.addr = 0;
1463+
mask.addr = 0;
1464+
gateway.addr = 0;
1465+
#endif
1466+
1467+
netif_set_addr(
1468+
networkInterface,
1469+
(const ip4_addr_t *)&ipAddress,
1470+
(const ip4_addr_t *)&mask,
1471+
(const ip4_addr_t *)&gateway);
1472+
1473+
// Need to start DHCP
1474+
// No need to check for return value, even if it fails, it will retry
1475+
esp_netif_dhcpc_start(espNetif);
1476+
}
1477+
else
1478+
{
1479+
// stop DHCP, ignore errors
1480+
esp_netif_dhcpc_stop(espNetif);
1481+
1482+
// Get static IPV4 address from config
1483+
esp_netif_ip_info_t ip_info;
1484+
ip_info.ip.addr = config->IPv4Address;
1485+
ip_info.gw.addr = config->IPv4GatewayAddress;
1486+
ip_info.netmask.addr = config->IPv4NetMask;
1487+
1488+
// set interface with our static IP configs, ignore error
1489+
esp_netif_set_ip_info(espNetif, &ip_info);
1490+
1491+
// Make sure DHCP client is disabled in esp_netif
1492+
espNetif->flags = (esp_netif_flags_t)(espNetif->flags & ~ESP_NETIF_DHCP_CLIENT);
1493+
1494+
// Inform DHCP server of change
1495+
dhcp_inform(networkInterface);
1496+
}
1497+
}
1498+
1499+
if (enableDHCP)
1500+
{
1501+
if (0 != (updateFlags & NetworkInterface_UpdateOperation_DhcpRelease))
1502+
{
1503+
esp_netif_dhcpc_stop(espNetif);
1504+
}
1505+
else if (0 != (updateFlags & NetworkInterface_UpdateOperation_DhcpRenew))
1506+
{
1507+
esp_netif_dhcpc_stop(espNetif);
1508+
esp_netif_dhcpc_start(espNetif);
1509+
}
1510+
}
1511+
#endif
1512+
1513+
if (0 != (updateFlags & NetworkInterface_UpdateOperation_Mac))
1514+
{
1515+
memcpy(networkInterface->hwaddr, config->MacAddress, NETIF_MAX_HWADDR_LEN);
1516+
networkInterface->hwaddr_len = NETIF_MAX_HWADDR_LEN;
1517+
1518+
// mac address requires stack re-init
1519+
Network_Interface_Close(interfaceIndex);
1520+
g_LWIP_SOCKETS_Driver.m_interfaces[interfaceIndex].m_interfaceNumber = Network_Interface_Open(interfaceIndex);
1521+
}
1522+
1523+
return S_OK;
1524+
}
1525+
#endif
13921526

13931527
int LWIP_SOCKETS_Driver::GetNativeTcpOption(int optname)
13941528
{

targets/ESP32/_Network/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/esp32_ethernet_options.h.in
1111
${CMAKE_BINARY_DIR}/targets/${RTOS}/${TARGET_BOARD}/esp32_ethernet_options.h @ONLY)
1212

1313
# append networking files, if enabled
14+
list(APPEND TARGET_ESP32_IDF_NETWORK_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/NF_ESP32_Network.cpp)
1415
list(APPEND TARGET_ESP32_IDF_NETWORK_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/NF_ESP32_Ethernet.cpp)
1516
list(APPEND TARGET_ESP32_IDF_NETWORK_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/NF_ESP32_Wireless.cpp)
1617
list(APPEND TARGET_ESP32_IDF_NETWORK_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/NF_ESP32_SmartConfig.cpp)

targets/ESP32/_Network/NF_ESP32_Ethernet.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ esp_err_t NF_ESP32_InitialiseEthernet(uint8_t *pMacAdr)
143143
buscfg.quadwp_io_num = -1;
144144
buscfg.quadhd_io_num = -1;
145145

146-
ESP_ERROR_CHECK(spi_bus_initialize(ESP32_ETHERNET_SPI_HOST, &buscfg, SPI_DMA_CH_AUTO));
146+
ESP_ERROR_CHECK_WITHOUT_ABORT(spi_bus_initialize(ESP32_ETHERNET_SPI_HOST, &buscfg, SPI_DMA_CH_AUTO));
147147

148148
#pragma
149149
// Define SPI interface to use
@@ -216,14 +216,14 @@ esp_err_t NF_ESP32_InitialiseEthernet(uint8_t *pMacAdr)
216216
#ifdef ESP32_ETHERNET_SPI
217217
// The SPI Ethernet module doesn't have a burned factory MAC address, we have to set it manually.
218218
// Supplied in the config
219-
ESP_ERROR_CHECK(esp_eth_ioctl(eth_handle, ETH_CMD_S_MAC_ADDR, pMacAdr));
219+
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_eth_ioctl(eth_handle, ETH_CMD_S_MAC_ADDR, pMacAdr));
220220
#endif
221221

222222
// attach Ethernet driver to TCP/IP stack
223-
ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handle)));
223+
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handle)));
224224

225225
// start Ethernet driver state machine
226-
ESP_ERROR_CHECK(esp_eth_start(eth_handle));
226+
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_eth_start(eth_handle));
227227

228228
#endif // ESP32_ETHERNET_SUPPORT
229229

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// See LICENSE file in the project root for full license information.
4+
//
5+
6+
// This file includes common method for networking code
7+
8+
#include "NF_ESP32_Network.h"
9+
#include "esp_netif_net_stack.h"
10+
#include "lwIP_Sockets.h"
11+
12+
// This function retrives the esp_netif ptr from the lwip structure for use else where
13+
// This is a copy of internal funtion of ESP_NETIF
14+
esp_netif_t *NF_ESP32_GetEspNetif(struct netif *netif)
15+
{
16+
#if LWIP_ESP_NETIF_DATA
17+
return (esp_netif *)netif_get_client_data(netif);
18+
#else
19+
return (esp_netif_t *)netif->state;
20+
#endif
21+
}
22+
23+
// Wait for the network interface to become available
24+
int NF_ESP32_Wait_NetNumber(int num)
25+
{
26+
int number = 0;
27+
int timeoutMs = 30000; // 30 seconds timeout
28+
int elapsedMs = 0;
29+
esp_netif_t *espNetif;
30+
31+
while (elapsedMs < timeoutMs)
32+
{
33+
switch (num)
34+
{
35+
case IDF_WIFI_STA_DEF:
36+
espNetif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
37+
break;
38+
39+
case IDF_WIFI_AP_DEF:
40+
espNetif = esp_netif_get_handle_from_ifkey("WIFI_AP_DEF");
41+
break;
42+
43+
case IDF_ETH_DEF:
44+
espNetif = esp_netif_get_handle_from_ifkey("ETH_DEF");
45+
break;
46+
47+
case IDF_OT_DEF:
48+
espNetif = esp_netif_get_handle_from_ifkey("OT_DEF");
49+
break;
50+
51+
default:
52+
// can't reach here
53+
HAL_AssertEx();
54+
break;
55+
}
56+
57+
if (espNetif != NULL)
58+
{
59+
break;
60+
}
61+
62+
const int delayMs = 20;
63+
vTaskDelay(delayMs / portTICK_PERIOD_MS);
64+
elapsedMs += delayMs;
65+
}
66+
67+
if (espNetif == NULL)
68+
{
69+
// Return error or default value
70+
return SOCK_SOCKET_ERROR;
71+
}
72+
73+
// Add a delay before picking up "lwip_netif->num" as not been filled in yet on slower processors
74+
vTaskDelay(50 / portTICK_PERIOD_MS);
75+
76+
return espNetif->lwip_netif->num;
77+
}
78+
79+
HAL_Configuration_NetworkInterface *NF_ESP32_GetNetworkConfigBlock(int index)
80+
{
81+
HAL_Configuration_NetworkInterface *networkConfig =
82+
(HAL_Configuration_NetworkInterface *)platform_malloc(sizeof(HAL_Configuration_NetworkInterface));
83+
84+
if (networkConfig != NULL)
85+
{
86+
if (ConfigurationManager_GetConfigurationBlock(networkConfig, DeviceConfigurationOption_Network, index))
87+
{
88+
return networkConfig;
89+
}
90+
platform_free(networkConfig);
91+
}
92+
93+
return NULL;
94+
}
95+
96+
esp_err_t NF_ESP32_ConfigureNetworkByConfigIndex(int index)
97+
{
98+
esp_err_t ec = ESP_OK;
99+
100+
HAL_Configuration_NetworkInterface *networkConfig = NF_ESP32_GetNetworkConfigBlock(index);
101+
if (networkConfig == NULL)
102+
{
103+
return ESP_FAIL;
104+
}
105+
106+
// Configure network interface
107+
LWIP_SOCKETS_Driver::UpdateAdapterConfiguration(
108+
index,
109+
NetworkInterface_UpdateOperation_Dns | NetworkInterface_UpdateOperation_Dhcp,
110+
networkConfig);
111+
112+
platform_free(networkConfig);
113+
114+
return ec;
115+
}

targets/ESP32/_Network/NF_ESP32_Wireless.cpp

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,14 @@ esp_err_t NF_ESP32_InitaliseWifi()
155155
// create Wi-Fi STA (ignoring return)
156156
wifiStaNetif = esp_netif_create_default_wifi_sta();
157157

158+
// Set static address if configured
159+
// ignore any errors
160+
ec = NF_ESP32_ConfigureNetworkByConfigIndex(IDF_WIFI_STA_DEF);
161+
if (ec != ESP_OK)
162+
{
163+
ESP_LOGE(TAG, "Unable to configure Wifi station - result %d", ec);
164+
}
165+
158166
// We need to start the WIFI stack before the station can Connect
159167
// Also we can only get the NetIf number used by ESP IDF after it has been started.
160168
// Starting will also start the Soft- AP (if we have enabled it).
@@ -514,47 +522,3 @@ bool NF_ESP32_WirelessAP_Close()
514522
}
515523

516524
#endif
517-
518-
// Wait for the network interface to become available
519-
int NF_ESP32_Wait_NetNumber(int num)
520-
{
521-
int number = 0;
522-
523-
esp_netif_t *espNetif;
524-
525-
while (true)
526-
{
527-
switch (num)
528-
{
529-
case IDF_WIFI_STA_DEF:
530-
espNetif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
531-
break;
532-
533-
case IDF_WIFI_AP_DEF:
534-
espNetif = esp_netif_get_handle_from_ifkey("WIFI_AP_DEF");
535-
break;
536-
537-
case IDF_ETH_DEF:
538-
espNetif = esp_netif_get_handle_from_ifkey("ETH_DEF");
539-
break;
540-
541-
case IDF_OT_DEF:
542-
espNetif = esp_netif_get_handle_from_ifkey("OT_DEF");
543-
break;
544-
545-
default:
546-
// can't reach here
547-
HAL_AssertEx();
548-
break;
549-
}
550-
551-
if (espNetif != NULL)
552-
{
553-
break;
554-
}
555-
556-
vTaskDelay(20 / portTICK_PERIOD_MS);
557-
}
558-
559-
return espNetif->lwip_netif->num;
560-
}

targets/ESP32/_common/targetHAL_Network.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,15 @@ static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_
373373
#ifdef PRINT_NET_EVENT
374374
ets_printf("ETHERNET_EVENT_CONNECTED\n");
375375
#endif
376+
// Make sure configuration is correct
377+
result = NF_ESP32_ConfigureNetworkByConfigIndex(IDF_ETH_DEF);
378+
if (result != ESP_OK)
379+
{
380+
#ifdef PRINT_NET_EVENT
381+
ets_printf("Failed to configure network for ethernet on connect: %d\n", err);
382+
#endif
383+
}
384+
376385
PostAvailabilityOn(IDF_ETH_DEF);
377386
break;
378387

targets/ESP32/_include/NF_ESP32_Network.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,7 @@ void NF_ESP32_Start_wifi_smart_config(void);
6060

6161
// Helpers
6262
int NF_ESP32_Wait_NetNumber(int num);
63-
63+
HAL_Configuration_NetworkInterface *NF_ESP32_GetNetworkConfigBlock(int index);
64+
esp_err_t NF_ESP32_ConfigureNetworkByConfigIndex(int index);
65+
esp_netif_t *NF_ESP32_GetEspNetif(struct netif *netif);
6466
#endif // NF_ESP32_NETWORK_H

0 commit comments

Comments
 (0)