mirror of
https://github.com/logicog/RTLPlayground.git
synced 2026-10-29 17:54:14 +08:00
The switch has no battery backed clock, only a seconds counter that starts at zero on every boot. An SNTP client keeps UTC as the time of the last answer plus the seconds counted since: "ntp server <name|ip>", "ntp interval <minutes>", "ntp timezone <+hh:mm>", "ntp dst off|eu|us", "ntp on|off", "ntp" and "time". A server given by name goes through the DNS resolver, an answer from an unsynchronised server is ignored, and a failed attempt is repeated after 30 seconds. The local time adds the time zone offset and, while the European or United States rule says so, one hour. The date is split with a shift and subtract division and a walk over the years, so no 32-bit division from the library ends up in the common area. The System page gets the NTP settings and shows the local time.
42 lines
1.1 KiB
C
42 lines
1.1 KiB
C
#ifndef _NTP_H_
|
|
#define _NTP_H_
|
|
|
|
#include <stdint.h>
|
|
#include "dns.h"
|
|
|
|
#define NTP_DST_OFF 0
|
|
#define NTP_DST_EU 1
|
|
#define NTP_DST_US 2
|
|
|
|
struct uip_udp_conn;
|
|
|
|
struct ntp_state {
|
|
uint8_t enabled;
|
|
char server[DNS_NAME_LEN]; /* host name or dotted address */
|
|
uint8_t addr[4]; /* address of the server last used */
|
|
uint16_t interval; /* minutes between two synchronisations */
|
|
int16_t offset; /* time zone, minutes east of UTC */
|
|
uint8_t dst; /* NTP_DST_* */
|
|
uint8_t phase;
|
|
uint8_t tries;
|
|
uint8_t synced;
|
|
uint8_t stratum;
|
|
uint32_t utc; /* seconds since 1970 UTC at the last synchronisation */
|
|
uint32_t at; /* uptime seconds at the last synchronisation */
|
|
uint32_t next; /* uptime seconds of the next synchronisation */
|
|
uint32_t sent; /* ticks when the request went out */
|
|
struct uip_udp_conn *conn;
|
|
};
|
|
|
|
extern __xdata struct ntp_state ntp_state;
|
|
|
|
void ntp_init(void) __banked;
|
|
void ntp_start(void) __banked;
|
|
void ntp_stop(void) __banked;
|
|
void ntp_show(void) __banked __reentrant;
|
|
void ntp_show_time(void) __banked __reentrant;
|
|
void ntp_callback(uint16_t lport) __banked __reentrant;
|
|
void ntp_parse(void) __banked __reentrant;
|
|
|
|
#endif
|