1 /*
2 * Copyright (C) 2020 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 #include <aidl/Gtest.h>
17 #include <aidl/Vintf.h>
18
19 #include <aidl/android/hardware/power/BnPower.h>
20 #include <aidl/android/hardware/power/BnPowerHintSession.h>
21 #include <android-base/properties.h>
22 #include <android/binder_ibinder.h>
23 #include <android/binder_manager.h>
24 #include <android/binder_process.h>
25 #include <android/binder_status.h>
26
27 #include <unistd.h>
28
29 namespace aidl::android::hardware::power {
30 namespace {
31
32 using ::android::base::GetUintProperty;
33 using android::hardware::power::Boost;
34 using android::hardware::power::IPower;
35 using android::hardware::power::IPowerHintSession;
36 using android::hardware::power::Mode;
37 using android::hardware::power::WorkDuration;
38
39 const std::vector<Boost> kBoosts{ndk::enum_range<Boost>().begin(), ndk::enum_range<Boost>().end()};
40
41 const std::vector<Mode> kModes{ndk::enum_range<Mode>().begin(), ndk::enum_range<Mode>().end()};
42
43 const std::vector<Boost> kInvalidBoosts = {
44 static_cast<Boost>(static_cast<int32_t>(kBoosts.front()) - 1),
45 static_cast<Boost>(static_cast<int32_t>(kBoosts.back()) + 1),
46 };
47
48 const std::vector<Mode> kInvalidModes = {
49 static_cast<Mode>(static_cast<int32_t>(kModes.front()) - 1),
50 static_cast<Mode>(static_cast<int32_t>(kModes.back()) + 1),
51 };
52
53 class DurationWrapper : public WorkDuration {
54 public:
DurationWrapper(int64_t dur,int64_t time)55 DurationWrapper(int64_t dur, int64_t time) {
56 durationNanos = dur;
57 timeStampNanos = time;
58 }
59 };
60
61 const std::vector<int32_t> kSelfTids = {
62 gettid(),
63 };
64
65 const std::vector<int32_t> kEmptyTids = {};
66
67 const std::vector<WorkDuration> kNoDurations = {};
68
69 const std::vector<WorkDuration> kDurationsWithZero = {
70 DurationWrapper(1000L, 1L),
71 DurationWrapper(0L, 2L),
72 };
73
74 const std::vector<WorkDuration> kDurationsWithNegative = {
75 DurationWrapper(1000L, 1L),
76 DurationWrapper(-1000L, 2L),
77 };
78
79 const std::vector<WorkDuration> kDurations = {
80 DurationWrapper(1L, 1L),
81 DurationWrapper(1000L, 2L),
82 DurationWrapper(1000000L, 3L),
83 DurationWrapper(1000000000L, 4L),
84 };
85
isUnknownOrUnsupported(const ndk::ScopedAStatus & status)86 inline bool isUnknownOrUnsupported(const ndk::ScopedAStatus& status) {
87 return status.getStatus() == STATUS_UNKNOWN_TRANSACTION ||
88 status.getExceptionCode() == EX_UNSUPPORTED_OPERATION;
89 }
90
91 class PowerAidl : public testing::TestWithParam<std::string> {
92 public:
SetUp()93 virtual void SetUp() override {
94 AIBinder* binder = AServiceManager_waitForService(GetParam().c_str());
95 ASSERT_NE(binder, nullptr);
96 power = IPower::fromBinder(ndk::SpAIBinder(binder));
97 }
98
99 std::shared_ptr<IPower> power;
100 };
101
TEST_P(PowerAidl,setMode)102 TEST_P(PowerAidl, setMode) {
103 for (const auto& mode : kModes) {
104 ASSERT_TRUE(power->setMode(mode, true).isOk());
105 ASSERT_TRUE(power->setMode(mode, false).isOk());
106 }
107 for (const auto& mode : kInvalidModes) {
108 ASSERT_TRUE(power->setMode(mode, true).isOk());
109 ASSERT_TRUE(power->setMode(mode, false).isOk());
110 }
111 }
112
TEST_P(PowerAidl,isModeSupported)113 TEST_P(PowerAidl, isModeSupported) {
114 for (const auto& mode : kModes) {
115 bool supported;
116 ASSERT_TRUE(power->isModeSupported(mode, &supported).isOk());
117 }
118 for (const auto& mode : kInvalidModes) {
119 bool supported;
120 ASSERT_TRUE(power->isModeSupported(mode, &supported).isOk());
121 // Should return false for values outside enum
122 ASSERT_FALSE(supported);
123 }
124 }
125
TEST_P(PowerAidl,setBoost)126 TEST_P(PowerAidl, setBoost) {
127 for (const auto& boost : kBoosts) {
128 ASSERT_TRUE(power->setBoost(boost, 0).isOk());
129 ASSERT_TRUE(power->setBoost(boost, 1000).isOk());
130 ASSERT_TRUE(power->setBoost(boost, -1).isOk());
131 }
132 for (const auto& boost : kInvalidBoosts) {
133 ASSERT_TRUE(power->setBoost(boost, 0).isOk());
134 ASSERT_TRUE(power->setBoost(boost, 1000).isOk());
135 ASSERT_TRUE(power->setBoost(boost, -1).isOk());
136 }
137 }
138
TEST_P(PowerAidl,isBoostSupported)139 TEST_P(PowerAidl, isBoostSupported) {
140 for (const auto& boost : kBoosts) {
141 bool supported;
142 ASSERT_TRUE(power->isBoostSupported(boost, &supported).isOk());
143 }
144 for (const auto& boost : kInvalidBoosts) {
145 bool supported;
146 ASSERT_TRUE(power->isBoostSupported(boost, &supported).isOk());
147 // Should return false for values outside enum
148 ASSERT_FALSE(supported);
149 }
150 }
151
TEST_P(PowerAidl,getHintSessionPreferredRate)152 TEST_P(PowerAidl, getHintSessionPreferredRate) {
153 int64_t rate = -1;
154 auto status = power->getHintSessionPreferredRate(&rate);
155 if (!status.isOk()) {
156 EXPECT_TRUE(isUnknownOrUnsupported(status));
157 return;
158 }
159
160 // At least 1ms rate limit from HAL
161 ASSERT_GE(rate, 1000000);
162 }
163
TEST_P(PowerAidl,createAndCloseHintSession)164 TEST_P(PowerAidl, createAndCloseHintSession) {
165 std::shared_ptr<IPowerHintSession> session;
166 auto status = power->createHintSession(getpid(), getuid(), kSelfTids, 16666666L, &session);
167 if (!status.isOk()) {
168 EXPECT_TRUE(isUnknownOrUnsupported(status));
169 return;
170 }
171 ASSERT_NE(nullptr, session);
172 ASSERT_TRUE(session->pause().isOk());
173 ASSERT_TRUE(session->resume().isOk());
174 // Test normal destroy operation
175 ASSERT_TRUE(session->close().isOk());
176 session.reset();
177 }
TEST_P(PowerAidl,createHintSessionFailed)178 TEST_P(PowerAidl, createHintSessionFailed) {
179 std::shared_ptr<IPowerHintSession> session;
180 auto status = power->createHintSession(getpid(), getuid(), kEmptyTids, 16666666L, &session);
181 ASSERT_FALSE(status.isOk());
182 if (isUnknownOrUnsupported(status)) {
183 return;
184 }
185 ASSERT_EQ(EX_ILLEGAL_ARGUMENT, status.getExceptionCode());
186 }
187
TEST_P(PowerAidl,updateAndReportDurations)188 TEST_P(PowerAidl, updateAndReportDurations) {
189 std::shared_ptr<IPowerHintSession> session;
190 auto status = power->createHintSession(getpid(), getuid(), kSelfTids, 16666666L, &session);
191 if (!status.isOk()) {
192 EXPECT_TRUE(isUnknownOrUnsupported(status));
193 return;
194 }
195 ASSERT_NE(nullptr, session);
196
197 ASSERT_TRUE(session->updateTargetWorkDuration(16666667LL).isOk());
198 ASSERT_TRUE(session->reportActualWorkDuration(kDurations).isOk());
199 }
200
201 // FIXED_PERFORMANCE mode is required for all devices which ship on Android 11
202 // or later
TEST_P(PowerAidl,hasFixedPerformance)203 TEST_P(PowerAidl, hasFixedPerformance) {
204 auto apiLevel = GetUintProperty<uint64_t>("ro.product.first_api_level", 0);
205 if (apiLevel == 0) {
206 apiLevel = GetUintProperty<uint64_t>("ro.build.version.sdk", 0);
207 }
208 ASSERT_NE(apiLevel, 0);
209
210 if (apiLevel >= 30) {
211 bool supported;
212 ASSERT_TRUE(power->isModeSupported(Mode::FIXED_PERFORMANCE, &supported).isOk());
213 ASSERT_TRUE(supported);
214 }
215 }
216
217 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(PowerAidl);
218 INSTANTIATE_TEST_SUITE_P(Power, PowerAidl,
219 testing::ValuesIn(::android::getAidlHalInstanceNames(IPower::descriptor)),
220 ::android::PrintInstanceNameToString);
221
222 } // namespace
223
main(int argc,char ** argv)224 int main(int argc, char** argv) {
225 ::testing::InitGoogleTest(&argc, argv);
226 ABinderProcess_setThreadPoolMaxThreadCount(1);
227 ABinderProcess_startThreadPool();
228 return RUN_ALL_TESTS();
229 }
230
231 } // namespace aidl::android::hardware::power
232