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 19 #include <android-base/file.h> 20 #include <android-base/logging.h> 21 #include <android-base/strings.h> 22 #include <android-base/stringprintf.h> 23 #include "sensors.h" 24 25 namespace android { 26 namespace hardware { 27 namespace thermal { 28 namespace V1_1 { 29 namespace implementation { 30 31 std::string Sensors::getSensorPath(const std::string& sensor_name) { 32 if (sensor_name_to_data_map_.find(sensor_name) != 33 sensor_name_to_data_map_.end()) { 34 return std::get<0>(sensor_name_to_data_map_.at(sensor_name)); 35 } 36 return ""; 37 } 38 39 bool Sensors::addSensor( 40 const std::string& sensor_name, const std::string& path, 41 const float throttling_threshold, const float shutdown_threshold, 42 const float vr_threshold, const TemperatureType& type) { 43 return sensor_name_to_data_map_.emplace( 44 sensor_name, std::make_tuple( 45 path, throttling_threshold, shutdown_threshold, 46 vr_threshold, type)).second; 47 } 48 49 bool Sensors::readSensorFile( 50 const std::string& sensor_name, std::string* data, 51 std::string* file_path) const { 52 std::string sensor_reading; 53 if (sensor_name_to_data_map_.find(sensor_name) == 54 sensor_name_to_data_map_.end()) { 55 *data = ""; 56 *file_path = ""; 57 return false; 58 } 59 60 android::base::ReadFileToString( 61 std::get<0>(sensor_name_to_data_map_.at(sensor_name)), &sensor_reading); 62 // Strip the newline. 63 *data = ::android::base::Trim(sensor_reading); 64 *file_path = std::get<0>(sensor_name_to_data_map_.at(sensor_name)); 65 return true; 66 } 67 68 bool Sensors::readTemperature( 69 const std::string& sensor_name, const float mult, 70 Temperature* out) const { 71 if (sensor_name_to_data_map_.find(sensor_name) == 72 sensor_name_to_data_map_.end()) { 73 return false; 74 } 75 76 std::string sensor_reading; 77 std::string path; 78 readSensorFile(sensor_name, &sensor_reading, &path); 79 80 auto sensor = sensor_name_to_data_map_.at(sensor_name); 81 out->name = sensor_name; 82 out->currentValue = std::stoi(sensor_reading) * mult; 83 out->throttlingThreshold = std::get<1>(sensor); 84 out->shutdownThreshold = std::get<2>(sensor); 85 out->vrThrottlingThreshold = std::get<3>(sensor); 86 out->type = std::get<4>(sensor); 87 88 LOG(DEBUG) << android::base::StringPrintf( 89 "readTemperature: %s, %d, %s, %g, %g, %g, %g", 90 path.c_str(), out->type, out->name.c_str(), out->currentValue, 91 out->throttlingThreshold, out->shutdownThreshold, 92 out->vrThrottlingThreshold); 93 return true; 94 } 95 96 } // namespace implementation 97 } // namespace V1_1 98 } // namespace thermal 99 } // namespace hardware 100 } // namespace android 101