From 3c971705bd7bd358fb45aca550c375a69f610697 Mon Sep 17 00:00:00 2001 From: zjs81 Date: Sun, 27 Sep 2026 12:04:05 -0700 Subject: [PATCH] Fix QR contact import replay guard, type fallback and rescan loops - Pass the existing contact's advert timestamp (0 for new contacts) so the firmware replay guard does not drop the node's real adverts. - Leave a missing/invalid QR type unset and fall back to the existing contact's type instead of rewriting repeaters/rooms as Companion. - Show a failed state with Retry instead of reopening the camera, which rescanned the same QR and resent the frame in a loop. - Keep the processing flag set after a successful import so the closing route cannot restart the camera, import again and pop twice. --- lib/screens/contact_qr_scanner_screen.dart | 47 +++++++++++++++--- lib/utils/contact_qr.dart | 11 +++-- lib/utils/contact_qr_frame.dart | 9 ++-- test/utils/contact_qr_frame_test.dart | 55 ++++++++++++++++++++++ test/utils/contact_qr_test.dart | 8 ++-- 5 files changed, 111 insertions(+), 19 deletions(-) diff --git a/lib/screens/contact_qr_scanner_screen.dart b/lib/screens/contact_qr_scanner_screen.dart index 74b2116b..87348c0f 100644 --- a/lib/screens/contact_qr_scanner_screen.dart +++ b/lib/screens/contact_qr_scanner_screen.dart @@ -25,6 +25,10 @@ class ContactQrScannerScreen extends StatefulWidget { class _ContactQrScannerScreenState extends State { bool _isProcessing = false; + /// Set after a failed import so the camera does not immediately rescan the + /// same QR and resend the frame in a loop. + bool _importFailed = false; + @override Widget build(BuildContext context) { return Scaffold( @@ -44,6 +48,8 @@ class _ContactQrScannerScreenState extends State { color: Theme.of(context).colorScheme.surface, child: const Center(child: CircularProgressIndicator()), ) + : _importFailed + ? _buildImportFailed(context) : QrScannerWidget( onScanned: (data) => _handleScannedData(data), validator: (data) => parseContactQr(data) != null, @@ -54,6 +60,36 @@ class _ContactQrScannerScreenState extends State { ); } + Widget _buildImportFailed(BuildContext context) { + return Center( + child: Padding( + padding: const EdgeInsets.all(24), + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + Icon( + Icons.error_outline, + size: 64, + color: Theme.of(context).colorScheme.error, + ), + const SizedBox(height: 16), + Text( + context.l10n.contacts_contactImportFailed, + style: Theme.of(context).textTheme.titleMedium, + textAlign: TextAlign.center, + ), + const SizedBox(height: 24), + FilledButton.icon( + onPressed: () => setState(() => _importFailed = false), + icon: const Icon(Icons.refresh), + label: Text(context.l10n.common_retry), + ), + ], + ), + ), + ); + } + Widget _buildThemedOverlay(BuildContext context) { return Stack( fit: StackFit.expand, @@ -202,6 +238,7 @@ class _ContactQrScannerScreenState extends State { setState(() { _isProcessing = true; + _importFailed = false; }); final scanned = parseContactQr(data); @@ -240,18 +277,14 @@ class _ContactQrScannerScreenState extends State { context, content: Text(context.l10n.contacts_contactImported), ); + // Keep _isProcessing set: resetting it would restart the camera on a + // closing route and let a second detection import and pop again. Navigator.pop(context); } catch (e) { - if (mounted) { - showDismissibleSnackBar( - context, - content: Text(context.l10n.contacts_contactImportFailed), - ); - } - } finally { if (mounted) { setState(() { _isProcessing = false; + _importFailed = true; }); } } diff --git a/lib/utils/contact_qr.dart b/lib/utils/contact_qr.dart index 6e8d53d6..cc789926 100644 --- a/lib/utils/contact_qr.dart +++ b/lib/utils/contact_qr.dart @@ -11,14 +11,15 @@ class ScannedContact { /// Contact name from the card form; empty string when unknown. final String name; - /// Advert type: 1 Companion, 2 Repeater, 3 Room Server, 4 Sensor. - final int type; + /// Advert type: 1 Companion, 2 Repeater, 3 Room Server, 4 Sensor; null + /// when the card omits it or carries an unknown value. + final int? type; const ScannedContact({ this.advertPacket, this.publicKey, this.name = '', - this.type = 1, + this.type, }); bool get isAdvertPacket => advertPacket != null; @@ -71,8 +72,8 @@ ScannedContact? _parseContactCard(Uri uri) { } final name = (params['name'] ?? '').trim(); - int type = int.tryParse(params['type'] ?? '') ?? 1; - if (type < 1 || type > 4) type = 1; + int? type = int.tryParse(params['type'] ?? ''); + if (type != null && (type < 1 || type > 4)) type = null; return ScannedContact(publicKey: publicKey, name: name, type: type); } diff --git a/lib/utils/contact_qr_frame.dart b/lib/utils/contact_qr_frame.dart index 23310f89..26492e76 100644 --- a/lib/utils/contact_qr_frame.dart +++ b/lib/utils/contact_qr_frame.dart @@ -8,8 +8,10 @@ import 'contact_qr.dart'; /// Builds the device frame that applies [scanned]. /// /// Advert blobs are replayed with `CMD_IMPORT_CONTACT`; contact cards are -/// written with `CMD_ADD_UPDATE_CONTACT`, preserving the flags, name and route -/// of [existing] when the QR omits them. A card carries no route, so a contact +/// written with `CMD_ADD_UPDATE_CONTACT`, preserving the type, flags, name, +/// route and advert timestamp of [existing] when the QR omits them. The +/// timestamp is the firmware's replay guard, so a new contact gets 0 rather +/// than the phone clock, which would make it drop the node's real adverts. A card carries no route, so a contact /// the device does not know yet is written with the `0xFF` unknown-path /// sentinel (`Contact.fromFrame` maps it to `hopCount == -1`) and its messages /// flood until an advert supplies a path. @@ -29,11 +31,12 @@ Uint8List buildContactQrFrame( scanned.publicKey!, route.path, route.pathLen, - type: scanned.type, + type: scanned.type ?? existing?.type ?? advTypeChat, flags: existing?.flags ?? 0, name: _fitName( scanned.name.isEmpty ? (existing?.name ?? '') : scanned.name, ), + lastAdvert: existing?.lastSeen, ); } diff --git a/test/utils/contact_qr_frame_test.dart b/test/utils/contact_qr_frame_test.dart index 66237da7..13af81ee 100644 --- a/test/utils/contact_qr_frame_test.dart +++ b/test/utils/contact_qr_frame_test.dart @@ -56,6 +56,61 @@ void main() { expect(name, 'Alice'); }); + test('defaults a new contact without a type to Companion', () { + final frame = buildContactQrFrame(ScannedContact(publicKey: publicKey)); + + expect(frame[33], advTypeChat); + }); + + test('keeps the type of an existing contact when the card has none', () { + final existing = Contact( + publicKey: publicKey, + name: 'Hilltop', + type: advTypeRepeater, + pathLength: -1, + path: Uint8List(0), + lastSeen: DateTime.fromMillisecondsSinceEpoch(0), + ); + final frame = buildContactQrFrame( + ScannedContact(publicKey: publicKey), + existing: existing, + ); + + expect(frame[33], advTypeRepeater); + }); + + test('writes a zero advert timestamp for a new contact', () { + final frame = buildContactQrFrame( + ScannedContact(publicKey: publicKey, name: 'Alice'), + ); + + final tsStart = 36 + maxPathSize + maxNameSize; + expect(frame.sublist(tsStart, tsStart + 4), [0, 0, 0, 0]); + }); + + test('keeps the advert timestamp of an existing contact', () { + final existing = Contact( + publicKey: publicKey, + name: 'Alice', + type: advTypeChat, + pathLength: -1, + path: Uint8List(0), + lastSeen: DateTime.fromMillisecondsSinceEpoch(1700000000 * 1000), + ); + final frame = buildContactQrFrame( + ScannedContact(publicKey: publicKey, name: 'Alice'), + existing: existing, + ); + + final tsStart = 36 + maxPathSize + maxNameSize; + final ts = ByteData.sublistView( + frame, + tsStart, + tsStart + 4, + ).getUint32(0, Endian.little); + expect(ts, 1700000000); + }); + test('preserves the flags of an existing contact', () { final frame = buildContactQrFrame( ScannedContact(publicKey: publicKey, name: 'Alice'), diff --git a/test/utils/contact_qr_test.dart b/test/utils/contact_qr_test.dart index f4efafe7..c900207c 100644 --- a/test/utils/contact_qr_test.dart +++ b/test/utils/contact_qr_test.dart @@ -21,24 +21,24 @@ void main() { expect(result.type, 2); }); - test('missing type defaults to 1', () { + test('missing type is left unset', () { final key = 'ab' * 32; final result = parseContactQr( 'meshcore://contact/add?name=Bob&public_key=$key', ); expect(result, isNotNull); - expect(result!.type, 1); + expect(result!.type, isNull); }); - test('garbage type falls back to 1', () { + test('garbage type is left unset', () { final key = 'ab' * 32; final result = parseContactQr( 'meshcore://contact/add?name=Bob&public_key=$key&type=nope', ); expect(result, isNotNull); - expect(result!.type, 1); + expect(result!.type, isNull); }); test('uppercase hex public key is accepted', () {