1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "enum_converter.h" 18 19 #include <errno.h> 20 21 #include "../common.h" 22 23 namespace v4l2_camera_hal { 24 25 EnumConverter::EnumConverter( 26 const std::multimap<int32_t, uint8_t>& v4l2_to_metadata) 27 : v4l2_to_metadata_(v4l2_to_metadata) { 28 HAL_LOG_ENTER(); 29 } 30 31 int EnumConverter::MetadataToV4L2(uint8_t value, int32_t* conversion) { 32 // Unfortunately no bi-directional map lookup in C++. 33 // Breaking on second, not first found so that a warning 34 // can be given if there are multiple values. 35 size_t count = 0; 36 for (auto kv : v4l2_to_metadata_) { 37 if (kv.second == value) { 38 ++count; 39 if (count == 1) { 40 // First match. 41 *conversion = kv.first; 42 } else { 43 // second match. 44 break; 45 } 46 } 47 } 48 49 if (count == 0) { 50 HAL_LOGV("Couldn't find V4L2 conversion of metadata value %d.", value); 51 return -EINVAL; 52 } else if (count > 1) { 53 HAL_LOGV( 54 "Multiple V4L2 conversions found for metadata value %d, using first.", 55 value); 56 } 57 return 0; 58 } 59 60 int EnumConverter::V4L2ToMetadata(int32_t value, uint8_t* conversion) { 61 auto element_range = v4l2_to_metadata_.equal_range(value); 62 if (element_range.first == element_range.second) { 63 HAL_LOGV("Couldn't find metadata conversion of V4L2 value %d.", value); 64 return -EINVAL; 65 } 66 67 auto element = element_range.first; 68 *conversion = element->second; 69 70 if (++element != element_range.second) { 71 HAL_LOGV( 72 "Multiple metadata conversions found for V4L2 value %d, using first.", 73 value); 74 } 75 return 0; 76 } 77 78 } // namespace v4l2_camera_hal 79