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 #ifndef V4L2_CAMERA_HAL_METADATA_V4L2_CONTROL_DELEGATE_H_ 18 #define V4L2_CAMERA_HAL_METADATA_V4L2_CONTROL_DELEGATE_H_ 19 20 #include "control_delegate_interface.h" 21 #include "converter_interface.h" 22 #include "v4l2_wrapper.h" 23 24 namespace v4l2_camera_hal { 25 26 // A V4L2ControlDelegate routes getting and setting through V4L2 27 template <typename TMetadata, typename TV4L2 = int32_t> 28 class V4L2ControlDelegate : public ControlDelegateInterface<TMetadata> { 29 public: V4L2ControlDelegate(std::shared_ptr<V4L2Wrapper> device,int control_id,std::shared_ptr<ConverterInterface<TMetadata,TV4L2>> converter)30 V4L2ControlDelegate( 31 std::shared_ptr<V4L2Wrapper> device, 32 int control_id, 33 std::shared_ptr<ConverterInterface<TMetadata, TV4L2>> converter) 34 : device_(std::move(device)), 35 control_id_(control_id), 36 converter_(std::move(converter)){}; 37 GetValue(TMetadata * value)38 int GetValue(TMetadata* value) override { 39 TV4L2 v4l2_value; 40 int res = device_->GetControl(control_id_, &v4l2_value); 41 if (res) { 42 HAL_LOGE("Failed to get device value for control %d.", control_id_); 43 return res; 44 } 45 return converter_->V4L2ToMetadata(v4l2_value, value); 46 }; 47 SetValue(const TMetadata & value)48 int SetValue(const TMetadata& value) override { 49 TV4L2 v4l2_value; 50 int res = converter_->MetadataToV4L2(value, &v4l2_value); 51 if (res) { 52 HAL_LOGE("Failed to convert metadata value to V4L2."); 53 return res; 54 } 55 return device_->SetControl(control_id_, v4l2_value); 56 }; 57 58 private: 59 std::shared_ptr<V4L2Wrapper> device_; 60 int control_id_; 61 std::shared_ptr<ConverterInterface<TMetadata, TV4L2>> converter_; 62 }; 63 64 } // namespace v4l2_camera_hal 65 66 #endif // V4L2_CAMERA_HAL_METADATA_V4L2_CONTROL_DELEGATE_H_ 67