1 /*
2  * Copyright (C) 2019 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 "ConfigManagerUtil.h"
18 
19 #include <log/log.h>
20 #include <system/graphics-base-v1.0.h>
21 
22 #include <linux/videodev2.h>
23 #include <sstream>
24 #include <string>
25 
26 namespace android::hardware::automotive::evs::V1_1::implementation {
27 
28 using namespace std;
29 
convertToEvsCameraParam(const string & id,CameraParam & camParam)30 bool ConfigManagerUtil::convertToEvsCameraParam(const string& id, CameraParam& camParam) {
31     string trimmed = ConfigManagerUtil::trimString(id);
32     bool success = true;
33 
34     if (!trimmed.compare("BRIGHTNESS")) {
35         camParam = CameraParam::BRIGHTNESS;
36     } else if (!trimmed.compare("CONTRAST")) {
37         camParam = CameraParam::CONTRAST;
38     } else if (!trimmed.compare("AUTOGAIN")) {
39         camParam = CameraParam::AUTOGAIN;
40     } else if (!trimmed.compare("GAIN")) {
41         camParam = CameraParam::GAIN;
42     } else if (!trimmed.compare("AUTO_WHITE_BALANCE")) {
43         camParam = CameraParam::AUTO_WHITE_BALANCE;
44     } else if (!trimmed.compare("WHITE_BALANCE_TEMPERATURE")) {
45         camParam = CameraParam::WHITE_BALANCE_TEMPERATURE;
46     } else if (!trimmed.compare("SHARPNESS")) {
47         camParam = CameraParam::SHARPNESS;
48     } else if (!trimmed.compare("AUTO_EXPOSURE")) {
49         camParam = CameraParam::AUTO_EXPOSURE;
50     } else if (!trimmed.compare("ABSOLUTE_EXPOSURE")) {
51         camParam = CameraParam::ABSOLUTE_EXPOSURE;
52     } else if (!trimmed.compare("ABSOLUTE_FOCUS")) {
53         camParam = CameraParam::ABSOLUTE_FOCUS;
54     } else if (!trimmed.compare("AUTO_FOCUS")) {
55         camParam = CameraParam::AUTO_FOCUS;
56     } else if (!trimmed.compare("ABSOLUTE_ZOOM")) {
57         camParam = CameraParam::ABSOLUTE_ZOOM;
58     } else {
59         success = false;
60     }
61 
62     return success;
63 }
64 
convertToPixelFormat(const string & format,int32_t & pixFormat)65 bool ConfigManagerUtil::convertToPixelFormat(const string& format, int32_t& pixFormat) {
66     string trimmed = ConfigManagerUtil::trimString(format);
67     bool success = true;
68 
69     if (!trimmed.compare("RGBA_8888")) {
70         pixFormat = HAL_PIXEL_FORMAT_RGBA_8888;
71     } else if (!trimmed.compare("YCRCB_420_SP")) {
72         pixFormat = HAL_PIXEL_FORMAT_YCRCB_420_SP;
73     } else if (!trimmed.compare("YCBCR_422_I")) {
74         pixFormat = HAL_PIXEL_FORMAT_YCBCR_422_I;
75     } else {
76         success = false;
77     }
78 
79     return success;
80 }
81 
convertToMetadataTag(const char * name,camera_metadata_tag & aTag)82 bool ConfigManagerUtil::convertToMetadataTag(const char* name, camera_metadata_tag& aTag) {
83     if (!strcmp(name, "LENS_DISTORTION")) {
84         aTag = ANDROID_LENS_DISTORTION;
85     } else if (!strcmp(name, "LENS_INTRINSIC_CALIBRATION")) {
86         aTag = ANDROID_LENS_INTRINSIC_CALIBRATION;
87     } else if (!strcmp(name, "LENS_POSE_ROTATION")) {
88         aTag = ANDROID_LENS_POSE_ROTATION;
89     } else if (!strcmp(name, "LENS_POSE_TRANSLATION")) {
90         aTag = ANDROID_LENS_POSE_TRANSLATION;
91     } else if (!strcmp(name, "REQUEST_AVAILABLE_CAPABILITIES")) {
92         aTag = ANDROID_REQUEST_AVAILABLE_CAPABILITIES;
93     } else if (!strcmp(name, "LOGICAL_MULTI_CAMERA_PHYSICAL_IDS")) {
94         aTag = ANDROID_LOGICAL_MULTI_CAMERA_PHYSICAL_IDS;
95     } else {
96         return false;
97     }
98 
99     return true;
100 }
101 
convertToCameraCapability(const char * name,camera_metadata_enum_android_request_available_capabilities_t & cap)102 bool ConfigManagerUtil::convertToCameraCapability(
103         const char* name, camera_metadata_enum_android_request_available_capabilities_t& cap) {
104     if (!strcmp(name, "DEPTH_OUTPUT")) {
105         cap = ANDROID_REQUEST_AVAILABLE_CAPABILITIES_DEPTH_OUTPUT;
106     } else if (!strcmp(name, "LOGICAL_MULTI_CAMERA")) {
107         cap = ANDROID_REQUEST_AVAILABLE_CAPABILITIES_LOGICAL_MULTI_CAMERA;
108     } else if (!strcmp(name, "MONOCHROME")) {
109         cap = ANDROID_REQUEST_AVAILABLE_CAPABILITIES_MONOCHROME;
110     } else if (!strcmp(name, "SECURE_IMAGE_DATA")) {
111         cap = ANDROID_REQUEST_AVAILABLE_CAPABILITIES_SECURE_IMAGE_DATA;
112     } else {
113         return false;
114     }
115 
116     return true;
117 }
118 
convertFloatArray(const char * sz,const char * vals,size_t & count,const char delimiter)119 float* ConfigManagerUtil::convertFloatArray(const char* sz, const char* vals, size_t& count,
120                                             const char delimiter) {
121     string size_string(sz);
122     string value_string(vals);
123 
124     count = stoi(size_string);
125     float* result = new float[count];
126     stringstream values(value_string);
127 
128     int32_t idx = 0;
129     string token;
130     while (getline(values, token, delimiter)) {
131         result[idx++] = stof(token);
132     }
133 
134     return result;
135 }
136 
trimString(const string & src,const string & ws)137 string ConfigManagerUtil::trimString(const string& src, const string& ws) {
138     const auto s = src.find_first_not_of(ws);
139     if (s == string::npos) {
140         return "";
141     }
142 
143     const auto e = src.find_last_not_of(ws);
144     const auto r = e - s + 1;
145 
146     return src.substr(s, r);
147 }
148 
149 }  // namespace android::hardware::automotive::evs::V1_1::implementation
150