summaryrefslogtreecommitdiff
path: root/powerstats/dataproviders/WlanStateResidencyDataProvider.cpp
blob: 668cd872c611e0e807bfb3784b8f1ab3c8aea890 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "android.hardware.powerstats-service.pixel"

#include <android-base/logging.h>
#include <android-base/properties.h>
#include <android-base/strings.h>

#include <dataproviders/WlanStateResidencyDataProvider.h>

namespace aidl {
namespace android {
namespace hardware {
namespace powerstats {

enum {
    ACTIVE_ID = 0,
    DEEPSLEEP_ID = 1,
};

static bool extractStat(const char *line, const std::string &prefix, uint64_t *stat) {
    char const *prefixStart = strstr(line, prefix.c_str());
    if (prefixStart == nullptr) {
        // Did not find the given prefix
        return false;
    }

    *stat = strtoull(prefixStart + prefix.length(), nullptr, 0);
    return true;
}

bool WlanStateResidencyDataProvider::getResults(
        std::unordered_map<std::string, std::vector<PowerEntityStateResidencyData>> *results) {
    std::vector<PowerEntityStateResidencyData> result = {{.powerEntityStateId = ACTIVE_ID},
                                                         {.powerEntityStateId = DEEPSLEEP_ID}};

    std::string wlanDriverStatus = ::android::base::GetProperty("wlan.driver.status", "unloaded");
    if (wlanDriverStatus != "ok") {
        LOG(ERROR) << ": wlan is " << wlanDriverStatus;
        // Return 0s for Wlan stats, because the driver is unloaded
        results->emplace(mName, result);
        return true;
    }

    // Using FILE* instead of std::ifstream for performance reasons (b/122253123)
    std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(mPath.c_str(), "r"), fclose);
    if (!fp) {
        PLOG(ERROR) << ":Failed to open file " << mPath;
        return false;
    }
    size_t numFieldsRead = 0;
    const size_t numFields = 4;
    size_t len = 0;
    char *line = nullptr;

    while ((numFieldsRead < numFields) && (getline(&line, &len, fp.get()) != -1)) {
        uint64_t stat = 0;
        if (extractStat(line, "cumulative_sleep_time_ms:", &stat)) {
            result[1].totalTimeInStateMs = stat;
            ++numFieldsRead;
        } else if (extractStat(line, "cumulative_total_on_time_ms:", &stat)) {
            result[0].totalTimeInStateMs = stat;
            ++numFieldsRead;
        } else if (extractStat(line, "deep_sleep_enter_counter:", &stat)) {
            result[0].totalStateEntryCount = stat;
            result[1].totalStateEntryCount = stat;
            ++numFieldsRead;
        } else if (extractStat(line, "last_deep_sleep_enter_tstamp_ms:", &stat)) {
            result[1].lastEntryTimestampMs = stat;
            ++numFieldsRead;
        }
    }

    free(line);

    // End of file was reached and not all state data was parsed. Something
    // went wrong
    if (numFieldsRead != numFields) {
        LOG(ERROR) << __func__ << ": failed to parse stats for wlan";
        return false;
    }

    results->emplace(mName, result);

    return true;
}

std::unordered_map<std::string, std::vector<PowerEntityStateInfo>>
WlanStateResidencyDataProvider::getInfo() {
    std::unordered_map<std::string, std::vector<PowerEntityStateInfo>> ret = {
            {mName,
             {{.powerEntityStateId = ACTIVE_ID, .powerEntityStateName = "Active"},
              {.powerEntityStateId = DEEPSLEEP_ID, .powerEntityStateName = "Deep-Sleep"}}},
    };
    return ret;
}

}  // namespace powerstats
}  // namespace hardware
}  // namespace android
}  // namespace aidl