首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WebHID API如何在不发送输出报告的情况下获取输入报告

WebHID API如何在不发送输出报告的情况下获取输入报告
EN

Stack Overflow用户
提问于 2022-02-21 21:02:33
回答 2查看 302关注 0票数 0

是否有一种使用WebHID API只要求输入报告的方法?API似乎有一个直接读取特性报告的函数,但我不明白为什么要等待事件来听输入报告。根据我对输入报告的理解,主机PC必须触发请求才能得到报告,我认为当我编写输出报告时,它可以触发读取。打开设备也应该触发读取。

代码语言:javascript
复制
const dataView = await device.receiveFeatureReport(/* reportId= */ 1);

因为我知道我试图读取的ReportID不是一个特性报告类型,所以这个函数无法工作。

使用事件侦听方法,在打开设备或将报告写入输出报告后,我在试图获得要触发的输入报告时遇到了问题。该设备是实现USB的STM32L4x设备。编写输出报告没有问题,但我似乎无法触发事件。这件事似乎从未触发过。这篇文章(WEBHID API: Inputreport not triggering with barcode scanner)也有同样的问题,但由于驱动程序的安装需求,我不想切换到使用WebUSB。

这是几个示例使用的相同的事件侦听代码。

代码语言:javascript
复制
if (!device.opened) await device.open();

device.addEventListener("inputreport", event => {
  const { data, device, reportId } = event;

  if (device.productId !== 0x5751) return;

  const value = data.getUint8(0);
  if (value == 0) return;
 
  console.log(`Data: ${value}.`);
});

我可以在device.collections中插入以获得所有的报告类型。

代码语言:javascript
复制
for (let collection of device.collections) {
  // An HID collection includes usage, usage page, reports, and subcollections.

  msg += `Usage: ${collection.usage}` + "<br>";
  msg += `Usage page: ${collection.usagePage}`+ "<br>";

  for (let inputReport of collection.inputReports) {
    msg += `Input report: ${inputReport.reportId}`+ "<br>";
    // Loop through inputReport.items
  }

  for (let outputReport of collection.outputReports) {
    msg += `Output report: ${outputReport.reportId}`+ "<br>";
    // Loop through outputReport.items
  }

  for (let featureReport of collection.featureReports) {
    msg += `Feature report: ${featureReport.reportId}`+ "<br>";
    // Loop through featureReport.items
  }
}
showMessage(msg);

这是输出:

代码语言:javascript
复制
Usage: 1
Usage page: 255
Input report: 1
Input report: 3
Output report: 2
Output report: 4

没有多少例子可以让我直接得到一个输入报告,而不需要进行监听活动。也许我对USB报告的理解是不完整的,因此任何帮助都将不胜感激。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-04-15 15:57:31

根据我们对WebHID GitHub开放问题的讨论,我们的硬件很可能无法按照标准正确地实现WebHID。因此,在固件上正确实现USB标准比使用get_report函数绕过常规方法更有意义。

结束这个问题。

票数 0
EN

Stack Overflow用户

发布于 2022-03-07 14:34:13

我今天了解到,要触发输入报告事件,设备必须在中断请求期间发送输入报告,比如写入输出报告或执行连接之类的操作。如果设备没有实现它,那么在事件发生期间没有任何返回作为输入报告。按照我的设备的工作方式,我必须使用传输控制来检索输入报告。这在Windows中不是问题,因为HID.dll有一个输入报表请求函数。在LibUSB中也有一个可以与Linux和MacOS一起使用,get_input_report实现如下:

代码语言:javascript
复制
int HID_API_EXPORT HID_API_CALL hid_get_input_report(hid_device *dev, unsigned char *data, size_t length)
{
int res = -1;
int skipped_report_id = 0;
int report_number = data[0];

if (report_number == 0x0) {
    /* Offset the return buffer by 1, so that the report ID
       will remain in byte 0. */
    data++;
    length--;
    skipped_report_id = 1;
}
res = libusb_control_transfer(dev->device_handle,
    LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE|LIBUSB_ENDPOINT_IN,
    0x01/*HID get_report*/,
    (1/*HID Input*/ << 8) | report_number,
    dev->interface,
    (unsigned char *)data, length,
    1000/*timeout millis*/);

if (res < 0)
    return -1;

if (skipped_report_id)
    res++;

return res;
}

不幸的是,我似乎无法找到一种方法,使当前版本的WebHID使用低级别的传输控制函数调用来复制此功能。在WebHID实现某种"get_input_report“方法之前,我想我暂时不走运了。由于WebHID实现了receiveFeatureReport函数,我们可能不得不转到一个特性报告。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71212985

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档