thermal-hal: Return empty vector when a sensor is not supported

Right now the thermal HAL will return a temperature, temperature
threshold and cooling device entry with a NAN value for a sensor that is
not supported. This will break the VTS test case, which expects an empty
vector in that case.

Change the thermal HAL to return empty vector in case the sensor is not
supported.

Also populate the thresholds value for CPU, GPU, Battery and skin
sensors for the V1.0 getTemperature API. Make sure that the threshold
values are also read in degree celsius.

Change-Id: I82ab1e9dd9672f1b93760e93b55e8943fc5acce0
diff --git a/thermal.cpp b/thermal.cpp
index f650c9f..1d8cdcb 100644
--- a/thermal.cpp
+++ b/thermal.cpp
@@ -90,7 +90,7 @@
 		return exit_hal(_hidl_cb, temperatures,
 			"ThermalHAL not initialized properly.");
 
-	if (!utils.readTemperatures(&temperatures))
+	if (utils.readTemperatures(temperatures) <= 0)
 		return exit_hal(_hidl_cb, temperatures,
 				"Sensor Temperature read failure.");
 
@@ -143,7 +143,7 @@
 	if (!utils.isCdevInitialized())
 		return exit_hal(_hidl_cb, cdev,
 			"ThermalHAL not initialized properly.");
-	if (!utils.readCdevStates(filterType, type, &cdev))
+	if (utils.readCdevStates(filterType, type, cdev) <= 0)
 		return exit_hal(_hidl_cb, cdev,
 			"Failed to read thermal cooling devices.");
 
@@ -164,7 +164,7 @@
 		return exit_hal(_hidl_cb, temperatures,
 			"ThermalHAL not initialized properly.");
 
-	if (!utils.readTemperatures(filterType, type, &temperatures))
+	if (utils.readTemperatures(filterType, type, temperatures) <= 0)
 		return exit_hal(_hidl_cb, temperatures,
 				"Sensor Temperature read failure.");
 
@@ -186,9 +186,9 @@
 		return exit_hal(_hidl_cb, thresh,
 			"ThermalHAL not initialized properly.");
 
-	if (!utils.readTemperatureThreshold(filterType, type, &thresh))
+	if (utils.readTemperatureThreshold(filterType, type, thresh) <= 0)
 		return exit_hal(_hidl_cb, thresh,
-				"Sensor Temperature threshold read failure.");
+		"Sensor Threshold read failure or type not supported.");
 
 	_hidl_cb(status, thresh);
 
