Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2a1acadc authored by Josep del Rio's avatar Josep del Rio Committed by Edward Liaw
Browse files

Add hid command support for uniq device attribute

Sony gamepads need a uniq attribute of a certain size in order to
obtain the MAC address, which makes our CTS test fail. This CL
adds the ability to define this attribute in our test definitions.

Bug: 314732730
Test: Compiles properly, will be tested together with the test
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:93a4c0fbdc14d99cbd382fd50be83b7058ca6b56

)
Merged-In: I83203e8eb876b27d0e6e60adfbf6de3f4a6e9872
Change-Id: I83203e8eb876b27d0e6e60adfbf6de3f4a6e9872
Signed-off-by: default avatarEdward Liaw <edliaw@google.com>
parent 0ffcc276
Loading
Loading
Loading
Loading
+20 −8
Original line number Diff line number Diff line
@@ -134,8 +134,9 @@ JNIEnv* DeviceCallback::getJNIEnv() {
    return env;
}

std::unique_ptr<Device> Device::open(int32_t id, const char* name, int32_t vid, int32_t pid,
                                     uint16_t bus, const std::vector<uint8_t>& descriptor,
std::unique_ptr<Device> Device::open(int32_t id, const char* name, const char* uniq, int32_t vid,
                                     int32_t pid, uint16_t bus,
                                     const std::vector<uint8_t>& descriptor,
                                     std::unique_ptr<DeviceCallback> callback) {
    size_t size = descriptor.size();
    if (size > HID_MAX_DESCRIPTOR_SIZE) {
@@ -152,8 +153,7 @@ std::unique_ptr<Device> Device::open(int32_t id, const char* name, int32_t vid,
    struct uhid_event ev = {};
    ev.type = UHID_CREATE2;
    strlcpy(reinterpret_cast<char*>(ev.u.create2.name), name, sizeof(ev.u.create2.name));
    std::string uniq = android::base::StringPrintf("Id: %d", id);
    strlcpy(reinterpret_cast<char*>(ev.u.create2.uniq), uniq.c_str(), sizeof(ev.u.create2.uniq));
    strlcpy(reinterpret_cast<char*>(ev.u.create2.uniq), uniq, sizeof(ev.u.create2.uniq));
    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;
@@ -314,19 +314,31 @@ std::vector<uint8_t> getData(JNIEnv* env, jbyteArray javaArray) {
    return data;
}

static jlong openDevice(JNIEnv* env, jclass /* clazz */, jstring rawName, jint id, jint vid,
                        jint pid, jint bus, jbyteArray rawDescriptor, jobject callback) {
static jlong openDevice(JNIEnv* env, jclass /* clazz */, jstring rawName, jstring rawUniq, jint id,
                        jint vid, jint pid, jint bus, jbyteArray rawDescriptor, jobject callback) {
    ScopedUtfChars name(env, rawName);
    if (name.c_str() == nullptr) {
        return 0;
    }

    std::string uniq;
    if (rawUniq != nullptr) {
        uniq = ScopedUtfChars(env, rawUniq);
    } else {
        uniq = android::base::StringPrintf("Id: %d", id);
    }

    if (uniq.c_str() == nullptr) {
        return 0;
    }

    std::vector<uint8_t> desc = getData(env, rawDescriptor);

    std::unique_ptr<uhid::DeviceCallback> cb(new uhid::DeviceCallback(env, callback));

    std::unique_ptr<uhid::Device> d =
            uhid::Device::open(id, reinterpret_cast<const char*>(name.c_str()), vid, pid, bus, desc,
            uhid::Device::open(id, reinterpret_cast<const char*>(name.c_str()),
                               reinterpret_cast<const char*>(uniq.c_str()), vid, pid, bus, desc,
                               std::move(cb));
    return reinterpret_cast<jlong>(d.release());
}
@@ -370,7 +382,7 @@ static void closeDevice(JNIEnv* /* env */, jclass /* clazz */, jlong ptr) {

static JNINativeMethod sMethods[] = {
        {"nativeOpenDevice",
         "(Ljava/lang/String;IIII[B"
         "(Ljava/lang/String;Ljava/lang/String;IIII[B"
         "Lcom/android/commands/hid/Device$DeviceCallback;)J",
         reinterpret_cast<void*>(openDevice)},
        {"nativeSendReport", "(J[B)V", reinterpret_cast<void*>(sendReport)},
+3 −2
Original line number Diff line number Diff line
@@ -42,8 +42,9 @@ private:

class Device {
public:
    static std::unique_ptr<Device> open(int32_t id, const char* name, int32_t vid, int32_t pid,
                                        uint16_t bus, const std::vector<uint8_t>& descriptor,
    static std::unique_ptr<Device> open(int32_t id, const char* name, const char* uniq, int32_t vid,
                                        int32_t pid, uint16_t bus,
                                        const std::vector<uint8_t>& descriptor,
                                        std::unique_ptr<DeviceCallback> callback);

    ~Device();
+7 −3
Original line number Diff line number Diff line
@@ -71,6 +71,7 @@ public class Device {

    private static native long nativeOpenDevice(
            String name,
            String uniq,
            int id,
            int vid,
            int pid,
@@ -89,6 +90,7 @@ public class Device {
    public Device(
            int id,
            String name,
            String uniq,
            int vid,
            int pid,
            int bus,
@@ -113,8 +115,9 @@ public class Device {
        } else {
            args.arg1 = id + ":" + vid + ":" + pid;
        }
        args.arg2 = descriptor;
        args.arg3 = report;
        args.arg2 = uniq;
        args.arg3 = descriptor;
        args.arg4 = report;
        mHandler.obtainMessage(MSG_OPEN_DEVICE, args).sendToTarget();
        mTimeToSend = SystemClock.uptimeMillis();
    }
@@ -167,11 +170,12 @@ public class Device {
                    mPtr =
                            nativeOpenDevice(
                                    (String) args.arg1,
                                    (String) args.arg2,
                                    args.argi1,
                                    args.argi2,
                                    args.argi3,
                                    args.argi4,
                                    (byte[]) args.arg2,
                                    (byte[]) args.arg3,
                                    new DeviceCallback());
                    pauseEvents();
                    break;
+13 −0
Original line number Diff line number Diff line
@@ -56,6 +56,7 @@ public class Event {
    private int mId;
    private String mCommand;
    private String mName;
    private String mUniq;
    private byte[] mDescriptor;
    private int mVid;
    private int mPid;
@@ -78,6 +79,10 @@ public class Event {
        return mName;
    }

    public String getUniq() {
        return mUniq;
    }

    public byte[] getDescriptor() {
        return mDescriptor;
    }
@@ -118,6 +123,7 @@ public class Event {
        return "Event{id=" + mId
            + ", command=" + String.valueOf(mCommand)
            + ", name=" + String.valueOf(mName)
            + ", uniq=" + String.valueOf(mUniq)
            + ", descriptor=" + Arrays.toString(mDescriptor)
            + ", vid=" + mVid
            + ", pid=" + mPid
@@ -149,6 +155,10 @@ public class Event {
            mEvent.mName = name;
        }

        public void setUniq(String uniq) {
            mEvent.mUniq = uniq;
        }

        public void setDescriptor(byte[] descriptor) {
            mEvent.mDescriptor = descriptor;
        }
@@ -247,6 +257,9 @@ public class Event {
                            case "name":
                                eb.setName(mReader.nextString());
                                break;
                            case "uniq":
                                eb.setUniq(mReader.nextString());
                                break;
                            case "vid":
                                eb.setVid(readInt());
                                break;
+11 −2
Original line number Diff line number Diff line
@@ -117,8 +117,17 @@ 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(), e.getBus(),
                e.getDescriptor(), e.getReport(), e.getFeatureReports(), e.getOutputs());
        Device d = new Device(
                id,
                e.getName(),
                e.getUniq(),
                e.getVendorId(),
                e.getProductId(),
                e.getBus(),
                e.getDescriptor(),
                e.getReport(),
                e.getFeatureReports(),
                e.getOutputs());
        mDevices.append(id, d);
    }