• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 android {
31 namespace hardware {
32 namespace thermal {
33 namespace V2_0 {
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     // Strip the newline.
67     *data = ::android::base::Trim(sensor_reading);
68     return true;
69 }
70 
writeCdevFile(std::string_view cdev_name,std::string_view data)71 bool ThermalFiles::writeCdevFile(std::string_view cdev_name, std::string_view data) {
72     std::string file_path =
73             getThermalFilePath(android::base::StringPrintf("%s_%s", cdev_name.data(), "w"));
74 
75     ATRACE_NAME(StringPrintf("ThermalFiles::writeCdevFile - %s", cdev_name.data()).c_str());
76     if (!android::base::WriteStringToFile(data.data(), file_path)) {
77         PLOG(WARNING) << "Failed to write cdev: " << cdev_name << " to " << data.data();
78         return false;
79     }
80 
81     return true;
82 }
83 
84 }  // namespace implementation
85 }  // namespace V2_0
86 }  // namespace thermal
87 }  // namespace hardware
88 }  // namespace android
89