raspberry pi 4 - 尝试模拟鼠标和键盘 - BrokenPipeError:[Errno 108] 传输终结点关闭后无法发送

raspberry pi 4 - trying to emulate mouse and keyboard - BrokenPipeError: [Errno 108] Cannot send after transport endpoint shutdown

提问人:martinval 提问时间:11/8/2023 更新时间:11/8/2023 访问量:26

问:

我有一个运行内核 6.1.21 的 Bookworm 的 Pi4,我正在努力模拟键盘和鼠标。 我有一个运行的启动脚本,看起来像这样: Raspberry Pi 4 上的鼠标仿真不起作用

我遵循了这个链接的基础知识:在这里输入链接描述来初始化一些内核位。

我尝试从 python 文件运行这些示例:

NULL_CHAR = chr(0)

#keyboard
def write_report(report):
    with open('/dev/hidg0', 'rb+') as fd:
        fd.write(report.encode())

write_report(NULL_CHAR*2+chr(6)+chr(7)+chr(8)+chr(9)+chr(10)+chr(11))
write_report(NULL_CHAR*8)

#mouse
def ms_write(report):
    with open("/dev/hidg1", "wb+") as fd:
        fd.write(report.encode())

ms_write(NULL_CHAR+chr(100)+NULL_CHAR)
ms_write(NULL_CHAR*3)

我得到的错误是:BrokenPipeError:[Errno 108]传输终结点关闭后无法发送

通过四处搜索,我可以看到工作示例将较旧的安装与其他内核一起使用,因此这可能是问题所在。

或者设置在某种程度上是错误的,因为大多数例子都是Pi Zero连接到另一个设备,这不是我正在做的。

任何与 Bookworm 或任何 poointers 合作的人可能有什么问题?

我的初始化 bash 脚本:

#!/bin/bash

# Initializes the device by adding necessary kernel modules.

set -e

reboot_required=0

# Make sure we use the dwc2 USB driver.
# This driver is capable of handling OTG, consequently
# allowing this device to act as USB slave.

if ! grep "dtoverlay=dwc2" /boot/config.txt; then
  echo "dtoverlay=dwc2" >> /boot/config.txt
  reboot_required=1
fi

# Enable the corresponding kernel module.

if ! grep "dwc2" /etc/modules; then
  echo "dwc2" >> /etc/modules
  reboot_required=1
fi

if [ $reboot_required -eq 1 ]; then
  echo "Please reboot the device for the initialization to take effect"
else
  echo "Everything set up"
fi

还有我的设置脚本:

#!/bin/bash
set -e

modprobe libcomposite

cd /sys/kernel/config/usb_gadget/
mkdir -p pihid
cd pihid

echo 0x1d6b > idVendor # Linux Foundation
echo 0x0104 > idProduct # Multifunction Composite Gadget
echo 0x0100 > bcdDevice # v1.0.0
echo 0x0200 > bcdUSB # USB2

mkdir -p strings/0x409
echo "fedcba9876543210" > strings/0x409/serialnumber
echo "test" > strings/0x409/manufacturer
echo "PiHID" > strings/0x409/product
mkdir -p configs/c.1/strings/0x409
echo "Config 1: ECM network" > configs/c.1/strings/0x409/configuration
echo 250 > configs/c.1/MaxPower

# Add functions here
mkdir -p functions/hid.usb0
echo 1 > functions/hid.usb0/protocol
echo 1 > functions/hid.usb0/subclass
echo 8 > functions/hid.usb0/report_length
echo -ne \\x05\\x01\\x09\\x06\\xa1\\x01\\x05\\x07\\x19\\xe0\\x29\\xe7\\x15\\x00\\x25\\x01\\x75\\x01\\x95\\x08\\x81\\x02\\x95\\x01\\x75\\x08\\x81\\x03\\x95\\x05\\x75\\x01\\x05\\x08\\x19\\x01\\x29\\x05\\x91\\x02\\x95\\x01\\x75\\x03\\x91\\x03\\x95\\x06\\x75\\x08\\x15\\x00\\x25\\x65\\x05\\x07\\x19\\x00\\x29\\x65\\x81\\x00\\xc0 > functions/hid.usb0/report_desc
ln -s functions/hid.usb0 configs/c.1/

mkdir -p functions/hid.usb1
echo 2 > functions/hid.usb1/protocol
echo 2 > functions/hid.usb1/subclass
echo 8 > functions/hid.usb1/report_length
echo -ne \\x05\\x01\\x09\\x02\\xA1\\x01\\x09\\x01\\xA1\\x00\\x05\\x09\\x19\\x01\\x29\\x03\\x15\\x00\\x25\\x01\\x95\\x03\\x75\\x01\\x81\\x02\\x95\\x01\\x75\\x05\\x81\\x03\\x05\\x01\\x09\\x30\\x09\\x31\\x15\\x81\\x25\\x7F\\x75\\x08\\x95\\x02\\x81\\x06\\xC0\\xC0 > functions/hid.usb1/report_desc
ln -s functions/hid.usb1 configs/c.1/
# End functions

ls /sys/class/udc > UDC

#chmod 777 /dev/hidg0
#chmod 777 /dev/hidg1
#

和 /etc/rc.local:

#mouse emulator config
sudo /home/martinval/projects/hid/mouse_keyb.sh

sudo chmod -R 777 /dev/hidg0
sudo chmod -R 777 /dev/hidg1
树莓

评论


答: 暂无答案