Skip to content
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

Update IP addresses in ESP_NETIF to stop static addresses being overwritten #3133

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

AdrianSoundy
Copy link
Member

@AdrianSoundy AdrianSoundy commented Mar 14, 2025

Description

This has been an issue since the IDF changed from TCPIP adapters to the ESP_NETIF interface.
The problem is the ESP_NETIF starts the DHCP when the WIFI connects overriding the static IP settings.

This change updates IP addresses in ESP_NETIF by call esp_netif_set_ip_info() for Ethernet & Wireless networks.
Also tidies up code by moving common code to new file NF_ESP32_Network.cpp

Motivation and Context

How Has This Been Tested?

Some testing was done at the time these changes where done.
Further testing should be done to confirm chnage is working correctly.

Types of changes

  • Improvement (non-breaking change that improves a feature, code or algorithm)
  • Bug fix (non-breaking change which fixes an issue with code or algorithm)
  • New feature (non-breaking change which adds functionality to code)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Config and build (change in the configuration and build system, has no impact on code or features)
  • Dev Containers (changes related with Dev Containers, has no impact on code or features)
  • Dependencies/declarations (update dependencies or assembly declarations and changes associated, has no impact on code or features)
  • Documentation (changes or updates in the documentation, has no impact on code or features)

Checklist

  • My code follows the code style of this project (only if there are changes in source code).
  • My changes require an update to the documentation (there are changes that require the docs website to be updated).
  • I have updated the documentation accordingly (the changes require an update on the docs in this repo).
  • I have read the CONTRIBUTING document.
  • I have tested everything locally and all new and existing tests passed (only if there are changes in source code).

Summary by CodeRabbit

  • New Features

    • Enhanced ESP32 networking capabilities with improved static IP support for Ethernet and refined Wi-Fi station setup, offering more consistent connectivity.
    • Added functions for managing network configurations, including retrieving network configuration blocks and configuring network settings based on provided configurations.
  • Refactor

    • Streamlined the network initialization process by removing redundant waiting mechanisms, ensuring a more efficient and reliable configuration experience without altering existing functionality.

@nfbot nfbot changed the title Update IP addresses in ESP_NETIF to stop static addresses being overwritten. Update IP addresses in ESP_NETIF to stop static addresses being overwritten Mar 14, 2025
Copy link

coderabbitai bot commented Mar 14, 2025

Walkthrough

This update integrates new ESP32 network configuration functionality. A new source file is added to the build list and implemented with functions that retrieve network configuration blocks, configure network settings, and apply settings by index. Additionally, the Ethernet module now sets a static IP by invoking the new configuration function, while the wireless module replaces a waiting function with a dedicated station configuration function. Corresponding header declarations have also been updated.

Changes

File(s) Change Summary
targets/ESP32/.../CMakeLists.txt Appended new source file NF_ESP32_Network.cpp to the ESP32 networking source list.
targets/ESP32/_Network/NF_ESP32_Ethernet.cpp Added a static Ethernet configuration call using NF_ESP32_ConfigureNetworkByIndex after attaching the TCP/IP stack.
targets/ESP32/_Network/NF_ESP32_Network.cpp
targets/ESP32/_include/NF_ESP32_Network.h
Introduced new network management functions: NF_ESP32_Wait_NetNumber,
NF_ESP32_GetNetworkConfigBlock,
NF_ESP32_ConfigureNetwork, and
NF_ESP32_ConfigureNetworkByIndex with corresponding declarations.
targets/ESP32/_Network/NF_ESP32_Wireless.cpp Added the NF_ESP32_ConfigureNetworkStation function (invoking NF_ESP32_ConfigureNetworkByIndex for Wi‑Fi) and removed the obsolete NF_ESP32_Wait_NetNumber function.

Suggested labels

Platform: ESP32, Type: dependencies

Suggested reviewers

  • josesimoes
  • Ellerbach

Tip

⚡🧪 Multi-step agentic review comment chat (experimental)
  • We're introducing multi-step agentic chat in review comments. This experimental feature enhances review discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments.
    - To enable this feature, set early_access to true under in the settings.
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Automated fixes for code style.
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Nitpick comments (2)
targets/ESP32/_Network/NF_ESP32_Ethernet.cpp (1)

222-223: Implementation looks good for configuring static addresses

