summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSiarhei Vishniakou <svv@google.com>2019-12-09 16:46:23 -0800
committerSiarhei Vishniakou <svv@google.com>2019-12-10 13:47:08 -0800
commit9a02feacd280ac57ae3d1b4e9df5ba4190b0d0d1 (patch)
tree1a54ad62576cc0f57e07fc154b287bf98b42d4af
parent46e7cc14ad7678ef627b103e7fa107992353204f (diff)
Use a separate writeEvent function
This function will also be used for handling UHID_OUTPUT. It also moves all error checking into a single place. Bug: none Test: NintendoSwitchProTest Change-Id: I72faa4bbfbe41842bbb651730dbd614c5bcbbaea
-rw-r--r--cmds/hid/jni/com_android_commands_hid_Device.cpp18
1 files changed, 10 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 fa75ef435051..d7bdda34bc54 100644
--- a/cmds/hid/jni/com_android_commands_hid_Device.cpp
+++ b/cmds/hid/jni/com_android_commands_hid_Device.cpp
@@ -162,6 +162,14 @@ Device::~Device() {
TEMP_FAILURE_RETRY(::write(mFd, &ev, sizeof(ev)));
}
+// Send event over the fd.
+static void writeEvent(int fd, struct uhid_event& ev, const char* messageType) {
+ ssize_t ret = TEMP_FAILURE_RETRY(::write(fd, &ev, sizeof(ev)));
+ if (ret < 0 || ret != sizeof(ev)) {
+ LOGE("Failed to send uhid_event %s: %s", messageType, strerror(errno));
+ }
+}
+
void Device::sendReport(const std::vector<uint8_t>& report) const {
if (report.size() > UHID_DATA_MAX) {
LOGE("Received invalid report of size %zu, skipping", report.size());
@@ -172,10 +180,7 @@ void Device::sendReport(const std::vector<uint8_t>& report) const {
ev.type = UHID_INPUT2;
ev.u.input2.size = report.size();
memcpy(&ev.u.input2.data, report.data(), report.size() * sizeof(ev.u.input2.data[0]));
- ssize_t ret = TEMP_FAILURE_RETRY(::write(mFd, &ev, sizeof(ev)));
- if (ret < 0 || ret != sizeof(ev)) {
- LOGE("Failed to send hid event: %s", strerror(errno));
- }
+ writeEvent(mFd, ev, "UHID_INPUT2");
}
void Device::sendGetFeatureReportReply(uint32_t id, const std::vector<uint8_t>& report) const {
@@ -186,10 +191,7 @@ void Device::sendGetFeatureReportReply(uint32_t id, const std::vector<uint8_t>&
ev.u.get_report_reply.size = report.size();
memcpy(&ev.u.get_report_reply.data, report.data(),
report.size() * sizeof(ev.u.get_report_reply.data[0]));
- ssize_t ret = TEMP_FAILURE_RETRY(::write(mFd, &ev, sizeof(ev)));
- if (ret < 0 || ret != sizeof(ev)) {
- LOGE("Failed to send hid event (UHID_GET_REPORT_REPLY): %s", strerror(errno));
- }
+ writeEvent(mFd, ev, "UHID_GET_REPORT_REPLY");
}
int Device::handleEvents(int events) {