Skip to content

Gps on demand - #3877

Open
dhandzic wants to merge 2 commits into
meshcore-dev:devfrom
dhandzic:gps-on-demand
Open

dhandzic wants to merge 2 commits into
meshcore-dev:devfrom
dhandzic:gps-on-demand

Conversation

@dhandzic

@dhandzic dhandzic commented Nov 19, 2026 •

Copy link
Copy Markdown

Companion: GPS on demand for permitted location telemetry requests

Motivation

GPS-equipped companions (e.g. T1000-E) currently have two options: GPS permanently on (battery life drops from weeks to days) or GPS off (a location telemetry request returns nothing, or a stale/manual position on some boards).

Use case: a volunteer emergency-messaging network in rural Sweden. Many users work in the forest, and when someone sends an alert the receiving station needs their real position, but nobody can afford to keep GPS on all day. The station is a contact with location permission and polls the sender's telemetry after receiving an alert.

This PR adds an opt-in mode: GPS stays off, and is powered up only when a contact that is allowed to read location requests telemetry.

Behaviour

New companion prefs (persisted in the gps section of the new config format):

key custom var default meaning
gps.od gps_od 0 enable GPS on demand
gps.od_to gps_od_to 120 max seconds to search for a fix (10–600)

The mode is only active when gps_on_demand == 1, gps_enabled == 0, gps_period == 0 (no periodic acquisition) and the board has a LocationProvider.

When a REQ_TYPE_GET_TELEMETRY_DATA arrives and the requester's effective permissions include TELEM_PERM_LOCATION:

  1. Immediate reply (unchanged protocol): base and environment telemetry is sent at once, without location. The requester sees the node is alive and gets the battery level.
  2. GPS is powered up via the existing sensors.setSettingValue("gps", "1"), and the request (requester key, tag, permissions) is queued (max 4).
  3. As soon as a fresh fix is available (isValid() and a GPS timestamp newer than at power-up, so stale NMEA state is ignored), plus 2 s for sensors.loop() to update node_lat/lon, a second PAYLOAD_TYPE_RESPONSE with full telemetry including location is sent to each queued requester. It is sent direct if a path is known, flood-scoped otherwise, and reuses the original tag.
  4. GPS stays on for 30 s (follow-up requests are then answered at once with location), then powers down. If no fix arrives within gps_od_to, GPS powers down and nothing more is sent. A new request while GPS is still searching restarts the search window.

Permissions are evaluated exactly as today (telemetry_mode_loc plus per-contact flags plus the request's inverse mask). Nobody without location permission can wake the GPS.

Compatibility

  • Off by default. With gps_od = 0 the code path is identical to today. Telemetry reply building was only moved into buildTelemetryReply(), with no change in content.
  • Requester side needs no change. The second response carries the original tag:
    • if the requester is still waiting (pending_telemetry matches, e.g. the first reply was lost), it is handled as a normal telemetry response;
    • otherwise it falls through to the existing unhandled response path and reaches the app as PUSH_CODE_BINARY_RESPONSE (tag + LPP). This is the same path already used for pushed telemetry, so host tools (e.g. meshcore_py) can decode it.
  • Users who switch GPS on permanently while a demand is pending take over seamlessly: the on-demand logic stops managing the GPS.
  • The new pref fields are only in the new key/value config format, so the legacy binary prefs layout is untouched.

Files

  • src/helpers/CompanionNodePrefs.h: two new fields plus serializer defs
  • examples/companion_radio/MyMesh.h: demand queue and state
  • examples/companion_radio/MyMesh.cpp:
    • buildTelemetryReply() refactor
    • gpsOnDemandApplies(), queueGpsDemand(), checkGpsOnDemand() (called from loop()), stopGpsOnDemand()
    • custom vars gps_od / gps_od_to (get and set)

Testing

Tested on T1000-E (t1000e_companion_radio_usb build; _ble builds and runs) as responder, with a Wio Tracker L1 USB companion as requester, gps_od=1, gps_od_to=120. GPS on/off state was observed through the gps custom var.

Case Result
Location-permitted request, GPS off, outdoors 1st reply after 1.0 s without location; 2nd reply with location after 4–55 s (first fix 46 s, warm 10 s, recent fix 4 s)
Repeat request within 30 s hold location in 1st reply (1.0 s), no 2nd reply
GPS power-down after fix off ~30 s after 2nd reply (total GPS on-time 84 s for one position)
Indoors, no fix 1st reply only; GPS off after 120 s (gps_od_to)
Requester without base permission no reply at all (unchanged behaviour)
Requester with base but no location permission 1st reply without location; GPS stays off
gps_od = 0 unchanged: immediate reply with last stored position (may be stale), GPS stays off
gps = 1 (permanently on) + gps_od = 1 location in 1st reply; GPS is not switched off by on-demand logic
Power cycle gps_od / gps_od_to persisted

The 2nd reply arrives with the original tag and reaches the app as PUSH_CODE_BINARY_RESPONSE; it decodes fine with meshcore_py lpp_parse().

Update

  • Rebased onto dev. Location on demand is skipped when gps_period (periodic acquisition) is set.
  • A new request while GPS is already searching restarts the search window.
  • Re-tested on dev (T1000-E): outdoors 2nd reply with location after 58 s and 37.5 s; indoors GPS off after gps_od_to.

Not yet tested

  • a board using EnvironmentSensorManager (e.g. Wio Tracker L1 as responder)
  • multiple simultaneous requesters
  • current measurement (my meter setup measured mostly the T1000-E charging current)

Side note: with gps_od = 0, the T1000-E answers location requests with the last stored position and no age indication, which can be hours old. For safety use cases that is a further argument for on-demand fixes.

Open questions for maintainers

  1. Would you prefer the deferred location to go through the new 1.18 telemetry push/subscription mechanism instead of a second response with the same tag?
  2. Is 4 queued requesters / 30 s hold reasonable, or should the hold time also be a pref?
  3. Should the immediate reply carry an explicit "location pending" marker (e.g. a reserved LPP channel) so apps can show "waiting for GPS…"?

Closes #3919

@dhandzic dhandzic mentioned this pull request Nov 23, 2026
@dhandzic
dhandzic changed the base branch from main to dev November 23, 2026 08:46

This branch has not been deployed

No deployments
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.

1 participant