1 /*
2 * Copyright (C) 2022 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 #define ATRACE_TAG (ATRACE_TAG_THERMAL | ATRACE_TAG_HAL)
18
19 #include "thermal_files.h"
20
21 #include <android-base/file.h>
22 #include <android-base/logging.h>
23 #include <android-base/stringprintf.h>
24 #include <android-base/strings.h>
25 #include <utils/Trace.h>
26
27 #include <algorithm>
28 #include <string_view>
29
30 namespace aidl {
31 namespace android {
32 namespace hardware {
33 namespace thermal {
34 namespace implementation {
35
36 using ::android::base::StringPrintf;
37
getThermalFilePath(std::string_view thermal_name) const38 std::string ThermalFiles::getThermalFilePath(std::string_view thermal_name) const {
39 auto sensor_itr = thermal_name_to_path_map_.find(thermal_name.data());
40 if (sensor_itr == thermal_name_to_path_map_.end()) {
41 return "";
42 }
43 return sensor_itr->second;
44 }
45
addThermalFile(std::string_view thermal_name,std::string_view path)46 bool ThermalFiles::addThermalFile(std::string_view thermal_name, std::string_view path) {
47 return thermal_name_to_path_map_.emplace(thermal_name, path).second;
48 }
49
readThermalFile(std::string_view thermal_name,std::string * data) const50 bool ThermalFiles::readThermalFile(std::string_view thermal_name, std::string *data) const {
51 std::string sensor_reading;
52 std::string file_path = getThermalFilePath(std::string_view(thermal_name));
53 *data = "";
54
55 ATRACE_NAME(StringPrintf("ThermalFiles::readThermalFile - %s", thermal_name.data()).c_str());
56 if (file_path.empty()) {
57 PLOG(WARNING) << "Failed to find " << thermal_name << "'s path";
58 return false;
59 }
60
61 if (!::android::base::ReadFileToString(file_path, &sensor_reading)) {
62 PLOG(WARNING) << "Failed to read sensor: " << thermal_name;
63 return false;
64 }
65
66 if (sensor_reading.size() <= 1) {
67 LOG(ERROR) << thermal_name << "'s return size:" << sensor_reading.size() << " is invalid";
68 return false;
69 }
70
71 // Strip the newline.
72 *data = ::android::base::Trim(sensor_reading);
73 return true;
74 }
75
writeCdevFile(std::string_view cdev_name,std::string_view data)76 bool ThermalFiles::writeCdevFile(std::string_view cdev_name, std::string_view data) {
77 std::string file_path =
78 getThermalFilePath(::android::base::StringPrintf("%s_%s", cdev_name.data(), "w"));
79
80 ATRACE_NAME(StringPrintf("ThermalFiles::writeCdevFile - %s", cdev_name.data()).c_str());
81 if (!::android::base::WriteStringToFile(data.data(), file_path)) {
82 PLOG(WARNING) << "Failed to write cdev: " << cdev_name << " to " << data.data();
83 return false;
84 }
85
86 return true;
87 }
88
89 } // namespace implementation
90 } // namespace thermal
91 } // namespace hardware
92 } // namespace android
93 } // namespace aidl
94