55#include < GCS_MAVLink/GCS.h>
66#include < time.h>
77
8+ #define DEBUG_RTC_SHIFT 1
9+
10+ #if DEBUG_RTC_SHIFT
11+ #include < AP_Logger/AP_Logger.h>
12+ #endif
13+
814extern const AP_HAL ::HAL & hal;
915
1016AP_RTC ::AP_RTC ()
@@ -43,9 +49,19 @@ void AP_RTC::set_utc_usec(uint64_t time_utc_usec, source_type type)
4349{
4450 const uint64_t oldest_acceptable_date_us = 1640995200ULL *1000 *1000 ; // 2022-01-01 0:00
4551
46- if (type >= rtc_source_type) {
47- // e.g. system-time message when we've been set by the GPS
48- return ;
52+ // only allow time to be moved forward from the same sourcetype
53+ // while the vehicle is disarmed:
54+ if (hal.util ->get_soft_armed ()) {
55+ if (type >= rtc_source_type) {
56+ // e.g. system-time message when we've been set by the GPS
57+ return ;
58+ }
59+ } else {
60+ // vehicle is disarmed; accept (e.g.) GPS time source updates
61+ if (type > rtc_source_type) {
62+ // e.g. system-time message when we've been set by the GPS
63+ return ;
64+ }
4965 }
5066
5167 // check it's from an allowed sources:
@@ -66,6 +82,11 @@ void AP_RTC::set_utc_usec(uint64_t time_utc_usec, source_type type)
6682 }
6783 WITH_SEMAPHORE (rsem);
6884
85+ #if DEBUG_RTC_SHIFT
86+ uint64_t old_utc = 0 ;
87+ UNUSED_RESULT (get_utc_usec (old_utc));
88+ #endif
89+
6990 rtc_shift = tmp;
7091
7192 // update hardware clock:
@@ -79,6 +100,32 @@ void AP_RTC::set_utc_usec(uint64_t time_utc_usec, source_type type)
79100 // update signing timestamp
80101 GCS_MAVLINK::update_signing_timestamp (time_utc_usec);
81102#endif
103+
104+ #if DEBUG_RTC_SHIFT
105+ uint64_t new_utc = 0 ;
106+ UNUSED_RESULT (get_utc_usec (new_utc));
107+ if (old_utc != new_utc) {
108+ if (AP::logger ().should_log (0xFFFF )){
109+ // log to AP_Logger
110+ // @LoggerMessage: RTC
111+ // @Description: Information about RTC clock resets
112+ // @Field: TimeUS: Time since system startup
113+ // @Field: old: old time
114+ // @Field: new: new time
115+ // @Field: in: new argument time
116+ AP::logger ().WriteStreaming (
117+ " RTC" ,
118+ " TimeUS,old_utc,new_utc" ,
119+ " sss" ,
120+ " FFF" ,
121+ " QQQ" ,
122+ AP_HAL::micros64 (),
123+ old_utc,
124+ new_utc
125+ );
126+ }
127+ }
128+ #endif
82129}
83130
84131bool AP_RTC::get_utc_usec (uint64_t &usec) const
0 commit comments