Merge f61c6149d2bada4ff0873650b0c9e6775e3dc2c2 on remote branch

Change-Id: Id6fcea037e9e4077912d165298b8a7e3842c3749
diff --git a/thermal.cpp b/thermal.cpp
index c525e8b..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.");
 
@@ -109,7 +109,7 @@
 	if (!utils.isSensorInitialized())
 		return exit_hal(_hidl_cb, cpu_usages,
 			"ThermalHAL not initialized properly.");
-	if (utils.fetchCpuUsages(&cpu_usages) <= 0)
+	if (utils.fetchCpuUsages(cpu_usages) <= 0)
 		return exit_hal(_hidl_cb, cpu_usages,
 				"CPU usage 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");
 	}
@@ -264,9 +263,17 @@
 	LOG(DEBUG) << "Throttle Severity change: " << " Type: " << (int)t.type
 		<< " Name: " << t.name << " Value: " << t.value <<
 		" ThrottlingStatus: " << (int)t.throttlingStatus;
-	for (it = cb.begin(); it != cb.end(); it++) {
-		if (!it->is_filter_type || it->type == t.type)
-			it->callback->notifyThrottling(t);
+	it = cb.begin();
+	while (it != cb.end()) {
+		if (!it->is_filter_type || it->type == t.type) {
+			Return<void> ret = it->callback->notifyThrottling(t);
+			if (!ret.isOk()) {
+				LOG(ERROR) << "Notify callback execution error. Removing";
+				it = cb.erase(it);
+				continue;
+			}
+		}
+		it++;
 	}
 }
 
diff --git a/thermalCommon.cpp b/thermalCommon.cpp
index 642573f..6fb0d03 100644
--- a/thermalCommon.cpp
+++ b/thermalCommon.cpp
@@ -241,29 +241,29 @@
 	sensor.thresh.type = sensor.t.type = cfg.type;
 	sensor.thresh.vrThrottlingThreshold =
 	UNKNOWN_TEMPERATURE;
