• 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_SECONDS 300
22 
23 namespace android {
24 namespace hardware {
25 namespace google {
26 namespace pixel {
27 
BatteryMitigation(const struct MitigationConfig::Config & cfg)28 BatteryMitigation::BatteryMitigation(const struct MitigationConfig::Config &cfg) {
29         mThermalMgr = &MitigationThermalManager::getInstance();
30         mThermalMgr->updateConfig(cfg);
31 }
32 
isMitigationLogTimeValid(std::chrono::system_clock::time_point startTime,const char * const logFilePath,const char * const timestampFormat,const std::regex pattern)33 bool BatteryMitigation::isMitigationLogTimeValid(std::chrono::system_clock::time_point startTime,
34                                                  const char *const logFilePath,
35                                                  const char *const timestampFormat,
36                                                  const std::regex pattern) {
37     std::string logFile;
38     if (!android::base::ReadFileToString(logFilePath, &logFile)) {
39         return false;
40     }
41     std::istringstream content(logFile);
42     std::string line;
43     int counter = 0;
44     std::smatch pattern_match;
45     while (std::getline(content, line)) {
46         if (std::regex_match(line, pattern_match, pattern)) {
47             std::tm triggeredTimestamp = {};
48             std::istringstream ss(pattern_match.str());
49             ss >> std::get_time(&triggeredTimestamp, timestampFormat);
50             auto logFileTime = std::chrono::system_clock::from_time_t(mktime(&triggeredTimestamp));
51             auto delta = std::chrono::duration_cast<std::chrono::seconds>(startTime - logFileTime);
52             if ((delta.count() < MAX_BROWNOUT_DATA_AGE_SECONDS) && (delta.count() > 0)) {
53                 return true;
54             }
55         }
56         counter += 1;
57         if (counter > 5) {
58             break;
59         }
60     }
61     return false;
62 }
63 
64 }  // namespace pixel
65 }  // namespace google
66 }  // namespace hardware
67 }  // namespace android
68