From d79157021379e4e333ce1de66bb7173f4bfd0b44 Mon Sep 17 00:00:00 2001 From: hdhog Date: Wed, 9 Sep 2026 21:53:43 +0400 Subject: [PATCH] fix: use explicit time for repeater clock sync Companion firmware (MeshCore commit 4a869163, 2025-12-30) replaces the CLI frame timestamp with the companion node's RTC for TXT_TYPE_CLI_DATA, so "clock sync" sets the repeater clock to the node RTC. Nodes with a wrong RTC push repeater clocks years into the future; firmware cannot set the clock backwards without clkreboot. - RepeaterCommandService: translate "clock sync" to "time " before sending, matching official meshcore-cli behavior - repeater login dialog: send "time " for auto clock sync after login (this path bypasses RepeaterCommandService) - add unit tests for the command translation Ref: https://github.com/zjs81/meshcore-open/issues/464 --- lib/services/repeater_command_service.dart | 13 ++++++ lib/widgets/repeater_login_dialog.dart | 6 ++- .../repeater_command_service_test.dart | 43 +++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 test/services/repeater_command_service_test.dart diff --git a/lib/services/repeater_command_service.dart b/lib/services/repeater_command_service.dart index 61d22c8e..477b1f40 100644 --- a/lib/services/repeater_command_service.dart +++ b/lib/services/repeater_command_service.dart @@ -4,6 +4,18 @@ import '../models/path_selection.dart'; import '../connector/meshcore_connector.dart'; import '../connector/meshcore_protocol.dart'; +/// Companion firmware (MeshCore commit 4a869163, 2025-12-30) replaces the CLI +/// frame timestamp with the companion node's RTC for TXT_TYPE_CLI_DATA, so a +/// plain "clock sync" sets the repeater clock to the node clock. The official +/// meshcore-cli translates it to an explicit `time ` command instead; +/// do the same so the repeater always gets the phone's time. +String normalizeRepeaterClockSyncCommand(String command, {int? nowSeconds}) { + final epoch = nowSeconds ?? DateTime.now().millisecondsSinceEpoch ~/ 1000; + return command.trim().toLowerCase() == 'clock sync' + ? 'time $epoch' + : command; +} + class RepeaterCommandService { final MeshCoreConnector _connector; final Map> _pendingCommands = {}; @@ -25,6 +37,7 @@ class RepeaterCommandService { Function(int)? onAttempt, int retries = maxRetries, }) async { + command = normalizeRepeaterClockSyncCommand(command); final attemptCount = retries < 1 ? 1 : retries; final selection = await _connector.preparePathForContactSend(repeater); diff --git a/lib/widgets/repeater_login_dialog.dart b/lib/widgets/repeater_login_dialog.dart index 03f6e345..560828f7 100644 --- a/lib/widgets/repeater_login_dialog.dart +++ b/lib/widgets/repeater_login_dialog.dart @@ -7,6 +7,7 @@ import '../l10n/l10n.dart'; import '../models/contact.dart'; import '../l10n/contact_localization.dart'; import '../services/storage_service.dart'; +import '../services/repeater_command_service.dart'; import '../connector/meshcore_connector.dart'; import '../connector/meshcore_protocol.dart'; import '../theme/mesh_theme.dart'; @@ -201,7 +202,10 @@ class _RepeaterLoginDialogState extends State { await _connector.sendFrame( buildSendCliCommandFrame( repeater.publicKey, - 'clock sync', + normalizeRepeaterClockSyncCommand( + 'clock sync', + nowSeconds: timestampSeconds, + ), timestampSeconds: timestampSeconds, ), ); diff --git a/test/services/repeater_command_service_test.dart b/test/services/repeater_command_service_test.dart new file mode 100644 index 00000000..10b517e5 --- /dev/null +++ b/test/services/repeater_command_service_test.dart @@ -0,0 +1,43 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:meshcore_open/services/repeater_command_service.dart'; + +void main() { + group('normalizeRepeaterClockSyncCommand', () { + test('translates "clock sync" to explicit time command', () { + final result = normalizeRepeaterClockSyncCommand( + 'clock sync', + nowSeconds: 1787900000, + ); + expect(result, 'time 1787900000'); + }); + + test('is case-insensitive and tolerates surrounding whitespace', () { + expect( + normalizeRepeaterClockSyncCommand(' Clock Sync ', nowSeconds: 1), + 'time 1', + ); + }); + + test('passes other commands through unchanged', () { + expect(normalizeRepeaterClockSyncCommand('clock', nowSeconds: 1), 'clock'); + expect( + normalizeRepeaterClockSyncCommand('get radio', nowSeconds: 1), + 'get radio', + ); + expect( + normalizeRepeaterClockSyncCommand('time 123', nowSeconds: 1), + 'time 123', + ); + }); + + test('uses current time when nowSeconds is omitted', () { + final before = DateTime.now().millisecondsSinceEpoch ~/ 1000; + final result = normalizeRepeaterClockSyncCommand('clock sync'); + final after = DateTime.now().millisecondsSinceEpoch ~/ 1000; + + expect(result, startsWith('time ')); + final epoch = int.parse(result.substring('time '.length)); + expect(epoch, inInclusiveRange(before, after)); + }); + }); +}