Skip to content

Fix WiFi helper retry/reset behavior and test API usage#351

Merged
josesimoes merged 3 commits into
nanoframework:mainfrom
josesimoes:improvements-wifi-helper
May 29, 2026
Merged

Fix WiFi helper retry/reset behavior and test API usage#351
josesimoes merged 3 commits into
nanoframework:mainfrom
josesimoes:improvements-wifi-helper

Conversation

@josesimoes
Copy link
Copy Markdown
Member

Description

  • Allow WifiNetworkHelper to be reset and started again.
  • Keep reconnect state accurate when IP is lost and restored.
  • Fix nanoFramework-nullability issue in the WiFi helper.
  • Update WiFi tests to the v3 API.
  • Refresh WiFi README guidance for retry/reconfiguration.

Motivation and Context

How Has This Been Tested?

Screenshots

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)
  • Dependencies (update dependencies and changes associated, has no impact on code or features)
  • Unit Tests (add new Unit Test(s) or improved existing one(s), 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).
  • I have added new tests to cover my changes.

- Allow `WifiNetworkHelper` to be reset and started again.
- Keep reconnect state accurate when IP is lost and restored.
- Fix nanoFramework-nullability issue in the WiFi helper.
- Update WiFi tests to the v3 API.
- Refresh WiFi README guidance for retry/reconfiguration.
@josesimoes josesimoes force-pushed the improvements-wifi-helper branch from 1d9a38a to 6352542 Compare May 29, 2026 14:13
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 29, 2026

Review Change Stack

Warning

Review limit reached

@josesimoes, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 44 minutes and 17 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository: nanoframework/coderabbit/.coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 307bf682-4acd-4768-ab84-c572d2f7ecf3

📥 Commits

Reviewing files that changed from the base of the PR and between 7755030 and 5d3012e.

📒 Files selected for processing (3)
  • README.md
  • System.Device.Wifi/NetworkHelper/WifiNetworkHelper.cs
  • Tests/NFUnitTestWifiConnection/ConnectToWifiWithCredentialsScanTests.cs
📝 Walkthrough

Walkthrough

This PR adds a public Reset() method to WifiNetworkHelper to clear internal state and enable retry after connection timeouts. The method documentation, state management callbacks, and all tests are updated to use the new reset semantics. A new "Retry and reconfiguration" section documents retry patterns and reset workflows for users.

Changes

WiFi Helper Retry and Reset Support

