• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::SessionHint;
38 using android::hardware::power::WorkDuration;
39 
40 const std::vector<Boost> kBoosts{ndk::enum_range<Boost>().begin(), ndk::enum_range<Boost>().end()};
41 
42 const std::vector<Mode> kModes{ndk::enum_range<Mode>().begin(), ndk::enum_range<Mode>().end()};
43 
44 const std::vector<SessionHint> kSessionHints{ndk::enum_range<SessionHint>().begin(),
45                                              ndk::enum_range<SessionHint>().end()};
46 
47 const std::vector<Boost> kInvalidBoosts = {
48         static_cast<Boost>(static_cast<int32_t>(kBoosts.front()) - 1),
49         static_cast<Boost>(static_cast<int32_t>(kBoosts.back()) + 1),
50 };
51 
52 const std::vector<Mode> kInvalidModes = {
53         static_cast<Mode>(static_cast<int32_t>(kModes.front()) - 1),
54         static_cast<Mode>(static_cast<int32_t>(kModes.back()) + 1),
55 };
56 
57 const std::vector<SessionHint> kInvalidSessionHints = {
58         static_cast<SessionHint>(static_cast<int32_t>(kSessionHints.front()) - 1),
59         static_cast<SessionHint>(static_cast<int32_t>(kSessionHints.back()) + 1),
60 };
61 
62 class DurationWrapper : public WorkDuration {
63   public:
DurationWrapper(int64_t dur,int64_t time)64     DurationWrapper(int64_t dur, int64_t time) {
65         durationNanos = dur;
66         timeStampNanos = time;
67     }
68 };
69 
70 const std::vector<int32_t> kSelfTids = {
71         gettid(),
72 };
73 
74 const std::vector<int32_t> kEmptyTids = {};
75 
76 const std::vector<WorkDuration> kNoDurations = {};
77 
78 const std::vector<WorkDuration> kDurationsWithZero = {
79         DurationWrapper(1000L, 1L),
80         DurationWrapper(0L, 2L),
81 };
82 
83 const std::vector<WorkDuration> kDurationsWithNegative = {
84         DurationWrapper(1000L, 1L),
85         DurationWrapper(-1000L, 2L),
86 };
87 
88 const std::vector<WorkDuration> kDurations = {
89         DurationWrapper(1L, 1L),
90         DurationWrapper(1000L, 2L),
91         DurationWrapper(1000000L, 3L),
92         DurationWrapper(1000000000L, 4L),
93 };
94 
95 // DEVICEs launching with Android 11 MUST meet the requirements for the
96 // target-level=5 compatibility_matrix file.
97 const uint64_t kCompatibilityMatrix5ApiLevel = 30;
98 
99 // DEVICEs launching with Android 13 MUST meet the requirements for the
100 // target-level=7 compatibility_matrix file.
101 const uint64_t kCompatibilityMatrix7ApiLevel = 33;
102 
103 // DEVICEs launching with Android 14 MUST meet the requirements for the
104 // target-level=8 compatibility_matrix file.
105 const uint64_t kCompatibilityMatrix8ApiLevel = 34;
106 
isUnknownOrUnsupported(const ndk::ScopedAStatus & status)107 inline bool isUnknownOrUnsupported(const ndk::ScopedAStatus& status) {
108     return status.getStatus() == STATUS_UNKNOWN_TRANSACTION ||
109            status.getExceptionCode() == EX_UNSUPPORTED_OPERATION;
110 }
111 
112 class PowerAidl : public testing::TestWithParam<std::string> {
113   public:
SetUp()114     virtual void SetUp() override {
115         AIBinder* binder = AServiceManager_waitForService(GetParam().c_str());
116         ASSERT_NE(binder, nullptr);
117         power = IPower::fromBinder(ndk::SpAIBinder(binder));
118 
119         mApiLevel = GetUintProperty<uint64_t>("ro.vendor.api_level", 0);
120         ASSERT_NE(mApiLevel, 0);
121     }
122 
123     std::shared_ptr<IPower> power;
124     uint64_t mApiLevel;
125 };
126 
TEST_P(PowerAidl,setMode)127 TEST_P(PowerAidl, setMode) {
128     for (const auto& mode : kModes) {
129         ASSERT_TRUE(power->setMode(mode, true).isOk());
130         ASSERT_TRUE(power->setMode(mode, false).isOk());
131     }
132     for (const auto& mode : kInvalidModes) {
133         ASSERT_TRUE(power->setMode(mode, true).isOk());
134         ASSERT_TRUE(power->setMode(mode, false).isOk());
135     }
136 }
137 
TEST_P(PowerAidl,isModeSupported)138 TEST_P(PowerAidl, isModeSupported) {
139     for (const auto& mode : kModes) {
140         bool supported;
141         ASSERT_TRUE(power->isModeSupported(mode, &supported).isOk());
142     }
143     for (const auto& mode : kInvalidModes) {
144         bool supported;
145         ASSERT_TRUE(power->isModeSupported(mode, &supported).isOk());
146         // Should return false for values outside enum
147         ASSERT_FALSE(supported);
148     }
149 }
150 
TEST_P(PowerAidl,setBoost)151 TEST_P(PowerAidl, setBoost) {
152     for (const auto& boost : kBoosts) {
153         ASSERT_TRUE(power->setBoost(boost, 0).isOk());
154         ASSERT_TRUE(power->setBoost(boost, 1000).isOk());
155         ASSERT_TRUE(power->setBoost(boost, -1).isOk());
156     }
157     for (const auto& boost : kInvalidBoosts) {
158         ASSERT_TRUE(power->setBoost(boost, 0).isOk());
159         ASSERT_TRUE(power->setBoost(boost, 1000).isOk());
160         ASSERT_TRUE(power->setBoost(boost, -1).isOk());
161     }
162 }
163 
TEST_P(PowerAidl,isBoostSupported)164 TEST_P(PowerAidl, isBoostSupported) {
165     for (const auto& boost : kBoosts) {
166         bool supported;
167         ASSERT_TRUE(power->isBoostSupported(boost, &supported).isOk());
168     }
169     for (const auto& boost : kInvalidBoosts) {
170         bool supported;
171         ASSERT_TRUE(power->isBoostSupported(boost, &supported).isOk());
172         // Should return false for values outside enum
173         ASSERT_FALSE(supported);
174     }
175 }
176 
TEST_P(PowerAidl,getHintSessionPreferredRate)177 TEST_P(PowerAidl, getHintSessionPreferredRate) {
178     int64_t rate = -1;
179     auto status = power->getHintSessionPreferredRate(&rate);
180     if (mApiLevel < kCompatibilityMatrix7ApiLevel && !status.isOk()) {
181         EXPECT_TRUE(isUnknownOrUnsupported(status));
182         GTEST_SKIP() << "DEVICE not launching with Android 13 and beyond.";
183     }
184     ASSERT_TRUE(status.isOk());
185     // At least 1ms rate limit from HAL
186     ASSERT_GE(rate, 1000000);
187 }
188 
TEST_P(PowerAidl,createAndCloseHintSession)189 TEST_P(PowerAidl, createAndCloseHintSession) {
190     std::shared_ptr<IPowerHintSession> session;
191     auto status = power->createHintSession(getpid(), getuid(), kSelfTids, 16666666L, &session);
192     if (mApiLevel < kCompatibilityMatrix7ApiLevel && !status.isOk()) {
193         EXPECT_TRUE(isUnknownOrUnsupported(status));
194         GTEST_SKIP() << "DEVICE not launching with Android 13 and beyond.";
195     }
196     ASSERT_TRUE(status.isOk());
197     ASSERT_NE(nullptr, session);
198     ASSERT_TRUE(session->pause().isOk());
199     ASSERT_TRUE(session->resume().isOk());
200     // Test normal destroy operation
201     ASSERT_TRUE(session->close().isOk());
202     session.reset();
203 }
204 
TEST_P(PowerAidl,createHintSessionFailed)205 TEST_P(PowerAidl, createHintSessionFailed) {
206     std::shared_ptr<IPowerHintSession> session;
207     auto status = power->createHintSession(getpid(), getuid(), kEmptyTids, 16666666L, &session);
208 
209     // Regardless of whether V2 and beyond is supported, the status is always not STATUS_OK.
210     ASSERT_FALSE(status.isOk());
211 
212     // If device not launching with Android 13 and beyond, check whether it's supported,
213     // if not, skip the test.
214     if (mApiLevel < kCompatibilityMatrix7ApiLevel && isUnknownOrUnsupported(status)) {
215         GTEST_SKIP() << "DEVICE not launching with Android 13 and beyond.";
216     }
217     ASSERT_EQ(EX_ILLEGAL_ARGUMENT, status.getExceptionCode());
218 }
219 
TEST_P(PowerAidl,updateAndReportDurations)220 TEST_P(PowerAidl, updateAndReportDurations) {
221     std::shared_ptr<IPowerHintSession> session;
222     auto status = power->createHintSession(getpid(), getuid(), kSelfTids, 16666666L, &session);
223     if (mApiLevel < kCompatibilityMatrix7ApiLevel && !status.isOk()) {
224         EXPECT_TRUE(isUnknownOrUnsupported(status));
225         GTEST_SKIP() << "DEVICE not launching with Android 13 and beyond.";
226     }
227     ASSERT_TRUE(status.isOk());
228     ASSERT_NE(nullptr, session);
229 
230     ASSERT_TRUE(session->updateTargetWorkDuration(16666667LL).isOk());
231     ASSERT_TRUE(session->reportActualWorkDuration(kDurations).isOk());
232 }
233 
TEST_P(PowerAidl,sendSessionHint)234 TEST_P(PowerAidl, sendSessionHint) {
235     std::shared_ptr<IPowerHintSession> session;
236     auto status = power->createHintSession(getpid(), getuid(), kSelfTids, 16666666L, &session);
237     if (!status.isOk()) {
238         EXPECT_TRUE(isUnknownOrUnsupported(status));
239         return;
240     }
241     for (const auto& sessionHint : kSessionHints) {
242         ASSERT_TRUE(session->sendHint(sessionHint).isOk());
243     }
244     for (const auto& sessionHint : kInvalidSessionHints) {
245         ASSERT_TRUE(session->sendHint(sessionHint).isOk());
246     }
247 }
248 
TEST_P(PowerAidl,setThreads)249 TEST_P(PowerAidl, setThreads) {
250     std::shared_ptr<IPowerHintSession> session;
251     auto status = power->createHintSession(getpid(), getuid(), kSelfTids, 16666666L, &session);
252     if (mApiLevel < kCompatibilityMatrix7ApiLevel && !status.isOk()) {
253         EXPECT_TRUE(isUnknownOrUnsupported(status));
254         GTEST_SKIP() << "DEVICE not launching with Android 13 and beyond.";
255     }
256     ASSERT_TRUE(status.isOk());
257 
258     status = session->setThreads(kEmptyTids);
259     if (mApiLevel < kCompatibilityMatrix8ApiLevel && isUnknownOrUnsupported(status)) {
260         GTEST_SKIP() << "DEVICE not launching with Android 14 and beyond.";
261     }
262     ASSERT_FALSE(status.isOk());
263     ASSERT_EQ(EX_ILLEGAL_ARGUMENT, status.getExceptionCode());
264 
265     status = session->setThreads(kSelfTids);
266     ASSERT_TRUE(status.isOk());
267 }
268 
269 // FIXED_PERFORMANCE mode is required for all devices which ship on Android 11
270 // or later
TEST_P(PowerAidl,hasFixedPerformance)271 TEST_P(PowerAidl, hasFixedPerformance) {
272     if (mApiLevel < kCompatibilityMatrix5ApiLevel) {
273         GTEST_SKIP() << "FIXED_PERFORMANCE mode is only required for all devices launching Android "
274                         "11 or later.";
275     }
276     bool supported;
277     ASSERT_TRUE(power->isModeSupported(Mode::FIXED_PERFORMANCE, &supported).isOk());
278     ASSERT_TRUE(supported);
279 }
280 
281 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(PowerAidl);
282 INSTANTIATE_TEST_SUITE_P(Power, PowerAidl,
283                          testing::ValuesIn(::android::getAidlHalInstanceNames(IPower::descriptor)),
284                          ::android::PrintInstanceNameToString);
285 
286 }  // namespace
287 }  // namespace aidl::android::hardware::power
288 
main(int argc,char ** argv)289 int main(int argc, char** argv) {
290     ::testing::InitGoogleTest(&argc, argv);
291     ABinderProcess_setThreadPoolMaxThreadCount(1);
292     ABinderProcess_startThreadPool();
293     return RUN_ALL_TESTS();
294 }
295