Files
swiftcontrol/lib/services/debug_diagnostics.dart
Jonas Bark 274a6ba634 diag: show repeat counts in the mDNS query log; bump prop submodule
Renders "×N" on folded entries so a continuous poller reads as one line that
fired N times rather than silently occupying one slot. Single occurrences stay
unmarked.

Picks up the prop-side fold + the 40 -> 200 ceiling, after a field capture
filled the whole buffer in eight seconds and lost the window that mattered.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PanTG9kRBi4QTQ2psvpTxP
2026-07-30 21:52:25 +02:00

218 lines
7.3 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'dart:io' show Platform;
import 'package:bike_control/main.dart' show recordError;
import 'package:bike_control/services/mdns_discovery_scan.dart';
import 'package:flutter/foundation.dart';
import 'package:prop/mdns/mdns_responder.dart' show MdnsQueryLogEntry;
import 'package:prop/mdns/service_advertiser.dart';
import 'package:prop/utils/advertised_service_registry.dart';
import 'package:prop/utils/network_address.dart';
import 'package:prop/utils/resilient_tcp_server.dart';
/// A running TCP bridge server, for diagnostics.
class TcpServerInfo {
final String? label;
final int? port;
final bool listening;
final bool hasClient;
const TcpServerInfo({
required this.label,
required this.port,
required this.listening,
required this.hasClient,
});
}
/// Status of the permissions whose denial silently breaks WiFi/BLE bridging.
class PermissionsSnapshot {
/// iOS Local Network can't be queried directly; inferred from whether a
/// discovery scan returned anything. Null when no scan ran.
final bool? localNetworkInferred;
const PermissionsSnapshot({
required this.localNetworkInferred,
});
static Future<PermissionsSnapshot> gather({bool? localNetworkInferred}) async {
return PermissionsSnapshot(
localNetworkInferred: localNetworkInferred,
);
}
}
/// The full diagnostics snapshot shown on the Logs page and embedded in
/// [debugText]. Build with [gather]; render with [toText].
class DebugDiagnostics {
final List<AdvertisedRecord> advertised;
final String backend;
final String? hostLabel;
final bool holdsMulticastLock;
final List<DiscoveredMdnsService> discovered;
final bool discoveryRan;
final AddressPickReport addressReport;
final List<TcpServerInfo> servers;
final PermissionsSnapshot permissions;
/// mDNS queries our responder saw. Empty on the nsd backend (iOS), where the
/// OS responder handles queries and never tells us about them.
final List<MdnsQueryLogEntry> recentQueries;
const DebugDiagnostics({
required this.advertised,
required this.backend,
required this.hostLabel,
required this.holdsMulticastLock,
required this.discovered,
required this.discoveryRan,
required this.addressReport,
required this.servers,
required this.permissions,
this.recentQueries = const [],
});
static Future<DebugDiagnostics> gather({
bool includeDiscovery = true,
Duration discoveryTimeout = const Duration(seconds: 4),
}) async {
final advertiser = ServiceAdvertiser.instance;
final isResponder = advertiser is ResponderServiceAdvertiser;
AddressPickReport addressReport;
try {
addressReport = await AdvertisedAddressPicker.report();
} catch (e, s) {
recordError(e, s, context: 'DebugDiagnostics.address');
addressReport = const AddressPickReport(chosen: null, candidates: []);
}
final servers = ResilientTcpServer.activeServers
.map(
(s) => TcpServerInfo(
label: s.label,
port: s.isRunning ? s.boundPort : null,
listening: s.isRunning,
hasClient: s.hasClient,
),
)
.toList();
var discovered = <DiscoveredMdnsService>[];
var discoveryRan = false;
if (includeDiscovery && !kIsWeb) {
try {
discovered = await MdnsDiscoveryScan().run(timeout: discoveryTimeout);
discoveryRan = true;
} catch (e, s) {
recordError(e, s, context: 'DebugDiagnostics.discovery');
}
}
final permissions = await PermissionsSnapshot.gather(
// iOS is the only platform with a (non-queryable) "Local Network"
// permission; infer it from whether discovery saw anything. Elsewhere an
// empty scan just means no peers, so leave it unset.
localNetworkInferred: (discoveryRan && !kIsWeb && Platform.isIOS) ? discovered.isNotEmpty : null,
);
return DebugDiagnostics(
advertised: AdvertisedServiceRegistry.instance.records,
backend: isResponder ? 'responder' : 'nsd',
hostLabel: isResponder ? advertiser.hostLabel : null,
holdsMulticastLock: isResponder ? advertiser.holdsMulticastLock : false,
discovered: discovered,
discoveryRan: discoveryRan,
addressReport: addressReport,
servers: servers,
permissions: permissions,
recentQueries: isResponder ? advertiser.recentQueries : const [],
);
}
String _txt(Map<String, String> txt) {
final entries = txt.entries.toList()..sort((a, b) => a.key.compareTo(b.key));
return entries.map((e) => '${e.key}=${e.value}').join(', ');
}
String toText() {
final b = StringBuffer();
b.writeln('Diagnostics:');
b.writeln(' Advertised by this device:');
if (advertised.isEmpty) {
b.writeln(' (none)');
} else {
for (final a in advertised) {
b.writeln(' ${a.type} "${a.name}" ${a.address}:${a.port}');
if (a.txt.isNotEmpty) b.writeln(' txt: ${_txt(a.txt)}');
}
}
b.writeln(
' backend: $backend'
'${hostLabel != null ? ' · host: $hostLabel.local' : ''}'
'${backend == 'responder' ? ' · multicast-lock: ${holdsMulticastLock ? 'held' : 'not held'}' : ''}',
);
b.writeln(' Discovered on network:');
if (!discoveryRan) {
b.writeln(' (skipped)');
} else if (discovered.isEmpty) {
b.writeln(' (none found)');
} else {
for (final d in discovered) {
b.writeln(' ${d.type} "${d.name}" ${d.host}:${d.port}${d.isSelf ? ' (this device)' : ''}');
if (d.txt.isNotEmpty) b.writeln(' txt: ${_txt(d.txt)}');
}
}
b.writeln(' Network interfaces (advertised = ${addressReport.chosen?.address ?? 'none'}):');
for (final c in addressReport.candidates) {
final tags = [
if (addressReport.chosen?.address == c.address) 'advertised',
if (c.isVirtual) 'virtual',
];
b.writeln(' ${c.interfaceName}/${c.address} = ${c.score}${tags.isEmpty ? '' : ' (${tags.join(', ')})'}');
}
// "Did the trainer app on this machine ask us anything, and did we answer?"
// A same-host querier shows up with the advertised address as its source.
// No entries at all means the failure is upstream of this responder.
b.writeln(' mDNS queries received:');
if (recentQueries.isEmpty) {
b.writeln(' (none)');
} else {
for (final q in recentQueries) {
final at = q.at.toIso8601String().split('T').last.split('.').first;
// Repeats are folded; the count keeps a continuous poller visible as
// one line instead of hiding that it fired hundreds of times.
final repeats = q.count > 1 ? ' ×${q.count}' : '';
b.writeln(
' $at ${q.source}:${q.sourcePort} ${q.wantsUnicast ? 'QU' : 'QM'} '
'${q.questions.join(', ')} → ${q.reply}$repeats',
);
}
}
b.writeln(' TCP servers:');
if (servers.isEmpty) {
b.writeln(' (none)');
} else {
for (final s in servers) {
b.writeln(
' ${s.label ?? 'tcp'} :${s.port ?? '-'} '
'${s.listening ? 'listening' : 'down'} · ${s.hasClient ? '1 client' : 'no client'}',
);
}
}
if (permissions.localNetworkInferred != null) {
b.writeln(
' Permissions: ios-local-network='
'${permissions.localNetworkInferred! ? 'inferred-ok' : 'inferred-blocked'}',
);
}
return b.toString().trimRight();
}
}