Merge remote-tracking branch 'origin/dev' into self-telemetry

This commit is contained in:
zjs81 committed 2026-09-27 12:08:23 -07:00
commit 052a45e3cd
237 files changed
+51244 -4960

No files matched your search

+219 -68
View File
@@ -1,5 +1,6 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:meshcore_open/utils/gpx_export.dart';
import 'package:meshcore_open/widgets/elements_ui.dart';
import 'package:provider/provider.dart';
@@ -29,15 +30,24 @@ int _toUiCodingRate(int deviceCr) {
return deviceCr <= 4 ? deviceCr + 4 : deviceCr;
}
/// Convert UI coding-rate value (5-8) back to firmware encoding.
/// Uses the current device CR to detect which encoding the firmware expects.
int _toDeviceCodingRate(int uiCr, int? deviceCr) {
if (deviceCr != null && deviceCr <= 4) {
return uiCr - 4;
const Set<int> _defaultRepeatFreqsKHz = {433000, 869495, 918000};
/// Whether the firmware accepts [freqKHz] for off-grid client repeat, using
/// the ranges from CMD_GET_ALLOWED_REPEAT_FREQ when known.
bool _isAllowedRepeatFreqKHz(MeshCoreConnector connector, int freqKHz) {
final ranges = connector.allowedRepeatFreqRanges;
if (ranges == null || ranges.isEmpty) {
return _defaultRepeatFreqsKHz.contains(freqKHz);
}
return uiCr;
return ranges.any((r) => freqKHz >= r.lowKHz && freqKHz <= r.highKHz);
}
/// Companion firmware version that added CMD_SET_PATH_HASH_MODE (v1.14.0).
const int _minFirmwareVerCodeForPathHashMode = 10;
/// Lowest TX power companion firmware accepts (CMD_SET_RADIO_TX_POWER).
const int _minTxPowerDbm = -9;
class SettingsScreen extends StatefulWidget {
const SettingsScreen({super.key});
@@ -281,6 +291,18 @@ class _SettingsScreenState extends State<SettingsScreen> {
value: connector.deviceIdLabel,
),
_buildBatteryInfoRow(context, connector),
if (connector.manufacturerName != null)
_infoRow(
context,
label: l10n.settings_infoHardware,
value: connector.manufacturerName!,
),
if (connector.firmwareVersionString != null)
_infoRow(
context,
label: l10n.settings_infoFirmware,
value: connector.firmwareVersionString!,
),
if (connector.selfName != null)
_infoRow(
context,
@@ -288,12 +310,10 @@ class _SettingsScreenState extends State<SettingsScreen> {
value: connector.selfName!,
),
if (connector.selfPublicKey != null)
_infoRow(
_publicKeyRow(
context,
label: l10n.settings_infoPublicKey,
value:
'${pubKeyToHex(connector.selfPublicKey!).substring(0, 16)}...',
mono: true,
publicKeyHex: pubKeyToHex(connector.selfPublicKey!),
),
_infoRow(
context,
@@ -314,6 +334,31 @@ class _SettingsScreenState extends State<SettingsScreen> {
);
}
/// Copies the key rendered in the row, not whatever the connector holds when
/// the tap lands: a disconnect in between clears `selfPublicKey`.
Widget _publicKeyRow(
BuildContext context, {
required String label,
required String publicKeyHex,
}) {
return _infoRow(
context,
label: label,
value: publicKeyHex,
mono: true,
onTap: () => _copyPublicKey(context, publicKeyHex),
);
}
Future<void> _copyPublicKey(BuildContext context, String publicKeyHex) async {
await Clipboard.setData(ClipboardData(text: publicKeyHex));
if (!context.mounted) return;
showDismissibleSnackBar(
context,
content: Text(context.l10n.settings_publicKeyCopied),
);
}
Widget _infoRow(
BuildContext context, {
required String label,
@@ -464,7 +509,10 @@ class _SettingsScreenState extends State<SettingsScreen> {
context,
icon: Icons.sensors_outlined,
title: l10n.radioStats_settingsTile,
subtitle: l10n.radioStats_settingsSubtitle,
subtitle:
connector.isConnected && !connector.supportsCompanionRadioStats
? l10n.settings_requiresFirmware('v1.10')
: l10n.radioStats_settingsSubtitle,
onTap: connector.isConnected && connector.supportsCompanionRadioStats
? () => pushCompanionRadioStatsScreen(context)
: null,
@@ -484,8 +532,16 @@ class _SettingsScreenState extends State<SettingsScreen> {
context,
icon: Icons.route_outlined,
title: l10n.repeater_pathHashMode,
subtitle: _pathHashModeSubtitle(context, connector.pathHashByteWidth),
onTap: connector.isConnected
subtitle:
connector.isConnected &&
(connector.firmwareVerCode ?? 0) <
_minFirmwareVerCodeForPathHashMode
? l10n.settings_requiresFirmware('v1.14')
: _pathHashModeSubtitle(context, connector.pathHashByteWidth),
onTap:
connector.isConnected &&
(connector.firmwareVerCode ?? 0) >=
_minFirmwareVerCodeForPathHashMode
? () => _editPathHashMode(context, connector)
: null,
),
@@ -793,12 +849,12 @@ class _SettingsScreenState extends State<SettingsScreen> {
void _editPathHashMode(BuildContext context, MeshCoreConnector connector) {
final l10n = context.l10n;
var selectedMode = (connector.pathHashByteWidth - 1).clamp(0, 3).toInt();
var selectedMode = (connector.pathHashByteWidth - 1).clamp(0, 2).toInt();
showDialog(
context: context,
builder: (dialogContext) => StatefulBuilder(
builder: (context, setDialogState) => AlertDialog(
builder: (builderContext, setDialogState) => AlertDialog(
title: Text(l10n.repeater_pathHashMode),
content: Column(
mainAxisSize: MainAxisSize.min,
@@ -823,10 +879,6 @@ class _SettingsScreenState extends State<SettingsScreen> {
value: 2,
child: Text(l10n.repeater_pathHashModeOption2),
),
DropdownMenuItem(
value: 3,
child: Text(l10n.repeater_pathHashModeOption3),
),
],
onChanged: (value) {
if (value == null) return;
@@ -835,8 +887,8 @@ class _SettingsScreenState extends State<SettingsScreen> {
),
const SizedBox(height: 12),
Text(
l10n.repeater_pathHashModeHelper,
style: Theme.of(context).textTheme.bodySmall,
l10n.settings_pathHashModeHelper,
style: Theme.of(builderContext).textTheme.bodySmall,
),
],
),
@@ -896,7 +948,7 @@ class _SettingsScreenState extends State<SettingsScreen> {
showDialog(
context: context,
builder: (dialogContext) => StatefulBuilder(
builder: (context, setDialogState) => AlertDialog(
builder: (builderContext, setDialogState) => AlertDialog(
title: Text(l10n.settings_location),
content: Column(
mainAxisSize: MainAxisSize.min,
@@ -962,7 +1014,7 @@ class _SettingsScreenState extends State<SettingsScreen> {
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
onPressed: () => Navigator.pop(dialogContext),
child: Text(l10n.common_cancel),
),
TextButton(
@@ -983,7 +1035,7 @@ class _SettingsScreenState extends State<SettingsScreen> {
}
}
Navigator.pop(context);
Navigator.pop(dialogContext);
if (interval != null) {
await settingsService.setGpsIntervalSeconds(
@@ -1301,21 +1353,37 @@ void _privacySettings(BuildContext context, MeshCoreConnector connector) {
settingsService.settings.autoSendZeroHopAdvertOnGpsUpdate;
final telemModeBase = [
DropdownMenuItem(value: teleModeDeny, child: Text(l10n.settings_denyAll)),
DropdownMenuItem(
value: teleModeDeny,
child: Text(
l10n.settings_denyAll,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
DropdownMenuItem(
value: teleModeAllowFlags,
child: Text(l10n.settings_allowByContact),
child: Text(
l10n.settings_allowByContact,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
DropdownMenuItem(
value: teleModeAllowAll,
child: Text(l10n.settings_allowAll),
child: Text(
l10n.settings_allowAll,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
];
showDialog(
context: context,
builder: (dialogContext) => StatefulBuilder(
builder: (context, setDialogState) => AlertDialog(
builder: (builderContext, setDialogState) => AlertDialog(
insetPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 24),
title: Text(l10n.settings_privacy),
content: SingleChildScrollView(
child: Column(
@@ -1349,6 +1417,7 @@ void _privacySettings(BuildContext context, MeshCoreConnector connector) {
const SizedBox(height: 8),
SwitchListTile(
title: Text(l10n.settings_multiAck),
subtitle: Text(l10n.settings_multiAckSubtitle),
value: multiAcks == 1,
onChanged: (value) {
setDialogState(() => multiAcks = value ? 1 : 0);
@@ -1358,6 +1427,7 @@ void _privacySettings(BuildContext context, MeshCoreConnector connector) {
const SizedBox(height: 16),
DropdownButtonFormField<int>(
initialValue: telemetryMode,
isExpanded: true,
decoration: InputDecoration(
labelText: l10n.settings_telemetryBaseMode,
border: const OutlineInputBorder(),
@@ -1372,6 +1442,7 @@ void _privacySettings(BuildContext context, MeshCoreConnector connector) {
const SizedBox(height: 16),
DropdownButtonFormField<int>(
initialValue: telemetryLocMode,
isExpanded: true,
decoration: InputDecoration(
labelText: l10n.settings_telemetryLocationMode,
border: const OutlineInputBorder(),
@@ -1386,6 +1457,7 @@ void _privacySettings(BuildContext context, MeshCoreConnector connector) {
const SizedBox(height: 16),
DropdownButtonFormField<int>(
initialValue: telemetryEnvMode,
isExpanded: true,
decoration: InputDecoration(
labelText: l10n.settings_telemetryEnvironmentMode,
border: const OutlineInputBorder(),
@@ -1397,17 +1469,22 @@ void _privacySettings(BuildContext context, MeshCoreConnector connector) {
}
},
),
const SizedBox(height: 8),
Text(
l10n.settings_telemetryPerContactHint,
style: Theme.of(context).textTheme.bodySmall,
),
],
),
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
onPressed: () => Navigator.pop(dialogContext),
child: Text(l10n.common_cancel),
),
TextButton(
onPressed: () async {
Navigator.pop(context);
Navigator.pop(dialogContext);
await connector.setTelemetryModeBase(
telemetryMode,
telemetryLocMode,
@@ -1550,8 +1627,7 @@ class _RadioSettingsDialogState extends State<_RadioSettingsDialog> {
if (preset.frequencyHz == snapshot.frequencyHz &&
preset.bandwidth == snapshot.bandwidth &&
preset.spreadingFactor == snapshot.spreadingFactor &&
preset.codingRate == snapshot.codingRate &&
preset.txPowerDbm == snapshot.txPowerDbm) {
preset.codingRate == snapshot.codingRate) {
return i;
}
}
@@ -1585,9 +1661,15 @@ class _RadioSettingsDialogState extends State<_RadioSettingsDialog> {
}
double _offGridFrequencyForBaseFrequency(double baseFrequencyMHz) {
if (baseFrequencyMHz < 500) return 433.0;
if (baseFrequencyMHz < 900) return 869.0;
return 918.0;
final baseKHz = (baseFrequencyMHz * 1000).round();
final ranges = widget.connector.allowedRepeatFreqRanges;
final candidates = ranges == null || ranges.isEmpty
? _defaultRepeatFreqsKHz
: {for (final r in ranges) baseKHz.clamp(r.lowKHz, r.highKHz)};
final closestKHz = candidates.reduce(
(a, b) => (a - baseKHz).abs() <= (b - baseKHz).abs() ? a : b,
);
return closestKHz / 1000.0;
}
double _normalFrequencyForBand(double frequencyMHz) {
@@ -1632,14 +1714,13 @@ class _RadioSettingsDialogState extends State<_RadioSettingsDialog> {
if (offGridFreqHz == current.frequencyHz &&
preset.bandwidth == current.bandwidth &&
preset.spreadingFactor == current.spreadingFactor &&
preset.codingRate == current.codingRate &&
preset.txPowerDbm == current.txPowerDbm) {
preset.codingRate == current.codingRate) {
return _RadioSettingsSnapshot(
frequencyMHz: preset.frequencyMHz,
bandwidth: preset.bandwidth,
spreadingFactor: preset.spreadingFactor,
codingRate: preset.codingRate,
txPowerDbm: preset.txPowerDbm,
txPowerDbm: current.txPowerDbm,
);
}
}
@@ -1728,14 +1809,15 @@ class _RadioSettingsDialogState extends State<_RadioSettingsDialog> {
void _validateFields() {
final l10n = context.l10n;
final freqMHz = double.tryParse(_frequencyController.text);
_frequencyError = (freqMHz == null || freqMHz < 300 || freqMHz > 2500)
_frequencyError = (freqMHz == null || freqMHz < 150 || freqMHz > 2500)
? l10n.settings_frequencyInvalid
: null;
final maxTxPower = widget.connector.maxTxPower ?? 22;
final txPower = int.tryParse(_txPowerController.text);
_txPowerError = (txPower == null || txPower < 0 || txPower > maxTxPower)
? '${l10n.settings_txPowerInvalid} (0-$maxTxPower dBm)'
_txPowerError =
(txPower == null || txPower < _minTxPowerDbm || txPower > maxTxPower)
? '${l10n.settings_txPowerInvalid} ($_minTxPowerDbm-$maxTxPower dBm)'
: null;
}
@@ -1772,7 +1854,7 @@ class _RadioSettingsDialogState extends State<_RadioSettingsDialog> {
final freqMHz = double.tryParse(_frequencyController.text);
final txPower = int.tryParse(_txPowerController.text);
if (freqMHz == null || freqMHz < 300 || freqMHz > 2500) {
if (freqMHz == null || freqMHz < 150 || freqMHz > 2500) {
showDismissibleSnackBar(
context,
content: Text(l10n.settings_frequencyInvalid),
@@ -1781,10 +1863,12 @@ class _RadioSettingsDialogState extends State<_RadioSettingsDialog> {
}
final maxTxPower = widget.connector.maxTxPower ?? 22;
if (txPower == null || txPower < 0 || txPower > maxTxPower) {
if (txPower == null || txPower < _minTxPowerDbm || txPower > maxTxPower) {
showDismissibleSnackBar(
context,
content: Text('${l10n.settings_txPowerInvalid} (0-$maxTxPower dBm)'),
content: Text(
'${l10n.settings_txPowerInvalid} ($_minTxPowerDbm-$maxTxPower dBm)',
),
);
return;
}
@@ -1792,18 +1876,14 @@ class _RadioSettingsDialogState extends State<_RadioSettingsDialog> {
final freqHz = (freqMHz * 1000).round();
final bwHz = _bandwidth.hz;
final sf = _spreadingFactor.value;
final cr = _toDeviceCodingRate(
_codingRate.value,
widget.connector.currentCr,
);
final cr = _codingRate.value;
// if the client repeat isnt null then we know its supported
//otherwise we leave it out of the frame to avoid accidentally enabling
final knownRepeat = widget.connector.clientRepeat != null;
if (knownRepeat) {
const validRepeatFreqsKHz = {433000, 869000, 918000};
if (_clientRepeat && !validRepeatFreqsKHz.contains(freqHz)) {
if (_clientRepeat && !_isAllowedRepeatFreqKHz(widget.connector, freqHz)) {
showDismissibleSnackBar(
context,
content: Text(l10n.settings_clientRepeatFreqWarning),
@@ -1814,23 +1894,53 @@ class _RadioSettingsDialogState extends State<_RadioSettingsDialog> {
try {
_logRadioSettingsState('Saving radio settings');
await widget.connector.sendFrame(
buildSetRadioParamsFrame(
freqHz,
bwHz,
sf,
cr,
final accepted = await widget.connector.setRadioParams(
freqHz,
bwHz,
sf,
cr,
clientRepeat: knownRepeat ? _clientRepeat : null,
);
if (!accepted) {
_logRadioSettingsState('Radio rejected requested settings');
if (!mounted) return;
showDismissibleSnackBar(
context,
content: Text(l10n.settings_radioSettingsNotApplied),
);
return;
}
await widget.connector.sendFrame(buildSetRadioTxPowerFrame(txPower));
final selfInfo = widget.connector.receivedFrames
.firstWhere((f) => f.isNotEmpty && f[0] == respCodeSelfInfo)
.timeout(const Duration(seconds: 5));
await widget.connector.refreshDeviceInfo();
final applied = await selfInfo.then(
(_) => _deviceMatches(
freqHz: freqHz,
bwHz: bwHz,
sf: sf,
cr: cr,
txPower: txPower,
clientRepeat: knownRepeat ? _clientRepeat : null,
),
onError: (_) => true,
);
await widget.connector.sendFrame(buildSetRadioTxPowerFrame(txPower));
await widget.connector.refreshDeviceInfo();
if (!applied) {
_logRadioSettingsState('Radio did not apply requested settings');
if (!mounted) return;
showDismissibleSnackBar(
context,
content: Text(l10n.settings_radioSettingsNotApplied),
);
return;
}
final rememberedSnapshot = _clientRepeat
? _lastNonRepeatSnapshot
: _currentSnapshot();
if (rememberedSnapshot != null) {
widget.connector.rememberNonRepeatRadioState(
rememberedSnapshot.toMeshCoreSnapshot(widget.connector.currentCr),
rememberedSnapshot.toMeshCoreSnapshot(),
);
}
@@ -1852,6 +1962,27 @@ class _RadioSettingsDialogState extends State<_RadioSettingsDialog> {
Navigator.pop(context);
}
bool _deviceMatches({
required int freqHz,
required int bwHz,
required int sf,
required int cr,
required int txPower,
required bool? clientRepeat,
}) {
final c = widget.connector;
// Firmware stores freq/bw as float MHz/kHz, so allow 1 unit of rounding.
return c.currentFreqHz != null &&
(c.currentFreqHz! - freqHz).abs() <= 1 &&
c.currentBwHz != null &&
(c.currentBwHz! - bwHz).abs() <= 1 &&
c.currentSf == sf &&
c.currentCr != null &&
_toUiCodingRate(c.currentCr!) == cr &&
c.currentTxPower == txPower &&
(clientRepeat == null || c.clientRepeat == clientRepeat);
}
String _presetLabel(int? index) {
if (index == null) {
return 'custom';
@@ -1898,20 +2029,30 @@ class _RadioSettingsDialogState extends State<_RadioSettingsDialog> {
children: [
DropdownButtonFormField<int>(
key: ValueKey<int?>(_selectedPresetIndex),
initialValue: _selectedPresetIndex,
initialValue: _selectedPresetIndex ?? -1,
isExpanded: true,
decoration: InputDecoration(
labelText: l10n.settings_presets,
border: const OutlineInputBorder(),
),
items: [
if (_selectedPresetIndex == null)
DropdownMenuItem(
value: -1,
child: Text(l10n.settings_presetCustom),
),
for (final i in _visiblePresetIndexes())
DropdownMenuItem(
value: i,
child: Text(RadioSettings.presets[i].$1),
child: Text(
RadioSettings.presets[i].$1,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
],
onChanged: (index) {
if (index != null) {
if (index != null && index >= 0) {
_applyPreset(index);
}
},
@@ -1923,7 +2064,11 @@ class _RadioSettingsDialogState extends State<_RadioSettingsDialog> {
decoration: InputDecoration(
labelText: l10n.settings_frequency,
border: const OutlineInputBorder(),
helperText: l10n.settings_frequencyHelper,
helperText: _clientRepeat
? l10n.settings_clientRepeatFrequencyNote(
_frequencyController.text,
)
: l10n.settings_frequencyHelper,
errorText: _frequencyError,
),
keyboardType: const TextInputType.numberWithOptions(
@@ -2005,9 +2150,10 @@ class _RadioSettingsDialogState extends State<_RadioSettingsDialog> {
decoration: InputDecoration(
labelText: l10n.settings_txPower,
border: const OutlineInputBorder(),
helperText: widget.connector.maxTxPower != null
? '${l10n.settings_txPowerHelper} (max: ${widget.connector.maxTxPower} dBm)'
: l10n.settings_txPowerHelper,
helperText: l10n.settings_txPowerRangeHelper(
_minTxPowerDbm,
widget.connector.maxTxPower ?? 22,
),
errorText: _txPowerError,
),
keyboardType: TextInputType.number,
@@ -2022,6 +2168,11 @@ class _RadioSettingsDialogState extends State<_RadioSettingsDialog> {
contentPadding: EdgeInsets.zero,
),
],
const SizedBox(height: 16),
Text(
l10n.settings_radioMatchWarning,
style: Theme.of(context).textTheme.bodySmall,
),
],
),
),
@@ -2083,12 +2234,12 @@ class _RadioSettingsSnapshot {
}
/// Convert back to the connector's raw-int snapshot.
MeshCoreRadioStateSnapshot toMeshCoreSnapshot(int? deviceCr) {
MeshCoreRadioStateSnapshot toMeshCoreSnapshot() {
return MeshCoreRadioStateSnapshot(
freqHz: frequencyHz,
bwHz: bandwidth.hz,
sf: spreadingFactor.value,
cr: _toDeviceCodingRate(codingRate.value, deviceCr),
cr: codingRate.value,
txPowerDbm: txPowerDbm,
);
}