Flow: 1. If the calibration file exist, use the value. 2. If no property, use the default setting {5,95}. Bug: 356823441 Bug: 322937989 Test: Check dumpsys records for the scaling results. case1: no calibration file and properties. case2: valid property range case3: float property (invalid) Flag: EXEMPT bugfix Change-Id: Ia3e89f34189e9725ab01d85d931925129745608c (cherry picked from commit 4063493a64fcd0f20b49a492106f58b0e3349c5c)
188 lines
5.1 KiB
C++
188 lines
5.1 KiB
C++
/*
|
|
* Copyright (C) 2019 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.
|
|
*/
|
|
#pragma once
|
|
|
|
#include <android-base/macros.h>
|
|
#include <android-base/parsedouble.h>
|
|
#include <android-base/properties.h>
|
|
#include <log/log.h>
|
|
|
|
#include <fstream>
|
|
#include <map>
|
|
#include <sstream>
|
|
|
|
namespace aidl {
|
|
namespace android {
|
|
namespace hardware {
|
|
namespace vibrator {
|
|
namespace utils {
|
|
|
|
template <typename T>
|
|
class Is_Iterable {
|
|
private:
|
|
template <typename U>
|
|
static std::true_type test(typename U::iterator *u);
|
|
|
|
template <typename U>
|
|
static std::false_type test(U *u);
|
|
|
|
public:
|
|
static const bool value = decltype(test<T>(0))::value;
|
|
};
|
|
|
|
template <typename T, bool B>
|
|
using Enable_If_Iterable = std::enable_if_t<Is_Iterable<T>::value == B>;
|
|
|
|
template <typename T, typename U = void>
|
|
using Enable_If_Signed = std::enable_if_t<std::is_signed_v<T>, U>;
|
|
|
|
template <typename T, typename U = void>
|
|
using Enable_If_Unsigned = std::enable_if_t<std::is_unsigned_v<T>, U>;
|
|
|
|
// override for default behavior of printing as a character
|
|
inline std::ostream &operator<<(std::ostream &stream, const int8_t value) {
|
|
return stream << +value;
|
|
}
|
|
// override for default behavior of printing as a character
|
|
inline std::ostream &operator<<(std::ostream &stream, const uint8_t value) {
|
|
return stream << +value;
|
|
}
|
|
|
|
template <typename T>
|
|
inline auto toUnderlying(const T value) {
|
|
return static_cast<std::underlying_type_t<T>>(value);
|
|
}
|
|
|
|
template <typename T>
|
|
inline Enable_If_Iterable<T, true> unpack(std::istream &stream, T *value) {
|
|
for (auto &entry : *value) {
|
|
stream >> entry;
|
|
}
|
|
}
|
|
|
|
template <typename T>
|
|
inline Enable_If_Iterable<T, false> unpack(std::istream &stream, T *value) {
|
|
stream >> *value;
|
|
}
|
|
|
|
template <>
|
|
inline void unpack<std::string>(std::istream &stream, std::string *value) {
|
|
*value = std::string(std::istreambuf_iterator(stream), {});
|
|
stream.setstate(std::istream::eofbit);
|
|
}
|
|
|
|
template <typename T>
|
|
inline Enable_If_Signed<T, T> getProperty(const std::string &key, const T def) {
|
|
if (std::is_floating_point_v<T>) {
|
|
float result;
|
|
std::string value = ::android::base::GetProperty(key, "");
|
|
if (!value.empty() && ::android::base::ParseFloat(value, &result)) {
|
|
return result;
|
|
}
|
|
return def;
|
|
} else {
|
|
return ::android::base::GetIntProperty(key, def);
|
|
}
|
|
}
|
|
|
|
template <typename T>
|
|
inline Enable_If_Unsigned<T, T> getProperty(const std::string &key, const T def) {
|
|
return ::android::base::GetUintProperty(key, def);
|
|
}
|
|
|
|
template <typename T, size_t N>
|
|
inline std::array<T, N> getProperty(const std::string &key, const std::array<T, N> &def) {
|
|
std::string value = ::android::base::GetProperty(key, "");
|
|
if (!value.empty()) {
|
|
std::array<T, N> result{0};
|
|
std::stringstream stream{value};
|
|
utils::unpack(stream, &result);
|
|
if (stream && stream.eof())
|
|
return result;
|
|
}
|
|
return def;
|
|
}
|
|
|
|
template <>
|
|
inline bool getProperty<bool>(const std::string &key, const bool def) {
|
|
return ::android::base::GetBoolProperty(key, def);
|
|
}
|
|
|
|
template <typename T>
|
|
static void openNoCreate(const std::string &file, T *outStream) {
|
|
if (!std::filesystem::exists(file)) {
|
|
ALOGE("File does not exist: %s", file.c_str());
|
|
return;
|
|
}
|
|
|
|
outStream->open(file);
|
|
if (!*outStream) {
|
|
ALOGE("Failed to open %s (%d): %s", file.c_str(), errno, strerror(errno));
|
|
}
|
|
}
|
|
|
|
template <typename T>
|
|
static void fileFromEnv(const char *env, T *outStream, std::string *outName = nullptr) {
|
|
auto file = std::getenv(env);
|
|
|
|
if (file == nullptr) {
|
|
ALOGE("Failed get env %s", env);
|
|
return;
|
|
}
|
|
|
|
if (outName != nullptr) {
|
|
*outName = std::string(file);
|
|
}
|
|
|
|
openNoCreate(file, outStream);
|
|
}
|
|
|
|
static ATTRIBUTE_UNUSED auto pathsFromEnv(const char *env, const std::string &prefix = "") {
|
|
std::map<std::string, std::ifstream> ret;
|
|
auto value = std::getenv(env);
|
|
|
|
if (value == nullptr) {
|
|
return ret;
|
|
}
|
|
|
|
std::istringstream paths{value};
|
|
std::string path;
|
|
|
|
while (paths >> path) {
|
|
ret[path].open(prefix + path);
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
static ATTRIBUTE_UNUSED std::string trim(const std::string &str,
|
|
const std::string &whitespace = " \t") {
|
|
const auto str_begin = str.find_first_not_of(whitespace);
|
|
if (str_begin == std::string::npos) {
|
|
return "";
|
|
}
|
|
|
|
const auto str_end = str.find_last_not_of(whitespace);
|
|
const auto str_range = str_end - str_begin + 1;
|
|
|
|
return str.substr(str_begin, str_range);
|
|
}
|
|
|
|
} // namespace utils
|
|
} // namespace vibrator
|
|
} // namespace hardware
|
|
} // namespace android
|
|
} // namespace aidl
|