summaryrefslogtreecommitdiff
path: root/modules/sensors/dynamic_sensor/HidUtils
diff options
context:
space:
mode:
authoralk3pInjection <webmaster@raspii.tech>2022-05-01 20:50:40 +0800
committeralk3pInjection <webmaster@raspii.tech>2022-05-01 20:50:40 +0800
commit5b2cac52ba778c82474b770b8218f12b200abbba (patch)
treeaa6a2935fe22ca8ee764f8d0c5637cc9e540a5f5 /modules/sensors/dynamic_sensor/HidUtils
parentfff55eaf71d44da7a003bccc4f835351b61efd6e (diff)
parent05445d8b448ea0349822250fa0c2a6f651289736 (diff)
Merge tag 'LA.QSSI.12.0.r1-06800-qssi.0' into sugisawa-mr1HEADsugisawa-mr1
"LA.QSSI.12.0.r1-06800-qssi.0" Change-Id: I3c5a85f5c37b5bba1590d2aa8f67e5b3f01939c8
Diffstat (limited to 'modules/sensors/dynamic_sensor/HidUtils')
-rw-r--r--modules/sensors/dynamic_sensor/HidUtils/Android.bp18
-rw-r--r--modules/sensors/dynamic_sensor/HidUtils/HidParser.cpp2
-rw-r--r--modules/sensors/dynamic_sensor/HidUtils/HidParser.h2
-rw-r--r--modules/sensors/dynamic_sensor/HidUtils/HidUtils.cpp72
-rw-r--r--modules/sensors/dynamic_sensor/HidUtils/HidUtils.h29
-rw-r--r--modules/sensors/dynamic_sensor/HidUtils/test/CopyBitsTest.cpp73
6 files changed, 196 insertions, 0 deletions
diff --git a/modules/sensors/dynamic_sensor/HidUtils/Android.bp b/modules/sensors/dynamic_sensor/HidUtils/Android.bp
index bbed0327..5823c794 100644
--- a/modules/sensors/dynamic_sensor/HidUtils/Android.bp
+++ b/modules/sensors/dynamic_sensor/HidUtils/Android.bp
@@ -45,6 +45,7 @@ cc_library {
"HidParser.cpp",
"HidReport.cpp",
"HidTree.cpp",
+ "HidUtils.cpp",
],
export_include_dirs: ["."],
@@ -99,3 +100,20 @@ cc_test_host {
local_include_dirs: ["test"],
}
+
+//
+// Test for HidUtils
+//
+cc_test_host {
+ name: "hid_utils_test",
+ defaults: ["hid_defaults"],
+
+ srcs: ["test/CopyBitsTest.cpp"],
+
+ shared_libs: [
+ "libhidparser",
+ ],
+
+ local_include_dirs: ["test"],
+}
+
diff --git a/modules/sensors/dynamic_sensor/HidUtils/HidParser.cpp b/modules/sensors/dynamic_sensor/HidUtils/HidParser.cpp
index 264f13c2..28d87d97 100644
--- a/modules/sensors/dynamic_sensor/HidUtils/HidParser.cpp
+++ b/modules/sensors/dynamic_sensor/HidUtils/HidParser.cpp
@@ -248,6 +248,7 @@ std::vector<HidParser::ReportPacket> HidParser::convertGroupToPacket(
ReportItem digest = {
.usage = r.getFullUsage(),
.id = id,
+ .usageVector = r.getUsageVector(),
.minRaw = logical.first,
.maxRaw = logical.second,
.a = scale,
@@ -316,4 +317,5 @@ std::ostream& operator<<(std::ostream &os, const HidParser::DigestVector &digest
os << LOG_ENDL;
return os;
}
+
} // namespace HidUtil
diff --git a/modules/sensors/dynamic_sensor/HidUtils/HidParser.h b/modules/sensors/dynamic_sensor/HidUtils/HidParser.h
index 4ef5ec6c..cb4a92a8 100644
--- a/modules/sensors/dynamic_sensor/HidUtils/HidParser.h
+++ b/modules/sensors/dynamic_sensor/HidUtils/HidParser.h
@@ -89,6 +89,7 @@ struct HidParser::ReportItem {
unsigned int usage;
unsigned int id;
int type; // feature, input or output
+ std::vector<unsigned int> usageVector;
int64_t minRaw;
int64_t maxRaw;
@@ -173,6 +174,7 @@ struct HidParser::ReportPacket {
};
std::ostream& operator<<(std::ostream &os, const HidParser::DigestVector &digest2);
+
} // namespace HidUtil
#endif // HIDUTIL_HIDPARSER_H_
diff --git a/modules/sensors/dynamic_sensor/HidUtils/HidUtils.cpp b/modules/sensors/dynamic_sensor/HidUtils/HidUtils.cpp
new file mode 100644
index 00000000..0cce2a39
--- /dev/null
+++ b/modules/sensors/dynamic_sensor/HidUtils/HidUtils.cpp
@@ -0,0 +1,72 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "HidUtils.h"
+#include <stdint.h>
+#include <algorithm>
+
+namespace HidUtil {
+
+void copyBits(const void *src, void *dst, size_t dst_size,
+ unsigned int src_bit_offset, unsigned int dst_bit_offset,
+ unsigned int bit_count) {
+ const uint8_t *p_src;
+ uint8_t *p_dst;
+ uint8_t dst_mask;
+ unsigned int bits_rem;
+ unsigned int bit_block_count;
+
+ // Do nothing if copying past the end of the destination buffer.
+ if ((static_cast<size_t>(dst_bit_offset) > (8 * dst_size)) ||
+ (static_cast<size_t>(bit_count) > (8 * dst_size)) ||
+ (static_cast<size_t>(dst_bit_offset + bit_count) > (8 * dst_size))) {
+ return;
+ }
+
+ // Copy bits from source to destination buffer.
+ p_src = static_cast<const uint8_t*>(src) + (src_bit_offset / 8);
+ src_bit_offset = src_bit_offset % 8;
+ p_dst = static_cast<uint8_t*>(dst) + (dst_bit_offset / 8);
+ dst_bit_offset = dst_bit_offset % 8;
+ bits_rem = bit_count;
+ while (bits_rem > 0) {
+ // Determine the size of the next block of bits to copy. The block must
+ // not cross a source or desintation byte boundary.
+ bit_block_count = std::min(bits_rem, 8 - src_bit_offset);
+ bit_block_count = std::min(bit_block_count, 8 - dst_bit_offset);
+
+ // Determine the destination bit block mask.
+ dst_mask = ((1 << bit_block_count) - 1) << dst_bit_offset;
+
+ // Copy the block of bits.
+ *p_dst = (*p_dst & ~dst_mask) |
+ (((*p_src >> src_bit_offset) << dst_bit_offset) & dst_mask);
+
+ // Advance past the block of copied bits in the source.
+ src_bit_offset += bit_block_count;
+ p_src += src_bit_offset / 8;
+ src_bit_offset = src_bit_offset % 8;
+
+ // Advance past the block of copied bits in the destination.
+ dst_bit_offset += bit_block_count;
+ p_dst += dst_bit_offset / 8;
+ dst_bit_offset = dst_bit_offset % 8;
+
+ // Decrement the number of bits remaining.
+ bits_rem -= bit_block_count;
+ }
+}
+
+} // namespace HidUtil
diff --git a/modules/sensors/dynamic_sensor/HidUtils/HidUtils.h b/modules/sensors/dynamic_sensor/HidUtils/HidUtils.h
new file mode 100644
index 00000000..54aa31e9
--- /dev/null
+++ b/modules/sensors/dynamic_sensor/HidUtils/HidUtils.h
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef HIDUTIL_HIDUTILS_H_
+#define HIDUTIL_HIDUTILS_H_
+
+#include <stddef.h>
+
+namespace HidUtil {
+
+void copyBits(const void *src, void *dst, size_t dst_size,
+ unsigned int src_bit_offset, unsigned int dst_bit_offset,
+ unsigned int bit_count);
+
+} // namespace HidUtil
+
+#endif // HIDUTIL_HIDUTILS_H_
diff --git a/modules/sensors/dynamic_sensor/HidUtils/test/CopyBitsTest.cpp b/modules/sensors/dynamic_sensor/HidUtils/test/CopyBitsTest.cpp
new file mode 100644
index 00000000..1b1ca709
--- /dev/null
+++ b/modules/sensors/dynamic_sensor/HidUtils/test/CopyBitsTest.cpp
@@ -0,0 +1,73 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "HidUtils.h"
+#include <gtest/gtest.h>
+
+using HidUtil::copyBits;
+
+TEST(CopyBitsTest, CopyBits) {
+ const struct {
+ uint32_t src;
+ uint32_t dst;
+ int src_bit_offset;
+ int dst_bit_offset;
+ int bit_count;
+ uint32_t expected_dst;
+ } kTestVectorList[] = {
+ { 0x00000005, 0x00000000, 0, 0, 8, 0x00000005 },
+ { 0x00000005, 0x00000000, 0, 4, 8, 0x00000050 },
+ { 0x0000000C, 0x00000020, 0, 4, 8, 0x000000C0 },
+ { 0x00000005, 0x0000F02F, 0, 4, 8, 0x0000F05F },
+ { 0x12345678, 0x87654321, 5, 11, 17, 0x8D159B21 },
+ { 0x12345678, 0x87654321, 11, 5, 17, 0x8748D141 },
+ };
+
+ for (auto test_vector : kTestVectorList) {
+ uint32_t dst = test_vector.dst;
+ copyBits(&(test_vector.src), &dst, sizeof(dst),
+ test_vector.src_bit_offset, test_vector.dst_bit_offset,
+ test_vector.bit_count);
+ EXPECT_EQ(test_vector.expected_dst, dst);
+ }
+}
+
+TEST(CopyBitsTest, Overflow) {
+ const struct {
+ uint32_t src;
+ uint32_t dst;
+ unsigned int src_bit_offset;
+ unsigned int dst_bit_offset;
+ unsigned int bit_count;
+ uint32_t expected_dst;
+ } kTestVectorList[] = {
+ { 0x000000FF, 0x00000000, 0, 0, 8, 0x000000FF },
+ { 0x000000FF, 0x00000000, 0, 24, 8, 0xFF000000 },
+ { 0x000000FF, 0x00000000, 0, 25, 8, 0x00000000 },
+ { 0x000000FF, 0x00000000, 0, 32, 8, 0x00000000 },
+ { 0x000000FF, 0x00000000, 0, UINT_MAX, 8, 0x00000000 },
+ { 0x000000FF, 0x00000000, 0, 8, UINT_MAX, 0x00000000 },
+ };
+
+ for (auto test_vector : kTestVectorList) {
+ uint32_t dst = test_vector.dst;
+ copyBits(&(test_vector.src), &dst, sizeof(dst),
+ test_vector.src_bit_offset, test_vector.dst_bit_offset,
+ test_vector.bit_count);
+ EXPECT_EQ(test_vector.expected_dst, dst);
+ }
+}
+