Layer / File(s) Summary
Reset method contract and public API documentation
System.Device.Wifi/NetworkHelper/WifiNetworkHelper.cs
New public Reset() method deregisters callbacks and clears internal state. XML docs for SetupNetworkHelper overloads, ConnectDhcp, ConnectFixAddress, ScanAndConnectDhcp, and Reconnect updated to document reset requirements and retry behavior after timeouts.
SetupHelper state management refactoring
System.Device.Wifi/NetworkHelper/WifiNetworkHelper.cs
SetupHelper(bool setupEvents) reorganized to branch based on setupEvents: when true, performs explicit network interface checks and registers event handler; when false, delegates to underlying helper without event management.
AddressChangedCallback state signaling and conditional readiness
System.Device.Wifi/NetworkHelper/WifiNetworkHelper.cs
AddressChangedCallback updated to defensively check _ipAddressAvailable before signaling, conditionally gate _networkReady on DateTime requirement, and reset signals on IP loss. ResetInstance() now delegates to Reset().
Existing tests migrated to Reset() API
Tests/NFUnitTestWifiConnection/*Tests.cs
All test files updated to call WifiNetworkHelper.Reset() instead of ResetInstance() for cleanup. Assertions standardized to Assert.IsTrue() and Assert.IsNull() across four test files.
New retry and reset behavior validation tests
Tests/NFUnitTestWifiConnection/ConnectToWifiWithCredentialsTests.cs
TestRetryAfterTimeout verifies successful retry after initial timeout. TestResetAllowsEventBasedRestart confirms event-based setup can reconnect after Reset(). TestSingleUsageEventBased validates that repeated event setup without Reset() throws InvalidOperationException.
User documentation and version bump
README.md, System.Device.Wifi/Properties/AssemblyInfo.cs
README section updated with retry patterns, Disconnect() + Reset() workflow, Reconnect() vs ConnectDhcp() semantics, and NetworkReady behavior during transient disconnects. Version bumped to 100.0.6.6.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested labels

Type: bug, Area: Config-and-Build

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main changes: fixing WiFi helper retry/reset behavior and updating test API usage, matching the substantial refactoring in WifiNetworkHelper, test updates, and documentation changes.
Description check ✅ Passed The description is directly related to the changeset, covering the reset functionality, reconnect state accuracy, nullability fixes, test API updates, and README refresh that are all present in the code changes.
Linked Issues check ✅ Passed The PR successfully addresses issue #1511 by introducing a public Reset() method that allows WifiNetworkHelper and NetworkHelper to reset internal state and be reinitialized, enabling retry after timeout failures; updated tests verify retry behavior works correctly.
Out of Scope Changes check ✅ Passed All changes are in scope: WifiNetworkHelper reset/retry functionality, state management on IP loss/recovery, test API updates to v3, README documentation refresh, and assembly version bump directly address the linked issue objectives.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@nfbot
Copy link
Copy Markdown
Member

nfbot commented May 29, 2026

🛑 Native version bump required — now resolved.

Checksum changed from 0x030E2768 to 0xEE721CDA. Native version was not bumped (100.0.6.5) — this has since been corrected to 100.0.6.6.

@nfbot
Copy link
Copy Markdown
Member

nfbot commented May 29, 2026

⚠️ Native declaration update required!

The checksum and/or native version of nanoFramework.System.Device.Wifi have changed. The native declaration in nf-interpreter must be updated before this package can be released.

Previously published This build
Native version 100.0.6.5 100.0.6.6
Checksum 0x030E2768 0xEE721CDA

👉 Tracking issue: nanoframework/Home#1775

The issue contains a link to the stubs artifact from this build and instructions for GitHub Copilot to perform the update.

@josesimoes
Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 29, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@josesimoes
Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 29, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@josesimoes
Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 29, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Copy Markdown

@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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
Tests/NFUnitTestWifiConnection/ConnectToWifiWithCredentialsScanTests.cs (1)

77-87: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Add a trailing Reset() to avoid leaking helper state into later tests.

The first SetupNetworkHelper() succeeds and sets _helperInstanciated = true before the second call throws. Since Reset() is not called after the ThrowsException block, the static guard stays set, which can cause subsequent event-based tests (running on real hardware) to throw unexpectedly. The new TestSingleUsageEventBased already adds this cleanup; mirror it here for consistent test isolation.

🧹 Proposed cleanup
                 // call twice, it's a NO NO and should throw an exception
                 WifiNetworkHelper.SetupNetworkHelper();
             });
+
+            WifiNetworkHelper.Reset();
         }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@Tests/NFUnitTestWifiConnection/ConnectToWifiWithCredentialsScanTests.cs`
around lines 77 - 87, TestSingleUsage leaves WifiNetworkHelper's static guard
set because the first call to WifiNetworkHelper.SetupNetworkHelper() sets
_helperInstanciated = true before the second call throws; add a trailing cleanup
call to WifiNetworkHelper.Reset() after the Assert.ThrowsException block in
TestSingleUsage so the static state is cleared and does not leak into subsequent
tests (mirror the cleanup used in TestSingleUsageEventBased).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@README.md`:
- Line 177: Remove the stray empty Markdown heading introduced as "## " in
README.md (the blank second-level header at line where an empty section
appears); either delete that line entirely or replace it with an appropriate
section title so the document renders correctly and navigation is not broken.

In `@System.Device.Wifi/NetworkHelper/WifiNetworkHelper.cs`:
- Line 47: The XML doc cref currently points to a non-existent parameterless
SetupNetworkHelper(); update the cref to reference one of the actual overload
signatures that exist in this class (for example SetupNetworkHelper(WifiConfig
config) or SetupNetworkHelper(Action onConnected) — use the exact parameter type
list used in the implementation) so the XML reference resolves; apply the same
change to both occurrences that mention SetupNetworkHelper().
- Line 540: Extract the "ensure connected to the target SSID" block out of
SetupHelper into a private helper method (e.g., TryPrepareWifiConnection) that
accepts NetworkInterface[] nis and returns bool indicating whether caller should
proceed to connect; inside it check _ssid/_password emptiness, iterate
Wireless80211 NetworkInterface entries, use
Wireless80211Configuration.GetAllWireless80211Configurations()[ni.SpecificConfigId]
to compare wc.Ssid to _ssid and return false if already configured, otherwise
call _wifi.Disconnect(), call StoreWifiConfiguration(ni) for the first matching
wireless interface and return true; update SetupHelper to call bool
connectToWifi = TryPrepareWifiConnection(nis) and use that flag to flatten the
existing if/else nesting while preserving existing behavior and side-effects.

---

Outside diff comments:
In `@Tests/NFUnitTestWifiConnection/ConnectToWifiWithCredentialsScanTests.cs`:
- Around line 77-87: TestSingleUsage leaves WifiNetworkHelper's static guard set
because the first call to WifiNetworkHelper.SetupNetworkHelper() sets
_helperInstanciated = true before the second call throws; add a trailing cleanup
call to WifiNetworkHelper.Reset() after the Assert.ThrowsException block in
TestSingleUsage so the static state is cleared and does not leak into subsequent
tests (mirror the cleanup used in TestSingleUsageEventBased).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository: nanoframework/coderabbit/.coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: e48e49fa-82bd-4579-9600-60d14705d3b9

📥 Commits

Reviewing files that changed from the base of the PR and between 4c1f133 and 7755030.

📒 Files selected for processing (7)
  • README.md
  • System.Device.Wifi/NetworkHelper/WifiNetworkHelper.cs
  • System.Device.Wifi/Properties/AssemblyInfo.cs
  • Tests/NFUnitTestWifiConnection/ConnectToWifiFixIPAddressTests.cs
  • Tests/NFUnitTestWifiConnection/ConnectToWifiWithCredentialsScanTests.cs
  • Tests/NFUnitTestWifiConnection/ConnectToWifiWithCredentialsTests.cs
  • Tests/NFUnitTestWifiConnection/ConnectToWifiWithoutCredentialsTests.cs

Comment thread README.md Outdated
Comment thread System.Device.Wifi/NetworkHelper/WifiNetworkHelper.cs Outdated
Comment thread System.Device.Wifi/NetworkHelper/WifiNetworkHelper.cs
@josesimoes josesimoes merged commit f3af6ad into nanoframework:main May 29, 2026
6 checks passed
@josesimoes josesimoes deleted the improvements-wifi-helper branch May 29, 2026 16:24
josesimoes added a commit to josesimoes/System.Device.WiFi that referenced this pull request May 29, 2026
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.

NetworkHelper.SetupAnConnect() or WifiNetworkHelper.ConnectDhcp() do not properly reset if they fail with a Cancelation Token Timeout

2 participants