• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <Gs201CommonDataProviders.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     std::vector<std::string> states = {
38         "Off",
39         "LP: 1080x2400@30",
40         "On: 1080x2400@60",
41         "On: 1080x2400@90",
42         "HBM: 1080x2400@60",
43         "HBM: 1080x2400@90"};
44 
45     p->addStateResidencyDataProvider(std::make_unique<DisplayStateResidencyDataProvider>(
46             "Display",
47             "/sys/class/backlight/panel0-backlight/state",
48             states));
49 
50     // Add display energy consumer
51     p->addEnergyConsumer(PowerStatsEnergyConsumer::createMeterAndEntityConsumer(
52             p, EnergyConsumerType::DISPLAY, "display", {"VSYS_PWR_DISPLAY"}, "Display",
53             {{"LP: 1080x2400@30", 1},
54              {"On: 1080x2400@60", 2},
55              {"On: 1080x2400@90", 3},
56              {"HBM: 1080x2400@60", 4},
57              {"HBM: 1080x2400@90", 5}}));
58 }
59 
main()60 int main() {
61     struct stat buffer;
62 
63     LOG(INFO) << "Pixel PowerStats HAL AIDL Service is starting.";
64 
65     // single thread
66     ABinderProcess_setThreadPoolMaxThreadCount(0);
67 
68     std::shared_ptr<PowerStats> p = ndk::SharedRefBase::make<PowerStats>();
69 
70     addGs201CommonDataProviders(p);
71     addDisplay(p);
72 
73     if (!stat("/sys/devices/platform/10970000.hsi2c/i2c-2/i2c-st21nfc/power_stats", &buffer)) {
74         addNFC(p, "/sys/devices/platform/10970000.hsi2c/i2c-2/i2c-st21nfc/power_stats");
75     } else if (!stat("/sys/devices/platform/10970000.hsi2c/i2c-3/i2c-st21nfc/power_stats", &buffer)) {
76         addNFC(p, "/sys/devices/platform/10970000.hsi2c/i2c-3/i2c-st21nfc/power_stats");
77     } else if (!stat("/sys/devices/platform/10970000.hsi2c/i2c-4/i2c-st21nfc/power_stats", &buffer)) {
78         addNFC(p, "/sys/devices/platform/10970000.hsi2c/i2c-4/i2c-st21nfc/power_stats");
79     } else if (!stat("/sys/devices/platform/10970000.hsi2c/i2c-5/i2c-st21nfc/power_stats", &buffer)) {
80         addNFC(p, "/sys/devices/platform/10970000.hsi2c/i2c-5/i2c-st21nfc/power_stats");
81     } else if (!stat("/sys/devices/platform/10970000.hsi2c/i2c-6/i2c-st21nfc/power_stats", &buffer)) {
82         addNFC(p, "/sys/devices/platform/10970000.hsi2c/i2c-6/i2c-st21nfc/power_stats");
83     } else if (!stat("/sys/devices/platform/10970000.hsi2c/i2c-7/i2c-st21nfc/power_stats", &buffer)) {
84         addNFC(p, "/sys/devices/platform/10970000.hsi2c/i2c-7/i2c-st21nfc/power_stats");
85     } else {
86         addNFC(p, "/sys/devices/platform/10970000.hsi2c/i2c-8/i2c-st21nfc/power_stats");
87     }
88     const std::string instance = std::string() + PowerStats::descriptor + "/default";
89     binder_status_t status = AServiceManager_addService(p->asBinder().get(), instance.c_str());
90     LOG_ALWAYS_FATAL_IF(status != STATUS_OK);
91 
92     ABinderProcess_joinThreadPool();
93     return EXIT_FAILURE;  // should not reach
94 }
95