thermal: reconnect to PowerHAL in case of failure

Bug: 262585499
Test: stop/start vendor.power-hal-aidl and verified thermal power hint
by emul_temp

Change-Id: I6dfc0a3d7f6ef0c39a464f5952bb447db73c20d8
diff --git a/thermal/utils/powerhal_helper.cpp b/thermal/utils/powerhal_helper.cpp
index 515fa2d..e20f518 100644
--- a/thermal/utils/powerhal_helper.cpp
+++ b/thermal/utils/powerhal_helper.cpp
@@ -48,11 +48,14 @@
 
 bool PowerHalService::connect() {
     std::lock_guard<std::mutex> lock(lock_);
-    if (!power_hal_aidl_exist_)
-        return false;
 
-    if (power_hal_aidl_ != nullptr)
+    if (!power_hal_aidl_exist_) {
+        return false;
+    }
+
+    if (power_hal_aidl_ && power_hal_ext_aidl_) {
         return true;
+    }
 
     const std::string kInstance = std::string(IPower::descriptor) + "/default";
     ndk::SpAIBinder power_binder = ndk::SpAIBinder(AServiceManager_getService(kInstance.c_str()));
@@ -90,14 +93,13 @@
 
 bool PowerHalService::isModeSupported(const std::string &type, const ThrottlingSeverity &t) {
     bool isSupported = false;
-    if (!isPowerHalConnected()) {
+    if (!connect()) {
         return false;
     }
     std::string power_hint = StringPrintf("THERMAL_%s_%s", type.c_str(), toString(t).c_str());
     lock_.lock();
     if (!power_hal_ext_aidl_->isModeSupported(power_hint, &isSupported).isOk()) {
         LOG(ERROR) << "Fail to check supported mode, Hint: " << power_hint;
-        power_hal_aidl_exist_ = false;
         power_hal_ext_aidl_ = nullptr;
         power_hal_aidl_ = nullptr;
         lock_.unlock();
@@ -108,20 +110,23 @@
 }
 
 void PowerHalService::setMode(const std::string &type, const ThrottlingSeverity &t,
-                              const bool &enable) {
-    if (!isPowerHalConnected()) {
+                              const bool &enable, const bool error_on_exit) {
+    if (!connect()) {
         return;
     }
 
     std::string power_hint = StringPrintf("THERMAL_%s_%s", type.c_str(), toString(t).c_str());
-    LOG(INFO) << "Send Hint " << power_hint << " Enable: " << std::boolalpha << enable;
+    LOG(INFO) << (error_on_exit ? "Resend Hint " : "Send Hint ") << power_hint
+              << " Enable: " << std::boolalpha << enable;
     lock_.lock();
     if (!power_hal_ext_aidl_->setMode(power_hint, enable).isOk()) {
         LOG(ERROR) << "Fail to set mode, Hint: " << power_hint;
-        power_hal_aidl_exist_ = false;
         power_hal_ext_aidl_ = nullptr;
         power_hal_aidl_ = nullptr;
         lock_.unlock();
+        if (!error_on_exit) {
+            setMode(type, t, enable, true);
+        }
         return;
     }
     lock_.unlock();
diff --git a/thermal/utils/powerhal_helper.h b/thermal/utils/powerhal_helper.h
index 761e315..d629cef 100644
--- a/thermal/utils/powerhal_helper.h
+++ b/thermal/utils/powerhal_helper.h
@@ -51,7 +51,8 @@
     bool isModeSupported(const std::string &type, const ThrottlingSeverity &t);
     bool isPowerHalConnected() { return power_hal_aidl_ != nullptr; }
     bool isPowerHalExtConnected() { return power_hal_ext_aidl_ != nullptr; }
-    void setMode(const std::string &type, const ThrottlingSeverity &t, const bool &enable);
+    void setMode(const std::string &type, const ThrottlingSeverity &t, const bool &enable,
+                 const bool error_on_exit = false);
 
   private:
     bool power_hal_aidl_exist_;