197 lines
7.5 KiB
C#
197 lines
7.5 KiB
C#
using TuneBoss.Core.Models;
|
|
using TuneBoss.Core.Protocol;
|
|
|
|
namespace TuneBoss.Core.BLE;
|
|
|
|
/// <summary>
|
|
/// Represents a discovered BLE device before connection.
|
|
/// </summary>
|
|
public sealed record BleDeviceInfo(
|
|
ulong Address,
|
|
string Name,
|
|
short Rssi)
|
|
{
|
|
public string MacAddress => FormatMac(Address);
|
|
|
|
private static string FormatMac(ulong a)
|
|
{
|
|
var b = BitConverter.GetBytes(a);
|
|
return $"{b[5]:X2}:{b[4]:X2}:{b[3]:X2}:{b[2]:X2}:{b[1]:X2}:{b[0]:X2}";
|
|
}
|
|
|
|
public override string ToString() => $"{Name} | {MacAddress} | {Rssi} dBm";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Connection state published to observers.
|
|
/// </summary>
|
|
public enum BleConnectionState
|
|
{
|
|
Disconnected,
|
|
Connecting,
|
|
Connected,
|
|
Disconnecting,
|
|
}
|
|
|
|
/// <summary>
|
|
/// Result of a command sent to the device.
|
|
/// </summary>
|
|
public sealed record CommandResult(
|
|
bool Success,
|
|
byte ResponseCode, // 0x51 = success, 0x52 = fail, etc.
|
|
byte[] Payload,
|
|
string? ErrorMessage = null)
|
|
{
|
|
public static CommandResult Ok(byte[] payload) =>
|
|
new(true, 0x51, payload);
|
|
|
|
public static CommandResult Fail(byte responseCode, string message) =>
|
|
new(false, responseCode, Array.Empty<byte>(), message);
|
|
|
|
public static CommandResult Timeout() =>
|
|
new(false, 0x00, Array.Empty<byte>(), "Command timed out");
|
|
}
|
|
|
|
/// <summary>
|
|
/// The primary BLE service contract.
|
|
/// All table and device operations go through this interface.
|
|
/// The UI layer never touches BLE APIs directly.
|
|
/// </summary>
|
|
public interface IBleService : IDisposable
|
|
{
|
|
// ── State ────────────────────────────────────────────────────────────────
|
|
|
|
BleConnectionState ConnectionState { get; }
|
|
bool IsConnected { get; }
|
|
|
|
/// <summary>Observable connection state changes.</summary>
|
|
IObservable<BleConnectionState> ConnectionStateChanged { get; }
|
|
|
|
// ── Scanning ─────────────────────────────────────────────────────────────
|
|
|
|
/// <summary>Starts BLE advertisement scanning. Emits discovered devices.</summary>
|
|
IObservable<BleDeviceInfo> StartScan();
|
|
|
|
/// <summary>Stops active scan.</summary>
|
|
void StopScan();
|
|
|
|
// ── Connection ───────────────────────────────────────────────────────────
|
|
|
|
/// <summary>
|
|
/// Connects to a device. Handles GATT channel opening, NUS service
|
|
/// discovery, notification subscription, and the post-connect APP_READ_INFO.
|
|
/// </summary>
|
|
Task<bool> ConnectAsync(BleDeviceInfo device, CancellationToken ct = default);
|
|
|
|
/// <summary>Gracefully disconnects, stops diagnostics, cleans up GATT resources.</summary>
|
|
Task DisconnectAsync();
|
|
|
|
// ── Device info ──────────────────────────────────────────────────────────
|
|
|
|
/// <summary>
|
|
/// Reads ECU info (APP_READ_INFO). Returns null on timeout or failure.
|
|
/// Also sets the active GenerationConfig internally.
|
|
/// </summary>
|
|
Task<EcuInfo?> ReadEcuInfoAsync(CancellationToken ct = default);
|
|
|
|
// ── Auth ─────────────────────────────────────────────────────────────────
|
|
|
|
/// <summary>Sends unlock command with password and current date.</summary>
|
|
Task<CommandResult> UnlockAsync(string password, CancellationToken ct = default);
|
|
|
|
/// <summary>Sends tuner unlock command.</summary>
|
|
Task<CommandResult> UnlockTunerAsync(string password, CancellationToken ct = default);
|
|
|
|
// ── Table operations ─────────────────────────────────────────────────────
|
|
|
|
/// <summary>
|
|
/// Reads a 2D table (split read: two halves, with diagnostics pause/resume).
|
|
/// Returns the raw combined payload.
|
|
/// </summary>
|
|
Task<CommandResult> ReadTable2DAsync(
|
|
ushort address,
|
|
byte lengthBytes,
|
|
bool userTable = false,
|
|
CancellationToken ct = default);
|
|
|
|
/// <summary>Writes a 2D table (split write).</summary>
|
|
Task<CommandResult> WriteTable2DAsync(
|
|
ushort address,
|
|
byte[] data,
|
|
bool userTable = false,
|
|
CancellationToken ct = default);
|
|
|
|
/// <summary>Reads a 1D table.</summary>
|
|
Task<CommandResult> ReadTable1DAsync(
|
|
ushort address,
|
|
byte lengthBytes,
|
|
CancellationToken ct = default);
|
|
|
|
/// <summary>Writes a 1D table.</summary>
|
|
Task<CommandResult> WriteTable1DAsync(
|
|
ushort address,
|
|
byte[] data,
|
|
CancellationToken ct = default);
|
|
|
|
/// <summary>Reads a subtable.</summary>
|
|
Task<CommandResult> ReadSubTableAsync(
|
|
ushort address,
|
|
CancellationToken ct = default);
|
|
|
|
/// <summary>Writes a subtable.</summary>
|
|
Task<CommandResult> WriteSubTableAsync(
|
|
ushort address,
|
|
byte[] data,
|
|
CancellationToken ct = default);
|
|
|
|
/// <summary>Reads misc values block.</summary>
|
|
Task<CommandResult> ReadMiscAsync(
|
|
ushort address,
|
|
byte lengthBytes,
|
|
CancellationToken ct = default);
|
|
|
|
/// <summary>Writes misc values block.</summary>
|
|
Task<CommandResult> WriteMiscAsync(
|
|
ushort address,
|
|
byte[] data,
|
|
CancellationToken ct = default);
|
|
|
|
// ── Flash ────────────────────────────────────────────────────────────────
|
|
|
|
/// <summary>Sends APP_FLASH_WR to persist all writes to ECU flash.</summary>
|
|
Task<CommandResult> FlashWriteAsync(CancellationToken ct = default);
|
|
|
|
/// <summary>
|
|
/// Writes the 4-byte first-use settings block via APP_FIRSTUSE (0x35).
|
|
/// Byte[0] (DD slot) carries the TPS-calibration-done flag.
|
|
/// </summary>
|
|
Task<CommandResult> WriteFirstUseAsync(byte[] fourBytes, CancellationToken ct = default);
|
|
|
|
// ── Diagnostics streaming ────────────────────────────────────────────────
|
|
|
|
/// <summary>
|
|
/// Observable stream of diagnostics frames (APP_MONITORING 0x70).
|
|
/// Emits continuously while diagnostics are running.
|
|
/// </summary>
|
|
IObservable<DiagnosticsFrame> DiagnosticsFrames { get; }
|
|
|
|
bool DiagnosticsRunning { get; }
|
|
|
|
/// <summary>Sends APP_MON_START and begins emitting DiagnosticsFrames.</summary>
|
|
Task StartDiagnosticsAsync(CancellationToken ct = default);
|
|
|
|
/// <summary>
|
|
/// Sends APP_MON_STOP and waits for silence before returning.
|
|
/// Safe to call even if diagnostics aren't running.
|
|
/// </summary>
|
|
Task StopDiagnosticsAsync(CancellationToken ct = default);
|
|
|
|
// ── Keepalive ────────────────────────────────────────────────────────────
|
|
|
|
/// <summary>
|
|
/// Called by the keepalive timer. Sends APP_READ_INFO if idle for > 15s.
|
|
/// Internally managed — exposed for test visibility only.
|
|
/// </summary>
|
|
void OnKeepaliveTimer();
|
|
}
|