I need to be able to access the current time in my application. I have some questions about how SNTP is implemented in NET+OS 7.0 on the ConnectME (although I would guess this is common to all versions on all platforms).
The primary and secondary time servers are defined by APP_SNTP_PRIMARY and APP_SNTP_SECONDARY in appconf.h. What happens if someone needs to change these in the field? For instance, a device that is not connected to the internet may need to connect to a local time server.
It looks like perhaps NAinstallSntpTime could be used to define your own time server. However, it only takes one argument for a server, so presumably you either don’t get a backup server, or all you get is the hard coded secondary server.
int NAinstallSntpTime(char const * server_addr, unsigned int SyncTime, SNTP_CALLBACK_T callback);
This leads to checking into NAstartSntp. Here it looks like you must provide the call back, and somehow must hook into time() yourself. But you can specify both time servers here. There are no examples of using this to hook into time() that I can find.
int NAstartSntp(const char * priAddr, const char * secAddr, unsigned int SyncTime, unsigned int maxCtx, SNTP_CALLBACK_T callback);
So, does anyone have any good examples of how to have programmable time servers available for an application?
Furthermore, is there any way to determine if the time server could not be reached? For instance, would the time show up as 1970 or something like that?
Use int NArestartSntpServer(const char * priAddr, const char * secAddr); when you want to change any of primary or secondary server. The return codes will tell you if a server cannot be reached. Anyway, the time function will return 0 if there is no server (and 0 means 1970)
From the help file:
“If this function is not called from the user callback function, SNTP will restart after it finishes servicing the previous SNTP server (It usually restarts after re-synchronize time is expired).”
Since I didn’t set the “re-synchronize time” to begin with (SNTP is done from the BSP), how do I know how long this will take, or is there a way I can force it to happen sooner?
Do I have to call NAstartSntp to set the “re-synchronize time”? If so, what must I put into the callback? Or can I use NASntpTimeInstall and leave out the callback, and have the system work the same as without, as implied here?
“If, on the other hand, no user’s callback function is provided, then the system will be responsible to accessing and returning the time.”
Thanks for the help, and any additional help.
This is what I use for my system. I call this function from a web page when pressing Submit and when the page reloads it already shows the new time. I didn’t provide any callback function, the time_t time (time_t*); function (from time.h) works just fine