The new code addresses the PR objective by calling NF_ESP32_ConfigureNetworkByIndex to configure static IP addresses before starting the Ethernet driver, which should prevent them from being overwritten by DHCP.

Consider adding error logging here similar to what's in the Wireless implementation for consistency:

- ESP_ERROR_CHECK(NF_ESP32_ConfigureNetworkByIndex(IDF_ETH_DEF, eth_netif));
+ esp_err_t ec = NF_ESP32_ConfigureNetworkByIndex(IDF_ETH_DEF, eth_netif);
+ if (ec != ESP_OK) 
+ {               
+     ESP_LOGE(TAG, "Unable to configure Ethernet - result %d", ec);
+ }
targets/ESP32/_Network/NF_ESP32_Network.cpp (1)

38-42: Improve error handling in default case

The comment "can't reach here" is misleading since the default case can be reached with an invalid network interface number. Consider logging the invalid number and returning an error code.

default:
-   // can't reach here
-   HAL_AssertEx();
+   // Invalid network interface number
+   DEBUG_PRINT("Invalid network interface number: %d\n", num);
+   return -1;
    break;
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6492d91 and d150ff7.

📒 Files selected for processing (5)
  • targets/ESP32/_Network/CMakeLists.txt (1 hunks)
  • targets/ESP32/_Network/NF_ESP32_Ethernet.cpp (1 hunks)
  • targets/ESP32/_Network/NF_ESP32_Network.cpp (1 hunks)
  • targets/ESP32/_Network/NF_ESP32_Wireless.cpp (2 hunks)
  • targets/ESP32/_include/NF_ESP32_Network.h (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: nf-interpreter (Check_Code_Style)
  • GitHub Check: nf-interpreter (Check_Build_Options)
  • GitHub Check: nf-interpreter (Nightly build) (Check_Build_Options)
🔇 Additional comments (6)
targets/ESP32/_Network/CMakeLists.txt (1)

14-14: Correctly adds the new network file to the build

This change properly adds the new NF_ESP32_Network.cpp file to the build, aligning with the PR objective of moving common code into a dedicated file.

targets/ESP32/_include/NF_ESP32_Network.h (1)

63-65: New function declarations look good

These new function declarations support the PR objective by providing proper interfaces for retrieving network configuration blocks and applying network settings to prevent static addresses from being overwritten.

targets/ESP32/_Network/NF_ESP32_Wireless.cpp (2)

115-118: Helper function implementation looks good

This new function provides a clean, dedicated way to configure the Wi-Fi station with the correct network index.


163-169: Error handling for network configuration looks good

This implementation properly checks for errors when configuring the Wi-Fi station and logs them appropriately, helping with debugging if there are issues with static IP configuration.

targets/ESP32/_Network/NF_ESP32_Network.cpp (2)

55-69: Memory allocation handling is well implemented

The function properly checks for memory allocation failures and configuration retrieval errors, returning NULL in both cases. This allows the caller to handle errors appropriately.


103-118: Proper memory management in NF_ESP32_ConfigureNetworkByIndex

The function correctly frees the allocated memory for the network configuration block after use, which prevents memory leaks.

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);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

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 function esp_netif_dhcpc_stop() to disable the DHCP client.

- netIf->flags = (esp_netif_flags_t)(netIf->flags & ~ESP_NETIF_DHCP_CLIENT);
+ esp_err_t dhcp_stop_result = esp_netif_dhcpc_stop(netIf);
+ // It's okay if DHCP was already stopped
+ if (dhcp_stop_result != ESP_OK && dhcp_stop_result != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED)
+ {
+     return dhcp_stop_result;
+ }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
netIf->flags = (esp_netif_flags_t)(netIf->flags & ~ESP_NETIF_DHCP_CLIENT);
esp_err_t dhcp_stop_result = esp_netif_dhcpc_stop(netIf);
// It's okay if DHCP was already stopped
if (dhcp_stop_result != ESP_OK && dhcp_stop_result != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED)
{
return dhcp_stop_result;
}

Comment on lines 71 to 101
//
// 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;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Missing DHCP client enablement when configured

The function calculates whether DHCP should be enabled but only handles disabling it for static IP configurations. When enableDHCP is true, the code should enable the DHCP client.

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);

+   // Handle DHCP configuration
+   if (enableDHCP)
+   {
+       // Enable DHCP client
+       ec = esp_netif_dhcpc_start(netIf);
+       return ec;
+   }
+   // Static IP configuration
    // 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);
