import 'package:flutter/foundation.dart'; import 'package:universal_ble/universal_ble.dart'; import 'emulated_ble_platform.dart'; import 'emulation_profile.dart'; /// Retained entry cap for [EmulationSession.writeLog]. const _maxWriteLogEntries = 100; /// A live emulated device: its peripheral, profile, decoded write log and the /// interactive inputs bound to it. class EmulationSession { EmulationSession({required this.profile, required this.peripheral, required this.ble}) { profile.onRegistered?.call(ble, peripheral); final scripted = peripheral.onWrite; peripheral.onWrite = (service, characteristic, value) { scripted?.call(service, characteristic, value); final decoded = profile.decodeWrite?.call(characteristic.toLowerCase(), value); if (decoded != null) { final updated = [...writeLog.value, decoded]; // Incline writes stream continuously for the ride's duration; cap // retained entries so a long ride doesn't grow this unbounded. writeLog.value = updated.length > _maxWriteLogEntries ? updated.sublist(updated.length - _maxWriteLogEntries) : updated; } }; inputs = profile.inputs?.call(this) ?? const []; } final EmulationProfile profile; final FakePeripheral peripheral; final FakeUniversalBlePlatform ble; final ValueNotifier> writeLog = ValueNotifier(const []); late final List inputs; /// Delivers a notification unless the peripheral has been removed — profiles /// fire DELAYED notifies (e.g. the SRAM double-tap's second edge, 100ms /// late), and a dangling one must not reach the app after the session was /// stopped: it would register a phantom press post-teardown. void notify(String characteristicUuid, List bytes) { if (!ble.peripherals.containsKey(peripheral.deviceId)) return; ble.notify(peripheral.deviceId, characteristicUuid, bytes); } void dropConnection() => ble.dropConnection(peripheral.deviceId); /// Re-emits the scan result with a different RSSI (drives the signal UI). void setRssi(int rssi) { final base = peripheral.scanResult; ble.updateScanResult( BleDevice( deviceId: base.deviceId, name: peripheral.name, rssi: rssi, services: base.services, manufacturerDataList: base.manufacturerDataList, ), ); } } /// Owns the emulated peripherals in debug builds. Never attached in release /// builds, so it stays inert there. class EmulationManager { FakeUniversalBlePlatform? _ble; final Map _sessions = {}; bool get isAvailable => _ble != null; /// The fake platform the emulated peripherals live on. Only valid when /// [isAvailable]. FakeUniversalBlePlatform get ble => _ble!; void attach(FakeUniversalBlePlatform ble) => _ble = ble; List get sessions => _sessions.values.toList(); EmulationSession start(EmulationProfile profile) { final ble = _ble; if (ble == null) throw StateError('EmulationManager.attach was never called'); final peripheral = profile.build(); final existing = _sessions[peripheral.deviceId]; if (existing != null) return existing; final session = EmulationSession(profile: profile, peripheral: peripheral, ble: ble); _sessions[peripheral.deviceId] = session; ble.addPeripheral(peripheral); return session; } bool isEmulated(String deviceId) => _sessions.containsKey(deviceId); EmulationSession? sessionFor(String deviceId) => _sessions[deviceId]; void stop(String deviceId) { _sessions.remove(deviceId); _ble?.removePeripheral(deviceId); } /// Test hook: drop all sessions (the fake platform's own reset() clears the /// peripherals). @visibleForTesting void reset() => _sessions.clear(); }