thermal-hal: Return failure only for uninitialized sensor request

The thermal hal checks for all configured sensors are initialized
or not. If any of the sensors in config is not initialized or failed
to initialize, it returns failure immediately for all sensors
temperature related HAL API request.

Change-Id: I3f06fa74262dcf22268d265725baee69520696aa
(cherry picked from commit 947b8a06cddc33c1fbc5e8408ef0ffb08a8f18b3)
Signed-off-by: Priyansh Jain <quic_priyjain@quicinc.com>
diff --git a/thermal.cpp b/thermal.cpp
index cca9caa..7134abd 100644
--- a/thermal.cpp
+++ b/thermal.cpp
@@ -30,7 +30,7 @@
 
 /* Changes from Qualcomm Innovation Center are provided under the following license:
 
-Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
+Copyright (c) 2023, 2025 Qualcomm Innovation Center, Inc. All rights reserved.
 SPDX-License-Identifier: BSD-3-Clause-Clear */
 
 #include <ctype.h>
@@ -139,9 +139,9 @@
 
 	std::vector<Temperature> temperatures;
 
-	if (!utils.isSensorInitialized())
+	if (!utils.isSensorInitialized(in_type))
 		return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
-					"ThermalHAL not initialized properly.");
+					"ThermalHAL given sensor type Not initialized.");
 	else {
 		if (utils.readTemperatures(in_type, temperatures) <= 0)
 			LOG(VERBOSE) << __func__ << "Sensor Temperature read failure.";
@@ -159,7 +159,7 @@
 
 	if (!utils.isSensorInitialized())
 		return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
-					"ThermalHAL not initialized properly.");
+					"ThermalHAL for sensor not initialized.");
 
 	if (utils.readTemperatureThreshold(thresh) <= 0)
 		LOG(VERBOSE) << __func__ << "Sensor Threshold read failure or type not supported.";
@@ -176,9 +176,9 @@
 
 	std::vector<TemperatureThreshold> thresh;
 
-	if (!utils.isSensorInitialized())
+	if (!utils.isSensorInitialized(in_type))
 		return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
-					"ThermalHAL not initialized properly.");
+					"ThermalHAL given sensor type not initialized.");
 	else{
 		if (utils.readTemperatureThreshold(in_type, thresh) <= 0)
 			LOG(VERBOSE) << __func__ << "Sensor Threshold read failure or type not supported.";
diff --git a/thermalCommon.cpp b/thermalCommon.cpp
index 5ecd8de..4274638 100644
--- a/thermalCommon.cpp
+++ b/thermalCommon.cpp
@@ -309,6 +309,17 @@
 	return 0;
 }
 
