USBEndpoint - Web APIs 编辑

Secure context

This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

The USBEndpoint interface of the WebUSB API provides information about an endpoint provided by the USB device. An endpoint represents a unidirectional data stream into or out of a device.

Constructor

USBEndpoint.USBEndpoint
Creates a new USBEndpoint object which will be populated with information about the endpoint on the provided USBAltenateInterface with the given endpoint number and transfer direction.

Properties

USBEndpoint.endpointNumber
Returns this endpoint's "endpoint number" which is a value from 1 to 15 extracted from the bEndpointAddress field of the endpoint descriptor defining this endpoint. This value is used to identify the endpoint when calling methods on USBDevice.
USBEndpoint.direction
Returns the direction in which this endpoint transfers data, one of:
  • "in" - Data is transferred from device to host.
  • "out" - Data is transferred from host to device.
USBEndpoint.type
Returns the type of this endpoint, one of:
  • "bulk" - Provides reliable data transfer for large payloads. Data sent through a bulk endpoint is guaranteed to be delivered or generate an error but may be preempted by other data traffic.
  • "interrupt" - Provides reliable data transfer for small payloads. Data sent through an interrupt endpoint is guaranteed to be delivered or generate an error and is also given dedicated bus time for transmission.
  • "isochronous" - Provides unreliable data transfer for payloads that must be delivered periodically. They are given dedicated bus time but if a deadline is missed the data is dropped.
USBEndpoint.packetSize
Returns the size of the packets that data sent through this endpoint will be divided into.

Examples

While sometimes the developer knows ahead of time the exact layout of a device's endpoints there are cases where this must be discovered at runtime. For example, a USB serial device must provide bulk input and output endpoints but their endpoint numbers will depend on what other interfaces the device provides.

This code identifies the correct endpoints by searching for the interface implementing the USB CDC interface class and then identifying the candidate endpoints based on their type and direction.

let inEndpoint = undefined;
let outEndpoint = undefined;

for (const interface of device.configuration.interfaces) {
  // Only support devices with out multiple alternate interfaces.
  const alternate = interface.alternates[0];

  // Identify the interface implementing the USB CDC class.
  const USB_CDC_CLASS = 10;
  if (alternate.interfaceClass != USB_CDC_CLASS) {
    continue;
  }

  for (const endpoint of alternate.endpoints) {
    // Identify the bulk transfer endpoints.
    if (endpoint.type != "bulk") {
      continue;
    }

    if (endpoint.direction == "in") {
      inEndpoint = endpoint.endpointNumber;
    } else if (endpoint.direction == "out") {
      outEndpoint = endpoint.endpointNumber;
    }
  }
}

Specifications

SpecificationStatusComment
WebUSB
The definition of 'USBEndPoint' in that specification.
DraftInitial definition.

Browser compatibility

BCD tables only load in the browser

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:36 次

字数:5984

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文