Skip to content

Commit d150ff7

Browse files
committed
Set IP addresses and tidy
1 parent 6492d91 commit d150ff7

File tree

5 files changed

+138
-44
lines changed

5 files changed

+138
-44
lines changed

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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ esp_err_t NF_ESP32_InitialiseEthernet(uint8_t *pMacAdr)
219219
// attach Ethernet driver to TCP/IP stack
220220
ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handle)));
221221

222+
// COnfigure static address if required in config
223+
ESP_ERROR_CHECK(NF_ESP32_ConfigureNetworkByIndex(IDF_ETH_DEF, eth_netif));
224+
222225
// start Ethernet driver state machine
223226
ESP_ERROR_CHECK(esp_eth_start(eth_handle));
224227

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
11+
// Wait for the network interface to become available
12+
int NF_ESP32_Wait_NetNumber(int num)
13+
{
14+
int number = 0;
15+
16+
esp_netif_t *espNetif;
17+
18+
while (true)
19+
{
20+
switch (num)
21+
{
22+
case IDF_WIFI_STA_DEF:
23+
espNetif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
24+
break;
25+
26+
case IDF_WIFI_AP_DEF:
27+
espNetif = esp_netif_get_handle_from_ifkey("WIFI_AP_DEF");
28+
break;
29+
30+
case IDF_ETH_DEF:
31+
espNetif = esp_netif_get_handle_from_ifkey("ETH_DEF");
32+
break;
33+
34+
case IDF_OT_DEF:
35+
espNetif = esp_netif_get_handle_from_ifkey("OT_DEF");
36+
break;
37+
38+
default:
39+
// can't reach here
40+
HAL_AssertEx();
41+
break;
42+
}
43+
44+
if (espNetif != NULL)
45+
{
46+
break;
47+
}
48+
49+
vTaskDelay(20 / portTICK_PERIOD_MS);
50+
}
51+
52+
return espNetif->lwip_netif->num;
53+
}
54+
55+
HAL_Configuration_NetworkInterface * NF_ESP32_GetNetworkConfigBlock(int index)
56+
{
57+
HAL_Configuration_NetworkInterface *networkConfig =
58+
(HAL_Configuration_NetworkInterface *)platform_malloc(sizeof(HAL_Configuration_NetworkInterface));
59+
60+
if (networkConfig != NULL)
61+
{
62+
if (ConfigurationManager_GetConfigurationBlock(networkConfig, DeviceConfigurationOption_Network, index))
63+
{
64+
return networkConfig;
65+
}
66+
}
67+
68+
return NULL;
69+
}
70+
71+
//
72+
// Configure network settings for a espressif network interface
73+
//
74+
esp_err_t NF_ESP32_ConfigureNetwork(esp_netif_t *netIf, HAL_Configuration_NetworkInterface *config)
75+
{
76+
esp_err_t ec;
77+
esp_netif_ip_info_t ip_info;
78+
79+
ec = esp_netif_get_ip_info(netIf, &ip_info);
80+
if (ec != ESP_OK)
81+
{
82+
return ec;
83+
}
84+
85+
bool enableDHCP = (config->StartupAddressMode == AddressMode_DHCP);
86+
87+
// Set static addresses
88+
if (config->IPv4Address != 0)
89+
{
90+
ip_info.ip.addr = config->IPv4Address;
91+
ip_info.netmask.addr = config->IPv4NetMask;
92+
ip_info.gw.addr = config->IPv4GatewayAddress;
93+
94+
ec = esp_netif_set_ip_info(netIf, &ip_info);
95+
96+
// Make sure DHCP client is disabled
97+
netIf->flags = (esp_netif_flags_t)(netIf->flags & ~ESP_NETIF_DHCP_CLIENT);
98+
}
99+
100+
return ec;
101+
}
102+
103+
esp_err_t NF_ESP32_ConfigureNetworkByIndex(int index, esp_netif_t *netIf)
104+
{
105+
esp_err_t ec;
106+
107+
HAL_Configuration_NetworkInterface *networkConfig = NF_ESP32_GetNetworkConfigBlock(index);
108+
if (networkConfig == NULL)
109+
{
110+
return ESP_FAIL;
111+
}
112+
113+
ec = NF_ESP32_ConfigureNetwork(netIf, networkConfig);
114+
115+
platform_free(networkConfig);
116+
117+
return ec;
118+
}

targets/ESP32/_Network/NF_ESP32_Wireless.cpp

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ wifi_mode_t NF_ESP32_GetCurrentWifiMode()
112112
return current_wifi_mode;
113113
}
114114

115+
esp_err_t NF_ESP32_ConfigureNetworkStation(esp_netif_t *netIf)
116+
{
117+
return NF_ESP32_ConfigureNetworkByIndex(IDF_WIFI_STA_DEF, netIf);
118+
}
119+
115120
void NF_ESP32_DeinitWifi()
116121
{
117122
// clear flags
@@ -155,6 +160,14 @@ esp_err_t NF_ESP32_InitaliseWifi()
155160
// create Wi-Fi STA (ignoring return)
156161
wifiStaNetif = esp_netif_create_default_wifi_sta();
157162

163+
// Set static address if configured
164+
// ignore any errors
165+
ec = NF_ESP32_ConfigureNetworkStation(wifiStaNetif);
166+
if (ec != ESP_OK)
167+
{
168+
ESP_LOGE(TAG, "Unable to configure Wifi station - result %d", ec);
169+
}
170+
158171
// We need to start the WIFI stack before the station can Connect
159172
// Also we can only get the NetIf number used by ESP IDF after it has been started.
160173
// Starting will also start the Soft- AP (if we have enabled it).
@@ -514,47 +527,3 @@ bool NF_ESP32_WirelessAP_Close()
514527
}
515528

516529
#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/_include/NF_ESP32_Network.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,8 @@ void NF_ESP32_Start_wifi_smart_config(void);
6060

6161
// Helpers
6262
int NF_ESP32_Wait_NetNumber(int num);
63+
HAL_Configuration_NetworkInterface * NF_ESP32_GetNetworkConfigBlock(int index);
64+
esp_err_t NF_ESP32_ConfigureNetwork(esp_netif_t *netIf, HAL_Configuration_NetworkInterface *config);
65+
esp_err_t NF_ESP32_ConfigureNetworkByIndex(int index, esp_netif_t *netIf);
6366

6467
#endif // NF_ESP32_NETWORK_H

0 commit comments

Comments
 (0)