From 25f197c979ce6b476a32ff08565b724d2103fb28 Mon Sep 17 00:00:00 2001 From: zjs81 Date: Sun, 11 Oct 2026 01:25:31 -0700 Subject: [PATCH] Fix review findings, add default flood scope, and address recent issues Review fixes: - Keep stored message history beyond the 200-message window; stop messages leaking between radios; reset link/session state on disconnect - Accept firmware's 176-byte RX frames over USB/TCP - Hold APP_START during contact sync; contact-sync watchdog; replay DMs from not-yet-known senders; tombstone late command replies; finish channel sync on ERR_NOT_FOUND; fail region-scoped sends cleanly - DM retry watchdog when RESP_CODE_SENT never arrives; late repeater CLI replies; ACK hash collisions; UTF-8-safe name truncation - Image pipeline: delete-during-decode, lifecycle cancels, decoder memory release, entry cap, dead worker recovery, atomic model writes - Storage: deterministic reaction hash across web/native, Smaz double-decode, per-entry parsing, per-radio path history and regions, review-mode data isolation - UI: safe channel slot selection, byte-length name limits, double-send guard, mounted checks, localized hardcoded strings - Skip persisted-store reload on repeat SELF_INFO Features: - Radio default flood scope (firmware ver 11+) and explicit unscoped sends (ver 12+) - Scan for nearby nodes (#570) - Optional distinct first-hop repeater counting for channel resend (#589) - Optional region next to hop count (#580) Issues: - Message text follows system font size (#569) - Repeater/room password field accepts 20 chars (#576) - TCP auto-reconnect and keepalive (#571) - Order feeds by local receive time; flag future timestamps (#584) - Coding rate help text (#568), French "Favoris" (#575) - iOS/macOS app badge now clears Translations for all new strings in every locale. --- documentation/ble-protocol.md | 7 +- documentation/regions.md | 32 +- ios/Runner/AppDelegate.swift | 21 + lib/connector/meshcore_connector.dart | 831 +++++++++++++++--- lib/connector/meshcore_protocol.dart | 165 +++- lib/connector/pending_command_replies.dart | 49 +- lib/helpers/channel_resend_coverage.dart | 53 ++ lib/helpers/cyr2lat.dart | 4 +- lib/helpers/link_handler.dart | 16 + lib/helpers/reaction_helper.dart | 25 +- lib/helpers/smaz.dart | 24 +- lib/helpers/utf8_length_limiter.dart | 30 + lib/l10n/app_bg.arb | 56 +- lib/l10n/app_de.arb | 56 +- lib/l10n/app_en.arb | 108 ++- lib/l10n/app_es.arb | 56 +- lib/l10n/app_fr.arb | 58 +- lib/l10n/app_hu.arb | 56 +- lib/l10n/app_it.arb | 56 +- lib/l10n/app_ja.arb | 56 +- lib/l10n/app_ko.arb | 56 +- lib/l10n/app_localizations.dart | 314 ++++++- lib/l10n/app_localizations_bg.dart | 207 ++++- lib/l10n/app_localizations_de.dart | 206 ++++- lib/l10n/app_localizations_en.dart | 204 ++++- lib/l10n/app_localizations_es.dart | 207 ++++- lib/l10n/app_localizations_fr.dart | 209 ++++- lib/l10n/app_localizations_hu.dart | 206 ++++- lib/l10n/app_localizations_it.dart | 207 ++++- lib/l10n/app_localizations_ja.dart | 199 ++++- lib/l10n/app_localizations_ko.dart | 199 ++++- lib/l10n/app_localizations_nl.dart | 206 ++++- lib/l10n/app_localizations_pl.dart | 210 ++++- lib/l10n/app_localizations_pt.dart | 207 ++++- lib/l10n/app_localizations_ru.dart | 210 ++++- lib/l10n/app_localizations_sk.dart | 210 ++++- lib/l10n/app_localizations_sl.dart | 209 ++++- lib/l10n/app_localizations_sv.dart | 205 ++++- lib/l10n/app_localizations_uk.dart | 210 ++++- lib/l10n/app_localizations_zh.dart | 196 ++++- lib/l10n/app_nl.arb | 56 +- lib/l10n/app_pl.arb | 56 +- lib/l10n/app_pt.arb | 56 +- lib/l10n/app_ru.arb | 56 +- lib/l10n/app_sk.arb | 56 +- lib/l10n/app_sl.arb | 56 +- lib/l10n/app_sv.arb | 56 +- lib/l10n/app_uk.arb | 56 +- lib/l10n/app_zh.arb | 56 +- lib/main.dart | 14 +- lib/models/app_settings.dart | 17 + lib/models/channel_message.dart | 15 + lib/models/message.dart | 31 + lib/review_mode/review_mode_storage.dart | 33 +- lib/review_mode/review_radio.dart | 71 +- lib/screens/app_settings_screen.dart | 35 +- lib/screens/channel_chat_screen.dart | 152 +++- lib/screens/channels_screen.dart | 151 +++- lib/screens/chat_screen.dart | 30 +- lib/screens/community_qr_scanner_screen.dart | 30 +- lib/screens/discovery_screen.dart | 7 + lib/screens/line_of_sight_map_screen.dart | 63 +- lib/screens/map_cache_screen.dart | 1 + lib/screens/map_screen.dart | 65 +- lib/screens/path_trace_map.dart | 9 +- lib/screens/region_management_screen.dart | 97 +- lib/screens/repeater_cli_screen.dart | 25 +- lib/screens/repeater_settings_screen.dart | 65 +- lib/screens/scanner_screen.dart | 5 +- lib/screens/settings_screen.dart | 111 +-- lib/services/app_settings_service.dart | 10 + lib/services/image_chunk_transport.dart | 13 +- lib/services/image_codec_backend.dart | 6 + lib/services/image_codec_file_store_io.dart | 29 +- lib/services/image_codec_service.dart | 30 +- lib/services/image_codec_session_io.dart | 26 +- lib/services/image_codec_session_stub.dart | 2 + lib/services/message_retry_service.dart | 159 +++- lib/services/notification_service.dart | 36 +- .../received_image_blob_store_io.dart | 12 +- lib/services/received_image_store.dart | 250 ++++-- lib/services/repeater_command_service.dart | 146 ++- lib/services/storage_service.dart | 47 +- .../tcp_transport_service_native.dart | 80 ++ lib/services/timeout_prediction_service.dart | 5 + lib/services/usb_serial_frame_codec.dart | 6 +- lib/services/usb_serial_service_web.dart | 14 +- lib/storage/channel_message_store.dart | 160 +++- lib/storage/channel_region_store.dart | 23 +- lib/storage/channel_store.dart | 18 +- lib/storage/contact_discovery_store.dart | 3 +- lib/storage/message_store.dart | 154 +++- lib/storage/region_store.dart | 21 +- lib/storage/unread_store.dart | 16 +- lib/widgets/future_timestamp_indicator.dart | 26 + lib/widgets/gif_message.dart | 6 +- lib/widgets/node_scan_sheet.dart | 310 +++++++ lib/widgets/qr_scanner_widget.dart | 12 +- lib/widgets/received_image_message.dart | 15 +- lib/widgets/repeater_login_dialog.dart | 3 +- lib/widgets/room_login_dialog.dart | 3 +- macos/Runner/MainFlutterWindow.swift | 14 + test/connector/default_flood_scope_test.dart | 238 +++++ .../meshcore_protocol_strings_test.dart | 69 ++ test/connector/node_discover_test.dart | 206 +++++ .../pending_command_replies_test.dart | 48 + test/connector/tcp_auto_reconnect_test.dart | 210 +++++ .../helpers/channel_resend_coverage_test.dart | 142 +++ test/helpers/utf8_length_limiter_test.dart | 25 + test/models/received_at_test.dart | 217 +++++ test/models/region_on_hops_line_test.dart | 21 + test/reaction_helper_test.dart | 35 + .../services/message_retry_watchdog_test.dart | 95 ++ test/services/received_image_store_test.dart | 150 ++++ .../repeater_command_service_test.dart | 81 ++ .../tcp_transport_service_native_test.dart | 59 ++ .../services/usb_serial_frame_codec_test.dart | 31 +- test/storage/message_store_archive_test.dart | 223 +++++ test/storage/scoped_store_fixes_test.dart | 184 ++++ .../translated_message_content_test.dart | 55 ++ 120 files changed, 10424 insertions(+), 687 deletions(-) create mode 100644 lib/helpers/channel_resend_coverage.dart create mode 100644 lib/widgets/future_timestamp_indicator.dart create mode 100644 lib/widgets/node_scan_sheet.dart create mode 100644 test/connector/default_flood_scope_test.dart create mode 100644 test/connector/meshcore_protocol_strings_test.dart create mode 100644 test/connector/node_discover_test.dart create mode 100644 test/connector/tcp_auto_reconnect_test.dart create mode 100644 test/helpers/channel_resend_coverage_test.dart create mode 100644 test/models/received_at_test.dart create mode 100644 test/models/region_on_hops_line_test.dart create mode 100644 test/services/message_retry_watchdog_test.dart create mode 100644 test/storage/message_store_archive_test.dart create mode 100644 test/storage/scoped_store_fixes_test.dart create mode 100644 test/widgets/translated_message_content_test.dart diff --git a/documentation/ble-protocol.md b/documentation/ble-protocol.md index a8a67bf3..8bd742ad 100644 --- a/documentation/ble-protocol.md +++ b/documentation/ble-protocol.md @@ -112,7 +112,7 @@ On unexpected disconnection, auto-reconnect with exponential backoff: | 40 | CMD_GET_CUSTOM_VAR | Get custom variables | | 41 | CMD_SET_CUSTOM_VAR | Set a custom variable | | 50 | CMD_SEND_BINARY_REQ | Send binary request | -| 54 | CMD_SET_FLOOD_SCOPE_KEY | Set flood routing scope (v8+; `cmdSetFloodScope` in the app) | +| 54 | CMD_SET_FLOOD_SCOPE_KEY | Set flood routing scope (v8+; `cmdSetFloodScope` in the app). `[54,0,key16]` sets, `[54,0]` clears the override (radio default scope then applies), `[54,1]` sends unscoped (firmware code 12+) | | 55 | CMD_SEND_CONTROL_DATA | Send control data (e.g. zero-hop discovery, v8+) | | 56 | CMD_GET_STATS | Request companion radio stats | | 57 | CMD_SEND_ANON_REQ | Send anonymous request | @@ -120,6 +120,8 @@ On unexpected disconnection, auto-reconnect with exponential backoff: | 59 | CMD_GET_AUTO_ADD_CONFIG | Get auto-add configuration | | 61 | CMD_SET_PATH_HASH_MODE | Set path hash width (bytes per hop) | | 62 | CMD_SEND_CHANNEL_DATA | Send GRP_DATA image chunks (firmware code 11+) | +| 63 | CMD_SET_DEFAULT_FLOOD_SCOPE | Store the radio's default flood scope: `[63][name 31, NUL padded][key 16]`, or `[63]` to clear (firmware code 11+) | +| 64 | CMD_GET_DEFAULT_FLOOD_SCOPE | Read the radio's default flood scope (firmware code 11+) | ## Response / Push Codes (Device → App) @@ -146,6 +148,7 @@ On unexpected disconnection, auto-reconnect with exponential backoff: | 24 | RESP_CODE_STATS | Companion radio stats | | 25 | RESP_CODE_AUTO_ADD_CONFIG | Auto-add flags | | 27 | RESP_CODE_CHANNEL_DATA_RECV | Received GRP_DATA image chunk | +| 28 | RESP_CODE_DEFAULT_FLOOD_SCOPE | `[28][name 31][key 16]`, or `[28]` alone when no default is set | | 0x80 | PUSH_CODE_ADVERT | Known contact re-seen | | 0x81 | PUSH_CODE_PATH_UPDATED | Better path found; carries the 32-byte public key of the updated contact | | 0x82 | PUSH_CODE_SEND_CONFIRMED | Delivery ACK from remote; carries ACK hash (4 bytes) + trip time (4 bytes) | @@ -272,6 +275,6 @@ Reactions use `r::`. Channel replies ## Regions and image data -Transport-routed raw packets include four transport-code bytes before packed path metadata. For transport floods, the app matches the first two code bytes against locally known region candidates using HMAC-SHA256 over `[payloadType][payload]`. Region scope keys are the first 16 bytes of SHA256 of `#`. The name itself is not carried as plain text. Unknown or ambiguous matches remain null; see [regions](regions.md). +Transport-routed raw packets include four transport-code bytes before packed path metadata. For transport floods, the app matches the first two code bytes against locally known region candidates using HMAC-SHA256 over `[payloadType][payload]`. Region scope keys are the first 16 bytes of SHA256 of `#`. The radio's default flood scope is also a candidate, matched on the key the radio reports (it may have been set by another client). The name itself is not carried as plain text. Unknown or ambiguous matches remain null; see [regions](regions.md). Image chunks use `CMD_SEND_CHANNEL_DATA` (62), `RESP_CODE_CHANNEL_DATA_RECV` (27), and application data type `0xAE1C`. The framing, capacities, metadata byte, and XOR recovery algorithm are defined in [image_chunk_transport.dart](../lib/services/image_chunk_transport.dart), which is the source of truth. See [image messages](image-messages.md) for user requirements and limitations. diff --git a/documentation/regions.md b/documentation/regions.md index e6651fb2..e2a83d1a 100644 --- a/documentation/regions.md +++ b/documentation/regions.md @@ -7,12 +7,32 @@ Regions limit which configured repeaters forward a channel flood. They are routi 1. While connected, open Settings → Regions. 2. Add a region name manually, or choose Fetch from repeaters and add names from the results. Discovery queries nearby responding repeaters already present in your contacts; an empty result does not prove that no regions exist on the wider mesh. 3. Manual names accept lowercase letters, digits, and hyphens, up to 30 characters. Use the exact name configured by your local repeater operators. -4. Choose a Default region to use for channels without their own override. None means the app sets no default scope. -5. In channel chat, tap the title or landscape icon to select an override. The subtitle shows the effective region, with `(default)` when inherited. +4. Choose a Default region to use for channels without their own override. None means the app sets no default scope (the radio default region, if any, applies). +5. In channel chat, tap the title or landscape icon to select an override. The subtitle shows the effective region, with `(default)` when inherited from the app default or `(radio default)` when the radio's own default applies. -Tapping the selected region again, or choosing Clear region, removes the channel override. **If a default exists, clearing an override falls back to that default; it does not force an unscoped send.** With no override and the default set to None, the app sets no scope: the companion's scope override stays cleared (`[54, 0]`). Since companion firmware v1.15.0, the radio then falls back to its own default flood scope if one is configured on the device. The app does not issue an explicitly unscoped send. +Tapping the selected region again, or choosing Clear region, removes the channel override. **If a default exists, clearing an override falls back to that default; it does not force an unscoped send.** To send a channel truly unscoped, pick **No region (unscoped)** instead (see below). -The saved region-name list is global to the app. Default and channel assignments are stored per connected radio identity. Removing a region from the list also clears matching assignments stored for the current radio; it does not change repeater configuration. +## Radio default region + +Companion firmware v1.15.0 (firmware code 11) and later can store a default flood scope on the radio itself. The radio uses it for every flood sent without a scope override, by this app or any other app connected to the radio. + +- On supported firmware, Settings → Regions shows **Radio default region**. Choosing a region writes it to the radio (`CMD_SET_DEFAULT_FLOOD_SCOPE`, 63); choosing None clears it. The app reads it back after connecting and after each change (`CMD_GET_DEFAULT_FLOOD_SCOPE`, 64 → `RESP_CODE_DEFAULT_FLOOD_SCOPE`, 28). The key written is derived the same way as for app regions. +- On older firmware the setting is hidden and these commands are never sent. + +## Precedence and "No region" + +The scope used for a channel flood is decided in this order: + +1. **The channel's own assignment**: a region, or **No region (unscoped)**. +2. **The app default region**: a region, None, or **No region (unscoped)**. +3. **The radio default region**, when the app sets no scope (channel cleared and app default None). +4. Otherwise the flood is unscoped. + +**No region (unscoped)** is offered only on firmware code 12 and later (companion v1.16.0+), where the app can override the radio default with `[54, 1]` around the send. On firmware code 11 the option is hidden: floods without a region use the radio default, and the channel region picker says so when a radio default is set. On firmware without a radio default, None and No region behave the same. + +Replying with a message's original region (below) is a one-off override at step 1. + +The saved region-name list is global to the app. Default and channel assignments are stored per connected radio identity. Removing a region from the list also clears matching assignments stored for every radio; it does not change repeater configuration or the radio default region stored on a radio. ## Regions displayed on messages @@ -20,7 +40,7 @@ Channel text messages show a region label beside routing metadata when a region Outgoing messages retain the effective region used when they were sent. Changing a channel or default region does not relabel earlier messages. Region metadata persists with message history; older records may have no region. -Incoming region names are inferred from raw **transport-flood** packets by testing locally known names (saved regions, channel overrides, and the default). The name is not sent as plain text in the packet. Unknown names, ambiguous matches, and messages without suitable raw transport metadata do not get a region label. `foo` and `#foo` are treated as the same candidate for matching. +Incoming region names are inferred from raw **transport-flood** packets by testing locally known names (saved regions, channel overrides, the app default, and the radio default region). The name is not sent as plain text in the packet. Unknown names, ambiguous matches, and messages without suitable raw transport metadata do not get a region label. `foo` and `#foo` are treated as the same candidate for matching. **No label does not prove a message was unscoped.** It can mean that its region could not be identified. A region label is routing metadata, not proof of the sender’s location or identity. @@ -34,6 +54,6 @@ If the original message has no known region, the reply uses the channel’s effe ## Technical reference -Scoped sends temporarily set the companion’s flood scope, send the message, and then clear the scope override (`buildSetFloodScopeFrame('')`); they do not restore a previously set scope. Channel sends are serialized to prevent another channel from inheriting the temporary setting. Repeaters and companion firmware must support the relevant scope functionality. +Scoped sends temporarily set the companion’s flood scope (`[54, 0, key]`, or `[54, 1]` for No region on firmware code 12+), send the message, and then clear the scope override (`buildSetFloodScopeFrame('')`, `[54, 0]`); they do not restore a previously set scope. Channels with no scope to apply send without any scope command, so the radio default region applies. Channel sends are serialized to prevent another channel from inheriting the temporary setting. Repeaters and companion firmware must support the relevant scope functionality. See [channels](channels.md), [routing paths](routing-paths.md), and [protocol metadata](ble-protocol.md#regions-and-image-data). diff --git a/ios/Runner/AppDelegate.swift b/ios/Runner/AppDelegate.swift index c30b367e..5c38c90f 100644 --- a/ios/Runner/AppDelegate.swift +++ b/ios/Runner/AppDelegate.swift @@ -1,5 +1,6 @@ import Flutter import UIKit +import UserNotifications @main @objc class AppDelegate: FlutterAppDelegate, FlutterImplicitEngineDelegate { @@ -12,5 +13,25 @@ import UIKit func didInitializeImplicitFlutterEngine(_ engineBridge: FlutterImplicitEngineBridge) { GeneratedPluginRegistrant.register(with: engineBridge.pluginRegistry) + if let registrar = engineBridge.pluginRegistry.registrar(forPlugin: "MeshAppBadge") { + let channel = FlutterMethodChannel( + name: "meshcore_open/app_badge", + binaryMessenger: registrar.messenger() + ) + channel.setMethodCallHandler { call, result in + guard call.method == "setBadgeCount" else { + result(FlutterMethodNotImplemented) + return + } + let count = (call.arguments as? Int) ?? 0 + UNUserNotificationCenter.current().setBadgeCount(max(0, count)) { error in + if let error = error { + result(FlutterError(code: "badge", message: error.localizedDescription, details: nil)) + } else { + result(nil) + } + } + } + } } } diff --git a/lib/connector/meshcore_connector.dart b/lib/connector/meshcore_connector.dart index d5eb2282..d8bbf986 100644 --- a/lib/connector/meshcore_connector.dart +++ b/lib/connector/meshcore_connector.dart @@ -17,6 +17,7 @@ import '../models/message.dart'; import '../models/path_selection.dart'; import '../models/translation_support.dart'; import '../helpers/reaction_helper.dart'; +import '../helpers/channel_resend_coverage.dart'; import '../helpers/cyr2lat.dart'; import '../helpers/smaz.dart'; import '../services/app_debug_log_service.dart'; @@ -192,6 +193,9 @@ class MeshCoreConnector extends ChangeNotifier { String? _lastDeviceId; String? _lastDeviceDisplayName; bool _manualDisconnect = false; + // Bumped by every BLE connect attempt so a superseded attempt can tell it + // no longer owns the connector. + int _bleConnectGeneration = 0; /// Non-null while [disconnect] is tearing the transport down. Connect paths /// join it so a teardown cannot finish after a new connection and reset the @@ -226,8 +230,18 @@ class MeshCoreConnector extends ChangeNotifier { staleAfter: _commandAckTimeout * 2, ); static const String _reactionSendQueuePrefix = '__reaction_send__'; + // TXT_TYPE_CLI_COMMAND (firmware TxtDataHelpers.h). + static const int _txtTypeCliCommand = 3; + // ERR_CODE_NOT_FOUND (companion MyMesh.cpp). + static const int _errCodeNotFound = 2; int _reactionSendQueueSequence = 0; final Set _loadedConversationKeys = {}; + // Self public key of the radio whose messages are held in memory. + String? _messageStateOwnerKey; + // Self public key whose persisted stores were loaded this link session. + // Later SELF_INFO replies (refreshDeviceInfo after a settings change) skip + // the reload while it still matches. + String? _persistedStateLoadedKey; final Map> _processedChannelReactions = {}; // channelIndex -> Set of "targetHash_emoji" final Map> _processedContactReactions = @@ -249,6 +263,16 @@ class MeshCoreConnector extends ChangeNotifier { final ValueNotifier radioStatsNotifier = ValueNotifier(null); int _reconnectAttempts = 0; + // Endpoint of the last TCP link that completed its handshake. Set only + // after SELF_INFO, cleared on a manual disconnect or a user-started + // connect, so a failed first attempt never loops on a bad address. + String? _tcpReconnectHost; + int? _tcpReconnectPort; + // True while [connectTcp] runs on behalf of [_scheduleTcpReconnect]. + bool _tcpAutoReconnectAttempt = false; + // Bumped by each [connectTcp] and by every disconnect, so an attempt that + // was superseded mid-handshake cannot record a stale reconnect target. + int _tcpConnectGeneration = 0; bool _notifyListenersDirty = false; static const Duration _notifyListenersDebounce = Duration(milliseconds: 50); @@ -353,6 +377,19 @@ class MeshCoreConnector extends ChangeNotifier { bool _queuedMessageSyncInFlight = false; final List _deferredQueuedContactMessageFrames = []; bool _pendingQueueSync = false; + // CMD_APP_START stops the firmware's contact iterator (END_OF_CONTACTS is + // then never sent), so APP_START requested during a contact sync is held + // until the sync ends. + bool _appStartDeferredForContactSync = false; + // Ends a contact sync whose frames stopped arriving, so queued-message sync + // and deferred processing cannot stay blocked behind it. + Timer? _contactSyncWatchdog; + static const Duration _contactSyncStallTimeout = Duration(seconds: 20); + // Contact messages whose sender was not a known contact yet. The firmware + // already removed them from its offline queue, so they are held and + // replayed once the sender's CONTACT frame arrives (or the sync ends). + final List _unknownSenderMessageFrames = []; + static const int _maxUnknownSenderMessageFrames = 32; Timer? _queueSyncTimeout; int _queueSyncRetries = 0; static const int _maxQueueSyncRetries = 3; @@ -379,6 +416,9 @@ class MeshCoreConnector extends ChangeNotifier { int _nextChannelIndexToRequest = 0; int _totalChannelsToRequest = 0; List _previousChannelsCache = []; + // Slots whose GET_CHANNEL timed out in the last sync with no cached copy: + // their contents are unknown, so UI must not treat them as free. + final Set _unconfirmedChannelSlots = {}; static const int _maxChannelSyncRetries = 3; static const int _channelSyncTimeoutMs = 2000; // 2 second timeout per channel static const Duration _batteryPollInterval = Duration(seconds: 120); @@ -411,8 +451,10 @@ class MeshCoreConnector extends ChangeNotifier { final Map _channelCyr2LatProfileId = {}; final Map _channelRegions = {}; Region _defaultRegion = ''; - bool _lastSentWasCliCommand = - false; // Track if last sent message was a CLI command + // Radio-stored default flood scope (GET_DEFAULT_FLOOD_SCOPE, ver 11+). + // Null when the radio has none or has not reported it yet. + String? _defaultFloodScopeName; + Uint8List? _defaultFloodScopeKey; final Map _contactSmazEnabled = {}; final Map _contactCyr2LatEnabled = {}; final Map _contactUrlImagesEnabled = {}; @@ -458,7 +500,8 @@ class MeshCoreConnector extends ChangeNotifier { _state == MeshCoreConnectionState.connected && _activeTransport == MeshCoreTransportType.usb; bool get isAutoReconnectScheduled => - _shouldAutoReconnect && (_reconnectTimer?.isActive ?? false); + (_shouldAutoReconnect || _shouldAutoReconnectTcp) && + (_reconnectTimer?.isActive ?? false); String? get activeTcpEndpoint => _tcpConnector.activeEndpoint; bool get isTcpTransportConnected => _state == MeshCoreConnectionState.connected && @@ -519,6 +562,8 @@ class MeshCoreConnector extends ChangeNotifier { bool get hasLoadedContacts => _hasLoadedContacts; bool get isLoadingChannels => _isLoadingChannels; bool get hasLoadedChannels => _hasLoadedChannels; + Set get unconfirmedChannelSlots => + Set.unmodifiable(_unconfirmedChannelSlots); Stream get receivedFrames => _receivedFramesController.stream; Uint8List? get selfPublicKey => _selfPublicKey; String get selfPublicKeyHex => pubKeyToHex(_selfPublicKey ?? Uint8List(0)); @@ -570,6 +615,21 @@ class MeshCoreConnector extends ChangeNotifier { /// CMD_GET_ALLOWED_REPEAT_FREQ exists from ver code 9 (client repeat). bool get supportsAllowedRepeatFreq => (_firmwareVerCode ?? 0) >= 9; + /// CMD_SEND_CONTROL_DATA / PUSH_CODE_CONTROL_DATA (node discover) exist + /// from ver code 8. + bool get supportsNodeDiscover => (_firmwareVerCode ?? 0) >= 8; + + /// CMD_GET/SET_DEFAULT_FLOOD_SCOPE exist from ver code 11 (v1.15.0). + bool get supportsDefaultFloodScope => (_firmwareVerCode ?? 0) >= 11; + + /// CMD_SET_FLOOD_SCOPE `[54,1]` (send unscoped, overriding the radio's + /// default scope) exists from ver code 12. + bool get supportsExplicitUnscopedFlood => (_firmwareVerCode ?? 0) >= 12; + + /// Name of the radio's default flood scope, or null when none is set. + String? get defaultFloodScopeName => _defaultFloodScopeName; + Uint8List? get defaultFloodScopeKey => _defaultFloodScopeKey; + /// Frequency ranges (kHz, inclusive) on which the firmware accepts client /// repeat mode. Null until RESP_ALLOWED_REPEAT_FREQ has been received. List? get allowedRepeatFreqRanges => @@ -696,6 +756,10 @@ class MeshCoreConnector extends ChangeNotifier { final windowedMessages = allMessages.length > _messageWindowSize ? allMessages.sublist(allMessages.length - _messageWindowSize) : allMessages; + final archivedMessages = allMessages.sublist( + 0, + allMessages.length - windowedMessages.length, + ); final currentMessages = _conversations[contactKeyHex] ?? const []; @@ -723,13 +787,22 @@ class MeshCoreConnector extends ChangeNotifier { // Re-sort after merging persisted and in-memory messages so the // conversation window remains stable after optimistic inserts. - mergedMessages.sort((a, b) => a.timestamp.compareTo(b.timestamp)); - final windowedMergedMessages = mergedMessages.length > _messageWindowSize - ? mergedMessages.sublist(mergedMessages.length - _messageWindowSize) - : mergedMessages; + mergedMessages.sort((a, b) => a.receivedAt.compareTo(b.receivedAt)); + final trimCount = mergedMessages.length > _messageWindowSize + ? mergedMessages.length - _messageWindowSize + : 0; + final windowedMergedMessages = mergedMessages.sublist(trimCount); + // Everything stored ahead of the window stays in storage on the next + // save instead of being overwritten by the window. + _messageStore.setArchived(contactKeyHex, [ + ...archivedMessages, + ...mergedMessages.sublist(0, trimCount), + ]); _conversations[contactKeyHex] = windowedMergedMessages; notifyListeners(); + } else { + _messageStore.setArchived(contactKeyHex, const []); } } @@ -749,15 +822,20 @@ class MeshCoreConnector extends ChangeNotifier { final allMessages = await _messageStore.loadMessages(contactKeyHex); final currentMessages = _conversations[contactKeyHex] ?? []; - if (allMessages.length <= currentMessages.length) { + // Storage holds the archived entries followed by the in-memory window. + final currentOffset = + (_messageStore.archivedCount(contactKeyHex) ?? + allMessages.length - currentMessages.length) + .clamp(0, allMessages.length); + if (currentOffset == 0) { return []; // No more messages to load } - final currentOffset = allMessages.length - currentMessages.length; final fetchCount = count.clamp(0, currentOffset); final startIndex = currentOffset - fetchCount; final olderMessages = allMessages.sublist(startIndex, currentOffset); + _messageStore.unarchiveNewest(contactKeyHex, olderMessages.length); // Prepend to current conversation _conversations[contactKeyHex] = [...olderMessages, ...currentMessages]; @@ -858,19 +936,68 @@ class MeshCoreConnector extends ChangeNotifier { return _contactUrlImagesEnabled[contactKeyHex] ?? false; } + /// True when the channel has its own (real) region assigned. bool hasChannelRegion(int channelIndex) { - return (_channelRegions[channelIndex] ?? '').isNotEmpty; + return getChannelRegion(channelIndex).isNotEmpty; } + /// The channel's own region, or '' when it follows the default or is set + /// to "no region" (see [isChannelExplicitlyUnscoped]). Region getChannelRegion(int channelIndex) { - return _channelRegions[channelIndex] ?? ''; + final region = _channelRegions[channelIndex] ?? ''; + return region == kUnscopedRegion ? '' : region; } + /// True when the user picked "no region" for this channel, overriding the + /// app and radio defaults. + bool isChannelExplicitlyUnscoped(int channelIndex) { + return _channelRegions[channelIndex] == kUnscopedRegion; + } + + /// The app default region ('' for none). [kUnscopedRegion] when the app + /// default is "no region". Region get defaultRegion => _defaultRegion; + /// The region a flood on this channel is scoped to by the app, as a real + /// region name, or '' when the app sends without a region (in which case + /// the radio's default flood scope, if any, may still apply — see + /// [channelUsesRadioDefaultScope]). Region getEffectiveChannelRegion(int channelIndex) { - final region = getChannelRegion(channelIndex); - return region.isNotEmpty ? region : _defaultRegion; + final scope = _channelSendScope(channelIndex); + return scope == kUnscopedRegion ? '' : scope; + } + + /// True when floods on this channel fall through to the radio's default + /// flood scope: no app region applies and the radio has a default that + /// this firmware cannot (or was not asked to) override. + bool channelUsesRadioDefaultScope(int channelIndex) { + if (_defaultFloodScopeName == null) return false; + final scope = _channelSendScope(channelIndex); + if (scope.isEmpty) return true; + return scope == kUnscopedRegion && !supportsExplicitUnscopedFlood; + } + + /// Precedence: channel assignment (region or "no region"), then the app + /// default (region or "no region"), then '' (the radio default applies). + Region _channelSendScope(int channelIndex) { + final own = _channelRegions[channelIndex] ?? ''; + if (own.isNotEmpty) return own; + return _defaultRegion; + } + + /// Stores [region] on the radio as its default flood scope ('' clears it), + /// then re-reads it. Throws on firmware without the command, on ERR or on + /// timeout. + Future setDefaultFloodScope(String region) async { + if (!isConnected) return; + if (!supportsDefaultFloodScope) { + throw UnsupportedError('Firmware does not support default flood scope'); + } + await sendFrame( + buildSetDefaultFloodScopeFrame(region), + waitForGenericAck: true, + ); + await sendFrame(buildGetDefaultFloodScopeFrame()); } Future setDefaultRegion(Region region) async { @@ -904,6 +1031,14 @@ class MeshCoreConnector extends ChangeNotifier { _unreadStateLoaded = true; _recalculateCachedUnreadTotals(); notifyListeners(); + syncAppBadge(); + } + + /// Sets the app icon badge to the current unread total, e.g. on resume, so + /// a badge left by delivered notifications doesn't outlive the unreads. + void syncAppBadge() { + if (!_unreadStateLoaded) return; + unawaited(_notificationService.setAppBadge(getTotalUnreadCount())); } Future loadCachedChannels() async { @@ -1200,15 +1335,19 @@ class MeshCoreConnector extends ChangeNotifier { final allMessages = await _channelMessageStore.loadChannelMessages( channelIndex, ); - if (allMessages.isNotEmpty) { - // Keep only the most recent N messages in memory to bound memory usage - final windowedMessages = allMessages.length > _messageWindowSize - ? allMessages.sublist(allMessages.length - _messageWindowSize) - : allMessages; - - _channelMessages[channelIndex] = windowedMessages; - notifyListeners(); - } + // Keep only the most recent N messages in memory to bound memory usage + final windowedMessages = allMessages.length > _messageWindowSize + ? allMessages.sublist(allMessages.length - _messageWindowSize) + : allMessages; + _channelMessageStore.setArchived( + channelIndex, + allMessages.sublist(0, allMessages.length - windowedMessages.length), + ); + // Replace even when nothing is stored so another radio's messages never + // linger under this index. + final hadMessages = _channelMessages[channelIndex]?.isNotEmpty ?? false; + _channelMessages[channelIndex] = windowedMessages; + if (hadMessages || windowedMessages.isNotEmpty) notifyListeners(); } /// Load older channel messages (pagination) @@ -1221,15 +1360,20 @@ class MeshCoreConnector extends ChangeNotifier { ); final currentMessages = _channelMessages[channelIndex] ?? []; - if (allMessages.length <= currentMessages.length) { + // Storage holds the archived entries followed by the in-memory window. + final currentOffset = + (_channelMessageStore.archivedCount(channelIndex) ?? + allMessages.length - currentMessages.length) + .clamp(0, allMessages.length); + if (currentOffset == 0) { return []; // No more messages to load } - final currentOffset = allMessages.length - currentMessages.length; final fetchCount = count.clamp(0, currentOffset); final startIndex = currentOffset - fetchCount; final olderMessages = allMessages.sublist(startIndex, currentOffset); + _channelMessageStore.unarchiveNewest(channelIndex, olderMessages.length); // Prepend to current conversation _channelMessages[channelIndex] = [...olderMessages, ...currentMessages]; @@ -2010,6 +2154,7 @@ class MeshCoreConnector extends ChangeNotifier { await stopScan(); _cancelReconnectTimer(); + _clearTcpReconnectTarget(); _manualDisconnect = false; _resetConnectionHandshakeState(); _activeTransport = MeshCoreTransportType.usb; @@ -2120,9 +2265,16 @@ class MeshCoreConnector extends ChangeNotifier { await stopScan(); _cancelReconnectTimer(); + if (!_tcpAutoReconnectAttempt) { + // A user-started connect replaces any earlier endpoint; it becomes the + // reconnect target only once its handshake completes. + _clearTcpReconnectTarget(); + _reconnectAttempts = 0; + } _manualDisconnect = false; _resetConnectionHandshakeState(); _activeTransport = MeshCoreTransportType.tcp; + final tcpGeneration = ++_tcpConnectGeneration; _setState(MeshCoreConnectionState.connecting); try { @@ -2201,6 +2353,17 @@ class MeshCoreConnector extends ChangeNotifier { if (!gotSelfInfo) { throw StateError('Timed out waiting for SELF_INFO during TCP connect'); } + if (tcpGeneration != _tcpConnectGeneration) { + _appDebugLogService?.info( + 'connectTcp superseded after SELF_INFO; leaving the connector to ' + 'the newer attempt', + tag: 'TCP', + ); + return; + } + _tcpReconnectHost = host; + _tcpReconnectPort = port; + _reconnectAttempts = 0; await syncTime(); } catch (error) { @@ -2250,6 +2413,7 @@ class MeshCoreConnector extends ChangeNotifier { await stopScan(); _cancelReconnectTimer(); + _clearTcpReconnectTarget(); _manualDisconnect = false; _resetConnectionHandshakeState(); _activeTransport = MeshCoreTransportType.review; @@ -2397,6 +2561,7 @@ class MeshCoreConnector extends ChangeNotifier { await stopScan(); _setState(MeshCoreConnectionState.connecting); + final connectGeneration = ++_bleConnectGeneration; _device = device; _deviceId = device.remoteId.toString(); if (displayName != null && displayName.trim().isNotEmpty) { @@ -2409,6 +2574,7 @@ class MeshCoreConnector extends ChangeNotifier { _lastDeviceDisplayName = _deviceDisplayName; _manualDisconnect = false; _cancelReconnectTimer(); + _clearTcpReconnectTarget(); _bleInitialSyncStarted = false; if (PlatformInfo.isWeb) { _resetConnectionHandshakeState(); @@ -2757,6 +2923,18 @@ class MeshCoreConnector extends ChangeNotifier { _reconnectAttempts = 0; } } catch (e) { + // The user cancelled (manual disconnect) or a newer connect took over + // while this attempt was in flight. Tearing down again here would + // re-arm auto-reconnect or kill the newer connection. + if (connectGeneration != _bleConnectGeneration || + _manualDisconnect || + !identical(_device, device)) { + _appDebugLogService?.info( + 'Ignoring error from cancelled connect attempt: $e', + tag: 'BLE Connect', + ); + rethrow; + } _appDebugLogService?.error('Connection error: $e', tag: 'BLE Connect'); final errorText = e.toString(); final lowerErrorText = errorText.toLowerCase(); @@ -3068,6 +3246,7 @@ class MeshCoreConnector extends ChangeNotifier { _queuedMessageSyncInFlight = false; _deferredQueuedContactMessageFrames.clear(); _pendingQueueSync = false; + _resetSyncFlowState(); _queueSyncTimeout?.cancel(); _queueSyncTimeout = null; _queueSyncRetries = 0; @@ -3079,6 +3258,7 @@ class MeshCoreConnector extends ChangeNotifier { _nextChannelIndexToRequest = 0; _totalChannelsToRequest = 0; _previousChannelsCache.clear(); + _unconfirmedChannelSlots.clear(); } bool get _shouldAutoReconnect => @@ -3087,6 +3267,20 @@ class MeshCoreConnector extends ChangeNotifier { _lastDeviceId != null && _activeTransport == MeshCoreTransportType.bluetooth; + /// TCP links drop when Android parks Wi-Fi in the background (#571), so a + /// non-manual TCP disconnect reconnects to the last handshaked endpoint. + /// USB is left alone: losing it almost always means the cable was pulled. + bool get _shouldAutoReconnectTcp => + !_disposed && + !_manualDisconnect && + _tcpReconnectHost != null && + _tcpReconnectPort != null; + + void _clearTcpReconnectTarget() { + _tcpReconnectHost = null; + _tcpReconnectPort = null; + } + bool get _shouldGateInitialChannelSync => _activeTransport == MeshCoreTransportType.usb || _activeTransport == MeshCoreTransportType.tcp || @@ -3133,6 +3327,37 @@ class MeshCoreConnector extends ChangeNotifier { }); } + void _scheduleTcpReconnect() { + if (!_shouldAutoReconnectTcp) return; + if (_reconnectTimer?.isActive == true) return; + + final delayMs = _nextReconnectDelayMs(); + _appDebugLogService?.info( + 'TCP reconnect to $_tcpReconnectHost:$_tcpReconnectPort in ${delayMs}ms ' + '(attempt $_reconnectAttempts)', + tag: 'TCP', + ); + _reconnectTimer = Timer(Duration(milliseconds: delayMs), () async { + if (!_shouldAutoReconnectTcp) return; + if (_state == MeshCoreConnectionState.connecting || + _state == MeshCoreConnectionState.connected) { + return; + } + final host = _tcpReconnectHost!; + final port = _tcpReconnectPort!; + _tcpAutoReconnectAttempt = true; + try { + await connectTcp(host: host, port: port); + } catch (_) { + // A failed attempt tears down through disconnect(manual: false), + // which already rescheduled; this only covers early failures. + _scheduleTcpReconnect(); + } finally { + _tcpAutoReconnectAttempt = false; + } + }); + } + Future disconnect({ bool manual = true, bool skipBleDeviceDisconnect = false, @@ -3145,6 +3370,7 @@ class MeshCoreConnector extends ChangeNotifier { // schedules an auto-reconnect the user did not ask for. _manualDisconnect = true; _cancelReconnectTimer(); + _clearTcpReconnectTarget(); unawaited(_backgroundService?.stop()); } return inFlight; @@ -3198,11 +3424,17 @@ class MeshCoreConnector extends ChangeNotifier { _manualDisconnect = true; _cancelReconnectTimer(); _reconnectAttempts = 0; + _clearTcpReconnectTarget(); unawaited(_backgroundService?.stop()); } else { _manualDisconnect = false; - // Only BLE reconnects on its own; nothing keeps TCP/USB alive. - if (transportAtDisconnect != MeshCoreTransportType.bluetooth) { + // BLE and handshaked TCP links reconnect on their own and keep the + // foreground service; nothing keeps USB alive. + final tcpWillReconnect = + transportAtDisconnect == MeshCoreTransportType.tcp && + _shouldAutoReconnectTcp; + if (transportAtDisconnect != MeshCoreTransportType.bluetooth && + !tcpWillReconnect) { unawaited(_backgroundService?.stop()); } } @@ -3217,6 +3449,7 @@ class MeshCoreConnector extends ChangeNotifier { await _tcpConnector.disconnect(); final wasReviewMode = transportAtDisconnect == MeshCoreTransportType.review; _reviewConnectGeneration++; + _tcpConnectGeneration++; await _reviewFrameSubscription?.cancel(); _reviewFrameSubscription = null; _reviewRadio?.dispose(); @@ -3271,9 +3504,8 @@ class MeshCoreConnector extends ChangeNotifier { _deviceId = null; _contacts.clear(); _discoveredContacts.clear(); - _conversations.clear(); - _loadedConversationKeys.clear(); - _selfPublicKey = null; + _clearSessionMessageState(); + _resetLinkSessionState(); _selfName = null; _selfLatitude = null; _selfLongitude = null; @@ -3285,17 +3517,7 @@ class MeshCoreConnector extends ChangeNotifier { _allowedRepeatFreqRanges = null; _autoAddMaxHops = null; _contactsStorageFull = false; - _batteryMillivolts = null; _repeaterBatterySnapshots.clear(); - _batteryRequested = false; - _awaitingSelfInfo = false; - _hasReceivedDeviceInfo = false; - _maxContacts = _defaultMaxContacts; - _maxChannels = _defaultMaxChannels; - _resetSyncProgressState(); - _pendingChannelSentQueue.clear(); - _pendingGenericAckQueue.clear(); - _reactionSendQueueSequence = 0; _activeTransport = MeshCoreTransportType.bluetooth; if (wasReviewMode) { @@ -3313,6 +3535,9 @@ class MeshCoreConnector extends ChangeNotifier { if (!manual && transportAtDisconnect == MeshCoreTransportType.bluetooth) { _scheduleReconnect(); } + if (!manual && transportAtDisconnect == MeshCoreTransportType.tcp) { + _scheduleTcpReconnect(); + } } Future sendFrame( @@ -3375,7 +3600,9 @@ class MeshCoreConnector extends ChangeNotifier { try { await pendingAck!.completer!.future.timeout(const Duration(seconds: 5)); } on TimeoutException { - _pendingGenericAckQueue.remove(pendingAck!); + // Keep a tombstone: the firmware still answers this command later, + // and that reply must not be credited to the next pending command. + _pendingGenericAckQueue.markTimedOut(pendingAck!); throw TimeoutException( 'Timed out waiting for firmware acknowledgement', ); @@ -3422,7 +3649,10 @@ class MeshCoreConnector extends ChangeNotifier { _gpsLocationPollTimer = null; return; } - unawaited(sendFrame(buildAppStartFrame()).catchError(_logTimerSendError)); + // Skipped mid contact sync: APP_START would abort the firmware's + // contact iteration. The next tick polls again. + if (_isLoadingContacts) return; + unawaited(_sendAppStart().catchError(_logTimerSendError)); }); } @@ -3560,7 +3790,7 @@ class MeshCoreConnector extends ChangeNotifier { _webInitialHandshakeRequestSent = true; } await sendFrame(buildDeviceQueryFrame()); - await sendFrame(buildAppStartFrame()); + await _sendAppStart(); await requestBatteryStatus(force: true); await sendFrame(buildGetCustomVarsFrame()); _deviceConfigRequested = false; @@ -3584,7 +3814,7 @@ class MeshCoreConnector extends ChangeNotifier { _webInitialHandshakeRequestSent = true; } await sendFrame(buildDeviceQueryFrame()); - await sendFrame(buildAppStartFrame()); + await _sendAppStart(); await sendFrame(buildGetCustomVarsFrame()); await requestBatteryStatus(); _deviceConfigRequested = false; @@ -3614,6 +3844,23 @@ class MeshCoreConnector extends ChangeNotifier { ).catchError(_logTimerSendError), ); } + if (supportsDefaultFloodScope) { + unawaited( + sendFrame( + buildGetDefaultFloodScopeFrame(), + ).catchError(_logTimerSendError), + ); + } + } + + /// Sends CMD_APP_START unless a contact sync is running, in which case it is + /// sent when the sync ends (see [_continueAfterContactSync]). + Future _sendAppStart() async { + if (_isLoadingContacts) { + _appStartDeferredForContactSync = true; + return; + } + await sendFrame(buildAppStartFrame()); } void _scheduleSelfInfoRetry() { @@ -3633,9 +3880,7 @@ class MeshCoreConnector extends ChangeNotifier { return; } attempts += 1; - unawaited( - sendFrame(buildAppStartFrame()).catchError(_logTimerSendError), - ); + unawaited(_sendAppStart().catchError(_logTimerSendError)); if (attempts >= maxAttempts) { timer.cancel(); } @@ -3653,7 +3898,7 @@ class MeshCoreConnector extends ChangeNotifier { timer.cancel(); return; } - unawaited(sendFrame(buildAppStartFrame()).catchError(_logTimerSendError)); + unawaited(_sendAppStart().catchError(_logTimerSendError)); }); } @@ -3693,6 +3938,7 @@ class MeshCoreConnector extends ChangeNotifier { _hasLoadedContacts = false; _contacts.clear(); } + _armContactSyncWatchdog(); notifyListeners(); try { @@ -3700,10 +3946,107 @@ class MeshCoreConnector extends ChangeNotifier { } catch (_) { _isLoadingContacts = false; notifyListeners(); + _continueAfterContactSync(); rethrow; } } + void _armContactSyncWatchdog() { + _contactSyncWatchdog?.cancel(); + _contactSyncWatchdog = Timer( + _contactSyncStallTimeout, + _handleContactSyncStalled, + ); + } + + void _handleContactSyncStalled() { + _contactSyncWatchdog = null; + if (!_isLoadingContacts || !isConnected) return; + _appDebugLogService?.warn( + 'Contact sync stalled (no END_OF_CONTACTS); ending it after ' + '$_contactSyncReceived contacts', + tag: 'Connector', + ); + // The partial list is not persisted so the stored contacts survive. + _isLoadingContacts = false; + _preserveContactsOnRefresh = false; + _contactSyncUsesSinceFilter = false; + notifyListeners(); + _continueAfterContactSync(); + } + + /// Runs the work that waits for a contact sync to end, whether it finished + /// with END_OF_CONTACTS, stalled, or failed to start. + void _continueAfterContactSync() { + _contactSyncWatchdog?.cancel(); + _contactSyncWatchdog = null; + if (!isConnected) return; + if (PlatformInfo.isWeb && + _activeTransport == MeshCoreTransportType.bluetooth && + _isSyncingChannels && + !_channelSyncInFlight) { + unawaited(_requestNextChannel()); + } + if (_appStartDeferredForContactSync) { + _appStartDeferredForContactSync = false; + unawaited(_sendAppStart().catchError(_logTimerSendError)); + } + unawaited(_replayUnknownSenderMessages()); + if (_deferQueuedContactMessagesUntilContacts) { + unawaited(_processDeferredQueuedContactMessages()); + } else if (_pendingQueueSync) { + _pendingQueueSync = false; + unawaited(syncQueuedMessages(force: true)); + } + } + + /// Clears the contact-sync follow-up state. Call on connection teardown. + void _resetSyncFlowState() { + _contactSyncWatchdog?.cancel(); + _contactSyncWatchdog = null; + _appStartDeferredForContactSync = false; + _unknownSenderMessageFrames.clear(); + } + + void _holdUnknownSenderMessage(Uint8List frame) { + if (_unknownSenderMessageFrames.length >= _maxUnknownSenderMessageFrames) { + _unknownSenderMessageFrames.removeAt(0); + appLogger.warn( + 'Too many messages from unknown contacts; dropping the oldest', + tag: 'Connector', + ); + } + _unknownSenderMessageFrames.add(Uint8List.fromList(frame)); + } + + /// Replays held unknown-sender messages exactly once. With + /// [onlyResolvable], frames whose sender is still unknown stay held (a + /// single CONTACT frame arrived); otherwise unresolved frames are dropped. + Future _replayUnknownSenderMessages({ + bool onlyResolvable = false, + }) async { + if (_unknownSenderMessageFrames.isEmpty) return; + final ready = []; + _unknownSenderMessageFrames.removeWhere((frame) { + if (onlyResolvable) { + final prefix = _extractSenderPrefix(frame); + if (prefix == null || + !_contacts.any((c) => _matchesPrefix(c.publicKey, prefix))) { + return false; + } + } + ready.add(frame); + return true; + }); + for (final frame in ready) { + await _handleIncomingMessage( + frame, + alreadyAdvancedQueueSync: true, + isUnknownSenderReplay: true, + ); + } + } + Future refreshContacts() async { await getContacts(preserveExisting: true); } @@ -4147,7 +4490,15 @@ class MeshCoreConnector extends ChangeNotifier { ChannelMessage? replyTo, }) async { if (!isConnected || text.isEmpty) return; - final sendRegion = region ?? getEffectiveChannelRegion(channel.index); + final sendScope = region ?? _channelSendScope(channel.index); + // Label only: with no app scope the radio applies its own default + // (and "no region" cannot force unscoped below ver 12). + final radioDefaultApplies = + sendScope.isEmpty || + (sendScope == kUnscopedRegion && !supportsExplicitUnscopedFlood); + final sendRegion = radioDefaultApplies + ? (_defaultFloodScopeName ?? '') + : (sendScope == kUnscopedRegion ? '' : sendScope); // Check if this is a reaction - if so, process it immediately instead of adding as a message final reactionInfo = ReactionHelper.parseReaction(text); @@ -4183,13 +4534,19 @@ class MeshCoreConnector extends ChangeNotifier { // Send the reaction to the device (don't add as a visible message) final reactionQueueId = _nextReactionSendQueueId(); _pendingChannelSentQueue.add(reactionQueueId); - await _runScopedChannelSend(() async { - await _waitForRadioQuiet(lastInboundRxTime: _lastChannelMsgRxTime); - await _sendFrameAndWaitForCommandAck( - buildSendChannelTextMsgFrame(channel.index, text), - channelSendQueueId: reactionQueueId, - ); - }, region: getEffectiveChannelRegion(channel.index)); + try { + await _runScopedChannelSend(() async { + await _waitForRadioQuiet(lastInboundRxTime: _lastChannelMsgRxTime); + await _sendFrameAndWaitForCommandAck( + buildSendChannelTextMsgFrame(channel.index, text), + channelSendQueueId: reactionQueueId, + ); + }, region: _channelSendScope(channel.index)); + } catch (_) { + // e.g. SET_FLOOD_SCOPE rejected or timed out: nothing was sent. + _pendingChannelSentQueue.remove(reactionQueueId); + rethrow; + } return; } // It looks like a reaction, but we did not find its target, so @@ -4224,8 +4581,17 @@ class MeshCoreConnector extends ChangeNotifier { ), channelSendQueueId: isResend ? null : message.messageId, ); - }, region: sendRegion); - await transmit(); + }, region: sendScope); + try { + await transmit(); + } catch (_) { + // A failed or timed-out SET_FLOOD_SCOPE (or text send) means no OK/ERR + // will resolve this message, so fail it here instead of leaving it + // pending forever. + _pendingChannelSentQueue.remove(message.messageId); + _markPendingChannelMessageFailedById(message.messageId); + rethrow; + } final settings = _appSettingsService?.settings; if (settings != null && settings.channelMinHopsEnabled) { @@ -4262,9 +4628,20 @@ class MeshCoreConnector extends ChangeNotifier { if (message == null) return; if (message.status == ChannelMessageStatus.failed) return; hops = message.pathLength ?? 0; - if (hops >= minHops) { + // Read live so toggling the setting mid-resend takes effect. + final countRepeaters = + _appSettingsService?.settings.channelMinHopsCountRepeaters ?? false; + if (channelResendSatisfied( + message, + minHops: minHops, + countDistinctRepeaters: countRepeaters, + selfPublicKey: _selfPublicKey, + )) { _appDebugLogService?.info( - 'Channel message reached $hops hops after $attempt resends', + hops >= minHops + ? 'Channel message reached $hops hops after $attempt resends' + : 'Channel message heard via ${distinctFirstHopRepeaters(message, selfPublicKey: _selfPublicKey).length} ' + 'first-hop repeaters after $attempt resends', tag: 'Channel resend', ); return; @@ -4388,7 +4765,7 @@ class MeshCoreConnector extends ChangeNotifier { ); onProgress?.call(i + 1, blobs.length); } - }, region: getEffectiveChannelRegion(channelIndex)); + }, region: _channelSendScope(channelIndex)); return sentAll; } @@ -4407,11 +4784,19 @@ class MeshCoreConnector extends ChangeNotifier { // firmware that predates CMD_SET_FLOOD_SCOPE. The lock is still held so an // unscoped send can't interleave with (and inherit the scope of) a // concurrent scoped send. - if (region.isEmpty) { + // "No region" can only override the radio's default scope on ver 12+; + // older firmware sends it like any unscoped channel. + final forceUnscoped = + region == kUnscopedRegion && supportsExplicitUnscopedFlood; + if (region.isEmpty || (region == kUnscopedRegion && !forceUnscoped)) { await action(); return; } - await _sendFrameAndWaitForCommandAck(buildSetFloodScopeFrame(region)); + await _sendFrameAndWaitForCommandAck( + forceUnscoped + ? buildSetFloodScopeUnscopedFrame() + : buildSetFloodScopeFrame(region), + ); try { await action(); } finally { @@ -4463,6 +4848,7 @@ class MeshCoreConnector extends ChangeNotifier { unawaited(_persistContacts()); _conversations.remove(contact.publicKeyHex); _loadedConversationKeys.remove(contact.publicKeyHex); + _messageStore.forgetArchive(contact.publicKeyHex); final removedCount = _contactUnreadCount[contact.publicKeyHex] ?? 0; _cachedContactsUnreadTotal = (_cachedContactsUnreadTotal - removedCount) .clamp(0, _cachedContactsUnreadTotal); @@ -4496,6 +4882,77 @@ class MeshCoreConnector extends ChangeNotifier { notifyListeners(); } + /// Broadcasts a zero-hop DISCOVER_REQ and emits each distinct node that + /// answers with our tag until [timeout]. Repeaters reply after a random + /// delay, and firmware rate-limits replies, so the window stays generous. + Stream discoverNearbyNodes({ + int typeMask = nodeDiscoverScanTypeMask, + Duration timeout = const Duration(seconds: 12), + }) { + late final StreamController controller; + StreamSubscription? subscription; + Timer? timer; + final seenKeys = {}; + final tag = math.Random.secure().nextInt(0xFFFFFFFF) + 1; + final selfKeyHex = selfPublicKeyHex; + + Future close() async { + timer?.cancel(); + await subscription?.cancel(); + if (!controller.isClosed) await controller.close(); + } + + controller = StreamController( + onListen: () async { + if (!isConnected || !supportsNodeDiscover) { + await close(); + return; + } + subscription = receivedFrames.listen((frame) { + final response = parseNodeDiscoverResponse(frame); + if (response == null || response.tag != tag) return; + final keyHex = response.publicKeyHex; + if (selfKeyHex.isNotEmpty && selfKeyHex.startsWith(keyHex)) return; + if (!seenKeys.add(keyHex)) return; + if (!controller.isClosed) controller.add(response); + }); + try { + await sendFrame( + buildSendControlDataFrame( + buildDiscoveryRequestPayload(tag, typeMask: typeMask), + ), + ); + } catch (e) { + if (!controller.isClosed) controller.addError(e); + await close(); + return; + } + timer = Timer(timeout, () => unawaited(close())); + }, + onCancel: close, + ); + return controller.stream; + } + + /// The best-known contact for a scan response: an existing contact or a + /// discovered advert with the same key, else a placeholder built from the + /// response (direct neighbour; firmware fills the name on its next advert). + Contact contactForDiscoverResponse(NodeDiscoverResponse response) { + final keyHex = response.publicKeyHex; + for (final contact in [..._contacts, ..._discoveredContacts]) { + if (contact.publicKeyHex.startsWith(keyHex)) return contact; + } + return Contact( + publicKey: response.publicKey, + name: keyHex.substring(0, math.min(8, keyHex.length)).toUpperCase(), + type: response.nodeType, + pathLength: 0, + path: Uint8List(0), + lastSeen: DateTime.now(), + isActive: false, + ); + } + Future importDiscoveredContact(Contact contact) async { if (!isConnected) return false; @@ -4684,14 +5141,6 @@ class MeshCoreConnector extends ChangeNotifier { } } - Future sendCliCommand(String command) async { - if (!isConnected) return; - final selfKey = _selfPublicKey; - if (selfKey == null) return; - _lastSentWasCliCommand = true; - await sendFrame(buildSendCliCommandFrame(selfKey, command)); - } - Future setNodeName(String name) async { if (!isConnected) return; await sendFrame(buildSetAdvertNameFrame(name)); @@ -4737,10 +5186,6 @@ class MeshCoreConnector extends ChangeNotifier { await sendFrame(buildRebootFrame()); } - Future setPrivacyMode(bool enabled) async { - await sendCliCommand('set privacy ${enabled ? 'on' : 'off'}'); - } - Future setTelemetryModeBase( int base, int location, @@ -4786,6 +5231,7 @@ class MeshCoreConnector extends ChangeNotifier { _hasLoadedChannels = false; _previousChannelsCache = List.from(_channels); _channels.clear(); + _unconfirmedChannelSlots.clear(); _nextChannelIndexToRequest = 0; _totalChannelsToRequest = maxChannels ?? _maxChannels; _channelSyncRetries = 0; @@ -4866,7 +5312,8 @@ class MeshCoreConnector extends ChangeNotifier { ); } } catch (e) { - // No cached channel found, that's okay + // No cached copy: the slot's contents are unknown, not empty. + _unconfirmedChannelSlots.add(channelIndex); } // Move to next channel @@ -4878,6 +5325,20 @@ class MeshCoreConnector extends ChangeNotifier { } } + /// The firmware answers GET_CHANNEL with ERR_NOT_FOUND only for an index + /// past its MAX_GROUP_CHANNELS, so every later index is out of range too: + /// finish the sync now instead of retrying each one until it times out. + void _handleChannelSyncNotFound() { + debugPrint( + '[ChannelSync] Channel $_nextChannelIndexToRequest not found; ' + 'device has $_nextChannelIndexToRequest channel slots', + ); + _channelSyncTimeout?.cancel(); + _channelSyncInFlight = false; + _totalChannelsToRequest = _nextChannelIndexToRequest; + _completeChannelSync(); + } + void _completeChannelSync() { _channelSyncTimeout?.cancel(); @@ -4953,6 +5414,7 @@ class MeshCoreConnector extends ChangeNotifier { await _channelMessageStore.clearChannelMessages(index); // Clear in-memory messages for this channel _channelMessages.remove(index); + _processedChannelReactions.remove(index); // Refresh channels after deleting await getChannels(force: true); } @@ -4988,6 +5450,7 @@ class MeshCoreConnector extends ChangeNotifier { } _isLoadingContacts = true; _contactSyncReceived = 0; + _armContactSyncWatchdog(); // Firmware v3+ includes total contacts after CONTACTS_START. // Incremental sync reports total contacts, not filtered result count. if (frame.length >= 5 && !_contactSyncUsesSinceFilter) { @@ -5020,6 +5483,11 @@ class MeshCoreConnector extends ChangeNotifier { case respCodeContact: debugPrint('Got CONTACT'); _handleContact(frame, countTowardSync: !completedPendingReply); + if (_isLoadingContacts && !completedPendingReply) { + _armContactSyncWatchdog(); + } else if (!_isLoadingContacts) { + unawaited(_replayUnknownSenderMessages(onlyResolvable: true)); + } break; case pushCodeContactDeleted: _handleContactDeleted(frame); @@ -5039,6 +5507,12 @@ class MeshCoreConnector extends ChangeNotifier { ); notifyListeners(); break; + case respCodeDefaultFloodScope: + final scope = parseDefaultFloodScopeFrame(frame); + _defaultFloodScopeName = scope?.name; + _defaultFloodScopeKey = scope?.key; + notifyListeners(); + break; case respCodeEndOfContacts: debugPrint('Got END_OF_CONTACTS'); _isLoadingContacts = false; @@ -5051,18 +5525,7 @@ class MeshCoreConnector extends ChangeNotifier { unawaited(updateKnownDiscovered()); notifyListeners(); unawaited(_persistContacts()); - if (PlatformInfo.isWeb && - _activeTransport == MeshCoreTransportType.bluetooth && - _isSyncingChannels && - !_channelSyncInFlight) { - unawaited(_requestNextChannel()); - } - if (_deferQueuedContactMessagesUntilContacts) { - unawaited(_processDeferredQueuedContactMessages()); - } else if (_pendingQueueSync) { - _pendingQueueSync = false; - unawaited(syncQueuedMessages(force: true)); - } + _continueAfterContactSync(); break; case respCodeContactMsgRecv: case respCodeContactMsgRecvV3: @@ -5162,6 +5625,14 @@ class MeshCoreConnector extends ChangeNotifier { ); final failedAck = _pendingGenericAckQueue.takeErr(errCode); + if (failedAck != null && + failedAck.commandCode == cmdGetChannel && + errCode == _errCodeNotFound && + _isSyncingChannels && + _channelSyncInFlight) { + _handleChannelSyncNotFound(); + return; + } if (failedAck == null || failedAck.commandCode != cmdSendChannelTxtMsg || failedAck.channelSendQueueId == null) { @@ -5301,6 +5772,28 @@ class MeshCoreConnector extends ChangeNotifier { return; } + // A different radio answered (for example after a dropped BLE link): + // drop the previous radio's history before this radio's stores load. + if (_messageStateOwnerKey != null && + _messageStateOwnerKey != selfPublicKeyHex) { + _clearSessionMessageState(); + } + _messageStateOwnerKey = selfPublicKeyHex; + + if (_persistedStateLoadedKey == selfPublicKeyHex) { + _awaitingSelfInfo = false; + _selfInfoRetryTimer?.cancel(); + _selfInfoRetryTimer = null; + _maybeRequestDeviceConfig(); + notifyListeners(); + _maybeStartInitialChannelSync(); + if (_pendingQueueSync && _hasLoadedContacts && !_isLoadingContacts) { + _pendingQueueSync = false; + unawaited(syncQueuedMessages(force: true)); + } + return; + } + //set all the stores' public key so they can load the correct data _channelMessageStore.setPublicKeyHex = selfPublicKeyHex; _messageStore.setPublicKeyHex = selfPublicKeyHex; @@ -5324,6 +5817,7 @@ class MeshCoreConnector extends ChangeNotifier { loadAllChannelMessages(); loadUnreadState(); _loadDiscoveredContactCache(); + _persistedStateLoadedKey = selfPublicKeyHex; _awaitingSelfInfo = false; _selfInfoRetryTimer?.cancel(); @@ -5333,6 +5827,13 @@ class MeshCoreConnector extends ChangeNotifier { // Start the serialized initial sync pipeline after SELF_INFO. _maybeStartInitialChannelSync(); + + // A queue sync deferred while this SELF_INFO was awaited (e.g. a later + // refreshDeviceInfo) has nothing else to resume it. + if (_pendingQueueSync && _hasLoadedContacts && !_isLoadingContacts) { + _pendingQueueSync = false; + unawaited(syncQueuedMessages(force: true)); + } } void _handleDeviceInfo(Uint8List frame) { @@ -5977,6 +6478,7 @@ class MeshCoreConnector extends ChangeNotifier { Future _handleIncomingMessage( Uint8List frame, { bool alreadyAdvancedQueueSync = false, + bool isUnknownSenderReplay = false, }) async { if (_selfPublicKey == null) return; @@ -5988,24 +6490,26 @@ class MeshCoreConnector extends ChangeNotifier { var message = _parseContactMessage(frame); - // If message parsing failed due to unknown contact, refresh contacts and retry - if (message == null && !_isLoadingContacts) { + // Unknown sender: the firmware has already dropped this message from its + // offline queue, so hold it until the sender's CONTACT frame arrives + // rather than re-parsing before the refresh has delivered anything. + if (message == null && !isUnknownSenderReplay) { final senderPrefix = _extractSenderPrefix(frame); - if (senderPrefix != null) { - final hasContact = _contacts.any( - (c) => _matchesPrefix(c.publicKey, senderPrefix), - ); - if (!hasContact) { - debugPrint( - 'Received message from unknown contact, refreshing contacts...', - ); - await refreshContactsSinceLastmod(); - // Retry parsing after refresh - message = _parseContactMessage(frame); - if (message != null) { - debugPrint('Successfully parsed message after contact refresh'); + if (senderPrefix != null && + !_contacts.any((c) => _matchesPrefix(c.publicKey, senderPrefix))) { + debugPrint('Message from unknown contact; holding until contacts load'); + _holdUnknownSenderMessage(frame); + if (!_isLoadingContacts) { + try { + await refreshContactsSinceLastmod(); + } catch (e) { + appLogger.warn( + 'Contact refresh for unknown sender failed: $e', + tag: 'Connector', + ); } } + return; } } @@ -6137,10 +6641,10 @@ class MeshCoreConnector extends ChangeNotifier { timestampRaw * 1000, ); - final flags = txtType; - final shiftedType = flags >> 2; - final rawType = flags; - final isSigned = shiftedType == txtTypeSigned || rawType == txtTypeSigned; + // Companion frames carry the raw TXT_TYPE (0..3); legacy firmware sent + // it shifted into the upper six bits like the over-the-air flags byte. + final type = txtType <= _txtTypeCliCommand ? txtType : txtType >> 2; + final isSigned = type == txtTypeSigned; final Uint8List? roomAuthorPrefix; if (isSigned) { // Room-server pushed posts use signed/plain contact messages where this @@ -6153,13 +6657,12 @@ class MeshCoreConnector extends ChangeNotifier { final msgText = reader.readCString(); - final isPlain = - shiftedType == txtTypePlain || rawType == txtTypePlain || isSigned; - final isCli = shiftedType == txtTypeCliData || rawType == txtTypeCliData; + final isPlain = type == txtTypePlain || isSigned; + // TXT_TYPE_CLI_COMMAND is a remote CLI command the firmware did not run + // (sender lacks remote-CLI permission); show it as CLI, never as chat. + final isCli = type == txtTypeCliData || type == _txtTypeCliCommand; if (!isPlain && !isCli) { - appLogger.warn( - 'Unknown message type received: txtType=$txtType, shifted=$shiftedType, raw=$rawType', - ); + appLogger.warn('Unknown message type received: txtType=$txtType'); return null; } @@ -6186,6 +6689,7 @@ class MeshCoreConnector extends ChangeNotifier { senderKey: contact.publicKey, text: decodedText, timestamp: timestamp, + receivedAt: DateTime.now(), isOutgoing: false, isCli: isCli, status: MessageStatus.delivered, @@ -6514,6 +7018,7 @@ class MeshCoreConnector extends ChangeNotifier { senderName: parsed.senderName, text: decodedText, timestamp: DateTime.fromMillisecondsSinceEpoch(timestampRaw * 1000), + receivedAt: DateTime.now(), isOutgoing: false, status: ChannelMessageStatus.sent, pathLength: packet.isFlood ? packet.hopCount : 0, @@ -6570,14 +7075,6 @@ class MeshCoreConnector extends ChangeNotifier { final ackHash = reader.readUInt32LE(); final timeoutMs = reader.readUInt32LE(); - // Check if this is a CLI command ACK - if so, ignore it - if (_lastSentWasCliCommand) { - final ackHashHex = ackHashToHex(ackHash); - debugPrint('Ignoring CLI command ACK (sent): $ackHashHex'); - _lastSentWasCliCommand = false; - return; - } - if (_handleRepeaterCommandSent(ackHash, timeoutMs)) { return; } @@ -6681,8 +7178,6 @@ class MeshCoreConnector extends ChangeNotifier { final ackHash = reader.readUInt32LE(); final tripTimeMs = reader.readUInt32LE(); - // CLI command ACKs are already filtered in _handleMessageSent, so this should only see real messages - if (_handleRepeaterCommandAck(ackHash, tripTimeMs)) { return; } @@ -6738,6 +7233,7 @@ class MeshCoreConnector extends ChangeNotifier { void _handleChannelInfo(Uint8List frame) { final channel = Channel.fromFrame(frame); if (channel == null) return; + _unconfirmedChannelSlots.remove(channel.index); debugPrint( '[ChannelSync] Received channel ${channel.index}: ${channel.isEmpty ? "empty" : channel.name}', @@ -6992,7 +7488,9 @@ class MeshCoreConnector extends ChangeNotifier { messages.add(message); if (messages.length > _messageWindowSize) { - messages.removeRange(0, messages.length - _messageWindowSize); + final trimCount = messages.length - _messageWindowSize; + _messageStore.archive(pubKeyHex, messages.sublist(0, trimCount)); + messages.removeRange(0, trimCount); } _messageStore.saveMessages(pubKeyHex, messages); notifyListeners(); @@ -7134,18 +7632,35 @@ class MeshCoreConnector extends ChangeNotifier { // `foo` and `#foo` share a scope key, so dedupe without the prefix or a // region listed both ways would always look ambiguous. - final candidates = { + final names = { for (final region in [ ...RegionStore().loadRegions(), ..._channelRegions.values, _defaultRegion, ]) region.startsWith('#') ? region.substring(1) : region, - }.where((region) => region.trim().isNotEmpty).toList()..sort(); + }.where((region) => region.trim().isNotEmpty && region != kUnscopedRegion); + final keys = { + for (final name in names) name: floodScopeKeyForRegion(name), + }; + // The radio's default scope key may not be derived from its name (another + // client may have set it), so match on the stored key itself. + final radioDefaultName = _defaultFloodScopeName; + final radioDefaultKey = _defaultFloodScopeKey; + if (radioDefaultName != null && radioDefaultKey != null) { + final name = radioDefaultName.startsWith('#') + ? radioDefaultName.substring(1) + : radioDefaultName; + final derived = keys[name]; + if (derived == null || !_pathsEqual(derived, radioDefaultKey)) { + keys[name] = radioDefaultKey; + } + } + final candidates = keys.keys.toList()..sort(); String? match; for (final region in candidates) { final code = floodTransportCode( - scopeKey: floodScopeKeyForRegion(region), + scopeKey: keys[region]!, payloadType: packet.payloadType, payload: packet.payload, ); @@ -7296,6 +7811,7 @@ class MeshCoreConnector extends ChangeNotifier { translationStatus: message.translationStatus, translationModelId: message.translationModelId, timestamp: message.timestamp, + receivedAt: message.receivedAt, isOutgoing: message.isOutgoing, status: message.status, repeats: message.repeats, @@ -7530,6 +8046,31 @@ class MeshCoreConnector extends ChangeNotifier { } void _handleDisconnection() { + _resetLinkSessionState(); + + _notifySubscription?.cancel(); + _notifySubscription = null; + _connectionSubscription?.cancel(); + _connectionSubscription = null; + + _device = null; + _rxCharacteristic = null; + _txCharacteristic = null; + // Preserve deviceId and displayName for UI display during reconnection + // They're only cleared on manual disconnect via disconnect() method. + // Conversations are kept for an auto-reconnect to the same radio; + // _handleSelfInfo drops them if a different radio answers. + + _setState(MeshCoreConnectionState.disconnected); + _scheduleReconnect(); + } + + /// Per-link state shared by a full disconnect and a dropped BLE link: + /// polling timers, radio stats, battery and pending protocol replies. + void _resetLinkSessionState() { + _persistedStateLoadedKey = null; + _defaultFloodScopeName = null; + _defaultFloodScopeKey = null; _stopBatteryPolling(); _stopGpsLocationPolling(); _stopRadioStatsPolling(); @@ -7544,21 +8085,14 @@ class MeshCoreConnector extends ChangeNotifier { } _pendingRepeaterAcks.clear(); - _notifySubscription?.cancel(); - _notifySubscription = null; - _connectionSubscription?.cancel(); - _connectionSubscription = null; - - _device = null; - _rxCharacteristic = null; - _txCharacteristic = null; - // Preserve deviceId and displayName for UI display during reconnection - // They're only cleared on manual disconnect via disconnect() method _selfPublicKey = null; _awaitingSelfInfo = false; _selfInfoRetryTimer?.cancel(); _selfInfoRetryTimer = null; _hasReceivedDeviceInfo = false; + _batteryMillivolts = null; + _batteryRequested = false; + _pathHashByteWidth = 1; _maxContacts = _defaultMaxContacts; _maxChannels = _defaultMaxChannels; _resetSyncProgressState(); @@ -7566,9 +8100,22 @@ class MeshCoreConnector extends ChangeNotifier { _pendingGenericAckQueue.clear(); _reactionSendQueueSequence = 0; _retryService?.failAllPending(); + } - _setState(MeshCoreConnectionState.disconnected); - _scheduleReconnect(); + /// Drops the conversation and channel history held in memory, so a + /// different radio never shows or persists another radio's messages. + void _clearSessionMessageState() { + _conversations.clear(); + _loadedConversationKeys.clear(); + _channelMessages.clear(); + _processedChannelReactions.clear(); + _processedContactReactions.clear(); + _channelHopChecks.clear(); + _channelHopShortfalls.clear(); + _messageStore.forgetArchive(); + _channelMessageStore.forgetArchive(); + _messageStateOwnerKey = null; + _persistedStateLoadedKey = null; } String _nextReactionSendQueueId() { @@ -8244,7 +8791,8 @@ class MeshCoreConnector extends ChangeNotifier { final messages = _conversations[contactKeyHex]; if (messages == null) return; messages.clear(); - unawaited(_messageStore.saveMessages(contactKeyHex, messages)); + // Clear storage too, not just the in-memory window. + unawaited(_messageStore.clearMessages(contactKeyHex)); markContactRead(contactKeyHex); notifyListeners(); } @@ -8253,7 +8801,8 @@ class MeshCoreConnector extends ChangeNotifier { final messages = _channelMessages[channelIndex]; if (messages == null) return; messages.clear(); - unawaited(_channelMessageStore.saveChannelMessages(channelIndex, messages)); + // Clear storage too, not just the in-memory window. + unawaited(_channelMessageStore.clearChannelMessages(channelIndex)); markChannelRead(channelIndex); notifyListeners(); } @@ -8305,7 +8854,7 @@ bool _isPathLenValidForMode( Uint8List _reversePathByHop(Uint8List pathBytes, int pathHashWidth) { if (pathBytes.isEmpty) return Uint8List(0); - final width = pathHashWidth.clamp(1, 4).toInt(); + final width = pathHashWidth.clamp(1, maxPathHashWidth).toInt(); final reversed = []; for (var i = pathBytes.length; i > 0; i -= width) { final start = (i - width).clamp(0, pathBytes.length).toInt(); diff --git a/lib/connector/meshcore_protocol.dart b/lib/connector/meshcore_protocol.dart index f740969b..126110ad 100644 --- a/lib/connector/meshcore_protocol.dart +++ b/lib/connector/meshcore_protocol.dart @@ -133,9 +133,8 @@ class BufferWriter { void writeCString(String string, int maxLength) { final bytes = Uint8List(maxLength); final encoded = utf8.encode(string); - for (var i = 0; i < maxLength - 1 && i < encoded.length; i++) { - bytes[i] = encoded[i]; - } + final len = utf8BoundaryLength(encoded, maxLength - 1); + bytes.setRange(0, len, encoded); writeBytes(bytes); } @@ -215,6 +214,8 @@ const int cmdSetAutoAddConfig = 58; const int cmdGetAutoAddConfig = 59; const int cmdGetAllowedRepeatFreq = 60; const int cmdSetPathHashMode = 61; +const int cmdSetDefaultFloodScope = 63; // FIRMWARE_VER_CODE 11+ +const int cmdGetDefaultFloodScope = 64; // FIRMWARE_VER_CODE 11+ // Text message types const int txtTypePlain = 0; @@ -265,6 +266,12 @@ const int respCodeCustomVars = 21; const int respCodeAutoAddConfig = 25; const int respCodeAllowedRepeatFreq = 26; const int respCodeStats = 24; +const int respCodeDefaultFloodScope = 28; + +/// Region name field width in SET/GET_DEFAULT_FLOOD_SCOPE frames (NUL padded, +/// so at most 30 name bytes). +const int defaultFloodScopeNameSize = 31; +const int floodScopeKeySize = 16; const int statsTypeCore = 0; const int statsTypeRadio = 1; @@ -333,13 +340,34 @@ const int autoAddRoomServerFlag = const int autoAddSensorFlag = 1 << 4; // 0x10 - auto-add Sensor (ADV_TYPE_SENSOR) +/// Returns the largest prefix length of [bytes] that is at most [maxBytes] +/// and does not end in the middle of a UTF-8 encoded character. +int utf8BoundaryLength(List bytes, int maxBytes) { + if (bytes.length <= maxBytes) return bytes.length; + if (maxBytes <= 0) return 0; + var len = maxBytes; + // Back up over continuation bytes (10xxxxxx) to the lead byte, then drop + // that lead byte too since its character would be cut short. + while (len > 0 && (bytes[len] & 0xC0) == 0x80) { + len--; + } + return len; +} + // Sizes const int pubKeySize = 32; const int signatureSize = 64; const int maxPathSize = 64; const int pathHashSize = 1; const int maxNameSize = 32; +// Largest frame the app sends. Kept at 172 so commands still fit radios on +// firmware built before MAX_FRAME_SIZE grew to 176. const int maxFrameSize = 172; +// Largest frame current firmware may send (MAX_FRAME_SIZE 176, +4 bytes for +// region transport codes). Receive paths must accept frames up to this size. +const int maxRxFrameSize = 176; +// Widest per-hop path hash firmware accepts on air (mode 2); 4 is reserved. +const int maxPathHashWidth = 3; const int appProtocolVersion = 4; // Matches firmware MAX_TEXT_LEN (10 * CIPHER_BLOCK_SIZE). const int maxTextPayloadBytes = 160; @@ -599,7 +627,8 @@ Uint8List buildGetStatsFrame(int statsType) { return Uint8List.fromList([cmdGetStats, statsType & 0xFF]); } -/// Path hash width on air: [61][0][mode], mode 0..3 → (mode+1) bytes per hop hash. +/// Path hash width on air: [61][0][mode], mode 0..2 → (mode+1) bytes per hop +/// hash. Firmware rejects mode 3 (4-byte hashes are reserved). Uint8List buildSetPathHashModeFrame(int mode) { final m = mode.clamp(0, 2).toInt(); return Uint8List.fromList([cmdSetPathHashMode, 0, m]); @@ -623,9 +652,7 @@ Uint8List buildSendSelfAdvertFrame({bool flood = false}) { // Format: [cmd][name...] Uint8List buildSetAdvertNameFrame(String name) { final nameBytes = utf8.encode(name); - final nameLen = nameBytes.length < maxNameSize - ? nameBytes.length - : maxNameSize - 1; + final nameLen = utf8BoundaryLength(nameBytes, maxNameSize - 1); final writer = BufferWriter(); writer.writeByte(cmdSetAdvertName); writer.writeBytes(Uint8List.fromList(nameBytes.sublist(0, nameLen))); @@ -938,9 +965,72 @@ Uint8List buildDiscoveryRequestPayload( return writer.toBytes(); } +/// Node types a manual nearby-node scan asks for. Firmware repeaters and +/// sensors answer DISCOVER_REQ; room servers and companions currently don't, +/// but the room bit is harmless and lets future firmware reply. +const int nodeDiscoverScanTypeMask = + (1 << advTypeRepeater) | (1 << advTypeRoom) | (1 << advTypeSensor); + +/// One DISCOVER_RESP, as pushed by the companion in PUSH_CODE_CONTROL_DATA: +/// `[0x8E][snr x4 int8][rssi int8][path_len][0x9][snr x4 int8][tag u32 LE][pub_key 8|32]`. +class NodeDiscoverResponse { + NodeDiscoverResponse({ + required this.nodeType, + required this.snr, + required this.rssi, + required this.remoteSnr, + required this.tag, + required this.publicKey, + }); + + final int nodeType; + + /// SNR (dB) of the response as received by our radio. + final double snr; + final int rssi; + + /// SNR (dB) of our request as heard by the responding node. + final double remoteSnr; + final int tag; + + /// Full 32-byte key, or an 8-byte prefix when the request was prefix-only. + final Uint8List publicKey; + + bool get hasFullKey => publicKey.length == pubKeySize; + String get publicKeyHex => pubKeyToHex(publicKey); +} + +/// Parses a PUSH_CODE_CONTROL_DATA frame carrying a DISCOVER_RESP, or returns +/// null for any other or malformed frame. +NodeDiscoverResponse? parseNodeDiscoverResponse(Uint8List frame) { + // Push header (4) + type, snr, tag (6) + at least an 8-byte key prefix. + if (frame.length < 4 + 6 + 8 || frame[0] != pushCodeControlData) { + return null; + } + final reader = BufferReader(frame); + reader.skipBytes(1); + final snr = reader.readInt8() / 4.0; + final rssi = reader.readInt8(); + reader.skipBytes(1); // path_len: responses are sent zero-hop. + final typeByte = reader.readByte(); + if ((typeByte >> 4) != controlSubtypeDiscoverResp) return null; + final remoteSnr = reader.readInt8() / 4.0; + final tag = reader.readUInt32LE(); + final keyBytes = reader.readRemainingBytes(); + final keyLength = keyBytes.length >= pubKeySize ? pubKeySize : 8; + return NodeDiscoverResponse( + nodeType: typeByte & 0x0F, + snr: snr, + rssi: rssi, + remoteSnr: remoteSnr, + tag: tag, + publicKey: Uint8List.fromList(keyBytes.sublist(0, keyLength)), + ); +} + Uint8List _reversePathByHop(Uint8List path, int pathHashWidth) { if (path.isEmpty) return Uint8List(0); - final width = pathHashWidth.clamp(1, 4).toInt(); + final width = pathHashWidth.clamp(1, maxPathHashWidth).toInt(); if (path.length % width != 0) { return Uint8List.fromList(path.reversed.toList()); } @@ -963,7 +1053,7 @@ Uint8List buildSendAnonReqFrame( int replyHopCount = 0, int pathHashWidth = pathHashSize, }) { - final width = pathHashWidth.clamp(1, 4).toInt(); + final width = pathHashWidth.clamp(1, maxPathHashWidth).toInt(); final path = replyPath ?? Uint8List(0); final encodedPathLen = ((width - 1) << 6) | (replyHopCount & 0x3F); final writer = BufferWriter(); @@ -1086,6 +1176,63 @@ Uint8List buildSetFloodScopeFrame(String region) { return Uint8List.fromList([cmdSetFloodScope, 0, ...scope]); } +/// CMD_SET_FLOOD_SCOPE variant 1 (FIRMWARE_VER_CODE 12+): send floods with no +/// transport scope at all, overriding the radio's default flood scope. +Uint8List buildSetFloodScopeUnscopedFrame() { + return Uint8List.fromList([cmdSetFloodScope, 1]); +} + +Uint8List buildGetDefaultFloodScopeFrame() { + return Uint8List.fromList([cmdGetDefaultFloodScope]); +} + +/// CMD_SET_DEFAULT_FLOOD_SCOPE: `[63][name 31][key 16]` stores [region] as the +/// radio's default flood scope; an empty [region] clears it (`[63]`). +/// The name is stored without a leading '#'; the key uses the same derivation +/// as [buildSetFloodScopeFrame]. +Uint8List buildSetDefaultFloodScopeFrame(String region) { + final name = region.startsWith('#') ? region.substring(1) : region; + if (name.isEmpty) return Uint8List.fromList([cmdSetDefaultFloodScope]); + final nameBytes = utf8.encode(name); + if (nameBytes.length >= defaultFloodScopeNameSize) { + throw ArgumentError.value(region, 'region', 'name longer than 30 bytes'); + } + final frame = Uint8List(1 + defaultFloodScopeNameSize + floodScopeKeySize); + frame[0] = cmdSetDefaultFloodScope; + frame.setRange(1, 1 + nameBytes.length, nameBytes); + frame.setRange( + 1 + defaultFloodScopeNameSize, + frame.length, + floodScopeKeyForRegion(name), + ); + return frame; +} + +/// Parses RESP_CODE_DEFAULT_FLOOD_SCOPE. Returns null when the radio has no +/// default scope (`[28]` alone) or the frame is malformed. +({String name, Uint8List key})? parseDefaultFloodScopeFrame(Uint8List frame) { + if (frame.isEmpty || frame[0] != respCodeDefaultFloodScope) return null; + if (frame.length < 1 + defaultFloodScopeNameSize + floodScopeKeySize) { + return null; + } + final nameField = frame.sublist(1, 1 + defaultFloodScopeNameSize); + final end = nameField.indexOf(0); + final name = utf8 + .decode( + end >= 0 ? nameField.sublist(0, end) : nameField, + allowMalformed: true, + ) + .trim(); + if (name.isEmpty) return null; + final keyStart = 1 + defaultFloodScopeNameSize; + return ( + name: name, + key: Uint8List.fromList( + frame.sublist(keyStart, keyStart + floodScopeKeySize), + ), + ); +} + Uint8List floodScopeKeyForRegion(String region) { final name = region.startsWith('#') ? region : '#$region'; final hash = crypto.sha256.convert(utf8.encode(name)).bytes; diff --git a/lib/connector/pending_command_replies.dart b/lib/connector/pending_command_replies.dart index 43968c02..a0dc9b5d 100644 --- a/lib/connector/pending_command_replies.dart +++ b/lib/connector/pending_command_replies.dart @@ -17,7 +17,14 @@ class PendingCommandReply { final Uint8List? matchKey; final String? channelSendQueueId; final Completer? completer; - final DateTime createdAt = DateTime.now(); + final DateTime createdAt; + + /// Set when the sender gave up waiting. The entry then stays queued as a + /// tombstone so the firmware's late reply is absorbed here instead of being + /// credited to the next command. + DateTime? timedOutAt; + + bool get isTombstone => timedOutAt != null; PendingCommandReply({ required this.commandCode, @@ -25,7 +32,8 @@ class PendingCommandReply { this.matchKey, this.channelSendQueueId, this.completer, - }); + DateTime? createdAt, + }) : createdAt = createdAt ?? DateTime.now(); } /// Success reply for each command whose single reply is OK/ERR or a specific @@ -53,6 +61,7 @@ int? replyCodeForCommand(Uint8List data) { case cmdSetAutoAddConfig: case cmdSetPathHashMode: case cmdSendChannelData: + case cmdSetDefaultFloodScope: return respCodeOk; case cmdSendTxtMsg: case cmdSendLogin: @@ -88,6 +97,8 @@ int? replyCodeForCommand(Uint8List data) { return respCodeAutoAddConfig; case cmdGetAllowedRepeatFreq: return respCodeAllowedRepeatFreq; + case cmdGetDefaultFloodScope: + return respCodeDefaultFloodScope; } return null; } @@ -96,11 +107,20 @@ int? replyCodeForCommand(Uint8List data) { /// and answers each with OK, ERR or a specific RESP_CODE, so an ERR belongs /// to the oldest pending command and an OK to the oldest one expecting OK. class PendingCommandReplies { - PendingCommandReplies({required this.staleAfter}); + PendingCommandReplies({ + required this.staleAfter, + this.tombstoneLifetime = const Duration(seconds: 10), + DateTime Function()? clock, + }) : _clock = clock ?? DateTime.now; /// Entries older than this are dropped so a lost reply cannot swallow a /// later, unrelated OK/ERR. Awaited entries time out in sendFrame first. final Duration staleAfter; + + /// How long a timed-out entry waits for its late reply. Kept short so a + /// reply that never comes swallows at most a brief window of replies. + final Duration tombstoneLifetime; + final DateTime Function() _clock; final List _queue = []; int get length => _queue.length; @@ -123,6 +143,7 @@ class PendingCommandReplies { : null, channelSendQueueId: channelSendQueueId, completer: waitForReply ? Completer() : null, + createdAt: _clock(), ); if (pending.completer != null) { // The sender awaits this future after transport I/O; attach an error @@ -135,11 +156,19 @@ class PendingCommandReplies { void remove(PendingCommandReply pending) => _queue.remove(pending); + /// Marks [pending] as abandoned by its sender. It keeps its place in the + /// FIFO so a late reply is consumed by it rather than misattributed. + void markTimedOut(PendingCommandReply pending) { + if (!_queue.contains(pending)) return; + pending.timedOutAt = _clock(); + } + void clear() => _queue.clear(); /// Completes the oldest command whose success reply is [frame]'s code. bool completeReply(Uint8List frame) { if (frame.isEmpty) return false; + _pruneStale(); final code = frame[0]; if (code == respCodeOk || code == respCodeErr) return false; final key = code == respCodeContact && frame.length >= 1 + pubKeySize @@ -156,20 +185,24 @@ class PendingCommandReplies { } /// Attributes RESP_CODE_OK; commands expecting a specific reply are skipped. + /// Returns null when no command matches or a timed-out entry absorbed it. PendingCommandReply? takeOk() { _pruneStale(); final index = _queue.indexWhere((p) => p.replyCode == respCodeOk); if (index < 0) return null; final pending = _queue.removeAt(index); + if (pending.isTombstone) return null; pending.completer?.complete(); return pending; } /// Attributes RESP_CODE_ERR to the oldest pending command. + /// Returns null when nothing is pending or a timed-out entry absorbed it. PendingCommandReply? takeErr(int errCode) { _pruneStale(); 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'), ); @@ -177,7 +210,13 @@ class PendingCommandReplies { } void _pruneStale() { - final cutoff = DateTime.now().subtract(staleAfter); - _queue.removeWhere((p) => p.createdAt.isBefore(cutoff)); + final now = _clock(); + final cutoff = now.subtract(staleAfter); + final tombstoneCutoff = now.subtract(tombstoneLifetime); + _queue.removeWhere( + (p) => + p.createdAt.isBefore(cutoff) || + (p.timedOutAt?.isBefore(tombstoneCutoff) ?? false), + ); } } diff --git a/lib/helpers/channel_resend_coverage.dart b/lib/helpers/channel_resend_coverage.dart new file mode 100644 index 00000000..d2eca9d3 --- /dev/null +++ b/lib/helpers/channel_resend_coverage.dart @@ -0,0 +1,53 @@ +import 'dart:typed_data'; + +import '../models/channel_message.dart'; + +/// Distinct first-hop repeaters heard echoing [message], across every resend +/// (echoes of all attempts merge into the same message's path variants). +/// +/// Each first hop is the leading `pathHashWidth` bytes of an echo's path. +/// Zero-hop echoes and a first hop matching our own key prefix are ignored. +Set distinctFirstHopRepeaters( + ChannelMessage message, { + Uint8List? selfPublicKey, +}) { + final width = (message.pathHashWidth ?? 1).clamp(1, 3).toInt(); + String? selfPrefix; + if (selfPublicKey != null && selfPublicKey.length >= width) { + selfPrefix = _hex(selfPublicKey, width); + } + final hops = {}; + for (final path in [message.pathBytes, ...message.pathVariants]) { + if (path.length < width) continue; + final hop = _hex(path, width); + if (hop != selfPrefix) hops.add(hop); + } + return hops; +} + +/// Whether a channel resend has heard [message] travel far enough: +/// an echo through at least [minHops] repeaters, or, when +/// [countDistinctRepeaters] is on, echoes from at least [minHops] different +/// first-hop repeaters. +bool channelResendSatisfied( + ChannelMessage message, { + required int minHops, + required bool countDistinctRepeaters, + Uint8List? selfPublicKey, +}) { + if ((message.pathLength ?? 0) >= minHops) return true; + if (!countDistinctRepeaters) return false; + return distinctFirstHopRepeaters( + message, + selfPublicKey: selfPublicKey, + ).length >= + minHops; +} + +String _hex(Uint8List bytes, int length) { + final buffer = StringBuffer(); + for (var i = 0; i < length; i++) { + buffer.write(bytes[i].toRadixString(16).padLeft(2, '0')); + } + return buffer.toString(); +} diff --git a/lib/helpers/cyr2lat.dart b/lib/helpers/cyr2lat.dart index a7ec5dde..d5d0c23b 100644 --- a/lib/helpers/cyr2lat.dart +++ b/lib/helpers/cyr2lat.dart @@ -24,7 +24,9 @@ class Cyr2Lat { 'х': 'x', }; - static final RegExp _prefixRegExp = RegExp(r'\@\[[\S\s]+\] '); + // Non-greedy so only the leading "@[name] " mention is treated as the + // sender prefix, not everything up to the last "] " in the message. + static final RegExp _prefixRegExp = RegExp(r'@\[[\S\s]+?\] '); static void setCharMap(Map charMap) { _charMap = Map.from(charMap); diff --git a/lib/helpers/link_handler.dart b/lib/helpers/link_handler.dart index 980369da..41acba45 100644 --- a/lib/helpers/link_handler.dart +++ b/lib/helpers/link_handler.dart @@ -15,6 +15,19 @@ class LinkHandler { return base.copyWith(color: orange, decoration: TextDecoration.underline); } + /// The system text scale as a linear factor for [style]'s size. + /// + /// flutter_linkify only takes a `textScaleFactor` (default 1.0, which + /// ignores the system font size), so the ambient [TextScaler] is sampled + /// at the style's font size to keep Android's nonlinear scaling. The + /// in-app chat text scale is already part of `style.fontSize`. + static double systemTextScaleFactor(BuildContext context, TextStyle style) { + final fontSize = + style.fontSize ?? DefaultTextStyle.of(context).style.fontSize ?? 14; + if (fontSize <= 0) return 1.0; + return MediaQuery.textScalerOf(context).scale(fontSize) / fontSize; + } + /// Returns a [SelectableLinkify] on desktop or a [Linkify] on mobile. static Widget buildLinkifyText({ required BuildContext context, @@ -27,6 +40,7 @@ class LinkHandler { const options = LinkifyOptions(humanize: false, defaultToHttps: false); const linkifiers = [UrlLinkifier(), EmailLinkifier()]; void onOpen(LinkableElement link) => handleLinkTap(context, link.url); + final textScaleFactor = systemTextScaleFactor(context, style); if (PlatformInfo.isDesktop) { final linkify = SelectableLinkify( @@ -36,6 +50,7 @@ class LinkHandler { options: options, linkifiers: linkifiers, onOpen: onOpen, + textScaleFactor: textScaleFactor, ); if (onSecondaryTap == null) return linkify; return Listener( @@ -53,6 +68,7 @@ class LinkHandler { options: options, linkifiers: linkifiers, onOpen: onOpen, + textScaleFactor: textScaleFactor, ); } diff --git a/lib/helpers/reaction_helper.dart b/lib/helpers/reaction_helper.dart index 58928284..982ce48c 100644 --- a/lib/helpers/reaction_helper.dart +++ b/lib/helpers/reaction_helper.dart @@ -120,11 +120,32 @@ class ReactionHelper { final input = senderName != null ? '$timestampSeconds$senderName$first5' : '$timestampSeconds$first5'; - // Use hashCode and take lower 16 bits, format as 4 hex chars - final hash = input.hashCode & 0xFFFF; + // Take the lower 16 bits of the string hash, format as 4 hex chars + final hash = stableStringHash(input) & 0xFFFF; return hash.toRadixString(16).padLeft(4, '0'); } + /// Platform-independent reimplementation of the Dart VM's + /// `String.hashCode` (one-at-a-time hash over UTF-16 code units, finalized + /// to 30 bits). Reaction hashes were originally computed with + /// `String.hashCode`, which differs on the web (dart2js), so this keeps + /// the wire format identical to existing native clients while giving web + /// builds the same result. + static int stableStringHash(String input) { + const mask32 = 0xFFFFFFFF; + var hash = 0; + for (final unit in input.codeUnits) { + hash = (hash + unit) & mask32; + hash = (hash + ((hash << 10) & mask32)) & mask32; + hash ^= hash >> 6; + } + hash = (hash + ((hash << 3) & mask32)) & mask32; + hash ^= hash >> 11; + hash = (hash + ((hash << 15) & mask32)) & mask32; + hash &= 0x3FFFFFFF; + return hash == 0 ? 1 : hash; + } + // Compute the type of reaction hash used by MeshCoreOne. static String computeReactionHashMC1( int timestampSeconds, diff --git a/lib/helpers/smaz.dart b/lib/helpers/smaz.dart index de34ddee..aefae7e1 100644 --- a/lib/helpers/smaz.dart +++ b/lib/helpers/smaz.dart @@ -281,19 +281,41 @@ class Smaz { return text; } + /// Decodes text produced by [encodeIfSmaller]. Returns null when [text] is + /// not a compressed payload. + /// + /// Plain text can also start with `s:` followed by base64-looking + /// characters ("s:test"), so a payload only counts as compressed when it + /// satisfies the encoder's invariants: valid UTF-8, re-compressing the + /// result yields the same bytes, and the encoded form is shorter than the + /// decoded text. Very short ambiguous inputs can still match. static String? tryDecodePrefixed(String text) { final trimmedLeft = text.trimLeft(); if (!trimmedLeft.startsWith('s:') || trimmedLeft.length <= 2) return null; final encoded = trimmedLeft.substring(2); try { final compressed = _decodeBase64Flexible(encoded); + if (compressed.isEmpty) return null; final decompressed = decompressBytes(compressed); - return utf8.decode(decompressed, allowMalformed: true); + final decoded = utf8.decode(decompressed); + final recompressed = compressBytes(decompressed); + if (!_bytesEqual(recompressed, compressed)) return null; + final candidateLength = 2 + base64Encode(compressed).length; + if (candidateLength >= decompressed.length) return null; + return decoded; } catch (_) { return null; } } + static bool _bytesEqual(List a, List b) { + if (a.length != b.length) return false; + for (int i = 0; i < a.length; i++) { + if (a[i] != b[i]) return false; + } + return true; + } + static Uint8List compressBytes(Uint8List input) { final out = BytesBuilder(copy: false); final verbatim = []; diff --git a/lib/helpers/utf8_length_limiter.dart b/lib/helpers/utf8_length_limiter.dart index 235b84b2..e03061ec 100644 --- a/lib/helpers/utf8_length_limiter.dart +++ b/lib/helpers/utf8_length_limiter.dart @@ -72,3 +72,33 @@ class Utf8LengthLimitingTextInputFormatter extends TextInputFormatter { bool _isLowSurrogate(int codeUnit) => codeUnit >= 0xDC00 && codeUnit <= 0xDFFF; } + +/// Longest name (in UTF-8 bytes) the radio stores for channels and the node +/// advert name: a 32-byte field that keeps one byte for the NUL terminator. +const int maxRadioNameBytes = 31; + +int utf8ByteLength(String text) => utf8.encode(text).length; + +/// Cuts [text] to at most [maxBytes] UTF-8 bytes without splitting a +/// character (grapheme cluster). +String truncateUtf8(String text, int maxBytes) { + if (maxBytes <= 0) return ''; + if (utf8ByteLength(text) <= maxBytes) return text; + final kept = StringBuffer(); + var used = 0; + for (final char in text.characters) { + final size = utf8ByteLength(char); + if (used + size > maxBytes) break; + kept.write(char); + used += size; + } + return kept.toString(); +} + +/// Builds `"