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/PowerStatsEnergyConsumer.h>
21 #include <Gs101CommonDataProviders.h>
22 #include <PowerStatsAidl.h>
23 
24 #include <android-base/logging.h>
25 #include <android-base/properties.h>
26 #include <android/binder_manager.h>
27 #include <android/binder_process.h>
28 #include <log/log.h>
29 #include <sys/stat.h>
30 
31 using aidl::android::hardware::power::stats::DisplayStateResidencyDataProvider;
32 using aidl::android::hardware::power::stats::EnergyConsumerType;
33 using aidl::android::hardware::power::stats::PowerStatsEnergyConsumer;
34 
35 const char kBootRevision[] = "ro.boot.revision";
36 std::map<std::string, std::string> displayChannelNames = {
37     {"PROTO1.0", "PPVAR_VSYS_PWR_DISP"},
38     {"EVT1.0", "PPVAR_VSYS_PWR_DISP"},
39     {"EVT1.1", "VSYS_PWR_DISPLAY"},
40 };
41 
addDisplay(std::shared_ptr<PowerStats> p)42 void addDisplay(std::shared_ptr<PowerStats> p) {
43     // Add display residency stats
44     struct stat buffer;
45     if (!stat("/sys/class/drm/card0/device/primary-panel/time_in_state", &buffer)) {
46         // time_in_state exists
47         addDisplayMrr(p);
48     } else {
49         // time_in_state doesn't exist
50         std::vector<std::string> states = {
51             "Off",
52             "LP: 1080x2400@30",
53             "On: 1080x2400@60",
54             "On: 1080x2400@90",
55             "HBM: 1080x2400@60",
56         };
57 
58         p->addStateResidencyDataProvider(std::make_unique<DisplayStateResidencyDataProvider>("Display",
59                 "/sys/class/backlight/panel0-backlight/state",
60                 states));
61     }
62 
63     std::string rev = android::base::GetProperty(kBootRevision, "");
64 
65     std::string channelName;
66     if (displayChannelNames.find(rev) == displayChannelNames.end()) {
67         channelName = displayChannelNames["EVT1.1"];
68     } else {
69         channelName = displayChannelNames[rev];
70     }
71 
72     // Add display energy consumer
73     /*
74      * TODO(b/167216667): Add correct display power model here. Must read from display rail
75      * and include proper coefficients for display states.
76      */
77     p->addEnergyConsumer(PowerStatsEnergyConsumer::createMeterAndEntityConsumer(p,
78             EnergyConsumerType::DISPLAY, "display", {channelName}, "Display",
79             {{"LP: 1080x2400@30", 1},
80              {"On: 1080x2400@60", 2},
81              {"On: 1080x2400@90", 3},
82              {"HBM: 1080x2400@60", 4}}));
83 }
84 
main()85 int main() {
86     struct stat buffer;
87 
88     LOG(INFO) << "Pixel PowerStats HAL AIDL Service is starting.";
89 
90     // single thread
91     ABinderProcess_setThreadPoolMaxThreadCount(0);
92 
93     std::shared_ptr<PowerStats> p = ndk::SharedRefBase::make<PowerStats>();
94 
95     addGs101CommonDataProviders(p);
96     addDisplay(p);
97     if (!stat("/sys/devices/platform/10960000.hsi2c/i2c-7/7-0008/power_stats", &buffer)) {
98         addNFC(p, "/sys/devices/platform/10960000.hsi2c/i2c-7/7-0008/power_stats");
99     }
100     const std::string instance = std::string() + PowerStats::descriptor + "/default";
101     binder_status_t status = AServiceManager_addService(p->asBinder().get(), instance.c_str());
102     LOG_ALWAYS_FATAL_IF(status != STATUS_OK);
103 
104     ABinderProcess_joinThreadPool();
105     return EXIT_FAILURE;  // should not reach
106 }
107