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

Commit c490e7ac authored by Siarhei Vishniakou's avatar Siarhei Vishniakou
Browse files

Use switch statement for report types

We are adding new report types, and it is easier to use switch
statements to ensure that the default case is handled.

Also, expand the diagnostic messaging for SET_REPORT type, because it is
useful to see the actual data.

Bug: none
Test: atest NintendoSwitchProTest
Change-Id: I330e3b1464ae8e35a57d448e460cdb55ebbf6260
parent 9a02feac
Loading
Loading
Loading
Loading
+37 −8
Original line number Diff line number Diff line
@@ -21,10 +21,11 @@
#include <linux/uhid.h>

#include <fcntl.h>
#include <inttypes.h>
#include <unistd.h>
#include <cstdio>
#include <cstring>
#include <memory>
#include <unistd.h>

#include <jni.h>
#include <nativehelper/JNIHelp.h>
@@ -33,6 +34,8 @@
#include <android/looper.h>
#include <android/log.h>

#include <android-base/stringprintf.h>

#define  LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
#define  LOGW(...)  __android_log_print(ANDROID_LOG_WARN,LOG_TAG,__VA_ARGS__)
#define  LOGD(...)  __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
@@ -61,6 +64,14 @@ static void checkAndClearException(JNIEnv* env, const char* methodName) {
    }
}

static std::string toString(const std::vector<uint8_t>& data) {
    std::string s = "";
    for (uint8_t b : data) {
        s += android::base::StringPrintf("%x ", b);
    }
    return s;
}

DeviceCallback::DeviceCallback(JNIEnv* env, jobject callback) :
    mCallbackObject(env->NewGlobalRef(callback)) {
    env->GetJavaVM(&mJavaVM);
@@ -208,15 +219,33 @@ int Device::handleEvents(int events) {
        return 0;
    }

    if (ev.type == UHID_OPEN) {
    switch (ev.type) {
        case UHID_OPEN: {
            mDeviceCallback->onDeviceOpen();
    } else if (ev.type == UHID_GET_REPORT) {
            break;
        }
        case UHID_GET_REPORT: {
            mDeviceCallback->onDeviceGetReport(ev.u.get_report.id, ev.u.get_report.rnum);
    } else if (ev.type == UHID_SET_REPORT) {
        LOGE("UHID_SET_REPORT is currently not supported");
            break;
        }
        case UHID_SET_REPORT: {
            const struct uhid_set_report_req& set_report = ev.u.set_report;
            if (set_report.size > UHID_DATA_MAX) {
                LOGE("SET_REPORT contains too much data: size = %" PRIu16, set_report.size);
                return 0;
            }

            std::vector<uint8_t> data(set_report.data, set_report.data + set_report.size);
            LOGI("Received SET_REPORT: id=%" PRIu32 " rnum=%" PRIu8 " data=%s", set_report.id,
                 set_report.rnum, toString(data).c_str());
            break;
        }
        default: {
            LOGI("Unhandled event type: %" PRIu32, ev.type);
            break;
        }
    }

    return 1;
}