blob: d36d9869ce3d287feabfa7421261d5358b825c95 [file] [log] [blame]
Peiyong Lin56960752022-09-30 21:52:52 +00001/*
2 * Copyright (C) 2022 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#include <algorithm>
18#include <chrono>
19#include <cmath>
20#include <memory>
21#include <string>
22#include <thread>
Xiang Wange88a10c2023-09-12 17:29:38 -070023#include <unordered_map>
Peiyong Lin56960752022-09-30 21:52:52 +000024#include <vector>
25
26#define LOG_TAG "thermal_aidl_hal_test"
27
Xiang Wang5eb4c732024-04-12 17:38:09 -070028#include <VtsCoreUtil.h>
Peiyong Lin56960752022-09-30 21:52:52 +000029#include <aidl/Gtest.h>
30#include <aidl/Vintf.h>
TeYuan Wang2bbd1ec2023-12-11 14:55:39 -080031#include <aidl/android/hardware/thermal/BnCoolingDeviceChangedCallback.h>
Peiyong Lin56960752022-09-30 21:52:52 +000032#include <aidl/android/hardware/thermal/BnThermal.h>
33#include <aidl/android/hardware/thermal/BnThermalChangedCallback.h>
34#include <android-base/logging.h>
35#include <android-base/properties.h>
36#include <android/binder_ibinder.h>
37#include <android/binder_interface_utils.h>
38#include <android/binder_manager.h>
39#include <android/binder_process.h>
40#include <android/binder_status.h>
41#include <gtest/gtest.h>
42
43#include <unistd.h>
44
45namespace aidl::android::hardware::thermal {
46
47namespace {
48
49using ::android::sp;
50using android::hardware::thermal::CoolingDevice;
51using android::hardware::thermal::IThermal;
52using android::hardware::thermal::Temperature;
53using android::hardware::thermal::TemperatureType;
54
55using namespace std::string_literals;
56using namespace std::chrono_literals;
57
58static const Temperature kThrottleTemp = {
59 .type = TemperatureType::SKIN,
60 .name = "test temperature sensor",
61 .value = 98.6,
62 .throttlingStatus = ThrottlingSeverity::CRITICAL,
63};
64
TeYuan Wang2bbd1ec2023-12-11 14:55:39 -080065static const CoolingDevice kCoolingDevice = {
66 .type = CoolingType::CPU,
67 .name = "test cooling device",
68 .value = 1,
69 .powerLimitMw = 300,
70 .powerMw = 500,
71 .timeWindowMs = 7000,
72};
73
Xiang Wang5eb4c732024-04-12 17:38:09 -070074static const std::string FEATURE_WATCH = "android.hardware.type.watch";
75static const std::string FEATURE_TELEVISION = "android.hardware.type.television";
76static const std::string FEATURE_LEANBACK = "android.software.leanback";
77static const std::string FEATURE_AUTOMOTIVE = "android.hardware.type.automotive";
78static const std::string FEATURE_PC = "android.hardware.type.pc";
79static const std::string FEATURE_EMBEDDED = "android.hardware.type.embedded";
80static const std::string kNonHandheldFeatures[] = {FEATURE_AUTOMOTIVE, FEATURE_LEANBACK,
81 FEATURE_PC, FEATURE_TELEVISION,
82 FEATURE_WATCH, FEATURE_EMBEDDED};
83
Peiyong Lin56960752022-09-30 21:52:52 +000084// Callback class for receiving thermal event notifications from main class
85class ThermalCallback : public BnThermalChangedCallback {
86 public:
87 ndk::ScopedAStatus notifyThrottling(const Temperature&) override {
88 {
89 std::lock_guard<std::mutex> lock(mMutex);
90 mInvoke = true;
91 }
92 mNotifyThrottling.notify_all();
93 return ndk::ScopedAStatus::ok();
94 }
95
96 template <typename R, typename P>
97 [[nodiscard]] bool waitForCallback(std::chrono::duration<R, P> duration) {
98 std::unique_lock<std::mutex> lock(mMutex);
99 bool r = mNotifyThrottling.wait_for(lock, duration, [this] { return this->mInvoke; });
100 mInvoke = false;
101 return r;
102 }
103
104 private:
105 std::mutex mMutex;
106 std::condition_variable mNotifyThrottling;
107 bool mInvoke = false;
108};
109
TeYuan Wang2bbd1ec2023-12-11 14:55:39 -0800110// Callback class for receiving cooling device event notifications from main class
111class CoolingDeviceCallback : public BnCoolingDeviceChangedCallback {
112 public:
113 ndk::ScopedAStatus notifyCoolingDeviceChanged(const CoolingDevice&) override {
114 {
115 std::lock_guard<std::mutex> lock(mMutex);
116 mInvoke = true;
117 }
118 mNotifyCoolingDeviceChanged.notify_all();
119 return ndk::ScopedAStatus::ok();
120 }
121
122 template <typename R, typename P>
123 [[nodiscard]] bool waitForCallback(std::chrono::duration<R, P> duration) {
124 std::unique_lock<std::mutex> lock(mMutex);
125 bool r = mNotifyCoolingDeviceChanged.wait_for(lock, duration,
126 [this] { return this->mInvoke; });
127 mInvoke = false;
128 return r;
129 }
130
131 private:
132 std::mutex mMutex;
133 std::condition_variable mNotifyCoolingDeviceChanged;
134 bool mInvoke = false;
135};
136
Peiyong Lin56960752022-09-30 21:52:52 +0000137// The main test class for THERMAL HIDL HAL.
138class ThermalAidlTest : public testing::TestWithParam<std::string> {
139 public:
140 void SetUp() override {
141 AIBinder* binder = AServiceManager_waitForService(GetParam().c_str());
142 ASSERT_NE(binder, nullptr);
143 mThermal = IThermal::fromBinder(ndk::SpAIBinder(binder));
144
145 mThermalCallback = ndk::SharedRefBase::make<ThermalCallback>();
146 ASSERT_NE(mThermalCallback, nullptr);
Xiang Wange7b177e2022-12-21 17:55:11 -0800147 ::ndk::ScopedAStatus status = mThermal->registerThermalChangedCallback(mThermalCallback);
148 ASSERT_TRUE(status.isOk()) << status.getMessage();
TeYuan Wang2bbd1ec2023-12-11 14:55:39 -0800149
150 auto ret = mThermal->getInterfaceVersion(&thermal_version);
151 ASSERT_TRUE(ret.isOk()) << ret;
152 if (thermal_version > 1) {
153 mCoolingDeviceCallback = ndk::SharedRefBase::make<CoolingDeviceCallback>();
154 ASSERT_NE(mCoolingDeviceCallback, nullptr);
155 status = mThermal->registerCoolingDeviceChangedCallbackWithType(mCoolingDeviceCallback,
156 kCoolingDevice.type);
157 ASSERT_TRUE(status.isOk()) << status.getMessage();
158 }
Peiyong Lin56960752022-09-30 21:52:52 +0000159 }
160
161 void TearDown() override {
Xiang Wange7b177e2022-12-21 17:55:11 -0800162 ::ndk::ScopedAStatus status = mThermal->unregisterThermalChangedCallback(mThermalCallback);
163 ASSERT_TRUE(status.isOk()) << status.getMessage();
TeYuan Wang2bbd1ec2023-12-11 14:55:39 -0800164
Peiyong Lin56960752022-09-30 21:52:52 +0000165 // Expect to fail if unregister again
Xiang Wangd43e8732023-02-07 12:10:33 -0800166 status = mThermal->unregisterThermalChangedCallback(mThermalCallback);
Xiang Wangd43e8732023-02-07 12:10:33 -0800167 ASSERT_EQ(EX_ILLEGAL_ARGUMENT, status.getExceptionCode());
TeYuan Wang2bbd1ec2023-12-11 14:55:39 -0800168
169 auto ret = mThermal->getInterfaceVersion(&thermal_version);
170 ASSERT_TRUE(ret.isOk()) << ret;
171 if (thermal_version > 1) {
172 status = mThermal->unregisterCoolingDeviceChangedCallback(mCoolingDeviceCallback);
173 ASSERT_TRUE(status.isOk()) << status.getMessage();
174 status = mThermal->unregisterCoolingDeviceChangedCallback(mCoolingDeviceCallback);
175 ASSERT_EQ(EX_ILLEGAL_ARGUMENT, status.getExceptionCode());
176 }
Peiyong Lin56960752022-09-30 21:52:52 +0000177 }
TeYuan Wang2bbd1ec2023-12-11 14:55:39 -0800178 // Stores thermal version
179 int32_t thermal_version;
Peiyong Lin56960752022-09-30 21:52:52 +0000180
181 protected:
182 std::shared_ptr<IThermal> mThermal;
183 std::shared_ptr<ThermalCallback> mThermalCallback;
TeYuan Wang2bbd1ec2023-12-11 14:55:39 -0800184 std::shared_ptr<CoolingDeviceCallback> mCoolingDeviceCallback;
Peiyong Lin56960752022-09-30 21:52:52 +0000185};
186
187// Test ThermalChangedCallback::notifyThrottling().
188// This just calls into and back from our local ThermalChangedCallback impl.
189TEST_P(ThermalAidlTest, NotifyThrottlingTest) {
190 std::shared_ptr<ThermalCallback> thermalCallback = ndk::SharedRefBase::make<ThermalCallback>();
Xiang Wange7b177e2022-12-21 17:55:11 -0800191 ::ndk::ScopedAStatus status = thermalCallback->notifyThrottling(kThrottleTemp);
192 ASSERT_TRUE(status.isOk()) << status.getMessage();
Peiyong Lin56960752022-09-30 21:52:52 +0000193 ASSERT_TRUE(thermalCallback->waitForCallback(200ms));
194}
195
TeYuan Wang2bbd1ec2023-12-11 14:55:39 -0800196// Test CoolingDeviceChangedCallback::notifyCoolingDeviceChanged().
197// This just calls into and back from our local CoolingDeviceChangedCallback impl.
198TEST_P(ThermalAidlTest, NotifyCoolingDeviceChangedTest) {
199 auto ret = mThermal->getInterfaceVersion(&thermal_version);
200 ASSERT_TRUE(ret.isOk()) << ret;
201 if (thermal_version < 2) {
202 return;
203 }
204 std::shared_ptr<CoolingDeviceCallback> cdevCallback =
205 ndk::SharedRefBase::make<CoolingDeviceCallback>();
206 ::ndk::ScopedAStatus status = cdevCallback->notifyCoolingDeviceChanged(kCoolingDevice);
207 ASSERT_TRUE(status.isOk()) << status.getMessage();
208 ASSERT_TRUE(cdevCallback->waitForCallback(200ms));
209}
210
Xiang Wange7b177e2022-12-21 17:55:11 -0800211// Test Thermal->registerThermalChangedCallback.
212TEST_P(ThermalAidlTest, RegisterThermalChangedCallbackTest) {
213 // Expect to fail with same callback
214 ::ndk::ScopedAStatus status = mThermal->registerThermalChangedCallback(mThermalCallback);
215 ASSERT_EQ(EX_ILLEGAL_ARGUMENT, status.getExceptionCode());
216 // Expect to fail with null callback
217 status = mThermal->registerThermalChangedCallback(nullptr);
Sean Dooher3869fa62023-06-12 20:14:00 +0000218 ASSERT_TRUE(status.getExceptionCode() == EX_ILLEGAL_ARGUMENT
219 || status.getExceptionCode() == EX_NULL_POINTER);
Xiang Wange7b177e2022-12-21 17:55:11 -0800220 std::shared_ptr<ThermalCallback> localThermalCallback =
221 ndk::SharedRefBase::make<ThermalCallback>();
222 // Expect to succeed with different callback
223 status = mThermal->registerThermalChangedCallback(localThermalCallback);
224 ASSERT_TRUE(status.isOk()) << status.getMessage();
Xiang Wange7b177e2022-12-21 17:55:11 -0800225 // Remove the local callback
226 status = mThermal->unregisterThermalChangedCallback(localThermalCallback);
227 ASSERT_TRUE(status.isOk()) << status.getMessage();
228 // Expect to fail with null callback
229 status = mThermal->unregisterThermalChangedCallback(nullptr);
Sean Dooher3869fa62023-06-12 20:14:00 +0000230 ASSERT_TRUE(status.getExceptionCode() == EX_ILLEGAL_ARGUMENT
231 || status.getExceptionCode() == EX_NULL_POINTER);
Xiang Wange7b177e2022-12-21 17:55:11 -0800232}
233
234// Test Thermal->registerThermalChangedCallbackWithType.
235TEST_P(ThermalAidlTest, RegisterThermalChangedCallbackWithTypeTest) {
236 // Expect to fail with same callback
237 ::ndk::ScopedAStatus status = mThermal->registerThermalChangedCallbackWithType(
238 mThermalCallback, TemperatureType::SKIN);
239 ASSERT_EQ(EX_ILLEGAL_ARGUMENT, status.getExceptionCode());
240 // Expect to fail with null callback
241 status = mThermal->registerThermalChangedCallbackWithType(nullptr, TemperatureType::SKIN);
Sean Dooher3869fa62023-06-12 20:14:00 +0000242 ASSERT_TRUE(status.getExceptionCode() == EX_ILLEGAL_ARGUMENT
243 || status.getExceptionCode() == EX_NULL_POINTER);
Xiang Wange7b177e2022-12-21 17:55:11 -0800244 std::shared_ptr<ThermalCallback> localThermalCallback =
245 ndk::SharedRefBase::make<ThermalCallback>();
246 // Expect to succeed with different callback
247 status = mThermal->registerThermalChangedCallbackWithType(localThermalCallback,
248 TemperatureType::SKIN);
249 ASSERT_TRUE(status.isOk()) << status.getMessage();
Xiang Wange7b177e2022-12-21 17:55:11 -0800250 // Remove the local callback
251 status = mThermal->unregisterThermalChangedCallback(localThermalCallback);
252 ASSERT_TRUE(status.isOk()) << status.getMessage();
253 // Expect to fail with null callback
254 status = mThermal->unregisterThermalChangedCallback(nullptr);
Sean Dooher3869fa62023-06-12 20:14:00 +0000255 ASSERT_TRUE(status.getExceptionCode() == EX_ILLEGAL_ARGUMENT
256 || status.getExceptionCode() == EX_NULL_POINTER);
Xiang Wange7b177e2022-12-21 17:55:11 -0800257}
258
TeYuan Wang2bbd1ec2023-12-11 14:55:39 -0800259// Test Thermal->registerCoolingDeviceChangedCallbackWithType.
260TEST_P(ThermalAidlTest, RegisterCoolingDeviceChangedCallbackWithTypeTest) {
261 auto ret = mThermal->getInterfaceVersion(&thermal_version);
262 ASSERT_TRUE(ret.isOk()) << ret;
263 if (thermal_version < 2) {
264 return;
265 }
266
267 // Expect to fail with same callback
268 ::ndk::ScopedAStatus status = mThermal->registerCoolingDeviceChangedCallbackWithType(
269 mCoolingDeviceCallback, CoolingType::CPU);
270 ASSERT_EQ(EX_ILLEGAL_ARGUMENT, status.getExceptionCode());
271 // Expect to fail with null callback
272 status = mThermal->registerCoolingDeviceChangedCallbackWithType(nullptr, CoolingType::CPU);
273 ASSERT_TRUE(status.getExceptionCode() == EX_ILLEGAL_ARGUMENT ||
274 status.getExceptionCode() == EX_NULL_POINTER);
275 std::shared_ptr<CoolingDeviceCallback> localCoolingDeviceCallback =
276 ndk::SharedRefBase::make<CoolingDeviceCallback>();
277 // Expect to succeed with different callback
278 status = mThermal->registerCoolingDeviceChangedCallbackWithType(localCoolingDeviceCallback,
279 CoolingType::CPU);
280 ASSERT_TRUE(status.isOk()) << status.getMessage();
281 // Remove the local callback
282 status = mThermal->unregisterCoolingDeviceChangedCallback(localCoolingDeviceCallback);
283 ASSERT_TRUE(status.isOk()) << status.getMessage();
284 // Expect to fail with null callback
285 status = mThermal->unregisterCoolingDeviceChangedCallback(nullptr);
286 ASSERT_TRUE(status.getExceptionCode() == EX_ILLEGAL_ARGUMENT ||
287 status.getExceptionCode() == EX_NULL_POINTER);
288}
289
Xiang Wange7b177e2022-12-21 17:55:11 -0800290// Test Thermal->getCurrentTemperatures().
291TEST_P(ThermalAidlTest, TemperatureTest) {
292 std::vector<Temperature> ret;
293 ::ndk::ScopedAStatus status = mThermal->getTemperatures(&ret);
294 if (status.isOk()) {
295 for (auto& i : ret) {
296 EXPECT_LT(0u, i.name.size());
297 LOG(INFO) << i.name + " " + toString(i.type) << "\n";
298 }
299 } else {
300 ASSERT_EQ(EX_ILLEGAL_STATE, status.getExceptionCode());
301 }
302
303 auto types = ::ndk::enum_range<TemperatureType>();
304 for (const auto& type : types) {
305 status = mThermal->getTemperaturesWithType(type, &ret);
306
307 if (status.isOk()) {
308 for (auto& i : ret) {
309 EXPECT_EQ(type, i.type) << "Expect type " + toString(type) + " but got " +
310 toString(i.type) + " for " + i.name;
311 EXPECT_LT(0u, i.name.size());
312 }
313 } else {
314 ASSERT_EQ(EX_ILLEGAL_STATE, status.getExceptionCode());
315 }
316 }
317}
318
319// Test Thermal->getTemperatureThresholds().
320TEST_P(ThermalAidlTest, TemperatureThresholdTest) {
321 std::vector<TemperatureThreshold> ret;
322 ::ndk::ScopedAStatus status = mThermal->getTemperatureThresholds(&ret);
323 if (status.isOk()) {
324 for (auto& i : ret) {
325 EXPECT_LT(0u, i.name.size());
326 LOG(INFO) << i.name + " " + toString(i.type) << "\n";
327 }
328 } else {
329 ASSERT_EQ(EX_ILLEGAL_STATE, status.getExceptionCode());
330 }
331
332 auto types = ::ndk::enum_range<TemperatureType>();
333 for (const auto& type : types) {
334 status = mThermal->getTemperatureThresholdsWithType(type, &ret);
335
336 if (status.isOk()) {
337 for (auto& i : ret) {
338 EXPECT_EQ(type, i.type) << "Expect type " + toString(type) + " but got " +
339 toString(i.type) + " for " + i.name;
340 EXPECT_LT(0u, i.name.size());
341 }
342 } else {
343 ASSERT_EQ(EX_ILLEGAL_STATE, status.getExceptionCode());
344 }
345 }
346}
347
Xiang Wange88a10c2023-09-12 17:29:38 -0700348// Test Thermal->getTemperatureThresholdsWithType(SKIN).
349// @VsrTest = GMS-VSR-3.2.5-001
350// @VsrTest = VSR-3.2.5-001
351// @VsrTest = GMS-VSR-3.2.5-002
352// @VsrTest = VSR-3.2.5-002
353TEST_P(ThermalAidlTest, SkinTemperatureThresholdsTest) {
354 auto apiLevel = ::android::base::GetIntProperty<int32_t>("ro.vendor.api_level", 0);
355 if (apiLevel < 35) {
356 GTEST_SKIP() << "Skipping test as the vendor level is below 35: " << apiLevel;
357 }
Xiang Wang5eb4c732024-04-12 17:38:09 -0700358 for (const auto& feature : kNonHandheldFeatures) {
359 if (::testing::deviceSupportsFeature(feature.c_str())) {
360 GTEST_SKIP() << "Skipping test as the device has feature: " << feature;
361 }
362 }
Xiang Wange88a10c2023-09-12 17:29:38 -0700363 std::vector<Temperature> temperatures;
364 ::ndk::ScopedAStatus status =
365 mThermal->getTemperaturesWithType(TemperatureType::SKIN, &temperatures);
366 ASSERT_TRUE(status.isOk()) << "getTemperaturesWithType(SKIN) failed";
367 ASSERT_FALSE(temperatures.empty()) << "getTemperaturesWithType(SKIN) returns empty";
368 ASSERT_EQ(1, temperatures.size())
369 << "getTemperaturesWithType(SKIN) returns multiple temperatures";
370
371 std::vector<TemperatureThreshold> thresholds;
372 status = mThermal->getTemperatureThresholdsWithType(TemperatureType::SKIN, &thresholds);
373 ASSERT_TRUE(status.isOk()) << "getTemperatureThresholdsWithType(SKIN) failed";
374 ASSERT_FALSE(thresholds.empty()) << "getTemperatureThresholdsWithType(SKIN) returns empty";
375 ASSERT_EQ(1, thresholds.size())
376 << "getTemperatureThresholdsWithType(SKIN) returns multiple thresholds";
377 auto temperature = temperatures[0];
378 auto threshold = thresholds[0];
379 ASSERT_EQ(temperature.name, threshold.name);
380 auto severities = ::ndk::enum_range<ThrottlingSeverity>();
381 auto cardinality = std::distance(severities.begin(), severities.end());
382 ASSERT_NE(NAN, temperature.value);
383 ASSERT_EQ(cardinality, threshold.hotThrottlingThresholds.size());
384 float lastThreshold = threshold.hotThrottlingThresholds[1];
385 // skip NONE, and check that the rest should be set and non-decreasing
386 for (auto i = 2; i < cardinality; i++) {
387 float t = threshold.hotThrottlingThresholds[i];
388 ASSERT_NE(NAN, t);
389 ASSERT_TRUE(t >= lastThreshold) << "Temperature thresholds should be non-decreasing "
390 << "but got " << t << " for status " << i << " and "
391 << lastThreshold << " for status " << i - 1;
392 lastThreshold = t;
393 }
394}
395
Xiang Wange7b177e2022-12-21 17:55:11 -0800396// Test Thermal->getCoolingDevices().
397TEST_P(ThermalAidlTest, CoolingDeviceTest) {
398 std::vector<CoolingDevice> ret;
399 ::ndk::ScopedAStatus status = mThermal->getCoolingDevices(&ret);
400 if (status.isOk()) {
401 for (auto& i : ret) {
402 EXPECT_LT(0u, i.name.size());
403 LOG(INFO) << i.name + " " + toString(i.type) << "\n";
404 }
405 } else {
406 ASSERT_EQ(EX_ILLEGAL_STATE, status.getExceptionCode());
407 }
408
409 auto types = ::ndk::enum_range<CoolingType>();
410 for (const auto& type : types) {
411 status = mThermal->getCoolingDevicesWithType(type, &ret);
412 if (status.isOk()) {
413 ASSERT_TRUE(status.isOk());
414 for (auto& i : ret) {
415 EXPECT_EQ(type, i.type) << "Expect type " + toString(type) + " but got " +
416 toString(i.type) + " for " + i.name;
417 EXPECT_LT(0u, i.name.size());
418 }
419 } else {
420 ASSERT_EQ(EX_ILLEGAL_STATE, status.getExceptionCode());
421 }
422 }
423}
424
Peiyong Lin56960752022-09-30 21:52:52 +0000425GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ThermalAidlTest);
426INSTANTIATE_TEST_SUITE_P(
427 Thermal, ThermalAidlTest,
428 testing::ValuesIn(::android::getAidlHalInstanceNames(IThermal::descriptor)),
429 ::android::PrintInstanceNameToString);
430
431} // namespace
Xiang Wangdd0edc62023-02-08 16:47:06 -0800432} // namespace aidl::android::hardware::thermal
Peiyong Lin56960752022-09-30 21:52:52 +0000433
434int main(int argc, char** argv) {
435 ::testing::InitGoogleTest(&argc, argv);
436 ABinderProcess_setThreadPoolMaxThreadCount(1);
437 ABinderProcess_startThreadPool();
438 return RUN_ALL_TESTS();
439}