using System.Collections.Concurrent;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Text.Json;
using LogiBoardToolBox.Core.Onboard;
using LogiBoardToolBox.Core.Windows;
/// Short firmware-executed F24 probe; existing bindings are never changed.
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
internal static class SelfTriggeredKeyboardExercise
{
public static async Task RunAsync(LogitechDevice device, string folder, bool nativeHeader = false, bool standardFunctionKey = false, byte holdMs = 10, bool keyboardPresence = false, bool releaseKeyboard = false, bool standaloneReceiver = false, bool hostMode = false, bool activeSafe = false, bool firstFunctionKey = false, bool completeConfiguration = false, bool activeKeyboardPresence = false)
{
if (activeSafe && (nativeHeader || standardFunctionKey || keyboardPresence || releaseKeyboard || standaloneReceiver || hostMode))
throw new ArgumentException("免空闲对照仅允许独立功能键测试。");
standardFunctionKey |= nativeHeader;
if (device.Identity != "046D:C547:C972BA47:01") throw new InvalidOperationException("仅测试已核对的目标鼠标。");
Directory.CreateDirectory(folder);
if (File.Exists(Path.Combine(folder, "prepared.json"))) throw new IOException("诊断目录已经使用。");
using var client = LogitechDiscovery.Open(device);
var trace = new ConcurrentQueue(); client.Trace = trace.Enqueue;
var memory = new OnboardMemory(client, device.Identity, device.Name);
var before = await memory.ReadAsync();
if (before.Mode != 1 || before.ActiveSector != 1 || before.ReadErrors.Count != 0)
throw new InvalidOperationException("当前状态不适合无干扰诊断。");
await OnboardMemory.SaveBackupAsync(before, Path.Combine(folder, "before.json"));
const int sector = 6, offset = 128;
// Known native macro sector; use only its unreferenced erased tail. The
// two-byte entry alignment and original CRC convention are retained.
if (!before.Sectors[sector].AsSpan(offset, 48).ToArray().All(b => b == 0xFF))
throw new InvalidOperationException("测试位置不是空白。");
foreach (var profile in before.GetProfiles())
for (var layer = 0; layer < 2; layer++)
for (var button = 0; button < before.Capabilities.BindingRecordCount; button++)
{
var binding = profile.GetBinding(button, layer == 1);
if (ButtonBinding.IsMacro(binding) && ButtonBinding.MacroSector(binding) == sector
&& ButtonBinding.MacroOffset(binding) >= offset)
throw new InvalidOperationException("现有绑定指向测试区。");
}
// Existing target programs contain no jump into their erased tail.
if (before.Sectors[sector].AsSpan(0, offset).Contains((byte)0x60))
throw new InvalidOperationException("存在需要另行分析的宏跳转。");
var candidate = (byte[])before.Sectors[sector].Clone();
var usage = (byte)(firstFunctionKey ? 0x3A : standardFunctionKey ? 0x41 : 0x73); // F1 / F8 / F24
var virtualKey = firstFunctionKey ? 0x70 : standardFunctionKey ? 0x77 : 0x87;
var keyName = firstFunctionKey ? "F1" : standardFunctionKey ? "F8" : "F24";
byte[] code = [0x43, 0, usage, 0x40, 0, holdMs, 0x44, 0, usage, 0x40, 0, 10, 0xFF, 0xFF];
code.CopyTo(candidate, offset);
// Consumer Control usage 1 is the collection itself, with no assigned
// OS command; zero movement likewise leaves pointer and wheel unchanged.
byte[] consumerControl = [0x45, 0, 1, 0x40, 0, 100, 0x46, 0, 1, 0xFF];
consumerControl.CopyTo(candidate, 144);
byte[] zeroMovement = [0x61, 0, 0, 0, 0, 0xFF];
zeroMovement.CopyTo(candidate, 160);
candidate = SectorChecksum.Seal(candidate);
var changes = new List { new(sector, before.Sectors[sector], candidate, "临时功能键单次诊断,不绑定实体键") };
if (releaseKeyboard)
{
var referenced = MacroAllocator.FindReferencedSectors(before);
var releases = Enumerable.Range(4, 112).Select(key => new byte[] { 0x44, 0, (byte)key })
.Concat(Enumerable.Range(0, 8).Select(bit => new byte[] { 0x44, (byte)(1 << bit), (byte)(0xE0 + bit) })).ToArray();
for (var part = 0; part < 2; part++)
{
var page = 9 + part;
if (referenced.Contains(page) || !before.Sectors[page].All(b => b == 0xFF))
throw new InvalidOperationException("清理程序所需的临时扇区不是空白或已被引用。");
var data = Enumerable.Repeat((byte)0xFF, before.Capabilities.SectorSize).ToArray();
releases.Skip(part * 60).Take(60).SelectMany(b => b).ToArray().CopyTo(data, 0);
SectorChecksum.Seal(data);
changes.Insert(part, new(page, before.Sectors[page], data, "临时纯松开指令,结束恢复空白扇区"));
}
}
if (nativeHeader)
{
var native = await OnboardMemory.LoadBackupAsync(Path.GetFullPath("work/ghub-binding-fix/native-ghub-mode/native-once-final-encoded.json"));
if (native.DeviceIdentity != before.DeviceIdentity || !native.Description.Take(11).SequenceEqual(before.Description.Take(11)))
throw new InvalidDataException("原生对照不属于目标设备。");
var profile = (byte[])before.Sectors[before.ActiveSector].Clone();
native.Sectors[3].AsSpan(22, 4).CopyTo(profile.AsSpan(22, 4));
SectorChecksum.Seal(profile);
changes.Add(new(before.ActiveSector, before.Sectors[before.ActiveSector], profile, "仅对照原生头字段 22–25,结束恢复"));
}
if (keyboardPresence)
{
var profile = before.GetProfiles().Single(p => p.Sector == before.ActiveSector).Copy();
for (var button = 0; button < before.Capabilities.BindingRecordCount; button++)
{
var binding = profile.GetBinding(button);
if (binding[0] == 0x90 && binding[1] == 11)
throw new InvalidOperationException("普通层存在 G-Shift 入口,不能用此无入口层作对照。");
}
profile.SetBinding(10, true, ButtonBinding.Keyboard(0, 0x73));
changes.Add(new(before.ActiveSector, before.Sectors[before.ActiveSector], profile.ToBytes(), "仅在无法进入的 G-Shift 层对照直接键盘绑定,结束恢复"));
}
if (activeKeyboardPresence)
{
var profile = before.GetProfiles().Single(p => p.Sector == before.ActiveSector).Copy();
if (!profile.GetBinding(4).SequenceEqual(ButtonBinding.Macro(8, 0))) throw new InvalidOperationException("取消刀实体键的现有绑定与预期不符。");
profile.SetBinding(4, false, ButtonBinding.Keyboard(0, 0x73));
changes.Add(new(before.ActiveSector, before.Sectors[before.ActiveSector], profile.ToBytes(), "临时将故障的 G5 设为 F24,结束恢复原取消刀"));
}
var plan = new MemoryWritePlan(before, changes, before.ActiveSector, "后台短时固件诊断");
await Save("prepared.json", new { device.Identity, Sector = sector, Offset = offset, Bytecode = Convert.ToHexString(code),
PrimaryBindingsChanged = activeKeyboardPresence, ActiveKeyboardPresence=activeKeyboardPresence, InactiveGShiftProbeBinding = keyboardPresence, ReleaseOnlyRecovery = releaseKeyboard, NativeHeaderFields = nativeHeader, NativeExecutionFunction = "8100/9", TestVirtualKey = virtualKey,
NormalKeysIntercepted = false, MouseIntercepted = false, SoftwareInputGeneration = false,
RestoreAllSectorsAtEnd = true, WaitForInputIdle = !activeSafe });
var deadline = DateTime.UtcNow.AddSeconds(60);
while (!activeSafe && (IdleMilliseconds() < 5000 || AnyInputHeld()))
{
if (DateTime.UtcNow >= deadline) { await Save("skipped.json", new { Reason = "No idle interval", NoDeviceChanges = true }); return; }
await Task.Delay(250);
}
var touched = false;
var results = new List