All fixes before Bluetooth

This commit is contained in:
2026-10-14 07:12:24 +08:00
parent fbc92d57ba
commit 1ec485701a
13 changed files with 504 additions and 17 deletions
+20
View File
@@ -42,6 +42,18 @@ public sealed class EcuInfo
public byte UpgradeFuture12 { get; init; }
public byte UpgradeFuture2 { get; init; } // only if buffer > 68
// ── Registration / activation ─────────────────────────────────────────────
/// <summary>
/// ECU activation nibble (legacy <c>Reg1_Reg2</c> = <c>buf[offset+53] &amp; 0x0F</c>,
/// present only on longer info buffers / newer firmware). &gt; 0 means the unit has
/// been activated. Used by the post-unlock status gate as the offline / server-error
/// fallback and to rescue "not registered" verdicts (see EcuStatusInterpreter).
/// </summary>
public byte Reg1Reg2 { get; init; }
/// <summary>True when the ECU reports itself activated (<see cref="Reg1Reg2"/> &gt; 0).</summary>
public bool IsActivated => Reg1Reg2 > 0;
// ── Derived ──────────────────────────────────────────────────────────────
public int RpmLimit => UpgradeRpmMax * 100;
/// <summary>
@@ -153,6 +165,13 @@ public sealed class EcuInfo
hwComp = buf[o + 12];
}
// Registration/activation nibble — legacy Reg1_Reg2 = buf[offset+53] & 0x0F.
// The legacy parser maps offset+37 → FW_Version, which is this parser's o, so
// Reg1_Reg2 lands at o+16. Only present on longer buffers (legacy gate: len >= 84);
// absent on older units → 0 (treated as "not activated", but those are FW < 150
// which the status gate passes anyway).
byte reg1Reg2 = buf.Length > o + 16 ? (byte)(buf[o + 16] & 0x0F) : (byte)0;
var appInfoVersion = buf[^1]; // last byte
var pwdRstCount = buf[^5]; // 4 bytes from end
@@ -182,6 +201,7 @@ public sealed class EcuInfo
UpgradeLogImmo = upgradeLogImmo,
UpgradeFuture12 = upgradeFuture12,
UpgradeFuture2 = upgradeFuture2,
Reg1Reg2 = reg1Reg2,
};
}
+153
View File
@@ -0,0 +1,153 @@
namespace TuneBoss.Core.Models;
/// <summary>
/// What the app should do after the post-unlock server status check.
/// Mirrors the Flutter <c>checkEmailStatus</c> gate (doneAfterConnect):
/// Pass → continue into the app,
/// Warn → continue, but show a warning first,
/// Block → show the message, disconnect, do NOT load tables.
/// </summary>
public enum EcuStatusOutcome
{
Pass,
Warn,
Block,
}
/// <summary>
/// Result of the post-unlock ECU status check against the TracBoss account server.
/// </summary>
public sealed record EcuStatusResult(
EcuStatusOutcome Outcome,
string Message,
int? SuccessCode,
bool ServerReachable)
{
public bool IsBlocked => Outcome == EcuStatusOutcome.Block;
}
/// <summary>
/// Pure interpretation of the server's <c>success</c> code into an app outcome.
/// No HTTP or UI here so it can be unit-tested and reused.
///
/// Ported from the Flutter app's <c>checkEmailStatus</c> (lib/services/tb_interactor.dart),
/// MINUS the ECU activation-bit (reg1Reg2) second chance and the global payment/systemSync
/// side effects — those are a later phase. Where the Flutter code relied on reg1Reg2 to
/// rescue a "not activated" verdict, this build hard-blocks instead.
/// </summary>
public static class EcuStatusInterpreter
{
/// <summary>FW below this is a legacy unit that predates activation — always allowed.</summary>
public const int LegacyFwCeiling = 150;
/// <summary>
/// Interpret a definitive server response. Mirrors the legacy Android app's
/// <c>CheckEmailStatus</c> success-code handling, including the ECU activation-bit
/// (<paramref name="isActivated"/>) rescue on the "not registered" verdicts (5 and 0).
/// </summary>
/// <param name="success">The server's <c>success</c> status code.</param>
/// <param name="fwVersion">This unit's firmware version (for the legacy-ECU branch).</param>
/// <param name="isActivated">ECU self-reported activation (<c>Reg1_Reg2 &gt; 0</c>).</param>
/// <param name="serverMessage">Optional human message echoed from the server.</param>
public static EcuStatusResult Interpret(int success, byte fwVersion, bool isActivated,
string? serverMessage = null)
{
string msg = string.IsNullOrWhiteSpace(serverMessage) ? "" : serverMessage!.Trim();
switch (success)
{
// Registered / global / dealer-bypass — full access.
case 1:
case 2:
case 3:
case 8:
return new EcuStatusResult(
EcuStatusOutcome.Pass,
success == 8 ? "Dealer-bypassed ECU — access granted."
: "ECU registered — access granted.",
success, ServerReachable: true);
// Activated but not warranty-registered — allowed, with a warning.
case 6:
case 7:
return new EcuStatusResult(
EcuStatusOutcome.Warn,
"This ECU is activated but not registered for warranty.",
success, ServerReachable: true);
// Phone not registered. Legacy: FW < 150 → pass; else if activated → pass with a
// "password change disabled" warning; else block.
case 5:
if (fwVersion < LegacyFwCeiling)
return new EcuStatusResult(EcuStatusOutcome.Pass,
"Legacy ECU — access granted.", success, ServerReachable: true);
return isActivated
? new EcuStatusResult(EcuStatusOutcome.Warn,
"This ECU is activated but not registered for warranty. "
+ "Password change is disabled.",
success, ServerReachable: true)
: NotActivated(success);
// Unregistered. Legacy: FW < 150 → pass; else if activated → pass; else block.
case 0:
if (fwVersion < LegacyFwCeiling)
return new EcuStatusResult(EcuStatusOutcome.Pass,
"Legacy ECU — access granted.", success, ServerReachable: true);
return isActivated
? new EcuStatusResult(EcuStatusOutcome.Pass,
"ECU activated — access granted.", success, ServerReachable: true)
: NotActivated(success);
// Account removed — warranty void, but access is allowed.
case 9:
return new EcuStatusResult(
EcuStatusOutcome.Warn,
"The account for this ECU has been removed — warranty is void.",
success, ServerReachable: true);
// App out of date (server rejected the request shape).
case 98:
return new EcuStatusResult(
EcuStatusOutcome.Block,
"Missing parameter. Please update TuneBoss PC to the latest version.",
success, ServerReachable: true);
default:
return new EcuStatusResult(
EcuStatusOutcome.Block,
$"Unrecognised ECU status from server (code {success})"
+ (msg.Length > 0 ? $": {msg}" : "."),
success, ServerReachable: true);
}
}
/// <summary>
/// Outcome when the server can't be reached (offline, timeout, or a transport/parse error).
/// Mirrors the legacy Android offline / server-error fallback exactly: FW &lt; 150 passes,
/// an activated ECU (<paramref name="isActivated"/>) passes, and only a genuinely
/// unactivated modern unit is blocked. This is why an activated unit still works offline.
/// </summary>
public static EcuStatusResult Unreachable(byte fwVersion, bool isActivated, string? reason = null)
{
if (fwVersion < LegacyFwCeiling)
return new EcuStatusResult(EcuStatusOutcome.Pass,
"Legacy ECU — access granted (server not contacted).", SuccessCode: null,
ServerReachable: false);
if (isActivated)
return new EcuStatusResult(EcuStatusOutcome.Pass,
"ECU activated — access granted (server not contacted).", SuccessCode: null,
ServerReachable: false);
string why = string.IsNullOrWhiteSpace(reason) ? "" : $" ({reason})";
return new EcuStatusResult(EcuStatusOutcome.Block,
"This ECU has not been activated, and the activation server could not be reached "
+ "to verify it. Please activate the ECU with your dealer." + why,
SuccessCode: null, ServerReachable: false);
}
private static EcuStatusResult NotActivated(int success) => new(
EcuStatusOutcome.Block,
"This ECU has not been activated. Please activate it with your dealer.",
success, ServerReachable: true);
}
+26 -5
View File
@@ -49,6 +49,15 @@ public sealed record MemoryLayout
public MemoryRegion Fuel_Angle2D { get; init; } = MemoryRegion.Unavailable;
public MemoryRegion Secondary_Load_Conversion { get; init; } = MemoryRegion.Unavailable;
// ── Acceleration-compensation 2D tables (FW_Version >= 197) ───────────────
// 228-byte tables in the MAIN 2D address space (base 2D opcodes — NOT user tables),
// at slots 9/10/11 (index × 228): 0x0804 / 0x08E8 / 0x09CC. The firmware allocates
// the cyl2 base tables (slots 5-8) on every unit, so accel comp sits at a fixed
// 9-11 regardless of cylinder count.
public MemoryRegion Accel_Fuel_Comp { get; init; } = MemoryRegion.Unavailable; // 0x0804 (9×228)
public MemoryRegion Accel_Spark_Comp { get; init; } = MemoryRegion.Unavailable; // 0x08E8 (10×228)
public MemoryRegion Accel_FuelAngle_Comp { get; init; } = MemoryRegion.Unavailable; // 0x09CC (11×228)
// ── 2D user-adjustable tables (cyl1) ─────────────────────────────────────
public MemoryRegion Fuel_PulseWidth_Adjust1 { get; init; } = MemoryRegion.Unavailable;
public MemoryRegion Spark_Angle_Adjust1 { get; init; } = MemoryRegion.Unavailable;
@@ -179,11 +188,14 @@ public sealed record MemoryLayout
Fuel_PulseWidth_Adjust1_SubTable = R(0x0018, LenSubTable),
Spark_Angle_Adjust1_SubTable = R(0x0030, LenSubTable),
// Cyl2 2D (zero/unavailable for single cylinder)
Fuel_PulseWidth2 = isTwoCyl ? R(0x063C, Len2DTable) : MemoryRegion.Unavailable,
AFR_Target2 = isTwoCyl ? R(0x0720, LenAFRTable) : MemoryRegion.Unavailable,
Fuel_Angle2D_2 = isTwoCyl ? R(0x0804, Len2DTable) : MemoryRegion.Unavailable,
Spark_Angle2 = isTwoCyl ? R(0x08E8, Len2DTable) : MemoryRegion.Unavailable,
// Cyl2 2D — MAIN space slots 5/6/7/8, matching the firmware's contiguous 2D
// declaration (0x0474/0x0558/0x063C/0x0720). The memory is allocated on EVERY
// unit; single-cylinder just doesn't access it (gated isTwoCyl for the nav).
// (Previously mis-mapped to slots 7-10, which then clashed with accel at 9-11.)
Fuel_PulseWidth2 = isTwoCyl ? R(0x0474, Len2DTable) : MemoryRegion.Unavailable, // idx 5
AFR_Target2 = isTwoCyl ? R(0x0558, LenAFRTable) : MemoryRegion.Unavailable, // idx 6
Fuel_Angle2D_2 = isTwoCyl ? R(0x063C, Len2DTable) : MemoryRegion.Unavailable, // idx 7
Spark_Angle2 = isTwoCyl ? R(0x0720, Len2DTable) : MemoryRegion.Unavailable, // idx 8
Fuel_PulseWidth_Adjust2 = isTwoCyl ? R(0x09CC, Len2DTable) : MemoryRegion.Unavailable,
Spark_Angle_Adjust2 = isTwoCyl ? R(0x0AB0, Len2DTable) : MemoryRegion.Unavailable,
@@ -191,6 +203,13 @@ public sealed record MemoryLayout
AFR_Target2_SubTable = isTwoCyl ? R(0x0048, LenSubTable) : MemoryRegion.Unavailable,
Fuel_PulseWidth_Adjust2_SubTable = isTwoCyl ? R(0x0060, LenSubTable) : MemoryRegion.Unavailable,
Spark_Angle_Adjust2_SubTable = isTwoCyl ? R(0x0078, LenSubTable) : MemoryRegion.Unavailable,
// Acceleration-compensation tables — FW >= 197, MAIN 2D space, slots 9/10/11
// (index × 228): 0x0804 / 0x08E8 / 0x09CC, right after cyl2 Spark_Angle2 (idx 8).
// Present on EVERY unit regardless of cylinder count — the cyl2 slots 5-8 are
// always allocated in firmware, so accel sits at a fixed 9-11 (fw-gate only).
Accel_Fuel_Comp = fw >= 197 ? R(0x0804, Len2DTable) : MemoryRegion.Unavailable,
Accel_Spark_Comp = fw >= 197 ? R(0x08E8, Len2DTable) : MemoryRegion.Unavailable,
Accel_FuelAngle_Comp = fw >= 197 ? R(0x09CC, Len2DTable) : MemoryRegion.Unavailable,
// Misc core
MiscCore = R(0x0000, LenMiscCore),
@@ -228,6 +247,8 @@ public sealed record MemoryLayout
Spark_Angle_Adjust1 = R(0x00E4, Len2DTable),
Fuel_PulseWidth_Adjust2 = isTwoCyl ? R(0x01C8, Len2DTable) : MemoryRegion.Unavailable,
Spark_Angle_Adjust2 = isTwoCyl ? R(0x02AC, Len2DTable) : MemoryRegion.Unavailable,
// Acceleration-compensation tables are inherited from BuildGen2 (MAIN 2D space,
// slots 9/10/11, single-cylinder only) — no GEN3 override needed.
};
}