2020-10-08 07:28:57 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "configuration.h"
|
|
|
|
|
#include "sys/time.h"
|
|
|
|
|
#include <Arduino.h>
|
|
|
|
|
|
2020-10-08 07:46:20 +08:00
|
|
|
enum RTCQuality {
|
2022-04-27 11:05:08 +02:00
|
|
|
|
2020-10-08 07:46:20 +08:00
|
|
|
/// We haven't had our RTC set yet
|
|
|
|
|
RTCQualityNone = 0,
|
|
|
|
|
|
2022-04-27 11:05:08 +02:00
|
|
|
/// We got time from an onboard peripheral after boot.
|
|
|
|
|
RTCQualityDevice = 1,
|
|
|
|
|
|
2020-10-08 07:46:20 +08:00
|
|
|
/// Some other node gave us a time we can use
|
2022-04-27 11:05:08 +02:00
|
|
|
RTCQualityFromNet = 2,
|
2020-10-08 07:46:20 +08:00
|
|
|
|
2021-12-28 19:24:10 -08:00
|
|
|
/// Our time is based on NTP
|
2022-10-14 19:12:55 -05:00
|
|
|
RTCQualityNTP = 3,
|
2021-12-28 19:24:10 -08:00
|
|
|
|
2020-10-08 07:46:20 +08:00
|
|
|
/// Our time is based on our own GPS
|
2022-04-27 11:05:08 +02:00
|
|
|
RTCQualityGPS = 4
|
2020-10-08 07:46:20 +08:00
|
|
|
};
|
|
|
|
|
|
2025-07-07 19:35:57 -05:00
|
|
|
/// The RTC set result codes
|
|
|
|
|
/// Used to indicate the result of an attempt to set the RTC.
|
|
|
|
|
enum RTCSetResult {
|
|
|
|
|
RTCSetResultNotSet = 0, ///< RTC was set successfully
|
|
|
|
|
RTCSetResultSuccess = 1, ///< RTC was set successfully
|
|
|
|
|
RTCSetResultInvalidTime = 3, ///< The provided time was invalid (e.g., before the build epoch)
|
|
|
|
|
RTCSetResultError = 4 ///< An error occurred while setting the RTC
|
|
|
|
|
};
|
|
|
|
|
|
2020-10-08 07:46:20 +08:00
|
|
|
RTCQuality getRTCQuality();
|
2020-10-08 07:28:57 +08:00
|
|
|
|
2024-08-13 06:56:20 -05:00
|
|
|
extern uint32_t lastSetFromPhoneNtpOrGps;
|
|
|
|
|
|
2020-10-08 07:28:57 +08:00
|
|
|
/// If we haven't yet set our RTC this boot, set it from a GPS derived time
|
2025-07-07 19:35:57 -05:00
|
|
|
RTCSetResult perhapsSetRTC(RTCQuality q, const struct timeval *tv, bool forceUpdate = false);
|
|
|
|
|
RTCSetResult perhapsSetRTC(RTCQuality q, struct tm &t);
|
2020-10-08 07:28:57 +08:00
|
|
|
|
2024-04-21 14:40:47 -05:00
|
|
|
/// Return a string name for the quality
|
|
|
|
|
const char *RtcName(RTCQuality quality);
|
|
|
|
|
|
2020-10-08 07:46:20 +08:00
|
|
|
/// Return time since 1970 in secs. While quality is RTCQualityNone we will be returning time based at zero
|
2024-04-08 00:01:44 +02:00
|
|
|
uint32_t getTime(bool local = false);
|
2020-10-08 07:28:57 +08:00
|
|
|
|
2020-10-08 07:46:20 +08:00
|
|
|
/// Return time since 1970 in secs. If quality is RTCQualityNone return zero
|
2024-04-08 00:01:44 +02:00
|
|
|
uint32_t getValidTime(RTCQuality minQuality, bool local = false);
|
2020-10-08 07:28:57 +08:00
|
|
|
|
2021-03-20 10:22:06 +08:00
|
|
|
void readFromRTC();
|
|
|
|
|
|
2024-04-09 00:26:23 +02:00
|
|
|
time_t gm_mktime(struct tm *tm);
|
|
|
|
|
|
2021-03-20 10:22:06 +08:00
|
|
|
#define SEC_PER_DAY 86400
|
|
|
|
|
#define SEC_PER_HOUR 3600
|
2024-07-31 19:56:06 +08:00
|
|
|
#define SEC_PER_MIN 60
|