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
This commit is contained in:
Chris Ye 2021-02-23 12:25:06 -08:00
parent 99051f249a
commit 5dea9a9082
3 changed files with 32 additions and 8 deletions

View File

@ -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<uint8_t>& 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<uint8_t>& 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 ||

View File

@ -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<uint8_t>& data);
void onDeviceSetReport(uint8_t rType, const std::vector<uint8_t>& data);
void onDeviceOutput(uint8_t rType, const std::vector<uint8_t>& data);
void onDeviceError();
private:

View File

@ -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;