Add NTP update state. Refactoring.
This commit is contained in:
@@ -5,20 +5,32 @@
|
||||
#include <time.h>
|
||||
#include "udp_logger.h"
|
||||
|
||||
#define NTP_MAX_UPDATE_TIME_US (500 * 1000) // 500ms max update time
|
||||
typedef enum
|
||||
{
|
||||
NTP_UPDATE_FAILED = 0,
|
||||
NTP_UPDATE_OK = 1,
|
||||
NTP_UPDATE_PENDING = 2,
|
||||
NTP_UPDATE_RETRY_DELAY = 3,
|
||||
NTP_UPDATE_TOO_EARLY = 4,
|
||||
} ntp_update_state;
|
||||
|
||||
class TimeManager
|
||||
{
|
||||
#define NTP_MAX_UPDATE_TIME_US (500 * 1000) // 500ms max update time
|
||||
|
||||
public:
|
||||
struct tm time_info(void);
|
||||
bool ntp_sync_successful(void) const; // was there a NTP sync once?
|
||||
bool ntp_time_update(bool init = false);
|
||||
ntp_update_state ntp_time_update(bool init = false);
|
||||
bool ntp_update_failed_prolonged(void) const; // indicates if maximum time since last NTP update was too long
|
||||
void log_time(struct tm time_info) const; // log local_time
|
||||
struct tm time_info(void);
|
||||
void increment_time_now_local(void);
|
||||
void log_time() const; // log _time_info
|
||||
void log_time(struct tm time_info) const; // log argument time_info
|
||||
|
||||
int tm_min(void);
|
||||
int tm_hour(void);
|
||||
int tm_day(void);
|
||||
int tm_mon(void);
|
||||
int tm_year(void);
|
||||
bool tm_isdst(void); // true if summertime
|
||||
|
||||
@@ -30,18 +42,18 @@ public:
|
||||
UDPLogger *logger);
|
||||
|
||||
private:
|
||||
void set_up_ntp(void) const; // set up NTP server
|
||||
void set_up_timer_isr(void) const; // set up timer interrupt
|
||||
const char *_tz; // timezone
|
||||
const char *_ntp_server; // used ntp server
|
||||
UDPLogger *_logger; // logger instance
|
||||
struct tm _time_info; // structure tm holds time information
|
||||
time_t _time_now_local = 0; // local timer value, updated by timer interrupt and synced by NTP when needed
|
||||
time_t _time_now_ntp = 0; // NTP timer value, seconds since Epoch (1970) - UTC, only synced by NTP request.
|
||||
uint32 _ntp_max_offline_time_s; // maximum time in seconds which is considered ok since last NTP update
|
||||
uint32 _ntp_update_period_s; // NTP request update period in seconds
|
||||
uint32 _ntp_retry_delay_us; // minimum retry delay in us between two NTP requests
|
||||
uint32 _ntp_sync_timestamp_us = 0; // timestamp of last successful ntp update
|
||||
void set_up_ntp(void) const; // set up NTP server
|
||||
void set_up_timer_isr(void) const; // set up timer interrupt
|
||||
const char *_tz; // timezone
|
||||
const char *_ntp_server; // used ntp server
|
||||
UDPLogger *_logger; // logger instance
|
||||
struct tm _time_info = {0, 0, 0, 0, 0, 0, 0, 0, 0}; // structure tm holds time information
|
||||
time_t _time_now_local = 0; // local timer value, updated by timer interrupt and synced by NTP when needed
|
||||
time_t _time_now_ntp = 0; // NTP timer value, seconds since Epoch (1970) - UTC, only synced by NTP request.
|
||||
uint32 _ntp_max_offline_time_s; // maximum time in seconds which is considered ok since last NTP update
|
||||
uint32 _ntp_update_period_s; // NTP request update period in seconds
|
||||
uint32 _ntp_retry_delay_us; // minimum retry delay in us between two NTP requests
|
||||
uint32 _ntp_sync_timestamp_us = 0; // timestamp of last successful ntp update
|
||||
};
|
||||
|
||||
inline void TimeManager::increment_time_now_local(void)
|
||||
|
||||
@@ -71,7 +71,7 @@
|
||||
// NTP macros
|
||||
#define BUILD_YEAR (__DATE__ + 7) // Will expand to current year at compile time as string.
|
||||
#define NTP_MININUM_RX_YEAR (atoi(BUILD_YEAR) - 1) // Will expand to current year minus one at compile time.
|
||||
#define NTP_MININUM_YEAR (1900) // NTP minimum year is 1900
|
||||
#define NTP_START_YEAR (1900) // NTP minimum year is 1900
|
||||
#define NTP_UPDATE_PERIOD_S (12 * 3600) // 12h period between updates
|
||||
#define NTP_RETRY_DELAY_US (10 * 1000 * 1000) // 10s retry delay time between failed NTP requests
|
||||
#define NTP_MAX_OFFLINE_TIME_S (7 * 24 * 3600) // Watchdog value, maxmimum offline time before a restart is triggered
|
||||
|
||||
Reference in New Issue
Block a user