Add enum converter.
Converts between two different enumerations.
BUG: 30140438
TEST: Unit tests pass
Change-Id: I0976c440f0a48491fa52d26e173808917d53ebe7
diff --git a/modules/camera/3_4/Android.mk b/modules/camera/3_4/Android.mk
index 278a351..4c77968 100644
--- a/modules/camera/3_4/Android.mk
+++ b/modules/camera/3_4/Android.mk
@@ -39,6 +39,7 @@
v4l2_src_files := \
camera.cpp \
+ metadata/enum_converter.cpp \
metadata/metadata.cpp \
metadata/v4l2_enum_control.cpp \
stream.cpp \
@@ -51,6 +52,7 @@
v4l2_test_files := \
metadata/control_test.cpp \
+ metadata/enum_converter_test.cpp \
metadata/fixed_property_test.cpp \
metadata/ignored_control_test.cpp \
metadata/metadata_test.cpp \
diff --git a/modules/camera/3_4/metadata/enum_converter.cpp b/modules/camera/3_4/metadata/enum_converter.cpp
new file mode 100644
index 0000000..3165804
--- /dev/null
+++ b/modules/camera/3_4/metadata/enum_converter.cpp
@@ -0,0 +1,82 @@
+/*
+ * Copyright (C) 2016 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 "enum_converter.h"
+
+#include <errno.h>
+
+#include "../common.h"
+
+namespace v4l2_camera_hal {
+
+EnumConverter::EnumConverter(
+ const std::multimap<int32_t, uint8_t>& v4l2_to_metadata)
+ : v4l2_to_metadata_(v4l2_to_metadata) {
+ HAL_LOG_ENTER();
+}
+
+int EnumConverter::MetadataToV4L2(uint8_t value, int32_t* conversion) {
+ HAL_LOG_ENTER();
+
+ // Unfortunately no bi-directional map lookup in C++.
+ // Breaking on second, not first found so that a warning
+ // can be given if there are multiple values.
+ size_t count = 0;
+ for (auto kv : v4l2_to_metadata_) {
+ if (kv.second == value) {
+ ++count;
+ if (count == 1) {
+ // First match.
+ *conversion = kv.first;
+ } else {
+ // second match.
+ break;
+ }
+ }
+ }
+
+ if (count == 0) {
+ HAL_LOGE("Couldn't find V4L2 conversion of metadata value %d.", value);
+ return -EINVAL;
+ } else if (count > 1) {
+ HAL_LOGV(
+ "Multiple V4L2 conversions found for metadata value %d, using first.",
+ value);
+ }
+ return 0;
+}
+
+int EnumConverter::V4L2ToMetadata(int32_t value, uint8_t* conversion) {
+ HAL_LOG_ENTER();
+
+ auto element_range = v4l2_to_metadata_.equal_range(value);
+ if (element_range.first == element_range.second) {
+ HAL_LOGE("Couldn't find metadata conversion of V4L2 value %d.", value);
+ return -EINVAL;
+ }
+
+ auto element = element_range.first;
+ *conversion = element->second;
+
+ if (++element != element_range.second) {
+ HAL_LOGV(
+ "Multiple metadata conversions found for V4L2 value %d, using first.",
+ value);
+ }
+ return 0;
+}
+
+} // namespace v4l2_camera_hal
diff --git a/modules/camera/3_4/metadata/enum_converter.h b/modules/camera/3_4/metadata/enum_converter.h
new file mode 100644
index 0000000..df5cabb
--- /dev/null
+++ b/modules/camera/3_4/metadata/enum_converter.h
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2016 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 V4L2_CAMERA_HAL_METADATA_ENUM_CONVERTER_H_
+#define V4L2_CAMERA_HAL_METADATA_ENUM_CONVERTER_H_
+
+#include <map>
+
+#include "../common.h"
+#include "converter_interface.h"
+
+namespace v4l2_camera_hal {
+
+// An EnumConverter converts between enum values.
+class EnumConverter : public ConverterInterface<uint8_t, int32_t> {
+ public:
+ EnumConverter(const std::multimap<int32_t, uint8_t>& v4l2_to_metadata);
+
+ virtual int MetadataToV4L2(uint8_t value, int32_t* conversion) override;
+ virtual int V4L2ToMetadata(int32_t value, uint8_t* conversion) override;
+
+ private:
+ const std::multimap<int32_t, uint8_t> v4l2_to_metadata_;
+
+ DISALLOW_COPY_AND_ASSIGN(EnumConverter);
+};
+
+} // namespace v4l2_camera_hal
+
+#endif // V4L2_CAMERA_HAL_METADATA_ENUM_CONVERTER_H_
diff --git a/modules/camera/3_4/metadata/enum_converter_test.cpp b/modules/camera/3_4/metadata/enum_converter_test.cpp
new file mode 100644
index 0000000..9ba7ffc
--- /dev/null
+++ b/modules/camera/3_4/metadata/enum_converter_test.cpp
@@ -0,0 +1,100 @@
+/*
+ * Copyright (C) 2016 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 "enum_converter.h"
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+using testing::Test;
+
+namespace v4l2_camera_hal {
+
+class EnumConverterTest : public Test {
+ protected:
+ virtual void SetUp() {
+ converter_.reset(
+ new EnumConverter({{one_to_one_v4l2_, one_to_one_metadata_},
+ {one_to_many_v4l2_, many_to_one_metadata_1_},
+ {one_to_many_v4l2_, many_to_one_metadata_2_},
+ {many_to_one_v4l2_1_, one_to_many_metadata_},
+ {many_to_one_v4l2_2_, one_to_many_metadata_},
+ {unused_v4l2_, unused_metadata_}}));
+ }
+
+ std::unique_ptr<EnumConverter> converter_;
+
+ const int32_t one_to_one_v4l2_ = 12;
+ const int32_t one_to_many_v4l2_ = 34;
+ const int32_t many_to_one_v4l2_1_ = 56;
+ const int32_t many_to_one_v4l2_2_ = 78;
+ const int32_t unused_v4l2_ = 910;
+ const uint8_t one_to_one_metadata_ = 109;
+ const uint8_t one_to_many_metadata_ = 87;
+ const uint8_t many_to_one_metadata_1_ = 65;
+ const uint8_t many_to_one_metadata_2_ = 43;
+ const uint8_t unused_metadata_ = 21;
+};
+
+// Convert single.
+TEST_F(EnumConverterTest, OneToOneConversion) {
+ uint8_t metadata_val = 1;
+ ASSERT_EQ(converter_->V4L2ToMetadata(one_to_one_v4l2_, &metadata_val), 0);
+ EXPECT_EQ(metadata_val, one_to_one_metadata_);
+
+ int32_t v4l2_val = 1;
+ ASSERT_EQ(converter_->MetadataToV4L2(one_to_one_metadata_, &v4l2_val), 0);
+ EXPECT_EQ(v4l2_val, one_to_one_v4l2_);
+}
+
+TEST_F(EnumConverterTest, OneToManyConversion) {
+ // Should be one of the acceptable values.
+ uint8_t metadata_val = 1;
+ ASSERT_EQ(converter_->V4L2ToMetadata(one_to_many_v4l2_, &metadata_val), 0);
+ EXPECT_TRUE(metadata_val == many_to_one_metadata_1_ ||
+ metadata_val == many_to_one_metadata_2_);
+
+ int32_t v4l2_val = 1;
+ ASSERT_EQ(converter_->MetadataToV4L2(one_to_many_metadata_, &v4l2_val), 0);
+ EXPECT_TRUE(v4l2_val == many_to_one_v4l2_1_ ||
+ v4l2_val == many_to_one_v4l2_2_);
+}
+
+TEST_F(EnumConverterTest, ManyToOneConversion) {
+ uint8_t metadata_val = 1;
+ ASSERT_EQ(converter_->V4L2ToMetadata(many_to_one_v4l2_1_, &metadata_val), 0);
+ EXPECT_EQ(metadata_val, one_to_many_metadata_);
+ metadata_val = 1; // Reset.
+ ASSERT_EQ(converter_->V4L2ToMetadata(many_to_one_v4l2_2_, &metadata_val), 0);
+ EXPECT_EQ(metadata_val, one_to_many_metadata_);
+
+ int32_t v4l2_val = 1;
+ ASSERT_EQ(converter_->MetadataToV4L2(many_to_one_metadata_1_, &v4l2_val), 0);
+ EXPECT_EQ(v4l2_val, one_to_many_v4l2_);
+ v4l2_val = 1; // Reset.
+ ASSERT_EQ(converter_->MetadataToV4L2(many_to_one_metadata_2_, &v4l2_val), 0);
+ EXPECT_EQ(v4l2_val, one_to_many_v4l2_);
+}
+
+TEST_F(EnumConverterTest, InvalidConversion) {
+ uint8_t metadata_val = 1;
+ EXPECT_EQ(converter_->V4L2ToMetadata(1, &metadata_val), -EINVAL);
+
+ int32_t v4l2_val = 1;
+ EXPECT_EQ(converter_->MetadataToV4L2(1, &v4l2_val), -EINVAL);
+}
+
+} // namespace v4l2_camera_hal