Skip to content

Commit

Permalink
Merge pull request #72 from nemomobile/plu
Browse files Browse the repository at this point in the history
Dont allow ACTDEAD to USER change on low battery
  • Loading branch information
Pekka Lundstrom committed Jun 6, 2014
2 parents b3c0da0 + 16b9719 commit f1b6b49
Showing 1 changed file with 43 additions and 3 deletions.
46 changes: 43 additions & 3 deletions modules/state.c
Expand Up @@ -86,6 +86,14 @@
#define DSME_THERMAL_SHUTDOWN_TIMER 8
#define DSME_BATTERY_EMPTY_SHUTDOWN_TIMER 8

/**
* Minimum battery level % that is needed before we allow
* switch from ACTDEAD to USER
* TODO: don't hard code this but support config value
*/
#define DSME_MINIMUM_BATTERY_TO_USER 3

#define BATTERY_LEVEL_PATH "/run/state/namespaces/Battery/ChargePercentage"

typedef enum {
CHARGER_STATE_UNKNOWN,
Expand Down Expand Up @@ -147,7 +155,7 @@ static void stop_charger_disconnect_timer(void);
static bool rd_mode_enabled(void);

static void runlevel_switch_ind(const DsmeDbusMessage* ind);

static int get_battery_level(void);

static const struct {
int value;
Expand Down Expand Up @@ -283,12 +291,23 @@ static void try_to_change_state(dsme_state_t new_state)
start_delayed_shutdown_timer(SHUTDOWN_TIMER_TIMEOUT);
break;

case DSME_STATE_USER: /* Runlevel 2 */ /* FALL THROUGH */
case DSME_STATE_ACTDEAD: /* Runlevel 5 */
case DSME_STATE_USER: /* Runlevel 5 */ /* FALL THROUGH */
case DSME_STATE_ACTDEAD: /* Runlevel 4 */
if (current_state == DSME_STATE_NOT_SET) {
/* we have just booted up; simply change the state */
change_state(new_state);
} else if (current_state == DSME_STATE_ACTDEAD) {
/* We are in actdead and user state is wanted
* We don't allow that to happen if battery level is too low
*/
if (get_battery_level() < DSME_MINIMUM_BATTERY_TO_USER ) {
dsme_log(LOG_WARNING,
"Battery level %d%% too low for %s state",
get_battery_level(),
state_name(new_state));
break;
}
/* Battery ok, lets do it */
user_switch_done = false;
#ifndef DSME_SUPPORT_DIRECT_USER_ACTDEAD
/* We don't support direct transfer from ACTDEAD to USER
Expand Down Expand Up @@ -1095,6 +1114,27 @@ static void set_initial_state_bits(const char* bootstate)
}
}

static int get_battery_level(void)
{
FILE* f;
bool ok = false;
int batterylevel;


f = fopen(BATTERY_LEVEL_PATH, "r");
if (f) {
if (fscanf(f, "%d", &batterylevel) == 1) {
ok = true;
}
fclose(f);
}
if (!ok) {
dsme_log(LOG_ERR, "state: FAILED to read %s", BATTERY_LEVEL_PATH);
batterylevel = DSME_MINIMUM_BATTERY_TO_USER; /* return fake, don't block state change */
}
return batterylevel;
}

void module_init(module_t* handle)
{
/* Do not connect to D-Bus; it is probably not started yet.
Expand Down

0 comments on commit f1b6b49

Please sign in to comment.