Skip to content

Commit 2ca3cdf

Browse files
authored
Fix RTC time injection and consolidate position logic (#5396)
* Fix RTC time injection and consolidate position logic * Comment out unused var warning * Backerds
1 parent 485c371 commit 2ca3cdf

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

src/mesh/NodeDB.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ NodeDB::NodeDB()
114114
uint32_t channelFileCRC = crc32Buffer(&channelFile, sizeof(channelFile));
115115

116116
int saveWhat = 0;
117-
bool hasUniqueId = false;
117+
// bool hasUniqueId = false;
118118
// Get device unique id
119119
#if defined(ARCH_ESP32) && defined(ESP_EFUSE_OPTIONAL_UNIQUE_ID)
120120
uint32_t unique_id[4];

src/modules/PositionModule.cpp

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,20 @@ bool PositionModule::hasQualityTimesource()
146146
#if MESHTASTIC_EXCLUDE_GPS
147147
bool hasGpsOrRtc = (rtc_found.address != ScanI2C::ADDRESS_NONE.address);
148148
#else
149-
bool hasGpsOrRtc = (gps && gps->isConnected()) || (rtc_found.address != ScanI2C::ADDRESS_NONE.address);
149+
bool hasGpsOrRtc = hasGPS() || (rtc_found.address != ScanI2C::ADDRESS_NONE.address);
150150
#endif
151151
return hasGpsOrRtc || setFromPhoneOrNtpToday;
152152
}
153153

154+
bool PositionModule::hasGPS()
155+
{
156+
#if MESHTASTIC_EXCLUDE_GPS
157+
return false;
158+
#else
159+
return gps && gps->isConnected();
160+
#endif
161+
}
162+
154163
meshtastic_MeshPacket *PositionModule::allocReply()
155164
{
156165
if (precision == 0) {
@@ -194,10 +203,21 @@ meshtastic_MeshPacket *PositionModule::allocReply()
194203
p.precision_bits = precision;
195204
p.has_latitude_i = true;
196205
p.has_longitude_i = true;
197-
p.time = getValidTime(RTCQualityNTP) > 0 ? getValidTime(RTCQualityNTP) : localPosition.time;
206+
// Always use NTP / GPS time if available
207+
if (getValidTime(RTCQualityNTP) > 0) {
208+
p.time = getValidTime(RTCQualityNTP);
209+
} else if (rtc_found.address != ScanI2C::ADDRESS_NONE.address) {
210+
LOG_INFO("Use RTC time for position");
211+
p.time = getValidTime(RTCQualityDevice);
212+
} else if (getRTCQuality() < RTCQualityNTP) {
213+
LOG_INFO("Strip low RTCQuality (%d) time from position", getRTCQuality());
214+
p.time = 0;
215+
}
198216

199217
if (config.position.fixed_position) {
200218
p.location_source = meshtastic_Position_LocSource_LOC_MANUAL;
219+
} else {
220+
p.location_source = localPosition.location_source;
201221
}
202222

203223
if (pos_flags & meshtastic_Config_PositionConfig_PositionFlags_ALTITUDE) {
@@ -242,20 +262,6 @@ meshtastic_MeshPacket *PositionModule::allocReply()
242262
p.has_ground_speed = true;
243263
}
244264

245-
// Strip out any time information before sending packets to other nodes - to keep the wire size small (and because other
246-
// nodes shouldn't trust it anyways) Note: we allow a device with a local GPS or NTP to include the time, so that devices
247-
// without can get time.
248-
if (getRTCQuality() < RTCQualityNTP) {
249-
LOG_INFO("Strip time %u from position", p.time);
250-
p.time = 0;
251-
} else if (rtc_found.address != ScanI2C::ADDRESS_NONE.address) {
252-
LOG_INFO("Use RTC time %u for position", p.time);
253-
p.time = getValidTime(RTCQualityDevice);
254-
} else {
255-
p.time = getValidTime(RTCQualityNTP);
256-
LOG_INFO("Provide time to mesh %u", p.time);
257-
}
258-
259265
LOG_INFO("Position reply: time=%i lat=%i lon=%i", p.time, p.latitude_i, p.longitude_i);
260266

261267
// TAK Tracker devices should send their position in a TAK packet over the ATAK port

src/modules/PositionModule.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class PositionModule : public ProtobufModule<meshtastic_Position>, private concu
6161
uint32_t precision;
6262
void sendLostAndFoundText();
6363
bool hasQualityTimesource();
64+
bool hasGPS();
6465

6566
const uint32_t minimumTimeThreshold =
6667
Default::getConfiguredOrDefaultMs(config.position.broadcast_smart_minimum_interval_secs, 30);

0 commit comments

Comments
 (0)