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:
zjs81 committed 2026-09-27 12:04:05 -07:00
1 parent fba97b9628
commit 3c971705bd
5 files changed
+111 -19

No files matched your search

+6 -5
View File
@@ -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);
}
+6 -3
View File
@@ -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,
);
}