diff options
author | Siarhei Vishniakou <svv@google.com> | 2019-12-09 16:58:29 -0800 |
---|---|---|
committer | Siarhei Vishniakou <svv@google.com> | 2019-12-10 13:47:08 -0800 |
commit | c490e7acdeaf682275cb687684e82ac984ab4a61 (patch) | |
tree | 11a5b0a8a9113e852ff79b8c4daee1a60839025a /cmds/hid | |
parent | 9a02feacd280ac57ae3d1b4e9df5ba4190b0d0d1 (diff) |
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
Diffstat (limited to 'cmds/hid')
-rw-r--r-- | cmds/hid/jni/com_android_commands_hid_Device.cpp | 45 |
1 files changed, 37 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 d7bdda34bc54..f3871d74320b 100644 --- a/cmds/hid/jni/com_android_commands_hid_Device.cpp +++ b/cmds/hid/jni/com_android_commands_hid_Device.cpp @@ -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,13 +219,31 @@ int Device::handleEvents(int events) { return 0; } - if (ev.type == UHID_OPEN) { - mDeviceCallback->onDeviceOpen(); - } else if (ev.type == 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"); - return 0; + switch (ev.type) { + case UHID_OPEN: { + mDeviceCallback->onDeviceOpen(); + break; + } + case UHID_GET_REPORT: { + mDeviceCallback->onDeviceGetReport(ev.u.get_report.id, ev.u.get_report.rnum); + 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; |