summaryrefslogtreecommitdiff
path: root/thermalCommon.cpp
diff options
context:
space:
mode:
authorRam Chandrasekar <rkumbako@codeaurora.org>2021-07-29 15:41:00 -0700
committerRam Chandrasekar <rkumbako@codeaurora.org>2021-07-29 16:17:11 -0700
commit9e1b3262bb55a7cc2acc0b31621a95c03dcac6e2 (patch)
tree7fb501e08a37cfe56baca9e4e65777afe7883f21 /thermalCommon.cpp
parent372b93479a49d5b24443a28a1a8ad12dbe6a1749 (diff)
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
Diffstat (limited to 'thermalCommon.cpp')
-rw-r--r--thermalCommon.cpp67
1 files changed, 47 insertions, 20 deletions
diff --git a/thermalCommon.cpp b/thermalCommon.cpp
index 2bcf929..f5a6a8c 100644
--- a/thermalCommon.cpp
+++ b/thermalCommon.cpp
@@ -372,18 +372,30 @@ int ThermalCommon::read_cdev_state(struct therm_cdev& cdev)
{
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 @@ int ThermalCommon::estimateSeverity(struct therm_sensor& sensor)
}
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 @@ int ThermalCommon::read_temperature(struct therm_sensor& sensor)
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;