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.
This commit is contained in:
1 parent
fba97b9628
commit
3c971705bd
5 files changed
+111
-19
No files matched your search
@@ -25,6 +25,10 @@ class ContactQrScannerScreen extends StatefulWidget {
|
||||
class _ContactQrScannerScreenState extends State<ContactQrScannerScreen> {
|
||||
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<ContactQrScannerScreen> {
|
||||
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<ContactQrScannerScreen> {
|
||||
);
|
||||
}
|
||||
|
||||
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<ContactQrScannerScreen> {
|
||||
|
||||
setState(() {
|
||||
_isProcessing = true;
|
||||
_importFailed = false;
|
||||
});
|
||||
|
||||
final scanned = parseContactQr(data);
|
||||
@@ -240,18 +277,14 @@ class _ContactQrScannerScreenState extends State<ContactQrScannerScreen> {
|
||||
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;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -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'),
|
||||
|
||||
@@ -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', () {
|
||||
|
||||
Reference in new issue
Block a user