AW: [En-Nut-Discussion] Synching Calldirect using NutOS

Lars Andersson lakab at telia.com
Thu Oct 16 20:58:09 CEST 2003


Hi Fahad,
my code hack was not meant to be "official" like it has become here.
I am a hardware design guy happily playing with C code in my spare time 
and certainly am no expert in NTP matters. I use this thing to set the
clock
after power up or reset in a pair of home lighting controllers. It has
been running
every day since Nut ver 3.30 came. Obviously it is buggy since the day
and year part
does not work out.

The parameter "i_adr" in the call    
   NutUdpSendTo(sock, i_adr, 13, data, sizeof(*data));
should be of type 
   inet_addr("192.36.144.22")
but I guess you figured that out already.

In my app I use it like:

	for (i = 0; i < 4; i++)
	{
		if (SNTP_get_stamp(&NowTimeStamp,
(u_long)inet_addr(SNTP_IP1))) break;
		NutSleep(0);
		if (SNTP_get_stamp(&NowTimeStamp,
(u_long)inet_addr(SNTP_IP2))) break;
		NutSleep(0);
		if (SNTP_get_stamp(&NowTimeStamp,
(u_long)inet_addr(SNTP_IP3))) break;
		NutSleep(0);
	}

	if(NowTimeStamp) 
	{
		stamp2timedate(NowTimeStamp, &td);
	}
	else
....

// Lars

-----Original Message-----
From: Fahad G. [mailto:Fahad.Gilani at anu.edu.au] 
Sent: Thursday, 16 October, 2003 11:41
To: lakab at telia.com
Subject: RE: AW: [En-Nut-Discussion] Synching Calldirect using NutOS


Hi Lars,

Thanks for contributing your code.. I was in need of something similar
as
well. However, I tried it out and it doesn't seem to work somehow,
SNTP_get_stamp always seems to return 0 for the SNTP servers you listed.
I
then tweaked your code a bit to try connecting to time.nist.gov at port
13
(this server is always available) and that doesn't work either. Could
you
have a look at the code below and point out what I might be doing wrong?

int SNTP_get_stamp(u_long *Stamp, u_long i_adr)
// Return whole seconds part in parameter Stamp or 0 if failed
// Stamp is number of seconds since 00:00:00 1 Jan 1900
// Function returns 0 if failed, bytes read otherwise
{
   UDPSOCKET *sock;
   SNTP_FRAME *data;
   u_short i_port;
   int sts;

   data = NutHeapAlloc(60);
   sock = NutUdpCreateSocket(0);
   memset((void *)data, 0, sizeof(*data));
   data->mode_stratum = 0x0B;


   /* I changed the port to 13 */
   NutUdpSendTo(sock, i_adr, 13, data, sizeof(*data));


   i_adr = inet_addr("0.0.0.0");
   sts = NutUdpReceiveFrom(sock, &i_adr, &i_port, data,sizeof(*data),
5000);

   NutUdpDestroySocket(sock);
   // Do at least some checks on validity


   /* I commented some part which I thought wouldn't make a difference
*/
   if(sts /*&& i_port == 123 && (data->mode_stratum & 0xC0) != 0xc0 */)
   {
 	*Stamp = ntohl(data->TX_TS_sec); // We have a valid response
   }
   else
   {
 	*Stamp = 0;
	sts=0;
   }

   NutHeapFree(data);
   return sts;
}


Any help would be appreciated. Thanks,

Cheers,

Fahad



-----Original Message-----
From: en-nut-discussion-admin at egnite.de
[mailto:en-nut-discussion-admin at egnite.de] On Behalf Of Lars Andersson
Sent: Wednesday, October 15, 2003 5:35 AM
To: en-nut-discussion at egnite.de
Subject: RE: AW: [En-Nut-Discussion] Synching Calldirect using NutOS

They are all universal if you know about them ;-)
look at ntp.org or http://www.eecis.udel.edu/~mills/ntp/clock2a.html

Right now I use these
#define SNTP_IP1 "192.36.144.22"
#define SNTP_IP2 "192.36.133.17"
#define SNTP_IP3 "130.235.20.3"








More information about the En-Nut-Discussion mailing list