Files
meshcore-open/lib/utils/contact_qr_frame.dart
T
zjs81 3c971705bd 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.
2026-09-27 12:04:05 -07:00

78 lines
3.0 KiB
Dart

import 'dart:convert';
import 'dart:typed_data';
import '../connector/meshcore_protocol.dart';
import '../models/contact.dart';
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 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.
///
/// [pathHashByteWidth] is the connector's current path-hash mode, needed to
/// re-encode a preserved path the same way the rest of the app does.
Uint8List buildContactQrFrame(
ScannedContact scanned, {
Contact? existing,
int pathHashByteWidth = 1,
}) {
if (scanned.isAdvertPacket) {
return buildImportContactFrame(scanned.advertPacket!);
}
final route = _routeOf(existing, pathHashByteWidth);
return buildUpdateContactPathFrame(
scanned.publicKey!,
route.path,
route.pathLen,
type: scanned.type ?? existing?.type ?? advTypeChat,
flags: existing?.flags ?? 0,
name: _fitName(
scanned.name.isEmpty ? (existing?.name ?? '') : scanned.name,
),
lastAdvert: existing?.lastSeen,
);
}
/// Rescanning a contact must not throw away a route the device already has:
/// writing an empty path with the unknown sentinel would force flooding until
/// another advert restores it. Reuse [existing]'s path when it is valid for the
/// current mode, encoded as `MeshCoreConnector._encodePathLenForCurrentMode`
/// does (hop count in bits 0-5, mode in bits 6-7).
({Uint8List path, int pathLen}) _routeOf(Contact? existing, int width) {
final unknown = (path: Uint8List(0), pathLen: 0xFF);
if (existing == null) return unknown;
final hops = existing.pathLength;
if (hops < 0 || hops > 0x3F) return unknown;
final hashWidth = width.clamp(1, 4).toInt();
final path = existing.path;
if (path.length > maxPathSize || path.length != hops * hashWidth) {
return unknown;
}
return (path: path, pathLen: hops | ((hashWidth - 1) << 6));
}
/// `writeCString` clips the encoded name to `maxNameSize - 1` bytes without
/// looking at character boundaries, which would leave a half-written rune in
/// the contact record. Drop whole characters instead.
String _fitName(String name) {
const limit = maxNameSize - 1;
if (utf8.encode(name).length <= limit) return name;
final buffer = StringBuffer();
var used = 0;
for (final rune in name.runes) {
final char = String.fromCharCode(rune);
final charBytes = utf8.encode(char).length;
if (used + charBytes > limit) break;
buffer.write(char);
used += charBytes;
}
return buffer.toString();
}