diff --git a/cmds/hid/jni/com_android_commands_hid_Device.cpp b/cmds/hid/jni/com_android_commands_hid_Device.cpp index f56dd6e4968e1..95de6c506795d 100644 --- a/cmds/hid/jni/com_android_commands_hid_Device.cpp +++ b/cmds/hid/jni/com_android_commands_hid_Device.cpp @@ -129,7 +129,7 @@ JNIEnv* DeviceCallback::getJNIEnv() { } std::unique_ptr Device::open(int32_t id, const char* name, int32_t vid, int32_t pid, - const std::vector& descriptor, + uint16_t bus, const std::vector& descriptor, std::unique_ptr callback) { size_t size = descriptor.size(); if (size > HID_MAX_DESCRIPTOR_SIZE) { @@ -148,7 +148,7 @@ std::unique_ptr Device::open(int32_t id, const char* name, int32_t vid, strlcpy(reinterpret_cast(ev.u.create2.name), name, sizeof(ev.u.create2.name)); memcpy(&ev.u.create2.rd_data, descriptor.data(), size * sizeof(ev.u.create2.rd_data[0])); ev.u.create2.rd_size = size; - ev.u.create2.bus = BUS_BLUETOOTH; + ev.u.create2.bus = bus; ev.u.create2.vendor = vid; ev.u.create2.product = pid; ev.u.create2.version = 0; @@ -293,8 +293,8 @@ std::vector getData(JNIEnv* env, jbyteArray javaArray) { return data; } -static jlong openDevice(JNIEnv* env, jclass /* clazz */, jstring rawName, jint id, jint vid, jint pid, - jbyteArray rawDescriptor, jobject callback) { +static jlong openDevice(JNIEnv* env, jclass /* clazz */, jstring rawName, jint id, jint vid, + jint pid, jint bus, jbyteArray rawDescriptor, jobject callback) { ScopedUtfChars name(env, rawName); if (name.c_str() == nullptr) { return 0; @@ -305,7 +305,7 @@ static jlong openDevice(JNIEnv* env, jclass /* clazz */, jstring rawName, jint i std::unique_ptr cb(new uhid::DeviceCallback(env, callback)); std::unique_ptr d = - uhid::Device::open(id, reinterpret_cast(name.c_str()), vid, pid, desc, + uhid::Device::open(id, reinterpret_cast(name.c_str()), vid, pid, bus, desc, std::move(cb)); return reinterpret_cast(d.release()); } @@ -339,14 +339,14 @@ static void closeDevice(JNIEnv* /* env */, jclass /* clazz */, jlong ptr) { } static JNINativeMethod sMethods[] = { - { "nativeOpenDevice", - "(Ljava/lang/String;III[B" - "Lcom/android/commands/hid/Device$DeviceCallback;)J", - reinterpret_cast(openDevice) }, - { "nativeSendReport", "(J[B)V", reinterpret_cast(sendReport) }, - { "nativeSendGetFeatureReportReply", "(JI[B)V", - reinterpret_cast(sendGetFeatureReportReply) }, - { "nativeCloseDevice", "(J)V", reinterpret_cast(closeDevice) }, + {"nativeOpenDevice", + "(Ljava/lang/String;IIII[B" + "Lcom/android/commands/hid/Device$DeviceCallback;)J", + reinterpret_cast(openDevice)}, + {"nativeSendReport", "(J[B)V", reinterpret_cast(sendReport)}, + {"nativeSendGetFeatureReportReply", "(JI[B)V", + reinterpret_cast(sendGetFeatureReportReply)}, + {"nativeCloseDevice", "(J)V", reinterpret_cast(closeDevice)}, }; int register_com_android_commands_hid_Device(JNIEnv* env) { diff --git a/cmds/hid/jni/com_android_commands_hid_Device.h b/cmds/hid/jni/com_android_commands_hid_Device.h index 93ea881cfe282..7202b45adcde9 100644 --- a/cmds/hid/jni/com_android_commands_hid_Device.h +++ b/cmds/hid/jni/com_android_commands_hid_Device.h @@ -43,7 +43,7 @@ private: class Device { public: static std::unique_ptr open(int32_t id, const char* name, int32_t vid, int32_t pid, - const std::vector& descriptor, + uint16_t bus, const std::vector& descriptor, std::unique_ptr callback); ~Device(); diff --git a/cmds/hid/src/com/android/commands/hid/Device.java b/cmds/hid/src/com/android/commands/hid/Device.java index 874604ceb5e41..dade41511ae65 100644 --- a/cmds/hid/src/com/android/commands/hid/Device.java +++ b/cmds/hid/src/com/android/commands/hid/Device.java @@ -52,13 +52,13 @@ public class Device { System.loadLibrary("hidcommand_jni"); } - private static native long nativeOpenDevice(String name, int id, int vid, int pid, + private static native long nativeOpenDevice(String name, int id, int vid, int pid, int bus, byte[] descriptor, DeviceCallback callback); private static native void nativeSendReport(long ptr, byte[] data); private static native void nativeSendGetFeatureReportReply(long ptr, int id, byte[] data); private static native void nativeCloseDevice(long ptr); - public Device(int id, String name, int vid, int pid, byte[] descriptor, + public Device(int id, String name, int vid, int pid, int bus, byte[] descriptor, byte[] report, SparseArray featureReports, Map outputs) { mId = id; mThread = new HandlerThread("HidDeviceHandler"); @@ -70,6 +70,7 @@ public class Device { args.argi1 = id; args.argi2 = vid; args.argi3 = pid; + args.argi4 = bus; if (name != null) { args.arg1 = name; } else { @@ -115,7 +116,7 @@ public class Device { case MSG_OPEN_DEVICE: SomeArgs args = (SomeArgs) msg.obj; mPtr = nativeOpenDevice((String) args.arg1, args.argi1, args.argi2, args.argi3, - (byte[]) args.arg2, new DeviceCallback()); + args.argi4, (byte[]) args.arg2, new DeviceCallback()); pauseEvents(); break; case MSG_SEND_REPORT: diff --git a/cmds/hid/src/com/android/commands/hid/Event.java b/cmds/hid/src/com/android/commands/hid/Event.java index 62587a70f10dd..d4bf1d820a70b 100644 --- a/cmds/hid/src/com/android/commands/hid/Event.java +++ b/cmds/hid/src/com/android/commands/hid/Event.java @@ -36,12 +36,28 @@ public class Event { public static final String COMMAND_DELAY = "delay"; public static final String COMMAND_REPORT = "report"; + // These constants come from "include/uapi/linux/input.h" in the kernel + enum Bus { + USB(0x03), BLUETOOTH(0x05); + + Bus(int value) { + mValue = value; + } + + int getValue() { + return mValue; + } + + private int mValue; + } + private int mId; private String mCommand; private String mName; private byte[] mDescriptor; private int mVid; private int mPid; + private Bus mBus; private byte[] mReport; private SparseArray mFeatureReports; private Map mOutputs; @@ -71,6 +87,10 @@ public class Event { return mPid; } + public int getBus() { + return mBus.getValue(); + } + public byte[] getReport() { return mReport; } @@ -94,6 +114,7 @@ public class Event { + ", descriptor=" + Arrays.toString(mDescriptor) + ", vid=" + mVid + ", pid=" + mPid + + ", bus=" + mBus + ", report=" + Arrays.toString(mReport) + ", feature_reports=" + mFeatureReports.toString() + ", outputs=" + mOutputs.toString() @@ -144,6 +165,10 @@ public class Event { mEvent.mPid = pid; } + public void setBus(Bus bus) { + mEvent.mBus = bus; + } + public void setDuration(int duration) { mEvent.mDuration = duration; } @@ -206,6 +231,9 @@ public class Event { case "pid": eb.setPid(readInt()); break; + case "bus": + eb.setBus(readBus()); + break; case "report": eb.setReport(readData()); break; @@ -264,6 +292,11 @@ public class Event { return Integer.decode(val); } + private Bus readBus() throws IOException { + String val = mReader.nextString(); + return Bus.valueOf(val.toUpperCase()); + } + private SparseArray readFeatureReports() throws IllegalStateException, IOException { SparseArray featureReports = new SparseArray<>(); diff --git a/cmds/hid/src/com/android/commands/hid/Hid.java b/cmds/hid/src/com/android/commands/hid/Hid.java index 0ee2cc45932f8..fac0ab2ef1254 100644 --- a/cmds/hid/src/com/android/commands/hid/Hid.java +++ b/cmds/hid/src/com/android/commands/hid/Hid.java @@ -113,7 +113,7 @@ public class Hid { "Tried to send command \"" + e.getCommand() + "\" to an unregistered device!"); } int id = e.getId(); - Device d = new Device(id, e.getName(), e.getVendorId(), e.getProductId(), + Device d = new Device(id, e.getName(), e.getVendorId(), e.getProductId(), e.getBus(), e.getDescriptor(), e.getReport(), e.getFeatureReports(), e.getOutputs()); mDevices.append(id, d); }