+       ec = esp_netif_dhcpc_stop(netIf);
+       if (ec != ESP_OK && ec != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED)
+       {
+           return ec;
+       }
    }

    return ec;
}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
//
// 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;
}
//
// 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);
// Handle DHCP configuration
if (enableDHCP)
{
// Enable DHCP client
ec = esp_netif_dhcpc_start(netIf);
return ec;
}
// Static IP configuration
// 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);
ec = esp_netif_dhcpc_stop(netIf);
if (ec != ESP_OK && ec != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED)
{
return ec;
}
}
return ec;
}

@AdrianSoundy
Copy link
Member Author

@alberk8 Are you able to test this is working OK.
I did this change a while ago and got distracted with something else so PR was never raised. This has now been rebased with current version and should still be OK. I am currently away so can't complete it for a couple of weeks.

…a-dcca-4939-b3d0-7fada9631085' into Static_ip
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

♻️ Duplicate comments (2)
targets/ESP32/_Network/NF_ESP32_Network.cpp (2)

11-53: 🛠️ Refactor suggestion

Add a timeout for the infinite loop in NF_ESP32_Wait_NetNumber.
At present, if the requested interface is never created, the loop can block indefinitely. Consider implementing a max retry count or timeout to prevent the application from hanging.

 int NF_ESP32_Wait_NetNumber(int num)
 {
     esp_netif_t *espNetif = nullptr;
+    const int maxRetries = 1500; // e.g. 1500 * 20ms = 30 seconds
+    int retryCount = 0;

     while (true)
     {
         // ...
         if (espNetif != NULL)
         {
             break;
         }

         vTaskDelay(20 / portTICK_PERIOD_MS);
+        if (++retryCount >= maxRetries)
+        {
+            return -1; // Indicate failure
+        }
     }

     return espNetif->lwip_netif->num;
 }

74-101: 🛠️ Refactor suggestion

Use official DHCP APIs rather than manipulating netIf->flags.
Disabling DHCP by modifying netIf->flags directly can be fragile. The ESP-IDF recommends using esp_netif_dhcpc_stop() or esp_netif_dhcpc_start() for robust DHCP control. Additionally, if enableDHCP is true, you may want to explicitly enable the DHCP client.

bool enableDHCP = (config->StartupAddressMode == AddressMode_DHCP);

// If DHCP is enabled, start the DHCP client
+if (enableDHCP) {
+    ec = esp_netif_dhcpc_start(netIf);
+    if (ec != ESP_OK && ec != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED) {
+        return ec;
+    }
+} else {
    // 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);

-       netIf->flags = (esp_netif_flags_t)(netIf->flags & ~ESP_NETIF_DHCP_CLIENT);
+       // Stop DHCP client if configured for static IP
+       esp_err_t dhcpStop = esp_netif_dhcpc_stop(netIf);
+       if (dhcpStop != ESP_OK && dhcpStop != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED)
+       {
+           return dhcpStop;
+       }
    }
}
🧹 Nitpick comments (2)
targets/ESP32/_Network/NF_ESP32_Wireless.cpp (1)

163-169: Revisit the 'ignore any errors' comment.
Although the inline comment states to ignore errors, the code does log errors when ec != ESP_OK. If these unexpected failures need follow-up actions (e.g. disabling further Wi-Fi initialization steps or reverting to DHCP), you might consider more robust handling instead of continuing with partial configuration.

targets/ESP32/_Network/NF_ESP32_Network.cpp (1)

103-118: Validate the final result after configuration.
This function retrieves a network configuration block, applies it, and frees the block. Consider logging or confirming that the new IP configuration is successfully set. Adding a short verification step (like another esp_netif_get_ip_info() call) could help catch subtle runtime issues.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d150ff7 and 4a069e5.

