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

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

Open
muenchris opened this issue Jun 20, 2024 · 2 comments

Comments

@muenchris
Copy link

Library/API/IoT binding

nanoFramework.Networking

Visual Studio version

VS2022.10.9.7

.NET nanoFramework extension version

2022.7.8

Target name(s)

ESP32_OLIMEX_WROVER

Firmware version

.247

Device capabilities

Target capabilities:
Has nanoBooter: NO
IFU capable: NO
Has proprietary bootloader: YES

Description

If the NetworkHelper cannot get an IP or fails otherwise (i.e. disconnected media), it fails to reset properly to "try again".

How to reproduce

Upload a sketch to the ESP

Run the code below and dont plug in an ethernet cable.
Once it has failed, plug in the cable - it should try again but the cancellation token is not reset properly and the "SetupAndConnectNetwork" function fails right away.

Expected behaviour

The tokes is "newed" again and should allow a second try. Since it fails right away, there might be something not working/resetting correctly in the SetupAndConnectNetwork helper.

Screenshots

No response

Sample project or code

        private static void InitNetworkAndComms()
        {
            bool success = false;
            int trialcounter = 0;
            while (!success)
            {

                    using (CancellationTokenSource cs = new(60000))
                    {
                        DebugWriteLine($"Trying Ethernet...");
                        success = NetworkHelper.SetupAndConnectNetwork(cs.Token);
                    }
                    if (success)
                        Debug.WriteLine("Ethernet Connected");
                    else
                        Debug.WriteLine($"Eth failed:{trialcounter}");
                
                if (!success)
                {
                    using (CancellationTokenSource cs2 = new(60000))
                    {
                        DebugWriteLine($"Trying Wifi...{WifiSSID}");
                        success = WifiNetworkHelper.ConnectDhcp(WifiSSID, WifiPWD, requiresDateTime: false, token: cs2.Token);
                        if (success)
                            Debug.WriteLine("Wifi Connected");
                        else
                            Debug.WriteLine($"wifi failed:{trialcounter}");
                    }
                }

                if (!success)
                {
                    Debug.WriteLine($"Can't get a proper IP address, error: {NetworkHelper.Status}. Trying again in 5 seconds");
                    if (NetworkHelper.HelperException != null)
                    {
                        Debug.WriteLine($"Exception: {NetworkHelper.HelperException}");
                    }
                    trialcounter++;
                    Thread.Sleep(5000);
                }
            }
        }

Aditional information

The Wifi "ConnectDhcp" is never successful in this context even if the SSDI and PWD are correct

@josesimoes
Copy link
Member

Noted. That's a good suggestion for an improvement of the helper class!

@josesimoes
Copy link
Member

Happy to discuss the required changes and for a community member to submit a PR improving this. 😉

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

No branches or pull requests

2 participants