WICG / webusb

Connecting hardware to the web.
https://wicg.github.io/webusb/
Other
1.3k stars 126 forks source link

Integrate with Serial API and WebHID #186

Open reillyeon opened 4 years ago

reillyeon commented 4 years ago

The Serial and WebHID APIs are defining higher-level interfaces to USB devices implementing the CDC-ACM and HID interface classes as well as vendor-specific interfaces which are exposed through the host operating system's serial and HID APIs.

Due to variations on operating system configuration the drivers which translate between USB and these higher-level APIs may not be present. In this case a developer can implement this driver in JavaScript using the WebUSB API. It would therefore be convenient for developers to specify when requesting permission to access a device that they will accept either the high-level interface (such as SerialPort or HIDDevice) or the low-level interface, USBDevice.

Phrased in terms of the Permissions API using the proposed requestAll() method like this,

navigator.permissions.requestAll([
    {name: "usb", filters: [...]},
    {name: "serial", filters: [...]}
]);

Alternatively the requestDevice() method could be extended to optionally return instances of SerialPort or HIDDevice when requested like this,

navigator.usb.requestDevice({
    filters: [...],
    includeSerialPorts: true,
    includeHidDevices: true
});

However, since a USB device may include multiple interfaces implementing these protocols a better integration could be to add serialPorts and hidDevices attributes to the USBDevice object. These would be populated when the browser detects that there is a high-level driver claiming one of the device's interfaces.