diff --git a/docs/logitech-support.md b/docs/logitech-support.md index 611fcf2..68352d6 100644 --- a/docs/logitech-support.md +++ b/docs/logitech-support.md @@ -93,8 +93,7 @@ status]`, read with fn 0) when neither `0x1004` nor `0x1001` is advertised. Fn inversion (`0x40A0`, or `0x40A2` when that is absent) reads bit 0 of fn 0 and writes `[state]` with fn 1, verified by a read-back. State 1 means the top row sends media keys without Fn and F1–F12 need Fn; the K400 Plus reads -`01 01` (state, then power-on default) after a power cycle. `0x40A3` (K375s-style -Fn swap with a `0xFF` prefix) is not implemented. +`01 01` (state, then power-on default) after a power cycle. A K400 Plus (wpid `0x404D`, HID++ 4.1) on a `0xC52B` receiver was probed read-only and then driven through OpenMouse. It advertises `0x0003`, `0x0005`, @@ -139,6 +138,19 @@ only. Battery was read at 90% over `0x1000`. - `0x2005` (swap button cancel) and `0x6100` (raw touchpad reports) have no setting here. +### MX Keys (original) + +The original MX Keys identifies as `0xB35B` over Bluetooth LE and `0x408A` +through a Unifying receiver, according to [Solaar's MX Keys hardware report](https://github.com/pwr-Solaar/Solaar/issues/979). +Its battery uses `0x1000`. The [multi-host Fn inversion feature](https://openlogi.org/hidpp/features/x40a3-fn-inversion-multi-host) +(`0x40A3`) reads with fn 0 `[0xFF, 0, 0]` and writes with fn 1 +`[0xFF, state, 0]` for the current Easy-Switch host. The reply's second byte +is the state, which the driver reads back after a write. + +Both connection paths are covered by simulated driver tests. The catalog +entry is unverified on hardware through OpenMouse. MX Keys S and Mini use +different device IDs and are not included. + ## Transport HID++ 2.0 rides on usage page `0xFF00` (USB) or `0xFF43` (Bluetooth). Requests @@ -147,9 +159,8 @@ are output reports on report id `0x10` (short, three parameters) or `0x11` the device index is `0xFF` for a directly attached keyboard. Replies arrive as input reports and are matched by device index, feature index and function. -Product ids are restricted to the keyboard rows in G HUB's catalog so the same -predicate cannot claim a Logitech mouse or headset, which speak the same -transport. +Product ids are restricted to catalogued keyboards so the same predicate +cannot claim a Logitech mouse or headset, which speak the same transport. ## Implemented @@ -157,8 +168,8 @@ transport. | --- | --- | --- | --- | | Identity | `0x0003` | `getDeviceInfo`, `getFwInfo`, `getSerialNumber` | Unit id, transport bits, model ids, firmware version, serial when advertised. | | Battery | `0x1004` | `getBatteryCapabilities`, `getBatteryInfo` | Percentage only when the gauge capability is set; otherwise the coarse level. | -| Battery (legacy) | `0x1001`, `0x1000` | `getBatteryVoltage`, `getBatteryLevelStatus` | Voltage curve for `0x1001`; reported percentage and charge status for `0x1000` (K400 family). | -| Fn inversion | `0x40A0`, `0x40A2` | `getFnInversion`, `setFnInversion` | Reads and toggles whether F1–F12 or the media keys are the default, with read-back. | +| Battery (legacy) | `0x1001`, `0x1000` | `getBatteryVoltage`, `getBatteryLevelStatus` | Voltage curve for `0x1001`; reported percentage and charge status for `0x1000`. | +| Fn inversion | `0x40A0`, `0x40A2`, `0x40A3` | `getFnInversion`, `setFnInversion` | Reads and toggles whether F1–F12 or the media keys are the default, with read-back. Multi-host devices target the current Easy-Switch host. | | Unifying pairing | Receiver register `0xB5` | HID++ 1.0 long register read | Names a paired keyboard by wpid when it lacks `0x0003`. | | Polling rate | `0x8061` | `getActualReportRateList`, `getReportRate`, `setReportRate` | Reads the advertised mask, writes a discriminant and verifies the read-back. | | Brightness and illumination | `0x8040` | functions 0–4 | Reads the range and current brightness; writes big-endian brightness and verifies it. When the illumination capability is advertised, reads and toggles backlight power independently, with read-back. | diff --git a/src/drivers/logitech/hid.test.ts b/src/drivers/logitech/hid.test.ts index cbf339b..d53c18b 100644 --- a/src/drivers/logitech/hid.test.ts +++ b/src/drivers/logitech/hid.test.ts @@ -254,6 +254,91 @@ class FakeLogitechKeyboard { } } +class FakeMxKeys extends FakeLogitechKeyboard { + override productId = 0xb35b; + override productName = "MX Keys"; + override collections = [{ usagePage: 0xff43, usage: 1, inputReports: [], outputReports: [], featureReports: [], children: [] }]; + fnInverted = false; + ignoreFnWrite = false; + + override reply(featureIndex: number, functionId: number, params: number[]): DataView { + if (featureIndex === 0 && functionId === 0) { + const featureId = ((params[0] ?? 0) << 8) | (params[1] ?? 0); + const index = new Map([[0x0003, 1], [0x1000, 2], [0x40a3, 3]]).get(featureId) ?? 0; + return response(0, 0, [index, 0, 0], this.deviceIndex); + } + if (featureIndex === 1 && functionId === 0) { + // Synthetic identity, with the original MX Keys' Bluetooth and Unifying IDs. + return response(1, 0, [1, 0, 0, 0, 0, 0, 0x06, 0xb3, 0x5b, 0x40, 0x8a, 0, 0, 0, 0], this.deviceIndex); + } + if (featureIndex === 2 && functionId === 0) return response(2, 0, [50, 20, 0], this.deviceIndex); + if (featureIndex === 3) { + assert.equal(params[0], 0xff, "request must address the current host"); + if (functionId === 1 && !this.ignoreFnWrite) this.fnInverted = params[1] === 1; + return response(3, functionId, [1, Number(this.fnInverted), 1, 1], this.deviceIndex); + } + return super.reply(featureIndex, functionId, params); + } +} + +test("MX Keys is detected directly over Bluetooth and reads battery and host Fn state", async () => { + const device = new FakeMxKeys() as unknown as HIDDevice; + assert.equal(LogitechKeyboardHidClient.isSupported(device), true); + const client = new LogitechKeyboardHidClient(device); + const status = await client.readStatus(); + assert.equal(status.name, "MX Keys"); + assert.equal(status.connectionType, "Wireless"); + assert.equal(status.logitech?.productId, 0xb35b); + assert.equal(status.logitech?.battery?.percent, 50); + assert.equal(status.logitech?.fnInversion, false); + await client.close(); +}); + +test("MX Keys is detected behind a Unifying receiver and changes Fn state for the current host", async () => { + const device = new FakeMxKeys(); + device.productId = 0xc52b; + device.deviceIndex = 2; + device.collections = [{ ...device.collections[0]!, usagePage: 0xff00, usage: 2 }]; + const client = new LogitechKeyboardHidClient(device as unknown as HIDDevice); + const status = await client.readStatus(); + assert.equal(status.name, "MX Keys"); + assert.equal(status.connectionDetail, "Unifying receiver"); + assert.deepEqual(status.logitech?.modelIds, [0xb35b, 0x408a]); + assert.equal(await client.setFnInversion(true), true); + assert.equal(await client.setFnInversion(false), false); + const writes = device.writes.filter(packet => packet.featureIndex === 3 && packet.functionId === 1); + assert.deepEqual(writes.map(packet => packet.params.slice(0, 3)), [[0xff, 1, 0], [0xff, 0, 0]]); + await client.close(); +}); + +test("MX Keys reports a multi-host Fn write that did not take effect", async () => { + const device = new FakeMxKeys(); + device.ignoreFnWrite = true; + const client = new LogitechKeyboardHidClient(device as unknown as HIDDevice); + await assert.rejects(client.setFnInversion(true), /did not confirm/); + await client.close(); +}); + +test("MX Keys is identified by its wireless product id on a shared Unifying receiver", async () => { + const device = new FakeMxKeys(); + device.productId = 0xc52b; + device.deviceIndex = 2; + const client = new LogitechKeyboardHidClient(device as unknown as HIDDevice, { + slot: 2, + productId: 0x408a, + longReports: true, + open: () => device.open(), + async request(_reportId, body) { + assert.equal(body[0], 2); + return new Uint8Array(device.reply(body[1]!, body[2]! >> 4, [...body.subarray(3)]).buffer); + }, + }); + const status = await client.readStatus(); + assert.equal(status.name, "MX Keys"); + assert.equal(status.logitech?.productId, 0x408a); + await client.close(); +}); + test("the Logitech client reads identity, battery, report rate and brightness over HID++", async () => { const device = new FakeLogitechKeyboard(); const client = new LogitechKeyboardHidClient(device as unknown as HIDDevice); diff --git a/src/drivers/logitech/hid.ts b/src/drivers/logitech/hid.ts index c414084..9438611 100644 --- a/src/drivers/logitech/hid.ts +++ b/src/drivers/logitech/hid.ts @@ -519,22 +519,26 @@ export class LogitechKeyboardHidClient { // Older boards (G915 and siblings) expose only the voltage feature. const voltageIndex = await this.getFeature(LOGITECH_FEATURE.batteryVoltage); if (voltageIndex) return decodeLogitechBatteryVoltage(await this.request(voltageIndex, 0x00, [0x00, 0x00, 0x00])); - // Unifying office keyboards (K400 family) report the original batteryStatus. + // Office keyboards such as the K400 and MX Keys report the original batteryStatus. const statusIndex = await this.getFeature(LOGITECH_FEATURE.batteryStatus); if (!statusIndex) return null; return decodeLogitechBatteryStatus(await this.request(statusIndex, 0x00, [0x00, 0x00, 0x00])); } - /** The advertised Fn-inversion feature index, preferring the original `0x40A0`. */ - private async fnInversionIndex(): Promise { - return (await this.getFeature(LOGITECH_FEATURE.fnInversion)) - || this.getFeature(LOGITECH_FEATURE.newFnInversion); + private async fnInversionFeature(): Promise<{ index: number; multiHost: boolean }> { + const index = (await this.getFeature(LOGITECH_FEATURE.fnInversion)) + || (await this.getFeature(LOGITECH_FEATURE.newFnInversion)); + if (index) return { index, multiHost: false }; + return { index: await this.getFeature(LOGITECH_FEATURE.multiHostFnInversion), multiHost: true }; } private async readFnInversion(): Promise { - const index = await this.fnInversionIndex(); + const { index, multiHost } = await this.fnInversionFeature(); if (!index) return null; - return decodeLogitechFnInversion(await this.request(index, 0x00, [0x00, 0x00, 0x00])); + // 0x40A3 addresses the current Easy-Switch host with 0xFF and returns + // the host index before the inversion state. + const params = await this.request(index, 0x00, [multiHost ? 0xff : 0x00, 0x00, 0x00]); + return decodeLogitechFnInversion(multiHost ? params.subarray(1) : params); } private async readDisabledKeys(): Promise { @@ -671,15 +675,16 @@ export class LogitechKeyboardHidClient { } /** - * Make F1–F12 the default (`true`) or the media keys (`false`). Writes + * Make the media keys the default (`true`) or F1–F12 (`false`). Writes * `setFnInversion` and confirms the change with a read-back. */ setFnInversion(inverted: boolean): Promise { return this.serialized(async () => { await this.open(); - const index = await this.fnInversionIndex(); + const { index, multiHost } = await this.fnInversionFeature(); if (!index) throw new Error("This Logitech keyboard does not expose an Fn-inversion feature."); - await this.request(index, 0x01, [inverted ? 0x01 : 0x00, 0x00, 0x00]); + const state = inverted ? 0x01 : 0x00; + await this.request(index, 0x01, multiHost ? [0xff, state, 0x00] : [state, 0x00, 0x00]); const confirmed = await this.readFnInversion(); if (confirmed !== inverted) throw new Error("The Logitech keyboard did not confirm the Fn-inversion change."); return confirmed; diff --git a/src/logitech/index.ts b/src/logitech/index.ts index 524a2a1..4e37bd7 100644 --- a/src/logitech/index.ts +++ b/src/logitech/index.ts @@ -34,6 +34,7 @@ export const LOGITECH_FEATURE = { unifiedBattery: 0x1004, fnInversion: 0x40a0, newFnInversion: 0x40a2, + multiHostFnInversion: 0x40a3, reprogControls: 0x1b04, analogKeys: 0x1b08, controlList: 0x1b10, @@ -118,6 +119,9 @@ export const LOGITECH_KEYBOARD_PRODUCTS: ReadonlyMap