-	for (idx = 0; idx < (size_t)ThrottlingSeverity::SHUTDOWN; idx++) {
+	for (idx = 0; idx <= (size_t)ThrottlingSeverity::SHUTDOWN; idx++) {
 		sensor.thresh.hotThrottlingThresholds[idx] =
 		sensor.thresh.coldThrottlingThresholds[idx] =
 			UNKNOWN_TEMPERATURE;
 	}
 
 	if (cfg.throt_thresh != 0 && cfg.positive_thresh_ramp)
-		sensor.thresh.hotThrottlingThresholds[(size_t)ThrottlingSeverity::SEVERE - 1] =
-			cfg.throt_thresh;
+		sensor.thresh.hotThrottlingThresholds[(size_t)ThrottlingSeverity::SEVERE] =
+			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;
+		sensor.thresh.coldThrottlingThresholds[(size_t)ThrottlingSeverity::SEVERE] =
+			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;
+		sensor.thresh.hotThrottlingThresholds[(size_t)ThrottlingSeverity::SHUTDOWN] =
+			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;
+		sensor.thresh.coldThrottlingThresholds[(size_t)ThrottlingSeverity::SHUTDOWN] =
+			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,9 +394,9 @@
 {
 	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--) {
+	for (idx = (int)ThrottlingSeverity::SHUTDOWN; idx >= 0; idx--) {
 		if ((sensor->positiveThresh &&
 			!isnan(sensor->thresh.hotThrottlingThresholds[idx]) &&
 			temp >=
@@ -408,7 +408,7 @@
 			break;
 	}
 	if (idx >= 0)
-		severity = (ThrottlingSeverity)(idx + 1);
+		severity = (ThrottlingSeverity)(idx);
 	LOG(DEBUG) << "Sensor Name:" << sensor->t.name << ". old severity:" <<
 		(int)sensor->t.throttlingStatus << " New severity:" <<
 		(int)severity << std::endl;
@@ -472,12 +472,13 @@
 	}
 
 	next_trip = UNKNOWN_TEMPERATURE;
-	for (idx = 0;idx < (int)ThrottlingSeverity::SHUTDOWN; idx++) {
+	for (idx = 0;idx <= (int)ThrottlingSeverity::SHUTDOWN; idx++) {
 		if (isnan(sensor.thresh.hotThrottlingThresholds[idx])
-			|| idx <= ((int)sensor.t.throttlingStatus) - 1)
+			|| idx <= (int)sensor.t.throttlingStatus)
 			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]
+					* sensor.mulFactor;
 		if (!isnan(next_trip))
 			hyst_temp = (next_trip - curr_trip) + DEFAULT_HYSTERESIS;
 		else
@@ -505,7 +507,7 @@
 	return;
 }
 
-int ThermalCommon::get_cpu_usages(hidl_vec<CpuUsage> *list) {
+int ThermalCommon::get_cpu_usages(hidl_vec<CpuUsage>& list) {
 	int vals, cpu_num, online;
 	ssize_t read;
 	uint64_t user, nice, system, idle, active, total;
@@ -516,6 +518,7 @@
 	FILE *file;
 	FILE *cpu_file;
 
+	list.resize(ncpus);
 	file = fopen(CPU_USAGE_FILE, "r");
 	if (file == NULL) {
 		LOG(ERROR) << "failed to open:" << CPU_USAGE_FILE <<
@@ -574,10 +577,10 @@
 		}
 		fclose(cpu_file);
 
-		(*list)[cpu_num].name = std::string("CPU") + std::to_string(cpu_num);
-		(*list)[cpu_num].active = active;
-		(*list)[cpu_num].total = total;
-		(*list)[cpu_num].isOnline = online;
+		list[cpu_num].name = std::string("CPU") + std::to_string(cpu_num);
+		list[cpu_num].active = active;
+		list[cpu_num].total = total;
+		list[cpu_num].isOnline = online;
 		cpu++;
 	}
 	fclose(file);
diff --git a/thermalCommon.h b/thermalCommon.h
index b3c557c..4375e1e 100644
--- a/thermalCommon.h
+++ b/thermalCommon.h
@@ -52,7 +52,7 @@
 		int read_cdev_state(struct therm_cdev *cdev);
 		int read_temperature(struct therm_sensor *sensor);
 		int estimateSeverity(struct therm_sensor *sensor);
-		int get_cpu_usages(hidl_vec<CpuUsage> *list);
+		int get_cpu_usages(hidl_vec<CpuUsage>& list);
 
 		std::vector<struct therm_sensor> fetch_sensor_list()
 		{
diff --git a/thermalConfig.cpp b/thermalConfig.cpp
index 7525022..c0510d1 100644
--- a/thermalConfig.cpp
+++ b/thermalConfig.cpp
@@ -196,7 +196,7 @@
 			{ "npu-usr" },
 			"npu",
 			95000,
-			105000,
+			115000,
 			95000,
 			true,
 		},
@@ -217,7 +217,7 @@
 			{ "nspss-0-usr" },
 			"nsp0",
 			95000,
-			105000,
+			115000,
 			95000,
 			true,
 		},
@@ -226,7 +226,7 @@
 			{ "nspss-1-usr" },
 			"nsp1",
 			95000,
-			105000,
+			115000,
 			95000,
 			true,
 		},
@@ -235,7 +235,7 @@
 			{ "nspss-2-usr" },
 			"nsp2",
 			95000,
-			105000,
+			115000,
 			95000,
 			true,
 		},
diff --git a/thermalUtils.cpp b/thermalUtils.cpp
index 517b979..7aaada4 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];
+		_temp.shutdownThreshold = sens.thresh.hotThrottlingThresholds[
+					(size_t)ThrottlingSeverity::SHUTDOWN];
+		_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,78 +152,53 @@
 		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)
+int ThermalUtils::fetchCpuUsages(hidl_vec<CpuUsage>& cpu_usages)
 {
 	return cmnInst.get_cpu_usages(cpu_usages);
 }
diff --git a/thermalUtils.h b/thermalUtils.h
index 8cf4a41..cbf74c2 100644
--- a/thermalUtils.h
+++ b/thermalUtils.h
@@ -59,14 +59,14 @@
 		{
 			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);
-		int fetchCpuUsages(hidl_vec<CpuUsage> *cpu_usages);
+                                            hidl_vec<CoolingDevice>& cdev);
+		int fetchCpuUsages(hidl_vec<CpuUsage>& cpu_usages);
 	private:
 		bool is_sensor_init;
 		bool is_cdev_init;