diff --git a/CLAUDE.md b/CLAUDE.md index 73509451..fc69bf60 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -21,6 +21,8 @@ flutter build web --release The versioned web build uses `dart run build_pipe:build` (also `bun run build`). Web deployment pins Flutter 3.41.2; other CI jobs track stable. Read `pubspec.yaml` for current app version and dependencies rather than copying them into this guide. +For Linux, use `nix develop`; see [Linux with Nix](README.md#linux-with-nix) for the pinned SDK, native build checks, cache troubleshooting, and Sysprof host prerequisites. + ## Architecture and important files | Area | Source | diff --git a/README.md b/README.md index 92d60fe4..de5d2101 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ See the [user documentation](documentation/README.md), [connection guide](docume - **Contact Groups**: Create custom groups to organize your mesh network contacts - **Message Reactions**: React to messages with emoji responses - **Message Replies**: Thread conversations with inline reply functionality -- **Android Auto & Watches**: Hear messages and reply by voice in Android Auto, or reply from the notification shade and Wear OS/Galaxy watches ([guide](documentation/notifications.md#android-auto)) +- **Android Auto & Watches**: Reply from the notification shade and Wear OS/Galaxy watches, or hear messages and reply by voice in Android Auto (optional build flag) ([guide](documentation/notifications.md#android-auto)) - **Channel Muting**: Mute a channel from its chat screen, the channel list, or its notification ### Mesh Network @@ -103,7 +103,7 @@ See the [user documentation](documentation/README.md), [connection guide](docume | Map & Location | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | Device Management | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | Repeater Hub | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | -| Notification replies | ✅
(incl. Android Auto, watches) | ❌ | ❌ | ❌ | ❌ | ❌ | +| Notification replies | ✅
(incl. watches; Android Auto optional) | ❌ | ❌ | ❌ | ❌ | ❌ | The matrix describes implemented functionality, not a guarantee that every feature has been tested on every device. Web device connections use Web Serial and require Chrome and a secure origin. Image inference and translation require native runtimes and are unavailable on web. Android APKs currently include only `arm64-v8a`; the minimum Android API follows the Flutter SDK used to build. @@ -150,6 +150,59 @@ The matrix describes implemented functionality, not a guarantee that every featu flutter run ``` +### Linux with Nix + +Enter the development shell from the repository root: + +```bash +nix develop +flutter --version +pkg-config --exists gtk+-3.0 glib-2.0 gio-2.0 sysprof-capture-4 +flutter pub get +flutter build linux --debug +flutter build linux --profile +``` + +The lockfile currently supplies Flutter 3.47.4 and its matching Dart SDK. The +app requires Flutter 3.41.2 or newer. The Linux shell provides wrapped Clang +compilers, native headers and libraries, and Sysprof capture development files. +Android SDK warnings do not prevent Linux builds. + +If an SDK or toolchain update leaves CMake using an old compiler or install +prefix, remove the affected generated cache, for example +`rm -f build/linux/x64/profile/CMakeCache.txt`, and rebuild. If widget tests +report that `ink_sparkle.frag` cannot be decoded after an SDK update, remove +the generated `build/unit_test_assets` directory and rerun `flutter test`. + +Launch the profile bundle from a graphical session (replace `x64` with `arm64` +on ARM Linux): + +```bash +build/linux/x64/profile/bundle/meshcore_open +``` + +To record with Sysprof, launch the app through the recorder and press Ctrl+C +when finished. Use a new capture filename for each recording: + +```bash +sysprof-cli meshcore.syscap -- build/linux/x64/profile/bundle/meshcore_open +sysprof meshcore.syscap +``` + +CPU stack sampling requires host support for kernel perf events and appropriate +permissions, potentially through the Sysprof system service and polkit. The Nix +shell supplies tools and libraries, not system services or permission changes. +If Sysprof reports `Permission denied` opening perf events, ask the host +administrator to configure profiling access. A file being created does not +prove CPU samples were recorded; check the recording diagnostics and contents. +For an unprivileged capture smoke test only, use +`sysprof-cli --no-sysprofd --no-perf smoke.syscap -- build/linux/x64/profile/bundle/meshcore_open`; +this intentionally excludes CPU stack sampling. + +To update the package pin, run `nix flake update nixpkgs`, review the lockfile +diff, and repeat both Linux builds, analysis, and tests before committing it. +Keep the updated lockfile with the shell configuration for reproducibility. + ### Web (self-host) Use a Chromium browser. Device APIs are origin-gated, so serve the build over HTTPS. @@ -228,7 +281,7 @@ Messages are transmitted as binary frames using a custom protocol optimized for - **Theme**: System default, light, or dark mode - **Language**: Use one of 18 languages (English, Chinese, French, Spanish, Portuguese, German, Dutch, Polish, Swedish, Italian, Slovak, Slovenian, Bulgarian, Russian, Ukrainian, Hungarian, Japanese, Korean) -- **Notifications**: Configurable for messages, channels, and node advertisements; per-channel muting; reply, mark-as-read, and mute actions on Android notifications and watches (reply and mark-as-read in Android Auto) +- **Notifications**: Configurable for messages, channels, and node advertisements; per-channel muting; reply, mark-as-read, and mute actions on Android notifications and watches (reply and mark-as-read in Android Auto when built with `--android-project-arg=androidAuto=true`) - **Battery Chemistry**: Support for NMC, LiFePO4, LiPo, and LiPo HV battery types - **Message Retry**: Automatic retry with configurable path clearing diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts index 8aa73ce6..24b5883c 100644 --- a/android/app/build.gradle.kts +++ b/android/app/build.gradle.kts @@ -98,6 +98,24 @@ android { // } } +// Android Auto (notification read-aloud and voice reply) is opt-in, because +// declaring it puts the app through Play's separate Android Auto review. +// Enable it with `flutter build apk --android-project-arg=androidAuto=true` +// or `./gradlew -PandroidAuto=true ...`. +val androidAutoEnabled = (findProperty("androidAuto") as String?)?.toBoolean() == true + +if (androidAutoEnabled) { + android.sourceSets.getByName("main").res.srcDir("src/androidAuto/res") +} + +androidComponents { + onVariants { variant -> + if (androidAutoEnabled) { + variant.sources.manifests.addStaticManifestFile("src/androidAuto/AndroidManifest.xml") + } + } +} + flutter { source = "../.." } diff --git a/android/app/src/androidAuto/AndroidManifest.xml b/android/app/src/androidAuto/AndroidManifest.xml new file mode 100644 index 00000000..4c5db2aa --- /dev/null +++ b/android/app/src/androidAuto/AndroidManifest.xml @@ -0,0 +1,9 @@ + + + + + + + diff --git a/android/app/src/main/res/xml/automotive_app_desc.xml b/android/app/src/androidAuto/res/xml/automotive_app_desc.xml similarity index 100% rename from android/app/src/main/res/xml/automotive_app_desc.xml rename to android/app/src/androidAuto/res/xml/automotive_app_desc.xml diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 3bb75c69..27d9692a 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -34,10 +34,6 @@ - - ` -- **Unread badge**: Red pill with count (if unread messages exist) -- **Last seen**: Relative timestamp ("Now", "5 mins ago", "2 hours ago", "3 days ago"). For chat contacts, this shows whichever is more recent: the last advertisement time or the last message time -- **Favorite star**: Amber star icon if favorited -- **Location pin**: Grey pin icon if the contact has GPS coordinates +- **Last seen**: Relative time at the end of the second line ("recently", "~ 5 min.", "~ 2 hours", "~ 3 days"). It shows whichever is most recent: the last advertisement or telemetry location update, or for chat contacts the last message +- **Unread badge**: Red pill with count at the right edge (if unread messages exist) +- **Manage button**: Shield icon at the right edge for repeaters and rooms -Pull-to-refresh re-fetches the full contact list from the device. +The **Discovered nearby** row at the top of the list opens the Discovery screen and scrolls with the list. + +Pull-to-refresh re-fetches the full contact list from the device. Telemetry locations survive the refresh until the node sends a newer advert. ## Search and Filter @@ -80,7 +83,7 @@ Groups are stored per radio identity (scoped by public key). | Add/Remove Favorite | All types | Toggles the favorite flag | | Share Contact | All types | Requests advert from device → copies `meshcore://` URI to clipboard | | Share Contact Zero-Hop | All types | Broadcasts the contact's advertisement one hop | -| Delete Contact | All types | Confirmation dialog → removes from device and clears messages | +| Delete Contact | All types | Confirmation dialog → removes from device and clears messages. If auto-add is on for that type, the radio adds it back the next time it advertises; the dialog warns about this | ## App Bar Menus diff --git a/documentation/map-and-location.md b/documentation/map-and-location.md index 3cfa7a6f..9ae0f25a 100644 --- a/documentation/map-and-location.md +++ b/documentation/map-and-location.md @@ -4,6 +4,8 @@ The Map feature is a full-featured node-location visualization and radio-planning tool built on OpenStreetMap tiles. It is one of the three primary views accessible from the QuickSwitchBar. +The colors and markers below describe the standard theme. A custom, preset, or holiday theme recolors the map panels and accent, tints the tiles, and can replace the node and self-location markers; see [Themes](themes.md#map). + ## How to Access - **QuickSwitchBar tab 2** (rightmost) from Contacts or Channels @@ -79,10 +81,24 @@ In a mesh network, every message hops through one or more repeaters on its way t Standard pinch-to-zoom (range 2–18). Initial camera position is calculated from the statistical spread of all plotted points. ### Tap on a Node Marker -Opens a dialog showing: type, path (hop chain), coordinates, last-seen time, and public key. Action buttons vary by type: +Opens a card at the bottom of the map showing the node's status, type, last-seen time, path, node ID, and coordinates, with buttons to center on the node or close the card. Action buttons vary by type: - **Chat nodes**: "Open Chat" - **Repeaters**: "Manage Repeater" -- **Rooms**: "Join Room" +- **Rooms**: "Join Room" and "Manage Server" +- **Chat nodes and sensors in your contacts**: "Update location" (see below) +- **All nodes**: "Details" opens the full node info sheet + +### Update Location +**Update location** asks the node for its telemetry. If the reply includes a GPS position, the marker moves there right away and the last-seen time updates; a snackbar confirms it. You can also refresh a node's position from its Telemetry screen: any telemetry reply with a GPS position updates the map the same way. + +The node decides whether to include its position: +- **Companion radios** only include a position if they have a working GPS module and their owner allows location in telemetry, for everyone or only for favourites (**Settings → Privacy → Telemetry Location Mode**). A companion without GPS hardware never sends one, even if its adverts carry a manually set location. The app then says the node replied without a location. +- **Sensors** include their position (if they have GPS) for any client they accept, including guests. A sensor only answers clients it knows, so you may need to log in to it first. +- **Repeaters and rooms** only send their position to logged-in admins, so the button isn't shown for them. Their position normally comes from their adverts. + +If the node doesn't answer before the request times out, the app says there was no reply. The node has to be in your contacts (not only discovered) for the button to appear. + +A telemetry position stays on the map until the node sends a newer advert, which then replaces it. Refreshing or re-syncing the contact list from the radio keeps it. ### Long-Press on Empty Map Area Shows a bottom sheet with: @@ -103,10 +119,18 @@ Shows node count and pin count. Tappable to expand a legend of all marker types. ## Path Trace Map ### How to Access -- From the main map's radar icon +- From the main map's menu → **Path Trace** (build your own route, see below) - From a contact's long-press menu → "Path Trace / Ping" - From a message's path view → radar icon +### Building a Custom Trace on the Map +1. Open the map menu and choose **Path Trace**. A panel appears at the top of the map. +2. Tap nodes on the map in the order the trace should travel. **Remove Last** and **Clear** edit the list. +3. Choose **Round trip** (the trace comes back along the same hops) or **As entered** (the hops are used exactly as listed, so add the return hops yourself). +4. Tap **Run path trace**. Nothing is sent until you press this button. + +Returning from the result keeps your hops, mode and the last result (**View last result** reopens it without sending again). **Close** or the back button hides the panel without discarding the hops; reopening **Path Trace** during the same session resumes where you left off. The hops are dropped when you connect to a different radio. + ### What the User Sees A map with a polyline showing the route from your node through repeater hops to the target: - **Green circles**: Hops with known GPS coordinates @@ -193,6 +217,7 @@ The phone's own GPS is **never used**. All location data comes from the mesh: 1. **Device self-location**: Read from firmware device-info response. Set manually in Settings → Location, or updated automatically if the device has a GPS module. 2. **Remote node locations**: Extracted from advertisement packets received over the mesh. Encoded as integer lat/lon × 1,000,000. +3. **Telemetry replies**: A GPS position in a node's telemetry (from **Update location** or the Telemetry screen) replaces its advert position until the node adverts again. Telemetry positions have about 11 m precision (Cayenne LPP). ## Regions and offline use diff --git a/documentation/notifications.md b/documentation/notifications.md index 0f08a095..535570c3 100644 --- a/documentation/notifications.md +++ b/documentation/notifications.md @@ -42,7 +42,7 @@ Message notifications can be answered without opening the app, from the notifica - **Reply** sends through the connected radio exactly like the chat screen: same length limits, SMAZ and Cyr2Lat handling, and retries. Your reply is then added to the notification. - **Replies over the message limit** are split at word boundaries into up to 3 messages. Longer replies are not sent; a "Reply not sent" notification asks you to open the app. - **Mark as read** clears the unread count on the phone only. MeshCore has no read receipts, so nothing is sent over the mesh. -- **Mute channel** (phone and watch) mutes that channel, the same as the bell icon in the channel chat. +- **Mute channel** (phone and watch) sets that channel to **Muted**, the same as choosing Muted from the bell icon in the channel chat. - If the radio is disconnected, the conversation is no longer on the radio, the radio reports a send error, or the app isn't running, a "Reply not sent" notification explains why. The reply is not queued. If a split reply fails partway, earlier parts may already have been sent. - Replies are sent as typed; composer translation is not applied. @@ -52,6 +52,14 @@ iOS notifications do not have reply actions yet. MeshCore Open works in Android Auto as a notification-based messaging app. There is no MeshCore screen in the car; Android Auto provides the interface. +Android Auto support is off by default and is compiled in only when the build passes the `androidAuto` Gradle property: + +```bash +flutter build apk --release --android-project-arg=androidAuto=true +``` + +The flag merges `android/app/src/androidAuto/` (the `com.google.android.gms.car.application` manifest entry and `automotive_app_desc.xml`) into the app. Without it, Android Auto does not list the app. Notification replies from the shade and watches work either way. + - New direct and channel messages appear on the car display and can be read aloud. - Reply by voice or with a suggested reply; replies follow the rules above (splitting, failure notifications, no translation). - Android Auto shows **Reply** and **Mark as read**. **Mute channel** is marked as a mute action, but Android Auto only documents reply and mark-as-read for messaging apps, so mute may not appear in the car; use the phone, a watch, or the bell icon. @@ -99,15 +107,24 @@ Access via **App Settings → Notifications**: | Channel Message Notifications | On | Channel alerts (greyed out if master is off) | | Advertisement Notifications | On | New node alerts (greyed out if master is off) | -### Per-Channel Muting +### Per-Channel Notification Modes -Mute or unmute a channel from any of these places. Muted channels do not generate OS notifications. +Each channel supports three notification modes: -- The bell icon in the channel chat app bar -- Long-press a channel in the channels list → "Mute channel" / "Unmute channel" -- **Mute channel** on a channel notification (Android) +| Mode | Icon | Behavior | +|---|---|---| +| **All messages** | 🔔 | Receive notifications for every message | +| **Mentions only** | 🔔! | Only notify when a message mentions your name (see below) | +| **Muted** | 🔕 | No notifications | -There is no per-contact muting. +Set the mode from: + +- The notifications icon in the channel chat app bar (tap to open the mode picker) +- Long-press a channel in the channels list → **Notification settings** → select mode + +The Android **Mute channel** notification action sets the channel to **Muted**; it cannot choose another mode. + +A message mentions you if it contains `@[yourname]` or `@yourname`, case-insensitively. The plain `@yourname` form must stand alone as a word, so `@bobby` does not mention `bob` and `x@bob.com` is not a mention; names with spaces must be written in full. Detection (`MeshCoreConnector.textMentionsName`) runs on the original text, before any automatic translation. ## Rate Limiting diff --git a/documentation/repeater-management.md b/documentation/repeater-management.md index 6c25ce05..b6d8a2d4 100644 --- a/documentation/repeater-management.md +++ b/documentation/repeater-management.md @@ -160,23 +160,20 @@ A card titled "Repeater's Neighbors - N" listing each neighbor as: ### What the User Sees -Nine configuration cards, each with its own per-field refresh button(s): +Most settings are declared once in `repeaterSettingSpecs` (`lib/models/repeater_setting_spec.dart`): the CLI key, section, input type (toggle, slider, number, choice or text), range, step and labels. The screen builds each control, its refresh button (`get `) and its save command (`set `) from that list, so a new setting only needs a new entry. The radio parameters, passwords and identity keys are hand-built. **1. Basic Settings** -- Name field +- Name (`set name`) - Admin password field (write-only; always sent when non-empty) - Guest password field (write-only; always sent when non-empty) **2. Radio Settings** -- Frequency (MHz) -- TX Power (dBm) — has its own independent refresh button -- Bandwidth dropdown (kHz) -- Spreading Factor (SF5–SF12) -- Coding Rate (4/5–4/8) -- RX Gain boost toggle +- Frequency (MHz), bandwidth, spreading factor (SF5–SF12) and coding rate (4/5–4/8), sent together as `set radio freq,bw,sf,cr`. The firmware only accepts `set freq` over serial, so when the radio hasn't been fetched all four must be filled in before saving +- TX power (−9 to 30 dBm; `set tx`) +- Boosted RX gain toggle (`set radio.rxgain`) **3. Location Settings** -- Latitude and longitude fields, each with an independent refresh button +- Latitude and longitude (`set lat`, `set lon`) **4. Features** - Packet forwarding toggle (`set repeat`) @@ -187,13 +184,16 @@ Nine configuration cards, each with its own per-field refresh button(s): **5. Network Health** - Loop detection dropdown (off / minimal / moderate / strict; `set loop.detect`) - Duty cycle slider (1–100%; `set dutycycle`) +- Receive delay (0–20; `set rxdelay`) **6. Advertisement Settings** - Local advert interval slider (60–240 minutes) with enable/disable toggle - Flood advert interval slider (3–168 hours) with enable/disable toggle - Flood max hops slider (0–64; `set flood.max`) +- Advert flood max hops slider (0–64, default 8; `set flood.max.advert`) +- Unscoped flood max hops slider (0–64, default 64; `set flood.max.unscoped`). The firmware reports 255 while it is unset and follows `flood.max`; the screen shows that as "Same as flood max" -**7. Owner Info** +**7. Operator Info** - Multi-line text field for operator contact info (`set owner.info`); newlines sent as `|` **8. Actions** (one-tap, no save needed) @@ -203,23 +203,27 @@ Nine configuration cards, each with its own per-field refresh button(s): **9. Advanced** (collapsed by default) - Path hash mode dropdown (0–2; `set path.hash.mode`) -- TX delay field (`set txdelay`) -- Direct TX delay field (`set direct.txdelay`) -- Interference threshold field (`set int.thresh`) -- AGC reset interval slider (0–240s in multiples of 4; `set agc.reset.interval`) +- TX delay and direct TX delay (0–2; `set txdelay`, `set direct.txdelay`) +- Interference threshold (0–255; `set int.thresh`) +- Channel activity detection toggle (`set cad`) +- AGC reset interval slider (0–1020 s in multiples of 4; `set agc.reset.interval`) +- Battery voltage calibration (0–10, 0 = board default; `set adc.multiplier`). Not every board supports it; the screen notes this +- Extra spreading factors (up to 3, comma separated; `set extra.sf`). Only LR2021 builds have this setting; the screen notes this. Its `get` reply has no `> ` prefix + +**10. Change Identity Keys** (collapsed by default) +- Private key field (`set prv.key`, reboot to apply), with a generator that can search for a chosen public-key prefix **Danger Zone** (red-styled card) - Reboot repeater (sends `reboot` with confirmation dialog) -- Erase filesystem (serial-only; shows a confirmation dialog, then an informational snackbar — no command is sent over the air) +- Erase filesystem (serial-only; shown disabled with an explanation — no command is sent over the air) ### Key Interactions -- **Settings are NOT auto-fetched on open**. Name is pre-filled from cached contact data. Each section has its own refresh button to fetch live values from the repeater -- TX Power, RX Gain, latitude, longitude, and advanced fields each have independent inline refresh buttons -- Save button in app bar appears when any change is detected; failed commands keep those fields dirty for retry +- **Settings are not fetched on open by default.** Each `get` is a separate LoRa round trip, so fetching everything is slow and uses airtime. Turn on **Auto-fetch repeater settings** in App Settings to fetch everything when the screen opens; otherwise use **Refresh all** or the per-setting refresh buttons. Name and location are pre-filled from cached contact data +- Every control can be edited before its value has been fetched. Unfetched values show as "—" (or off) until fetched or edited, and only edited settings are sent on save +- Save button in app bar appears when any change is detected; failed commands keep those settings dirty for retry, and a saved value counts as the repeater's current value - Settings are sent sequentially with 200ms delays between commands; firmware responses are checked and partial failures are reported in a snackbar -- Some changes (e.g. radio frequency) require a reboot; the firmware response triggers an orange "reboot needed" snackbar -- Advertisement interval sliders reset to defaults when re-enabled (local: 60 min, flood: 3 hours) -- **Erase Filesystem** does NOT send any command over the air — tapping it only shows a snackbar explaining the operation requires physical serial access +- Some changes (e.g. radio parameters) require a reboot; the firmware response triggers an orange "reboot needed" snackbar +- Advertisement interval sliders reset to their minimum when re-enabled (local: 60 min, flood: 3 hours) ## Region configuration and companion statistics diff --git a/documentation/settings.md b/documentation/settings.md index 04fbe3ef..bf380de4 100644 --- a/documentation/settings.md +++ b/documentation/settings.md @@ -104,7 +104,10 @@ Settings take effect when saved. A snackbar confirms the update. A dedicated sub-screen for app-level preferences (nothing here is sent to the device). All settings persist locally via SharedPreferences. ### Appearance -- **Theme**: System / Light / Dark +- **Theme**: System / Light / Dark, the brightness of the standard MeshCore theme. While a custom, preset, or holiday theme is active, no option is selected; tapping one returns to the standard theme +- **Color theme**: Opens the Themes screen: the standard MeshCore theme, built-in presets, holiday themes, and your own themes (create, edit, share codes, `.jsonl` import/export). See [Themes](themes.md) +- **Automatic holiday themes** (on by default, toggled on the Themes screen as **Switch automatically**): Shows the Halloween, Thanksgiving, Christmas, or New Year's theme from 17 days before each holiday through the day itself, then restores your own theme. Picking any theme or Light/Dark keeps your choice for the rest of that holiday; turn the toggle off to stop it entirely. See [Automatic holiday themes](themes.md#automatic-holiday-themes) +- **Holiday message animations** (on by default): Plays a short animation when a holiday greeting arrives in or is sent from an open chat. See [Holiday message animations](themes.md#holiday-message-animations) - **Language**: System default or one of 18 languages (English, French, Spanish, German, Polish, Slovenian, Portuguese, Italian, Chinese, Swedish, Dutch, Slovak, Bulgarian, Russian, Ukrainian, Hungarian, Japanese, Korean) ### Notifications @@ -118,6 +121,7 @@ Individual channels can be muted from the channel chat bell icon, the channel li ### Messaging - **Clear Path on Max Retry**: Erases the stored routing path after all retries fail - **Jump to Oldest Unread**: When opening a chat, scrolls to the oldest unread message instead of the newest +- **Auto-fetch Repeater Settings** (off by default): Requests every setting when a repeater's settings screen opens. Each setting is a separate request over LoRa, so this is slow and uses airtime; when off, use Refresh All or the per-setting refresh buttons. See [Repeater Settings](repeater-management.md#repeater-settings) - **Auto Route Rotation**: Enables weighted routing algorithm. When enabled, expands to show five slider sub-settings (hidden when off): - Max Route Weight (1–10, default 5, integer steps) - Initial Route Weight (0.5–5.0, default 3.0) @@ -158,6 +162,20 @@ Not shown on web. Controls on-device message translation powered by a locally-do The auto-translate and composer toggles are disabled until **Enable translation** is on. +### IPFS Provider Configuration + +Open **App Settings → IPFS provider configuration** to configure IPFS uploads and previews: + +- **Public IPFS gateway**: Select Filebase (the default), Pinata, a custom gateway, or deactivate it. This gateway lets the app load bare IPFS content IDs in image links. +- **Upload provider**: Select Pinata, Filebase, a custom provider, or deactivate uploads. Presets fill in the provider’s upload endpoint and upload gateway; custom setups can edit both. +- **API key**: Enter the credential required by the selected provider. The value is masked in the settings dialog. Pinata and Filebase presets require provider credentials. + +For a self-hosted Kubo node, choose **Custom (Kubo)** and configure its RPC upload endpoint and HTTP gateway. The preset defaults use `http://127.0.0.1:5001/api/v0/add` and `http://127.0.0.1:8080/ipfs/`; replace these with addresses reachable from the device running MeshCore Open. Do not expose Kubo’s unauthenticated RPC API to the public internet. + +When an **upload gateway** is configured, uploaded content is added to the draft as an HTTP(S) gateway URL in the form `http://gateway/ipfs/CID`. If it is blank, the app uses `ipfs://CID`. The public gateway setting is separate and is used to retrieve bare IPFS content IDs in incoming links. + +Save the settings before sharing. The upload action is shown in the chat composer only when an upload endpoint is configured. Provider accounts, credentials, limits, and terms are managed by the provider, not MeshCore Open. See [sharing images via IPFS](additional-features.md#share-an-image-via-ipfs) for the chat workflow. + ### Image Messages Image message controls live in App Settings → Messaging. Turn on **Enable image messages** first; **Process images automatically** and the image model download/cancel/removal controls then appear. Automatic processing reconstructs every image as it arrives and uses about 2 GB of memory for a second each time; leave it off to reconstruct with a tap. See [Image Messages](image-messages.md) for firmware, native-runtime, storage, and memory requirements. diff --git a/documentation/themes.md b/documentation/themes.md new file mode 100644 index 00000000..cf882766 --- /dev/null +++ b/documentation/themes.md @@ -0,0 +1,103 @@ +# Themes, Chat Backgrounds & Holiday Themes + +Themes change the app's colors, icons, map markers, and (optionally) chat backgrounds. Everything here is stored on this device only; nothing is sent to the radio or the mesh. + +## How to Access + +**Settings → App Settings → Appearance → Color theme** opens the Themes screen. The tile's subtitle shows the theme in use ("MeshCore" for the standard theme), with a color swatch when a custom or preset theme is active. + +## Light / Dark and Color Theme + +Appearance has two related controls: + +- **Theme** (System / Light / Dark): the brightness of the standard MeshCore theme. +- **Color theme**: the standard MeshCore theme, a built-in preset, a holiday theme, or one of your own. + +A custom or preset theme sets its own brightness (a dark background makes a dark theme), so while one is active, none of the System / Light / Dark buttons is selected. Tapping one of them goes back to the standard MeshCore theme in that mode. + +## Themes Screen + +The screen has three sections. Tap a theme to use it right away; the active theme is marked as selected. + +### My themes + +Themes you created or imported. Each has a menu with **Edit**, **Copy theme code**, **Export to file**, and **Delete**. Deleting the active theme returns to the standard theme. + +### Built-in + +- **MeshCore**: the standard colors, following the Light / Dark setting. +- Presets: Forest, Ocean, Ember, Grape, High Contrast, Paper, Mint, and Sky. + +Preset menus offer **Customize a copy** (opens the editor on a copy under "My themes") and **Copy theme code**. Presets themselves can't be edited. + +### Holidays + +Halloween, Thanksgiving, Christmas, and New Year's themes, each with its own colors, chat background picture, tab and send icons, and map markers. You can pick one at any time, like any other theme. This section also holds the **Switch automatically** toggle; see [Automatic holiday themes](#automatic-holiday-themes). + +## Automatic Holiday Themes + +**Switch automatically** is **on by default**, including for people who already chose a theme before updating. While it is on, each holiday's theme is shown in place of your own theme from **17 days before the holiday until the end of the holiday itself**: + +| Holiday | Date | Automatic theme starts | +|---|---|---| +| Halloween | October 31 | October 14 | +| Thanksgiving | Fourth Thursday of November (US) | 17 days before (Nov 5–11, depending on the year) | +| Christmas | December 25 | December 8 | +| New Year's | January 1 | December 15 | + +When windows overlap, the nearest upcoming holiday wins. A holiday keeps its theme through its own day, and the next holiday takes over the day after (for example, Christmas runs through December 25 and New Year's from December 26). Dates use the device's local calendar, and the app checks for a date change at midnight (and at least hourly), so the theme switches without restarting. + +Your own choice is never changed: the holiday theme only covers it. When the window ends, your theme comes back by itself. While a holiday theme is on automatically, the Themes screen shows a note ("… theme is on automatically. Your own theme comes back after the holiday.") and tags that holiday's tile **On automatically**. + +### Keeping your own theme + +- **For the current holiday only**: pick any theme, or tap System / Light / Dark in App Settings. Your choice sticks for the rest of that holiday; the next holiday switches automatically again. +- **For all holidays**: turn off **Switch automatically** on the Themes screen. Your own theme returns immediately. + +Turning **Switch automatically** back on also forgets an earlier "for this holiday only" choice, so the current holiday's theme shows again. Picking the holiday theme that is already on automatically doesn't change your saved theme, so your own theme still comes back after the holiday. + +## Creating and Editing a Theme + +Tap **Create theme** (or **Edit** / **Customize a copy**) to open the editor. A live preview shows chat bubbles and a switch in the new colors. + +- **Name** (required) +- **Background**: the main background color. A dark background makes a dark theme. Surfaces, text, and bubbles are derived from it. +- **Accent**: buttons, highlights, and your own chat bubbles. A warning appears when the accent is hard to see on the background. +- Colors can be chosen from swatches or entered as a hex color. +- **Chat background**: none, a color, or a picture, with a fade slider. It is shown in chats that have no background of their own. Turn on **Use in every chat** to also replace backgrounds set on individual chats. +- **Icons**: swap the Contacts, Channels, and Map tab icons, the send button, the Settings icon, and the Repeater, Room, and Sensor avatars. Tap an icon to choose another named icon or **Use a picture** from your gallery; **Use the default icon** resets it. Pictures are stored as lossless JPEG XL. +- **Map markers**: swap the map markers for chat nodes, repeaters, rooms, sensors, and your own position the same way. + +**Save and use** saves the theme and makes it active. + +## Sharing Themes + +- **Copy theme code** copies a single theme as text. Share it any way you like; the recipient uses **Paste theme code** (Themes screen ⋮ menu) to add it. +- **Export to file** (one theme) and **Export my themes** (all of yours, ⋮ menu) write a `.jsonl` file, one theme per line. On Android and iOS it opens the share sheet, on desktop a save dialog, and on web the file downloads. +- **Import from file** (⋮ menu) accepts `.jsonl`, `.json`, or `.txt`. Invalid lines are skipped, and themes identical to one you already have aren't added twice. + +Imported themes appear under "My themes"; importing doesn't switch to them. + +## Map + +The map follows the active theme: panels, accent color, a tint over the map tiles, and the theme's map markers. See [Map & Location](map-and-location.md). + +## Chat Backgrounds + +Each direct chat and channel can have its own background. Open the chat, then **⋮ → Chat background**: + +- **None**, one of 12 colors, or **Picture** (**Choose a picture** from your gallery). +- **Fade** (10–90%) blends the background toward the theme's surface color so messages stay readable. + +Changes apply live behind the sheet and affect only that chat. If the active theme has **Use in every chat** on, the sheet says so, and the theme's background is shown until you turn that option off in the theme. Holiday themes include a background picture that appears in chats without their own background. + +Backgrounds are tied to the channel or contact, not the radio, so they follow the chat if you connect a different radio. Pictures are re-encoded as JPEG XL; on web they are kept in browser storage. + +## Holiday Message Animations + +When a new message containing a holiday greeting arrives in, or is sent from, the chat you have open, a short full-screen animation plays: bats for "Happy Halloween", a turkey for "Happy Thanksgiving", Santa for "Merry Christmas", and fireworks for "Happy New Year". Greetings are recognized in all of the app's languages, whatever your own language setting, and case and punctuation are ignored. Hashtags like `#HappyNewYear` also count. + +- Only new messages play; messages already in the chat when you open it, and synced messages more than 2 minutes old, stay quiet. +- Animations play any day of the year, not only during the holiday windows. +- Touches pass through the animation. +- Turn them off with **App Settings → Appearance → Holiday message animations**. They are also skipped when the system's reduce-motion / remove-animations setting is on. diff --git a/flake.lock b/flake.lock index 4d0355b2..7fe6a19f 100644 --- a/flake.lock +++ b/flake.lock @@ -20,11 +20,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1770562336, - "narHash": "sha256-ub1gpAONMFsT/GU2hV6ZWJjur8rJ6kKxdm9IlCT0j84=", + "lastModified": 1793341285, + "narHash": "sha256-Y1CiZ4SoBUjLQyQM/pY+rS2dXD3CHLPstF0lmOc73D4=", "owner": "nixos", "repo": "nixpkgs", - "rev": "d6c71932130818840fc8fe9509cf50be8c64634f", + "rev": "1c89083e451e891152365b799595cf7790a43325", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index 16711455..d64b0fb3 100644 --- a/flake.nix +++ b/flake.nix @@ -12,11 +12,18 @@ pkgs = nixpkgs.legacyPackages.${system}; in { - devShells.default = pkgs.mkShell { + devShells.default = (pkgs.mkShell.override { + stdenv = if pkgs.stdenv.hostPlatform.isLinux then pkgs.clangStdenv else pkgs.stdenv; + }) { + nativeBuildInputs = with pkgs; [ + cmake + ninja + pkg-config + ]; + buildInputs = with pkgs; [ # Flutter and Dart flutter - dart # Java (required for Android development) jdk17 @@ -29,22 +36,18 @@ gtk3 glib sysprof - libclang - cmake - ninja - pkg-config libdatrie # Additional tools for installing Android SDK if not present curl unzip + ] ++ pkgs.lib.optionals pkgs.stdenv.hostPlatform.isLinux [ + pkgs.libsysprof-capture ]; shellHook = '' echo "MeshCore Flutter Development Environment" - export PKG_CONFIG_PATH="${pkgs.gtk3}/lib/pkgconfig:${pkgs.glib}/lib/pkgconfig:${pkgs.sysprof}/lib/pkgconfig:$PKG_CONFIG_PATH" export LD_LIBRARY_PATH="${pkgs.lib.makeLibraryPath [pkgs.gtk3 pkgs.glib pkgs.sysprof pkgs.libdatrie]}:$LD_LIBRARY_PATH" - export CMAKE_INSTALL_PREFIX="$PWD/build/bundle" # Setup Android SDK in home directory (standard location) export ANDROID_HOME="$HOME/Android/Sdk" @@ -62,7 +65,7 @@ echo "" cat << 'EOF' mkdir -p ~/Android/Sdk && cd ~/Android/Sdk && \ -curl -o cmdline-tools.zip ${if pkgs.stdenv.isDarwin then "https://dl.google.com/android/repository/commandlinetools-mac-10406996_latest.zip" else "https://dl.google.com/android/repository/commandlinetools-linux-10406996_latest.zip"} && \ +curl -o cmdline-tools.zip ${if pkgs.stdenv.hostPlatform.isDarwin then "https://dl.google.com/android/repository/commandlinetools-mac-10406996_latest.zip" else "https://dl.google.com/android/repository/commandlinetools-linux-10406996_latest.zip"} && \ unzip -q cmdline-tools.zip && \ mkdir -p cmdline-tools/latest && \ mv cmdline-tools/* cmdline-tools/latest/ 2>/dev/null || echo "Warning: failed to move Android cmdline-tools into 'latest' directory; please check your SDK layout." >&2 && \ diff --git a/lib/connector/meshcore_connector.dart b/lib/connector/meshcore_connector.dart index 03b03944..e69b4f32 100644 --- a/lib/connector/meshcore_connector.dart +++ b/lib/connector/meshcore_connector.dart @@ -38,7 +38,8 @@ import '../services/image_codec_service.dart'; import '../services/received_image_store.dart'; import '../services/message_retry_service.dart'; import '../services/path_history_service.dart'; -import '../services/app_settings_service.dart'; +import '../services/app_settings_service.dart' + show AppSettingsService, ChannelNotificationMode; import '../services/background_service.dart'; import '../services/timeout_prediction_service.dart'; import '../services/translation_service.dart'; @@ -66,6 +67,7 @@ import 'meshcore_uuids.dart'; import 'meshcore_protocol.dart'; import 'pending_command_replies.dart'; import 'raw_packet.dart'; +import '../utils/coalesced_writes.dart'; class DirectRepeater { static const int maxAgeMinutes = 30; // Max age for direct repeater info @@ -298,6 +300,22 @@ class MeshCoreConnector extends ChangeNotifier { Stopwatch? _airtimeBumpStopwatch; int _prevTotalAirSecs = 0; int? _batteryMillivolts; + // Thresholds (percent) that trigger a low-battery notification once per + // crossing, re-arming 5 points above the threshold after recharge. + static const List _lowBatteryThresholds = [25, 10]; + final Set _batteryAlertedThresholds = {}; + // Consecutive readings that newly crossed a threshold. Alerts need + // [_lowBatteryConfirmReadings] in a row so a single TX-load voltage sag + // doesn't notify. + int _lowBatteryReadingStreak = 0; + // Device key the posted low-battery notifications were scoped to, kept so + // they can still be cancelled after the session's identity is cleared. + String? _lowBatteryAlertDeviceKey; + static const int _lowBatteryConfirmReadings = 2; + // Readings below this are treated as "no battery sensor" (boards without + // one report 0 mV). Any single Li cell, including LiFePO4 (2600 mV empty), + // is protection-cut well above 2 V, so lower values are not real readings. + static const int _minPlausibleBatteryMillivolts = 2000; double? _selfLatitude; double? _selfLongitude; final List _directRepeaters = List.empty(growable: true); @@ -316,12 +334,6 @@ class MeshCoreConnector extends ChangeNotifier { ReceivedImageStore? _receivedImageStore; void Function(ImageChunkOutcome outcome)? _onImageChunk; void Function(int senderPrefix)? _onImageSenderPrefix; - // Intentionally global (not per-contact): tracks overall network activity. - // Frequent RX from any source indicates a busy network with more collisions. - DateTime _lastRxTime = DateTime.now(); - // Snapshot of _lastRxTime taken before the ACK frame updates it, so that - // onDeliveryObserved records the pre-ACK elapsed time (matching prediction). - DateTime _lastRxBeforeFrame = DateTime.fromMillisecondsSinceEpoch(0); DateTime _lastRadioRxTime = DateTime.fromMillisecondsSinceEpoch(0); DateTime _lastContactMsgRxTime = DateTime.fromMillisecondsSinceEpoch(0); DateTime _lastChannelMsgRxTime = DateTime.fromMillisecondsSinceEpoch(0); @@ -462,6 +474,9 @@ class MeshCoreConnector extends ChangeNotifier { BleDebugLogService? _bleDebugLogService; AppDebugLogService? _appDebugLogService; final ChannelMessageStore _channelMessageStore = ChannelMessageStore(); + // Per-frame persistence (channel history, conversations, contact lists) is + // batched here; see [CoalescedWrites]. + final CoalescedWrites _coalescedWrites = CoalescedWrites(); final MessageStore _messageStore = MessageStore(); final ChannelOrderStore _channelOrderStore = ChannelOrderStore(); final ChannelSettingsStore _channelSettingsStore = ChannelSettingsStore(); @@ -474,7 +489,7 @@ class MeshCoreConnector extends ChangeNotifier { List _cachedChannels = []; final Map _channelSmazEnabled = {}; final Map _channelCyr2LatEnabled = {}; - final Map _channelUrlImagesEnabled = {}; + final Map _channelUrlImagesEnabled = {}; final Map _channelCyr2LatProfileId = {}; final Map _channelRegions = {}; Region _defaultRegion = ''; @@ -484,7 +499,7 @@ class MeshCoreConnector extends ChangeNotifier { Uint8List? _defaultFloodScopeKey; final Map _contactSmazEnabled = {}; final Map _contactCyr2LatEnabled = {}; - final Map _contactUrlImagesEnabled = {}; + final Map _contactUrlImagesEnabled = {}; final Map _contactCyr2LatProfileId = {}; final Set _knownContactKeys = {}; final Map _contactUnreadCount = {}; @@ -667,6 +682,16 @@ class MeshCoreConnector extends ChangeNotifier { bool? get autoAddSensors => _autoAddSensors; bool? get autoAddOverwriteOldest => _overwriteOldest; + /// Whether the radio stores adverts of [type] by itself, so a removed + /// contact of that type returns the next time it advertises. + bool autoAddsContactType(int type) => switch (type) { + advTypeChat => _autoAddUsers, + advTypeRepeater => _autoAddRepeaters, + advTypeRoom => _autoAddRoomServers, + advTypeSensor => _autoAddSensors, + _ => false, + }; + /// autoadd_max_hops from GET_AUTOADD_CONFIG (0 = no limit), null when the /// firmware did not report it (pre-v1.13). int? get autoAddMaxHops => _autoAddMaxHops; @@ -681,6 +706,9 @@ class MeshCoreConnector extends ChangeNotifier { /// from ver code 8. bool get supportsNodeDiscover => (_firmwareVerCode ?? 0) >= 8; + /// CMD_SET_PATH_HASH_MODE exists from ver code 10 (v1.14.0). + bool get supportsPathHashMode => (_firmwareVerCode ?? 0) >= 10; + /// CMD_GET/SET_DEFAULT_FLOOD_SCOPE exist from ver code 11 (v1.15.0). bool get supportsDefaultFloodScope => (_firmwareVerCode ?? 0) >= 11; @@ -776,6 +804,82 @@ class MeshCoreConnector extends ChangeNotifier { } } + /// Applies a GPS fix from a telemetry reply to the contact so the map and + /// lists show where the node is now, not where its last advert put it. + /// [lastSeen] is left alone: it is the node's advert timestamp, which path + /// and flag writes send back to the radio. + void updateContactLocationFromTelemetry( + String contactKeyHex, + double latitude, + double longitude, + ) { + final index = _contacts.indexWhere((c) => c.publicKeyHex == contactKeyHex); + if (index < 0) return; + final updated = _contacts[index].copyWith( + latitude: latitude, + longitude: longitude, + telemetryFixAt: DateTime.now(), + ); + if (!updated.hasLocation) return; + _contacts[index] = updated; + notifyListeners(); + unawaited(_persistContacts()); + } + + /// Carries [previous]'s telemetry fix onto [incoming], a record the radio + /// just returned, while it still holds the advert that fix superseded. A + /// newer advert (by the node's own clock) wins. + Contact? _withTelemetryFix(Contact previous, Contact incoming) { + final fixAt = previous.telemetryFixAt; + if (fixAt == null || incoming.lastSeen.isAfter(previous.lastSeen)) { + return null; + } + return incoming.copyWith( + latitude: previous.latitude, + longitude: previous.longitude, + telemetryFixAt: fixAt, + ); + } + + /// Merges a contact record from the radio (contact list or advert) into the + /// one we already hold, keeping the user's path choice and any telemetry + /// fix still newer than the radio's advert. + Contact _mergeRefreshedContact( + Contact existing, + Contact incoming, { + bool keepLocationWhenMissing = false, + }) { + final mergedLastMessageAt = + existing.lastMessageAt.isAfter(incoming.lastMessageAt) + ? existing.lastMessageAt + : incoming.lastMessageAt; + final base = incoming.copyWith( + lastMessageAt: mergedLastMessageAt, + pathOverride: existing.pathOverride, // Preserve user's path choice + pathOverrideBytes: existing.pathOverrideBytes, + latitude: keepLocationWhenMissing + ? incoming.latitude ?? existing.latitude + : incoming.latitude, + longitude: keepLocationWhenMissing + ? incoming.longitude ?? existing.longitude + : incoming.longitude, + clearTelemetryFix: true, + ); + return _withTelemetryFix(existing, base) ?? base; + } + + /// Telemetry fixes held across a full contact sync, which clears the list + /// before the radio resends it. + final Map _telemetryFixesDuringSync = {}; + + void _stashTelemetryFixes() { + for (final c in _contacts) { + if (c.telemetryFixAt != null) { + _telemetryFixesDuringSync[c.publicKeyHex] = c; + } + } + } + String _batteryChemistryForDevice() { final deviceId = batteryDeviceKey; if (deviceId == null || _appSettingsService == null) return 'nmc'; @@ -958,9 +1062,40 @@ class MeshCoreConnector extends ChangeNotifier { Future _saveChannelMessages(int slot, List messages) { final id = _channelIdForSlot(slot); if (id == null) return Future.value(); + _coalescedWrites.cancel('channel:$id'); return _channelMessageStore.saveChannelMessages(id, messages); } + /// Queues a save of [slot]'s history. The write looks the channel up again + /// when it runs, so it persists whatever the history is by then and skips a + /// channel that was removed in the meantime. + void _scheduleChannelSave(int slot) { + final id = _channelIdForSlot(slot); + if (id == null) return; + _coalescedWrites.schedule('channel:$id', () { + final currentSlot = _slotForChannelId(id); + final messages = currentSlot == null + ? null + : _channelMessages[_canonicalSlot(currentSlot)]; + if (messages == null) return Future.value(); + return _channelMessageStore.saveChannelMessages(id, messages); + }); + } + + /// Queues a save of the conversation with [contactKeyHex]; skipped when the + /// conversation is gone by the time it runs. + void _scheduleConversationSave(String contactKeyHex) { + _coalescedWrites.schedule('conversation:$contactKeyHex', () { + final messages = _conversations[contactKeyHex]; + if (messages == null) return Future.value(); + return _messageStore.saveMessages(contactKeyHex, messages); + }); + } + + /// Runs queued persistence now. Called before the app is backgrounded and + /// whenever the stores are about to switch to another radio. + Future flushPendingWrites() => _coalescedWrites.flush(); + int getUnreadCountForContact(Contact contact) { if (contact.type == advTypeRepeater) return 0; return getUnreadCountForContactKey(contact.publicKeyHex); @@ -1029,13 +1164,25 @@ class MeshCoreConnector extends ChangeNotifier { return _contactSmazEnabled[contactKeyHex] ?? false; } + bool _lastShowLinkImagesByDefault = false; + + void _onAppSettingsChanged() { + final showByDefault = _showLinkImagesByDefault; + if (showByDefault == _lastShowLinkImagesByDefault) return; + _lastShowLinkImagesByDefault = showByDefault; + notifyListeners(); + } + + bool get _showLinkImagesByDefault => + _appSettingsService?.settings.showLinkImagesByDefault ?? false; + bool isChannelUrlImagesEnabled(int channelIndex) { - return _channelUrlImagesEnabled[channelIndex] ?? false; + return _channelUrlImagesEnabled[channelIndex] ?? _showLinkImagesByDefault; } bool isContactUrlImagesEnabled(String contactKeyHex) { _ensureContactUrlImagesSettingLoaded(contactKeyHex); - return _contactUrlImagesEnabled[contactKeyHex] ?? false; + return _contactUrlImagesEnabled[contactKeyHex] ?? _showLinkImagesByDefault; } /// True when the channel has its own (real) region assigned. @@ -1202,6 +1349,17 @@ class MeshCoreConnector extends ChangeNotifier { notifyListeners(); } + /// Queues a save of the channel list, which carries the unread counts that + /// change on every received channel message. + void _scheduleChannelListSave() { + _coalescedWrites.schedule( + 'channels', + () => _channelStore.saveChannels( + _channels.isNotEmpty ? _channels : _cachedChannels, + ), + ); + } + void setChannelUnreadCount(int channelIndex, int count) { final channel = _findChannelByIndex(channelIndex); if (channel != null) { @@ -1209,11 +1367,7 @@ class MeshCoreConnector extends ChangeNotifier { channel.unreadCount = count; final total = _cachedChannelsUnreadTotal + (count - previousCount); _cachedChannelsUnreadTotal = total < 0 ? 0 : total; - unawaited( - _channelStore.saveChannels( - _channels.isNotEmpty ? _channels : _cachedChannels, - ), - ); + _scheduleChannelListSave(); notifyListeners(); } } @@ -1229,11 +1383,7 @@ class MeshCoreConnector extends ChangeNotifier { 'Channel ${channel.name.isNotEmpty ? channel.name : channelIndex} marked as read (was $previousCount unread)', tag: 'Unread', ); - unawaited( - _channelStore.saveChannels( - _channels.isNotEmpty ? _channels : _cachedChannels, - ), - ); + _scheduleChannelListSave(); _notificationService.clearChannelNotification( channelIndex, getTotalUnreadCount(), @@ -1246,8 +1396,9 @@ class MeshCoreConnector extends ChangeNotifier { /// (notification shade, watch, or Android Auto). Future handleNotificationReply(NotificationReply reply) async { if (reply.action == NotificationReplyAction.mute) { - await _appSettingsService?.muteChannel( + await _appSettingsService?.setChannelNotificationMode( _channelDisplayName(reply.channelIndex!), + ChannelNotificationMode.muted, ); await _notificationService.clearChannelNotification( reply.channelIndex!, @@ -1492,6 +1643,9 @@ class MeshCoreConnector extends ChangeNotifier { /// Load persisted channel messages for a specific channel Future _loadChannelMessages(int channelIndex) async { + // The load replaces the in-memory history, so queued writes of it must + // reach storage (whose cache updates synchronously) first. + unawaited(_coalescedWrites.flush()); final channelId = _slotChannelIds[channelIndex]; final allMessages = channelId == null ? [] @@ -1590,6 +1744,8 @@ class MeshCoreConnector extends ChangeNotifier { _retryService = retryService; _pathHistoryService = pathHistoryService; _appSettingsService = appSettingsService; + _lastShowLinkImagesByDefault = _showLinkImagesByDefault; + appSettingsService?.addListener(_onAppSettingsChanged); _translationService = translationService; _imageCodecService = imageCodecService; _imageTransport = imageTransport; @@ -1648,15 +1804,11 @@ class MeshCoreConnector extends ChangeNotifier { recentSelections: recentSelections, ), onDeliveryObserved: (contactKey, pathLength, messageBytes, tripTimeMs) { - final secSinceRx = DateTime.now() - .difference(_lastRxBeforeFrame) - .inSeconds; _timeoutPredictionService?.recordObservation( contactKey: contactKey, pathLength: pathLength, messageBytes: messageBytes, tripTimeMs: tripTimeMs, - secondsSinceLastRx: secSinceRx, ); }, ), @@ -3794,6 +3946,9 @@ class MeshCoreConnector extends ChangeNotifier { if (pendingAck != null) { _pendingGenericAckQueue.remove(pendingAck); } + if (_activeTransport == MeshCoreTransportType.tcp) { + unawaited(disconnect(manual: false)); + } rethrow; } @@ -4137,6 +4292,7 @@ class MeshCoreConnector extends ChangeNotifier { _contactSyncUsesSinceFilter = since != null; if (!preserveExisting) { _hasLoadedContacts = false; + _stashTelemetryFixes(); _contacts.clear(); } _armContactSyncWatchdog(); @@ -4356,7 +4512,7 @@ class MeshCoreConnector extends ChangeNotifier { _pathOpLock = completer.future; await prev; try { - if (!isConnected) return; + if (!isConnected || !_isKnownContact(contact)) return; final width = pathHashWidth ?? @@ -4399,6 +4555,13 @@ class MeshCoreConnector extends ChangeNotifier { } } + /// Whether [contact] is still in the radio's contact list. The firmware's + /// CMD_ADD_UPDATE_CONTACT adds unknown keys, so path/flag writes for a + /// removed contact (e.g. a repeater screen still open after its old key was + /// deleted) would re-create it on the radio. + bool _isKnownContact(Contact contact) => + _contacts.any((c) => c.publicKeyHex == contact.publicKeyHex); + /// The advert timestamp the firmware holds for [contact] (Contact.lastSeen /// is parsed from that field); the stored copy is freshest. DateTime _deviceLastAdvert(Contact contact) { @@ -4415,7 +4578,7 @@ class MeshCoreConnector extends ChangeNotifier { bool? teleLoc, bool? teleEnv, }) async { - if (!isConnected) return; + if (!isConnected || !_isKnownContact(contact)) return; final latestContact = await _fetchContactSnapshotFromDevice(contact.publicKey) ?? contact; int updatedFlags = isFavorite != null @@ -5034,13 +5197,26 @@ class MeshCoreConnector extends ChangeNotifier { }) async { if (!isConnected) return; + // Only drop the contact locally once the radio confirms; otherwise the + // next contact sync restores it. ERR means the radio does not have it. + try { + await sendFrame( + buildRemoveContactFrame(contact.publicKey), + waitForGenericAck: true, + ); + } on FirmwareCommandException catch (e) { + _appDebugLogService?.warn( + 'Radio has no contact ${contact.publicKeyHex.substring(0, 8)} ' + '(error ${e.errCode}); removing it locally', + tag: 'Connector', + ); + } + _handleDiscovery( contact, contact.rawPacket ?? Uint8List(0), noNotify: true, ); - - await sendFrame(buildRemoveContactFrame(contact.publicKey)); _contactsStorageFull = false; _contacts.removeWhere((c) => c.publicKeyHex == contact.publicKeyHex); _knownContactKeys.remove(contact.publicKeyHex); @@ -5069,7 +5245,7 @@ class MeshCoreConnector extends ChangeNotifier { ), ); } - unawaited(_persistDiscoveredContacts()); + _scheduleDiscoveredContactsPersist(); notifyListeners(); } @@ -5078,7 +5254,7 @@ class MeshCoreConnector extends ChangeNotifier { _discoveredContacts.removeWhere( (c) => c.publicKeyHex == contact.publicKeyHex, ); - unawaited(_persistDiscoveredContacts()); + _scheduleDiscoveredContactsPersist(); notifyListeners(); } @@ -5175,7 +5351,9 @@ class MeshCoreConnector extends ChangeNotifier { lat: contact.latitude, lon: contact.longitude, lastAdvert: contact.lastSeen, - lastModified: contact.lastSeen, + // No lastModified: the radio then stamps its own clock. Sending the + // (often old) advert time made the new contact the "oldest" one, so + // with overwrite-oldest on a full table the next add evicted it. ), waitForGenericAck: true, ); @@ -5204,7 +5382,7 @@ class MeshCoreConnector extends ChangeNotifier { ), ); notifyListeners(); - unawaited(_persistDiscoveredContacts()); + _scheduleDiscoveredContactsPersist(); return true; } @@ -5649,6 +5827,9 @@ class MeshCoreConnector extends ChangeNotifier { '$unconfirmed', ); } + // Queued channel writes find their channel by slot; land them before + // the map moves, or a channel whose slot changed would never be saved. + unawaited(_coalescedWrites.flush()); _slotChannelIds ..clear() ..addAll(next); @@ -5774,6 +5955,7 @@ class MeshCoreConnector extends ChangeNotifier { ); await _loadChannelOrder(); final saved = await _channelStore.loadSlotChannelIds(); + unawaited(_coalescedWrites.flush()); _slotChannelIds ..clear() ..addAll(saved); @@ -5846,22 +6028,32 @@ class MeshCoreConnector extends ChangeNotifier { _findChannelByIndex(index)?.storageId ?? _slotChannelIds[index]; // Delete by setting empty name and zero PSK await sendFrame(buildSetChannelFrame(index, '', Uint8List(16))); - // Another slot holding the same PSK keeps the shared history. - final stillHeld = _channels.any( - (channel) => channel.index != index && channel.storageId == channelId, - ); - if (channelId != null && !stillHeld) { - await _clearChannelStorage(channelId); - } + // A twin slot with the same PSK keeps this history, so queued writes of + // it land before the list is dropped; a full delete clears them below. + unawaited(_coalescedWrites.flush()); _channelMessages.remove(index); _processedChannelReactions.remove(index); - // Refresh channels after deleting - await getChannels(force: true); + try { + // Refresh before clearing history so messages from an in-flight queue + // sync cannot recreate the channel's stored history after it is deleted. + await getChannels(force: true); + } finally { + // Another slot holding the same PSK keeps the shared history. The + // deleted slot is skipped: a failed or partial refresh can leave it in + // the map. + final stillHeld = _slotChannelIds.entries.any( + (entry) => entry.key != index && entry.value == channelId, + ); + if (channelId != null && !stillHeld) { + await _clearChannelStorage(channelId); + } + } } /// Removes everything persisted for [channelId]: messages, settings, /// region, images and its place in the channel order. Future _clearChannelStorage(String channelId) async { + _coalescedWrites.cancel('channel:$channelId'); await _channelMessageStore.clearChannelMessages(channelId); await _channelSettingsStore.clearChannel(channelId); await _channelRegionStore.clearRegion(channelId); @@ -5873,8 +6065,6 @@ class MeshCoreConnector extends ChangeNotifier { void _handleFrame(List data) { if (data.isEmpty) return; - _lastRxBeforeFrame = _lastRxTime; - _lastRxTime = DateTime.now(); final frame = Uint8List.fromList(data); _receivedFramesController.add(frame); @@ -5898,6 +6088,7 @@ class MeshCoreConnector extends ChangeNotifier { case respCodeContactsStart: debugPrint('Got CONTACTS_START'); if (!_preserveContactsOnRefresh) { + _stashTelemetryFixes(); _contacts.clear(); } _isLoadingContacts = true; @@ -5973,6 +6164,7 @@ class MeshCoreConnector extends ChangeNotifier { } _hasLoadedContacts = true; _preserveContactsOnRefresh = false; + _telemetryFixesDuringSync.clear(); _contactSyncUsesSinceFilter = false; unawaited(updateKnownDiscovered()); notifyListeners(); @@ -6253,6 +6445,7 @@ class MeshCoreConnector extends ChangeNotifier { return; } + unawaited(_coalescedWrites.flush()); //set all the stores' public key so they can load the correct data _channelMessageStore.setPublicKeyHex = selfPublicKeyHex; _messageStore.setPublicKeyHex = selfPublicKeyHex; @@ -6471,6 +6664,7 @@ class MeshCoreConnector extends ChangeNotifier { _batteryMillivolts = reader.readUInt16LE(); _storageUsedKb = reader.readUInt32LE(); _storageTotalKb = reader.readUInt32LE(); + _checkLowBatteryThresholds(); final volts = (_batteryMillivolts! / 1000.0).toStringAsFixed(2); _appDebugLogService?.info( 'Pulled battery: $volts V ($_batteryMillivolts mV)', @@ -6485,6 +6679,64 @@ class MeshCoreConnector extends ChangeNotifier { } } + void _checkLowBatteryThresholds() { + final millivolts = _batteryMillivolts; + if (millivolts == null || millivolts < _minPlausibleBatteryMillivolts) { + return; + } + // Until the device identity (and so its saved chemistry) is known, the + // percentage falls back to NMC and can read low for e.g. LiFePO4 packs. + // Skip evaluation; the next battery poll re-checks once identity is set. + if (batteryDeviceKey == null || _appSettingsService == null) return; + final percent = batteryPercent; + if (percent == null) return; + + // Only the lowest newly crossed threshold notifies; higher ones crossed + // in the same reading are marked as alerted silently. + final newlyCrossed = []; + for (final threshold in _lowBatteryThresholds) { + if (percent <= threshold && + !_batteryAlertedThresholds.contains(threshold)) { + newlyCrossed.add(threshold); + } else if (percent > threshold + 5 && + _batteryAlertedThresholds.contains(threshold)) { + // Re-arm once the battery recovers 5 points above the threshold, + // so a recharge + discharge cycle alerts again without flapping. + _batteryAlertedThresholds.remove(threshold); + final alertDeviceKey = _lowBatteryAlertDeviceKey; + if (alertDeviceKey != null) { + unawaited( + _notificationService.clearLowBatteryNotifications(alertDeviceKey, [ + threshold, + ]), + ); + } + } + } + if (newlyCrossed.isEmpty) { + _lowBatteryReadingStreak = 0; + return; + } + // Debounce: wait for consecutive low readings before alerting. + if (++_lowBatteryReadingStreak < _lowBatteryConfirmReadings) return; + _lowBatteryReadingStreak = 0; + _batteryAlertedThresholds.addAll(newlyCrossed); + final deviceKey = batteryDeviceKey!; + _lowBatteryAlertDeviceKey = deviceKey; + final lowestNewlyCrossed = newlyCrossed.reduce((a, b) => a < b ? a : b); + if (!(_appSettingsService?.settings.notificationsEnabled ?? false)) { + return; + } + unawaited( + _notificationService.showLowBatteryNotification( + deviceKey: deviceKey, + deviceName: deviceDisplayName, + batteryPercent: percent, + threshold: lowestNewlyCrossed, + ), + ); + } + void _checkManualAddContacts() async { // If manual add contacts is enabled, set auto add config and other params. // and disable it after @@ -6572,12 +6824,10 @@ class MeshCoreConnector extends ChangeNotifier { final physicsMax = _physicsMaxTimeout(pathLength, airtime); // Try ML-based prediction - final secSinceRx = DateTime.now().difference(_lastRxTime).inSeconds; final mlTimeout = _timeoutPredictionService?.predictTimeout( contactKey: contactKey, pathLength: pathLength, messageBytes: messageBytes, - secondsSinceLastRx: secSinceRx, ); if (mlTimeout != null) { // Use device est_timeout as a baseline floor when available — @@ -6678,10 +6928,6 @@ class MeshCoreConnector extends ChangeNotifier { if (existingIndex >= 0) { final existing = _contacts[existingIndex]; - final mergedLastMessageAt = - existing.lastMessageAt.isAfter(contact.lastMessageAt) - ? existing.lastMessageAt - : contact.lastMessageAt; appLogger.info( 'Refreshing contact ${contact.name}: devicePath=${contact.pathLength}, existingOverride=${existing.pathOverride}', @@ -6690,12 +6936,10 @@ class MeshCoreConnector extends ChangeNotifier { // Preserve user-selected path settings and previously known GPS when // refreshed frames omit coordinates (lat/lon encoded as 0,0). - _contacts[existingIndex] = contact.copyWith( - lastMessageAt: mergedLastMessageAt, - pathOverride: existing.pathOverride, // Preserve user's path choice - pathOverrideBytes: existing.pathOverrideBytes, - latitude: contact.latitude ?? existing.latitude, - longitude: contact.longitude ?? existing.longitude, + _contacts[existingIndex] = _mergeRefreshedContact( + existing, + contact, + keepLocationWhenMissing: true, ); appLogger.info( @@ -6703,7 +6947,12 @@ class MeshCoreConnector extends ChangeNotifier { tag: 'Connector', ); } else { - _contacts.add(contact); + // A full sync cleared the list; restore a telemetry fix it held. + final stashed = _telemetryFixesDuringSync.remove(contact.publicKeyHex); + _contacts.add( + (stashed == null ? null : _withTelemetryFix(stashed, contact)) ?? + contact, + ); appLogger.info( 'Added new contact ${contact.name}: pathLen=${contact.pathLength}', tag: 'Connector', @@ -6762,10 +7011,6 @@ class MeshCoreConnector extends ChangeNotifier { if (existingIndex >= 0) { final existing = _contacts[existingIndex]; - final mergedLastMessageAt = - existing.lastMessageAt.isAfter(contact.lastMessageAt) - ? existing.lastMessageAt - : contact.lastMessageAt; appLogger.info( 'Refreshing contact ${contact.name}: devicePath=${contact.pathLength}, existingOverride=${existing.pathOverride}', @@ -6773,11 +7018,7 @@ class MeshCoreConnector extends ChangeNotifier { ); // CRITICAL: Preserve user's path override when contact is refreshed from device - _contacts[existingIndex] = contact.copyWith( - lastMessageAt: mergedLastMessageAt, - pathOverride: existing.pathOverride, // Preserve user's path choice - pathOverrideBytes: existing.pathOverrideBytes, - ); + _contacts[existingIndex] = _mergeRefreshedContact(existing, contact); appLogger.info( 'After merge: pathOverride=${_contacts[existingIndex].pathOverride}, devicePath=${_contacts[existingIndex].pathLength}', @@ -6820,14 +7061,27 @@ class MeshCoreConnector extends ChangeNotifier { } } + /// Queues a save of the contact list; adverts and contact replies arrive + /// many times a second on a busy mesh. Future _persistContacts() async { - await _contactStore.saveContacts(_contacts); + _coalescedWrites.schedule( + 'contacts', + () => _contactStore.saveContacts(_contacts), + ); } Future _persistDiscoveredContacts() async { + _coalescedWrites.cancel('discovered'); await _discoveryContactStore.saveContacts(_discoveredContacts); } + void _scheduleDiscoveredContactsPersist() { + _coalescedWrites.schedule( + 'discovered', + () => _discoveryContactStore.saveContacts(_discoveredContacts), + ); + } + int _latestContactLastmod() { if (_contacts.isEmpty) return 0; var latest = 0; @@ -7201,7 +7455,7 @@ class MeshCoreConnector extends ChangeNotifier { void _ensureContactUrlImagesSettingLoaded(String contactKeyHex) { if (_contactUrlImagesEnabled.containsKey(contactKeyHex)) return; _contactSettingsStore.loadUrlImagesEnabled(contactKeyHex).then((enabled) { - if (_contactUrlImagesEnabled[contactKeyHex] == enabled) return; + if (_contactUrlImagesEnabled.containsKey(contactKeyHex)) return; _contactUrlImagesEnabled[contactKeyHex] = enabled; notifyListeners(); }); @@ -7353,7 +7607,12 @@ class MeshCoreConnector extends ChangeNotifier { } final label = channelName ?? _channelDisplayName(channelIndex); - if (_appSettingsService!.isChannelMuted(label)) return; + final notificationMode = _appSettingsService!.getChannelNotificationMode( + label, + ); + + // Check if channel is muted + if (notificationMode == ChannelNotificationMode.muted) return; // Reuse translation result only if completed and non-empty; else use original text final resolvedText = @@ -7362,6 +7621,14 @@ class MeshCoreConnector extends ChangeNotifier { translationResult.translatedText.trim().isNotEmpty) ? translationResult.translatedText.trim() : message.text.trim(); + + // Mentions are matched on the untranslated text. + if (notificationMode == ChannelNotificationMode.mentionsOnly) { + if (!textMentionsName(message.originalText ?? message.text, _selfName)) { + return; + } + } + unawaited(() async { await _notificationService.showChannelMessageNotification( channelName: label, @@ -7374,6 +7641,22 @@ class MeshCoreConnector extends ChangeNotifier { }()); } + /// Whether [text] mentions [name], case-insensitively, as either + /// `@[name]` (the reply/mention prefix the app sends) or a plain `@name`. + /// The plain form needs a word boundary on both sides, so `@bobby` does + /// not mention `bob` and `x@bob.com` is not a mention. Names containing + /// spaces must be written out in full. + @visibleForTesting + static bool textMentionsName(String text, String? name) { + if (name == null || name.isEmpty) return false; + final escaped = RegExp.escape(name); + return RegExp( + '@\\[$escaped\\]|(? 0) messages.removeRange(0, overflow); } - // Save to persistent storage - _saveChannelMessages(channelIndex, messages); + _scheduleChannelSave(channelIndex); return isNew; } @@ -8522,6 +8805,7 @@ class MeshCoreConnector extends ChangeNotifier { } void _handleDisconnection() { + unawaited(_coalescedWrites.flush()); _resetLinkSessionState(); _notifySubscription?.cancel(); @@ -8568,6 +8852,18 @@ class MeshCoreConnector extends ChangeNotifier { _hasReceivedDeviceInfo = false; _batteryMillivolts = null; _batteryRequested = false; + final alertDeviceKey = _lowBatteryAlertDeviceKey; + if (alertDeviceKey != null && _batteryAlertedThresholds.isNotEmpty) { + unawaited( + _notificationService.clearLowBatteryNotifications( + alertDeviceKey, + _batteryAlertedThresholds.toList(), + ), + ); + } + _batteryAlertedThresholds.clear(); + _lowBatteryAlertDeviceKey = null; + _lowBatteryReadingStreak = 0; _pathHashByteWidth = 1; _maxContacts = _defaultMaxContacts; _maxChannels = _defaultMaxChannels; @@ -8581,6 +8877,9 @@ class MeshCoreConnector extends ChangeNotifier { /// Drops the conversation and channel history held in memory, so a /// different radio never shows or persists another radio's messages. void _clearSessionMessageState() { + // Writes start synchronously, so they still read this radio's history and + // store keys before both change below. + unawaited(_coalescedWrites.flush()); _conversations.clear(); _loadedConversationKeys.clear(); _channelMessages.clear(); @@ -8714,6 +9013,7 @@ class MeshCoreConnector extends ChangeNotifier { @override void dispose() { _disposed = true; + _appSettingsService?.removeListener(_onAppSettingsChanged); _scanSubscription?.cancel(); _isScanningSubscription?.cancel(); _connectionSubscription?.cancel(); @@ -8722,6 +9022,7 @@ class MeshCoreConnector extends ChangeNotifier { _notificationReplySubscription?.cancel(); _notificationService.releaseReplyPort(); _notifyListenersTimer?.cancel(); + unawaited(_coalescedWrites.flush()); _reconnectTimer?.cancel(); _batteryPollTimer?.cancel(); _gpsLocationPollTimer?.cancel(); @@ -8826,14 +9127,19 @@ class MeshCoreConnector extends ChangeNotifier { final flags = packet.readByte(); type = flags & 0x0F; hasLocation = (flags & 0x10) != 0; - // For future use: - //final hasFeature1 = (flags & 0x20) != 0; - //final hasFeature2 = (flags & 0x40) != 0; hasName = (flags & 0x80) != 0; if (hasLocation && packet.remaining >= 8) { latitude = packet.readInt32LE() / 1e6; longitude = packet.readInt32LE() / 1e6; } + // Two optional 16-bit feature fields sit between the location and the + // name (AdvertDataParser in MeshCore AdvertDataHelpers.cpp). Skip them + // so their bytes aren't read as the start of the name. + for (final featureMask in const [0x20, 0x40]) { + if ((flags & featureMask) != 0 && packet.remaining >= 2) { + packet.skipBytes(2); + } + } if (hasName && packet.remaining > 0) { name = packet.readCString(); } @@ -8903,14 +9209,19 @@ class MeshCoreConnector extends ChangeNotifier { final flags = advert.readByte(); type = flags & 0x0F; hasLocation = (flags & 0x10) != 0; - // For future use: - //final hasFeature1 = (flags & 0x20) != 0; - //final hasFeature2 = (flags & 0x40) != 0; hasName = (flags & 0x80) != 0; if (hasLocation && advert.remaining >= 8) { latitude = advert.readInt32LE() / 1e6; longitude = advert.readInt32LE() / 1e6; } + // Two optional 16-bit feature fields sit between the location and the + // name (AdvertDataParser in MeshCore AdvertDataHelpers.cpp). Skip them + // so their bytes aren't read as the start of the name. + for (final featureMask in const [0x20, 0x40]) { + if ((flags & featureMask) != 0 && advert.remaining >= 2) { + advert.skipBytes(2); + } + } // Validate location values if present hasLocation = hasValidLocation(latitude, longitude); @@ -9188,22 +9499,23 @@ class MeshCoreConnector extends ChangeNotifier { // Update existing contact if (existingIndex >= 0) { - _discoveredContacts[existingIndex] = _discoveredContacts[existingIndex] - .copyWith( - rawPacket: rawPacket, - name: contact.name, - type: contact.type, - pathLength: contact.pathLength, - path: contact.path, - pathHashWidth: contact.pathHashWidth, - latitude: contact.latitude, - longitude: contact.longitude, - lastSeen: contact.lastSeen, - flags: 0, - isActive: addActive, - ); + final existing = _discoveredContacts[existingIndex]; + _discoveredContacts[existingIndex] = existing.copyWith( + rawPacket: rawPacket, + // A nameless advert or removal must not wipe a known name. + name: contact.name.isEmpty ? existing.name : contact.name, + type: contact.type, + pathLength: contact.pathLength, + path: contact.path, + pathHashWidth: contact.pathHashWidth, + latitude: contact.latitude, + longitude: contact.longitude, + lastSeen: contact.lastSeen, + flags: 0, + isActive: addActive, + ); notifyListeners(); - unawaited(_persistDiscoveredContacts()); + _scheduleDiscoveredContactsPersist(); return; } @@ -9230,7 +9542,7 @@ class MeshCoreConnector extends ChangeNotifier { } _discoveredContacts.add(disContact); - unawaited(_persistDiscoveredContacts()); + _scheduleDiscoveredContactsPersist(); // Show notification for new contact (advertisement) if (_appSettingsService != null && !noNotify) { @@ -9260,7 +9572,7 @@ class MeshCoreConnector extends ChangeNotifier { void removeAllDiscoveredContacts() { _discoveredContacts.clear(); - unawaited(_persistDiscoveredContacts()); + _scheduleDiscoveredContactsPersist(); notifyListeners(); } diff --git a/lib/connector/meshcore_protocol.dart b/lib/connector/meshcore_protocol.dart index 126110ad..22476f63 100644 --- a/lib/connector/meshcore_protocol.dart +++ b/lib/connector/meshcore_protocol.dart @@ -497,8 +497,16 @@ String ackHashToHex(int ackHash) { } // Helper to convert public key to hex string +const String _hexDigits = '0123456789abcdef'; + String pubKeyToHex(Uint8List pubKey) { - return pubKey.map((b) => b.toRadixString(16).padLeft(2, '0')).join(); + final codes = Uint8List(pubKey.length * 2); + for (var i = 0; i < pubKey.length; i++) { + final b = pubKey[i]; + codes[i * 2] = _hexDigits.codeUnitAt(b >> 4); + codes[i * 2 + 1] = _hexDigits.codeUnitAt(b & 0x0F); + } + return String.fromCharCodes(codes); } // Helper to convert hex string to public key diff --git a/lib/connector/pending_command_replies.dart b/lib/connector/pending_command_replies.dart index a0dc9b5d..094846e2 100644 --- a/lib/connector/pending_command_replies.dart +++ b/lib/connector/pending_command_replies.dart @@ -5,6 +5,16 @@ import 'package:flutter/foundation.dart'; import '../services/image_chunk_transport.dart' show cmdSendChannelData; import 'meshcore_protocol.dart'; +/// RESP_CODE_ERR received for a command that was waiting for its reply. +class FirmwareCommandException implements Exception { + final int errCode; + + const FirmwareCommandException(this.errCode); + + @override + String toString() => 'Exception: Command failed with error code $errCode'; +} + /// A command waiting for its reply from the companion firmware. class PendingCommandReply { final int commandCode; @@ -203,9 +213,7 @@ class PendingCommandReplies { if (_queue.isEmpty) return null; final pending = _queue.removeAt(0); if (pending.isTombstone) return null; - pending.completer?.completeError( - Exception('Command failed with error code $errCode'), - ); + pending.completer?.completeError(FirmwareCommandException(errCode)); return pending; } diff --git a/lib/helpers/holiday_triggers.dart b/lib/helpers/holiday_triggers.dart new file mode 100644 index 00000000..54b4c50e --- /dev/null +++ b/lib/helpers/holiday_triggers.dart @@ -0,0 +1,99 @@ +/// Animation trigger messages: holiday greetings that play a short +/// full-screen animation when they arrive in (or are sent to) an open chat. +/// +/// The greetings are translation strings (`holidayGreeting_*` in the ARB +/// files, alternatives separated by `|`). A message is checked against the +/// greetings of *every* app language, since the sender may write in any of +/// them, and matching ignores case and punctuation. +library; + +import 'dart:ui' show Locale; + +import '../l10n/app_localizations.dart'; + +enum HolidayAnimation { halloween, thanksgiving, christmas, newYear } + +/// The greeting string for [animation] in one language. +String holidayGreetingsIn(AppLocalizations l10n, HolidayAnimation animation) => + switch (animation) { + HolidayAnimation.halloween => l10n.holidayGreeting_halloween, + HolidayAnimation.thanksgiving => l10n.holidayGreeting_thanksgiving, + HolidayAnimation.christmas => l10n.holidayGreeting_christmas, + HolidayAnimation.newYear => l10n.holidayGreeting_newYear, + }; + +final RegExp _apostrophes = RegExp("['’‘`´]"); +final RegExp _nonWord = RegExp(r'[^\p{L}\p{N}]+', unicode: true); + +/// Scripts written without spaces between words; their greetings are +/// matched anywhere in the text. +final RegExp _unspacedScript = RegExp(r'[぀-ヿ㐀-鿿豈-﫿]'); + +/// Lowercase, without apostrophes, with every run of punctuation or spaces +/// turned into a single space. +String _normalize(String text) => text + .toLowerCase() + .replaceAll(_apostrophes, '') + .replaceAll(_nonWord, ' ') + .trim(); + +class _Matcher { + final HolidayAnimation animation; + + /// Whole-word phrase with surrounding spaces, e.g. " happy new year ". + final String spaced; + + /// The phrase with spaces removed, for hashtags and unspaced scripts. + final String joined; + final bool unspaced; + + _Matcher(this.animation, String normalized) + : spaced = ' $normalized ', + joined = normalized.replaceAll(' ', ''), + unspaced = _unspacedScript.hasMatch(normalized); +} + +/// Matchers for every greeting in every supported language. +List<_Matcher> _buildMatchers() { + final seen = <(HolidayAnimation, String)>{}; + final matchers = <_Matcher>[]; + for (final locale in AppLocalizations.supportedLocales) { + final l10n = lookupAppLocalizations(Locale(locale.languageCode)); + for (final animation in HolidayAnimation.values) { + for (final phrase in holidayGreetingsIn(l10n, animation).split('|')) { + final normalized = _normalize(phrase); + // Skip blanks and greetings too short to be meaningful on their own. + if (normalized.replaceAll(' ', '').length < 3) continue; + if (seen.add((animation, normalized))) { + matchers.add(_Matcher(animation, normalized)); + } + } + } + } + // Longest first, so the most specific greeting wins: "Boas Festas e Feliz + // Ano Novo" is New Year's even though it contains "Boas Festas". + matchers.sort((a, b) => b.joined.length.compareTo(a.joined.length)); + return matchers; +} + +final List<_Matcher> _matchers = _buildMatchers(); + +/// The animation [text] triggers, or null. Greetings must be whole words +/// ("feliz natalia" is not "feliz natal"), except in scripts written +/// without spaces. A greeting run together as one word ("#HappyNewYear") +/// also counts. +HolidayAnimation? holidayAnimationFor(String text) { + if (text.isEmpty) return null; + final normalized = _normalize(text); + if (normalized.isEmpty) return null; + final spaced = ' $normalized '; + final joined = normalized.replaceAll(' ', ''); + for (final m in _matchers) { + if (m.unspaced) { + if (joined.contains(m.joined)) return m.animation; + } else if (spaced.contains(m.spaced) || spaced.contains(' ${m.joined} ')) { + return m.animation; + } + } + return null; +} diff --git a/lib/helpers/ipfs_link_attach_flow.dart b/lib/helpers/ipfs_link_attach_flow.dart new file mode 100644 index 00000000..e1cb6530 --- /dev/null +++ b/lib/helpers/ipfs_link_attach_flow.dart @@ -0,0 +1,147 @@ +import 'dart:convert'; + +import 'package:file_selector/file_selector.dart' as file_selector; +import 'package:flutter/material.dart'; +import 'package:provider/provider.dart'; + +import '../l10n/app_localizations.dart'; +import '../l10n/l10n.dart'; +import '../services/app_settings_service.dart'; +import 'ipfs_upload_helper.dart'; +import 'snack_bar_builder.dart'; + +/// Shared "send image via IPFS" flow for the direct-message and channel chat +/// screens: picks a file, uploads it with the configured IPFS settings and +/// puts the resulting link at the start of the composer draft. +/// +/// [maxBytes] is the screen's outbound byte limit and [encodeOutbound] turns +/// draft text into exactly what the send path measures (including any reply +/// prefix), so the limit is checked against that form. The draft is never +/// shortened: if the link won't fit, nothing is uploaded and the user is told +/// to shorten the message. +Future attachIpfsLinkToDraft({ + required BuildContext context, + required TextEditingController controller, + required FocusNode focusNode, + required int maxBytes, + required String Function(String text) encodeOutbound, +}) async { + final l10n = context.l10n; + final settings = context.read().settings; + if (settings.uploadIpfsEndpoint.trim().isEmpty) { + showDismissibleSnackBar( + context, + content: Text(l10n.chat_ipfsUploadNotConfigured), + ); + return; + } + + String draftWithLink(String messageUrl) => composeIpfsLinkDraft( + messageUrl: messageUrl, + existingText: controller.text.trim(), + ); + bool fits(String draft) => ipfsLinkDraftFits( + draft, + maxBytes: maxBytes, + encodeOutbound: encodeOutbound, + ); + + final sampleLink = IpfsUploadHelper.linkForCid( + uploadGateway: settings.uploadIpfsGateway, + cid: IpfsUploadHelper.sampleCid, + ); + if (!fits(draftWithLink(sampleLink))) { + showDismissibleSnackBar(context, content: Text(l10n.chat_ipfsLinkNoRoom)); + return; + } + + void showFailure(Object error) { + if (!context.mounted) return; + showDismissibleSnackBar( + context, + content: Text(ipfsUploadErrorText(l10n, error)), + ); + } + + final String messageUrl; + try { + final file = await file_selector.openFile(); + if (file == null || !context.mounted) return; + final bytes = await file.readAsBytes(); + if (!context.mounted) return; + if (bytes.isEmpty) { + showDismissibleSnackBar( + context, + content: Text(l10n.chat_ipfsSelectedFileEmpty), + ); + return; + } + messageUrl = await IpfsUploadHelper.uploadImageWithSettings( + settings: settings, + bytes: bytes, + fileName: file.name, + mimeType: IpfsUploadHelper.mimeTypeForFileName(file.name), + ); + } on Exception catch (error) { + showFailure(error); + return; + } catch (error, stackTrace) { + FlutterError.reportError( + FlutterErrorDetails( + exception: error, + stack: stackTrace, + library: 'ipfs link attach', + ), + ); + showFailure(error); + return; + } + if (!context.mounted) return; + + final messageText = draftWithLink(messageUrl); + controller.value = TextEditingValue( + text: messageText, + selection: TextSelection.collapsed(offset: messageText.length), + ); + focusNode.requestFocus(); + if (!fits(messageText)) { + showDismissibleSnackBar( + context, + content: Text(l10n.chat_ipfsLinkOverLimit), + ); + } +} + +/// The localized snackbar text for an upload [error]: config problems get a +/// specific message, anything else a generic one with technical details. +@visibleForTesting +String ipfsUploadErrorText(AppLocalizations l10n, Object error) { + if (error is IpfsUploadConfigException) { + return switch (error.reason) { + IpfsUploadConfigError.emptyEndpoint => l10n.chat_ipfsUploadNotConfigured, + IpfsUploadConfigError.invalidEndpoint => l10n.chat_ipfsEndpointInvalid, + IpfsUploadConfigError.pinataTokenMissing => + l10n.chat_ipfsPinataTokenMissing, + IpfsUploadConfigError.filebaseTokenMissing => + l10n.chat_ipfsFilebaseTokenMissing, + IpfsUploadConfigError.kuboPathInvalid => l10n.chat_ipfsKuboPathInvalid, + }; + } + final details = '$error'.replaceFirst(RegExp(r'^Exception: '), ''); + return l10n.chat_ipfsUploadFailed(details); +} + +/// Puts [messageUrl] in front of [existingText], leaving the text intact. +@visibleForTesting +String composeIpfsLinkDraft({ + required String messageUrl, + required String existingText, +}) => existingText.isEmpty ? '$messageUrl ' : '$messageUrl\n$existingText'; + +/// Whether [draft], encoded the way the send path encodes it, fits [maxBytes]. +@visibleForTesting +bool ipfsLinkDraftFits( + String draft, { + required int maxBytes, + required String Function(String text) encodeOutbound, +}) => utf8.encode(encodeOutbound(draft.trim())).length <= maxBytes; diff --git a/lib/helpers/ipfs_upload_helper.dart b/lib/helpers/ipfs_upload_helper.dart new file mode 100644 index 00000000..7f6e36e1 --- /dev/null +++ b/lib/helpers/ipfs_upload_helper.dart @@ -0,0 +1,228 @@ +import 'dart:convert'; + +import 'package:flutter/foundation.dart'; +import 'package:http/http.dart' as http; + +import '../models/app_settings.dart'; + +/// Why the IPFS upload settings can't be used. +enum IpfsUploadConfigError { + emptyEndpoint, + invalidEndpoint, + pinataTokenMissing, + filebaseTokenMissing, + kuboPathInvalid, +} + +/// Thrown when the IPFS upload settings can't be used, before or instead of +/// a network failure. The UI maps [reason] to a localized message. +class IpfsUploadConfigException implements Exception { + const IpfsUploadConfigException(this.reason); + + final IpfsUploadConfigError reason; + + @override + String toString() => 'IpfsUploadConfigException(${reason.name})'; +} + +class IpfsUploadHelper { + @visibleForTesting + static http.Client? httpClient; + + static final _kuboAddPath = RegExp(r'/api/v0/add/?$'); + + /// A CIDv1 (base32, sha2-256) as long as the CID of every upload, used to + /// size the link before uploading. + static const sampleCid = + 'bafkreihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku'; + + static String linkForCid({ + required String uploadGateway, + required String cid, + }) { + final gateway = normalizeIpfsGateway(uploadGateway); + return gateway.isEmpty ? 'ipfs://$cid' : '$gateway$cid'; + } + + static String mimeTypeForFileName(String fileName) { + switch (fileName.toLowerCase().split('.').last) { + case 'jpg': + case 'jpeg': + return 'image/jpeg'; + case 'png': + return 'image/png'; + case 'gif': + return 'image/gif'; + case 'webp': + return 'image/webp'; + case 'heic': + return 'image/heic'; + default: + return 'application/octet-stream'; + } + } + + static Future uploadImageWithSettings({ + required AppSettings settings, + required Uint8List bytes, + required String fileName, + required String mimeType, + }) { + return uploadImage( + bytes: bytes, + fileName: fileName, + mimeType: mimeType, + uploadEndpoint: settings.uploadIpfsEndpoint, + uploadApiKey: settings.uploadIpfsApiKey, + uploadGateway: settings.uploadIpfsGateway, + ); + } + + static Future uploadImage({ + required Uint8List bytes, + required String fileName, + required String mimeType, + required String uploadEndpoint, + required String uploadApiKey, + String uploadGateway = '', + }) async { + final endpoint = uploadEndpoint.trim(); + if (endpoint.isEmpty) { + throw const IpfsUploadConfigException( + IpfsUploadConfigError.emptyEndpoint, + ); + } + + final endpointUri = Uri.tryParse(endpoint); + if (endpointUri == null || + !(endpointUri.isScheme('http') || endpointUri.isScheme('https')) || + endpointUri.host.isEmpty) { + throw const IpfsUploadConfigException( + IpfsUploadConfigError.invalidEndpoint, + ); + } + + final token = uploadApiKey.trim(); + final endpointHost = endpointUri.host.toLowerCase(); + final isPinata = + endpointHost.contains('pinata') || + endpoint.toLowerCase().contains('pinfiletoipfs'); + final isFilebase = endpointHost.contains('filebase'); + final isKubo = !isPinata && !isFilebase; + final safeFileName = _sanitizeUploadFileName(fileName); + if (isPinata && token.isEmpty) { + throw const IpfsUploadConfigException( + IpfsUploadConfigError.pinataTokenMissing, + ); + } + if (isFilebase && token.isEmpty) { + throw const IpfsUploadConfigException( + IpfsUploadConfigError.filebaseTokenMissing, + ); + } + if (isKubo && !_kuboAddPath.hasMatch(endpointUri.path)) { + throw const IpfsUploadConfigException( + IpfsUploadConfigError.kuboPathInvalid, + ); + } + + final uploadUri = isPinata + ? endpointUri + : endpointUri.replace( + queryParameters: { + ...endpointUri.queryParameters, + 'cid-version': '1', + if (isKubo) 'pin': 'true', + }, + ); + + final request = http.MultipartRequest('POST', uploadUri) + ..files.add( + http.MultipartFile.fromBytes('file', bytes, filename: safeFileName), + ); + if (token.isNotEmpty) { + request.headers['Authorization'] = 'Bearer $token'; + } + if (isPinata) { + request.fields['pinataOptions'] = jsonEncode({'cidVersion': 1}); + request.fields['pinataMetadata'] = jsonEncode({'name': safeFileName}); + } + + final client = httpClient; + final response = await (client == null + ? request.send() + : client.send(request)); + final body = await response.stream.bytesToString(); + if (response.statusCode < 200 || response.statusCode >= 300) { + throw Exception('IPFS upload failed (${response.statusCode}): $body'); + } + + final cid = _extractUploadCid(body, isPinata: isPinata); + if (cid == null || cid.isEmpty) { + throw Exception('IPFS upload did not return a CID.'); + } + + if (isKubo) { + await _copyKuboFileToMfs( + addUri: endpointUri, + cid: cid, + fileName: safeFileName, + authorization: token.isEmpty ? null : 'Bearer $token', + ); + } + + return linkForCid(uploadGateway: uploadGateway, cid: cid); + } + + static Future _copyKuboFileToMfs({ + required Uri addUri, + required String cid, + required String fileName, + required String? authorization, + }) async { + final mfsPath = '/meshcore_uploads/$cid-$fileName'; + final copyUri = addUri.replace( + path: addUri.path.replaceFirst(_kuboAddPath, '/api/v0/files/cp'), + query: + 'arg=${Uri.encodeQueryComponent('/ipfs/$cid')}' + '&arg=${Uri.encodeQueryComponent(mfsPath)}&parents=true', + ); + final headers = authorization == null + ? {} + : {'Authorization': authorization}; + final client = httpClient; + final response = await (client == null + ? http.post(copyUri, headers: headers) + : client.post(copyUri, headers: headers)); + final alreadyCopied = response.body.contains('already has entry'); + if ((response.statusCode < 200 || response.statusCode >= 300) && + !alreadyCopied) { + throw Exception( + 'Kubo MFS import failed (${response.statusCode}): ${response.body}', + ); + } + } + + static String _sanitizeUploadFileName(String name) { + final cleaned = name.replaceAll(RegExp(r'[<>:"/\\|?*]'), '_').trim(); + return cleaned.isEmpty ? 'shared-image' : cleaned; + } + + static String? _extractUploadCid(String body, {required bool isPinata}) { + final lines = const LineSplitter() + .convert(body) + .where((line) => line.trim().isNotEmpty) + .toList(); + for (final line in lines.reversed) { + try { + final decoded = jsonDecode(line); + if (decoded is! Map) continue; + final value = isPinata ? decoded['IpfsHash'] : decoded['Hash']; + if (value is String && value.isNotEmpty) return value; + } catch (_) { + continue; + } + } + return null; + } +} diff --git a/lib/helpers/link_handler.dart b/lib/helpers/link_handler.dart index 41acba45..8a4752d7 100644 --- a/lib/helpers/link_handler.dart +++ b/lib/helpers/link_handler.dart @@ -77,6 +77,7 @@ class LinkHandler { final shouldOpen = await showDialog( context: context, builder: (context) => AlertDialog( + scrollable: true, title: Text(context.l10n.chat_openLink), content: Column( mainAxisSize: MainAxisSize.min, diff --git a/lib/helpers/marker_message.dart b/lib/helpers/marker_message.dart new file mode 100644 index 00000000..52f251c6 --- /dev/null +++ b/lib/helpers/marker_message.dart @@ -0,0 +1,30 @@ +import 'package:latlong2/latlong.dart'; + +class MarkerPayload { + final LatLng position; + final String label; + final String flags; + + MarkerPayload({ + required this.position, + required this.label, + required this.flags, + }); +} + +/// Parse a shared marker text message of the form +/// `m:,|