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_MENU_CONTROL_OPTIONS_H_
18 #define V4L2_CAMERA_HAL_METADATA_MENU_CONTROL_OPTIONS_H_
19 
20 #include <cerrno>
21 
22 #include "common.h"
23 #include "control_options_interface.h"
24 #include "default_option_delegate.h"
25 
26 namespace v4l2_camera_hal {
27 
28 // MenuControlOptions offer a fixed list of acceptable values.
29 template <typename T>
30 class MenuControlOptions : public ControlOptionsInterface<T> {
31  public:
32   // |options| must be non-empty.
MenuControlOptions(std::vector<T> options,std::shared_ptr<DefaultOptionDelegate<T>> defaults)33   MenuControlOptions(std::vector<T> options,
34                      std::shared_ptr<DefaultOptionDelegate<T>> defaults)
35       : options_(options), defaults_(defaults){};
MenuControlOptions(std::vector<T> options,std::map<int,T> defaults)36   MenuControlOptions(std::vector<T> options, std::map<int, T> defaults)
37       : options_(options),
38         defaults_(std::make_shared<DefaultOptionDelegate<T>>(defaults)){};
39 
MetadataRepresentation()40   virtual std::vector<T> MetadataRepresentation() override { return options_; };
IsSupported(const T & option)41   virtual bool IsSupported(const T& option) override {
42     return (std::find(options_.begin(), options_.end(), option) !=
43             options_.end());
44   };
DefaultValueForTemplate(int template_type,T * default_value)45   virtual int DefaultValueForTemplate(int template_type,
46                                       T* default_value) override {
47     // Default to the first option.
48     if (options_.empty()) {
49       HAL_LOGE("Can't get default value, options are empty.");
50       return -ENODEV;
51     }
52 
53     // Try to get it from the defaults delegate.
54     if (defaults_->DefaultValueForTemplate(template_type, default_value) &&
55         IsSupported(*default_value)) {
56       return 0;
57     }
58 
59     // Fall back to the first available.
60     *default_value = options_[0];
61     return 0;
62   };
63 
64  private:
65   std::vector<T> options_;
66   std::shared_ptr<DefaultOptionDelegate<T>> defaults_;
67 };
68 
69 }  // namespace v4l2_camera_hal
70 
71 #endif  // V4L2_CAMERA_HAL_METADATA_MENU_CONTROL_OPTIONS_H_
72