Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/features/bridge/bridge-hid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,25 @@ test("drives a device end to end over the socket", async () => {
fake.transport.onClose?.();
});

test("a vendor-less filter on a vendor-defined page is sent as a usage, never a standard one", async () => {
const fake = fakeTransport();
const hid = bridgeHid(fake.transport);

const scan = hid.requestDevice({
filters: [{ usagePage: 0xff60, usage: 0x61 }, { usagePage: 0xff60, usage: 0x61 }, { usagePage: 0x01, usage: 0x06 }, { vendorId: 0x3554, usagePage: 0xff02 }],
});
assert.deepEqual(fake.sent[0].usages, [{ usagePage: 0xff60, usage: 0x61 }]);
assert.deepEqual(fake.sent[0].vendorIds, [0x3554]);
fake.reply({ id: fake.sent[0].id, ok: true, devices: [] });
await scan;

const plain = hid.requestDevice({ filters: [{ vendorId: 0x3554 }] });
assert.equal("usages" in fake.sent[1], false);
fake.reply({ id: fake.sent[1].id, ok: true, devices: [] });
await plain;
fake.transport.onClose?.();
});

test("a rejected request surfaces the reason Bridge gave", async () => {
const fake = fakeTransport();
const hid = bridgeHid(fake.transport);
Expand Down
19 changes: 16 additions & 3 deletions src/features/bridge/bridge-hid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ interface Reply {
}

type Command =
| { type: "list"; vendorIds: number[] }
| { type: "list"; vendorIds: number[]; usages?: { usagePage: number; usage?: number }[] }
| { type: "open"; device: string }
| { type: "close"; device: string }
| { type: "listen"; device: string }
Expand Down Expand Up @@ -141,6 +141,19 @@ function vendorIdsFor(filters: HIDDeviceFilter[]): number[] {
return [...ids];
}

const VENDOR_DEFINED_USAGE_PAGE = 0xff00;

function listCommand(filters: HIDDeviceFilter[]): Command {
const usages = new Map<string, { usagePage: number; usage?: number }>();
for (const filter of filters) {
if (filter.vendorId !== undefined || filter.productId !== undefined) continue;
if (filter.usagePage === undefined || filter.usagePage < VENDOR_DEFINED_USAGE_PAGE) continue;
usages.set(`${filter.usagePage}:${filter.usage ?? ""}`, { usagePage: filter.usagePage, usage: filter.usage });
}
const vendorIds = vendorIdsFor(filters);
return usages.size > 0 ? { type: "list", vendorIds, usages: [...usages.values()] } : { type: "list", vendorIds };
}

// Listeners are kept in a set rather than by extending EventTarget: WebHID
// declares a narrower listener type than EventTarget's, which no override can
// satisfy under strict mode. Both other HID adapters in this project (Desktop's
Expand Down Expand Up @@ -467,7 +480,7 @@ class BridgeHid implements HID {
}

async #listDevices(): Promise<HIDDevice[]> {
const reply = await this.#client.request({ type: "list", vendorIds: vendorIdsFor(HID_FILTERS) });
const reply = await this.#client.request(listCommand(HID_FILTERS));
const { devices, added, removed } = this.#client.reconcile(reply.devices ?? []);
if (this.#enumerated) {
for (const device of added) this.#emit("connect", device);
Expand All @@ -479,7 +492,7 @@ class BridgeHid implements HID {
}

async requestDevice(options: { filters: HIDDeviceFilter[] }): Promise<HIDDevice[]> {
const reply = await this.#client.request({ type: "list", vendorIds: vendorIdsFor(options.filters) });
const reply = await this.#client.request(listCommand(options.filters));
const { devices } = this.#client.reconcile(reply.devices ?? []);
this.#enumerated = true;
this.#startPolling();
Expand Down