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 <ZumaCommonDataProviders.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 
addDisplay(std::shared_ptr<PowerStats> p)35 void addDisplay(std::shared_ptr<PowerStats> p) {
36     // Add display residency stats
37     struct stat buffer;
38     if (stat("/sys/class/drm/card0/device/primary-panel/time_in_state", &buffer)) {
39         // time_in_state doesn't exist
40         std::vector<std::string> states = {
41             "Off",
42             "LP: 1008x2244@1",
43             "LP: 1008x2244@30",
44             "On: 1008x2244@1",
45             "On: 1008x2244@10",
46             "On: 1008x2244@30",
47             "On: 1008x2244@60",
48             "On: 1008x2244@120",
49             "HBM: 1008x2244@1",
50             "HBM: 1008x2244@10",
51             "HBM: 1008x2244@30",
52             "HBM: 1008x2244@60",
53             "HBM: 1008x2244@120",
54             "LP: 1344x2992@1",
55             "LP: 1344x2992@30",
56             "On: 1344x2992@1",
57             "On: 1344x2992@10",
58             "On: 1344x2992@30",
59             "On: 1344x2992@60",
60             "On: 1344x2992@120",
61             "HBM: 1344x2992@1",
62             "HBM: 1344x2992@10",
63             "HBM: 1344x2992@30",
64             "HBM: 1344x2992@60",
65             "HBM: 1344x2992@120"};
66 
67         p->addStateResidencyDataProvider(std::make_unique<DisplayStateResidencyDataProvider>(
68                 "Display",
69                 "/sys/class/backlight/panel0-backlight/state",
70                 states));
71     } else {
72         // time_in_state exists
73         addDisplayMRR(p);
74     }
75 
76     // Add display energy consumer
77     p->addEnergyConsumer(PowerStatsEnergyConsumer::createMeterConsumer(
78             p,
79             EnergyConsumerType::DISPLAY,
80             "DISPLAY",
81             {"VSYS_PWR_DISPLAY"}));
82 }
83 
main()84 int main() {
85     LOG(INFO) << "Pixel PowerStats HAL AIDL Service is starting.";
86 
87     // single thread
88     ABinderProcess_setThreadPoolMaxThreadCount(0);
89 
90     std::shared_ptr<PowerStats> p = ndk::SharedRefBase::make<PowerStats>();
91 
92     addZumaCommonDataProviders(p);
93     addDisplay(p);
94 
95     const std::string instance = std::string() + PowerStats::descriptor + "/default";
96     binder_status_t status = AServiceManager_addService(p->asBinder().get(), instance.c_str());
97     LOG_ALWAYS_FATAL_IF(status != STATUS_OK);
98 
99     ABinderProcess_joinThreadPool();
100     return EXIT_FAILURE;  // should not reach
101 }
102