Merge "thermalservice: plumb up Thermal HAL 2.0 and add dump"
diff --git a/services/thermalservice/Android.bp b/services/thermalservice/Android.bp
index d754560..2812c13 100644
--- a/services/thermalservice/Android.bp
+++ b/services/thermalservice/Android.bp
@@ -40,11 +40,13 @@
     include_dirs: ["frameworks/native"],
 
     shared_libs: [
+        "libbase",
         "libthermalservice",
         "libbinder",
         "libutils",
         "libthermalcallback",
         "android.hardware.thermal@1.1",
+        "android.hardware.thermal@2.0",
         "libhidlbase",
         "libhidltransport",
         "liblog",
diff --git a/services/thermalservice/ThermalService.cpp b/services/thermalservice/ThermalService.cpp
index 6e09a83..b1a80de 100644
--- a/services/thermalservice/ThermalService.cpp
+++ b/services/thermalservice/ThermalService.cpp
@@ -15,11 +15,16 @@
  */
 
 #include "ThermalService.h"
-#include <android/os/IThermalService.h>
+#include <android-base/file.h>
+#include <android-base/stringprintf.h>
 #include <android/os/IThermalEventListener.h>
+#include <android/os/IThermalService.h>
 #include <android/os/Temperature.h>
 #include <binder/IPCThreadState.h>
 #include <binder/IServiceManager.h>
+#include <binder/PermissionCache.h>
+#include <log/log.h>
+#include <private/android_filesystem_config.h>
 #include <utils/Errors.h>
 #include <utils/Mutex.h>
 #include <utils/String16.h>
@@ -28,6 +33,35 @@
 namespace os {
 
 /**
+ * Dump thermal service
+ * @param fd file descriptor for dumping
+ * @param args not used
+ */
+status_t ThermalService::dump(int fd, const Vector<String16>& /* args */) {
+    status_t ret = OK;
+    std::string result;
+    const IPCThreadState* ipc = IPCThreadState::self();
+    const int pid = ipc->getCallingPid();
+    const int uid = ipc->getCallingUid();
+    if ((uid != AID_SHELL) &&
+        !PermissionCache::checkPermission(String16("android.permission.DUMP"), pid, uid)) {
+        result = android::base::
+                StringPrintf("Permission Denial: can't dump ThermalService from pid=%d, uid=%d\n",
+                             pid, uid);
+        ret = PERMISSION_DENIED;
+    } else {
+        Mutex::Autolock _l(mListenersLock);
+        result = android::base::StringPrintf("ThermalEventListener registered: %d\n",
+                                             (int)mListeners.size());
+    }
+    if (!android::base::WriteStringToFd(result, fd)) {
+        SLOGE("Failed to dump fd: %d", fd);
+        ret = FDS_NOT_ALLOWED;
+    }
+    return ret;
+}
+
+/**
  * Notify registered listeners of a thermal throttling start/stop event.
  * @param temperature the temperature at which the event was generated
  */
@@ -62,8 +96,9 @@
 binder::Status ThermalService::registerThermalEventListener(
     const sp<IThermalEventListener>& listener) {
     {
-        if (listener == NULL)
+        if (listener == NULL) {
             return binder::Status::ok();
+        }
         Mutex::Autolock _l(mListenersLock);
         // check whether this is a duplicate
         for (size_t i = 0; i < mListeners.size(); i++) {
@@ -87,8 +122,9 @@
  */
 binder::Status ThermalService::unregisterThermalEventListener(
     const sp<IThermalEventListener>& listener) {
-    if (listener == NULL)
+    if (listener == NULL) {
         return binder::Status::ok();
+    }
     Mutex::Autolock _l(mListenersLock);
     for (size_t i = 0; i < mListeners.size(); i++) {
         if (IInterface::asBinder(mListeners[i]) ==
diff --git a/services/thermalservice/ThermalService.h b/services/thermalservice/ThermalService.h
index 17dfcbc..d3da900 100644
--- a/services/thermalservice/ThermalService.h
+++ b/services/thermalservice/ThermalService.h
@@ -34,6 +34,7 @@
     void publish(const sp<ThermalService>& service);
     binder::Status notifyThrottling(
         const bool isThrottling, const Temperature& temperature);
+    status_t dump(int fd, const Vector<String16>& args) override;
 
 private:
     Mutex mListenersLock;
diff --git a/services/thermalservice/libthermalcallback/Android.bp b/services/thermalservice/libthermalcallback/Android.bp
index e98506e..312579c 100644
--- a/services/thermalservice/libthermalcallback/Android.bp
+++ b/services/thermalservice/libthermalcallback/Android.bp
@@ -2,6 +2,7 @@
     name: "libthermalcallback",
     srcs: [
         "ThermalCallback.cpp",
+        "ThermalChangedCallback.cpp",
     ],
     cflags: [
         "-Wall",
@@ -10,6 +11,7 @@
     include_dirs: ["frameworks/native"],
     shared_libs: [
         "android.hardware.thermal@1.1",
+        "android.hardware.thermal@2.0",
         "libhidlbase",
         "libhidltransport",
         "liblog",
diff --git a/services/thermalservice/libthermalcallback/ThermalCallback.cpp b/services/thermalservice/libthermalcallback/ThermalCallback.cpp
index 5e094fa..0f3132c 100644
--- a/services/thermalservice/libthermalcallback/ThermalCallback.cpp
+++ b/services/thermalservice/libthermalcallback/ThermalCallback.cpp
@@ -1,11 +1,11 @@
 #define LOG_TAG "android.hardware.thermal.thermalcallback@1.1-impl"
 #include <log/log.h>
 
-#include "ThermalCallback.h"
-#include "services/thermalservice/ThermalService.h"
-#include <math.h>
 #include <android/os/Temperature.h>
 #include <hardware/thermal.h>
+#include <cmath>
+#include "ThermalCallback.h"
+#include "services/thermalservice/ThermalService.h"
 
 namespace android {
 namespace hardware {
@@ -57,7 +57,7 @@
         android::os::Temperature thermal_svc_temp(value, type);
         mThermalService->notifyThrottling(isThrottling, thermal_svc_temp);
     } else {
-        ALOGE("IThermalService binder service not created, drop throttling event");
+        SLOGE("IThermalService binder service not created, drop throttling event");
     }
     return Void();
 }
diff --git a/services/thermalservice/libthermalcallback/ThermalChangedCallback.cpp b/services/thermalservice/libthermalcallback/ThermalChangedCallback.cpp
new file mode 100644
index 0000000..0efd732
--- /dev/null
+++ b/services/thermalservice/libthermalcallback/ThermalChangedCallback.cpp
@@ -0,0 +1,83 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+#define LOG_TAG "android.hardware.thermal.thermalchangedcallback@2.0-impl"
+#include <log/log.h>
+
+#include <android/os/Temperature.h>
+#include <hardware/thermal.h>
+#include <cmath>
+#include "ThermalChangedCallback.h"
+#include "services/thermalservice/ThermalService.h"
+
+namespace android {
+namespace hardware {
+namespace thermal {
+namespace V2_0 {
+namespace implementation {
+
+using ::android::hardware::thermal::V2_0::TemperatureType;
+using ::android::hardware::thermal::V2_0::ThrottlingSeverity;
+using ::android::os::ThermalService;
+
+// Register a binder ThermalService object for sending events
+void ThermalChangedCallback::registerThermalService(sp<ThermalService> thermalService) {
+    mThermalService = thermalService;
+}
+
+// Methods from IThermalChangedCallback::V2_0 follow.
+Return<void> ThermalChangedCallback::notifyThrottling(
+        const android::hardware::thermal::V2_0::Temperature& temperature) {
+    // Convert HIDL IThermal Temperature to binder IThermalService Temperature.
+    if (mThermalService != nullptr) {
+        float value = NAN;
+        int type = DEVICE_TEMPERATURE_UNKNOWN;
+
+        switch (temperature.type) {
+            case TemperatureType::CPU:
+                type = DEVICE_TEMPERATURE_CPU;
+                break;
+            case TemperatureType::GPU:
+                type = DEVICE_TEMPERATURE_GPU;
+                break;
+            case TemperatureType::BATTERY:
+                type = DEVICE_TEMPERATURE_BATTERY;
+                break;
+            case TemperatureType::SKIN:
+                type = DEVICE_TEMPERATURE_SKIN;
+                break;
+            case TemperatureType::UNKNOWN:
+            default:
+                type = DEVICE_TEMPERATURE_UNKNOWN;
+                break;
+        }
+        bool isThrottling = (static_cast<size_t>(temperature.throttlingStatus) >=
+                             static_cast<size_t>(ThrottlingSeverity::SEVERE))
+                ? true
+                : false;
+        android::os::Temperature thermal_svc_temp(value, type);
+        mThermalService->notifyThrottling(isThrottling, thermal_svc_temp);
+    } else {
+        SLOGE("IThermalService binder service not created, drop throttling event");
+    }
+    return Void();
+}
+
+} // namespace implementation
+} // namespace V2_0
+} // namespace thermal
+} // namespace hardware
+} // namespace android
diff --git a/services/thermalservice/libthermalcallback/ThermalChangedCallback.h b/services/thermalservice/libthermalcallback/ThermalChangedCallback.h
new file mode 100644
index 0000000..03de049
--- /dev/null
+++ b/services/thermalservice/libthermalcallback/ThermalChangedCallback.h
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+#ifndef ANDROID_HARDWARE_THERMAL_V1_1_THERMALCHANGEDCALLBACK_H
+#define ANDROID_HARDWARE_THERMAL_V1_1_THERMALCHANGEDCALLBACK_H
+
+#include <android/hardware/thermal/2.0/IThermalChangedCallback.h>
+#include <android/hardware/thermal/2.0/types.h>
+#include <android/os/Temperature.h>
+#include <hidl/MQDescriptor.h>
+#include <hidl/Status.h>
+#include "services/thermalservice/ThermalService.h"
+
+namespace android {
+namespace hardware {
+namespace thermal {
+namespace V2_0 {
+namespace implementation {
+
+using ::android::hardware::Return;
+using ::android::hardware::Void;
+using ::android::os::ThermalService;
+
+class ThermalChangedCallback : public IThermalChangedCallback {
+public:
+    // Register a binder ThermalService object for sending events
+    void registerThermalService(sp<ThermalService> thermalService);
+
+    // Methods from I ThermalChangedCallback::V2_0 follow.
+    Return<void> notifyThrottling(
+            const android::hardware::thermal::V2_0::Temperature& temperature) override;
+
+private:
+    // Our registered binder ThermalService object to use for sending events
+    sp<android::os::ThermalService> mThermalService;
+};
+
+} // namespace implementation
+} // namespace V2_0
+} // namespace thermal
+} // namespace hardware
+} // namespace android
+
+#endif // ANDROID_HARDWARE_THERMAL_V1_1_THERMALCHANGEDCALLBACK_H
diff --git a/services/thermalservice/thermalserviced.cpp b/services/thermalservice/thermalserviced.cpp
index 8e27266..b3da76b 100644
--- a/services/thermalservice/thermalserviced.cpp
+++ b/services/thermalservice/thermalserviced.cpp
@@ -17,93 +17,153 @@
 #define LOG_TAG "thermalserviced"
 #include <log/log.h>
 
-#include "thermalserviced.h"
 #include "ThermalService.h"
 #include "libthermalcallback/ThermalCallback.h"
+#include "libthermalcallback/ThermalChangedCallback.h"
+#include "thermalserviced.h"
 
 #include <android/hardware/thermal/1.1/IThermal.h>
+#include <android/hardware/thermal/2.0/IThermal.h>
+#include <android/hardware/thermal/2.0/types.h>
+
 #include <binder/IPCThreadState.h>
 #include <binder/IServiceManager.h>
 #include <hidl/HidlTransportSupport.h>
 
 using namespace android;
-using ::android::hardware::thermal::V1_1::IThermal;
-using ::android::hardware::thermal::V1_0::Temperature;
-using ::android::hardware::thermal::V1_1::IThermalCallback;
-using ::android::hardware::thermal::V1_1::implementation::ThermalCallback;
+using IThermal1_1 = ::android::hardware::thermal::V1_1::IThermal;
+using IThermal2_0 = ::android::hardware::thermal::V2_0::IThermal;
 using ::android::hardware::configureRpcThreadpool;
 using ::android::hardware::hidl_death_recipient;
+using ::android::hardware::Return;
+using ::android::hardware::thermal::V1_0::ThermalStatus;
+using ::android::hardware::thermal::V1_0::ThermalStatusCode;
+using ::android::hardware::thermal::V1_1::IThermalCallback;
+using ::android::hardware::thermal::V1_1::implementation::ThermalCallback;
+using ::android::hardware::thermal::V2_0::IThermalChangedCallback;
+using ::android::hardware::thermal::V2_0::TemperatureType;
 using ::android::hidl::base::V1_0::IBase;
 using ::android::os::ThermalService;
 
-template<typename T>
-using Return = hardware::Return<T>;
-
 namespace {
 
 // Our thermalserviced main object
 ThermalServiceDaemon* gThermalServiceDaemon;
 
-// Thermal HAL client
-sp<IThermal> gThermalHal = nullptr;
+// Thermal HAL 1.1 client
+sp<IThermal1_1> gThermalHal1_1 = nullptr;
+// Thermal HAL 2.0 client
+sp<IThermal2_0> gThermalHal2_0 = nullptr;
 
 // Binder death notifier informing of Thermal HAL death.
 struct ThermalServiceDeathRecipient : hidl_death_recipient {
     virtual void serviceDied(
         uint64_t cookie __unused, const wp<IBase>& who __unused) {
-        gThermalHal = nullptr;
-        ALOGE("IThermal HAL died");
+        SLOGE("IThermal HAL died");
+        gThermalHal1_1 = nullptr;
+        gThermalHal2_0 = nullptr;
         gThermalServiceDaemon->getThermalHal();
     }
 };
 
-sp<ThermalServiceDeathRecipient> gThermalHalDied = nullptr;
-
 }  // anonymous namespace
 
 void ThermalServiceDaemon::thermalServiceStartup() {
-    // Binder IThermalService startup
+    // Binder IThermal1_1Service startup
     mThermalService = new android::os::ThermalService;
     mThermalService->publish(mThermalService);
-    // Register IThermalService object with IThermalCallback
-    if (mThermalCallback != nullptr)
+    // Register IThermalService object to ThermalHAL callback
+    if (mThermalChangedCallback != nullptr) {
+        mThermalChangedCallback->registerThermalService(mThermalService);
+    } else if (mThermalCallback != nullptr) {
         mThermalCallback->registerThermalService(mThermalService);
+    }
     IPCThreadState::self()->joinThreadPool();
 }
 
 // Lookup Thermal HAL, register death notifier, register our
 // ThermalCallback with the Thermal HAL.
 void ThermalServiceDaemon::getThermalHal() {
-    gThermalHal = IThermal::getService();
-    if (gThermalHal == nullptr) {
-        ALOGW("Unable to get Thermal HAL V1.1, vendor thermal event notification not available");
-        return;
+    static sp<ThermalServiceDeathRecipient> gThermalHalDied = nullptr;
+    // Binder death notifier for Thermal HAL
+    if (gThermalHalDied == nullptr) {
+        gThermalHalDied = new ThermalServiceDeathRecipient();
     }
 
-    // Binder death notifier for Thermal HAL
-    if (gThermalHalDied == nullptr)
-        gThermalHalDied = new ThermalServiceDeathRecipient();
+    gThermalHal2_0 = IThermal2_0::getService();
+    if (gThermalHal2_0 == nullptr) {
+        SLOGW("Unable to get Thermal HAL V2.0, fallback to 1.1");
+        gThermalHal1_1 = IThermal1_1::getService();
+        if (gThermalHal1_1 == nullptr) {
+            SLOGW("Unable to get Thermal HAL V1.1, vendor thermal event "
+                  "notification not available");
+            return;
+        }
+        if (gThermalHalDied != nullptr) {
+            gThermalHal1_1->linkToDeath(gThermalHalDied, 0x451F /* cookie */);
+        }
 
-    if (gThermalHalDied != nullptr)
-        gThermalHal->linkToDeath(gThermalHalDied, 0x451F /* cookie */);
+        if (mThermalCallback != nullptr) {
+            Return<void> ret = gThermalHal1_1->registerThermalCallback(mThermalCallback);
+            if (!ret.isOk()) {
+                SLOGE("registerThermalCallback failed, status: %s", ret.description().c_str());
+            }
+        }
+    }
+
+    if (gThermalHalDied != nullptr) {
+        gThermalHal2_0->linkToDeath(gThermalHalDied, 0x451F /* cookie */);
+    }
 
     if (mThermalCallback != nullptr) {
-        Return<void> ret = gThermalHal->registerThermalCallback(
-            mThermalCallback);
-        if (!ret.isOk())
-            ALOGE("registerThermalCallback failed, status: %s",
-                  ret.description().c_str());
+        Return<void> ret =
+                gThermalHal2_0
+                        ->registerThermalChangedCallback(
+                            mThermalChangedCallback,
+                            false,
+                            TemperatureType::SKIN, // not used
+                            [](ThermalStatus status) {
+                                if (ThermalStatusCode::SUCCESS !=
+                                    status.code) {
+                                    SLOGE("registerThermalChangedCallback failed, status: %s",
+                                          status.debugMessage.c_str());
+                                }
+                            });
+        if (!ret.isOk()) {
+            SLOGE("registerThermalChangedCallback failed, status: %s", ret.description().c_str());
+        }
+    }
+}
+
+ThermalServiceDaemon::~ThermalServiceDaemon() {
+    if (mThermalCallback != nullptr && gThermalHal2_0 != nullptr) {
+        Return<void> ret =
+                gThermalHal2_0
+                        ->unregisterThermalChangedCallback(
+                            mThermalChangedCallback,
+                            [](ThermalStatus status) {
+                                if (ThermalStatusCode::SUCCESS !=
+                                    status.code) {
+                                    SLOGE("unregisterThermalChangedCallback failed, status: %s",
+                                          status.debugMessage
+                                          .c_str());
+                                }
+                            });
+        if (!ret.isOk()) {
+            SLOGE("unregisterThermalChangedCallback failed, status: %s", ret.description().c_str());
+        }
     }
 }
 
 void ThermalServiceDaemon::thermalCallbackStartup() {
-    // HIDL IThermalCallback startup
+    // HIDL IThermal Callback startup
     // Need at least 2 threads in thread pool since we wait for dead HAL
     // to come back on the binder death notification thread and we need
     // another thread for the incoming service now available call.
     configureRpcThreadpool(2, false /* callerWillJoin */);
     mThermalCallback = new ThermalCallback();
-    // Lookup Thermal HAL and register our ThermalCallback.
+    mThermalChangedCallback = new ThermalChangedCallback();
+    // Lookup Thermal HAL 1.1 and 2.0 to register our Callback.
     getThermalHal();
 }
 
diff --git a/services/thermalservice/thermalserviced.h b/services/thermalservice/thermalserviced.h
index 309e2fe..c6575f3 100644
--- a/services/thermalservice/thermalserviced.h
+++ b/services/thermalservice/thermalserviced.h
@@ -19,22 +19,26 @@
 
 #include "ThermalService.h"
 #include "libthermalcallback/ThermalCallback.h"
+#include "libthermalcallback/ThermalChangedCallback.h"
 
 using namespace android;
 using ::android::hardware::thermal::V1_0::Temperature;
 using ::android::hardware::thermal::V1_1::implementation::ThermalCallback;
+using ::android::hardware::thermal::V2_0::implementation::ThermalChangedCallback;
 using ::android::os::ThermalService;
 
 class ThermalServiceDaemon {
  public:
+    ~ThermalServiceDaemon();
     void thermalServiceStartup();
     void thermalCallbackStartup();
     void getThermalHal();
-    ThermalServiceDaemon() {};
+    ThermalServiceDaemon(){};
 
  private:
     sp<ThermalService> mThermalService;
     sp<ThermalCallback> mThermalCallback;
+    sp<ThermalChangedCallback> mThermalChangedCallback;
 };
 
 #endif  // ANDROID_THERMALSERVICE_THERMALSERVICED_H