blob: 6ee27d809f31b56525cb43aa70afc4f262919920 [file] [log] [blame]
Benjamin Schwartzc798cb32021-03-18 17:55:40 -07001/*
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
Benjamin Schwartze04673c2021-04-05 22:26:29 -070019#include <dataproviders/DisplayStateResidencyDataProvider.h>
Benjamin Schwartzeeaf5ea2021-04-14 14:49:31 -070020#include <dataproviders/GenericStateResidencyDataProvider.h>
Benjamin Schwartze04673c2021-04-05 22:26:29 -070021#include <dataproviders/PowerStatsEnergyConsumer.h>
Benjamin Schwartzc798cb32021-03-18 17:55:40 -070022#include <Gs101CommonDataProviders.h>
Benjamin Schwartze04673c2021-04-05 22:26:29 -070023#include <PowerStatsAidl.h>
Benjamin Schwartzc798cb32021-03-18 17:55:40 -070024
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
Benjamin Schwartze04673c2021-04-05 22:26:29 -070031using aidl::android::hardware::power::stats::DisplayStateResidencyDataProvider;
32using aidl::android::hardware::power::stats::EnergyConsumerType;
Benjamin Schwartzeeaf5ea2021-04-14 14:49:31 -070033using aidl::android::hardware::power::stats::GenericStateResidencyDataProvider;
Benjamin Schwartze04673c2021-04-05 22:26:29 -070034using aidl::android::hardware::power::stats::PowerStatsEnergyConsumer;
35
36void addDisplay(std::shared_ptr<PowerStats> p) {
37 // Add display residency stats
38 std::vector<std::string> states = {
39 "Off",
40 "LP: 1440x3120@10",
41 "LP: 1440x3120@30",
42 "On: 1440x3120@10",
43 "On: 1440x3120@30",
44 "On: 1440x3120@60",
45 "On: 1440x3120@90",
46 "On: 1440x3120@120",
47 "HBM: 1440x3120@60",
48 };
49
50 p->addStateResidencyDataProvider(std::make_unique<DisplayStateResidencyDataProvider>("Display",
51 "/sys/class/backlight/panel0-backlight/state",
52 states));
53
54 // Add display energy consumer
55 /*
56 * TODO(b/167216667): Add correct display power model here. Must read from display rail
57 * and include proper coefficients for display states.
58 */
59 p->addEnergyConsumer(PowerStatsEnergyConsumer::createMeterAndEntityConsumer(p,
60 EnergyConsumerType::DISPLAY, "display", {"PPVAR_VSYS_PWR_DISP"}, "Display",
61 {{"LP: 1440x3120@10", 1},
62 {"LP: 1440x3120@30", 2},
63 {"On: 1440x3120@10", 3},
64 {"On: 1440x3120@30", 4},
65 {"On: 1440x3120@60", 5},
66 {"On: 1440x3120@90", 6},
67 {"On: 1440x3120@120", 7},
68 {"HBM: 1440x3120@60", 8}}));
69}
70
Benjamin Schwartzeeaf5ea2021-04-14 14:49:31 -070071void addUwb(std::shared_ptr<PowerStats> p) {
72 // A constant to represent the number of nanoseconds in one millisecond.
73 const int NS_TO_MS = 1000000;
74
75 // ACPM stats are reported in nanoseconds. The transform function
76 // converts nanoseconds to milliseconds.
77 std::function<uint64_t(uint64_t)> uwbNsToMs = [](uint64_t a) { return a / NS_TO_MS; };
78 const GenericStateResidencyDataProvider::StateResidencyConfig stateConfig = {
79 .entryCountSupported = true,
80 .entryCountPrefix = "count:",
81 .totalTimeSupported = true,
82 .totalTimePrefix = "dur ns:",
83 .totalTimeTransform = uwbNsToMs,
84 .lastEntrySupported = false,
85 };
86
87 const std::vector<std::pair<std::string, std::string>> stateHeaders = {
88 std::make_pair("Off", "Off state:"),
89 std::make_pair("Run", "Run state:"),
90 std::make_pair("Idle", "Idle state:"),
91 std::make_pair("Tx", "Tx state:"),
92 std::make_pair("Rx", "Rx state:"),
93 };
94
95 std::vector<GenericStateResidencyDataProvider::PowerEntityConfig> cfgs;
96 cfgs.emplace_back(generateGenericStateResidencyConfigs(stateConfig, stateHeaders),
97 "UWB", "");
98
99 p->addStateResidencyDataProvider(std::make_unique<GenericStateResidencyDataProvider>(
100 "/sys/devices/platform/10d30000.spi/spi_master/spi10/spi10.0/uwb/power_stats", cfgs));
101}
102
Benjamin Schwartzc798cb32021-03-18 17:55:40 -0700103int main() {
104 LOG(INFO) << "Pixel PowerStats HAL AIDL Service is starting.";
105
106 // single thread
107 ABinderProcess_setThreadPoolMaxThreadCount(0);
108
109 std::shared_ptr<PowerStats> p = ndk::SharedRefBase::make<PowerStats>();
110
111 addGs101CommonDataProviders(p);
Benjamin Schwartze04673c2021-04-05 22:26:29 -0700112 addDisplay(p);
Manish Varma9aa69ff2021-05-13 18:37:05 -0700113 addNFC(p, "/sys/devices/platform/10960000.hsi2c/i2c-4/i2c-st21nfc/power_stats");
Benjamin Schwartzeeaf5ea2021-04-14 14:49:31 -0700114 addUwb(p);
Benjamin Schwartzc798cb32021-03-18 17:55:40 -0700115
116 const std::string instance = std::string() + PowerStats::descriptor + "/default";
117 binder_status_t status = AServiceManager_addService(p->asBinder().get(), instance.c_str());
118 LOG_ALWAYS_FATAL_IF(status != STATUS_OK);
119
120 ABinderProcess_joinThreadPool();
121 return EXIT_FAILURE; // should not reach
122}