fix(#1117): restore to radio places by identity, reports what landed

Restore to radio (#1060) replayed each cached channel into its remembered
slot, in place, and reported how many frames it sent. On the firmware
shipping next that is refused for every erased slot (an erased record
reads as occupied, #1865), for slot 0 unless it is Public, and for Public
anywhere else (#1896). It would still have said it restored them.

planChannelRestore (lib/helpers/channel_restore_plan.dart) now places by
identity:
- a cached channel whose key is already live on the radio is left out
- Public goes to slot 0
- any other channel goes to its remembered slot when that slot is free or
  erased and not slot 0, otherwise to the lowest free slot
- a live channel is never a destination, so nothing is overwritten
- an erased slot is cleared before it is written

Every write goes through _writeChannelSlot and waits for the radio's OK or
ERR. The result is what the radio confirmed. The screen says "The radio
confirmed all N channel(s)", or "confirmed X of Y. Not restored: ...",
persistently. Waiting for each answer also paces the writes by the radio
itself, which replaces the fixed 10 ms gap. The comment claiming a confirmed
count was impossible is gone. The confirm dialog lists only the channels the
radio is missing, and no longer warns that it replaces what the slots hold,
because it doesn't.

The #1060 restore tests are rewritten to the new contract, keeping what they
protected (no erased entry written back, nothing cached is refused, the key
travels with the channel, writes are paced), and adding: slot 0 for Public
only, clear-before-write, nothing live rewritten or overwritten, refusals
reported by name.

Mutation-checked with this commit and the previous one: not skipping slot
0 fails 5 tests; never clearing first fails 1; counting sends as confirmed
fails 1; allowing a Public delete fails 2; a one-# hashtag rule fails 1.
Full suite 2034 pass, analyze clean.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
fix/1896-client-slot0-public
diffwireauto 3 weeks ago
parent b16afb35ec
commit 7a1a7fec07

@ -17,6 +17,7 @@ import '../models/message.dart';
import '../models/offband_gps_status.dart';
import '../models/path_selection.dart';
import '../models/translation_support.dart';
import '../helpers/channel_restore_plan.dart';
import '../helpers/channel_send_timestamp.dart';
import '../helpers/pending_reactions.dart';
import '../services/corescope_service.dart';
@ -646,11 +647,6 @@ class MeshCoreConnector extends ChangeNotifier {
List<Channel> _previousChannelsCache = [];
static const int _maxChannelSyncRetries = 3;
/// Gap between consecutive channel writes in [restoreChannelsToRadio], so a
/// multi-channel restore cannot outrun the radio. Matches the spacing the USB
/// branch of [sendFrame] already applies per frame (#1060).
static const Duration _channelRestoreFrameGap = Duration(milliseconds: 10);
// #1062: fail-closed channel sync. A slot the device never answered for is
// UNKNOWN: tracked here, surfaced to the UI, and never backfilled from a
// previous list (that backfill is how a stale slot map misrouted
@ -7309,64 +7305,77 @@ class MeshCoreConnector extends ChangeNotifier {
}
}
/// Writes the client's cached channel list back onto the radio (#1060).
///
/// For a radio that lost channel storage (reflash, wipe) the client's cache
/// is the only surviving copy. Each cached channel goes back to the slot it
/// came from, so PSK-keyed history and per-channel settings line up again.
/// Cached channels the radio no longer has: what a restore would put back.
List<Channel> get channelsMissingFromRadio {
final liveKeys = _channels.map((c) => c.pskHex).toSet();
final seen = <String>{};
return [
for (final c in _cachedChannels)
if (!c.isUnusable && !liveKeys.contains(c.pskHex) && seen.add(c.pskHex))
c,
];
}
/// Writes the client's cached channels back onto a radio that lost them
/// (#1060), placed by identity (see [planChannelRestore]) so it works on
/// firmware that refuses occupied slots (#1865) and keeps slot 0 for Public
/// (#1896).
///
/// Frames are sent directly rather than through [setChannel] because that
/// re-syncs after every write; this re-syncs once at the end. Nothing here
/// clears history: the identities being restored are the ones that own it.
/// Each write waits for the radio's OK or ERR, so the result says what the
/// radio confirmed, not what was sent. Nothing here clears history: the
/// identities being restored are the ones that own it.
///
/// Returns the number of channels written, or -1 when there was nothing to
/// restore or the link is down. Written is not the same as confirmed: the
/// erased-slot banner is what tells the user the radio took them.
Future<int> restoreChannelsToRadio() async {
if (!isConnected) return -1;
final toRestore = _cachedChannels.where((c) => !c.isUnusable).toList();
if (toRestore.isEmpty) return -1;
/// Returns null when the link is down or nothing is missing.
Future<ChannelRestoreResult?> restoreChannelsToRadio() async {
if (!isConnected) return null;
final plan = planChannelRestore(
cached: _cachedChannels,
live: _channels,
erasedSlots: _erasedChannelSlots,
maxChannels: _maxChannels,
);
if (plan.steps.isEmpty && plan.noRoom.isEmpty) return null;
_appDebugLogService?.info(
'Restoring ${toRestore.length} cached channel(s) to the radio: '
'${toRestore.map((c) => 'slot ${c.index} "${c.name}"').join(', ')}',
'Restoring ${plan.steps.length} cached channel(s) to the radio: '
'${plan.steps.map((s) => 'slot ${s.slot} "${s.channel.name}"${s.clearFirst ? ' (clear first)' : ''}').join(', ')}'
'${plan.noRoom.isEmpty ? '' : '; no room for ${plan.noRoom.map((c) => '"${c.name}"').join(', ')}'}',
tag: 'ChannelSync',
);
// Counted as a write in flight for its whole length, so an add made while
// it runs is refused instead of choosing a slot this is about to fill
// (#1021, #1026).
// (#1021, #1026). Each write is answered before the next goes out, which
// also paces the radio.
_beginChannelWrite();
try {
var sent = 0;
for (final channel in toRestore) {
if (!isConnected) break;
await sendFrame(
buildSetChannelFrame(channel.index, channel.name, channel.psk),
var confirmed = 0;
final notRestored = <Channel>[...plan.noRoom];
for (final step in plan.steps) {
if (step.clearFirst) {
final cleared = await _writeChannelSlot(step.slot, '', Uint8List(16));
if (cleared != ChannelWrite.ok) {
notRestored.add(step.channel);
continue;
}
}
final written = await _writeChannelSlot(
step.slot,
step.channel.name,
step.channel.psk,
);
sent++;
// Space the writes. On BLE [sendFrame] prefers write-without-response,
// which returns once the frame reaches the OS and not once the radio
// has taken it, so an unpaced loop can overrun the radio and lose
// channels with no error anywhere. The USB branch of [sendFrame]
// already spaces its own frames for exactly this reason; BLE has no
// such pause.
if (sent < toRestore.length) {
await Future<void>.delayed(_channelRestoreFrameGap);
if (written == ChannelWrite.ok) {
confirmed++;
} else {
notRestored.add(step.channel);
}
}
// The radio now holds these again, so a fresh sync should confirm them
// and the erased-slot block clears itself on the next pass.
//
// This deliberately does NOT report how many the radio confirmed:
// [getChannels] returns once the first request is away, not once the
// sync has finished, so counting confirmations here would read a list
// the radio has not answered for yet and report 0 every time. The
// authoritative feedback is the erased-slot banner, which is re-derived
// on every sync and therefore stays up if some writes did not land.
_syncAfterChannelWrites();
return sent;
return ChannelRestoreResult(
confirmed: confirmed,
total: plan.steps.length + plan.noRoom.length,
notRestored: notRestored,
);
} finally {
_endChannelWrite();
}

@ -0,0 +1,121 @@
import '../models/channel.dart';
/// One channel to write back to the radio, and where.
class ChannelRestoreStep {
const ChannelRestoreStep({
required this.channel,
required this.slot,
required this.clearFirst,
});
final Channel channel;
final int slot;
/// The slot holds an erased record. Firmware treats that as occupied
/// (#1865), so it is cleared before the channel is written.
final bool clearFirst;
}
/// What a restore achieved, as the radio reported it.
class ChannelRestoreResult {
const ChannelRestoreResult({
required this.confirmed,
required this.total,
required this.notRestored,
});
/// Channels the radio confirmed taking.
final int confirmed;
/// Channels the restore set out to put back.
final int total;
/// Channels the radio refused or never answered for, or had no room for.
final List<Channel> notRestored;
}
class ChannelRestorePlan {
const ChannelRestorePlan(this.steps, this.noRoom);
final List<ChannelRestoreStep> steps;
/// Cached channels with no slot left to go to.
final List<Channel> noRoom;
}
/// Decides how to put a radio's lost channels back (#1060, #1117).
///
/// The client's cached list says which channels the radio had. Replaying each
/// one into its remembered slot no longer works on current firmware, which
/// refuses a channel over an occupied slot (#1865), treats an erased record
/// as occupied, refuses any channel but Public in slot 0, and refuses Public
/// anywhere else (#1896). So this places by identity:
///
/// * A cached channel whose key is already live on the radio, in any slot, is
/// left out: the radio has it.
/// * Public goes to slot 0. If slot 0 holds a live channel (older firmware
/// only), Public takes a free slot instead, so nothing is overwritten.
/// * Any other channel goes back to its remembered slot when that slot is
/// free or erased and is not slot 0; otherwise to the lowest free slot.
/// * A live channel is never a destination, so nothing on the radio is
/// overwritten.
/// * An erased slot is cleared first.
///
/// Public is placed first, so slot 0 is never used by anything else.
ChannelRestorePlan planChannelRestore({
required List<Channel> cached,
required List<Channel> live,
required Set<int> erasedSlots,
required int maxChannels,
}) {
final liveKeys = live.map((c) => c.pskHex).toSet();
final taken = live.map((c) => c.index).toSet();
final candidates =
cached
.where((c) => !c.isUnusable && !liveKeys.contains(c.pskHex))
.toList()
..sort(
(a, b) => (b.isPublicChannel ? 1 : 0) - (a.isPublicChannel ? 1 : 0),
);
final steps = <ChannelRestoreStep>[];
final noRoom = <Channel>[];
final placedKeys = <String>{};
bool free(int slot) =>
slot >= 0 && slot < maxChannels && !taken.contains(slot);
int? firstFree() {
for (var i = 0; i < maxChannels; i++) {
if (i == Channel.publicSlot) continue;
if (free(i)) return i;
}
return null;
}
for (final channel in candidates) {
// A cache can hold the same key twice; write it once.
if (!placedKeys.add(channel.pskHex)) continue;
final int? slot;
if (channel.isPublicChannel) {
slot = free(Channel.publicSlot) ? Channel.publicSlot : firstFree();
} else if (channel.index != Channel.publicSlot && free(channel.index)) {
slot = channel.index;
} else {
slot = firstFree();
}
if (slot == null) {
noRoom.add(channel);
continue;
}
taken.add(slot);
steps.add(
ChannelRestoreStep(
channel: channel,
slot: slot,
clearFirst: erasedSlots.contains(slot),
),
);
}
return ChannelRestorePlan(steps, noRoom);
}

@ -878,7 +878,7 @@
}
},
"channels_restoreToRadio": "Restore to radio",
"channels_restoreToRadioConfirm": "Write {count} channel(s) back to the radio: {names}. This replaces whatever those slots hold now.",
"channels_restoreToRadioConfirm": "Write {count} channel(s) back to the radio: {names}. Channels the radio still has are left as they are.",
"@channels_restoreToRadioConfirm": {
"placeholders": {
"count": {
@ -889,7 +889,7 @@
}
}
},
"channels_restoreToRadioDone": "Wrote {count} channel(s) back to the radio. If the warning above stays, some did not take.",
"channels_restoreToRadioDone": "The radio confirmed all {count} channel(s).",
"@channels_restoreToRadioDone": {
"placeholders": {
"count": {
@ -897,6 +897,21 @@
}
}
},
"channels_restoreToRadioPartial": "The radio confirmed {confirmed} of {total} channel(s). Not restored: {names}.",
"@channels_restoreToRadioPartial": {
"description": "Restore to radio finished with some channels refused, unanswered, or without a free slot. Persistent (#1117).",
"placeholders": {
"confirmed": {
"type": "int"
},
"total": {
"type": "int"
},
"names": {
"type": "String"
}
}
},
"channels_restoreToRadioFailed": "Could not restore the channel list: nothing cached to restore, or the radio is not connected.",
"channels_slotIdentityChanged": "Channel layout changed on the radio: {changes}",
"@channels_slotIdentityChanged": {

@ -2905,15 +2905,21 @@ abstract class AppLocalizations {
/// No description provided for @channels_restoreToRadioConfirm.
///
/// In en, this message translates to:
/// **'Write {count} channel(s) back to the radio: {names}. This replaces whatever those slots hold now.'**
/// **'Write {count} channel(s) back to the radio: {names}. Channels the radio still has are left as they are.'**
String channels_restoreToRadioConfirm(int count, String names);
/// No description provided for @channels_restoreToRadioDone.
///
/// In en, this message translates to:
/// **'Wrote {count} channel(s) back to the radio. If the warning above stays, some did not take.'**
/// **'The radio confirmed all {count} channel(s).'**
String channels_restoreToRadioDone(int count);
/// Restore to radio finished with some channels refused, unanswered, or without a free slot. Persistent (#1117).
///
/// In en, this message translates to:
/// **'The radio confirmed {confirmed} of {total} channel(s). Not restored: {names}.'**
String channels_restoreToRadioPartial(int confirmed, int total, String names);
/// No description provided for @channels_restoreToRadioFailed.
///
/// In en, this message translates to:

@ -1635,12 +1635,21 @@ class AppLocalizationsBg extends AppLocalizations {
@override
String channels_restoreToRadioConfirm(int count, String names) {
return 'Write $count channel(s) back to the radio: $names. This replaces whatever those slots hold now.';
return 'Write $count channel(s) back to the radio: $names. Channels the radio still has are left as they are.';
}
@override
String channels_restoreToRadioDone(int count) {
return 'Wrote $count channel(s) back to the radio. If the warning above stays, some did not take.';
return 'The radio confirmed all $count channel(s).';
}
@override
String channels_restoreToRadioPartial(
int confirmed,
int total,
String names,
) {
return 'The radio confirmed $confirmed of $total channel(s). Not restored: $names.';
}
@override

@ -1629,12 +1629,21 @@ class AppLocalizationsDe extends AppLocalizations {
@override
String channels_restoreToRadioConfirm(int count, String names) {
return 'Write $count channel(s) back to the radio: $names. This replaces whatever those slots hold now.';
return 'Write $count channel(s) back to the radio: $names. Channels the radio still has are left as they are.';
}
@override
String channels_restoreToRadioDone(int count) {
return 'Wrote $count channel(s) back to the radio. If the warning above stays, some did not take.';
return 'The radio confirmed all $count channel(s).';
}
@override
String channels_restoreToRadioPartial(
int confirmed,
int total,
String names,
) {
return 'The radio confirmed $confirmed of $total channel(s). Not restored: $names.';
}
@override

@ -1611,12 +1611,21 @@ class AppLocalizationsEn extends AppLocalizations {
@override
String channels_restoreToRadioConfirm(int count, String names) {
return 'Write $count channel(s) back to the radio: $names. This replaces whatever those slots hold now.';
return 'Write $count channel(s) back to the radio: $names. Channels the radio still has are left as they are.';
}
@override
String channels_restoreToRadioDone(int count) {
return 'Wrote $count channel(s) back to the radio. If the warning above stays, some did not take.';
return 'The radio confirmed all $count channel(s).';
}
@override
String channels_restoreToRadioPartial(
int confirmed,
int total,
String names,
) {
return 'The radio confirmed $confirmed of $total channel(s). Not restored: $names.';
}
@override

@ -1630,12 +1630,21 @@ class AppLocalizationsEs extends AppLocalizations {
@override
String channels_restoreToRadioConfirm(int count, String names) {
return 'Write $count channel(s) back to the radio: $names. This replaces whatever those slots hold now.';
return 'Write $count channel(s) back to the radio: $names. Channels the radio still has are left as they are.';
}
@override
String channels_restoreToRadioDone(int count) {
return 'Wrote $count channel(s) back to the radio. If the warning above stays, some did not take.';
return 'The radio confirmed all $count channel(s).';
}
@override
String channels_restoreToRadioPartial(
int confirmed,
int total,
String names,
) {
return 'The radio confirmed $confirmed of $total channel(s). Not restored: $names.';
}
@override

@ -1635,12 +1635,21 @@ class AppLocalizationsFr extends AppLocalizations {
@override
String channels_restoreToRadioConfirm(int count, String names) {
return 'Write $count channel(s) back to the radio: $names. This replaces whatever those slots hold now.';
return 'Write $count channel(s) back to the radio: $names. Channels the radio still has are left as they are.';
}
@override
String channels_restoreToRadioDone(int count) {
return 'Wrote $count channel(s) back to the radio. If the warning above stays, some did not take.';
return 'The radio confirmed all $count channel(s).';
}
@override
String channels_restoreToRadioPartial(
int confirmed,
int total,
String names,
) {
return 'The radio confirmed $confirmed of $total channel(s). Not restored: $names.';
}
@override

@ -1636,12 +1636,21 @@ class AppLocalizationsHu extends AppLocalizations {
@override
String channels_restoreToRadioConfirm(int count, String names) {
return 'Write $count channel(s) back to the radio: $names. This replaces whatever those slots hold now.';
return 'Write $count channel(s) back to the radio: $names. Channels the radio still has are left as they are.';
}
@override
String channels_restoreToRadioDone(int count) {
return 'Wrote $count channel(s) back to the radio. If the warning above stays, some did not take.';
return 'The radio confirmed all $count channel(s).';
}
@override
String channels_restoreToRadioPartial(
int confirmed,
int total,
String names,
) {
return 'The radio confirmed $confirmed of $total channel(s). Not restored: $names.';
}
@override

@ -1631,12 +1631,21 @@ class AppLocalizationsIt extends AppLocalizations {
@override
String channels_restoreToRadioConfirm(int count, String names) {
return 'Write $count channel(s) back to the radio: $names. This replaces whatever those slots hold now.';
return 'Write $count channel(s) back to the radio: $names. Channels the radio still has are left as they are.';
}
@override
String channels_restoreToRadioDone(int count) {
return 'Wrote $count channel(s) back to the radio. If the warning above stays, some did not take.';
return 'The radio confirmed all $count channel(s).';
}
@override
String channels_restoreToRadioPartial(
int confirmed,
int total,
String names,
) {
return 'The radio confirmed $confirmed of $total channel(s). Not restored: $names.';
}
@override

@ -1579,12 +1579,21 @@ class AppLocalizationsJa extends AppLocalizations {
@override
String channels_restoreToRadioConfirm(int count, String names) {
return 'Write $count channel(s) back to the radio: $names. This replaces whatever those slots hold now.';
return 'Write $count channel(s) back to the radio: $names. Channels the radio still has are left as they are.';
}
@override
String channels_restoreToRadioDone(int count) {
return 'Wrote $count channel(s) back to the radio. If the warning above stays, some did not take.';
return 'The radio confirmed all $count channel(s).';
}
@override
String channels_restoreToRadioPartial(
int confirmed,
int total,
String names,
) {
return 'The radio confirmed $confirmed of $total channel(s). Not restored: $names.';
}
@override

@ -1574,12 +1574,21 @@ class AppLocalizationsKo extends AppLocalizations {
@override
String channels_restoreToRadioConfirm(int count, String names) {
return 'Write $count channel(s) back to the radio: $names. This replaces whatever those slots hold now.';
return 'Write $count channel(s) back to the radio: $names. Channels the radio still has are left as they are.';
}
@override
String channels_restoreToRadioDone(int count) {
return 'Wrote $count channel(s) back to the radio. If the warning above stays, some did not take.';
return 'The radio confirmed all $count channel(s).';
}
@override
String channels_restoreToRadioPartial(
int confirmed,
int total,
String names,
) {
return 'The radio confirmed $confirmed of $total channel(s). Not restored: $names.';
}
@override

@ -1622,12 +1622,21 @@ class AppLocalizationsNl extends AppLocalizations {
@override
String channels_restoreToRadioConfirm(int count, String names) {
return 'Write $count channel(s) back to the radio: $names. This replaces whatever those slots hold now.';
return 'Write $count channel(s) back to the radio: $names. Channels the radio still has are left as they are.';
}
@override
String channels_restoreToRadioDone(int count) {
return 'Wrote $count channel(s) back to the radio. If the warning above stays, some did not take.';
return 'The radio confirmed all $count channel(s).';
}
@override
String channels_restoreToRadioPartial(
int confirmed,
int total,
String names,
) {
return 'The radio confirmed $confirmed of $total channel(s). Not restored: $names.';
}
@override

@ -1641,12 +1641,21 @@ class AppLocalizationsPl extends AppLocalizations {
@override
String channels_restoreToRadioConfirm(int count, String names) {
return 'Write $count channel(s) back to the radio: $names. This replaces whatever those slots hold now.';
return 'Write $count channel(s) back to the radio: $names. Channels the radio still has are left as they are.';
}
@override
String channels_restoreToRadioDone(int count) {
return 'Wrote $count channel(s) back to the radio. If the warning above stays, some did not take.';
return 'The radio confirmed all $count channel(s).';
}
@override
String channels_restoreToRadioPartial(
int confirmed,
int total,
String names,
) {
return 'The radio confirmed $confirmed of $total channel(s). Not restored: $names.';
}
@override

@ -1631,12 +1631,21 @@ class AppLocalizationsPt extends AppLocalizations {
@override
String channels_restoreToRadioConfirm(int count, String names) {
return 'Write $count channel(s) back to the radio: $names. This replaces whatever those slots hold now.';
return 'Write $count channel(s) back to the radio: $names. Channels the radio still has are left as they are.';
}
@override
String channels_restoreToRadioDone(int count) {
return 'Wrote $count channel(s) back to the radio. If the warning above stays, some did not take.';
return 'The radio confirmed all $count channel(s).';
}
@override
String channels_restoreToRadioPartial(
int confirmed,
int total,
String names,
) {
return 'The radio confirmed $confirmed of $total channel(s). Not restored: $names.';
}
@override

@ -1632,12 +1632,21 @@ class AppLocalizationsRu extends AppLocalizations {
@override
String channels_restoreToRadioConfirm(int count, String names) {
return 'Write $count channel(s) back to the radio: $names. This replaces whatever those slots hold now.';
return 'Write $count channel(s) back to the radio: $names. Channels the radio still has are left as they are.';
}
@override
String channels_restoreToRadioDone(int count) {
return 'Wrote $count channel(s) back to the radio. If the warning above stays, some did not take.';
return 'The radio confirmed all $count channel(s).';
}
@override
String channels_restoreToRadioPartial(
int confirmed,
int total,
String names,
) {
return 'The radio confirmed $confirmed of $total channel(s). Not restored: $names.';
}
@override

@ -1622,12 +1622,21 @@ class AppLocalizationsSk extends AppLocalizations {
@override
String channels_restoreToRadioConfirm(int count, String names) {
return 'Write $count channel(s) back to the radio: $names. This replaces whatever those slots hold now.';
return 'Write $count channel(s) back to the radio: $names. Channels the radio still has are left as they are.';
}
@override
String channels_restoreToRadioDone(int count) {
return 'Wrote $count channel(s) back to the radio. If the warning above stays, some did not take.';
return 'The radio confirmed all $count channel(s).';
}
@override
String channels_restoreToRadioPartial(
int confirmed,
int total,
String names,
) {
return 'The radio confirmed $confirmed of $total channel(s). Not restored: $names.';
}
@override

@ -1619,12 +1619,21 @@ class AppLocalizationsSl extends AppLocalizations {
@override
String channels_restoreToRadioConfirm(int count, String names) {
return 'Write $count channel(s) back to the radio: $names. This replaces whatever those slots hold now.';
return 'Write $count channel(s) back to the radio: $names. Channels the radio still has are left as they are.';
}
@override
String channels_restoreToRadioDone(int count) {
return 'Wrote $count channel(s) back to the radio. If the warning above stays, some did not take.';
return 'The radio confirmed all $count channel(s).';
}
@override
String channels_restoreToRadioPartial(
int confirmed,
int total,
String names,
) {
return 'The radio confirmed $confirmed of $total channel(s). Not restored: $names.';
}
@override

@ -1613,12 +1613,21 @@ class AppLocalizationsSv extends AppLocalizations {
@override
String channels_restoreToRadioConfirm(int count, String names) {
return 'Write $count channel(s) back to the radio: $names. This replaces whatever those slots hold now.';
return 'Write $count channel(s) back to the radio: $names. Channels the radio still has are left as they are.';
}
@override
String channels_restoreToRadioDone(int count) {
return 'Wrote $count channel(s) back to the radio. If the warning above stays, some did not take.';
return 'The radio confirmed all $count channel(s).';
}
@override
String channels_restoreToRadioPartial(
int confirmed,
int total,
String names,
) {
return 'The radio confirmed $confirmed of $total channel(s). Not restored: $names.';
}
@override

@ -1627,12 +1627,21 @@ class AppLocalizationsUk extends AppLocalizations {
@override
String channels_restoreToRadioConfirm(int count, String names) {
return 'Write $count channel(s) back to the radio: $names. This replaces whatever those slots hold now.';
return 'Write $count channel(s) back to the radio: $names. Channels the radio still has are left as they are.';
}
@override
String channels_restoreToRadioDone(int count) {
return 'Wrote $count channel(s) back to the radio. If the warning above stays, some did not take.';
return 'The radio confirmed all $count channel(s).';
}
@override
String channels_restoreToRadioPartial(
int confirmed,
int total,
String names,
) {
return 'The radio confirmed $confirmed of $total channel(s). Not restored: $names.';
}
@override

@ -1560,12 +1560,21 @@ class AppLocalizationsZh extends AppLocalizations {
@override
String channels_restoreToRadioConfirm(int count, String names) {
return 'Write $count channel(s) back to the radio: $names. This replaces whatever those slots hold now.';
return 'Write $count channel(s) back to the radio: $names. Channels the radio still has are left as they are.';
}
@override
String channels_restoreToRadioDone(int count) {
return 'Wrote $count channel(s) back to the radio. If the warning above stays, some did not take.';
return 'The radio confirmed all $count channel(s).';
}
@override
String channels_restoreToRadioPartial(
int confirmed,
int total,
String names,
) {
return 'The radio confirmed $confirmed of $total channel(s). Not restored: $names.';
}
@override

@ -1861,14 +1861,13 @@ class _ChannelsScreenState extends State<ChannelsScreen>
}
/// Writes the client's cached channels back onto a radio that lost them
/// (#1060). Confirmed first: it overwrites whatever those slots hold now.
/// (#1060). Confirmed first. Only channels the radio no longer has are
/// written, and nothing it still has is overwritten (#1117).
Future<void> _confirmRestoreChannels(
BuildContext context,
MeshCoreConnector connector,
) async {
final toRestore = connector.cachedChannels
.where((c) => !c.isUnusable)
.toList();
final toRestore = connector.channelsMissingFromRadio;
final confirmed = await showDialog<bool>(
context: context,
builder: (dialogContext) => AlertDialog(
@ -1897,16 +1896,27 @@ class _ChannelsScreenState extends State<ChannelsScreen>
if (!context.mounted) return;
if (refuseWhileChannelTableBusy(context, connector)) return;
final written = await connector.restoreChannelsToRadio();
final result = await connector.restoreChannelsToRadio();
if (!context.mounted) return;
// What the radio confirmed, not what was sent (#1117). Anything short of
// all of it persists, and names what is missing.
final l10n = context.l10n;
final String message;
if (result == null) {
message = l10n.channels_restoreToRadioFailed;
} else if (result.notRestored.isEmpty) {
message = l10n.channels_restoreToRadioDone(result.confirmed);
} else {
message = l10n.channels_restoreToRadioPartial(
result.confirmed,
result.total,
result.notRestored.map((c) => c.name).join(', '),
);
}
showDismissibleSnackBar(
context,
content: Text(
written < 0
? context.l10n.channels_restoreToRadioFailed
: context.l10n.channels_restoreToRadioDone(written),
),
persist: written < 0,
content: Text(message),
persist: result == null || result.notRestored.isNotEmpty,
);
}

@ -203,37 +203,149 @@ void main() {
});
});
// #1117: the restore places by identity and reports what the radio
// confirmed. Current firmware refuses an occupied slot (#1865), reads an
// erased record as occupied, refuses any channel but Public in slot 0, and
// refuses Public anywhere else (#1896).
group('restoring the list to the radio', () {
test('every cached channel is written back to its own slot', () async {
final realPublic = Channel(
index: 0,
name: 'Public',
psk: Channel.parsePskHex(Channel.publicChannelPsk),
);
/// A radio that answers every channel write, OK unless [refuseSlot].
void answerWrites({int? refuseSlot}) {
c.sendFrameOverrideForTest = (data) {
sent.add(data.toList());
if (data.isNotEmpty && data[0] == cmdSetChannel) {
final refused = data[1] == refuseSlot && data[2] != 0;
c.handleFrameForTest(
refused ? [respCodeErr, errCodeBadState] : [respCodeOk],
);
}
};
}
List<List<int>> setFrames() =>
sent.where((f) => f[0] == cmdSetChannel).toList();
test('each missing channel goes back, its key with it', () async {
c.seedCachedChannelsForTest([realPublic, _channel(1, 'wardriving', 40)]);
answerWrites();
sent.clear();
final result = await c.restoreChannelsToRadio();
expect(result!.confirmed, 2);
expect(result.notRestored, isEmpty);
final frames = setFrames();
expect(frames.map((f) => f[1]), [0, 1], reason: 'Public in slot 0');
// The PSK travels with it, so the restored channel keeps the identity
// its history and settings are keyed by.
expect(frames[1].sublist(34, 50), _channel(1, 'wardriving', 40).psk);
});
test('no channel but Public is restored into slot 0', () async {
// Cached at slot 0 under older firmware, where any channel could sit
// there. Current firmware refuses it there, so it moves.
c.seedCachedChannelsForTest([_channel(0, 'old slot 0', 10)]);
answerWrites();
sent.clear();
final result = await c.restoreChannelsToRadio();
expect(result!.confirmed, 1);
expect(setFrames().single[1], isNot(0));
});
test('an erased slot is cleared before it is written', () async {
// The field case: slot 0 survived, slots 1 and 2 came back erased.
await c.getChannels(force: true, maxChannels: 3);
c.handleFrameForTest(
_channelInfoFrame(index: 0, name: 'kept', pskSeed: 10),
);
await _settle(2);
c.handleFrameForTest(_erasedFrame(1));
await _settle(2);
c.handleFrameForTest(_erasedFrame(2));
await _settle();
c.seedCachedChannelsForTest([_channel(1, 'wardriving', 40)]);
answerWrites();
sent.clear();
final result = await c.restoreChannelsToRadio();
expect(result!.confirmed, 1);
final frames = setFrames();
expect(frames.map((f) => f[1]), [1, 1]);
expect(frames[0][2], 0, reason: 'the clear: an empty name');
expect(frames[1].sublist(34, 50), _channel(1, 'wardriving', 40).psk);
});
test('a channel the radio still has is not written again', () async {
await c.getChannels(force: true, maxChannels: 2);
c.handleFrameForTest(
_channelInfoFrame(index: 1, name: 'wardriving', pskSeed: 40),
);
await _settle();
c.seedCachedChannelsForTest([
_channel(0, 'Public', 10),
_channel(1, 'wardriving', 40),
_channel(2, 'test', 70),
]);
expect(c.channelsMissingFromRadio.map((ch) => ch.name), ['test']);
answerWrites();
sent.clear();
final written = await c.restoreChannelsToRadio();
final result = await c.restoreChannelsToRadio();
expect(written, 2);
final setFrames = sent.where((f) => f[0] == cmdSetChannel).toList();
expect(setFrames.length, 2);
expect(setFrames[0][1], 0);
expect(setFrames[1][1], 1);
// The PSK travels with it, so the restored channel keeps the identity
// its history and settings are keyed by.
expect(setFrames[1].sublist(34, 50), _channel(1, 'wardriving', 40).psk);
expect(result!.total, 1);
expect(setFrames().single.sublist(34, 50), _channel(2, 'test', 70).psk);
});
test('a live channel is never overwritten', () async {
// Slot 1 now holds a different channel than the cache remembers there.
await c.getChannels(force: true, maxChannels: 3);
c.handleFrameForTest(
_channelInfoFrame(index: 1, name: 'newer', pskSeed: 90),
);
await _settle();
c.seedCachedChannelsForTest([_channel(1, 'wardriving', 40)]);
answerWrites();
sent.clear();
final result = await c.restoreChannelsToRadio();
expect(result!.confirmed, 1);
expect(setFrames().single[1], 2, reason: 'moved, not over slot 1');
});
test('a refusal is reported by name, not counted as restored', () async {
c.seedCachedChannelsForTest([
_channel(1, 'wardriving', 40),
_channel(2, 'test', 70),
]);
answerWrites(refuseSlot: 2);
final result = await c.restoreChannelsToRadio();
expect(result!.confirmed, 1);
expect(result.total, 2);
expect(result.notRestored.map((ch) => ch.name), ['test']);
});
test('an erased cached entry is never written back', () async {
c.seedCachedChannelsForTest([
_channel(0, 'Public', 10),
Channel.fromFrame(_erasedFrame(1))!,
_channel(1, 'wardriving', 40),
Channel.fromFrame(_erasedFrame(2))!,
]);
answerWrites();
sent.clear();
final written = await c.restoreChannelsToRadio();
final result = await c.restoreChannelsToRadio();
expect(written, 1);
expect(sent.where((f) => f[0] == cmdSetChannel).length, 1);
expect(result!.confirmed, 1);
expect(setFrames().length, 1);
});
test(
@ -241,40 +353,33 @@ void main() {
() async {
c.seedCachedChannelsForTest([]);
expect(await c.restoreChannelsToRadio(), -1);
expect(await c.restoreChannelsToRadio(), isNull);
expect(sent.where((f) => f[0] == cmdSetChannel), isEmpty);
},
);
// Adversarial review finding: on BLE sendFrame prefers write-without-
// response, so its await returns when the frame reaches the OS and not
// when the radio has taken it. An unpaced loop can therefore overrun the
// radio and drop channels with no error raised anywhere. The USB branch
// of sendFrame already spaces its frames; this loop must too.
test(
'consecutive channel writes are spaced, not fired in a tight loop',
() async {
c.seedCachedChannelsForTest([
_channel(0, 'Public', 10),
_channel(1, 'wardriving', 40),
_channel(2, 'test', 70),
]);
sent.clear();
final sw = Stopwatch()..start();
final written = await c.restoreChannelsToRadio();
sw.stop();
expect(written, 3);
// Two gaps between three writes. A lower bound, so this asserts the
// pacing exists without pinning the exact duration.
expect(
sw.elapsedMilliseconds,
greaterThanOrEqualTo(20),
reason: 'three writes with no spacing would return almost instantly',
);
},
);
// Adversarial review finding (#1060): on BLE sendFrame returns when the
// frame reaches the OS, not when the radio has taken it, so an unpaced
// loop can overrun the radio. Each write now waits for the radio's own
// answer before the next goes out, which paces it by the radio itself.
test('the next write waits for the radio to answer the last', () async {
c.seedCachedChannelsForTest([
_channel(1, 'wardriving', 40),
_channel(2, 'test', 70),
]);
sent.clear();
final restore = c.restoreChannelsToRadio();
await _settle();
expect(setFrames().length, 1, reason: 'nothing answered yet');
c.handleFrameForTest([respCodeOk]);
await _settle();
expect(setFrames().length, 2);
c.handleFrameForTest([respCodeOk]);
expect((await restore)!.confirmed, 2);
});
});
// Adversarial review finding: the guards added here stop an erased record
@ -331,7 +436,7 @@ void main() {
expect(c.cachedChannels, isEmpty);
// Nothing usable cached means the restore is refused rather than
// writing erased records back onto the radio.
expect(await c.restoreChannelsToRadio(), -1);
expect(await c.restoreChannelsToRadio(), isNull);
expect(sent.where((f) => f[0] == cmdSetChannel), isEmpty);
});
});

@ -142,6 +142,7 @@
"channels_restoreToRadio",
"channels_restoreToRadioConfirm",
"channels_restoreToRadioDone",
"channels_restoreToRadioPartial",
"channels_restoreToRadioFailed",
"channels_slotIdentityChanged",
"common_dismiss",
@ -520,6 +521,7 @@
"channels_restoreToRadio",
"channels_restoreToRadioConfirm",
"channels_restoreToRadioDone",
"channels_restoreToRadioPartial",
"channels_restoreToRadioFailed",
"channels_slotIdentityChanged",
"common_dismiss",
@ -898,6 +900,7 @@
"channels_restoreToRadio",
"channels_restoreToRadioConfirm",
"channels_restoreToRadioDone",
"channels_restoreToRadioPartial",
"channels_restoreToRadioFailed",
"channels_slotIdentityChanged",
"common_dismiss",
@ -1276,6 +1279,7 @@
"channels_restoreToRadio",
"channels_restoreToRadioConfirm",
"channels_restoreToRadioDone",
"channels_restoreToRadioPartial",
"channels_restoreToRadioFailed",
"channels_slotIdentityChanged",
"common_dismiss",
@ -1654,6 +1658,7 @@
"channels_restoreToRadio",
"channels_restoreToRadioConfirm",
"channels_restoreToRadioDone",
"channels_restoreToRadioPartial",
"channels_restoreToRadioFailed",
"channels_slotIdentityChanged",
"common_dismiss",
@ -2032,6 +2037,7 @@
"channels_restoreToRadio",
"channels_restoreToRadioConfirm",
"channels_restoreToRadioDone",
"channels_restoreToRadioPartial",
"channels_restoreToRadioFailed",
"channels_slotIdentityChanged",
"common_dismiss",
@ -2410,6 +2416,7 @@
"channels_restoreToRadio",
"channels_restoreToRadioConfirm",
"channels_restoreToRadioDone",
"channels_restoreToRadioPartial",
"channels_restoreToRadioFailed",
"channels_slotIdentityChanged",
"common_dismiss",
@ -2788,6 +2795,7 @@
"channels_restoreToRadio",
"channels_restoreToRadioConfirm",
"channels_restoreToRadioDone",
"channels_restoreToRadioPartial",
"channels_restoreToRadioFailed",
"channels_slotIdentityChanged",
"common_dismiss",
@ -3166,6 +3174,7 @@
"channels_restoreToRadio",
"channels_restoreToRadioConfirm",
"channels_restoreToRadioDone",
"channels_restoreToRadioPartial",
"channels_restoreToRadioFailed",
"channels_slotIdentityChanged",
"common_dismiss",
@ -3544,6 +3553,7 @@
"channels_restoreToRadio",
"channels_restoreToRadioConfirm",
"channels_restoreToRadioDone",
"channels_restoreToRadioPartial",
"channels_restoreToRadioFailed",
"channels_slotIdentityChanged",
"common_dismiss",
@ -3922,6 +3932,7 @@
"channels_restoreToRadio",
"channels_restoreToRadioConfirm",
"channels_restoreToRadioDone",
"channels_restoreToRadioPartial",
"channels_restoreToRadioFailed",
"channels_slotIdentityChanged",
"common_dismiss",
@ -4300,6 +4311,7 @@
"channels_restoreToRadio",
"channels_restoreToRadioConfirm",
"channels_restoreToRadioDone",
"channels_restoreToRadioPartial",
"channels_restoreToRadioFailed",
"channels_slotIdentityChanged",
"common_dismiss",
@ -4678,6 +4690,7 @@
"channels_restoreToRadio",
"channels_restoreToRadioConfirm",
"channels_restoreToRadioDone",
"channels_restoreToRadioPartial",
"channels_restoreToRadioFailed",
"channels_slotIdentityChanged",
"common_dismiss",
@ -5056,6 +5069,7 @@
"channels_restoreToRadio",
"channels_restoreToRadioConfirm",
"channels_restoreToRadioDone",
"channels_restoreToRadioPartial",
"channels_restoreToRadioFailed",
"channels_slotIdentityChanged",
"common_dismiss",
@ -5434,6 +5448,7 @@
"channels_restoreToRadio",
"channels_restoreToRadioConfirm",
"channels_restoreToRadioDone",
"channels_restoreToRadioPartial",
"channels_restoreToRadioFailed",
"channels_slotIdentityChanged",
"common_dismiss",
@ -5812,6 +5827,7 @@
"channels_restoreToRadio",
"channels_restoreToRadioConfirm",
"channels_restoreToRadioDone",
"channels_restoreToRadioPartial",
"channels_restoreToRadioFailed",
"channels_slotIdentityChanged",
"common_dismiss",
@ -6190,6 +6206,7 @@
"channels_restoreToRadio",
"channels_restoreToRadioConfirm",
"channels_restoreToRadioDone",
"channels_restoreToRadioPartial",
"channels_restoreToRadioFailed",
"channels_slotIdentityChanged",
"common_dismiss",

Loading…
Cancel
Save

Powered by TurnKey Linux.