healthd: move charger executable to healthd charger mode

* Add healthd charger mode ops
* Check for executable invocation as "charger", set charger mode if so
* Incorporate charger executable as healthd charger mode

Change-Id: I4a44e7a4c3a65ae9be94491f7f498aa48d4f8a84
diff --git a/healthd/healthd.cpp b/healthd/healthd.cpp
index 9cb94db..66b961d 100644
--- a/healthd/healthd.cpp
+++ b/healthd/healthd.cpp
@@ -21,8 +21,10 @@
 #include "BatteryMonitor.h"
 
 #include <errno.h>
+#include <libgen.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
 #include <batteryservice/BatteryService.h>
 #include <cutils/klog.h>
@@ -79,6 +81,14 @@
 extern void healthd_mode_android_battery_update(
     struct android::BatteryProperties *props);
 
+// Charger mode
+
+extern void healthd_mode_charger_init(struct healthd_config *config);
+extern int healthd_mode_charger_preparetowait(void);
+extern void healthd_mode_charger_heartbeat(void);
+extern void healthd_mode_charger_battery_update(
+    struct android::BatteryProperties *props);
+
 // NOPs for modes that need no special action
 
 static void healthd_mode_nop_init(struct healthd_config *config);
@@ -94,6 +104,13 @@
     .battery_update = healthd_mode_android_battery_update,
 };
 
+static struct healthd_mode_ops charger_ops = {
+    .init = healthd_mode_charger_init,
+    .preparetowait = healthd_mode_charger_preparetowait,
+    .heartbeat = healthd_mode_charger_heartbeat,
+    .battery_update = healthd_mode_charger_battery_update,
+};
+
 static struct healthd_mode_ops recovery_ops = {
     .init = healthd_mode_nop_init,
     .preparetowait = healthd_mode_nop_preparetowait,
@@ -307,15 +324,22 @@
     klog_set_level(KLOG_LEVEL);
     healthd_mode_ops = &android_ops;
 
-    while ((ch = getopt(argc, argv, "r")) != -1) {
-        switch (ch) {
-        case 'r':
-            healthd_mode_ops = &recovery_ops;
-            break;
-        case '?':
-        default:
-            KLOG_ERROR(LOG_TAG, "Unrecognized healthd option: %c\n", ch);
-            exit(1);
+    if (!strcmp(basename(argv[0]), "charger")) {
+        healthd_mode_ops = &charger_ops;
+    } else {
+        while ((ch = getopt(argc, argv, "cr")) != -1) {
+            switch (ch) {
+            case 'c':
+                healthd_mode_ops = &charger_ops;
+                break;
+            case 'r':
+                healthd_mode_ops = &recovery_ops;
+                break;
+            case '?':
+            default:
+                KLOG_ERROR(LOG_TAG, "Unrecognized healthd option: %c\n", ch);
+                exit(1);
+            }
         }
     }