diff mbox

[v1,10/15] android-console: Add GF battery property getter

Message ID 1415751963-4081-11-git-send-email-greg.bellows@linaro.org
State New
Headers show

Commit Message

Greg Bellows Nov. 12, 2014, 12:25 a.m. UTC
Add a function for acquiring each of the goldfish battery properties.

Signed-off-by: Greg Bellows <greg.bellows@linaro.org>
---
 android-commands.h                 |  7 +++++++
 android-console.c                  | 20 ++++++++++++++++++
 android-console.h                  |  1 +
 hw/misc/goldfish_battery.c         | 42 ++++++++++++++++++++++++++++++++++++++
 include/hw/misc/goldfish_battery.h |  1 +
 5 files changed, 71 insertions(+)
diff mbox

Patch

diff --git a/android-commands.h b/android-commands.h
index 29c8769..8bf856d 100644
--- a/android-commands.h
+++ b/android-commands.h
@@ -26,6 +26,13 @@  static mon_cmd_t android_redir_cmds[] = {
 };
 
 static mon_cmd_t android_power_cmds[] = {
+    {
+        .name = "display",
+        .args_type = "",
+        .params = "",
+        .help = "display battery and charger state",
+        .mhandler.cmd = android_console_power_display,
+    },
     { NULL, NULL, },
 };
 
diff --git a/android-console.c b/android-console.c
index c08c607..faaeeaf 100644
--- a/android-console.c
+++ b/android-console.c
@@ -21,6 +21,7 @@ 
 #include "net/slirp.h"
 #include "slirp/libslirp.h"
 #include "qmp-commands.h"
+#include "hw/misc/goldfish_battery.h"
 
 typedef struct {
     int is_udp;
@@ -298,19 +299,38 @@  void android_console_redir(Monitor *mon, const QDict *qdict)
     monitor_printf(mon, "%s\n", redir_help[cmd]);
 }
 
+void android_console_power_display(Monitor *mon, const QDict *qdict)
+{
+    goldfish_battery_display(mon);
+
+    monitor_printf(mon, "OK\n");
+}
+
 enum {
     CMD_POWER,
+    CMD_POWER_DISPLAY,
 };
 
 static const char *power_help[] = {
         /* CMD_POWER */
         "allows to change battery and AC power status",
+        /* CMD_POWER_DISPLAY */
+        "display battery and charger state",
 };
 
 void android_console_power(Monitor *mon, const QDict *qdict)
 {
+    /* This only gets called for bad subcommands and help requests */
+    const char *helptext = qdict_get_try_str(qdict, "helptext");
+
     /* Default to the first entry which is the parent help message */
     int cmd = CMD_POWER;
 
+    if (helptext) {
+        if (strstr(helptext, "display")) {
+            cmd = CMD_POWER_DISPLAY;
+        }
+    }
+
     monitor_printf(mon, "%s\n", power_help[cmd]);
 }
diff --git a/android-console.h b/android-console.h
index ba936be..44ff11b 100644
--- a/android-console.h
+++ b/android-console.h
@@ -28,6 +28,7 @@  void android_console_redir_list(Monitor *mon, const QDict *qdict);
 void android_console_redir_add(Monitor *mon, const QDict *qdict);
 void android_console_redir_del(Monitor *mon, const QDict *qdict);
 
+void android_console_power_display(Monitor *mon, const QDict *qdict);
 void android_console_power(Monitor *mon, const QDict *qdict);
 
 void android_monitor_print_error(Monitor *mon, const char *fmt, ...);
diff --git a/hw/misc/goldfish_battery.c b/hw/misc/goldfish_battery.c
index c7c9d98..557bf7a 100644
--- a/hw/misc/goldfish_battery.c
+++ b/hw/misc/goldfish_battery.c
@@ -128,6 +128,48 @@  void goldfish_battery_display(Monitor *mon)
     monitor_printf(mon, "capacity: %d\n", s->capacity);
 }
 
+void goldfish_battery_set_prop(int ac, int property, int value)
+{
+    DeviceState *dev = qdev_find_recursive(sysbus_get_default(),
+                                           TYPE_GOLDFISH_BATTERY);
+    struct goldfish_battery_state *battery_state = GOLDFISH_BATTERY(dev);
+    int new_status = (ac ? AC_STATUS_CHANGED : BATTERY_STATUS_CHANGED);
+
+    if (!battery_state || !battery_state->hw_has_battery) {
+        return;
+    }
+
+    if (ac) {
+        switch (property) {
+        case POWER_SUPPLY_PROP_ONLINE:
+            battery_state->ac_online = value;
+            break;
+        }
+    } else {
+        switch (property) {
+        case POWER_SUPPLY_PROP_STATUS:
+            battery_state->status = value;
+            break;
+        case POWER_SUPPLY_PROP_HEALTH:
+            battery_state->health = value;
+            break;
+        case POWER_SUPPLY_PROP_PRESENT:
+            battery_state->present = value;
+            break;
+        case POWER_SUPPLY_PROP_CAPACITY:
+            battery_state->capacity = value;
+            break;
+        }
+    }
+
+    if (new_status != battery_state->int_status) {
+        battery_state->int_status |= new_status;
+        qemu_set_irq(battery_state->irq,
+                     (battery_state->int_status &
+                     battery_state->int_enable));
+    }
+}
+
 static uint64_t goldfish_battery_read(void *opaque, hwaddr offset, unsigned size)
 {
     uint64_t ret;
diff --git a/include/hw/misc/goldfish_battery.h b/include/hw/misc/goldfish_battery.h
index f497538..2d02a38 100644
--- a/include/hw/misc/goldfish_battery.h
+++ b/include/hw/misc/goldfish_battery.h
@@ -69,5 +69,6 @@  enum power_supply_property {
 };
 
 extern void goldfish_battery_display(Monitor *mon);
+void goldfish_battery_set_prop(int ac, int property, int value);
 
 #endif /* _HW_GOLDFISH_BATTERY_H */