Skip to content

Commit

Permalink
Merge pull request #17 from jctemkin/master
Browse files Browse the repository at this point in the history
[xcpmd] Report aggregate stats for multi-battery systems, and other small improvements
  • Loading branch information
rossphilipson committed Nov 25, 2015
2 parents 239c0e3 + 4a37ebf commit 3e9b2cc
Show file tree
Hide file tree
Showing 10 changed files with 488 additions and 145 deletions.
71 changes: 58 additions & 13 deletions xcpmd/src/acpi-events.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ int get_ac_adapter_status(void) {
fgets(data, sizeof(data), file);
fclose(file);

if (strstr(data, "1"))
return ON_AC;
else
if (strstr(data, "0"))
return ON_BATT;
else
return ON_AC;
}


Expand Down Expand Up @@ -158,18 +158,34 @@ static void handle_battery_status_event(int battery_index) {
static void handle_lid_event(int status) {

int lid_status;
char * lid_status_string;

struct ev_wrapper * e = acpi_event_table[EVENT_LID];

xcpmd_log(LOG_INFO, "Lid change event\n");

//We may have to check the sysfs if the ACPI string didn't tell us the status.
if (status == LID_UNKNOWN)
lid_status = get_lid_status();
else
lid_status = status;

xenstore_write(lid_status == LID_OPEN ? "open" : "closed", XS_LID_STATE_PATH);
switch (lid_status) {
case LID_OPEN:
lid_status_string = "open";
break;
case LID_CLOSED:
lid_status_string = "closed";
break;
case LID_UNKNOWN:
lid_status_string = "unknown";
break;
case NO_LID:
default:
lid_status_string = "no lid";
}

xcpmd_log(LOG_INFO, "Lid change event: %s\n", lid_status_string);

xenstore_write(lid_status == LID_CLOSED ? "closed" : "open", XS_LID_STATE_PATH);
xenstore_write("1", XS_LID_EVENT_PATH);
//notify_com_citrix_xenclient_xcpmd_lid_changed(xcdbus_conn, XCPMD_SERVICE, XCPMD_PATH);

Expand Down Expand Up @@ -225,11 +241,13 @@ static void handle_bcl_event(enum BCL_CMD cmd) {
struct ev_wrapper * e = acpi_event_table[EVENT_BCL];

if (cmd == BCL_UP) {
xcpmd_log(LOG_INFO, "Brightness up button pressed event\n");
xenstore_write("1", XS_BCL_CMD);
xenstore_write("1", XS_BCL_EVENT_PATH);
adjust_brightness(1, 0);
}
else if (cmd == BCL_DOWN) {
xcpmd_log(LOG_INFO, "Brightness down button pressed event\n");
xenstore_write("2", XS_BCL_CMD);
xenstore_write("1", XS_BCL_EVENT_PATH);
adjust_brightness(0, 0);
Expand Down Expand Up @@ -258,16 +276,16 @@ static void handle_ac_adapter_event(uint32_t data) {
switch(data) {
case ACPI_AC_STATUS_OFFLINE:
e->value.b = ON_BATT;
xcpmd_log(LOG_DEBUG, "AC adapter state change event: on battery");
xcpmd_log(LOG_INFO, "AC adapter state change event: on battery");
break;
case ACPI_AC_STATUS_ONLINE:
e->value.b = ON_AC;
xcpmd_log(LOG_DEBUG, "AC adapter state change event: on AC");
xcpmd_log(LOG_INFO, "AC adapter state change event: on AC");
break;
case ACPI_AC_STATUS_UNKNOWN:
default:
e->value.b = AC_UNKNOWN;
xcpmd_log(LOG_DEBUG, "AC adapter state change event: unknown state");
xcpmd_log(LOG_INFO, "AC adapter state change event: unknown state");
}

handle_events(e);
Expand Down Expand Up @@ -297,15 +315,42 @@ void acpi_initialize_state(void) {
int ac_adapter_status = get_ac_adapter_status();
int lid_status = get_lid_status();
int tablet_status = get_tablet_status();

xcpmd_log(LOG_DEBUG, "Lid is %s and system is on %s\n", lid_status == LID_OPEN ? "open" : "closed", ac_adapter_status == ON_AC ? "ac" : "battery");
char * acpi_status_string = NULL;

acpi_event_table[EVENT_ON_AC]->value.i = ac_adapter_status;
acpi_event_table[EVENT_LID]->value.i = lid_status;
acpi_event_table[EVENT_TABLET_MODE]->value.i = tablet_status;

xenstore_write_int((ac_adapter_status == ON_AC) ? 1 : 0, XS_AC_ADAPTER_STATE_PATH);
xenstore_write_int((lid_status == LID_CLOSED) ? 0 : 1, XS_LID_STATE_PATH);
switch (ac_adapter_status) {
case ON_AC:
xenstore_write_int(1, XS_AC_ADAPTER_STATE_PATH);
safe_str_append(&acpi_status_string, "System is on AC");
break;
case ON_BATT:
case AC_UNKNOWN:
xenstore_write_int(0, XS_AC_ADAPTER_STATE_PATH);
safe_str_append(&acpi_status_string, "System is on battery");
break;
case NO_AC:
xenstore_rm(XS_AC_ADAPTER_STATE_PATH);
safe_str_append(&acpi_status_string, "System has no removable AC adapter");
}

switch (lid_status) {
case LID_CLOSED:
xenstore_write_int(0, XS_LID_STATE_PATH);
safe_str_append(&acpi_status_string, " and the lid is closed.");
break;
case LID_OPEN:
case LID_UNKNOWN:
xenstore_write_int(1, XS_LID_STATE_PATH);
safe_str_append(&acpi_status_string, " and the lid is open.");
break;
case NO_LID:
xenstore_rm(XS_LID_STATE_PATH);
safe_str_append(&acpi_status_string, " and no lid.");
}
xcpmd_log(LOG_INFO, "%s\n", acpi_status_string);

update_batteries();
}
Expand Down
87 changes: 57 additions & 30 deletions xcpmd/src/acpi-module.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,24 @@
*/

//Function prototypes
bool bcl_up_pressed (struct ev_wrapper * event, struct arg_node * args);
bool bcl_down_pressed (struct ev_wrapper * event, struct arg_node * args);
bool pbtn_pressed (struct ev_wrapper * event, struct arg_node * args);
bool sbtn_pressed (struct ev_wrapper * event, struct arg_node * args);
bool susp_pressed (struct ev_wrapper * event, struct arg_node * args);
bool lid_closed (struct ev_wrapper * event, struct arg_node * args);
bool lid_open (struct ev_wrapper * event, struct arg_node * args);
bool on_ac (struct ev_wrapper * event, struct arg_node * args);
bool on_battery (struct ev_wrapper * event, struct arg_node * args);
bool tablet_mode (struct ev_wrapper * event, struct arg_node * args);
bool non_tablet_mode (struct ev_wrapper * event, struct arg_node * args);
bool battery_greater_than (struct ev_wrapper * event, struct arg_node * args);
bool battery_less_than (struct ev_wrapper * event, struct arg_node * args);
bool battery_equal_to (struct ev_wrapper * event, struct arg_node * args);
bool battery_present (struct ev_wrapper * event, struct arg_node * args);
bool bcl_up_pressed (struct ev_wrapper * event, struct arg_node * args);
bool bcl_down_pressed (struct ev_wrapper * event, struct arg_node * args);
bool pbtn_pressed (struct ev_wrapper * event, struct arg_node * args);
bool sbtn_pressed (struct ev_wrapper * event, struct arg_node * args);
bool susp_pressed (struct ev_wrapper * event, struct arg_node * args);
bool lid_closed (struct ev_wrapper * event, struct arg_node * args);
bool lid_open (struct ev_wrapper * event, struct arg_node * args);
bool on_ac (struct ev_wrapper * event, struct arg_node * args);
bool on_battery (struct ev_wrapper * event, struct arg_node * args);
bool tablet_mode (struct ev_wrapper * event, struct arg_node * args);
bool non_tablet_mode (struct ev_wrapper * event, struct arg_node * args);
bool battery_greater_than (struct ev_wrapper * event, struct arg_node * args);
bool battery_less_than (struct ev_wrapper * event, struct arg_node * args);
bool battery_equal_to (struct ev_wrapper * event, struct arg_node * args);
bool battery_present (struct ev_wrapper * event, struct arg_node * args);
bool overall_battery_greater_than (struct ev_wrapper * event, struct arg_node * args);
bool overall_battery_less_than (struct ev_wrapper * event, struct arg_node * args);
bool overall_battery_equal_to (struct ev_wrapper * event, struct arg_node * args);


//Private data structures
Expand Down Expand Up @@ -86,21 +89,24 @@ static struct event_data_row event_data[] = {


static struct cond_table_row condition_data[] = {
{"onBacklightDownBtn" , bcl_up_pressed , "n" , "void" , EVENT_BCL } ,
{"onBacklightUpBtn" , bcl_down_pressed , "n" , "void" , EVENT_BCL } ,
{"onPowerBtn" , pbtn_pressed , "n" , "void" , EVENT_PWR_BTN } ,
{"onSleepBtn" , sbtn_pressed , "n" , "void" , EVENT_SLP_BTN } ,
{"onSuspendBtn" , susp_pressed , "n" , "void" , EVENT_SUSP_BTN } ,
{"whileLidClosed" , lid_closed , "n" , "void" , EVENT_LID } ,
{"whileLidOpen" , lid_open , "n" , "void" , EVENT_LID } ,
{"whileUsingAc" , on_ac , "n" , "void" , EVENT_ON_AC } ,
{"whileUsingBatt" , on_battery , "n" , "void" , EVENT_ON_AC } ,
{"whileInTabletMode" , tablet_mode , "n" , "void" , EVENT_TABLET_MODE } ,
{"whileNotInTabletMode" , non_tablet_mode , "n" , "void" , EVENT_TABLET_MODE } ,
{"whileBattGreaterThan" , battery_greater_than , "i, i" , "int battNum, int percentage" , EVENT_BATT_STATUS } ,
{"whileBattLessThan" , battery_less_than , "i, i" , "int battNum, int percentage" , EVENT_BATT_STATUS } ,
{"whileBattEqualTo" , battery_equal_to , "i, i" , "int battNum, int percentage" , EVENT_BATT_STATUS } ,
{"whileBattPresent" , battery_present , "i" , "int battNum" , EVENT_BATT_INFO }
{"onBacklightDownBtn" , bcl_up_pressed , "n" , "void" , EVENT_BCL } ,
{"onBacklightUpBtn" , bcl_down_pressed , "n" , "void" , EVENT_BCL } ,
{"onPowerBtn" , pbtn_pressed , "n" , "void" , EVENT_PWR_BTN } ,
{"onSleepBtn" , sbtn_pressed , "n" , "void" , EVENT_SLP_BTN } ,
{"onSuspendBtn" , susp_pressed , "n" , "void" , EVENT_SUSP_BTN } ,
{"whileLidClosed" , lid_closed , "n" , "void" , EVENT_LID } ,
{"whileLidOpen" , lid_open , "n" , "void" , EVENT_LID } ,
{"whileUsingAc" , on_ac , "n" , "void" , EVENT_ON_AC } ,
{"whileUsingBatt" , on_battery , "n" , "void" , EVENT_ON_AC } ,
{"whileInTabletMode" , tablet_mode , "n" , "void" , EVENT_TABLET_MODE } ,
{"whileNotInTabletMode" , non_tablet_mode , "n" , "void" , EVENT_TABLET_MODE } ,
{"whileBattGreaterThan" , battery_greater_than , "i, i" , "int battNum, int percentage" , EVENT_BATT_STATUS } ,
{"whileBattLessThan" , battery_less_than , "i, i" , "int battNum, int percentage" , EVENT_BATT_STATUS } ,
{"whileBattEqualTo" , battery_equal_to , "i, i" , "int battNum, int percentage" , EVENT_BATT_STATUS } ,
{"whileBattPresent" , battery_present , "i" , "int battNum" , EVENT_BATT_INFO } ,
{"whileOverallBattGreaterThan" , overall_battery_greater_than , "i" , "int percentage" , EVENT_BATT_STATUS } ,
{"whileOverallBattLessThan" , overall_battery_less_than , "i" , "int percentage" , EVENT_BATT_STATUS } ,
{"whileOverallBattEqualTo" , overall_battery_equal_to , "i" , "int percentage" , EVENT_BATT_STATUS }
};

static unsigned int num_conditions = sizeof(condition_data) / sizeof(condition_data[0]);
Expand Down Expand Up @@ -255,3 +261,24 @@ bool battery_equal_to(struct ev_wrapper * event, struct arg_node * args) {
int percentage = get_arg(args, 0)->arg.i;
return get_battery_percentage(event->value.i) == percentage;
}


bool overall_battery_greater_than(struct ev_wrapper * event, struct arg_node * args) {

int percentage = get_arg(args, 0)->arg.i;
return get_overall_battery_percentage() > percentage;
}


bool overall_battery_less_than(struct ev_wrapper * event, struct arg_node * args) {

int percentage = get_arg(args, 0)->arg.i;
return get_overall_battery_percentage() < percentage;
}


bool overall_battery_equal_to(struct ev_wrapper * event, struct arg_node * args) {

int percentage = get_arg(args, 0)->arg.i;
return get_overall_battery_percentage() == percentage;
}
Loading

0 comments on commit 3e9b2cc

Please sign in to comment.