xref: /aosp_15_r20/frameworks/base/cmds/hid/README.md (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
1*d57664e9SAndroid Build Coastguard Worker# Usage
2*d57664e9SAndroid Build Coastguard Worker##  Two options to use the hid command:
3*d57664e9SAndroid Build Coastguard Worker### 1. Interactive through stdin:
4*d57664e9SAndroid Build Coastguard Workertype `hid -` into the terminal, then type/paste commands to send to the binary.
5*d57664e9SAndroid Build Coastguard WorkerUse Ctrl+D to signal end of stream to the binary (EOF).
6*d57664e9SAndroid Build Coastguard Worker
7*d57664e9SAndroid Build Coastguard WorkerThis mode can be also used from an app to send HID events.
8*d57664e9SAndroid Build Coastguard WorkerFor an example, see the cts test case at: [InputTestCase.java][2]
9*d57664e9SAndroid Build Coastguard Worker
10*d57664e9SAndroid Build Coastguard WorkerWhen using another program to control hid in interactive mode, registering a
11*d57664e9SAndroid Build Coastguard Workernew input device (for example, a bluetooth joystick) should be the first step.
12*d57664e9SAndroid Build Coastguard WorkerAfter the device is added, you need to wait for the _onInputDeviceAdded_
13*d57664e9SAndroid Build Coastguard Worker(see [InputDeviceListener][1]) notification before issuing commands
14*d57664e9SAndroid Build Coastguard Workerto the device.
15*d57664e9SAndroid Build Coastguard WorkerFailure to do so will cause missed events and inconsistent behaviour.
16*d57664e9SAndroid Build Coastguard WorkerIn the current implementation of the hid command, the hid binary will wait
17*d57664e9SAndroid Build Coastguard Workerfor the file descriptor to the uhid node to send the UHID_START and UHID_OPEN
18*d57664e9SAndroid Build Coastguard Workersignals before returning. However, this is not sufficient. These signals
19*d57664e9SAndroid Build Coastguard Workeronly notify the readiness of the kernel driver,
20*d57664e9SAndroid Build Coastguard Workerbut do not take into account the inputflinger framework.
21*d57664e9SAndroid Build Coastguard Worker
22*d57664e9SAndroid Build Coastguard Worker
23*d57664e9SAndroid Build Coastguard Worker### 2. Using a file as an input:
24*d57664e9SAndroid Build Coastguard Workertype `hid <filename>`, and the file will be used an an input to the binary.
25*d57664e9SAndroid Build Coastguard WorkerYou must add a sufficient delay after a "register" command to ensure device
26*d57664e9SAndroid Build Coastguard Workeris ready. The interactive mode is the recommended method of communicating
27*d57664e9SAndroid Build Coastguard Workerwith the hid binary.
28*d57664e9SAndroid Build Coastguard Worker
29*d57664e9SAndroid Build Coastguard WorkerAll of the input commands should be in pseudo-JSON format as documented below.
30*d57664e9SAndroid Build Coastguard WorkerSee examples [here][3].
31*d57664e9SAndroid Build Coastguard Worker
32*d57664e9SAndroid Build Coastguard WorkerThe file can have multiple commands one after the other (which is not strictly
33*d57664e9SAndroid Build Coastguard Workerlegal JSON format, as this would imply multiple root elements).
34*d57664e9SAndroid Build Coastguard Worker
35*d57664e9SAndroid Build Coastguard Worker## Command description
36*d57664e9SAndroid Build Coastguard Worker
37*d57664e9SAndroid Build Coastguard Worker1. `register`
38*d57664e9SAndroid Build Coastguard WorkerRegister a new uhid device
39*d57664e9SAndroid Build Coastguard Worker
40*d57664e9SAndroid Build Coastguard Worker| Field         | Type          | Description                |
41*d57664e9SAndroid Build Coastguard Worker|:-------------:|:-------------:|:-------------------------- |
42*d57664e9SAndroid Build Coastguard Worker| id            | integer       | Device id                  |
43*d57664e9SAndroid Build Coastguard Worker| command       | string        | Must be set to "register"  |
44*d57664e9SAndroid Build Coastguard Worker| name          | string        | Device name                |
45*d57664e9SAndroid Build Coastguard Worker| vid           | 16-bit integer| Vendor id                  |
46*d57664e9SAndroid Build Coastguard Worker| pid           | 16-bit integer| Product id                 |
47*d57664e9SAndroid Build Coastguard Worker| bus           | string        | Bus that device should use |
48*d57664e9SAndroid Build Coastguard Worker| descriptor    | byte array    | USB HID report descriptor  |
49*d57664e9SAndroid Build Coastguard Worker
50*d57664e9SAndroid Build Coastguard WorkerDevice ID is used for matching the subsequent commands to a specific device
51*d57664e9SAndroid Build Coastguard Workerto avoid ambiguity when multiple devices are registered.
52*d57664e9SAndroid Build Coastguard Worker
53*d57664e9SAndroid Build Coastguard WorkerDevice bus is used to determine how the uhid device is connected to the host.
54*d57664e9SAndroid Build Coastguard WorkerThe options are "usb" and "bluetooth".
55*d57664e9SAndroid Build Coastguard Worker
56*d57664e9SAndroid Build Coastguard WorkerUSB HID report descriptor should be generated according the the USB HID spec
57*d57664e9SAndroid Build Coastguard Workerand can be checked by reverse parsing using a variety of tools, for example
58*d57664e9SAndroid Build Coastguard Worker[usbdescreqparser][5].
59*d57664e9SAndroid Build Coastguard Worker
60*d57664e9SAndroid Build Coastguard WorkerExample:
61*d57664e9SAndroid Build Coastguard Worker```json
62*d57664e9SAndroid Build Coastguard Worker{
63*d57664e9SAndroid Build Coastguard Worker  "id": 1,
64*d57664e9SAndroid Build Coastguard Worker  "command": "register",
65*d57664e9SAndroid Build Coastguard Worker  "name": "Odie (Test)",
66*d57664e9SAndroid Build Coastguard Worker  "vid": 0x18d1,
67*d57664e9SAndroid Build Coastguard Worker  "pid": 0x2c40,
68*d57664e9SAndroid Build Coastguard Worker  "bus": "usb",
69*d57664e9SAndroid Build Coastguard Worker  "descriptor": [0x05, 0x01, 0x09, 0x05, 0xa1, 0x01, 0x85, 0x01, 0x05, 0x09, 0x0a, 0x01, 0x00,
70*d57664e9SAndroid Build Coastguard Worker    0x0a, 0x02, 0x00, 0x0a, 0x04, 0x00, 0x0a, 0x05, 0x00, 0x0a, 0x07, 0x00, 0x0a, 0x08, 0x00,
71*d57664e9SAndroid Build Coastguard Worker    0x0a, 0x0e, 0x00, 0x0a, 0x0f, 0x00, 0x0a, 0x0d, 0x00, 0x05, 0x0c, 0x0a, 0x24, 0x02, 0x0a,
72*d57664e9SAndroid Build Coastguard Worker    0x23, 0x02, 0x15, 0x00, 0x25, 0x01, 0x75, 0x01, 0x95, 0x0b, 0x81, 0x02, 0x75, 0x01, 0x95,
73*d57664e9SAndroid Build Coastguard Worker    0x01, 0x81, 0x03, 0x05, 0x01, 0x75, 0x04, 0x95, 0x01, 0x25, 0x07, 0x46, 0x3b, 0x01, 0x66,
74*d57664e9SAndroid Build Coastguard Worker    0x14, 0x00, 0x09, 0x39, 0x81, 0x42, 0x66, 0x00, 0x00, 0x09, 0x01, 0xa1, 0x00, 0x09, 0x30,
75*d57664e9SAndroid Build Coastguard Worker    0x09, 0x31, 0x09, 0x32, 0x09, 0x35, 0x05, 0x02, 0x09, 0xc5, 0x09, 0xc4, 0x15, 0x00, 0x26,
76*d57664e9SAndroid Build Coastguard Worker    0xff, 0x00, 0x35, 0x00, 0x46, 0xff, 0x00, 0x75, 0x08, 0x95, 0x06, 0x81, 0x02, 0xc0, 0x85,
77*d57664e9SAndroid Build Coastguard Worker    0x02, 0x05, 0x08, 0x0a, 0x01, 0x00, 0x0a, 0x02, 0x00, 0x0a, 0x03, 0x00, 0x0a, 0x04, 0x00,
78*d57664e9SAndroid Build Coastguard Worker    0x15, 0x00, 0x25, 0x01, 0x75, 0x01, 0x95, 0x04, 0x91, 0x02, 0x75, 0x04, 0x95, 0x01, 0x91,
79*d57664e9SAndroid Build Coastguard Worker    0x03, 0xc0, 0x05, 0x0c, 0x09, 0x01, 0xa1, 0x01, 0x85, 0x03, 0x05, 0x01, 0x09, 0x06, 0xa1,
80*d57664e9SAndroid Build Coastguard Worker    0x02, 0x05, 0x06, 0x09, 0x20, 0x15, 0x00, 0x26, 0xff, 0x00, 0x75, 0x08, 0x95, 0x01, 0x81,
81*d57664e9SAndroid Build Coastguard Worker    0x02, 0x06, 0xbc, 0xff, 0x0a, 0xad, 0xbd, 0x75, 0x08, 0x95, 0x06, 0x81, 0x02, 0xc0, 0xc0]
82*d57664e9SAndroid Build Coastguard Worker}
83*d57664e9SAndroid Build Coastguard Worker```
84*d57664e9SAndroid Build Coastguard Worker2. `delay`
85*d57664e9SAndroid Build Coastguard WorkerAdd a delay to command processing
86*d57664e9SAndroid Build Coastguard Worker
87*d57664e9SAndroid Build Coastguard Worker| Field         | Type          | Description                |
88*d57664e9SAndroid Build Coastguard Worker|:-------------:|:-------------:|:-------------------------- |
89*d57664e9SAndroid Build Coastguard Worker| id            | integer       | Device id                  |
90*d57664e9SAndroid Build Coastguard Worker| command       | string        | Must be set to "delay"     |
91*d57664e9SAndroid Build Coastguard Worker| duration      | integer       | Delay in milliseconds      |
92*d57664e9SAndroid Build Coastguard Worker
93*d57664e9SAndroid Build Coastguard WorkerExample:
94*d57664e9SAndroid Build Coastguard Worker```json
95*d57664e9SAndroid Build Coastguard Worker{
96*d57664e9SAndroid Build Coastguard Worker  "id": 1,
97*d57664e9SAndroid Build Coastguard Worker  "command": "delay",
98*d57664e9SAndroid Build Coastguard Worker  "duration": 10
99*d57664e9SAndroid Build Coastguard Worker}
100*d57664e9SAndroid Build Coastguard Worker```
101*d57664e9SAndroid Build Coastguard Worker
102*d57664e9SAndroid Build Coastguard Worker3. `report`
103*d57664e9SAndroid Build Coastguard WorkerSend a report to the HID device
104*d57664e9SAndroid Build Coastguard Worker
105*d57664e9SAndroid Build Coastguard Worker| Field         | Type          | Description                |
106*d57664e9SAndroid Build Coastguard Worker|:-------------:|:-------------:|:-------------------------- |
107*d57664e9SAndroid Build Coastguard Worker| id            | integer       | Device id                  |
108*d57664e9SAndroid Build Coastguard Worker| command       | string        | Must be set to "report"    |
109*d57664e9SAndroid Build Coastguard Worker| report        | byte array    | Report data to send        |
110*d57664e9SAndroid Build Coastguard Worker
111*d57664e9SAndroid Build Coastguard WorkerExample:
112*d57664e9SAndroid Build Coastguard Worker```json
113*d57664e9SAndroid Build Coastguard Worker{
114*d57664e9SAndroid Build Coastguard Worker  "id": 1,
115*d57664e9SAndroid Build Coastguard Worker  "command": "report",
116*d57664e9SAndroid Build Coastguard Worker  "report": [0x01, 0x01, 0x80, 0x7f, 0x7f, 0x7f, 0x7f, 0x00, 0x00]
117*d57664e9SAndroid Build Coastguard Worker}
118*d57664e9SAndroid Build Coastguard Worker```
119*d57664e9SAndroid Build Coastguard Worker
120*d57664e9SAndroid Build Coastguard Worker### Sending a joystick button press event
121*d57664e9SAndroid Build Coastguard WorkerTo send a button press event on a joystick device:
122*d57664e9SAndroid Build Coastguard Worker1. Register the joystick device
123*d57664e9SAndroid Build Coastguard Worker2. Send button down event with coordinates ABS_X, ABS_Y, ABS_Z, and ABS_RZ
124*d57664e9SAndroid Build Coastguard Workerat the center of the range. If the coordinates are not centered, this event
125*d57664e9SAndroid Build Coastguard Workerwill generate a motion event within the input framework, in addition to the
126*d57664e9SAndroid Build Coastguard Workerbutton press event. The range can be determined from the uhid report descriptor.
127*d57664e9SAndroid Build Coastguard Worker3. Send the button up event with the same coordinates as in 2.
128*d57664e9SAndroid Build Coastguard Worker4. Check that the button press event was received.
129*d57664e9SAndroid Build Coastguard Worker
130*d57664e9SAndroid Build Coastguard Worker### Notes
131*d57664e9SAndroid Build Coastguard Worker1. As soon as EOF is reached (either in interactive mode, or in file mode),
132*d57664e9SAndroid Build Coastguard Workerthe device that was created will be unregistered. There is no
133*d57664e9SAndroid Build Coastguard Workerexplicit command for unregistering a device.
134*d57664e9SAndroid Build Coastguard Worker2. The linux input subsystem does not generate events for those values
135*d57664e9SAndroid Build Coastguard Workerthat remain unchanged. For example, if there are two events sent to the driver,
136*d57664e9SAndroid Build Coastguard Workerand both events have the same value of ABS_X, then ABS_X coordinate
137*d57664e9SAndroid Build Coastguard Workerwill not be reported.
138*d57664e9SAndroid Build Coastguard Worker3. The description of joystick actions is available [here][6].
139*d57664e9SAndroid Build Coastguard Worker4. Joysticks are split axes. When an analog stick is in a resting state,
140*d57664e9SAndroid Build Coastguard Workerthe reported coordinates are at the center of the range.
141*d57664e9SAndroid Build Coastguard Worker5. The `getevent` utility can used to print out the key events
142*d57664e9SAndroid Build Coastguard Workerfor debugging purposes.
143*d57664e9SAndroid Build Coastguard Worker
144*d57664e9SAndroid Build Coastguard Worker
145*d57664e9SAndroid Build Coastguard Worker[1]: https://developer.android.com/reference/android/hardware/input/InputManager.InputDeviceListener.html
146*d57664e9SAndroid Build Coastguard Worker[2]: ../../../../cts/tests/tests/hardware/src/android/hardware/input/cts/tests/InputTestCase.java
147*d57664e9SAndroid Build Coastguard Worker[3]: ../../../../cts/tests/tests/hardware/res/raw/
148*d57664e9SAndroid Build Coastguard Worker[4]: https://developer.android.com/training/game-controllers/controller-input.html#button
149*d57664e9SAndroid Build Coastguard Worker[5]: http://eleccelerator.com/usbdescreqparser/
150*d57664e9SAndroid Build Coastguard Worker[6]: https://developer.android.com/training/game-controllers/controller-input.html
151