• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <battery_mitigation/BatteryMitigation.h>
18 
19 #include <sstream>
20 
21 #define MAX_BROWNOUT_DATA_AGE_MINUTES 5
22 #define ONE_SECOND_IN_US 1000000
23 
24 namespace android {
25 namespace hardware {
26 namespace google {
27 namespace pixel {
28 
BatteryMitigation(const struct MitigationConfig::Config & cfg)29 BatteryMitigation::BatteryMitigation(const struct MitigationConfig::Config &cfg) {
30         mThermalMgr = &MitigationThermalManager::getInstance();
31         mThermalMgr->updateConfig(cfg);
32 }
33 
isMitigationLogTimeValid(std::chrono::system_clock::time_point startTime,const char * const logFilePath,const char * const timestampFormat,const std::regex pattern)34 bool BatteryMitigation::isMitigationLogTimeValid(std::chrono::system_clock::time_point startTime,
35                                                  const char *const logFilePath,
36                                                  const char *const timestampFormat,
37                                                  const std::regex pattern) {
38     std::string logFile;
39     if (!android::base::ReadFileToString(logFilePath, &logFile)) {
40         return false;
41     }
42     std::istringstream content(logFile);
43     std::string line;
44     int counter = 0;
45     std::smatch pattern_match;
46     while (std::getline(content, line)) {
47         if (std::regex_match(line, pattern_match, pattern)) {
48             std::tm triggeredTimestamp = {};
49             std::istringstream ss(pattern_match.str());
50             ss >> std::get_time(&triggeredTimestamp, timestampFormat);
51             auto logFileTime = std::chrono::system_clock::from_time_t(mktime(&triggeredTimestamp));
52             auto epoch_logFileTime = logFileTime.time_since_epoch().count() / ONE_SECOND_IN_US;
53 
54             // Convert start time to same format
55             auto time_sec = std::chrono::system_clock::to_time_t(startTime);
56             struct tm start_tm;
57             std::stringstream oss;
58             localtime_r(&time_sec, &start_tm);
59             oss << std::put_time(&start_tm, timestampFormat) << std::flush;
60             std::tm startTimestamp = {};
61             std::istringstream st(oss.str());
62             st >> std::get_time(&startTimestamp, timestampFormat);
63             auto start = std::chrono::system_clock::from_time_t(mktime(&startTimestamp));
64             auto epoch_startTime = start.time_since_epoch().count() / ONE_SECOND_IN_US;
65 
66             auto delta = epoch_startTime - epoch_logFileTime;
67             auto delta_minutes = delta / 60;
68 
69             if ((delta_minutes < MAX_BROWNOUT_DATA_AGE_MINUTES) && (delta_minutes >= 0)) {
70                 return true;
71             }
72         }
73         counter += 1;
74         if (counter > 5) {
75             break;
76         }
77     }
78     return false;
79 }
80 
81 }  // namespace pixel
82 }  // namespace google
83 }  // namespace hardware
84 }  // namespace android
85