Skip to content

Commit

Permalink
More explicit guards for attempting to set RTC (#4452)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
thebentern authored Aug 13, 2024
1 parent 7740b4b commit 464f270
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 3 deletions.
3 changes: 2 additions & 1 deletion arch/stm32/stm32.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ build_flags =
-flto
-Isrc/platform/stm32wl -g
-DMESHTASTIC_MINIMIZE_BUILD
-DMESHTASTIC_EXCLUDE_GPS
-DDEBUG_MUTE
; -DVECT_TAB_OFFSET=0x08000000
-DconfigUSE_CMSIS_RTOS_V2=1
Expand All @@ -21,7 +22,7 @@ build_flags =
-fdata-sections

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
upload_protocol = stlink
Expand Down
4 changes: 4 additions & 0 deletions src/gps/RTC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <time.h>

static RTCQuality currentQuality = RTCQualityNone;
uint32_t lastSetFromPhoneNtpOrGps = 0;

RTCQuality getRTCQuality()
{
Expand Down Expand Up @@ -121,6 +122,9 @@ bool perhapsSetRTC(RTCQuality q, const struct timeval *tv, bool forceUpdate)
if (shouldSet) {
currentQuality = q;
lastSetMsec = now;
if (currentQuality >= RTCQualityNTP) {
lastSetFromPhoneNtpOrGps = now;
}

// This delta value works on all platforms
timeStartMsec = now;
Expand Down
2 changes: 2 additions & 0 deletions src/gps/RTC.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ enum RTCQuality {

RTCQuality getRTCQuality();

extern uint32_t lastSetFromPhoneNtpOrGps;

/// 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, struct tm &t);
Expand Down
13 changes: 12 additions & 1 deletion src/modules/PositionModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ bool PositionModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp, mes
// will always be an equivalent or lesser RTCQuality (RTCQualityNTP or RTCQualityNet).
force = true;
#endif

// Set from phone RTC Quality to RTCQualityNTP since it should be approximately so
trySetRtc(p, isLocal, force);
}
Expand Down Expand Up @@ -126,14 +125,26 @@ void PositionModule::alterReceivedProtobuf(meshtastic_MeshPacket &mp, meshtastic

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;
uint32_t secs = p.time;

tv.tv_sec = secs;
tv.tv_usec = 0;

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()
{
if (precision == 0) {
Expand Down
1 change: 1 addition & 0 deletions src/modules/PositionModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class PositionModule : public ProtobufModule<meshtastic_Position>, private concu
void trySetRtc(meshtastic_Position p, bool isLocal, bool forceUpdate = false);
uint32_t precision;
void sendLostAndFoundText();
bool hasQualityTimesource();

const uint32_t minimumTimeThreshold =
Default::getConfiguredOrDefaultMsScaled(config.position.broadcast_smart_minimum_interval_secs, 30, numOnlineNodes);
Expand Down
1 change: 1 addition & 0 deletions variants/rak2560/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ board_check = true
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"
-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)
build_src_filter = ${nrf52_base.build_src_filter} +<../variants/rak2560> +<mesh/eth/> +<mesh/api/> +<mqtt/>
lib_deps =
Expand Down
2 changes: 1 addition & 1 deletion variants/rak2560/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 PIN_GPS_EN PIN_3V3_EN
// 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

// RAK12002 RTC Module
Expand Down

0 comments on commit 464f270

Please sign in to comment.