Skip to content

Commit

Permalink
Merge pull request #75 from nemomobile/plu
Browse files Browse the repository at this point in the history
[thermalsensor] Don't report wrongly suspicious temp readings.
  • Loading branch information
Pekka Lundstrom committed Jul 3, 2014
2 parents 582d12b + 5c4b398 commit 0eb6bc7
Showing 1 changed file with 40 additions and 10 deletions.
50 changes: 40 additions & 10 deletions modules/thermalsensor_hw.c
Expand Up @@ -30,12 +30,15 @@
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/* TODO: These should be changed to use statefs once we have it */
#define CORE_SENSOR_MODE "/sys/devices/virtual/thermal/thermal_zone9/mode"
#define CORE_SENSOR_TEMP "/sys/devices/virtual/thermal/thermal_zone9/temp"
#define BATTERY_TEMP "/sys/class/power_supply/battery/temp"

static int get_current_time(void);

static bool read_temperature(const char *path, int *temperature)
{
FILE* f;
Expand Down Expand Up @@ -130,28 +133,43 @@ extern bool dsme_hw_get_battery_temperature(thermal_object_t* thermal_ob
int temperature;
bool ret;
static int previous_temp = -999;
static int previous_time = 0;
int current_time = 0;

// dsme_log(LOG_DEBUG, "thermal: %s", __FUNCTION__);

if (read_temperature(BATTERY_TEMP, &temperature)) {
/* We get the temp in one tens */
temperature = (temperature + 5 ) / 10;
temperature = (temperature > 0) ? (temperature + 5 ) / 10 : (temperature - 5 ) / 10;
ret = true;
/* Log reading time */
current_time = get_current_time();
} else {
dsme_log(LOG_WARNING, "thermal: Can't get battery temperature readings");
temperature = INVALID_TEMPERATURE;
ret = false;
}

/* Log unsual temp readings */
if ((previous_temp != -999) &&
((temperature < -30) ||
(temperature > 70) ||
(temperature > (previous_temp + 4)) ||
(temperature < (previous_temp - 4)))) {
dsme_log(LOG_WARNING, "thermal: Suspicious battery temperature %d (previous %d)",temperature, previous_temp );
}
previous_temp = temperature;
if (ret) {
/* Log unsual low or high temp readings */
if ((temperature < -30) ||
(temperature > 70)) {
dsme_log(LOG_WARNING, "thermal: Suspicious battery temperature level %d",temperature);
}
if ((previous_temp != -999) &&
((temperature > (previous_temp + 4)) ||
(temperature < (previous_temp - 4)))) {
/* Battery temp has changed a lot between two readings
* Log it as warning if this change happened in short time (~ 2 minutes)
*/
int delta_time = (current_time > previous_time) ? current_time - previous_time : 0;
if (delta_time <= 140 ) {
dsme_log(LOG_WARNING, "thermal: Suspicious battery temperature change from %d to %d in %ds",previous_temp, temperature, delta_time);
}
}
previous_temp = temperature;
previous_time = current_time;
}

if (callback)
callback(thermal_object, temperature);
Expand All @@ -166,3 +184,15 @@ extern bool dsme_hw_supports_battery_temp(void)
return true;
return false;
}

static int get_current_time(void)
{

struct timespec t;
int now = 0;

if (clock_gettime(CLOCK_BOOTTIME, &t) == 0) {
now = t.tv_sec;
}
return now;
}

0 comments on commit 0eb6bc7

Please sign in to comment.