1 /*
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>
23 #include <unordered_map>
24 #include <vector>
25 
26 #define LOG_TAG "thermal_aidl_hal_test"
27 
28 #include <VtsCoreUtil.h>
29 #include <aidl/Gtest.h>
30 #include <aidl/Vintf.h>
31 #include <aidl/android/hardware/thermal/BnCoolingDeviceChangedCallback.h>
32 #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 
45 namespace aidl::android::hardware::thermal {
46 
47 namespace {
48 
49 using ::android::sp;
50 using android::hardware::thermal::CoolingDevice;
51 using android::hardware::thermal::IThermal;
52 using android::hardware::thermal::Temperature;
53 using android::hardware::thermal::TemperatureType;
54 
55 using namespace std::string_literals;
56 using namespace std::chrono_literals;
57 
58 static const Temperature kThrottleTemp = {
59         .type = TemperatureType::SKIN,
60         .name = "test temperature sensor",
61         .value = 98.6,
62         .throttlingStatus = ThrottlingSeverity::CRITICAL,
63 };
64 
65 static 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 
74 static const std::string FEATURE_WATCH = "android.hardware.type.watch";
75 static const std::string FEATURE_TELEVISION = "android.hardware.type.television";
76 static const std::string FEATURE_LEANBACK = "android.software.leanback";
77 static const std::string FEATURE_AUTOMOTIVE = "android.hardware.type.automotive";
78 static const std::string FEATURE_PC = "android.hardware.type.pc";
79 static const std::string FEATURE_EMBEDDED = "android.hardware.type.embedded";
80 static const std::string kNonHandheldFeatures[] = {FEATURE_AUTOMOTIVE, FEATURE_LEANBACK,
81                                                    FEATURE_PC,         FEATURE_TELEVISION,
82                                                    FEATURE_WATCH,      FEATURE_EMBEDDED};
83 
84 // Callback class for receiving thermal event notifications from main class
85 class ThermalCallback : public BnThermalChangedCallback {
86   public:
notifyThrottling(const Temperature &)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>
waitForCallback(std::chrono::duration<R,P> duration)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 
110 // Callback class for receiving cooling device event notifications from main class
111 class CoolingDeviceCallback : public BnCoolingDeviceChangedCallback {
112   public:
notifyCoolingDeviceChanged(const CoolingDevice &)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>
waitForCallback(std::chrono::duration<R,P> duration)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 
137 // The main test class for THERMAL HIDL HAL.
138 class ThermalAidlTest : public testing::TestWithParam<std::string> {
139   public:
SetUp()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);
147         ::ndk::ScopedAStatus status = mThermal->registerThermalChangedCallback(mThermalCallback);
148         ASSERT_TRUE(status.isOk()) << status.getMessage();
149 
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         }
159     }
160 
TearDown()161     void TearDown() override {
162         ::ndk::ScopedAStatus status = mThermal->unregisterThermalChangedCallback(mThermalCallback);
163         ASSERT_TRUE(status.isOk()) << status.getMessage();
164 
165         // Expect to fail if unregister again
166         status = mThermal->unregisterThermalChangedCallback(mThermalCallback);
167         ASSERT_EQ(EX_ILLEGAL_ARGUMENT, status.getExceptionCode());
168 
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         }
177     }
178     // Stores thermal version
179     int32_t thermal_version;
180 
181   protected:
182     std::shared_ptr<IThermal> mThermal;
183     std::shared_ptr<ThermalCallback> mThermalCallback;
184     std::shared_ptr<CoolingDeviceCallback> mCoolingDeviceCallback;
185 };
186 
187 // Test ThermalChangedCallback::notifyThrottling().
188 // This just calls into and back from our local ThermalChangedCallback impl.
TEST_P(ThermalAidlTest,NotifyThrottlingTest)189 TEST_P(ThermalAidlTest, NotifyThrottlingTest) {
190     std::shared_ptr<ThermalCallback> thermalCallback = ndk::SharedRefBase::make<ThermalCallback>();
191     ::ndk::ScopedAStatus status = thermalCallback->notifyThrottling(kThrottleTemp);
192     ASSERT_TRUE(status.isOk()) << status.getMessage();
193     ASSERT_TRUE(thermalCallback->waitForCallback(200ms));
194 }
195 
196 // Test CoolingDeviceChangedCallback::notifyCoolingDeviceChanged().
197 // This just calls into and back from our local CoolingDeviceChangedCallback impl.
TEST_P(ThermalAidlTest,NotifyCoolingDeviceChangedTest)198 TEST_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 
211 // Test Thermal->registerThermalChangedCallback.
TEST_P(ThermalAidlTest,RegisterThermalChangedCallbackTest)212 TEST_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);
218     ASSERT_TRUE(status.getExceptionCode() == EX_ILLEGAL_ARGUMENT
219         || status.getExceptionCode() == EX_NULL_POINTER);
220     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();
225     // 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);
230     ASSERT_TRUE(status.getExceptionCode() == EX_ILLEGAL_ARGUMENT
231         || status.getExceptionCode() == EX_NULL_POINTER);
232 }
233 
234 // Test Thermal->registerThermalChangedCallbackWithType.
TEST_P(ThermalAidlTest,RegisterThermalChangedCallbackWithTypeTest)235 TEST_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);
242     ASSERT_TRUE(status.getExceptionCode() == EX_ILLEGAL_ARGUMENT
243         || status.getExceptionCode() == EX_NULL_POINTER);
244     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();
250     // 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);
255     ASSERT_TRUE(status.getExceptionCode() == EX_ILLEGAL_ARGUMENT
256         || status.getExceptionCode() == EX_NULL_POINTER);
257 }
258 
259 // Test Thermal->registerCoolingDeviceChangedCallbackWithType.
TEST_P(ThermalAidlTest,RegisterCoolingDeviceChangedCallbackWithTypeTest)260 TEST_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 
290 // Test Thermal->getCurrentTemperatures().
TEST_P(ThermalAidlTest,TemperatureTest)291 TEST_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().
TEST_P(ThermalAidlTest,TemperatureThresholdTest)320 TEST_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 
348 // 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
TEST_P(ThermalAidlTest,SkinTemperatureThresholdsTest)353 TEST_P(ThermalAidlTest, SkinTemperatureThresholdsTest) {
354     auto apiLevel = ::android::base::GetIntProperty<int32_t>("ro.vendor.api_level", 0);
355     if (apiLevel < 202404) {
356         GTEST_SKIP() << "Skipping test as the vendor level is below 202404: " << apiLevel;
357     }
358     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     }
363     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 
396 // Test Thermal->getCoolingDevices().
TEST_P(ThermalAidlTest,CoolingDeviceTest)397 TEST_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 
425 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ThermalAidlTest);
426 INSTANTIATE_TEST_SUITE_P(
427         Thermal, ThermalAidlTest,
428         testing::ValuesIn(::android::getAidlHalInstanceNames(IThermal::descriptor)),
429         ::android::PrintInstanceNameToString);
430 
431 }  // namespace
432 }  // namespace aidl::android::hardware::thermal
433 
main(int argc,char ** argv)434 int main(int argc, char** argv) {
435     ::testing::InitGoogleTest(&argc, argv);
436     ABinderProcess_setThreadPoolMaxThreadCount(1);
437     ABinderProcess_startThreadPool();
438     return RUN_ALL_TESTS();
439 }
440