From 5dea9a908278ca20c5293522dea697387fe0bc41 Mon Sep 17 00:00:00 2001 From: Chris Ye Date: Tue, 23 Feb 2021 12:25:06 -0800 Subject: [PATCH] Add onDeviceSetReport to HID command tool When UHID device receives UHID_SET_REPORT event, call onDeviceSetReprot callback function to set the report output. Bug: 161633625 Test: atest android.hardware.input.cts.tests.SonyDualshock3UsbTest#testLights Change-Id: I02923203ddf5a2f2090a11434db19925bbaeaf05 --- .../jni/com_android_commands_hid_Device.cpp | 21 ++++++++++++++----- .../hid/jni/com_android_commands_hid_Device.h | 3 ++- .../src/com/android/commands/hid/Device.java | 16 ++++++++++++-- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/cmds/hid/jni/com_android_commands_hid_Device.cpp b/cmds/hid/jni/com_android_commands_hid_Device.cpp index 422c2be44251..2cda57dd67e9 100644 --- a/cmds/hid/jni/com_android_commands_hid_Device.cpp +++ b/cmds/hid/jni/com_android_commands_hid_Device.cpp @@ -48,6 +48,7 @@ static const char* UHID_PATH = "/dev/uhid"; static struct { jmethodID onDeviceOpen; jmethodID onDeviceGetReport; + jmethodID onDeviceSetReport; jmethodID onDeviceOutput; jmethodID onDeviceError; } gDeviceCallbackClassInfo; @@ -113,10 +114,18 @@ void DeviceCallback::onDeviceGetReport(uint32_t requestId, uint8_t reportId) { checkAndClearException(env, "onDeviceGetReport"); } -void DeviceCallback::onDeviceOutput(uint8_t eventId, uint8_t rType, +void DeviceCallback::onDeviceSetReport(uint8_t rType, const std::vector& data) { JNIEnv* env = getJNIEnv(); - env->CallVoidMethod(mCallbackObject, gDeviceCallbackClassInfo.onDeviceOutput, eventId, rType, + env->CallVoidMethod(mCallbackObject, gDeviceCallbackClassInfo.onDeviceSetReport, rType, + toJbyteArray(env, data).get()); + checkAndClearException(env, "onDeviceSetReport"); +} + +void DeviceCallback::onDeviceOutput(uint8_t rType, + const std::vector& data) { + JNIEnv* env = getJNIEnv(); + env->CallVoidMethod(mCallbackObject, gDeviceCallbackClassInfo.onDeviceOutput, rType, toJbyteArray(env, data).get()); checkAndClearException(env, "onDeviceOutput"); } @@ -262,7 +271,7 @@ int Device::handleEvents(int events) { ALOGD("Received SET_REPORT: id=%" PRIu32 " rnum=%" PRIu8 " data=%s", set_report.id, set_report.rnum, toString(data).c_str()); } - mDeviceCallback->onDeviceOutput(UHID_SET_REPORT, set_report.rtype, data); + mDeviceCallback->onDeviceSetReport(set_report.rtype, data); break; } case UHID_OUTPUT: { @@ -271,7 +280,7 @@ int Device::handleEvents(int events) { if (DEBUG_OUTPUT) { ALOGD("UHID_OUTPUT rtype=%" PRIu8 " data=%s", output.rtype, toString(data).c_str()); } - mDeviceCallback->onDeviceOutput(UHID_OUTPUT, output.rtype, data); + mDeviceCallback->onDeviceOutput(output.rtype, data); break; } default: { @@ -366,8 +375,10 @@ int register_com_android_commands_hid_Device(JNIEnv* env) { env->GetMethodID(clazz, "onDeviceOpen", "()V"); uhid::gDeviceCallbackClassInfo.onDeviceGetReport = env->GetMethodID(clazz, "onDeviceGetReport", "(II)V"); + uhid::gDeviceCallbackClassInfo.onDeviceSetReport = + env->GetMethodID(clazz, "onDeviceSetReport", "(B[B)V"); uhid::gDeviceCallbackClassInfo.onDeviceOutput = - env->GetMethodID(clazz, "onDeviceOutput", "(BB[B)V"); + env->GetMethodID(clazz, "onDeviceOutput", "(B[B)V"); uhid::gDeviceCallbackClassInfo.onDeviceError = env->GetMethodID(clazz, "onDeviceError", "()V"); if (uhid::gDeviceCallbackClassInfo.onDeviceOpen == NULL || diff --git a/cmds/hid/jni/com_android_commands_hid_Device.h b/cmds/hid/jni/com_android_commands_hid_Device.h index bb73132cc20d..d10a9aa3680c 100644 --- a/cmds/hid/jni/com_android_commands_hid_Device.h +++ b/cmds/hid/jni/com_android_commands_hid_Device.h @@ -31,7 +31,8 @@ public: void onDeviceOpen(); void onDeviceGetReport(uint32_t requestId, uint8_t reportId); - void onDeviceOutput(uint8_t eventId, uint8_t rType, const std::vector& data); + void onDeviceSetReport(uint8_t rType, const std::vector& data); + void onDeviceOutput(uint8_t rType, const std::vector& data); void onDeviceError(); private: diff --git a/cmds/hid/src/com/android/commands/hid/Device.java b/cmds/hid/src/com/android/commands/hid/Device.java index 37d0b1c6bfa1..95b1e9a7b57f 100644 --- a/cmds/hid/src/com/android/commands/hid/Device.java +++ b/cmds/hid/src/com/android/commands/hid/Device.java @@ -199,8 +199,8 @@ public class Device { mHandler.sendMessageAtTime(msg, mTimeToSend); } - // native callback - public void onDeviceOutput(byte eventId, byte rtype, byte[] data) { + // Send out the report to HID command output + private void sendReportOutput(byte eventId, byte rtype, byte[] data) { JSONObject json = new JSONObject(); try { json.put("eventId", eventId); @@ -221,6 +221,18 @@ public class Device { throw new RuntimeException(e); } + } + + // native callback + public void onDeviceSetReport(byte rtype, byte[] data) { + // We don't need to reply for the SET_REPORT but just send it to HID output for test + // verification. + sendReportOutput(UHID_EVENT_TYPE_SET_REPORT, rtype, data); + } + + // native callback + public void onDeviceOutput(byte rtype, byte[] data) { + sendReportOutput(UHID_EVENT_TYPE_UHID_OUTPUT, rtype, data); if (mOutputs == null) { Log.e(TAG, "Received OUTPUT request, but 'outputs' section is not found"); return;