Author Topic: Time Server  (Read 6088 times)

Offline kingston

  • Newbie
  • *
  • Posts: 26
    • View Profile
Time Server
« on: July 06, 2012, 12:31:21 PM »
Hey all,

I recently tried using the time servers in my project but it looks like it never tries to get the time.
What are the necessary steps to fetch the time?
Is defining USE_TIME_SERVERS not enough?
I can't find documentation about this other than some forum posts so please help me.

Thank You

Paul

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3234
    • View Profile
    • uTasker
Re: Time Server
« Reply #1 on: July 06, 2012, 01:26:06 PM »
Hi Paul

As long as the project has Ethernet and TCP enabled you only need to set USE_TIME_SERVER.

The connection attempt is made once the board starts and is controlled in application.c.
The first attempt takes place when the task is first called - see fnValidatedInit(). If the IP settings are not yet validtated this won't take place, but that is an exception.

It connects with fnTCP_Connect(TIME_TCP_socket, (unsigned char *)&ucTimeServers[ucTimeServerTry++], TIME_PORT, 0, 0);

and will repeat a few times for the first server until it gets a response. (Search for fnTCP_Connect() in application.c to see the locations).

If there is no answer from the first server it will work through the list
static const unsigned char ucTimeServers[NUMBER_OF_TIME_SERVERS][IPV4_LENGTH];

Once the time has been received it is finished (the time may be displayed on an LCD or shown on a web page, etc. depending on which options are enabled in the project).

In some cases one would like this to be repeated at regular intervals and so resetting the server list (ucTimeServerTry = 0) and recalling fnTCP_Connect(TIME_TCP_socket, (unsigned char *)&ucTimeServers[ucTimeServerTry++], TIME_PORT, 0, 0); would restart the procedue at a later time.

Regards

Mark

P.S. Note that this will be an external TCP connection and so, when monitoring with Wireshark, it will need to be using a hub or correctly configured managed switch so that the traffic can be seen.

Offline kingston

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Time Server
« Reply #2 on: July 31, 2012, 09:01:52 AM »
Hi Mark,
 
I was near desperation because I just couldn't get it to work but now I found the problem in my firewall configuration...


Thanks again!

Paul


EDIT: Time works as it should but the date doesn't get synced. I tried writing a function myself but couldn't find the raw 32bit value transmitted in the time protocol. Any hints? :)
« Last Edit: July 31, 2012, 11:35:11 AM by kingston »

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3234
    • View Profile
    • uTasker
Re: Time Server
« Reply #3 on: August 02, 2012, 06:01:08 PM »
Hi Paul

The time server sends a 32 bit value which is the number of seconds from 0:0:0 1900.
The code receives this in fnTimeListener() and caluculates the seconds of the present day from it - it doesn't keep the data information as you have seen.

    case TCP_EVENT_DATA:                                                 // a time server sends the time in seconds from 0:0:0 1900 and terminates
        ulPresentTime = *ucIp_Data++;
        ulPresentTime <<= 8;
        ulPresentTime |= *ucIp_Data++;
        ulPresentTime <<= 8;
        ulPresentTime |= *ucIp_Data++;
        ulPresentTime <<= 8;
        ulPresentTime |= (*ucIp_Data);

        ulPresentTime -= REL_9_1_2007;                                   // relative to 0:0:0 on 9.1.2007
        ulPresentTime += GMT_OFFSET;
        while (ulPresentTime >= SECONDS_24_HOURS) {
            ulPresentTime -= SECONDS_24_HOURS;                           // convert to seconds of day
        }



Check out whether your project contains Gregorian time support. There is usually a function called
static void fnConvertSecondsTime(unsigned long ulSecondsTime);
in the processor file (eg. AVR32.c etc.).
This takes the number of seconds from 1970 and converts to time and date so could also be used by the timer server value after subtracting the difference in seconds between 1970 and 1900.

Regards

Mark