-
-
Notifications
You must be signed in to change notification settings - Fork 180
Update IP addresses in ESP_NETIF to stop static addresses being overwritten #3133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AdrianSoundy
wants to merge
6
commits into
nanoframework:main
Choose a base branch
from
AdrianSoundy:Static_ip
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d150ff7
Set IP addresses and tidy
AdrianSoundy 0533231
Code style fixes
nfbot 4a069e5
Merge remote-tracking branch 'upstream/nfbot/clang-format-fix/703469f…
AdrianSoundy 7705105
Update code changes suggested by coderabbitai
AdrianSoundy 1a1aea9
Merge branch 'main' into Static_ip
AdrianSoundy 46867d5
Fix comment typo
AdrianSoundy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// | ||
// Copyright (c) .NET Foundation and Contributors | ||
// See LICENSE file in the project root for full license information. | ||
// | ||
|
||
// This file includes common method for networking code | ||
|
||
#include "NF_ESP32_Network.h" | ||
#include "esp_netif_net_stack.h" | ||
|
||
// Wait for the network interface to become available | ||
int NF_ESP32_Wait_NetNumber(int num) | ||
{ | ||
int number = 0; | ||
int timeoutMs = 30000; // 30 seconds timeout | ||
int elapsedMs = 0; | ||
esp_netif_t *espNetif; | ||
|
||
while (elapsedMs < timeoutMs) | ||
{ | ||
switch (num) | ||
{ | ||
case IDF_WIFI_STA_DEF: | ||
espNetif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"); | ||
break; | ||
|
||
case IDF_WIFI_AP_DEF: | ||
espNetif = esp_netif_get_handle_from_ifkey("WIFI_AP_DEF"); | ||
break; | ||
|
||
case IDF_ETH_DEF: | ||
espNetif = esp_netif_get_handle_from_ifkey("ETH_DEF"); | ||
break; | ||
|
||
case IDF_OT_DEF: | ||
espNetif = esp_netif_get_handle_from_ifkey("OT_DEF"); | ||
break; | ||
|
||
default: | ||
// can't reach here | ||
HAL_AssertEx(); | ||
break; | ||
} | ||
|
||
if (espNetif != NULL) | ||
{ | ||
break; | ||
} | ||
|
||
const int delayMs = 20; | ||
vTaskDelay(delayMs / portTICK_PERIOD_MS); | ||
elapsedMs += delayMs; | ||
} | ||
|
||
if (espNetif == NULL) | ||
{ | ||
// Return error or default value | ||
return SOCK_SOCKET_ERROR; | ||
} | ||
|
||
return espNetif->lwip_netif->num; | ||
} | ||
|
||
HAL_Configuration_NetworkInterface *NF_ESP32_GetNetworkConfigBlock(int index) | ||
{ | ||
HAL_Configuration_NetworkInterface *networkConfig = | ||
(HAL_Configuration_NetworkInterface *)platform_malloc(sizeof(HAL_Configuration_NetworkInterface)); | ||
|
||
if (networkConfig != NULL) | ||
{ | ||
if (ConfigurationManager_GetConfigurationBlock(networkConfig, DeviceConfigurationOption_Network, index)) | ||
{ | ||
return networkConfig; | ||
} | ||
platform_free(networkConfig); | ||
} | ||
|
||
return NULL; | ||
} | ||
|
||
// | ||
// Configure network settings for a espressif network interface | ||
// | ||
esp_err_t NF_ESP32_ConfigureNetwork(esp_netif_t *netIf, HAL_Configuration_NetworkInterface *config) | ||
{ | ||
esp_err_t ec; | ||
esp_netif_ip_info_t ip_info; | ||
|
||
ec = esp_netif_get_ip_info(netIf, &ip_info); | ||
if (ec != ESP_OK) | ||
{ | ||
return ec; | ||
} | ||
|
||
bool enableDHCP = (config->StartupAddressMode == AddressMode_DHCP); | ||
|
||
// Set static addresses | ||
if (config->IPv4Address != 0) | ||
{ | ||
ip_info.ip.addr = config->IPv4Address; | ||
ip_info.netmask.addr = config->IPv4NetMask; | ||
ip_info.gw.addr = config->IPv4GatewayAddress; | ||
|
||
ec = esp_netif_set_ip_info(netIf, &ip_info); | ||
|
||
// Make sure DHCP client is disabled | ||
netIf->flags = (esp_netif_flags_t)(netIf->flags & ~ESP_NETIF_DHCP_CLIENT); | ||
} | ||
|
||
return ec; | ||
} | ||
|
||
esp_err_t NF_ESP32_ConfigureNetworkByIndex(int index, esp_netif_t *netIf) | ||
{ | ||
esp_err_t ec; | ||
|
||
HAL_Configuration_NetworkInterface *networkConfig = NF_ESP32_GetNetworkConfigBlock(index); | ||
if (networkConfig == NULL) | ||
{ | ||
return ESP_FAIL; | ||
} | ||
|
||
ec = NF_ESP32_ConfigureNetwork(netIf, networkConfig); | ||
|
||
platform_free(networkConfig); | ||
|
||
return ec; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid direct flag manipulation to stop DHCP client
Directly modifying
netIf->flags
may be fragile if the ESP-IDF API changes. Use the official ESP-IDF API functionesp_netif_dhcpc_stop()
to disable the DHCP client.📝 Committable suggestion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AdrianSoundy this looks a valid observation. Do you agree?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.