More explicit guards for attempting to set RTC (#4452)

* Guard against timesources from the mesh if we have good time

* Trunk

* Consider phone time in the past 24 hours authoritative as well

* Rename

* GPS can be null

* Declaration

* Remove RemoteHardware

* Explicitly remove GPS

* Exclude GPS earlier for RAK2560
This commit is contained in:
Ben Meadors 2024-08-13 06:56:20 -05:00 committed by GitHub
parent 7740b4bccd
commit 464f270b12
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 23 additions and 3 deletions

View File

@ -12,6 +12,7 @@ build_flags =
-flto -flto
-Isrc/platform/stm32wl -g -Isrc/platform/stm32wl -g
-DMESHTASTIC_MINIMIZE_BUILD -DMESHTASTIC_MINIMIZE_BUILD
-DMESHTASTIC_EXCLUDE_GPS
-DDEBUG_MUTE -DDEBUG_MUTE
; -DVECT_TAB_OFFSET=0x08000000 ; -DVECT_TAB_OFFSET=0x08000000
-DconfigUSE_CMSIS_RTOS_V2=1 -DconfigUSE_CMSIS_RTOS_V2=1
@ -21,7 +22,7 @@ build_flags =
-fdata-sections -fdata-sections
build_src_filter = build_src_filter =
${arduino_base.build_src_filter} -<platform/esp32/> -<nimble/> -<mesh/api/> -<mesh/wifi/> -<mesh/http/> -<modules/esp32> -<mesh/eth/> -<input> -<buzz> -<platform/nrf52> -<platform/portduino> -<platform/rp2040> -<mesh/raspihttp> ${arduino_base.build_src_filter} -<platform/esp32/> -<nimble/> -<mesh/api/> -<mesh/wifi/> -<mesh/http/> -<modules/esp32> -<mesh/eth/> -<input> -<buzz> -<modules/RemoteHardwareModule.cpp> -<platform/nrf52> -<platform/portduino> -<platform/rp2040> -<mesh/raspihttp>
board_upload.offset_address = 0x08000000 board_upload.offset_address = 0x08000000
upload_protocol = stlink upload_protocol = stlink

View File

@ -6,6 +6,7 @@
#include <time.h> #include <time.h>
static RTCQuality currentQuality = RTCQualityNone; static RTCQuality currentQuality = RTCQualityNone;
uint32_t lastSetFromPhoneNtpOrGps = 0;
RTCQuality getRTCQuality() RTCQuality getRTCQuality()
{ {
@ -121,6 +122,9 @@ bool perhapsSetRTC(RTCQuality q, const struct timeval *tv, bool forceUpdate)
if (shouldSet) { if (shouldSet) {
currentQuality = q; currentQuality = q;
lastSetMsec = now; lastSetMsec = now;
if (currentQuality >= RTCQualityNTP) {
lastSetFromPhoneNtpOrGps = now;
}
// This delta value works on all platforms // This delta value works on all platforms
timeStartMsec = now; timeStartMsec = now;

View File

@ -24,6 +24,8 @@ enum RTCQuality {
RTCQuality getRTCQuality(); RTCQuality getRTCQuality();
extern uint32_t lastSetFromPhoneNtpOrGps;
/// If we haven't yet set our RTC this boot, set it from a GPS derived time /// If we haven't yet set our RTC this boot, set it from a GPS derived time
bool perhapsSetRTC(RTCQuality q, const struct timeval *tv, bool forceUpdate = false); bool perhapsSetRTC(RTCQuality q, const struct timeval *tv, bool forceUpdate = false);
bool perhapsSetRTC(RTCQuality q, struct tm &t); bool perhapsSetRTC(RTCQuality q, struct tm &t);

View File

@ -90,7 +90,6 @@ bool PositionModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp, mes
// will always be an equivalent or lesser RTCQuality (RTCQualityNTP or RTCQualityNet). // will always be an equivalent or lesser RTCQuality (RTCQualityNTP or RTCQualityNet).
force = true; force = true;
#endif #endif
// Set from phone RTC Quality to RTCQualityNTP since it should be approximately so // Set from phone RTC Quality to RTCQualityNTP since it should be approximately so
trySetRtc(p, isLocal, force); trySetRtc(p, isLocal, force);
} }
@ -126,14 +125,26 @@ void PositionModule::alterReceivedProtobuf(meshtastic_MeshPacket &mp, meshtastic
void PositionModule::trySetRtc(meshtastic_Position p, bool isLocal, bool forceUpdate) void PositionModule::trySetRtc(meshtastic_Position p, bool isLocal, bool forceUpdate)
{ {
if (hasQualityTimesource() && !isLocal) {
LOG_DEBUG("Ignoring time from mesh because we have a GPS, RTC, or Phone/NTP time source in the past day\n");
return;
}
struct timeval tv; struct timeval tv;
uint32_t secs = p.time; uint32_t secs = p.time;
tv.tv_sec = secs; tv.tv_sec = secs;
tv.tv_usec = 0; tv.tv_usec = 0;
perhapsSetRTC(isLocal ? RTCQualityNTP : RTCQualityFromNet, &tv, forceUpdate); perhapsSetRTC(isLocal ? RTCQualityNTP : RTCQualityFromNet, &tv, forceUpdate);
} }
bool PositionModule::hasQualityTimesource()
{
bool setFromPhoneOrNtpToday = (millis() - lastSetFromPhoneNtpOrGps) <= (SEC_PER_DAY * 1000UL);
bool hasGpsOrRtc = (gps && gps->isConnected()) || (rtc_found.address != ScanI2C::ADDRESS_NONE.address);
return hasGpsOrRtc || setFromPhoneOrNtpToday;
}
meshtastic_MeshPacket *PositionModule::allocReply() meshtastic_MeshPacket *PositionModule::allocReply()
{ {
if (precision == 0) { if (precision == 0) {

View File

@ -60,6 +60,7 @@ class PositionModule : public ProtobufModule<meshtastic_Position>, private concu
void trySetRtc(meshtastic_Position p, bool isLocal, bool forceUpdate = false); void trySetRtc(meshtastic_Position p, bool isLocal, bool forceUpdate = false);
uint32_t precision; uint32_t precision;
void sendLostAndFoundText(); void sendLostAndFoundText();
bool hasQualityTimesource();
const uint32_t minimumTimeThreshold = const uint32_t minimumTimeThreshold =
Default::getConfiguredOrDefaultMsScaled(config.position.broadcast_smart_minimum_interval_secs, 30, numOnlineNodes); Default::getConfiguredOrDefaultMsScaled(config.position.broadcast_smart_minimum_interval_secs, 30, numOnlineNodes);

View File

@ -6,6 +6,7 @@ board_check = true
build_flags = ${nrf52840_base.build_flags} -Ivariants/rak2560 -D RAK_4631 build_flags = ${nrf52840_base.build_flags} -Ivariants/rak2560 -D RAK_4631
-L "${platformio.libdeps_dir}/${this.__env__}/bsec2/src/cortex-m4/fpv4-sp-d16-hard" -L "${platformio.libdeps_dir}/${this.__env__}/bsec2/src/cortex-m4/fpv4-sp-d16-hard"
-DGPS_POWER_TOGGLE ; comment this line to disable triple press function on the user button to turn off gps entirely. -DGPS_POWER_TOGGLE ; comment this line to disable triple press function on the user button to turn off gps entirely.
-DMESHTASTIC_EXCLUDE_GPS=1
-DHAS_RAKPROT=1 ; Define if RAk OneWireSerial is used (disables GPS) -DHAS_RAKPROT=1 ; Define if RAk OneWireSerial is used (disables GPS)
build_src_filter = ${nrf52_base.build_src_filter} +<../variants/rak2560> +<mesh/eth/> +<mesh/api/> +<mqtt/> build_src_filter = ${nrf52_base.build_src_filter} +<../variants/rak2560> +<mesh/eth/> +<mesh/api/> +<mqtt/>
lib_deps = lib_deps =

View File

@ -227,7 +227,7 @@ SO GPIO 39/TXEN MAY NOT BE DEFINED FOR SUCCESSFUL OPERATION OF THE SX1262 - TG
// #define GPS_TX_PIN PIN_SERIAL2_TX // #define GPS_TX_PIN PIN_SERIAL2_TX
// #define PIN_GPS_EN PIN_3V3_EN // #define PIN_GPS_EN PIN_3V3_EN
// Disable GPS // Disable GPS
#define MESHTASTIC_EXCLUDE_GPS 1 // #define MESHTASTIC_EXCLUDE_GPS 1
// Define pin to enable GPS toggle (set GPIO to LOW) via user button triple press // Define pin to enable GPS toggle (set GPIO to LOW) via user button triple press
// RAK12002 RTC Module // RAK12002 RTC Module