fix(#1023): every add-channel path chooses its slot at the write

The channels screen was fixed in #1022, but two more surfaces chose a
channel slot on their own and had the same race.

Community QR scanner:
- A channel share link picked its slot before the confirm dialog and wrote
  it after, so a sync during the confirmation (setChannel itself starts
  one) could leave it pointing at an occupied slot. The slot is now chosen
  after the user confirms, and a refusal keeps the screen open with the
  reason showing.
- Joining a community skipped the public channel silently when the radio
  was full and still showed the green "joined" toast. A refusal now
  explains itself, and the green toast shows only when the channel was
  actually written.
- Its private slot finder is deleted.

Config import:
- planChannelImport takes a required isSyncing. Mid-sync the connector's
  channel list is empty, so the planner would have written every incoming
  channel from slot 0 up, over Public. It now plans nothing and reports
  each channel as skipped with a new channelsSyncing reason. The parameter
  is required so a caller cannot forget it and get the unsafe behavior by
  default.

Both screens now share allocateChannelSlotOrExplain
(lib/helpers/channel_slot_ui.dart), so the "which slot is free" question
has a single answer. channels_noFreeSlot is retired; its only caller moved
to the shared message.

Tests: two planner tests for the syncing case, mutation-checked (with the
guard disabled both fail). Full suite 1781 pass, analyze clean.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
fix/1896-client-slot0-public
diffwireauto 1 month ago committed by Strycher
parent 0506738925
commit 9a5cd0caf7

@ -0,0 +1,59 @@
import 'package:flutter/material.dart';
import '../connector/meshcore_connector.dart';
import '../l10n/l10n.dart';
import 'channel_slot_decision.dart';
import 'snack_bar_builder.dart';
/// A channel slot that is safe to write right now, or null after telling the
/// user why not (#1021).
///
/// Every surface that adds a channel goes through this one function: the
/// channels screen, the community QR scanner, and anything else that writes a
/// slot. That is deliberate. The bug this fixes existed because the same
/// question ("which slot is free?") was answered three different ways in three
/// files, and only one of them was wrong. One answer cannot drift.
///
/// Call it at the moment of the write and never earlier. The connector empties
/// its channel list for the duration of a channel sync, and `setChannel` itself
/// starts one, so a slot chosen before a dialog or a confirmation can point at
/// an occupied slot by the time it is written.
///
/// The owner's ruling is that the unsafe write is blocked but the control stays
/// live: a greyed-out button prevents the damage and tells the user nothing. So
/// a refusal returns null *and* says what is wrong.
int? allocateChannelSlotOrExplain(
BuildContext context,
MeshCoreConnector connector,
) {
final decision = decideChannelSlot(
channels: connector.channels,
maxChannels: connector.maxChannels,
// isSyncingChannels, not hasLoadedChannels: the latter stays false after a
// FAILED sync while the cached list is restored, so gating on it would make
// adding a channel impossible on a flaky link.
isSyncing: connector.isSyncingChannels,
);
switch (decision.refusal) {
case ChannelSlotRefusal.syncing:
showChannelBlockedMessage(context, context.l10n.channels_slotBusySyncing);
return null;
case ChannelSlotRefusal.full:
showChannelBlockedMessage(
context,
context.l10n.channels_slotsAllFull(connector.maxChannels),
);
return null;
case null:
return decision.slot;
}
}
/// A refusal the user has to be able to read and act on, so it persists until
/// dismissed.
///
/// `showDismissibleSnackBar` defaults to `persist: false` and a 4 second toast,
/// which SAFELANE 6 rules out for an error.
void showChannelBlockedMessage(BuildContext context, String message) {
showDismissibleSnackBar(context, content: Text(message), persist: true);
}

@ -2524,7 +2524,6 @@
"channels_scanQrDesc": "Add a community or channel from a QR code",
"channels_scanQrInstructions": "Point at a MeshCore QR code",
"channels_invalidQr": "Invalid QR code",
"channels_noFreeSlot": "No free channel slot",
"channels_qrAddConfirm": "Add channel \"{name}\"?",
"@channels_qrAddConfirm": {
"placeholders": {
@ -3415,6 +3414,10 @@
"@importConfig_reasonNoFreeSlot": {
"description": "Skip reason: every channel slot is occupied (#576)."
},
"importConfig_reasonChannelsSyncing": "channels were still being read from the radio; import again in a moment",
"@importConfig_reasonChannelsSyncing": {
"description": "Skip reason: the channel list was mid-sync, so no slot could be trusted to be free (#1021)."
},
"importConfig_reasonAbsentFromFile": "not present in this file",
"@importConfig_reasonAbsentFromFile": {
"description": "Failure reason: the file did not contain the section (#576)."

@ -7514,12 +7514,6 @@ abstract class AppLocalizations {
/// **'Invalid QR code'**
String get channels_invalidQr;
/// No description provided for @channels_noFreeSlot.
///
/// In en, this message translates to:
/// **'No free channel slot'**
String get channels_noFreeSlot;
/// No description provided for @channels_qrAddConfirm.
///
/// In en, this message translates to:
@ -9514,6 +9508,12 @@ abstract class AppLocalizations {
/// **'no free channel slot on the device'**
String get importConfig_reasonNoFreeSlot;
/// Skip reason: the channel list was mid-sync, so no slot could be trusted to be free (#1021).
///
/// In en, this message translates to:
/// **'channels were still being read from the radio; import again in a moment'**
String get importConfig_reasonChannelsSyncing;
/// Failure reason: the file did not contain the section (#576).
///
/// In en, this message translates to:

@ -4423,9 +4423,6 @@ class AppLocalizationsBg extends AppLocalizations {
@override
String get channels_invalidQr => 'Invalid QR code';
@override
String get channels_noFreeSlot => 'No free channel slot';
@override
String channels_qrAddConfirm(String name) {
return 'Add channel \"$name\"?';
@ -5652,6 +5649,10 @@ class AppLocalizationsBg extends AppLocalizations {
String get importConfig_reasonNoFreeSlot =>
'no free channel slot on the device';
@override
String get importConfig_reasonChannelsSyncing =>
'channels were still being read from the radio; import again in a moment';
@override
String get importConfig_reasonAbsentFromFile => 'not present in this file';

@ -4429,9 +4429,6 @@ class AppLocalizationsDe extends AppLocalizations {
@override
String get channels_invalidQr => 'Invalid QR code';
@override
String get channels_noFreeSlot => 'No free channel slot';
@override
String channels_qrAddConfirm(String name) {
return 'Add channel \"$name\"?';
@ -5669,6 +5666,10 @@ class AppLocalizationsDe extends AppLocalizations {
String get importConfig_reasonNoFreeSlot =>
'no free channel slot on the device';
@override
String get importConfig_reasonChannelsSyncing =>
'channels were still being read from the radio; import again in a moment';
@override
String get importConfig_reasonAbsentFromFile => 'not present in this file';

@ -4360,9 +4360,6 @@ class AppLocalizationsEn extends AppLocalizations {
@override
String get channels_invalidQr => 'Invalid QR code';
@override
String get channels_noFreeSlot => 'No free channel slot';
@override
String channels_qrAddConfirm(String name) {
return 'Add channel \"$name\"?';
@ -5575,6 +5572,10 @@ class AppLocalizationsEn extends AppLocalizations {
String get importConfig_reasonNoFreeSlot =>
'no free channel slot on the device';
@override
String get importConfig_reasonChannelsSyncing =>
'channels were still being read from the radio; import again in a moment';
@override
String get importConfig_reasonAbsentFromFile => 'not present in this file';

@ -4418,9 +4418,6 @@ class AppLocalizationsEs extends AppLocalizations {
@override
String get channels_invalidQr => 'Invalid QR code';
@override
String get channels_noFreeSlot => 'No free channel slot';
@override
String channels_qrAddConfirm(String name) {
return 'Add channel \"$name\"?';
@ -5657,6 +5654,10 @@ class AppLocalizationsEs extends AppLocalizations {
String get importConfig_reasonNoFreeSlot =>
'no free channel slot on the device';
@override
String get importConfig_reasonChannelsSyncing =>
'channels were still being read from the radio; import again in a moment';
@override
String get importConfig_reasonAbsentFromFile => 'not present in this file';

@ -4440,9 +4440,6 @@ class AppLocalizationsFr extends AppLocalizations {
@override
String get channels_invalidQr => 'Invalid QR code';
@override
String get channels_noFreeSlot => 'No free channel slot';
@override
String channels_qrAddConfirm(String name) {
return 'Add channel \"$name\"?';
@ -5685,6 +5682,10 @@ class AppLocalizationsFr extends AppLocalizations {
String get importConfig_reasonNoFreeSlot =>
'no free channel slot on the device';
@override
String get importConfig_reasonChannelsSyncing =>
'channels were still being read from the radio; import again in a moment';
@override
String get importConfig_reasonAbsentFromFile => 'not present in this file';

@ -4435,9 +4435,6 @@ class AppLocalizationsHu extends AppLocalizations {
@override
String get channels_invalidQr => 'Invalid QR code';
@override
String get channels_noFreeSlot => 'No free channel slot';
@override
String channels_qrAddConfirm(String name) {
return 'Add channel \"$name\"?';
@ -5674,6 +5671,10 @@ class AppLocalizationsHu extends AppLocalizations {
String get importConfig_reasonNoFreeSlot =>
'no free channel slot on the device';
@override
String get importConfig_reasonChannelsSyncing =>
'channels were still being read from the radio; import again in a moment';
@override
String get importConfig_reasonAbsentFromFile => 'not present in this file';

@ -4423,9 +4423,6 @@ class AppLocalizationsIt extends AppLocalizations {
@override
String get channels_invalidQr => 'Invalid QR code';
@override
String get channels_noFreeSlot => 'No free channel slot';
@override
String channels_qrAddConfirm(String name) {
return 'Add channel \"$name\"?';
@ -5662,6 +5659,10 @@ class AppLocalizationsIt extends AppLocalizations {
String get importConfig_reasonNoFreeSlot =>
'no free channel slot on the device';
@override
String get importConfig_reasonChannelsSyncing =>
'channels were still being read from the radio; import again in a moment';
@override
String get importConfig_reasonAbsentFromFile => 'not present in this file';

@ -4237,9 +4237,6 @@ class AppLocalizationsJa extends AppLocalizations {
@override
String get channels_invalidQr => 'Invalid QR code';
@override
String get channels_noFreeSlot => 'No free channel slot';
@override
String channels_qrAddConfirm(String name) {
return 'Add channel \"$name\"?';
@ -5428,6 +5425,10 @@ class AppLocalizationsJa extends AppLocalizations {
String get importConfig_reasonNoFreeSlot =>
'no free channel slot on the device';
@override
String get importConfig_reasonChannelsSyncing =>
'channels were still being read from the radio; import again in a moment';
@override
String get importConfig_reasonAbsentFromFile => 'not present in this file';

@ -4239,9 +4239,6 @@ class AppLocalizationsKo extends AppLocalizations {
@override
String get channels_invalidQr => 'Invalid QR code';
@override
String get channels_noFreeSlot => 'No free channel slot';
@override
String channels_qrAddConfirm(String name) {
return 'Add channel \"$name\"?';
@ -5429,6 +5426,10 @@ class AppLocalizationsKo extends AppLocalizations {
String get importConfig_reasonNoFreeSlot =>
'no free channel slot on the device';
@override
String get importConfig_reasonChannelsSyncing =>
'channels were still being read from the radio; import again in a moment';
@override
String get importConfig_reasonAbsentFromFile => 'not present in this file';

@ -4404,9 +4404,6 @@ class AppLocalizationsNl extends AppLocalizations {
@override
String get channels_invalidQr => 'Invalid QR code';
@override
String get channels_noFreeSlot => 'No free channel slot';
@override
String channels_qrAddConfirm(String name) {
return 'Add channel \"$name\"?';
@ -5637,6 +5634,10 @@ class AppLocalizationsNl extends AppLocalizations {
String get importConfig_reasonNoFreeSlot =>
'no free channel slot on the device';
@override
String get importConfig_reasonChannelsSyncing =>
'channels were still being read from the radio; import again in a moment';
@override
String get importConfig_reasonAbsentFromFile => 'not present in this file';

@ -4436,9 +4436,6 @@ class AppLocalizationsPl extends AppLocalizations {
@override
String get channels_invalidQr => 'Invalid QR code';
@override
String get channels_noFreeSlot => 'No free channel slot';
@override
String channels_qrAddConfirm(String name) {
return 'Add channel \"$name\"?';
@ -5674,6 +5671,10 @@ class AppLocalizationsPl extends AppLocalizations {
String get importConfig_reasonNoFreeSlot =>
'no free channel slot on the device';
@override
String get importConfig_reasonChannelsSyncing =>
'channels were still being read from the radio; import again in a moment';
@override
String get importConfig_reasonAbsentFromFile => 'not present in this file';

@ -4417,9 +4417,6 @@ class AppLocalizationsPt extends AppLocalizations {
@override
String get channels_invalidQr => 'Invalid QR code';
@override
String get channels_noFreeSlot => 'No free channel slot';
@override
String channels_qrAddConfirm(String name) {
return 'Add channel \"$name\"?';
@ -5650,6 +5647,10 @@ class AppLocalizationsPt extends AppLocalizations {
String get importConfig_reasonNoFreeSlot =>
'no free channel slot on the device';
@override
String get importConfig_reasonChannelsSyncing =>
'channels were still being read from the radio; import again in a moment';
@override
String get importConfig_reasonAbsentFromFile => 'not present in this file';

@ -4425,9 +4425,6 @@ class AppLocalizationsRu extends AppLocalizations {
@override
String get channels_invalidQr => 'Invalid QR code';
@override
String get channels_noFreeSlot => 'No free channel slot';
@override
String channels_qrAddConfirm(String name) {
return 'Add channel \"$name\"?';
@ -5668,6 +5665,10 @@ class AppLocalizationsRu extends AppLocalizations {
String get importConfig_reasonNoFreeSlot =>
'no free channel slot on the device';
@override
String get importConfig_reasonChannelsSyncing =>
'channels were still being read from the radio; import again in a moment';
@override
String get importConfig_reasonAbsentFromFile => 'not present in this file';

@ -4404,9 +4404,6 @@ class AppLocalizationsSk extends AppLocalizations {
@override
String get channels_invalidQr => 'Invalid QR code';
@override
String get channels_noFreeSlot => 'No free channel slot';
@override
String channels_qrAddConfirm(String name) {
return 'Add channel \"$name\"?';
@ -5634,6 +5631,10 @@ class AppLocalizationsSk extends AppLocalizations {
String get importConfig_reasonNoFreeSlot =>
'no free channel slot on the device';
@override
String get importConfig_reasonChannelsSyncing =>
'channels were still being read from the radio; import again in a moment';
@override
String get importConfig_reasonAbsentFromFile => 'not present in this file';

@ -4399,9 +4399,6 @@ class AppLocalizationsSl extends AppLocalizations {
@override
String get channels_invalidQr => 'Invalid QR code';
@override
String get channels_noFreeSlot => 'No free channel slot';
@override
String channels_qrAddConfirm(String name) {
return 'Add channel \"$name\"?';
@ -5632,6 +5629,10 @@ class AppLocalizationsSl extends AppLocalizations {
String get importConfig_reasonNoFreeSlot =>
'no free channel slot on the device';
@override
String get importConfig_reasonChannelsSyncing =>
'channels were still being read from the radio; import again in a moment';
@override
String get importConfig_reasonAbsentFromFile => 'not present in this file';

@ -4379,9 +4379,6 @@ class AppLocalizationsSv extends AppLocalizations {
@override
String get channels_invalidQr => 'Invalid QR code';
@override
String get channels_noFreeSlot => 'No free channel slot';
@override
String channels_qrAddConfirm(String name) {
return 'Add channel \"$name\"?';
@ -5607,6 +5604,10 @@ class AppLocalizationsSv extends AppLocalizations {
String get importConfig_reasonNoFreeSlot =>
'no free channel slot on the device';
@override
String get importConfig_reasonChannelsSyncing =>
'channels were still being read from the radio; import again in a moment';
@override
String get importConfig_reasonAbsentFromFile => 'not present in this file';

@ -4421,9 +4421,6 @@ class AppLocalizationsUk extends AppLocalizations {
@override
String get channels_invalidQr => 'Invalid QR code';
@override
String get channels_noFreeSlot => 'No free channel slot';
@override
String channels_qrAddConfirm(String name) {
return 'Add channel \"$name\"?';
@ -5669,6 +5666,10 @@ class AppLocalizationsUk extends AppLocalizations {
String get importConfig_reasonNoFreeSlot =>
'no free channel slot on the device';
@override
String get importConfig_reasonChannelsSyncing =>
'channels were still being read from the radio; import again in a moment';
@override
String get importConfig_reasonAbsentFromFile => 'not present in this file';

@ -4142,9 +4142,6 @@ class AppLocalizationsZh extends AppLocalizations {
@override
String get channels_invalidQr => 'Invalid QR code';
@override
String get channels_noFreeSlot => 'No free channel slot';
@override
String channels_qrAddConfirm(String name) {
return 'Add channel \"$name\"?';
@ -5302,6 +5299,10 @@ class AppLocalizationsZh extends AppLocalizations {
String get importConfig_reasonNoFreeSlot =>
'no free channel slot on the device';
@override
String get importConfig_reasonChannelsSyncing =>
'channels were still being read from the radio; import again in a moment';
@override
String get importConfig_reasonAbsentFromFile => 'not present in this file';

@ -30,7 +30,7 @@ import '../widgets/channel_notify_mode.dart';
import '../widgets/radio_channel_notify.dart';
import '../widgets/radio_tone.dart';
import '../widgets/battery_optimization_banner.dart';
import '../helpers/channel_slot_decision.dart';
import '../helpers/channel_slot_ui.dart';
import '../helpers/snack_bar_builder.dart';
import 'channel_chat_screen.dart';
import 'community_qr_scanner_screen.dart';
@ -1878,7 +1878,7 @@ class _ChannelsScreenState extends State<ChannelsScreen>
// (`Channel.isPublicChannel`), not by its index, so it has no claim on
// slot 0 and can live anywhere.
if (connector.channels.any((c) => c.isPublicChannel)) {
_showChannelBlockedMessage(
showChannelBlockedMessage(
context,
context.l10n.channels_publicAlreadyAdded,
);
@ -1894,54 +1894,14 @@ class _ChannelsScreenState extends State<ChannelsScreen>
);
}
/// A slot that is safe to write right now, or null after explaining why not.
///
/// Call this at the moment of the write and never earlier. The connector
/// empties `channels` for the duration of a channel sync, so a slot chosen
/// when a dialog opened can look free when it is not; that is what lost
/// Public in #1021.
///
/// The decision itself lives in [decideChannelSlot] so it can be tested
/// without a widget and shared with the QR and config-import paths. This
/// wrapper only turns a refusal into something the user can read.
///
/// The owner's ruling is that the unsafe write is blocked but the control
/// stays live: a greyed-out button prevents the damage and tells the user
/// nothing. So both refusals return null *and* say what is wrong.
int? _allocateChannelSlot(BuildContext context, MeshCoreConnector connector) {
final decision = decideChannelSlot(
channels: connector.channels,
maxChannels: connector.maxChannels,
// isSyncingChannels, not hasLoadedChannels: the latter stays false after
// a FAILED sync while the cached list is restored, so gating on it would
// make adding a channel impossible on a flaky link.
isSyncing: connector.isSyncingChannels,
);
switch (decision.refusal) {
case ChannelSlotRefusal.syncing:
_showChannelBlockedMessage(
context,
context.l10n.channels_slotBusySyncing,
);
return null;
case ChannelSlotRefusal.full:
_showChannelBlockedMessage(
context,
context.l10n.channels_slotsAllFull(connector.maxChannels),
);
return null;
case null:
return decision.slot;
}
}
/// A refusal the user has to be able to read and act on, so it persists.
///
/// `showDismissibleSnackBar` defaults to `persist: false` and a 4 second
/// toast, which SAFELANE 6 rules out for an error.
void _showChannelBlockedMessage(BuildContext context, String message) {
showDismissibleSnackBar(context, content: Text(message), persist: true);
}
/// Thin alias for the shared allocator, kept so the five call sites in this
/// file read the same as before. The logic lives in
/// `helpers/channel_slot_ui.dart` so this screen, the QR scanner and config
/// import all answer "which slot is free?" the same way (#1021).
int? _allocateChannelSlot(
BuildContext context,
MeshCoreConnector connector,
) => allocateChannelSlotOrExplain(context, connector);
void _showManageCommunitiesDialog(BuildContext context) {
showModalBottomSheet(

@ -9,6 +9,7 @@ import '../models/community.dart';
import '../storage/community_store.dart';
import '../widgets/adaptive_app_bar_title.dart';
import '../widgets/qr_scanner_widget.dart';
import '../helpers/channel_slot_ui.dart';
import '../helpers/snack_bar_builder.dart';
/// Screen for scanning community QR codes to join communities.
@ -224,24 +225,37 @@ class _CommunityQrScannerScreenState extends State<CommunityQrScannerScreen> {
_communityStore.setPublicKeyHex = connector.selfPublicKeyHex;
await _communityStore.addCommunity(community);
// Optionally add the community public channel to the device
// Optionally add the community public channel to the device. The
// community is already saved above, so a refused slot only skips the
// channel; it never loses the community.
//
// This used to skip the channel silently when the radio was full and then
// show a green "joined" toast, which told the user everything had worked
// when the channel had not been added (#1021). A refusal now explains
// itself, and only a real success shows the green confirmation.
var channelRefused = false;
if (addPublicChannel && context.mounted) {
final connector = context.read<MeshCoreConnector>();
final nextIndex = _findNextAvailableChannelIndex(connector);
if (nextIndex != null) {
final slot = allocateChannelSlotOrExplain(context, connector);
if (slot == null) {
channelRefused = true;
} else {
final psk = community.deriveCommunityPublicPsk();
final channelName = '${community.name} Public';
connector.setChannel(nextIndex, channelName, psk);
connector.setChannel(slot, channelName, psk);
}
}
if (context.mounted) {
showDismissibleSnackBar(
context,
content: Text(context.l10n.community_joined(community.name)),
backgroundColor: Colors.green,
);
// The refusal message is already showing and persists; a green toast
// on top of it would contradict it.
if (!channelRefused) {
showDismissibleSnackBar(
context,
content: Text(context.l10n.community_joined(community.name)),
backgroundColor: Colors.green,
);
}
// Return to previous screen
Navigator.pop(context, community);
@ -271,17 +285,11 @@ class _CommunityQrScannerScreenState extends State<CommunityQrScannerScreen> {
}
return;
}
final nextIndex = _findNextAvailableChannelIndex(connector);
if (nextIndex == null) {
if (context.mounted) {
showDismissibleSnackBar(
context,
content: Text(context.l10n.channels_noFreeSlot),
backgroundColor: Colors.red,
);
}
return;
}
// No slot is chosen here. It used to be, and that is the same bug the
// channels screen had: the index was picked before the confirmation
// dialog below and written after it, so a channel sync during the
// confirmation (setChannel itself starts one) could leave it pointing at
// an occupied slot. The slot is now chosen after the user confirms.
if (!context.mounted) return;
final confirmed = await showDialog<bool>(
context: context,
@ -301,7 +309,11 @@ class _CommunityQrScannerScreenState extends State<CommunityQrScannerScreen> {
),
);
if (confirmed == true && context.mounted) {
connector.setChannel(nextIndex, channel.name, channel.psk);
final slot = allocateChannelSlotOrExplain(context, connector);
// Refused: the persistent message explains why. Stay on this screen so
// the user can retry, rather than popping away from the explanation.
if (slot == null) return;
connector.setChannel(slot, channel.name, channel.psk);
showDismissibleSnackBar(
context,
content: Text(context.l10n.channels_qrAdded(channel.name)),
@ -312,12 +324,4 @@ class _CommunityQrScannerScreenState extends State<CommunityQrScannerScreen> {
Navigator.pop(context);
}
}
int? _findNextAvailableChannelIndex(MeshCoreConnector connector) {
final usedIndices = connector.channels.map((c) => c.index).toSet();
for (int i = 0; i < connector.maxChannels; i++) {
if (!usedIndices.contains(i)) return i;
}
return null;
}
}

@ -323,6 +323,8 @@ String _reasonText(BuildContext context, StockConfigImportIssue reason) {
return l10n.importConfig_reasonAlreadyPresent;
case StockConfigImportIssue.noFreeSlot:
return l10n.importConfig_reasonNoFreeSlot;
case StockConfigImportIssue.channelsSyncing:
return l10n.importConfig_reasonChannelsSyncing;
case StockConfigImportIssue.notWritable:
return l10n.importConfig_reasonNotWritable;
}

@ -47,6 +47,10 @@ enum StockConfigImportIssue {
/// Every channel slot on the device is occupied.
noFreeSlot,
/// The channel list was being re-read from the radio, so no slot could be
/// trusted to be free. Importing again once it finishes will work (#1021).
channelsSyncing,
/// We have no supported way to write this value to the device.
notWritable,
}
@ -274,6 +278,7 @@ class StockConfigImportService {
existing: _connector.channels,
incoming: incoming,
maxChannels: _connector.maxChannels,
isSyncing: _connector.isSyncingChannels,
);
for (final assignment in plan.assignments) {
await _connector.setChannel(
@ -357,11 +362,25 @@ class ChannelImportPlan {
/// New channels go into the lowest free slot. Stock's file has no index field,
/// so incoming order is the only ordering information there is, and slots
/// already occupied are left alone.
///
/// While [isSyncing] is true nothing is planned. The connector empties its
/// channel list for the duration of a sync, so [existing] would look empty and
/// every incoming channel would be written over slots that are in fact
/// occupied, Public included (#1021). Each channel is reported as skipped
/// instead, so the user sees why and can import again. [isSyncing] is required
/// so a caller cannot forget it and get the unsafe behavior by default.
ChannelImportPlan planChannelImport({
required List<Channel> existing,
required List<StockChannel> incoming,
required int maxChannels,
required bool isSyncing,
}) {
if (isSyncing) {
return ChannelImportPlan(const [], [
for (final channel in incoming)
SkippedChannel(channel.name, StockConfigImportIssue.channelsSyncing),
]);
}
final live = existing.where((c) => !c.isEmpty).toList();
final takenSlots = live.map((c) => c.index).toSet();
final psks = live.map((c) => c.pskHex).toSet();

@ -28,6 +28,7 @@ void main() {
existing: [live(0, 'Public', 0x11)],
incoming: [incoming('Weather', 0x22)],
maxChannels: 40,
isSyncing: false,
);
expect(plan.assignments, hasLength(1));
@ -41,6 +42,7 @@ void main() {
existing: [live(0, 'Public', 0x11)],
incoming: [incoming('Renamed', 0x11)],
maxChannels: 40,
isSyncing: false,
);
expect(plan.assignments, isEmpty);
@ -55,6 +57,7 @@ void main() {
existing: [live(0, 'Public', 0x11)],
incoming: [incoming('Public', 0x99)],
maxChannels: 40,
isSyncing: false,
);
expect(plan.assignments, isEmpty);
@ -72,6 +75,7 @@ void main() {
],
incoming: [incoming('Emergency', 0x33)],
maxChannels: 40,
isSyncing: false,
);
expect(plan.assignments.single.slot, 1);
@ -86,6 +90,7 @@ void main() {
incoming('Third', 0x44),
],
maxChannels: 40,
isSyncing: false,
);
expect(plan.assignments.map((a) => a.slot), [1, 2, 3]);
@ -101,6 +106,7 @@ void main() {
existing: [live(0, 'A', 0x11), live(1, 'B', 0x22)],
incoming: [incoming('C', 0x33), incoming('D', 0x44)],
maxChannels: 2,
isSyncing: false,
);
expect(plan.assignments, isEmpty);
@ -116,6 +122,7 @@ void main() {
existing: const [],
incoming: [incoming('Dupe', 0x55), incoming('Dupe', 0x55)],
maxChannels: 40,
isSyncing: false,
);
expect(plan.assignments, hasLength(1));
@ -127,11 +134,48 @@ void main() {
existing: [live(0, 'Public', 0x11)],
incoming: const [],
maxChannels: 40,
isSyncing: false,
);
expect(plan.assignments, isEmpty);
expect(plan.skipped, isEmpty);
});
// #1021: the connector empties its channel list for the length of a sync,
// so mid-sync `existing` looks empty. Planning then would write every
// incoming channel over slots that are really occupied, starting at 0.
test('mid-sync nothing is written, even into slots that look free', () {
final plan = planChannelImport(
existing: const [],
incoming: [incoming('Weather', 0x22), incoming('Emergency', 0x33)],
maxChannels: 40,
isSyncing: true,
);
expect(plan.assignments, isEmpty);
expect(plan.skipped.map((s) => s.name), ['Weather', 'Emergency']);
expect(
plan.skipped.map((s) => s.reason),
everyElement(StockConfigImportIssue.channelsSyncing),
);
});
test('mid-sync every channel is reported as syncing, not as present', () {
// A partial list cannot prove a channel is already there either, so
// "already present" would be a guess. Syncing is the true reason.
final plan = planChannelImport(
existing: [live(0, 'Public', 0x11)],
incoming: [incoming('Public', 0x11)],
maxChannels: 40,
isSyncing: true,
);
expect(plan.assignments, isEmpty);
expect(
plan.skipped.single.reason,
StockConfigImportIssue.channelsSyncing,
);
});
});
group('import result', () {

@ -213,7 +213,6 @@
"channels_scanQrDesc",
"channels_scanQrInstructions",
"channels_invalidQr",
"channels_noFreeSlot",
"channels_qrAddConfirm",
"channels_qrAdded",
"channels_qrExists",
@ -321,6 +320,7 @@
"importConfig_resultFailedHeading",
"importConfig_reasonAlreadyPresent",
"importConfig_reasonNoFreeSlot",
"importConfig_reasonChannelsSyncing",
"importConfig_reasonAbsentFromFile",
"importConfig_reasonNoReply",
"importConfig_reasonUnsupported",
@ -586,7 +586,6 @@
"channels_scanQrDesc",
"channels_scanQrInstructions",
"channels_invalidQr",
"channels_noFreeSlot",
"channels_qrAddConfirm",
"channels_qrAdded",
"channels_qrExists",
@ -694,6 +693,7 @@
"importConfig_resultFailedHeading",
"importConfig_reasonAlreadyPresent",
"importConfig_reasonNoFreeSlot",
"importConfig_reasonChannelsSyncing",
"importConfig_reasonAbsentFromFile",
"importConfig_reasonNoReply",
"importConfig_reasonUnsupported",
@ -959,7 +959,6 @@
"channels_scanQrDesc",
"channels_scanQrInstructions",
"channels_invalidQr",
"channels_noFreeSlot",
"channels_qrAddConfirm",
"channels_qrAdded",
"channels_qrExists",
@ -1067,6 +1066,7 @@
"importConfig_resultFailedHeading",
"importConfig_reasonAlreadyPresent",
"importConfig_reasonNoFreeSlot",
"importConfig_reasonChannelsSyncing",
"importConfig_reasonAbsentFromFile",
"importConfig_reasonNoReply",
"importConfig_reasonUnsupported",
@ -1332,7 +1332,6 @@
"channels_scanQrDesc",
"channels_scanQrInstructions",
"channels_invalidQr",
"channels_noFreeSlot",
"channels_qrAddConfirm",
"channels_qrAdded",
"channels_qrExists",
@ -1440,6 +1439,7 @@
"importConfig_resultFailedHeading",
"importConfig_reasonAlreadyPresent",
"importConfig_reasonNoFreeSlot",
"importConfig_reasonChannelsSyncing",
"importConfig_reasonAbsentFromFile",
"importConfig_reasonNoReply",
"importConfig_reasonUnsupported",
@ -1705,7 +1705,6 @@
"channels_scanQrDesc",
"channels_scanQrInstructions",
"channels_invalidQr",
"channels_noFreeSlot",
"channels_qrAddConfirm",
"channels_qrAdded",
"channels_qrExists",
@ -1813,6 +1812,7 @@
"importConfig_resultFailedHeading",
"importConfig_reasonAlreadyPresent",
"importConfig_reasonNoFreeSlot",
"importConfig_reasonChannelsSyncing",
"importConfig_reasonAbsentFromFile",
"importConfig_reasonNoReply",
"importConfig_reasonUnsupported",
@ -2078,7 +2078,6 @@
"channels_scanQrDesc",
"channels_scanQrInstructions",
"channels_invalidQr",
"channels_noFreeSlot",
"channels_qrAddConfirm",
"channels_qrAdded",
"channels_qrExists",
@ -2186,6 +2185,7 @@
"importConfig_resultFailedHeading",
"importConfig_reasonAlreadyPresent",
"importConfig_reasonNoFreeSlot",
"importConfig_reasonChannelsSyncing",
"importConfig_reasonAbsentFromFile",
"importConfig_reasonNoReply",
"importConfig_reasonUnsupported",
@ -2451,7 +2451,6 @@
"channels_scanQrDesc",
"channels_scanQrInstructions",
"channels_invalidQr",
"channels_noFreeSlot",
"channels_qrAddConfirm",
"channels_qrAdded",
"channels_qrExists",
@ -2559,6 +2558,7 @@
"importConfig_resultFailedHeading",
"importConfig_reasonAlreadyPresent",
"importConfig_reasonNoFreeSlot",
"importConfig_reasonChannelsSyncing",
"importConfig_reasonAbsentFromFile",
"importConfig_reasonNoReply",
"importConfig_reasonUnsupported",
@ -2824,7 +2824,6 @@
"channels_scanQrDesc",
"channels_scanQrInstructions",
"channels_invalidQr",
"channels_noFreeSlot",
"channels_qrAddConfirm",
"channels_qrAdded",
"channels_qrExists",
@ -2932,6 +2931,7 @@
"importConfig_resultFailedHeading",
"importConfig_reasonAlreadyPresent",
"importConfig_reasonNoFreeSlot",
"importConfig_reasonChannelsSyncing",
"importConfig_reasonAbsentFromFile",
"importConfig_reasonNoReply",
"importConfig_reasonUnsupported",
@ -3197,7 +3197,6 @@
"channels_scanQrDesc",
"channels_scanQrInstructions",
"channels_invalidQr",
"channels_noFreeSlot",
"channels_qrAddConfirm",
"channels_qrAdded",
"channels_qrExists",
@ -3305,6 +3304,7 @@
"importConfig_resultFailedHeading",
"importConfig_reasonAlreadyPresent",
"importConfig_reasonNoFreeSlot",
"importConfig_reasonChannelsSyncing",
"importConfig_reasonAbsentFromFile",
"importConfig_reasonNoReply",
"importConfig_reasonUnsupported",
@ -3570,7 +3570,6 @@
"channels_scanQrDesc",
"channels_scanQrInstructions",
"channels_invalidQr",
"channels_noFreeSlot",
"channels_qrAddConfirm",
"channels_qrAdded",
"channels_qrExists",
@ -3678,6 +3677,7 @@
"importConfig_resultFailedHeading",
"importConfig_reasonAlreadyPresent",
"importConfig_reasonNoFreeSlot",
"importConfig_reasonChannelsSyncing",
"importConfig_reasonAbsentFromFile",
"importConfig_reasonNoReply",
"importConfig_reasonUnsupported",
@ -3943,7 +3943,6 @@
"channels_scanQrDesc",
"channels_scanQrInstructions",
"channels_invalidQr",
"channels_noFreeSlot",
"channels_qrAddConfirm",
"channels_qrAdded",
"channels_qrExists",
@ -4051,6 +4050,7 @@
"importConfig_resultFailedHeading",
"importConfig_reasonAlreadyPresent",
"importConfig_reasonNoFreeSlot",
"importConfig_reasonChannelsSyncing",
"importConfig_reasonAbsentFromFile",
"importConfig_reasonNoReply",
"importConfig_reasonUnsupported",
@ -4316,7 +4316,6 @@
"channels_scanQrDesc",
"channels_scanQrInstructions",
"channels_invalidQr",
"channels_noFreeSlot",
"channels_qrAddConfirm",
"channels_qrAdded",
"channels_qrExists",
@ -4424,6 +4423,7 @@
"importConfig_resultFailedHeading",
"importConfig_reasonAlreadyPresent",
"importConfig_reasonNoFreeSlot",
"importConfig_reasonChannelsSyncing",
"importConfig_reasonAbsentFromFile",
"importConfig_reasonNoReply",
"importConfig_reasonUnsupported",
@ -4689,7 +4689,6 @@
"channels_scanQrDesc",
"channels_scanQrInstructions",
"channels_invalidQr",
"channels_noFreeSlot",
"channels_qrAddConfirm",
"channels_qrAdded",
"channels_qrExists",
@ -4797,6 +4796,7 @@
"importConfig_resultFailedHeading",
"importConfig_reasonAlreadyPresent",
"importConfig_reasonNoFreeSlot",
"importConfig_reasonChannelsSyncing",
"importConfig_reasonAbsentFromFile",
"importConfig_reasonNoReply",
"importConfig_reasonUnsupported",
@ -5062,7 +5062,6 @@
"channels_scanQrDesc",
"channels_scanQrInstructions",
"channels_invalidQr",
"channels_noFreeSlot",
"channels_qrAddConfirm",
"channels_qrAdded",
"channels_qrExists",
@ -5170,6 +5169,7 @@
"importConfig_resultFailedHeading",
"importConfig_reasonAlreadyPresent",
"importConfig_reasonNoFreeSlot",
"importConfig_reasonChannelsSyncing",
"importConfig_reasonAbsentFromFile",
"importConfig_reasonNoReply",
"importConfig_reasonUnsupported",
@ -5435,7 +5435,6 @@
"channels_scanQrDesc",
"channels_scanQrInstructions",
"channels_invalidQr",
"channels_noFreeSlot",
"channels_qrAddConfirm",
"channels_qrAdded",
"channels_qrExists",
@ -5543,6 +5542,7 @@
"importConfig_resultFailedHeading",
"importConfig_reasonAlreadyPresent",
"importConfig_reasonNoFreeSlot",
"importConfig_reasonChannelsSyncing",
"importConfig_reasonAbsentFromFile",
"importConfig_reasonNoReply",
"importConfig_reasonUnsupported",
@ -5808,7 +5808,6 @@
"channels_scanQrDesc",
"channels_scanQrInstructions",
"channels_invalidQr",
"channels_noFreeSlot",
"channels_qrAddConfirm",
"channels_qrAdded",
"channels_qrExists",
@ -5916,6 +5915,7 @@
"importConfig_resultFailedHeading",
"importConfig_reasonAlreadyPresent",
"importConfig_reasonNoFreeSlot",
"importConfig_reasonChannelsSyncing",
"importConfig_reasonAbsentFromFile",
"importConfig_reasonNoReply",
"importConfig_reasonUnsupported",
@ -6181,7 +6181,6 @@
"channels_scanQrDesc",
"channels_scanQrInstructions",
"channels_invalidQr",
"channels_noFreeSlot",
"channels_qrAddConfirm",
"channels_qrAdded",
"channels_qrExists",
@ -6289,6 +6288,7 @@
"importConfig_resultFailedHeading",
"importConfig_reasonAlreadyPresent",
"importConfig_reasonNoFreeSlot",
"importConfig_reasonChannelsSyncing",
"importConfig_reasonAbsentFromFile",
"importConfig_reasonNoReply",
"importConfig_reasonUnsupported",

Loading…
Cancel
Save

Powered by TurnKey Linux.