xref: /aosp_15_r20/hardware/interfaces/power/stats/1.0/default/service.cpp (revision 4d7e907c777eeecc4c5bd7cf640a754fac206ff7)
1*4d7e907cSAndroid Build Coastguard Worker /*
2*4d7e907cSAndroid Build Coastguard Worker  * Copyright (C) 2018 The Android Open Source Project
3*4d7e907cSAndroid Build Coastguard Worker  *
4*4d7e907cSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*4d7e907cSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*4d7e907cSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*4d7e907cSAndroid Build Coastguard Worker  *
8*4d7e907cSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*4d7e907cSAndroid Build Coastguard Worker  *
10*4d7e907cSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*4d7e907cSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*4d7e907cSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*4d7e907cSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*4d7e907cSAndroid Build Coastguard Worker  * limitations under the License.
15*4d7e907cSAndroid Build Coastguard Worker  */
16*4d7e907cSAndroid Build Coastguard Worker 
17*4d7e907cSAndroid Build Coastguard Worker #define LOG_TAG "[email protected]"
18*4d7e907cSAndroid Build Coastguard Worker 
19*4d7e907cSAndroid Build Coastguard Worker #include <android/log.h>
20*4d7e907cSAndroid Build Coastguard Worker #include <hidl/HidlTransportSupport.h>
21*4d7e907cSAndroid Build Coastguard Worker 
22*4d7e907cSAndroid Build Coastguard Worker #include "PowerStats.h"
23*4d7e907cSAndroid Build Coastguard Worker 
24*4d7e907cSAndroid Build Coastguard Worker using android::OK;
25*4d7e907cSAndroid Build Coastguard Worker using android::sp;
26*4d7e907cSAndroid Build Coastguard Worker using android::status_t;
27*4d7e907cSAndroid Build Coastguard Worker 
28*4d7e907cSAndroid Build Coastguard Worker // libhwbinder:
29*4d7e907cSAndroid Build Coastguard Worker using android::hardware::configureRpcThreadpool;
30*4d7e907cSAndroid Build Coastguard Worker using android::hardware::joinRpcThreadpool;
31*4d7e907cSAndroid Build Coastguard Worker 
32*4d7e907cSAndroid Build Coastguard Worker // Generated HIDL files
33*4d7e907cSAndroid Build Coastguard Worker using android::hardware::power::stats::V1_0::IPowerStats;
34*4d7e907cSAndroid Build Coastguard Worker using android::hardware::power::stats::V1_0::PowerEntityStateResidencyResult;
35*4d7e907cSAndroid Build Coastguard Worker using android::hardware::power::stats::V1_0::PowerEntityStateSpace;
36*4d7e907cSAndroid Build Coastguard Worker using android::hardware::power::stats::V1_0::PowerEntityType;
37*4d7e907cSAndroid Build Coastguard Worker using android::hardware::power::stats::V1_0::implementation::IStateResidencyDataProvider;
38*4d7e907cSAndroid Build Coastguard Worker using android::hardware::power::stats::V1_0::implementation::PowerStats;
39*4d7e907cSAndroid Build Coastguard Worker 
40*4d7e907cSAndroid Build Coastguard Worker class DefaultStateResidencyDataProvider : public IStateResidencyDataProvider {
41*4d7e907cSAndroid Build Coastguard Worker    public:
DefaultStateResidencyDataProvider(uint32_t id)42*4d7e907cSAndroid Build Coastguard Worker     DefaultStateResidencyDataProvider(uint32_t id)
43*4d7e907cSAndroid Build Coastguard Worker         : mPowerEntityId(id), mActiveStateId(0), mSleepStateId(1) {}
44*4d7e907cSAndroid Build Coastguard Worker     ~DefaultStateResidencyDataProvider() = default;
45*4d7e907cSAndroid Build Coastguard Worker 
getResults(std::unordered_map<uint32_t,PowerEntityStateResidencyResult> & results)46*4d7e907cSAndroid Build Coastguard Worker     bool getResults(std::unordered_map<uint32_t, PowerEntityStateResidencyResult>& results) {
47*4d7e907cSAndroid Build Coastguard Worker         PowerEntityStateResidencyResult result = { .powerEntityId = mPowerEntityId };
48*4d7e907cSAndroid Build Coastguard Worker         result.stateResidencyData.resize(2);
49*4d7e907cSAndroid Build Coastguard Worker 
50*4d7e907cSAndroid Build Coastguard Worker         // Using fake numbers here for display only. A real implementation would
51*4d7e907cSAndroid Build Coastguard Worker         // use actual tracked stats.
52*4d7e907cSAndroid Build Coastguard Worker         result.stateResidencyData[0] = {
53*4d7e907cSAndroid Build Coastguard Worker             .powerEntityStateId = mActiveStateId,
54*4d7e907cSAndroid Build Coastguard Worker             .totalTimeInStateMs = 1,
55*4d7e907cSAndroid Build Coastguard Worker             .totalStateEntryCount = 2,
56*4d7e907cSAndroid Build Coastguard Worker             .lastEntryTimestampMs = 3
57*4d7e907cSAndroid Build Coastguard Worker         };
58*4d7e907cSAndroid Build Coastguard Worker         result.stateResidencyData[1] = {
59*4d7e907cSAndroid Build Coastguard Worker             .powerEntityStateId = mSleepStateId,
60*4d7e907cSAndroid Build Coastguard Worker             .totalTimeInStateMs = 4,
61*4d7e907cSAndroid Build Coastguard Worker             .totalStateEntryCount = 5,
62*4d7e907cSAndroid Build Coastguard Worker             .lastEntryTimestampMs = 6,
63*4d7e907cSAndroid Build Coastguard Worker         };
64*4d7e907cSAndroid Build Coastguard Worker         results.emplace(mPowerEntityId, result);
65*4d7e907cSAndroid Build Coastguard Worker         return true;
66*4d7e907cSAndroid Build Coastguard Worker     }
67*4d7e907cSAndroid Build Coastguard Worker 
getStateSpaces()68*4d7e907cSAndroid Build Coastguard Worker     std::vector<PowerEntityStateSpace> getStateSpaces() {
69*4d7e907cSAndroid Build Coastguard Worker         return {{
70*4d7e907cSAndroid Build Coastguard Worker           .powerEntityId = mPowerEntityId,
71*4d7e907cSAndroid Build Coastguard Worker           .states = {
72*4d7e907cSAndroid Build Coastguard Worker               {.powerEntityStateId = mActiveStateId, .powerEntityStateName = "Active"},
73*4d7e907cSAndroid Build Coastguard Worker               {.powerEntityStateId = mSleepStateId, .powerEntityStateName = "Sleep"}
74*4d7e907cSAndroid Build Coastguard Worker           }
75*4d7e907cSAndroid Build Coastguard Worker         }};
76*4d7e907cSAndroid Build Coastguard Worker     }
77*4d7e907cSAndroid Build Coastguard Worker 
78*4d7e907cSAndroid Build Coastguard Worker    private:
79*4d7e907cSAndroid Build Coastguard Worker     const uint32_t mPowerEntityId;
80*4d7e907cSAndroid Build Coastguard Worker     const uint32_t mActiveStateId;
81*4d7e907cSAndroid Build Coastguard Worker     const uint32_t mSleepStateId;
82*4d7e907cSAndroid Build Coastguard Worker };
83*4d7e907cSAndroid Build Coastguard Worker 
main(int,char **)84*4d7e907cSAndroid Build Coastguard Worker int main(int /* argc */, char** /* argv */) {
85*4d7e907cSAndroid Build Coastguard Worker     ALOGI("power.stats service 1.0 mock is starting.");
86*4d7e907cSAndroid Build Coastguard Worker 
87*4d7e907cSAndroid Build Coastguard Worker     PowerStats* service = new PowerStats();
88*4d7e907cSAndroid Build Coastguard Worker     if (service == nullptr) {
89*4d7e907cSAndroid Build Coastguard Worker         ALOGE("Can not create an instance of power.stats HAL Iface, exiting.");
90*4d7e907cSAndroid Build Coastguard Worker         return 1;
91*4d7e907cSAndroid Build Coastguard Worker     }
92*4d7e907cSAndroid Build Coastguard Worker 
93*4d7e907cSAndroid Build Coastguard Worker     uint32_t defaultId = service->addPowerEntity("DefaultEntity", PowerEntityType::SUBSYSTEM);
94*4d7e907cSAndroid Build Coastguard Worker     auto defaultSdp = std::make_shared<DefaultStateResidencyDataProvider>(defaultId);
95*4d7e907cSAndroid Build Coastguard Worker     service->addStateResidencyDataProvider(std::move(defaultSdp));
96*4d7e907cSAndroid Build Coastguard Worker 
97*4d7e907cSAndroid Build Coastguard Worker     configureRpcThreadpool(1, true /*callerWillJoin*/);
98*4d7e907cSAndroid Build Coastguard Worker 
99*4d7e907cSAndroid Build Coastguard Worker     status_t status = service->registerAsService();
100*4d7e907cSAndroid Build Coastguard Worker     if (status != OK) {
101*4d7e907cSAndroid Build Coastguard Worker         ALOGE("Could not register service for power.stats HAL Iface (%d), exiting.", status);
102*4d7e907cSAndroid Build Coastguard Worker         return 1;
103*4d7e907cSAndroid Build Coastguard Worker     }
104*4d7e907cSAndroid Build Coastguard Worker 
105*4d7e907cSAndroid Build Coastguard Worker     ALOGI("power.stats service is ready");
106*4d7e907cSAndroid Build Coastguard Worker     joinRpcThreadpool();
107*4d7e907cSAndroid Build Coastguard Worker 
108*4d7e907cSAndroid Build Coastguard Worker     // In normal operation, we don't expect the thread pool to exit
109*4d7e907cSAndroid Build Coastguard Worker     ALOGE("power.stats service is shutting down");
110*4d7e907cSAndroid Build Coastguard Worker     return 1;
111*4d7e907cSAndroid Build Coastguard Worker }
112