+int ThermalCommon::initNewThermalZone(struct target_therm_cfg& cfg)
+{
+
+	if (cfg.type == TemperatureType::CPU)
+		initializeCpuSensor(cfg);
+	else
+		initialize_sensor(cfg, 0);
+
+	return 1;
+}
+
 int ThermalCommon::initThermalZones(std::vector<struct target_therm_cfg>& cfg)
 {
 	std::vector<struct target_therm_cfg>::iterator it;
@@ -320,14 +331,10 @@
 
 	for (it = cfg.begin(); it != cfg.end(); it++)
 	{
-		if (it->type == TemperatureType::CPU) {
-			if (initializeCpuSensor(*it) < 0)
-				return -1;
-			continue;
-		}
-		if (initialize_sensor(*it, 0) < 0) {
-			return -1;
-		}
+		if (it->type == TemperatureType::CPU)
+			initializeCpuSensor(*it);
+		else
+			initialize_sensor(*it, 0);
 	}
 
 	return sens.size();
diff --git a/thermalCommon.h b/thermalCommon.h
index ef96179..9488afe 100644
--- a/thermalCommon.h
+++ b/thermalCommon.h
@@ -47,6 +47,7 @@
 
 		int readFromFile(std::string_view path, std::string& out);
 		int initThermalZones(std::vector<struct target_therm_cfg>& cfg);
+		int initNewThermalZone(struct target_therm_cfg& cfg);
 		void initThreshold(struct therm_sensor& sens);
 		int initCdev();
 
diff --git a/thermalUtilsNetlink.cpp b/thermalUtilsNetlink.cpp
index 7a6c86d..1a0e3a2 100644
--- a/thermalUtilsNetlink.cpp
+++ b/thermalUtilsNetlink.cpp
@@ -31,7 +31,7 @@
 
 /* Changes from Qualcomm Innovation Center are provided under the following license:
 
-Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
+Copyright (c) 2023, 2025 Qualcomm Innovation Center, Inc. All rights reserved.
 SPDX-License-Identifier: BSD-3-Clause-Clear */
 
 #include <android-base/file.h>
@@ -66,11 +66,9 @@
 	std::vector<struct therm_sensor> sensorList;
 	std::vector<struct target_therm_cfg> therm_cfg = cfg.fetchConfig();
 
-	is_sensor_init = false;
 	is_cdev_init = false;
 	ret = cmnInst.initThermalZones(therm_cfg);
 	if (ret > 0) {
-		is_sensor_init = true;
 		sensorList = cmnInst.fetch_sensor_list();
 		std::lock_guard<std::mutex> _lock(sens_cb_mutex);
 		for (struct therm_sensor sens: sensorList) {
@@ -88,6 +86,34 @@
 	}
 }
 
+bool ThermalUtils::isSensorInitialized()
+{
+	std::lock_guard<std::mutex> _lock(sens_cb_mutex);
+
+	if (thermalConfig.begin() == thermalConfig.end())
+		return false;
+
+	return true;
+}
+
+bool ThermalUtils::isSensorInitialized(TemperatureType type)
+{
+	std::unordered_map<int, struct therm_sensor>::iterator it;
+	std::lock_guard<std::mutex> _lock(sens_cb_mutex);
+
+	if (thermalConfig.begin() == thermalConfig.end())
+		return false;
+
+	for (it = thermalConfig.begin(); it != thermalConfig.end();
+			it++) {
+		struct therm_sensor& sens = it->second;
+		if (sens.t.type == type)
+			return true;
+	}
+
+	return false;
+}
+
 void ThermalUtils::Notify(struct therm_sensor& sens)
 {
 	int severity = cmnInst.estimateSeverity(sens);
@@ -136,8 +162,9 @@
 	std::vector<struct target_therm_cfg>::iterator it_vec;
 	std::vector<std::string>::iterator it;
 
-	if (isSensorInitialized())
+	if (thermalConfig.find(tzn) != thermalConfig.end())
 		return;
+
 	for (it_vec = therm_cfg.begin();
 		it_vec != therm_cfg.end(); it_vec++) {
 		for (it = it_vec->sensor_list.begin();
@@ -153,16 +180,18 @@
 			<< std::endl;
 		return;
 	}
-	ret = cmnInst.initThermalZones(therm_cfg);
+	ret = cmnInst.initNewThermalZone(*it_vec);
 	if (ret > 0) {
-		is_sensor_init = true;
 		sensorList = cmnInst.fetch_sensor_list();
 		std::lock_guard<std::mutex> _lock(sens_cb_mutex);
 		for (struct therm_sensor sens: sensorList) {
+			if (sens.sensor_name != name)
+				continue;
 			thermalConfig[sens.tzn] = sens;
 			cmnInst.read_temperature(sens);
 			cmnInst.estimateSeverity(sens);
 			cmnInst.initThreshold(sens);
+			break;
 		}
 	}
 }
diff --git a/thermalUtilsNetlink.h b/thermalUtilsNetlink.h
index 8900c9f..bee0fbc 100644
--- a/thermalUtilsNetlink.h
+++ b/thermalUtilsNetlink.h
@@ -31,7 +31,7 @@
 
 /* Changes from Qualcomm Innovation Center are provided under the following license:
 
-Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
+Copyright (c) 2023, 2025 Qualcomm Innovation Center, Inc. All rights reserved.
 SPDX-License-Identifier: BSD-3-Clause-Clear */
 
 #ifndef THERMAL_THERMAL_UTILS_H__
@@ -56,10 +56,8 @@
 	public:
 		ThermalUtils(const ueventCB &inp_cb);
 		~ThermalUtils() = default;
-		bool isSensorInitialized()
-		{
-			return is_sensor_init;
-		};
+		bool isSensorInitialized();
+		bool isSensorInitialized(TemperatureType type);
 		bool isCdevInitialized()
 		{
 			return is_cdev_init;
@@ -74,7 +72,6 @@
 		int readCdevStates(cdevType type,
                                             std::vector<CoolingDevice>& cdev);
 	private:
-		bool is_sensor_init;
 		bool is_cdev_init;
 		ThermalConfig cfg;
 		ThermalCommon cmnInst;