Some useful utilities for configuring USB HID gadget using configfs.
folder | main file | description |
---|---|---|
desc2bin | convert.sh | convert the .h header file exported from HidDescTool to .bin binary file, which is required in gadget functions setup |
setup_script | en_gadget.sh | automatically setup usb gadget according to information filled in. |
driver_sample | hid_gadget_test.c | a sample interactive c program to emulate a keyboard or mouse after gadget is set up. (from linux kernel doc, here) |
python_wrap | gadget.py | a wrapper class for send hid instructions from python |
Before using any of these utilities, you should check if your linux kernel as well as your hardware/development board itself support configuring USB Hid gadget through configfs.
Try finding the mount point of configfs:
sudo mount -l | grep configfs
if it outputs the mount point of configfs, go to step 4;
otherwise try load kernel module and mount it:
sudo modprobe libcomposite
CONFIGFS_HOME=XXX
mount none $CONFIGFS_HOME -t configfs
and then retry step 1.
If the output is still empty, you may need to modify kernel settings and compile the kernel, and try step 3&1 again.
If folder usb_gadget
doesn't exist in $CONFIGFS_HOME
, you also need to modify kernel settings and compile the kernel.
First download Hid-Descriptor-Tool on a Windows PC, unzip and run Dt.exe. Edit your own hid descriptor or click "File-Open" and select one from the given samples along with the tool. Click "File-SaveAs" and export the descriptor to a C header file (.h).
Upload the header file to your linux device. Within the desc2bin folder, mouseDesc.h and keybrdDesc.h are provided for reference or tests.
Then run the convert.sh script to convert the header file to required binary file.
cd desc2bin
sudo chmod +x ./convert.sh #if necessary
./convert.sh mouseDesc.h mouse-descriptor.bin
If all goes well, a binary file mouse-descriptor.bin will be generated in current folder, command hexdump
is useful to check its content:
hexdump mouse-descriptor.bin
You can replace the header file and output binary file name in commands above, as you need. The binary file is required in next step.
Open the en_gadget.sh
using your favourite text editor.
Edit the Settings between ####[ User Settings begin]#####
and ####[ User Settings end]#####
, referring to the comments following the keys.
Delete or append lines of function_setup
in main()
, according to your number of functions.
Save and Close.
Run the shellscript with sudo:
cd setup_script
sudo chmod +x ./en_gadget.sh #if necessary
sudo ./en_gadget.sh
If the scipt print the device file like /dev/hidg0
, /dev/hidg1
in the end, then the gadget has been set up properly, congratulations.
Note: the script DOES NOT handle any error occurred halfway, you may better look through the script and make clear exactly what it will do before execution.
After gadget setup, hid_gadget_test.c
could be used to test the hid functions interactively. (Note: this program is collected from linux kernel documentation about hid gadget)
Assure you have properly set up usb hid gadget in previous steps.
Compile the c source file.
cd driver_sample
gcc hid_gadget_test.c -o hid_gadget_test
Connect the device to your host PC(or other USB host devices) with a USB cable.
If there is multiple USB ports on device, make sure you connect the right one.
If the host PC has a operating system, check the Device Manager to see if the device is recognized properly as a USB device.
Run the program to emulate a keyboard:
./hid_gadget /dev/hidgX keyboard
Where X should be replaced by your real device number printed by the setup script step, and "keyboard" could also be replaced by "mouse" to emulate a mouse.
The program will print all the commands that is supported. For example, type--left-meta e
and press Enter, the host PC will open explorer on Windows system.
This is a wrapper class of the given sample program for controlling the gadget in your own python program.
Assure you have properly set up usb hid gadget in previous steps.
Compile the sample program to dynamic library, so that the C functions can be reused by python.
cd python_wrap
sudo chmod +x ./get_so.sh #if necessary
./get_so.sh
Then gadget.so
should be compiled in current folder.
Import gadget, and write your python code . For example:
from gadget import Gadget
gkey=Gadget("/dev/hidg0","keyboard")
gms=Gadget("/dev/hidg1","mouse")
gkey.send("--left-meta e") # win+e
gms.send("-65 -65 --b2") # move left-up and right click
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。