Scan official MeshCore channel QR codes
The QR scanner only read community codes, so the standard meshcore://channel/add?name=...&secret=... links used by the official app and sites like ipnt.uk were rejected as invalid community codes. The scanner now also accepts channel links. It skips channels already on the radio, asks before adding, and only uses a free slot after the channel sync has finished. Also fills in the 59 untranslated strings in all 17 locales. Fixes #604
This commit is contained in:
1 parent
b6c67b113c
commit
b1e968e7bf
42 files changed
+3816
-1850
No files matched your search
@@ -0,0 +1,59 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
/// Parsed result of an official MeshCore channel QR / link:
|
||||
/// `meshcore://channel/add?name=<name>&secret=<32 hex chars>`.
|
||||
class ScannedChannel {
|
||||
/// Channel name as encoded in the link; never empty.
|
||||
final String name;
|
||||
|
||||
/// 16-byte channel secret.
|
||||
final Uint8List psk;
|
||||
|
||||
const ScannedChannel({required this.name, required this.psk});
|
||||
}
|
||||
|
||||
const int _channelSecretBytes = 16;
|
||||
|
||||
/// Returns null when [data] is not a recognizable MeshCore channel QR/link.
|
||||
ScannedChannel? parseChannelQr(String data) {
|
||||
final trimmed = data.trim();
|
||||
if (trimmed.isEmpty) return null;
|
||||
|
||||
final uri = Uri.tryParse(trimmed);
|
||||
if (uri == null || uri.scheme != 'meshcore') return null;
|
||||
|
||||
final normalizedPath = '${uri.host}${uri.path}'.toLowerCase().replaceAll(
|
||||
RegExp(r'^/+|/+$'),
|
||||
'',
|
||||
);
|
||||
if (normalizedPath != 'channel/add') return null;
|
||||
|
||||
final params = uri.queryParameters;
|
||||
final name = (params['name'] ?? '').trim();
|
||||
if (name.isEmpty) return null;
|
||||
|
||||
final psk = _tryHexDecode(params['secret'] ?? '');
|
||||
if (psk == null || psk.length != _channelSecretBytes) return null;
|
||||
|
||||
return ScannedChannel(name: name, psk: psk);
|
||||
}
|
||||
|
||||
/// Local hex decoder so this util stays pure-Dart; see `contact_qr.dart`.
|
||||
Uint8List? _tryHexDecode(String hex) {
|
||||
if (hex.isEmpty || hex.length.isOdd) return null;
|
||||
final bytes = Uint8List(hex.length ~/ 2);
|
||||
for (int i = 0; i < bytes.length; i++) {
|
||||
final high = _hexDigit(hex.codeUnitAt(i * 2));
|
||||
final low = _hexDigit(hex.codeUnitAt(i * 2 + 1));
|
||||
if (high == null || low == null) return null;
|
||||
bytes[i] = (high << 4) | low;
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
int? _hexDigit(int codeUnit) {
|
||||
if (codeUnit >= 0x30 && codeUnit <= 0x39) return codeUnit - 0x30;
|
||||
if (codeUnit >= 0x61 && codeUnit <= 0x66) return codeUnit - 0x61 + 10;
|
||||
if (codeUnit >= 0x41 && codeUnit <= 0x46) return codeUnit - 0x41 + 10;
|
||||
return null;
|
||||
}
|
||||
Reference in new issue
Block a user