Home icon indicating copy to clipboard operation
Home copied to clipboard

SNTP update issues

Open networkfusion opened this issue 2 years ago • 0 comments

Library/API/IoT binding

nanoFramework.Networking.NetworkHelper

Visual Studio version

N/A

.NET nanoFramework extension version

N/A

Target name(s)

ST_STM32F769I_DISCOVERY

Firmware version

N/A

Device capabilities

N/A

Description

The program should be able to get a valid time.

How to reproduce

Load the sample program...

It will attempt to get the time over 10 itterations before showing it is possible.

Expected behaviour

It should be able to do it on first attempt.

Screenshots

No response

Sample project or code

using nanoFramework.Networking;
using nanoFramework.Runtime.Native;
using System;
using System.Diagnostics;
using System.Threading;

namespace test_stm32f769i_sntp
{
    // nanoff --target ST_STM32F769I_DISCOVERY --jtag --update --masserase
    public class Program
    {

        public static void Setup()
        {
            // Test setup!
            Rtc.SetSystemTime(new DateTime(2018, 2, 28, 10, 20, 30));

            Debug.WriteLine($"Start DateTime = {DateTime.UtcNow.ToString("o")}");
            Debug.WriteLine($"Start IP Address = {System.Net.NetworkInformation.IPGlobalProperties.GetIPAddress()}");
            Debug.WriteLine($"SNTP IsStarted = {Sntp.IsStarted}");
        }

        public static void Main()
        {
            Debug.WriteLine("nanoFramework SNTP smoke test!");
            Debug.WriteLine("This should pass in the following scenarios:");
            Debug.WriteLine("* Initial deploy of firmware.");
            Debug.WriteLine("* Initial deploy of program.");
            Debug.WriteLine("* Change of IP address.");
            Debug.WriteLine("* Change of time.");


            Setup();


            var res = false;
            var failedCount = 0;
            while (!res)
            {
                CancellationTokenSource cs = new(15000);
                res = NetworkHelper.SetupAndConnectNetwork(requiresDateTime: true, token: cs.Token);
                if (!res)
                {
                    Debug.WriteLine("Still Waiting for valid network IP address or date-time.");
                    Debug.WriteLine($"Current IP = {System.Net.NetworkInformation.IPGlobalProperties.GetIPAddress()}");
                    Debug.WriteLine($"Current RTC = {DateTime.UtcNow}");
                }

                failedCount++;

                if ( failedCount > 10 )
                {
                    Debug.WriteLine($"Test has failed, attempting forced update!");
                    Sntp.UpdateNow();
                }
            }

            Debug.WriteLine($"IP = {System.Net.NetworkInformation.IPGlobalProperties.GetIPAddress()}");
            Debug.WriteLine($"RTC = {DateTime.UtcNow}");


            Thread.Sleep(Timeout.Infinite);

        }

    }
}

Aditional information

Given that the NetworkHelper is just a while loop checking for a DateTime with the year greater than 2023, this is to some extent expected. However, the check is too simplistic and needs to at least try and force a check, otherwise we might as well not have it!

networkfusion avatar Apr 28 '23 21:04 networkfusion