@@ -203,7 +203,6 @@
 {
 	ThermalStatus status;
 	std::lock_guard<std::mutex> _lock(thermal_cb_mutex);
-	std::vector<CallbackSetting>::iterator it;
 
         status.code = ThermalStatusCode::SUCCESS;
 	if (callback == nullptr)
@@ -213,8 +212,8 @@
 		return exit_hal(_hidl_cb,
 			"BCL current and voltage notification not supported");
 
-	for (it = cb.begin(); it != cb.end(); it++) {
-		if (interfacesEqual(it->callback, callback))
+	for (CallbackSetting _cb: cb) {
+		if (interfacesEqual(_cb.callback, callback))
 			return exit_hal(_hidl_cb,
 				"Same callback interface registered already");
 	}
diff --git a/thermalCommon.cpp b/thermalCommon.cpp
index 308086c..2e981dd 100644
--- a/thermalCommon.cpp
+++ b/thermalCommon.cpp
@@ -249,21 +249,21 @@
 
 	if (cfg.throt_thresh != 0 && cfg.positive_thresh_ramp)
 		sensor.thresh.hotThrottlingThresholds[(size_t)ThrottlingSeverity::SEVERE - 1] =
-			cfg.throt_thresh;
+			cfg.throt_thresh / (float)sensor.mulFactor;
 	else if (cfg.throt_thresh != 0 && !cfg.positive_thresh_ramp)
 		sensor.thresh.coldThrottlingThresholds[(size_t)ThrottlingSeverity::SEVERE - 1] =
-			cfg.throt_thresh;
+			cfg.throt_thresh / (float)sensor.mulFactor;
 
 	if (cfg.shutdwn_thresh != 0 && cfg.positive_thresh_ramp)
 		sensor.thresh.hotThrottlingThresholds[(size_t)ThrottlingSeverity::SHUTDOWN - 1] =
-			cfg.shutdwn_thresh;
+			cfg.shutdwn_thresh / (float)sensor.mulFactor;
 	else if (cfg.shutdwn_thresh != 0 && !cfg.positive_thresh_ramp)
 		sensor.thresh.coldThrottlingThresholds[(size_t)ThrottlingSeverity::SHUTDOWN - 1] =
-			cfg.shutdwn_thresh;
+			cfg.shutdwn_thresh / (float)sensor.mulFactor;
 
 	if (cfg.vr_thresh != 0)
 		sensor.thresh.vrThrottlingThreshold =
-			cfg.vr_thresh;
+			cfg.vr_thresh / (float)sensor.mulFactor;
 	sens.push_back(sensor);
 	//read_temperature((struct therm_sensor *)sensor);
 
@@ -394,7 +394,7 @@
 {
 	int idx = 0;
 	ThrottlingSeverity severity = ThrottlingSeverity::NONE;
-	float temp = sensor->t.value * sensor->mulFactor;
+	float temp = sensor->t.value;
 
 	for (idx = (int)ThrottlingSeverity::SHUTDOWN - 1; idx >= 0; idx--) {
 		if ((sensor->positiveThresh &&
@@ -477,7 +477,8 @@
 			|| idx <= ((int)sensor.t.throttlingStatus) - 1)
 			continue;
 
-		next_trip = sensor.thresh.hotThrottlingThresholds[idx];
+		next_trip = sensor.thresh.hotThrottlingThresholds[idx] *
+				sensor.mulFactor;
 		break;
 	}
 
@@ -490,7 +491,8 @@
 	}
 	if (sensor.t.throttlingStatus != ThrottlingSeverity::NONE) {
 		curr_trip = sensor.thresh.hotThrottlingThresholds[
-				(int)sensor.t.throttlingStatus - 1];
+				(int)sensor.t.throttlingStatus - 1]
+					* sensor.mulFactor;
 		if (!isnan(next_trip))
 			hyst_temp = (next_trip - curr_trip) + DEFAULT_HYSTERESIS;
 		else
diff --git a/thermalUtils.cpp b/thermalUtils.cpp
index 8bff8de..b26d512 100644
--- a/thermalUtils.cpp
+++ b/thermalUtils.cpp
@@ -103,35 +103,46 @@
 	}
 }
 
-int ThermalUtils::readTemperatures(hidl_vec<Temperature_1_0> *temp)
+int ThermalUtils::readTemperatures(hidl_vec<Temperature_1_0>& temp)
 {
 	std::unordered_map<std::string, struct therm_sensor>::iterator it;
 	int ret = 0, idx = 0;
+	std::vector<Temperature_1_0> _temp_v;
 
 	if (!is_sensor_init)
 		return 0;
-	temp->resize(thermalConfig.size());
 	for (it = thermalConfig.begin(); it != thermalConfig.end();
 			it++, idx++) {
 		struct therm_sensor sens = it->second;
+		Temperature_1_0 _temp;
+
+		/* v1 supports only CPU, GPU, Battery and SKIN */
+		if (sens.t.type > TemperatureType::SKIN)
+			continue;
 		ret = cmnInst.read_temperature(&sens);
 		if (ret < 0)
 			return ret;
-		(*temp)[idx].currentValue = sens.t.value;
-		(*temp)[idx].name = sens.t.name;
-		(*temp)[idx].type = (TemperatureType_1_0)sens.t.type;
+		_temp.currentValue = sens.t.value;
+		_temp.name = sens.t.name;
+		_temp.type = (TemperatureType_1_0)sens.t.type;
+		_temp.throttlingThreshold = sens.thresh.hotThrottlingThresholds[
+					(size_t)ThrottlingSeverity::SEVERE - 1];
+		_temp.shutdownThreshold = sens.thresh.hotThrottlingThresholds[
+					(size_t)ThrottlingSeverity::SHUTDOWN - 1];
+		_temp.vrThrottlingThreshold = sens.thresh.vrThrottlingThreshold;
+		_temp_v.push_back(_temp);
 	}
 
-	return temp->size();
+	temp = _temp_v;
+	return temp.size();
 }
 
 int ThermalUtils::readTemperatures(bool filterType, TemperatureType type,
-                                            hidl_vec<Temperature> *temperatures)
+                                            hidl_vec<Temperature>& temp)
 {
-	std::vector<Temperature> local_temp;
 	std::unordered_map<std::string, struct therm_sensor>::iterator it;
 	int ret = 0;
-	Temperature nantemp;
+	std::vector<Temperature> _temp;
 
 	for (it = thermalConfig.begin(); it != thermalConfig.end(); it++) {
 		struct therm_sensor sens = it->second;
@@ -141,75 +152,50 @@
 		ret = cmnInst.read_temperature(&sens);
 		if (ret < 0)
 			return ret;
-		local_temp.push_back(sens.t);
+		_temp.push_back(sens.t);
 	}
-	if (local_temp.empty()) {
-		nantemp.type = type;
-		nantemp.value = UNKNOWN_TEMPERATURE;
-		local_temp.push_back(nantemp);
-	}
-	*temperatures = local_temp;
 
-	return temperatures->size();
+	temp = _temp;
+	return temp.size();
 }
 
 int ThermalUtils::readTemperatureThreshold(bool filterType, TemperatureType type,
-                                            hidl_vec<TemperatureThreshold> *thresh)
+                                            hidl_vec<TemperatureThreshold>& thresh)
 {
-	std::vector<TemperatureThreshold> local_thresh;
 	std::unordered_map<std::string, struct therm_sensor>::iterator it;
-	int idx = 0;
-	TemperatureThreshold nanthresh;
+	std::vector<TemperatureThreshold> _thresh;
 
 	for (it = thermalConfig.begin(); it != thermalConfig.end(); it++) {
 		struct therm_sensor sens = it->second;
 
 		if (filterType && sens.t.type != type)
 			continue;
-		local_thresh.push_back(sens.thresh);
+		_thresh.push_back(sens.thresh);
 	}
-	if (local_thresh.empty()) {
-		nanthresh.type = type;
-		nanthresh.vrThrottlingThreshold = UNKNOWN_TEMPERATURE;
-		for (idx = 0; idx < (size_t)ThrottlingSeverity::SHUTDOWN;
-				idx++) {
-			nanthresh.hotThrottlingThresholds[idx] =
-			nanthresh.coldThrottlingThresholds[idx] =
-				UNKNOWN_TEMPERATURE;
-		}
-		local_thresh.push_back(nanthresh);
-	}
-	*thresh = local_thresh;
 
-	return thresh->size();
+	thresh = _thresh;
+	return thresh.size();
 }
 
 int ThermalUtils::readCdevStates(bool filterType, cdevType type,
-                                            hidl_vec<CoolingDevice> *cdev_out)
+                                            hidl_vec<CoolingDevice>& cdev_out)
 {
-	std::vector<CoolingDevice> local_cdev;
-	std::vector<struct therm_cdev>::iterator it;
 	int ret = 0;
-	CoolingDevice nanCdev;
+	std::vector<CoolingDevice> _cdev;
 
-	for (it = cdevList.begin(); it != cdevList.end(); it++) {
-		struct therm_cdev cdev = *it;
+	for (struct therm_cdev cdev: cdevList) {
 
 		if (filterType && cdev.c.type != type)
 			continue;
 		ret = cmnInst.read_cdev_state(&cdev);
 		if (ret < 0)
 			return ret;
-		local_cdev.push_back(cdev.c);
+		_cdev.push_back(cdev.c);
 	}
-	if (local_cdev.empty()) {
-		nanCdev.type = type;
-		nanCdev.value = UNKNOWN_TEMPERATURE;
-		local_cdev.push_back(nanCdev);
-	}
-	*cdev_out = local_cdev;
 
-	return cdev_out->size();
+	cdev_out = _cdev;
+
+	return cdev_out.size();
 }
 
 int ThermalUtils::fetchCpuUsages(hidl_vec<CpuUsage>& cpu_usages)
diff --git a/thermalUtils.h b/thermalUtils.h
index 7de252b..cbf74c2 100644
--- a/thermalUtils.h
+++ b/thermalUtils.h
@@ -59,13 +59,13 @@
 		{
 			return is_cdev_init;
 		};
-		int readTemperatures(hidl_vec<Temperature_1_0> *temp);
+		int readTemperatures(hidl_vec<Temperature_1_0>& temp);
 		int readTemperatures(bool filterType, TemperatureType type,
-                                            hidl_vec<Temperature> *temperatures);
+                                            hidl_vec<Temperature>& temperatures);
 		int readTemperatureThreshold(bool filterType, TemperatureType type,
-                                            hidl_vec<TemperatureThreshold> *thresh);
+                                            hidl_vec<TemperatureThreshold>& thresh);
 		int readCdevStates(bool filterType, cdevType type,
-                                            hidl_vec<CoolingDevice> *cdev);
+                                            hidl_vec<CoolingDevice>& cdev);
 		int fetchCpuUsages(hidl_vec<CpuUsage>& cpu_usages);
 	private:
 		bool is_sensor_init;