Add Thermal HAL VTS

Bug: 32022734
Test: vts
Change-Id: I29ecd8b9da8275674343bce73c9f9deb20692bf2
diff --git a/thermal/1.0/vts/Android.mk b/thermal/1.0/vts/Android.mk
new file mode 100644
index 0000000..f1ac716
--- /dev/null
+++ b/thermal/1.0/vts/Android.mk
@@ -0,0 +1,84 @@
+#
+# Copyright (C) 2016 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+LOCAL_PATH := $(call my-dir)
+
+# build VTS driver for Thermal v1.0.
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := libvts_driver_hidl_thermal@1.0
+
+LOCAL_SRC_FILES := \
+  Thermal.vts \
+  types.vts \
+
+LOCAL_C_INCLUDES := \
+  android.hardware.thermal@1.0 \
+  system/core/base/include \
+  system/core/include \
+
+LOCAL_SHARED_LIBRARIES += \
+  android.hardware.thermal@1.0 \
+  libbase \
+  libutils \
+  libcutils \
+  liblog \
+  libhidl \
+  libhwbinder \
+  libprotobuf-cpp-full \
+  libvts_common \
+  libvts_datatype \
+  libvts_measurement \
+  libvts_multidevice_proto \
+
+LOCAL_STATIC_LIBRARIES := \
+
+LOCAL_PROTOC_OPTIMIZE_TYPE := full
+
+LOCAL_MULTILIB := both
+
+include $(BUILD_SHARED_LIBRARY)
+
+# build profiler for thermal.
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := libvts_profiler_hidl_thermal@1.0
+
+LOCAL_SRC_FILES := \
+   Thermal.vts \
+   types.vts \
+
+LOCAL_C_INCLUDES += \
+  test/vts/drivers/libprofiling \
+
+LOCAL_VTS_MODE := PROFILER
+
+LOCAL_SHARED_LIBRARIES := \
+   android.hardware.thermal@1.0 \
+   libbase \
+   libcutils \
+   liblog \
+   libhidl \
+   libhwbinder \
+   libprotobuf-cpp-full \
+   libvts_common \
+   libvts_multidevice_proto \
+   libvts_profiling \
+   libutils \
+
+LOCAL_PROTOC_OPTIMIZE_TYPE := full
+
+include $(BUILD_SHARED_LIBRARY)
diff --git a/thermal/1.0/vts/Thermal.vts b/thermal/1.0/vts/Thermal.vts
new file mode 100644
index 0000000..e76d943
--- /dev/null
+++ b/thermal/1.0/vts/Thermal.vts
@@ -0,0 +1,82 @@
+component_class: HAL_HIDL
+component_type_version: 1.0
+component_name: "IThermal"
+
+package: "android.hardware.thermal"
+
+import: "android.hardware.thermal@1.0::types"
+
+interface: {
+    api: {
+        name: "getTemperatures"
+        return_type_hidl: {
+            type: TYPE_STRUCT
+            predefined_type: "::android::hardware::thermal::V1_0::ThermalStatus"
+        }
+        return_type_hidl: {
+            type: TYPE_VECTOR
+            vector_value: {
+                type: TYPE_STRUCT
+                predefined_type: "::android::hardware::thermal::V1_0::Temperature"
+            }
+        }
+        callflow: {
+            next: "*"
+        }
+        callflow: {
+            entry: true
+        }
+        callflow: {
+            exit: true
+        }
+    }
+
+    api: {
+        name: "getCpuUsages"
+        return_type_hidl: {
+            type: TYPE_STRUCT
+            predefined_type: "::android::hardware::thermal::V1_0::ThermalStatus"
+        }
+        return_type_hidl: {
+            type: TYPE_VECTOR
+            vector_value: {
+                type: TYPE_STRUCT
+                predefined_type: "::android::hardware::thermal::V1_0::CpuUsage"
+            }
+        }
+        callflow: {
+            next: "*"
+        }
+        callflow: {
+            entry: true
+        }
+        callflow: {
+            exit: true
+        }
+    }
+
+    api: {
+        name: "getCoolingDevices"
+        return_type_hidl: {
+            type: TYPE_STRUCT
+            predefined_type: "::android::hardware::thermal::V1_0::ThermalStatus"
+        }
+        return_type_hidl: {
+            type: TYPE_VECTOR
+            vector_value: {
+                type: TYPE_STRUCT
+                predefined_type: "::android::hardware::thermal::V1_0::CoolingDevice"
+            }
+        }
+        callflow: {
+            next: "*"
+        }
+        callflow: {
+            entry: true
+        }
+        callflow: {
+            exit: true
+        }
+    }
+
+}
diff --git a/thermal/1.0/vts/functional/Android.bp b/thermal/1.0/vts/functional/Android.bp
new file mode 100644
index 0000000..7e0d1d0
--- /dev/null
+++ b/thermal/1.0/vts/functional/Android.bp
@@ -0,0 +1,37 @@
+//
+// Copyright (C) 2016 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+cc_test {
+    name: "thermal_hidl_hal_test",
+    gtest: true,
+    srcs: ["thermal_hidl_hal_test.cpp"],
+    shared_libs: [
+        "libbase",
+        "liblog",
+        "libcutils",
+        "libhidl",
+        "libhwbinder",
+        "libnativehelper",
+        "libutils",
+        "android.hardware.thermal@1.0",
+    ],
+    static_libs: ["libgtest"],
+    cflags: [
+        "-O0",
+        "-g",
+    ],
+}
+
diff --git a/thermal/1.0/vts/functional/thermal_hidl_hal_test.cpp b/thermal/1.0/vts/functional/thermal_hidl_hal_test.cpp
new file mode 100644
index 0000000..e3b00ab
--- /dev/null
+++ b/thermal/1.0/vts/functional/thermal_hidl_hal_test.cpp
@@ -0,0 +1,219 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <algorithm>
+#include <cmath>
+#include <string>
+#include <vector>
+
+#define LOG_TAG "thermal_hidl_hal_test"
+
+#include <android-base/logging.h>
+#include <android/hardware/thermal/1.0/IThermal.h>
+#include <android/hardware/thermal/1.0/types.h>
+#include <gtest/gtest.h>
+#include <unistd.h>
+
+using ::android::hardware::hidl_string;
+using ::android::hardware::hidl_vec;
+using ::android::hardware::thermal::V1_0::CoolingDevice;
+using ::android::hardware::thermal::V1_0::CpuUsage;
+using ::android::hardware::thermal::V1_0::IThermal;
+using ::android::hardware::thermal::V1_0::Temperature;
+using ::android::hardware::thermal::V1_0::TemperatureType;
+using ::android::hardware::thermal::V1_0::ThermalStatus;
+using ::android::hardware::thermal::V1_0::ThermalStatusCode;
+using ::android::hardware::Return;
+using ::android::hardware::Void;
+using ::android::sp;
+
+#define THERMAL_SERVICE_NAME "thermal"
+#define MONITORING_OPERATION_NUMBER 10
+
+#define UNDEFINED_TEMPERATURE (-FLT_MAX)
+
+#define MAX_DEVICE_TEMPERATURE 200
+#define MAX_FAN_SPEED 20000
+
+// The main test class for THERMAL HIDL HAL.
+class ThermalHidlTest : public ::testing::Test {
+ public:
+  virtual void SetUp() override {
+    thermal_ = IThermal::getService(THERMAL_SERVICE_NAME, false);
+    ASSERT_NE(thermal_, nullptr);
+    baseSize_ = 0;
+    names_.clear();
+  }
+
+  virtual void TearDown() override {}
+
+ protected:
+  // Check validity of temperatures returned by Thremal HAL.
+  void checkTemperatures(const hidl_vec<Temperature> temperatures) {
+    size_t size = temperatures.size();
+    EXPECT_LE(baseSize_, size);
+
+    for (size_t i = 0; i < size; ++i) {
+      checkDeviceTemperature(temperatures[i]);
+      if (i < baseSize_) {
+        EXPECT_EQ(names_[i], temperatures[i].name.c_str());
+      } else {
+        // Names must be unique.
+        EXPECT_EQ(names_.end(), std::find(names_.begin(), names_.end(),
+                                          temperatures[i].name.c_str()));
+        names_.push_back(temperatures[i].name);
+      }
+    }
+    baseSize_ = size;
+  }
+
+  // Check validity of CPU usages returned by Thermal HAL.
+  void checkCpuUsages(const hidl_vec<CpuUsage>& cpuUsages) {
+    size_t size = cpuUsages.size();
+    // A number of CPU's does not change.
+    if (baseSize_ != 0) EXPECT_EQ(baseSize_, size);
+
+    for (size_t i = 0; i < size; ++i) {
+      checkCpuUsage(cpuUsages[i]);
+      if (i < baseSize_) {
+        EXPECT_EQ(names_[i], cpuUsages[i].name.c_str());
+      } else {
+        // Names must be unique.
+        EXPECT_EQ(names_.end(), std::find(names_.begin(), names_.end(),
+                                          cpuUsages[i].name.c_str()));
+        names_.push_back(cpuUsages[i].name);
+      }
+    }
+    baseSize_ = size;
+  }
+
+  // Check validity of cooling devices information returned by Thermal HAL.
+  void checkCoolingDevices(const hidl_vec<CoolingDevice> coolingDevices) {
+    size_t size = coolingDevices.size();
+    EXPECT_LE(baseSize_, size);
+
+    for (size_t i = 0; i < size; ++i) {
+      checkCoolingDevice(coolingDevices[i]);
+      if (i < baseSize_) {
+        EXPECT_EQ(names_[i], coolingDevices[i].name.c_str());
+      } else {
+        // Names must be unique.
+        EXPECT_EQ(names_.end(), std::find(names_.begin(), names_.end(),
+                                          coolingDevices[i].name.c_str()));
+        names_.push_back(coolingDevices[i].name);
+      }
+    }
+    baseSize_ = size;
+  }
+
+  sp<IThermal> thermal_;
+
+ private:
+  // Check validity of temperature returned by Thermal HAL.
+  void checkDeviceTemperature(const Temperature& temperature) {
+    // .currentValue of known type is in Celsius and must be reasonable.
+    EXPECT_TRUE(temperature.type == TemperatureType::UNKNOWN ||
+                std::abs(temperature.currentValue) < MAX_DEVICE_TEMPERATURE ||
+                temperature.currentValue == UNDEFINED_TEMPERATURE);
+
+    // .name must not be empty.
+    EXPECT_LT(0u, temperature.name.size());
+
+    // .currentValue must not exceed .shutdwonThreshold if defined.
+    EXPECT_TRUE(temperature.currentValue < temperature.shutdownThreshold ||
+                temperature.currentValue == UNDEFINED_TEMPERATURE ||
+                temperature.shutdownThreshold == UNDEFINED_TEMPERATURE);
+
+    // .throttlingThreshold must not exceed .shutdownThreshold if defined.
+    EXPECT_TRUE(temperature.throttlingThreshold <
+                    temperature.shutdownThreshold ||
+                temperature.throttlingThreshold == UNDEFINED_TEMPERATURE ||
+                temperature.shutdownThreshold == UNDEFINED_TEMPERATURE);
+  }
+
+  // Check validity of CPU usage returned by Thermal HAL.
+  void checkCpuUsage(const CpuUsage& cpuUsage) {
+    // .active must be less than .total if CPU is online.
+    EXPECT_TRUE(!cpuUsage.isOnline ||
+                (cpuUsage.active >= 0 && cpuUsage.total >= 0 &&
+                 cpuUsage.total >= cpuUsage.active));
+
+    // .name must be not empty.
+    EXPECT_LT(0u, cpuUsage.name.size());
+  }
+
+  // Check validity of a cooling device information returned by Thermal HAL.
+  void checkCoolingDevice(const CoolingDevice& coolingDevice) {
+    EXPECT_LE(0, coolingDevice.currentValue);
+    EXPECT_GT(MAX_FAN_SPEED, coolingDevice.currentValue);
+    EXPECT_LT(0u, coolingDevice.name.size());
+  }
+
+  size_t baseSize_;
+  std::vector<hidl_string> names_;
+};
+
+// Sanity test for Thermal::getTemperatures().
+TEST_F(ThermalHidlTest, TemperatureTest) {
+  hidl_vec<Temperature> passed;
+  for (size_t i = 0; i < MONITORING_OPERATION_NUMBER; ++i) {
+    thermal_->getTemperatures(
+        [&passed](ThermalStatus status, hidl_vec<Temperature> temperatures) {
+          EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code);
+          passed = temperatures;
+        });
+
+    checkTemperatures(passed);
+    sleep(1);
+  }
+}
+
+// Sanity test for Thermal::getCpuUsages().
+TEST_F(ThermalHidlTest, CpuUsageTest) {
+  hidl_vec<CpuUsage> passed;
+  for (size_t i = 0; i < MONITORING_OPERATION_NUMBER; ++i) {
+    thermal_->getCpuUsages(
+        [&passed](ThermalStatus status, hidl_vec<CpuUsage> cpuUsages) {
+          EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code);
+          passed = cpuUsages;
+        });
+
+    checkCpuUsages(passed);
+    sleep(1);
+  }
+}
+
+// Sanity test for Thermal::getCoolingDevices().
+TEST_F(ThermalHidlTest, CoolingDeviceTest) {
+  hidl_vec<CoolingDevice> passed;
+  for (size_t i = 0; i < MONITORING_OPERATION_NUMBER; ++i) {
+    thermal_->getCoolingDevices([&passed](
+        ThermalStatus status, hidl_vec<CoolingDevice> coolingDevices) {
+      EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code);
+      passed = coolingDevices;
+    });
+
+    checkCoolingDevices(passed);
+    sleep(1);
+  }
+}
+
+int main(int argc, char** argv) {
+  ::testing::InitGoogleTest(&argc, argv);
+  int status = RUN_ALL_TESTS();
+  ALOGI("Test result = %d", status);
+  return status;
+}
diff --git a/thermal/1.0/vts/types.vts b/thermal/1.0/vts/types.vts
new file mode 100644
index 0000000..1e60f48
--- /dev/null
+++ b/thermal/1.0/vts/types.vts
@@ -0,0 +1,157 @@
+component_class: HAL_HIDL
+component_type_version: 1.0
+component_name: "types"
+
+package: "android.hardware.thermal"
+
+
+attribute: {
+    name: "::android::hardware::thermal::V1_0::TemperatureType"
+    type: TYPE_ENUM
+    enum_value: {
+        scalar_type: "uint32_t"
+
+        enumerator: "UNKNOWN"
+        scalar_value: {
+            uint32_t: 1000
+        }
+        enumerator: "CPU"
+        scalar_value: {
+            uint32_t: 0
+        }
+        enumerator: "GPU"
+        scalar_value: {
+            uint32_t: 1
+        }
+        enumerator: "BATTERY"
+        scalar_value: {
+            uint32_t: 2
+        }
+        enumerator: "SKIN"
+        scalar_value: {
+            uint32_t: 3
+        }
+    }
+}
+
+attribute: {
+    name: "::android::hardware::thermal::V1_0::CoolingType"
+    type: TYPE_ENUM
+    enum_value: {
+        scalar_type: "uint32_t"
+
+        enumerator: "FAN_RPM"
+        scalar_value: {
+            uint32_t: 0
+        }
+    }
+}
+
+attribute: {
+    name: "::android::hardware::thermal::V1_0::Temperature"
+    type: TYPE_STRUCT
+    struct_value: {
+        name: "type"
+        type: TYPE_ENUM
+        predefined_type: "::android::hardware::thermal::V1_0::TemperatureType"
+    }
+    struct_value: {
+        name: "name"
+        type: TYPE_STRING
+    }
+    struct_value: {
+        name: "currentValue"
+        type: TYPE_SCALAR
+        scalar_type: "float_t"
+    }
+    struct_value: {
+        name: "throttlingThreshold"
+        type: TYPE_SCALAR
+        scalar_type: "float_t"
+    }
+    struct_value: {
+        name: "shutdownThreshold"
+        type: TYPE_SCALAR
+        scalar_type: "float_t"
+    }
+    struct_value: {
+        name: "vrThrottlingThreshold"
+        type: TYPE_SCALAR
+        scalar_type: "float_t"
+    }
+}
+
+attribute: {
+    name: "::android::hardware::thermal::V1_0::CoolingDevice"
+    type: TYPE_STRUCT
+    struct_value: {
+        name: "type"
+        type: TYPE_ENUM
+        predefined_type: "::android::hardware::thermal::V1_0::CoolingType"
+    }
+    struct_value: {
+        name: "name"
+        type: TYPE_STRING
+    }
+    struct_value: {
+        name: "currentValue"
+        type: TYPE_SCALAR
+        scalar_type: "float_t"
+    }
+}
+
+attribute: {
+    name: "::android::hardware::thermal::V1_0::CpuUsage"
+    type: TYPE_STRUCT
+    struct_value: {
+        name: "name"
+        type: TYPE_STRING
+    }
+    struct_value: {
+        name: "active"
+        type: TYPE_SCALAR
+        scalar_type: "uint64_t"
+    }
+    struct_value: {
+        name: "total"
+        type: TYPE_SCALAR
+        scalar_type: "uint64_t"
+    }
+    struct_value: {
+        name: "isOnline"
+        type: TYPE_SCALAR
+        scalar_type: "bool_t"
+    }
+}
+
+attribute: {
+    name: "::android::hardware::thermal::V1_0::ThermalStatusCode"
+    type: TYPE_ENUM
+    enum_value: {
+        scalar_type: "uint32_t"
+
+        enumerator: "SUCCESS"
+        scalar_value: {
+            uint32_t: 0
+        }
+        enumerator: "FAILURE"
+        scalar_value: {
+            uint32_t: 1
+        }
+    }
+}
+
+attribute: {
+    name: "::android::hardware::thermal::V1_0::ThermalStatus"
+    type: TYPE_STRUCT
+    struct_value: {
+        name: "code"
+        type: TYPE_ENUM
+        predefined_type: "::android::hardware::thermal::V1_0::ThermalStatusCode"
+    }
+    struct_value: {
+        name: "debugMessage"
+        type: TYPE_STRING
+    }
+}
+