update zjs81/dev 724696cd3c and fix image preview position.

This commit is contained in:
ericz committed 2026-08-11 20:30:14 +02:00
1 parent d920449cbb
commit 6786c3348f
157 files changed
+31310 -1338

No files matched your search

+206 -28
View File
@@ -24,6 +24,16 @@ import '../services/ble_debug_log_service.dart';
import '../services/linux_ble_error_classifier.dart';
import '../services/linux_ble_pairing_service_stub.dart'
if (dart.library.io) '../services/linux_ble_pairing_service.dart';
import '../services/image_chunk_transport.dart'
show
ImageChunkOutcome,
ImageChunkTransport,
buildSendChannelDataFrame,
dataTypeAeicImage,
outPathUnknown,
respCodeChannelDataRecv,
senderPrefixFromKey;
import '../services/image_codec_service.dart';
import '../services/message_retry_service.dart';
import '../services/path_history_service.dart';
import '../services/app_settings_service.dart';
@@ -248,6 +258,13 @@ class MeshCoreConnector extends ChangeNotifier {
bool _hasLoadedChannels = false;
TimeoutPredictionService? _timeoutPredictionService;
TranslationService? _translationService;
ImageCodecService? _imageCodecService;
// Holds the ImageReassembler, so partially received images survive across
// frames (chunks of one image arrive in separate GRP_DATA frames, and via the
// firmware offline queue they may be minutes apart).
ImageChunkTransport? _imageTransport;
void Function(ImageChunkOutcome outcome)? _onImageChunk;
void Function(int senderPrefix)? _onImageSenderPrefix;
// Intentionally global (not per-contact): tracks overall network activity.
// Frequent RX from any source indicates a busy network with more collisions.
DateTime _lastRxTime = DateTime.now();
@@ -271,6 +288,8 @@ class MeshCoreConnector extends ChangeNotifier {
bool _batteryRequested = false;
bool _awaitingSelfInfo = false;
bool _hasReceivedDeviceInfo = false;
String? _manufacturerName;
String? _firmwareVersionString;
// Initial sync is serialized for predictable progress. Firmware exposes one
// FIFO queued-message stream, so direct/room frames are buffered until after
// contacts are known.
@@ -356,14 +375,14 @@ class MeshCoreConnector extends ChangeNotifier {
List<Channel> _cachedChannels = [];
final Map<int, bool> _channelSmazEnabled = {};
final Map<int, bool> _channelCyr2LatEnabled = {};
final Map<int, bool> _channelImagesEnabled = {};
final Map<int, bool> _channelUrlImagesEnabled = {};
final Map<int, String?> _channelCyr2LatProfileId = {};
final Map<int, Region> _channelRegions = {};
bool _lastSentWasCliCommand =
false; // Track if last sent message was a CLI command
final Map<String, bool> _contactSmazEnabled = {};
final Map<String, bool> _contactCyr2LatEnabled = {};
final Map<String, bool> _contactImagesEnabled = {};
final Map<String, bool> _contactUrlImagesEnabled = {};
final Map<String, String?> _contactCyr2LatProfileId = {};
final Set<String> _knownContactKeys = {};
final Map<String, int> _contactUnreadCount = {};
@@ -460,7 +479,14 @@ class MeshCoreConnector extends ChangeNotifier {
Stream<Uint8List> get receivedFrames => _receivedFramesController.stream;
Uint8List? get selfPublicKey => _selfPublicKey;
String get selfPublicKeyHex => pubKeyToHex(_selfPublicKey ?? Uint8List(0));
/// First 2 bytes of the local public key, big-endian: the `senderPrefix`
/// stamped into every outgoing image chunk header. Null until SELF_INFO.
int? get imageSenderPrefix => senderPrefixFromKey(_selfPublicKey);
String? get selfName => _selfName;
String? get manufacturerName => _manufacturerName;
String? get firmwareVersionString => _firmwareVersionString;
double? get selfLatitude => _selfLatitude;
double? get selfLongitude => _selfLongitude;
List<DirectRepeater> get directRepeaters => _directRepeaters;
@@ -758,13 +784,13 @@ class MeshCoreConnector extends ChangeNotifier {
return _contactSmazEnabled[contactKeyHex] ?? false;
}
bool isChannelImagesEnabled(int channelIndex) {
return _channelImagesEnabled[channelIndex] ?? false;
bool isChannelUrlImagesEnabled(int channelIndex) {
return _channelUrlImagesEnabled[channelIndex] ?? false;
}
bool isContactImagesEnabled(String contactKeyHex) {
_ensureContactImagesSettingLoaded(contactKeyHex);
return _contactImagesEnabled[contactKeyHex] ?? false;
bool isContactUrlImagesEnabled(String contactKeyHex) {
_ensureContactUrlImagesSettingLoaded(contactKeyHex);
return _contactUrlImagesEnabled[contactKeyHex] ?? false;
}
bool hasChannelRegion(int channelIndex) {
@@ -909,18 +935,21 @@ class MeshCoreConnector extends ChangeNotifier {
notifyListeners();
}
Future<void> setChannelImagesEnabled(int channelIndex, bool enabled) async {
_channelImagesEnabled[channelIndex] = enabled;
await _channelSettingsStore.saveImagesEnabled(channelIndex, enabled);
Future<void> setChannelUrlImagesEnabled(
int channelIndex,
bool enabled,
) async {
_channelUrlImagesEnabled[channelIndex] = enabled;
await _channelSettingsStore.saveUrlImagesEnabled(channelIndex, enabled);
notifyListeners();
}
Future<void> setContactImagesEnabled(
Future<void> setContactUrlImagesEnabled(
String contactKeyHex,
bool enabled,
) async {
_contactImagesEnabled[contactKeyHex] = enabled;
await _contactSettingsStore.saveImagesEnabled(contactKeyHex, enabled);
_contactUrlImagesEnabled[contactKeyHex] = enabled;
await _contactSettingsStore.saveUrlImagesEnabled(contactKeyHex, enabled);
notifyListeners();
}
@@ -1018,11 +1047,19 @@ class MeshCoreConnector extends ChangeNotifier {
AppDebugLogService? appDebugLogService,
BackgroundService? backgroundService,
TimeoutPredictionService? timeoutPredictionService,
ImageCodecService? imageCodecService,
ImageChunkTransport? imageTransport,
void Function(ImageChunkOutcome outcome)? onImageChunk,
void Function(int senderPrefix)? onImageSenderPrefix,
}) {
_retryService = retryService;
_pathHistoryService = pathHistoryService;
_appSettingsService = appSettingsService;
_translationService = translationService;
_imageCodecService = imageCodecService;
_imageTransport = imageTransport;
_onImageChunk = onImageChunk;
_onImageSenderPrefix = onImageSenderPrefix;
_bleDebugLogService = bleDebugLogService;
_appDebugLogService = appDebugLogService;
_backgroundService = backgroundService;
@@ -1096,7 +1133,7 @@ class MeshCoreConnector extends ChangeNotifier {
for (final contact in cached) {
_ensureContactSmazSettingLoaded(contact.publicKeyHex);
_ensureContactCyr2LatSettingLoaded(contact.publicKeyHex);
_ensureContactImagesSettingLoaded(contact.publicKeyHex);
_ensureContactUrlImagesSettingLoaded(contact.publicKeyHex);
}
}
@@ -1117,16 +1154,15 @@ class MeshCoreConnector extends ChangeNotifier {
Future<void> loadChannelSettings({int? maxChannels}) async {
_channelSmazEnabled.clear();
_channelCyr2LatEnabled.clear();
_channelImagesEnabled.clear();
_channelUrlImagesEnabled.clear();
_channelRegions.clear();
final channelCount = maxChannels ?? _maxChannels;
for (int i = 0; i < channelCount; i++) {
_channelSmazEnabled[i] = await _channelSettingsStore.loadSmazEnabled(i);
_channelCyr2LatEnabled[i] = await _channelSettingsStore
.loadCyr2LatEnabled(i);
_channelImagesEnabled[i] = await _channelSettingsStore.loadImagesEnabled(
i,
);
_channelUrlImagesEnabled[i] = await _channelSettingsStore
.loadUrlImagesEnabled(i);
_channelRegions[i] = await _channelRegionStore.loadRegion(i);
}
}
@@ -2602,6 +2638,8 @@ class MeshCoreConnector extends ChangeNotifier {
void _resetConnectionHandshakeState() {
_selfPublicKey = null;
// Partially received images belong to the previous session's sender prefix.
_imageTransport?.reassembler.clear();
_selfName = null;
_selfLatitude = null;
_selfLongitude = null;
@@ -2745,6 +2783,19 @@ class MeshCoreConnector extends ChangeNotifier {
_channelSyncTimeout = null;
_channelSyncRetries = 0;
await _translationService?.releaseModel();
// The decode graph is ~833 MB resident; drop it with the connection. The
// connector never disposes services it does not own (main.dart does).
//
// Cancel first: `releaseModel` serialises behind the codec's exclusive
// lock, so disconnecting mid-decode would otherwise block this teardown
// for the ~1 s the inference takes and then tear the session out from
// under a job whose result nobody is waiting for any more.
_imageCodecService?.cancelCodecJob();
await _imageCodecService?.releaseModel();
// Partial images belong to this session's sender prefixes; a reconnect
// starts over. Dropping them also stops `evictExpired` from firing "image
// incomplete" failures into the store minutes after the radio went away.
_imageTransport?.reassembler.clear();
if (!skipBleDeviceDisconnect) {
try {
@@ -3577,6 +3628,77 @@ class MeshCoreConnector extends ChangeNotifier {
}, region: getChannelRegion(channel.index));
}
/// Minimum companion firmware version code that implements
/// CMD_SEND_CHANNEL_DATA (62) / RESP_CODE_CHANNEL_DATA_RECV (27).
static const int _minFirmwareVerCodeForChannelData = 13;
/// True when the connected device can send/receive GRP_DATA blobs.
bool get supportsChannelData =>
isConnected &&
(_firmwareVerCode ?? 0) >= _minFirmwareVerCodeForChannelData;
/// Sends one image's chunk [blobs] as GRP_DATA packets on [channelIndex],
/// strictly sequentially, inside a single scoped-send scope for the whole
/// image (the flood scope is global, so re-entering it per chunk would let
/// another channel's send interleave mid-image).
///
/// [onProgress] fires after each chunk is acked, so the UI can show
/// "sending 2 of 3".
///
/// IMPORTANT: the command ACK only proves the firmware accepted the blob and
/// queued a packet to the radio. It says nothing about transmission or
/// delivery — GRP_DATA is unacknowledged flood traffic, there is no
/// PUSH_CODE_SEND_CONFIRMED for it, and a chunk may still be lost on air.
/// That is what the XOR parity chunk is for.
///
/// Returns false when the transport is unavailable (disconnected or firmware
/// too old); throws whatever [_sendFrameAndWaitForCommandAck] throws, i.e.
/// Exception('Command failed with error code N') where N is 1
/// (unsupported command), 2 (bad channel index), 3 (packet pool full, worth
/// retrying) or 4 (illegal arg — blob too long), or a TimeoutException after
/// _commandAckTimeout.
Future<bool> sendImageChunks(
List<Uint8List> blobs, {
required int channelIndex,
required Duration interChunkDelay,
void Function(int sent, int total)? onProgress,
}) async {
if (blobs.isEmpty) return false;
if (!supportsChannelData) return false;
// `return` inside the scoped callback only exits the callback, so a
// disconnect mid-send used to fall through to `return true` below and the
// caller would record a complete outgoing image and tell the user "sent"
// after transmitting a prefix of the chunks. Track it explicitly.
var sentAll = true;
await _runScopedChannelSend(() async {
for (var i = 0; i < blobs.length; i++) {
if (!isConnected) {
sentAll = false;
return;
}
if (i > 0) await Future<void>.delayed(interChunkDelay);
// Same collision avoidance as every other channel send. On a quiet mesh
// this returns almost immediately; interChunkDelay is what actually
// paces the chunks so we do not talk over our own previous packet.
await _waitForRadioQuiet(lastInboundRxTime: _lastChannelMsgRxTime);
await _sendFrameAndWaitForCommandAck(
buildSendChannelDataFrame(
channelIndex: channelIndex,
dataType: dataTypeAeicImage,
payload: blobs[i],
pathLen: outPathUnknown, // 0xFF => flood
),
// Code 62 must not be enrolled in the generic-ack queue, and the
// firmware replies with RESP_CODE_OK (writeOKFrame), not RESP_CODE_SENT.
expectsGenericAck: false,
successCode: respCodeOk,
);
onProgress?.call(i + 1, blobs.length);
}
}, region: getChannelRegion(channelIndex));
return sentAll;
}
Future<void> _runScopedChannelSend(
Future<void> Function() action, {
required String region,
@@ -4258,6 +4380,27 @@ class MeshCoreConnector extends ChangeNotifier {
case respCodeChannelMsgRecvV3:
_handleIncomingChannelMessage(frame);
break;
case respCodeChannelDataRecv:
// RESP_CODE 27 (GRP_DATA received) is delivered through the firmware
// offline queue in reply to CMD_SYNC_NEXT_MESSAGE, so the queued-message
// sync state machine MUST be advanced here — exactly like
// _handleIncomingChannelMessage does. Leaving code 27 unhandled stalls
// _queueSyncTimeout (5 s) x _maxQueueSyncRetries (3) per chunk and
// blocks every other queued message behind it. A receivedFrames
// listener cannot do this: the sync state is private. Advance exactly
// ONCE, before any async work.
if (_isSyncingQueuedMessages) {
_handleQueuedMessageReceived();
}
// Advance the sync state machine ALWAYS (above), but only reassemble
// when the feature is actually on. Without this the "disabled" feature
// still reassembles, persists and displays received images: the store
// keeps them and channel_chat_screen renders every stored entry, so a
// user who never enabled image messages would see them appear.
if (_appSettingsService?.settings.imageMessagesEnabled ?? false) {
_handleIncomingChannelData(frame);
}
break;
case respCodeSent:
_handleMessageSent(frame);
break;
@@ -4396,6 +4539,14 @@ class MeshCoreConnector extends ChangeNotifier {
_currentCr = reader.readByte();
_selfName = reader.readCString();
// The local public key only exists once SELF_INFO has arrived, so the
// image sender prefix (first 2 bytes, big-endian) is refreshed here.
final prefix = senderPrefixFromKey(_selfPublicKey);
if (prefix != null) {
_imageTransport?.senderPrefix = prefix;
_onImageSenderPrefix?.call(prefix);
}
} catch (e) {
_appDebugLogService?.error(
'Error parsing SELF_INFO frame: $e',
@@ -4491,6 +4642,20 @@ class MeshCoreConnector extends ChangeNotifier {
}
_firmwareVerCode = frame[1];
// Manufacturer/model name (bytes 20..59) and firmware version (60..79)
String? readCString(int start, int maxLen) {
if (frame.length < start + maxLen) return null;
final bytes = frame.sublist(start, start + maxLen);
final end = bytes.indexOf(0);
final s = String.fromCharCodes(
end >= 0 ? bytes.sublist(0, end) : bytes,
).trim();
return s.isEmpty ? null : s;
}
_manufacturerName = readCString(20, 40);
_firmwareVersionString = readCString(60, 20);
// Parse client_repeat from firmware v9+ (byte 80)
if (frame.length >= 81) {
_clientRepeat = frame[80] != 0;
@@ -5182,8 +5347,8 @@ class MeshCoreConnector extends ChangeNotifier {
await _notificationService.showMessageNotification(
contactName: c?.name ?? 'Unknown',
message: resolvedText,
imagesEnabled:
c != null && isContactImagesEnabled(c.publicKeyHex),
urlImagesEnabled:
c != null && isContactUrlImagesEnabled(c.publicKeyHex),
contactId: msg.senderKeyHex,
badgeCount: getTotalUnreadCount(),
);
@@ -5198,8 +5363,8 @@ class MeshCoreConnector extends ChangeNotifier {
await _notificationService.showMessageNotification(
contactName: c?.name ?? 'Unknown Room',
message: resolvedText,
imagesEnabled:
c != null && isContactImagesEnabled(c.publicKeyHex),
urlImagesEnabled:
c != null && isContactUrlImagesEnabled(c.publicKeyHex),
contactId: msg.senderKeyHex,
badgeCount: getTotalUnreadCount(),
);
@@ -5350,11 +5515,11 @@ class MeshCoreConnector extends ChangeNotifier {
});
}
void _ensureContactImagesSettingLoaded(String contactKeyHex) {
if (_contactImagesEnabled.containsKey(contactKeyHex)) return;
_contactSettingsStore.loadImagesEnabled(contactKeyHex).then((enabled) {
if (_contactImagesEnabled[contactKeyHex] == enabled) return;
_contactImagesEnabled[contactKeyHex] = enabled;
void _ensureContactUrlImagesSettingLoaded(String contactKeyHex) {
if (_contactUrlImagesEnabled.containsKey(contactKeyHex)) return;
_contactSettingsStore.loadUrlImagesEnabled(contactKeyHex).then((enabled) {
if (_contactUrlImagesEnabled[contactKeyHex] == enabled) return;
_contactUrlImagesEnabled[contactKeyHex] = enabled;
notifyListeners();
});
}
@@ -5513,13 +5678,26 @@ class MeshCoreConnector extends ChangeNotifier {
channelName: label,
senderName: message.senderName,
message: resolvedText,
imagesEnabled: isChannelImagesEnabled(channelIndex),
urlImagesEnabled: isChannelUrlImagesEnabled(channelIndex),
channelIndex: message.channelIndex,
badgeCount: getTotalUnreadCount(),
);
}());
}
/// Handles RESP_CODE_CHANNEL_DATA_RECV (GRP_DATA). Frames whose dataType is
/// not 0xAE1C — or that arrive before an [ImageChunkTransport] is installed —
/// are ignored without disturbing any other behaviour.
void _handleIncomingChannelData(Uint8List frame) {
// Anchor the post-RX backoff for our own next channel send, exactly as
// _handleIncomingChannelMessage does.
_lastChannelMsgRxTime = DateTime.now();
final outcome = _imageTransport?.handleFrame(frame);
if (outcome == null) return; // Not an AEIC image chunk.
_onImageChunk?.call(outcome);
notifyListeners();
}
void _handleIncomingChannelMessage(Uint8List frame) async {
// If we're syncing the queued messages, advance the queue immediately
// before any potentially long async work (like translation/notifications).