# HID "Report ID before the first Collection" hardware repro

Hardware repro for Chromium issue
[574273446](https://issues.chromium.org/issues/574273446) -- WebHID ignores a
Report ID item that appears before the first Collection item.

Fix CL: <https://chromium-review.googlesource.com/c/chromium/src/+/8669861>

Sampler page (host side):
<https://static.januschka.com/i-574273446/index.html>

## What the firmware does

It exposes a single USB HID interface whose report descriptor puts the
`Report ID (1)` item **before** the first `Collection (Application)` item:

```c
0x85, 0x01,        // Report ID (1)              <-- before any collection
0x06, 0x00, 0xFF,  // Usage Page (Vendor 0xFF00)
0x09, 0x01,        // Usage (0x01)
0xA1, 0x01,        // Collection (Application)
0x15, 0x00,        //   Logical Minimum (0)
0x26, 0xFF, 0x00,  //   Logical Maximum (255)
0x75, 0x08,        //   Report Size (8)
0x95, 0x02,        //   Report Count (2)
0x09, 0x02,        //   Usage (0x02)
0x81, 0x02,        //   Input (Data,Var,Abs)
0x09, 0x03,        //   Usage (0x03)
0x91, 0x02,        //   Output (Data,Var,Abs)
0xC0               // End Collection
```

A vendor-defined usage page is used on purpose: the descriptor in the issue
report uses `Usage (Keyboard)`, and WebHID always protects the input/output
reports of a top-level keyboard collection, so the reports would be
unreadable from a page regardless of this bug.

Behavior:

- sends a 2-byte input report (`[counter, 0x5A]`) with report ID 1 every 500 ms
- prints every output report it receives to the USB CDC serial console

## Boards

| Sketch | Board | Notes |
| --- | --- | --- |
| `feather_nrf52840/feather_nrf52840.ino` | Adafruit Feather nRF52840 Express | Primary. Native USB via `Adafruit_TinyUSB`, raw report descriptor. |
| `esp32s3/esp32s3.ino` | ESP32-S3 (or S2) devkit | Secondary cross-check. Needs Arduino-ESP32 core 3.x (`USBHIDDevice`). Plain ESP32/WROOM cannot do USB device mode -- do not use it. |

## Flashing: Feather nRF52840

1. Arduino IDE -> Boards Manager -> install **Adafruit nRF52** core.
2. Tools -> Board -> **Adafruit Feather nRF52840 Express**.
3. Library Manager -> install **Adafruit TinyUSB Library** (bundled with the
   core; make sure Tools -> "USB Stack" is set to *TinyUSB* if the menu exists).
4. Open `feather_nrf52840/feather_nrf52840.ino`, Upload.
5. Double-tap reset if the bootloader is needed (board shows up as `FTHR840BOOT`).

After reset the device enumerates as `HID ReportID Repro` (VID `0x239A`).

## Flashing: ESP32-S3

1. Boards Manager -> **esp32 by Espressif**, 3.x.
2. Tools -> Board -> **ESP32S3 Dev Module**; Tools -> **USB CDC On Boot: Enabled**,
   **USB Mode: USB-OTG (TinyUSB)**.
3. Open `esp32s3/esp32s3.ino`, Upload, then plug into the *native* USB port
   (not the UART bridge port).

Enumerates with VID `0x303A`.

## Linux: let Chrome open the device

`hidraw` nodes are root-only by default. Install the udev rule once:

```bash
sudo cp 99-hid-reportid-repro.rules /etc/udev/rules.d/
sudo udevadm control --reload-rules && sudo udevadm trigger
```

Then replug the board and check:

```bash
ls -l /dev/hidraw*
# descriptor as seen by the kernel (sanity check, independent of Chrome):
sudo usbhid-dump -d 239a | head
```

## Test procedure

Open <https://static.januschka.com/i-574273446/index.html> and click
*Connect device*. The page prints a verdict; run it twice:

1. **Stock Chrome** (any current channel) -> expected FAIL
2. **Patched build** -> expected PASS

```bash
# patched build (branch i-574273446, CL 8669861)
cd ~/chromium_3/src
export PATH=~/depot_tools:/usr/local/bin:$PATH
export SISO_CREDENTIAL_HELPER=gcloud
autoninja -C out/Default chrome
export DISPLAY=$(ps -o user=,args= -C Xorg 2>/dev/null \
  | awk '$1=="chrome" && /chrome_remote_desktop/ { for (i=1;i<=NF;i++) if ($i ~ /^:[0-9]+$/) { print $i; exit } }')
out/Default/chrome --user-data-dir=/tmp/hid-repro \
  https://static.januschka.com/i-574273446/index.html
```

### Expected results

| Check | Stock Chrome | Patched |
| --- | --- | --- |
| `collections[0].inputReports[0].reportId` | `0` | `1` |
| `collections[0].outputReports[0].reportId` | `0` | `1` |
| `sendReport(1, ...)` | rejects | resolves, firmware logs the bytes |
| `sendReport(0, ...)` | resolves (wrong report) | rejects |
| `inputreport` event `.reportId` | `0` | `1` |

The `sendReport` rows are the strongest signal: on stock Chrome the device is
treated as not using report IDs, so the only ID the page is allowed to use is
the wrong one.
