|
|
|
@ -4,6 +4,7 @@
@@ -4,6 +4,7 @@
|
|
|
|
|
#include <AP_Math/AP_Math.h> |
|
|
|
|
#include <GCS_MAVLink/GCS.h> |
|
|
|
|
#include <time.h> |
|
|
|
|
#include <stdio.h> |
|
|
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal; |
|
|
|
|
|
|
|
|
@ -41,20 +42,40 @@ const AP_Param::GroupInfo AP_RTC::var_info[] = {
@@ -41,20 +42,40 @@ const AP_Param::GroupInfo AP_RTC::var_info[] = {
|
|
|
|
|
|
|
|
|
|
void AP_RTC::set_utc_usec(uint64_t time_utc_usec, source_type type) |
|
|
|
|
{ |
|
|
|
|
bool need_prt = false; |
|
|
|
|
static uint32_t last_1s = AP_HAL::millis(); |
|
|
|
|
if (AP_HAL::millis() - last_1s > 5000) |
|
|
|
|
{ |
|
|
|
|
need_prt = true; |
|
|
|
|
last_1s = AP_HAL::millis(); |
|
|
|
|
} |
|
|
|
|
//////////////////////////////////////
|
|
|
|
|
const uint64_t oldest_acceptable_date_us = 1640995200ULL*1000*1000; // 2022-01-01 0:00
|
|
|
|
|
|
|
|
|
|
if (type >= rtc_source_type) { |
|
|
|
|
// e.g. system-time message when we've been set by the GPS
|
|
|
|
|
if (need_prt) |
|
|
|
|
{ |
|
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "type{%d} >= rtc_source_type{%d}",type , (uint8_t)rtc_source_type); |
|
|
|
|
} |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// check it's from an allowed sources:
|
|
|
|
|
if (!(allowed_types & (1<<type))) { |
|
|
|
|
if (need_prt) |
|
|
|
|
{ |
|
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "!(allowed_types{%d}& (1<<type{%d}))",(uint8_t)allowed_types,(uint8_t)type); |
|
|
|
|
} |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// don't allow old times
|
|
|
|
|
if (time_utc_usec < oldest_acceptable_date_us) { |
|
|
|
|
if (need_prt) |
|
|
|
|
{ |
|
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "time_utc_usec < oldest_acceptable_date_us"); |
|
|
|
|
} |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -62,6 +83,10 @@ void AP_RTC::set_utc_usec(uint64_t time_utc_usec, source_type type)
@@ -62,6 +83,10 @@ void AP_RTC::set_utc_usec(uint64_t time_utc_usec, source_type type)
|
|
|
|
|
const int64_t tmp = int64_t(time_utc_usec) - int64_t(now); |
|
|
|
|
if (tmp < rtc_shift) { |
|
|
|
|
// can't allow time to go backwards, ever
|
|
|
|
|
if (need_prt) |
|
|
|
|
{ |
|
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "tmp{%lld} < rtc_shift{%lld}",tmp , rtc_shift); |
|
|
|
|
} |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
WITH_SEMAPHORE(rsem); |
|
|
|
@ -75,6 +100,10 @@ void AP_RTC::set_utc_usec(uint64_t time_utc_usec, source_type type)
@@ -75,6 +100,10 @@ void AP_RTC::set_utc_usec(uint64_t time_utc_usec, source_type type)
|
|
|
|
|
|
|
|
|
|
rtc_source_type = type; |
|
|
|
|
|
|
|
|
|
if (need_prt) |
|
|
|
|
{ |
|
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "type:%d,in utc:%lld,rtc_shift:%lld",type,time_utc_usec,rtc_shift); |
|
|
|
|
} |
|
|
|
|
#if HAL_GCS_ENABLED |
|
|
|
|
// update signing timestamp
|
|
|
|
|
GCS_MAVLINK::update_signing_timestamp(time_utc_usec); |
|
|
|
@ -205,6 +234,26 @@ uint32_t AP_RTC::get_time_utc(int32_t hour, int32_t min, int32_t sec, int32_t ms
@@ -205,6 +234,26 @@ uint32_t AP_RTC::get_time_utc(int32_t hour, int32_t min, int32_t sec, int32_t ms
|
|
|
|
|
return static_cast<uint32_t>(total_delay_ms); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
char* AP_RTC::get_rtc_date(void) |
|
|
|
|
{ |
|
|
|
|
uint16_t year;uint8_t mon;uint8_t mday;uint8_t hour; uint8_t min; uint8_t sec; |
|
|
|
|
tm localtime_tm {}; // year is relative to 1900
|
|
|
|
|
uint64_t time_usec = 0; |
|
|
|
|
if (get_utc_usec(time_usec)) { // may fail, leaving time_unix at 0
|
|
|
|
|
const time_t time_sec = time_usec / 1000000; |
|
|
|
|
localtime_tm = *gmtime(&time_sec); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
year = uint16_t(localtime_tm.tm_year + 1900); // tm_year is relative to year 1900
|
|
|
|
|
mon = uint8_t(localtime_tm.tm_mon + 1); // MSP requires [1-12] months
|
|
|
|
|
mday = uint8_t(localtime_tm.tm_mday); |
|
|
|
|
hour = uint8_t(localtime_tm.tm_hour); |
|
|
|
|
min = uint8_t(localtime_tm.tm_min); |
|
|
|
|
sec = uint8_t(localtime_tm.tm_sec); |
|
|
|
|
static char buf[50]; |
|
|
|
|
snprintf(buf, sizeof(buf), "%04d%02d%02d%02d%02d%02d",year,mon,mday,hour,min,sec); |
|
|
|
|
return buf; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
mktime replacement from Samba |
|
|
|
|