📒 Files selected for processing (3)
  • targets/ESP32/_Network/NF_ESP32_Network.cpp (1 hunks)
  • targets/ESP32/_Network/NF_ESP32_Wireless.cpp (2 hunks)
  • targets/ESP32/_include/NF_ESP32_Network.h (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • targets/ESP32/_include/NF_ESP32_Network.h
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: nf-interpreter (Check_Code_Style)
  • GitHub Check: nf-interpreter (Nightly build) (Check_Build_Options)
  • GitHub Check: nf-interpreter (Check_Build_Options)
🔇 Additional comments (1)
targets/ESP32/_Network/NF_ESP32_Wireless.cpp (1)

115-118:

❓ Verification inconclusive

Consider explicit handling of station mode checks.
While this function successfully delegates configuration to NF_ESP32_ConfigureNetworkByIndex, ensure that the caller only invokes it if the device is indeed in station mode. If future code calls this outside station mode contexts, it might lead to unexpected results or error logs.

Would you like to verify that this function is only called for station interfaces by searching the codebase for its invocations and ensuring the associated logic?


🏁 Script executed:

#!/bin/bash
rg -A 4 "NF_ESP32_ConfigureNetworkStation"

Length of output: 828


Action: Confirm Explicit Station Mode Validation for NF_ESP32_ConfigureNetworkStation

We verified that the only identifiable invocation of this function is made with a variable named wifiStaNetif in targets/ESP32/_Network/NF_ESP32_Wireless.cpp, suggesting that it is intended for station mode configurations. However, there is no explicit check within the function (or immediately before its invocation) that confirms the provided interface indeed belongs to station mode. To guard against potential misuse from future changes, please consider adding an explicit validation step—either here or at the caller—to ensure that the function is only used with station interfaces.

  • File under review: targets/ESP32/_Network/NF_ESP32_Wireless.cpp
  • Identified call site: Invocation using wifiStaNetif without an accompanying station mode check

@AdrianSoundy
Copy link
Member Author

There are some useful suggestions coming out of coderabbitai for existing code but I don't have time at the moment to follow them up. I can do that once I am back from holiday.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

♻️ Duplicate comments (2)
targets/ESP32/_Network/NF_ESP32_Network.cpp (2)

95-96: ⚠️ Potential issue

Handle DHCP enablement as previously recommended.

This logic assigns enableDHCP but never starts the DHCP client. A past comment already addressed this omission. When enableDHCP is true, ensure you invoke esp_netif_dhcpc_start() to enable the DHCP client.

Consider applying the following diff:

 bool enableDHCP = (config->StartupAddressMode == AddressMode_DHCP);

+ if (enableDHCP)
+ {
+     ec = esp_netif_dhcpc_start(netIf);
+     if (ec != ESP_OK && ec != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED)
+     {
+         return ec;
+     }
+ }

107-107: ⚠️ Potential issue

Avoid direct flag manipulation to stop DHCP, as previously advised.

This line is still directly modifying netIf->flags to disable the DHCP client. Per the ESP-IDF guidelines, use esp_netif_dhcpc_stop(netIf) instead to improve reliability and forward compatibility.

Example fix:

- netIf->flags = (esp_netif_flags_t)(netIf->flags & ~ESP_NETIF_DHCP_CLIENT);
+ ec = esp_netif_dhcpc_stop(netIf);
+ // It's okay if DHCP was already stopped
+ if (ec != ESP_OK && ec != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED)
+ {
+     return ec;
+ }
🧹 Nitpick comments (1)
targets/ESP32/_Network/NF_ESP32_Network.cpp (1)

98-106: Validate static address settings to avoid invalid configurations.

If a static IP is required (enableDHCP == false) yet config->IPv4Address is 0, no valid IP info gets applied. Consider warning or handling this edge case to prevent silently misconfigured interfaces.

Example approach:

 bool enableDHCP = (config->StartupAddressMode == AddressMode_DHCP);

 // Set static addresses
 if (config->IPv4Address != 0)
 {
+    // Optionally validate these addresses before applying
     ip_info.ip.addr = config->IPv4Address;
     ip_info.netmask.addr = config->IPv4NetMask;
     ip_info.gw.addr = config->IPv4GatewayAddress;
 }
+else if (!enableDHCP)
+{
+    // Log warning or handle the scenario where the user specified static mode
+    // but didn't provide valid static IP data
+    // e.g. ESP_LOGW("NF_ESP32_Network", "No static IP configured in static mode");
+}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4a069e5 and 1a1aea9.

📒 Files selected for processing (1)
  • targets/ESP32/_Network/NF_ESP32_Network.cpp (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: nf-interpreter (Check_Code_Style)
  • GitHub Check: nf-interpreter (Nightly build) (Check_Build_Options)
  • GitHub Check: nf-interpreter (Check_Build_Options)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wifi Static IP assignment does not work
2 participants