1 /*
2  * Copyright (C) 2020 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 <android-base/logging.h>
18 #include <android-base/properties.h>
19 #include <android-base/strings.h>
20 
21 #include <dataproviders/WlanStateResidencyDataProvider.h>
22 
23 namespace aidl {
24 namespace android {
25 namespace hardware {
26 namespace power {
27 namespace stats {
28 
29 enum {
30     ACTIVE_ID = 0,
31     DEEPSLEEP_ID = 1,
32 };
33 
extractStat(const char * line,const std::string & prefix,uint64_t * stat)34 static bool extractStat(const char *line, const std::string &prefix, uint64_t *stat) {
35     char const *prefixStart = strstr(line, prefix.c_str());
36     if (prefixStart == nullptr) {
37         // Did not find the given prefix
38         return false;
39     }
40 
41     *stat = strtoull(prefixStart + prefix.length(), nullptr, 0);
42     return true;
43 }
44 
getStateResidencies(std::unordered_map<std::string,std::vector<StateResidency>> * residencies)45 bool WlanStateResidencyDataProvider::getStateResidencies(
46         std::unordered_map<std::string, std::vector<StateResidency>> *residencies) {
47     std::vector<StateResidency> result = {{.id = ACTIVE_ID}, {.id = DEEPSLEEP_ID}};
48 
49     std::string wlanDriverStatus = ::android::base::GetProperty("wlan.driver.status", "unloaded");
50     if (wlanDriverStatus != "ok") {
51         LOG(ERROR) << ": wlan is " << wlanDriverStatus;
52         // Return 0s for Wlan stats, because the driver is unloaded
53         residencies->emplace(mName, result);
54         return true;
55     }
56 
57     // Using FILE* instead of std::ifstream for performance reasons (b/122253123)
58     std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(mPath.c_str(), "r"), fclose);
59     if (!fp) {
60         PLOG(ERROR) << ":Failed to open file " << mPath;
61         return false;
62     }
63     size_t numFieldsRead = 0;
64     const size_t numFields = 4;
65     size_t len = 0;
66     char *line = nullptr;
67 
68     while ((numFieldsRead < numFields) && (getline(&line, &len, fp.get()) != -1)) {
69         uint64_t stat = 0;
70         if (extractStat(line, "cumulative_sleep_time_ms:", &stat)) {
71             result[1].totalTimeInStateMs = stat;
72             ++numFieldsRead;
73         } else if (extractStat(line, "cumulative_total_on_time_ms:", &stat)) {
74             result[0].totalTimeInStateMs = stat;
75             ++numFieldsRead;
76         } else if (extractStat(line, "deep_sleep_enter_counter:", &stat)) {
77             result[0].totalStateEntryCount = stat;
78             result[1].totalStateEntryCount = stat;
79             ++numFieldsRead;
80         } else if (extractStat(line, "last_deep_sleep_enter_tstamp_ms:", &stat)) {
81             result[1].lastEntryTimestampMs = stat;
82             ++numFieldsRead;
83         }
84     }
85 
86     free(line);
87 
88     // End of file was reached and not all state data was parsed. Something
89     // went wrong
90     if (numFieldsRead != numFields) {
91         LOG(ERROR) << __func__ << ": failed to parse stats for wlan";
92         return false;
93     }
94 
95     residencies->emplace(mName, result);
96 
97     return true;
98 }
99 
getInfo()100 std::unordered_map<std::string, std::vector<State>> WlanStateResidencyDataProvider::getInfo() {
101     std::unordered_map<std::string, std::vector<State>> ret = {
102             {mName,
103              {{.id = ACTIVE_ID, .name = "Active"}, {.id = DEEPSLEEP_ID, .name = "Deep-Sleep"}}},
104     };
105     return ret;
106 }
107 
108 }  // namespace stats
109 }  // namespace power
110 }  // namespace hardware
111 }  // namespace android
112 }  // namespace aidl
113