summaryrefslogtreecommitdiff
path: root/libs/input/Input.cpp
diff options
context:
space:
mode:
author Siarhei Vishniakou <svv@google.com> 2022-09-30 08:51:23 -0700
committer Siarhei Vishniakou <svv@google.com> 2022-10-14 16:47:36 -0700
commit31977184520e99110e1deadceb6197636f76450a (patch)
tree647bdda2d1f182ae7ee7e7fb8390e168d77a8ece /libs/input/Input.cpp
parentc08105877d67f18d6592ffacaf2402f0d07953b4 (diff)
Run some inputflinger_tests on host
Sometimes, it's convenient to execute the input tests without having a connected device. This is especially useful when doing cherry-picks of patch on a cloud device. Allow input code to build for host, and enable the tests for running on host. Bug: 249591924 Test: atest --host --no-bazel-mode -c -m inputflinger_tests Change-Id: Ib9be6a5fb6c35ffc450e41cb2a5688bfb2c8d01a
Diffstat (limited to 'libs/input/Input.cpp')
-rw-r--r--libs/input/Input.cpp36
1 files changed, 20 insertions, 16 deletions
diff --git a/libs/input/Input.cpp b/libs/input/Input.cpp
index 579b28e588..a6f6b14bae 100644
--- a/libs/input/Input.cpp
+++ b/libs/input/Input.cpp
@@ -22,6 +22,7 @@
#include <inttypes.h>
#include <string.h>
+#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/stringprintf.h>
#include <cutils/compiler.h>
@@ -34,9 +35,6 @@
#ifdef __linux__
#include <binder/Parcel.h>
#endif
-#ifdef __ANDROID__
-#include <sys/random.h>
-#endif
using android::base::StringPrintf;
@@ -112,28 +110,34 @@ const char* motionToolTypeToString(int32_t toolType) {
}
// --- IdGenerator ---
+
+static status_t getRandomBytes(uint8_t* data, size_t size) {
+ int ret = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOFOLLOW));
+ if (ret == -1) {
+ return -errno;
+ }
+
+ base::unique_fd fd(ret);
+ if (!base::ReadFully(fd, data, size)) {
+ return -errno;
+ }
+ return OK;
+}
+
IdGenerator::IdGenerator(Source source) : mSource(source) {}
int32_t IdGenerator::nextId() const {
constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK;
int32_t id = 0;
-// Avoid building against syscall getrandom(2) on host, which will fail build on Mac. Host doesn't
-// use sequence number so just always return mSource.
-#ifdef __ANDROID__
- constexpr size_t BUF_LEN = sizeof(id);
- size_t totalBytes = 0;
- while (totalBytes < BUF_LEN) {
- ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK));
- if (CC_UNLIKELY(bytes < 0)) {
- ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno));
- id = 0;
+#if defined(__linux__)
+ while (true) {
+ status_t result = getRandomBytes(reinterpret_cast<uint8_t*>(&id), sizeof(id));
+ if (result == OK) {
break;
}
- totalBytes += bytes;
}
-#endif // __ANDROID__
-
+#endif // __linux__
return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource);
}