BUG in SNTP.c
Answer
1/14/19 10:05 AM

Hello,

 

In file SDK/xdk110/Common/source/Utility/SNTP.c there is a bug in function SNTP_Enable(void). When you initialise a UDP socket instead of providing source port on which you whish to receive response from NTP server, you use destination port (123) of the NTP server. Since port 123 is from reserved range (0-1023) the function SntpTimeCallback is never called (no reply from server). This caused the whole SNTP.c implementation to be useles, because it could not update time via NTP.

I have changed the function implementation from

Ip_Port_T destPort = (Ip_Port_T) (SNTPSetup.ServerPort);

...

if (RC_OK != Sntp_start(Ip_convertIntToPort(destPort), SntpTimeCallback))

 

to

Ip_Port_T rcvPort = (Ip_Port_T) (SNTPSetup.ServerPort + 1023);

...

if (RC_OK != Sntp_start(Ip_convertIntToPort(rcvPort), SntpTimeCallback))

 

and now it works. 

0 (0 Votes)
RE: BUG in SNTP.c
Answer
1/16/19 4:53 PM as a reply to Mateusz Olejnik.
Hello Mateusz,

First of all, let me welcome you to the XDK Community.

I would like to note, that this is not a bug but the designated method of SNTP.
Could you give me please a more detailed description of the project you are working on?
Could you please get more into detail on why you don't want to use the standart port 123? This port is configured as a standart port for NTP. The shift of the port to a port with a higher number, let's you enter the area of registered ports which are assigned by the Internet Assigned Numbers Authority (IANA) for specific service.

Please tell me if this was helpful, and do not hesitate to ask further questions.

Kind regards,
Franjo
0 (0 Votes)
RE: BUG in SNTP.c
Answer
1/16/19 6:11 PM as a reply to Franjo Stjepandic.

Hello Franjo,

 

Thank you for your answer, but I must disagree - this is a bug, as the code without my change is not working (I cannot synchronise with any well known NTP server in the Internet). NTP port is indeed 123, but this is receive port number, no the port on which XDK opens socket for the reply. What I proposed in my fix is to change the outgoing port of XDK. Please refer to this poor draft of the situation:

 

XDK                ----- (NTP request) --------> NTP server (listens port 123)

(rcv port)        <---- (NTP reply)-------------

 

Here the (rcv port) cannot be 123. Thus I have added 1023 to it to get sth from range > 1023. And yes, this is called "registered ports" by IANA, but the port range can be used without any super privilages.

> Could you please get more into detail on why you don't want to use the standart port 123?

No, it's not about "I don't want", it's about I cannot use port 123 because it is simply not working. Please run any sample application from XDK like BoschXDKCloudConnectivity - it does not work (at least for me). Function SNTP_GetTimeFromServer cannot sync time with NTP server because of this bug. This is because in SNTP_Enable the sourcePort on which NTP reply should be received is confused with the destination port (which is 123).

 

-- update--

Maybe I will put it this way: I do use port 123, but as a destination port. I.e. I have SNTP setup as this:

static SNTP_Setup_T SNTPSetupInfo =

        {

                .ServerUrl = "0.pool.ntp.org",

                .ServerPort = UINT16_C(123),

        };

 

But since SNTP_Enable takes SNTPSetupInfo.ServerPort as both destination port (which is OK) and listen port (which is not OK) the whole SNTP feature does not work. Especially SntpTimeCallback is never called.

 

 

0 (0 Votes)
RE: BUG in SNTP.c
Answer
1/17/19 3:21 PM as a reply to Mateusz Olejnik.
Hello Mateusz,

I can assure you that this is not a bug since this has been tested would not be released if it had such a major bug.

Furthermore, I can personally ensure that it is working, since I used it in two different applications this week.

Please note SNTP uses UDP as the underlying protocol and there are some specifics, which need to be considered.
The biggest issue is that UDP is a connectionless protocol, that means that there are no mechanisms to check if the sent message has reached the destination server.


This means if you do not get any response from the server, how do you actually ensure that your message reached it? The first solution is to repeat the request within a do-while loop until a valid timestamp has been received.

Furthermore the initialization of the element SNTPSetupInfo.ServerPort uses the NTP port 123 as destination and as listening port which is fine since SNTP requests can also be sent on this port to the XDK.

Furthermore, in order to make SNTP work, the modules Wi-Fi, and ServalPAL must have been successfully initialized and enabled prior. Could you please make sure that this prerequisite is met?

Do you use a network analyzer (e.g. tcpdump) to confirm that there are UDP requests going out to your destination SNTP server?

For troubleshooting could you please go more into detail about the XDK application you are currently trying to use SNTP with and the surrounding Wi-Fi network your XDK is in?

Please tell me if this was helpful, and do not hesitate to ask further questions.

Kind regards,
Franjo
0 (0 Votes)
RE: BUG in SNTP.c
Answer
1/17/19 6:34 PM as a reply to Franjo Stjepandic.

Hello Franjo,

Yes, I do know how UDP works. And yes, I do setup WLAN module and ServalPal before trying to update time with SNTP. For example my code on XDK can communicate with MQTT server I run on my laptop via WiFi, so the WiFi works OK.

I just thought maybe my WiFi router is somehow blocking UDP packets going back from the NTP server to the SDK? Your idea with packet sniffer is very good, I will check what packets are actually going in/out with Wireshark/tcpdump.

And yes, I do use do...while loop. The case is that when I use port 123 I do not get response even after 100 tries (30s timeout between tries) and when I use port 123+1023 as described in my original post I get the reply just after first request. So maybe indeed the router is a blocker here. But then how other devices in my WiFi synchronise time?

What information do you need about my application? I just connect to wifi and establish MQTT connection and send sensor data. All works (wifi connection, mqtt, sending data and receiving them on my laptop), but when I wanted to append current timestamp to the data I cannot do this since the timestamp cannot be synced.

 

Regards,

Mateusz

0 (0 Votes)
RE: BUG in SNTP.c
Answer
1/18/19 2:32 PM as a reply to Mateusz Olejnik.
Dear Mateusz,

Thank you for clarifying. In regards to the questions about how other devices synchronize time, you have to differ between SNTP and NTP. SNTP is short for simple network time protocol, a simplified version of the NTP protocol for embedded devices.

This simplification might be the reason, why your XDK is unable to synchronize time. Furthermore, I assume you are running your tests in your own private Wi-Fi network apart from a corporate company Enterprise Wi-Fi network?

The information regarding your application would be if you could provide an outline about how and where you implemented and called the modules Wi-Fi, ServalPAL and SNTP itself. Are you using them in the same calling hierarchy such as in the example from the Welcome Screen of the XDK-Workbench?

Furthermore, if you are using the standalone tcpdump program, then the following command should display the information you seek:

sudo tcpdump -i "interface" udp -A -s 0 port 123

Please let me know if this was helpful and feel free to ask if you have further questions.

Kind regards,
Franjo
0 (0 Votes)