1 /*
2  * Copyright (C) 2021 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 #define LOG_TAG "android.hardware.power.stats-service.pixel"
18 
19 #include <dataproviders/DisplayStateResidencyDataProvider.h>
20 #include <dataproviders/GenericStateResidencyDataProvider.h>
21 #include <dataproviders/PowerStatsEnergyConsumer.h>
22 #include <Gs201CommonDataProviders.h>
23 #include <PowerStatsAidl.h>
24 
25 #include <android-base/logging.h>
26 #include <android-base/properties.h>
27 #include <android/binder_manager.h>
28 #include <android/binder_process.h>
29 #include <log/log.h>
30 #include <sys/stat.h>
31 
32 using aidl::android::hardware::power::stats::DisplayStateResidencyDataProvider;
33 using aidl::android::hardware::power::stats::EnergyConsumerType;
34 using aidl::android::hardware::power::stats::GenericStateResidencyDataProvider;
35 using aidl::android::hardware::power::stats::PowerStatsEnergyConsumer;
36 
addDisplay(std::shared_ptr<PowerStats> p)37 void addDisplay(std::shared_ptr<PowerStats> p) {
38     // Add display residency stats
39     struct stat buffer;
40     if (!stat("/sys/class/drm/card0/device/primary-panel/time_in_state", &buffer)) {
41         // time_in_state exists
42         addDisplayMrr(p);
43     } else {
44         // time_in_state doesn't exist
45         std::vector<std::string> states = {
46             "Off",
47             "LP: 1440x3120@30",
48             "On: 1440x3120@10",
49             "On: 1440x3120@60",
50             "On: 1440x3120@120",
51             "HBM: 1440x3120@60",
52             "HBM: 1440x3120@120",
53             "LP: 1080x2340@30",
54             "On: 1080x2340@10",
55             "On: 1080x2340@60",
56             "On: 1080x2340@120",
57             "HBM: 1080x2340@60",
58             "HBM: 1080x2340@120"};
59 
60         p->addStateResidencyDataProvider(std::make_unique<DisplayStateResidencyDataProvider>(
61                 "Display",
62                 "/sys/class/backlight/panel0-backlight/state",
63                 states));
64     }
65 
66     // Add display energy consumer
67     p->addEnergyConsumer(PowerStatsEnergyConsumer::createMeterAndEntityConsumer(
68             p, EnergyConsumerType::DISPLAY, "display", {"VSYS_PWR_DISPLAY"}, "Display",
69             {{"LP: 1440x3120@30", 1},
70              {"On: 1440x3120@10", 2},
71              {"On: 1440x3120@60", 3},
72              {"On: 1440x3120@120", 4},
73              {"HBM: 1440x3120@60", 5},
74              {"HBM: 1440x3120@120", 6},
75              {"LP: 1080x2340@30", 7},
76              {"On: 1080x2340@10", 8},
77              {"On: 1080x2340@60", 9},
78              {"On: 1080x2340@120", 10},
79              {"HBM: 1080x2340@60", 11},
80              {"HBM: 1080x2340@120", 12}}));
81 }
82 
addUwb(std::shared_ptr<PowerStats> p)83 void addUwb(std::shared_ptr<PowerStats> p) {
84     // A constant to represent the number of nanoseconds in one millisecond.
85     const int NS_TO_MS = 1000000;
86 
87     // ACPM stats are reported in nanoseconds. The transform function
88     // converts nanoseconds to milliseconds.
89     std::function<uint64_t(uint64_t)> uwbNsToMs = [](uint64_t a) { return a / NS_TO_MS; };
90     const GenericStateResidencyDataProvider::StateResidencyConfig stateConfig = {
91             .entryCountSupported = true,
92             .entryCountPrefix = "count:",
93             .totalTimeSupported = true,
94             .totalTimePrefix = "dur ns:",
95             .totalTimeTransform = uwbNsToMs,
96             .lastEntrySupported = false,
97     };
98 
99     const std::vector<std::pair<std::string, std::string>> stateHeaders = {
100             std::make_pair("Off", "Off state:"),
101             std::make_pair("Deep sleep", "Deep sleep state:"),
102             std::make_pair("Run", "Run state:"),
103             std::make_pair("Idle", "Idle state:"),
104             std::make_pair("Tx", "Tx state:"),
105             std::make_pair("Rx", "Rx state:"),
106     };
107 
108     std::vector<GenericStateResidencyDataProvider::PowerEntityConfig> cfgs;
109     cfgs.emplace_back(generateGenericStateResidencyConfigs(stateConfig, stateHeaders),
110             "UWB", "");
111 
112     p->addStateResidencyDataProvider(std::make_unique<GenericStateResidencyDataProvider>(
113             "/sys/devices/platform/10db0000.spi/spi_master/spi16/spi16.0/uwb/power_stats", cfgs));
114 }
115 
main()116 int main() {
117     struct stat buffer;
118 
119     LOG(INFO) << "Pixel PowerStats HAL AIDL Service is starting.";
120 
121     // single thread
122     ABinderProcess_setThreadPoolMaxThreadCount(0);
123 
124     std::shared_ptr<PowerStats> p = ndk::SharedRefBase::make<PowerStats>();
125 
126     addGs201CommonDataProviders(p);
127     addDisplay(p);
128     addUwb(p);
129 
130     if (!stat("/sys/devices/platform/10970000.hsi2c/i2c-8/8-0008/power_stats", &buffer)) {
131         addNFC(p, "/sys/devices/platform/10970000.hsi2c/i2c-8/8-0008/power_stats");
132     }
133     const std::string instance = std::string() + PowerStats::descriptor + "/default";
134     binder_status_t status = AServiceManager_addService(p->asBinder().get(), instance.c_str());
135     LOG_ALWAYS_FATAL_IF(status != STATUS_OK);
136 
137     ABinderProcess_joinThreadPool();
138     return EXIT_FAILURE;  // should not reach
139 }
140