Skip to content

gs: Fix LNS-initiated time transfers for Basic Station#7858

Closed
reissjason wants to merge 1 commit intoTheThingsNetwork:v3.36from
MultiTechSystems:fix/4852-lns-time-transfer
Closed

gs: Fix LNS-initiated time transfers for Basic Station#7858
reissjason wants to merge 1 commit intoTheThingsNetwork:v3.36from
MultiTechSystems:fix/4852-lns-time-transfer

Conversation

@reissjason
Copy link

Summary

References #4852

All Basic Station gateways send timesync requests to correlate the concentrator counter with GPS time, regardless of PPS/GPS availability. The current code incorrectly assumes that a gateway sending a timesync request has PPS and disables LNS-initiated time transfers for that session. This prevents non-GPS gateways from receiving periodic time correlation updates.

Additionally, the LNS-initiated time transfer ticker passes nil for both gpsTime and concentratorTime (with a TODO referencing #4852), so even when transfers are enabled, only MuxTime is sent — never the xtime+gpstime pair needed for direct concentrator-to-GPS mapping.

Changes

  • Do not disable LNS-initiated time transfers when a gateway sends a timesync request — both round-trip and direct mapping modes are needed.
  • Track xtime and gpstime from recent uplinks (jreq/updf) in session state.
  • Use uplink-derived xtime+gpstime (extrapolated to current time) in LNS-initiated time transfers, per the Basic Station protocol specification.

For GPS-equipped gateways, this enables the full direct concentrator↔GPS time mapping that #4852 describes (for frames from the same gateway). For non-GPS gateways, the round-trip timesync mechanism remains the primary time source, and the fix ensures it stays active.

Note: This is a partial fix for #4852. The full cross-gateway GPS time propagation (transferring GPS time from a PPS-equipped gateway to a non-GPS gateway via overlapping frames) requires frame deduplication at the Gateway Server layer, which is out of scope for this change.

Motivation

We are developing Class B Network-Assisted Synchronization (NAS) for LoRaWAN, which uses multicast ping slot downlinks on non-GPS gateways (MultiTech MTCAP3 with cellular backhaul). Accurate timesync is critical for ping slot TX scheduling. Without this fix, the LNS disables time transfers after the first timesync exchange, degrading the concentrator↔GPS correlation over time.

Testing

  • Existing TestTransferTime passes unchanged.
  • New TestTimeSyncDoesNotDisableTransferTime: Verifies that TransferTime produces output after a timesync request (previously it returned nil).
  • New TestTransferTimeWithUplinkXTime: Verifies the end-to-end flow of storing uplink timing and using it in time transfers, for both GPS and non-GPS gateways.
  • New TestLastUplink: Unit test for UpdateLastUplink/GetLastUplink session state round-trip.
  • Full go test ./pkg/gatewayserver/io/semtechws/... passes with zero regressions.
Regressions

Not expected. The change is additive — time transfers that were previously disabled are now enabled, and previously nil arguments are now populated when uplink data is available.

Checklist

  • Scope: The referenced issue is addressed, there are no unrelated changes.
  • Compatibility: The changes are backwards compatible with existing API, storage, configuration and CLI.
  • Documentation: Relevant documentation is added or updated.
  • Changelog: Significant fixes are added to CHANGELOG.md.
  • Commits: Commit messages follow guidelines in CONTRIBUTING.md, there are no fixup commits left.

Made with Cursor

The timesync request handler incorrectly assumed that a gateway sending
a timesync request has access to a PPS source, and disabled LNS-initiated
time transfers for that gateway. All Basic Station gateways send timesync
requests regardless of PPS/GPS availability, as it is the standard
mechanism for correlating the concentrator counter with GPS time.

This fix:
- Removes the incorrect PPS assumption and keeps time transfers enabled
  after receiving a timesync request
- Tracks xtime and gpstime from recent uplinks in session state
- Uses uplink-derived xtime+gpstime in LNS-initiated time transfers
  per the Basic Station protocol specification

For GPS-equipped gateways, this enables direct concentrator-to-GPS time
mapping. For non-GPS gateways (which report gpstime=0 in uplinks), the
round-trip timesync mechanism remains the primary time source, and
LNS-initiated transfers continue providing MuxTime updates.

References TheThingsNetwork#4852.

Made-with: Cursor
@reissjason reissjason requested a review from a team as a code owner March 17, 2026 17:47
@reissjason reissjason requested a review from vlasebian March 17, 2026 17:47
Copy link
Member

@johanstokking johanstokking left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @reissjason, one small thing and mostly one question.

Comment on lines +345 to +346
if time.Since(rxAt) < 10*time.Second {
elapsed := time.Since(rxAt)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if time.Since(rxAt) < 10*time.Second {
elapsed := time.Since(rxAt)
if elapsed := time.Since(rxAt); elapsed < maxTimeSyncAfterUplink {

And declare maxTimeSyncAfterUplink as const.

The time sync happens by default every 200 seconds. There might not be an uplink message in the last 10 seconds every period. On busy gateways, it may take long for time sync to happen.

What's the reasoning for the 10 seconds time since the last uplink? Clock drift?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like thia is only a partial fix. I'll close this one and make a new request once it ia complete.

@reissjason reissjason closed this Mar 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants