Files
MeshCore-Solo/examples/companion_radio/LiveTrack.h
MarekZegare4 57774d41f3 feat(companion): live location sharing, Locator geofencing, trail auto-pause
Squash merge of feat/location-beacon-alerts-autopause (v1.21).

Features:
- Live Location Sharing — broadcast position as movement-gated [LOC]
  messages to a channel or contact; live shares show as map pins with
  distance/bearing in Nearby Nodes and a status-bar indicator. [LOC] is
  parsed in DMs, channel messages and room messages; DM shares name the
  sender.
- Locator (geofence) — arm a geofence around a saved waypoint or a person
  (live [LOC] or last-known position), alert on arrive/leave or near/far,
  with an optional homing beeper (gated to arrive/both modes). Arm from
  Tools > Locator, Nearby Nodes, or Waypoints; target picker lists
  favourites first, clearable via a "None" entry. Active target is drawn
  as a flag on the map.
- One active target shared across Locator / Navigate / Map via a single
  resolver (resolvePersonPos / activeTargetPos) that prefers a live
  [LOC] share over the last-advertised GPS fix.
- Follow live contacts — Navigate to a live-sharing contact follows them
  as they move and adds an ETA line; quick-share your own position from
  the Map.
- Map & status-bar upgrades — home mini-map gets a north marker, scale
  tick, and a connected trail line (was disconnected dots); status line
  shows tracked-node count, an arrow + distance to the active Locator/Nav
  target (falling back to the nearest live-tracked contact); GPS fix icon
  in the top status bar, shown only on GPS boards with GPS enabled.
- Trail auto-pause — recording freezes on stops (banking elapsed time,
  breaking the map line across the idle gap) and resumes on movement
  without ending the session.
- Streaming trail simplification — GPS points are simplified in-stream
  via a fixed-corridor (Reumann-Witkam) pass tuned for fidelity: straight
  runs collapse to their endpoints, curves stay bounded to the Min-dist
  tolerance, so the 512-point buffer covers a far longer route than a
  flat point budget would suggest.
- Collapsible Tools (Location / Comms / System sections, fold-in-place
  like Settings) and page-indicator icons on the home carousel.
- Waypoint coordinate editor — add a waypoint by scroll-editing lat/lon
  digit by digit.

Fixes:
- Critical: low-heap hang and contact loss on RAM-tight builds. Halved
  message-history scrollback rings (recovering ~14 KB free heap) and
  made contacts/channels/prefs persistence atomic (temp file + rename),
  so an interrupted save can no longer corrupt or wipe the store.
- Serial.write() bounded so a stalled USB host can't hang the device.
- Nearby Nodes: live [LOC] senders respect the type filter, sort by
  shared position, and the list refreshes so live shares bubble up.
- Map: live contacts are labelled before waypoints.
- GPS status icon hidden when GPS is off in Settings.
- Splash screen no longer truncates a pre-release tag's own dash (e.g.
  v1.21-rc1) when stripping the build's commit-hash suffix.
- Null-guarded the Locator target picker; clamped loc-share channel
  index on load.

Under the hood:
- -Os size optimisation on the e-ink and GAT562 30S solo envs.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-25 19:04:21 +02:00

120 lines
4.3 KiB
C++

