我正在制作一个自定义 USB HID 设备,以及一个与之配套的桌面应用程序,在 Lubuntu 16.04.3 LTS 上使用 HIDAPI。我们myapp
现在就调用它。
显然,如果我这样做$ ./myapp
,libusb_open()
失败了LIBUSB_ERROR_ACCESS
。 (在我的调试器中显示为-3
; 花了一段时间才弄清楚,因为枚举似乎没有很好地记录)
但是如果我这样做了$ sudo ./myapp
,它就会成功。
我真的不想myapp
拥有 root 权限,那么没有它们我怎么能与我的 USB 设备通信呢?
我希望在这里得到答案,但似乎在这一点上已经被放弃了。它说明了一些关于用户权限的内容,但我似乎在我的系统上找不到它。我想我可能会找到一个叫做的组usb
或者libusb
我可以将自己添加到,但没有这样的运气。
另一个 SE 站点上的这个问题有一个答案,它使用一些简单的文本文件来更改全局(可能是个坏主意)或特定设备的权限,但是:
myapp
?myapp
在用户权限下仍然无法与 USB 通话。Aar*_*onD 13
It's still not the "muggle's tweak" that I'm really looking for, but at least this works:
Apparently there are two directories for udev (I have no idea why):
/etc/udev/rules.d
/lib/udev/rules.d
I'd been messing with the /lib
one and getting nowhere. I found the /etc
one here, and it does work:
Put SUBSYSTEM=="usb", ATTRS{idVendor}=="VID", ATTRS{idProduct}=="PID", MODE="0666"
VID
is the USB-IF-assigned Vendor ID of the device in question *
PID
is the Vendor-assigned Product ID of the device in question *
0666
对与此行匹配的任何内容进行通用读/写访问
*$ lsusb
查看所有连接的 USB 设备及其 ID。
在/etc/udev/rules.d/xx-my-rule.rules
(可能需要 root/sudo 权限)
xx
是任何大于 50 的数字(默认值为 50,数字越大优先)my-rule
随便你怎么称呼它.rules
然后udevadm control --reload-rules
(可能还需要 root/sudo 权限),它应该“适用于”特定的 VID/PID 对。
另一个选项,稍微收紧权限,是使用TAG+="uaccess"
代替MODE="0666"
. 这限制了对当前登录(物理)用户而不是所有用户的访问。谢谢@Lekensteyn!