thermal-hal: Add exception handling for stoi

stoi can throw exception when an invalid value is passed in buffer.
Enable exception handling and add retry mechanism when a exception is
thrown.

Change-Id: I2c1e0ddbbb408cb6b9739b516296125ec1efd64a
diff --git a/Android.bp b/Android.bp
index 0100465..8acb48b 100644
--- a/Android.bp
+++ b/Android.bp
@@ -32,6 +32,7 @@
 	cflags: [
 		"-Wno-unused-parameter",
 		"-Wno-unused-variable",
+		"-fexceptions",
 	],
 }
 
@@ -71,5 +72,6 @@
 		"-Wno-unused-parameter",
 		"-Wno-unused-variable",
 		"-DENABLE_THERMAL_NETLINK",
+		"-fexceptions",
 	],
 }
diff --git a/thermalCommon.cpp b/thermalCommon.cpp
index 2bcf929..f5a6a8c 100644
--- a/thermalCommon.cpp
+++ b/thermalCommon.cpp
@@ -372,18 +372,30 @@
 {
 	char file_name[MAX_PATH];
 	std::string buf;
-	int ret = 0;
+	int ret = 0, ct = 0;
+	bool read_ok = false;
 
-	LOG(DEBUG) << "Entering " <<__func__;
 	snprintf(file_name, sizeof(file_name), CDEV_CUR_STATE_PATH,
 			cdev.cdevn);
-	ret = readLineFromFile(std::string(file_name), buf);
-	if (ret <= 0) {
-		LOG(ERROR) << "Cdev state read error:"<< ret <<
-			" for cdev: " << cdev.c.name;
-		return -1;
-	}
-	cdev.c.value = std::stoi(buf, nullptr, 0);
+	do {
+		ret = readLineFromFile(std::string(file_name), buf);
+		if (ret <= 0) {
+			LOG(ERROR) << "Cdev state read error:"<< ret <<
+				" for cdev: " << cdev.c.name;
+			return -1;
+		}
+		try {
+			cdev.c.value = std::stoi(buf, nullptr, 0);
+			read_ok = true;
+		}
+		catch (std::exception &err) {
+			LOG(ERROR) << "Cdev read stoi error:" << err.what()
+				<< " cdev:" << cdev.c.name << " ID:"
+				<< cdev.cdevn << " buf:" << buf <<
+				std::endl;
+		}
+		ct++;
+	} while (!read_ok && ct < RETRY_CT);
 	LOG(DEBUG) << "cdev Name:" << cdev.c.name << ". state:" <<
 		cdev.c.value << std::endl;
 
@@ -425,7 +437,8 @@
 	}
 	if (idx >= 0)
 		severity = (ThrottlingSeverity)(idx);
-	LOG(DEBUG) << "Sensor Name:" << sensor.t.name << ". prev severity:" <<
+	LOG(INFO) << "Sensor Name:" << sensor.t.name << "temp: " <<
+		temp << ". prev severity:" <<
 		(int)sensor.lastThrottleStatus << ". cur severity:" <<
 		(int)sensor.t.throttlingStatus << " New severity:" <<
 		(int)severity << std::endl;
@@ -442,18 +455,32 @@
 	char file_name[MAX_PATH];
 	float temp;
 	std::string buf;
-	int ret = 0;
+	int ret = 0, ct = 0;
+	bool read_ok = false;
 
-	LOG(DEBUG) << "Entering " <<__func__;
-	snprintf(file_name, sizeof(file_name), TEMPERATURE_FILE_FORMAT,
+	do {
+		snprintf(file_name, sizeof(file_name), TEMPERATURE_FILE_FORMAT,
 			sensor.tzn);
-	ret = readLineFromFile(std::string(file_name), buf);
-	if (ret <= 0) {
-		LOG(ERROR) << "Temperature read error:"<< ret <<
-			" for sensor " << sensor.t.name;
-		return -1;
-	}
-	sensor.t.value = (float)std::stoi(buf, nullptr, 0) / (float)sensor.mulFactor;
+		ret = readLineFromFile(std::string(file_name), buf);
+		if (ret <= 0) {
+			LOG(ERROR) << "Temperature read error:"<< ret <<
+				" for sensor " << sensor.t.name;
+			return -1;
+		}
+		try {
+			sensor.t.value = (float)std::stoi(buf, nullptr, 0) /
+				 (float)sensor.mulFactor;
+			read_ok = true;
+		}
+		catch (std::exception &err) {
+			LOG(ERROR) << "Temperature buf stoi error: "
+				<< err.what()
+				<< " buf:" << buf << " sensor:"
+				<< sensor.t.name << " TZ:" <<
+				sensor.tzn << std::endl;
+		}
+		ct++;
+	} while (!read_ok && ct < RETRY_CT);
 	LOG(DEBUG) << "Sensor Name:" << sensor.t.name << ". Temperature:" <<
 		(float)sensor.t.value << std::endl;
 
diff --git a/thermalCommon.h b/thermalCommon.h
index 6a1b348..e61bb2a 100644
--- a/thermalCommon.h
+++ b/thermalCommon.h
@@ -39,6 +39,8 @@
 namespace V2_0 {
 namespace implementation {
 
+#define RETRY_CT 3
+
 class ThermalCommon {
 	public:
 		ThermalCommon();
diff --git a/thermalConfig.cpp b/thermalConfig.cpp
index c7086a5..ed65346 100644
--- a/thermalConfig.cpp
+++ b/thermalConfig.cpp
@@ -867,12 +867,24 @@
 		std::vector<struct target_therm_cfg>::iterator it_vec;
 		bool bcl_defined = false;
 		std::string soc_val;
+		int ct = 0;
+		bool read_ok = false;
 
-		if (cmnInst.readFromFile(socIDPath, soc_val) <= 0) {
-			LOG(ERROR) <<"soc ID fetch error";
-			return;
-		}
-		soc_id = std::stoi(soc_val, nullptr, 0);
+		do {
+			if (cmnInst.readFromFile(socIDPath, soc_val) <= 0) {
+				LOG(ERROR) <<"soc ID fetch error";
+				return;
+			}
+			try {
+				soc_id = std::stoi(soc_val, nullptr, 0);
+				read_ok = true;
+			}
+			catch (std::exception &err) {
+				LOG(ERROR) <<"soc id stoi err:" << err.what()
+					<< " buf:" << soc_val;
+			}
+			ct++;
+		} while (!read_ok && ct < RETRY_CT);
 		if (soc_id <= 0) {
 			LOG(ERROR) << "Invalid soc ID: " << soc_id;
 			return;