1 /*
2 * Copyright (C) 2018 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 <algorithm>
18 #include <string_view>
19
20 #include <android-base/file.h>
21 #include <android-base/logging.h>
22 #include <android-base/strings.h>
23 #include "thermal_files.h"
24
25 namespace android {
26 namespace hardware {
27 namespace thermal {
28 namespace V2_0 {
29 namespace implementation {
30
getThermalFilePath(std::string_view thermal_name) const31 std::string ThermalFiles::getThermalFilePath(std::string_view thermal_name) const {
32 auto sensor_itr = thermal_name_to_path_map_.find(thermal_name.data());
33 if (sensor_itr == thermal_name_to_path_map_.end()) {
34 return "";
35 }
36 return sensor_itr->second;
37 }
38
addThermalFile(std::string_view thermal_name,std::string_view path)39 bool ThermalFiles::addThermalFile(std::string_view thermal_name, std::string_view path) {
40 return thermal_name_to_path_map_.emplace(thermal_name, path).second;
41 }
42
readThermalFile(std::string_view thermal_name,std::string * data) const43 bool ThermalFiles::readThermalFile(std::string_view thermal_name, std::string *data) const {
44 std::string sensor_reading;
45 std::string file_path = getThermalFilePath(std::string_view(thermal_name));
46 *data = "";
47 if (file_path.empty()) {
48 return false;
49 }
50
51 if (!::android::base::ReadFileToString(file_path, &sensor_reading)) {
52 PLOG(WARNING) << "Failed to read sensor: " << thermal_name;
53 return false;
54 }
55
56 // Strip the newline.
57 *data = ::android::base::Trim(sensor_reading);
58 return true;
59 }
60
61 } // namespace implementation
62 } // namespace V2_0
63 } // namespace thermal
64 } // namespace hardware
65 } // namespace android
66