Files
TuneBossPC/TuneBoss.Core/Models/EcuInfo.cs
T
2026-09-28 16:55:31 +08:00

191 lines
9.4 KiB
C#
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.
namespace TuneBoss.Core.Models;
/// <summary>
/// Parsed response from APP_READ_INFO (0x37).
/// Immutable — constructed once per successful read.
/// </summary>
public sealed class EcuInfo
{
// ── Identity ─────────────────────────────────────────────────────────────
public string Username { get; init; } = string.Empty;
public string UniqueId { get; init; } = string.Empty; // "AABBCCDD-EEFF0011-..."
// ── Version ──────────────────────────────────────────────────────────────
public byte FwVersion { get; init; }
public byte HwVersion { get; init; }
public byte FwVersion2 { get; init; } // Only present if buffer > 68
public byte FwSubVersion2 { get; init; }
public byte TableVersion { get; init; }
public byte HwComponentVersion { get; init; }
public byte AppInfoVersion { get; init; } // Generation discriminator
// ── Bike identity ────────────────────────────────────────────────────────
public byte BikeManufacturer { get; init; } // 1=Yamaha 2=Honda 3=Kawasaki 6=Suzuki
public byte BikeModel { get; init; }
// ── Usage counters ───────────────────────────────────────────────────────
public uint FlashWriteCount { get; init; }
public uint SwitchToggleCount { get; init; }
public byte PwdResetCount { get; init; }
// ── Dates ────────────────────────────────────────────────────────────────
public DateOnly? ProgrammingDate { get; init; }
public DateOnly? FirstUseDate { get; init; }
public byte FirstUseInfo0 { get; init; } // raw buf[32] (FirstUseDate DD)
public byte FirstUseInfo1 { get; init; } // raw buf[33] — TPS cal-done flag (Android UIfirstUseDate[1])
public DateOnly? FirstFlashDate { get; init; }
// ── Upgrade flags ────────────────────────────────────────────────────────
public byte UpgradeRpmMax { get; init; } // × 100 = actual RPM limit
public byte UpgradeShiftAuto { get; init; }
public byte UpgradeLogImmo { get; init; } // low nibble = legacy Upgrade_Stage3 (see Stage)
public byte UpgradeFuture12 { get; init; }
public byte UpgradeFuture2 { get; init; } // only if buffer > 68
// ── Derived ──────────────────────────────────────────────────────────────
public int RpmLimit => UpgradeRpmMax * 100;
/// <summary>
/// Dual injector is a Stage 3 feature — the same nibble the Android app reads as
/// <c>Upgrade_Stage3</c>, so it follows the stage rather than being an independent flag.
/// </summary>
public bool IsDualInjectorUnlocked => Stage >= 3;
/// <summary>True once the throttle (TPS) calibration has been completed on this unit.
/// The firmware/Android app stores the flag in the second first-use byte
/// (UIfirstUseDate[1] = buf[33]), not the first.</summary>
public bool HasTpsCalibration => FirstUseInfo1 == TpsCalibration.CalDoneFlag;
/// <summary>
/// Legacy <c>Upgrade_Stage2</c> — high nibble of the shift/stage byte
/// (legacy <c>buf[offset+42] &amp; 0xF0</c>, whose low nibble is Shift Auto).
/// </summary>
public byte UpgradeStage2 => (byte)(UpgradeShiftAuto & 0xF0);
/// <summary>
/// Legacy <c>Upgrade_Stage3</c> — low nibble of the next byte
/// (legacy <c>buf[offset+43] &amp; 0x0F</c>; the WPF parser stores that byte
/// as <see cref="UpgradeLogImmo"/>).
/// </summary>
public byte UpgradeStage3 => (byte)(UpgradeLogImmo & 0x0F);
/// <summary>
/// ECU stage — 1, 2 or 3. Mirrors the Android app's precedence exactly:
/// Stage 3 wins if its nibble is set, else Stage 2, else Stage 1.
/// Stage 1 does not expose the user 24×8 adjust tables (only the subtables).
/// </summary>
public int Stage =>
UpgradeStage3 > 0 ? 3 :
UpgradeStage2 > 0 ? 2 : 1;
/// <summary>True when the stage exposes the user 24×8 adjust tables (Stage 2+).</summary>
public bool HasUserAdjustTables => Stage >= 2;
// ── Parser ───────────────────────────────────────────────────────────────
/// <summary>
/// Parses a raw APP_READ_INFO response buffer.
/// Buffer[0] must be APP_CMD_SUCCESS (0x51); payload starts at byte 4.
/// </summary>
public static EcuInfo Parse(byte[] buf)
{
if (buf[0] != 0x51)
throw new InvalidOperationException($"Expected APP_CMD_SUCCESS (0x51), got 0x{buf[0]:X2}");
const int payloadStart = 4;
const int fieldStart = payloadStart + 16; // after 16-byte username
// Username (16 bytes, null-terminated ASCII)
var username = System.Text.Encoding.ASCII
.GetString(buf, payloadStart, 16)
.TrimEnd('\0');
// Little-endian uint32 helper
uint ReadU32(int offset) =>
buf[offset] |
((uint)buf[offset + 1] << 8) |
((uint)buf[offset + 2] << 16) |
((uint)buf[offset + 3] << 24);
var flashWriteCount = ReadU32(fieldStart);
var switchToggleCount = ReadU32(fieldStart + 4);
// Dates — DD=0 means "Never"
// Day/Month/Year(+2000) as raw bytes. DD==0 means "Never". Some units carry
// uninitialised or garbage bytes in unused date slots (e.g. month 0 or 14,
// day 31 in a 30-day month); those must NOT throw and abort the whole info
// read — treat any non-calendar value as "not set" (null).
DateOnly? ParseDate(int offset)
{
int day = buf[offset], month = buf[offset + 1], year = 2000 + buf[offset + 2];
if (day == 0) return null;
if (month < 1 || month > 12) return null;
if (day > DateTime.DaysInMonth(year, month)) return null;
return new DateOnly(year, month, day);
}
var progDate = ParseDate(fieldStart + 8);
var firstUseDate = ParseDate(fieldStart + 12);
var firstUseInfo0 = buf[fieldStart + 12]; // DD slot (first-use date day)
var firstUseInfo1 = buf[fieldStart + 13]; // MM slot — doubles as the TPS cal-done flag
var firstFlashDate = ParseDate(fieldStart + 16);
// UniqueID — 16 bytes formatted as 4×4 hex groups
var uid = buf.Skip(fieldStart + 20).Take(16).ToArray();
var uniqueId = $"{Hex(uid, 0, 4)}-{Hex(uid, 4, 4)}-{Hex(uid, 8, 4)}-{Hex(uid, 12, 4)}";
int o = fieldStart + 36; // after UniqueID
var fwVersion = buf[o];
var hwVersion = buf[o + 1];
var bikeMfr = buf[o + 2];
var bikeModel = buf[o + 3];
var upgradeRpmMax = buf[o + 4];
var upgradeShift = buf[o + 5];
var upgradeLogImmo = buf[o + 6];
var upgradeFuture12 = buf[o + 7];
byte upgradeFuture2 = 0, fw2 = 0, fw2sub = 0, tableVer = 0, hwComp = 0;
if (buf.Length > 68)
{
upgradeFuture2 = buf[o + 8];
fw2 = buf[o + 9];
fw2sub = buf[o + 10];
tableVer = buf[o + 11];
hwComp = buf[o + 12];
}
var appInfoVersion = buf[^1]; // last byte
var pwdRstCount = buf[^5]; // 4 bytes from end
return new EcuInfo
{
Username = username,
UniqueId = uniqueId,
FwVersion = fwVersion,
HwVersion = hwVersion,
FwVersion2 = fw2,
FwSubVersion2 = fw2sub,
TableVersion = tableVer,
HwComponentVersion = hwComp,
AppInfoVersion = appInfoVersion,
BikeManufacturer = bikeMfr,
BikeModel = bikeModel,
FlashWriteCount = flashWriteCount,
SwitchToggleCount = switchToggleCount,
PwdResetCount = pwdRstCount,
ProgrammingDate = progDate,
FirstUseDate = firstUseDate,
FirstUseInfo0 = firstUseInfo0,
FirstUseInfo1 = firstUseInfo1,
FirstFlashDate = firstFlashDate,
UpgradeRpmMax = upgradeRpmMax,
UpgradeShiftAuto = upgradeShift,
UpgradeLogImmo = upgradeLogImmo,
UpgradeFuture12 = upgradeFuture12,
UpgradeFuture2 = upgradeFuture2,
};
}
private static string Hex(byte[] src, int start, int count) =>
string.Concat(src.Skip(start).Take(count).Select(b => $"{b:X2}"));
}