1 /*
2 * Copyright (C) 2018 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@1.0-service-mock"
18
19 #include <android/log.h>
20 #include <hidl/HidlTransportSupport.h>
21
22 #include "PowerStats.h"
23
24 using android::OK;
25 using android::sp;
26 using android::status_t;
27
28 // libhwbinder:
29 using android::hardware::configureRpcThreadpool;
30 using android::hardware::joinRpcThreadpool;
31
32 // Generated HIDL files
33 using android::hardware::power::stats::V1_0::IPowerStats;
34 using android::hardware::power::stats::V1_0::PowerEntityStateResidencyResult;
35 using android::hardware::power::stats::V1_0::PowerEntityStateSpace;
36 using android::hardware::power::stats::V1_0::PowerEntityType;
37 using android::hardware::power::stats::V1_0::implementation::IStateResidencyDataProvider;
38 using android::hardware::power::stats::V1_0::implementation::PowerStats;
39
40 class DefaultStateResidencyDataProvider : public IStateResidencyDataProvider {
41 public:
DefaultStateResidencyDataProvider(uint32_t id)42 DefaultStateResidencyDataProvider(uint32_t id)
43 : mPowerEntityId(id), mActiveStateId(0), mSleepStateId(1) {}
44 ~DefaultStateResidencyDataProvider() = default;
45
getResults(std::unordered_map<uint32_t,PowerEntityStateResidencyResult> & results)46 bool getResults(std::unordered_map<uint32_t, PowerEntityStateResidencyResult>& results) {
47 PowerEntityStateResidencyResult result = { .powerEntityId = mPowerEntityId };
48 result.stateResidencyData.resize(2);
49
50 // Using fake numbers here for display only. A real implementation would
51 // use actual tracked stats.
52 result.stateResidencyData[0] = {
53 .powerEntityStateId = mActiveStateId,
54 .totalTimeInStateMs = 1,
55 .totalStateEntryCount = 2,
56 .lastEntryTimestampMs = 3
57 };
58 result.stateResidencyData[1] = {
59 .powerEntityStateId = mSleepStateId,
60 .totalTimeInStateMs = 4,
61 .totalStateEntryCount = 5,
62 .lastEntryTimestampMs = 6,
63 };
64 results.emplace(mPowerEntityId, result);
65 return true;
66 }
67
getStateSpaces()68 std::vector<PowerEntityStateSpace> getStateSpaces() {
69 return {{
70 .powerEntityId = mPowerEntityId,
71 .states = {
72 {.powerEntityStateId = mActiveStateId, .powerEntityStateName = "Active"},
73 {.powerEntityStateId = mSleepStateId, .powerEntityStateName = "Sleep"}
74 }
75 }};
76 }
77
78 private:
79 const uint32_t mPowerEntityId;
80 const uint32_t mActiveStateId;
81 const uint32_t mSleepStateId;
82 };
83
main(int,char **)84 int main(int /* argc */, char** /* argv */) {
85 ALOGI("power.stats service 1.0 mock is starting.");
86
87 PowerStats* service = new PowerStats();
88 if (service == nullptr) {
89 ALOGE("Can not create an instance of power.stats HAL Iface, exiting.");
90 return 1;
91 }
92
93 uint32_t defaultId = service->addPowerEntity("DefaultEntity", PowerEntityType::SUBSYSTEM);
94 auto defaultSdp = std::make_shared<DefaultStateResidencyDataProvider>(defaultId);
95 service->addStateResidencyDataProvider(std::move(defaultSdp));
96
97 configureRpcThreadpool(1, true /*callerWillJoin*/);
98
99 status_t status = service->registerAsService();
100 if (status != OK) {
101 ALOGE("Could not register service for power.stats HAL Iface (%d), exiting.", status);
102 return 1;
103 }
104
105 ALOGI("power.stats service is ready");
106 joinRpcThreadpool();
107
108 // In normal operation, we don't expect the thread pool to exit
109 ALOGE("power.stats service is shutting down");
110 return 1;
111 }
112