#pragma once
#include <Arduino.h>
#include <stdint.h>
#include <string.h>
// RAM-only table of positions other nodes have shared via [LOC] messages
// (see geo::parseLocShare / LOCATION_MSG_TAG). Lets the device show a live
// bearing/distance to people who broadcast their location on a DM or channel,
// independent of how often they advert.
//
// Keying:
// DM share → keyed by pubkey prefix, verified == true (reliable).
// Channel share → keyed by sender name, verified == false (best-effort:
// channel sender names are unsigned and not unique).
//
// The table never persists (positions go stale fast) and entries expire after
// EXPIRY_SECS without a fresh update. When full, the oldest entry is evicted.
class LiveTrackStore {
public:
static const int CAPACITY = 16; // max simultaneously tracked nodes
static const uint32_t EXPIRY_SECS = 20UL * 60UL; // drop a track 20 min after its last update
static const int NAME_LEN = 16; // display name buffer (incl. NUL)
static const int KEY_LEN = 6; // pubkey prefix bytes (matches favourites)
struct Entry {
uint8_t key[KEY_LEN]; // pubkey prefix (verified DM); all-zero for channel entries
char name[NAME_LEN]; // display name, always set
int32_t lat_1e6, lon_1e6;
uint32_t ts; // RTC epoch seconds of the share
bool verified; // true = DM/pubkey, false = channel/name
bool used;
};
// Insert or refresh a shared position. `key` may be null/ignored when not
// verified. `name` is required (used as the channel key and for display).
void update(const uint8_t* key, const char* name,
int32_t lat_1e6, int32_t lon_1e6, uint32_t ts, bool verified) {
int slot = find(key, name, verified);
if (slot < 0) slot = allocSlot();
Entry& e = _e[slot];
if (verified && key) memcpy(e.key, key, KEY_LEN);
else memset(e.key, 0, KEY_LEN);
if (name) { strncpy(e.name, name, NAME_LEN - 1); e.name[NAME_LEN - 1] = '\0'; }
else { e.name[0] = '\0'; }
e.lat_1e6 = lat_1e6;
e.lon_1e6 = lon_1e6;
e.ts = ts;
e.verified = verified;
e.used = true;
}
// Drop entries not refreshed within EXPIRY_SECS. `now` is RTC epoch seconds.
// Guarded against now < ts (RTC stepped backwards) so a clock fix can't wipe
// the table.
void expire(uint32_t now) {
for (int i = 0; i < CAPACITY; i++) {
if (_e[i].used && now > _e[i].ts && (now - _e[i].ts) > EXPIRY_SECS) {
_e[i].used = false;
}
}
}
void clear() { for (int i = 0; i < CAPACITY; i++) _e[i].used = false; }
// Slot-wise access (caller skips inactive slots via isActive()).
const Entry& slotAt(int i) const { return _e[i]; }
bool isActive(int i, uint32_t now) const {
const Entry& e = _e[i];
if (!e.used) return false;
return !(now > e.ts && (now - e.ts) > EXPIRY_SECS);
}
// Number of currently non-expired entries.
int active(uint32_t now) const {
int n = 0;
for (int i = 0; i < CAPACITY; i++) if (isActive(i, now)) n++;
return n;
}
// Latest active position for a verified (DM/pubkey) share, or null if that
// node hasn't shared recently. Used by the locator engine to follow a
// moving contact.
const Entry* activeByKey(const uint8_t* key, uint32_t now) const {
if (!key) return nullptr;
for (int i = 0; i < CAPACITY; i++) {
if (!isActive(i, now)) continue;
const Entry& e = _e[i];
if (e.verified && memcmp(e.key, key, KEY_LEN) == 0) return &e;
}
return nullptr;
}
private:
Entry _e[CAPACITY] = {};
// Match an existing entry: verified shares by key, channel shares by name.
int find(const uint8_t* key, const char* name, bool verified) const {
for (int i = 0; i < CAPACITY; i++) {
if (!_e[i].used) continue;
if (verified) {
if (_e[i].verified && key && memcmp(_e[i].key, key, KEY_LEN) == 0) return i;
} else {
if (!_e[i].verified && name && strncmp(_e[i].name, name, NAME_LEN - 1) == 0) return i;
}
}
return -1;
}
// First free slot, else evict the oldest (smallest ts).
int allocSlot() {
int oldest = 0;
for (int i = 0; i < CAPACITY; i++) {
if (!_e[i].used) return i;
if (_e[i].ts < _e[oldest].ts) oldest = i;
}
return oldest;
}
};