healthd: break link between BatteryMonitor and BatteryPropertiesRegistrar

Make it easier to later separate out binder-related code.

Add helper functions healthd_battery_update(), healthd_get_property() to
allow these operations without needing references to the BatteryMonitor
object.

Change-Id: Ie584bf53e5178ce0a098d0d940d6c311fdff62d4
diff --git a/healthd/BatteryMonitor.cpp b/healthd/BatteryMonitor.cpp
index 5b3cac6..424d180 100644
--- a/healthd/BatteryMonitor.cpp
+++ b/healthd/BatteryMonitor.cpp
@@ -469,7 +469,7 @@
         KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n");
 
     if (nosvcmgr == false) {
-            mBatteryPropertiesRegistrar = new BatteryPropertiesRegistrar(this);
+            mBatteryPropertiesRegistrar = new BatteryPropertiesRegistrar();
             mBatteryPropertiesRegistrar->publish();
     }
 }
diff --git a/healthd/BatteryPropertiesRegistrar.cpp b/healthd/BatteryPropertiesRegistrar.cpp
index 3e06866..f2edee4 100644
--- a/healthd/BatteryPropertiesRegistrar.cpp
+++ b/healthd/BatteryPropertiesRegistrar.cpp
@@ -23,11 +23,9 @@
 #include <utils/Mutex.h>
 #include <utils/String16.h>
 
-namespace android {
+#include "healthd.h"
 
-BatteryPropertiesRegistrar::BatteryPropertiesRegistrar(BatteryMonitor* monitor) {
-    mBatteryMonitor = monitor;
-}
+namespace android {
 
 void BatteryPropertiesRegistrar::publish() {
     defaultServiceManager()->addService(String16("batterypropreg"), this);
@@ -53,7 +51,7 @@
         mListeners.add(listener);
         listener->asBinder()->linkToDeath(this);
     }
-    mBatteryMonitor->update();
+    healthd_battery_update();
 }
 
 void BatteryPropertiesRegistrar::unregisterListener(const sp<IBatteryPropertiesListener>& listener) {
@@ -68,7 +66,7 @@
 }
 
 status_t BatteryPropertiesRegistrar::getProperty(int id, struct BatteryProperty *val) {
-    return mBatteryMonitor->getProperty(id, val);
+    return healthd_get_property(id, val);
 }
 
 void BatteryPropertiesRegistrar::binderDied(const wp<IBinder>& who) {
diff --git a/healthd/BatteryPropertiesRegistrar.h b/healthd/BatteryPropertiesRegistrar.h
index 0ce6c04..3e86fdf 100644
--- a/healthd/BatteryPropertiesRegistrar.h
+++ b/healthd/BatteryPropertiesRegistrar.h
@@ -17,8 +17,6 @@
 #ifndef HEALTHD_BATTERYPROPERTIES_REGISTRAR_H
 #define HEALTHD_BATTERYPROPERTIES_REGISTRAR_H
 
-#include "BatteryMonitor.h"
-
 #include <binder/IBinder.h>
 #include <utils/Mutex.h>
 #include <utils/Vector.h>
@@ -28,17 +26,13 @@
 
 namespace android {
 
-class BatteryMonitor;
-
 class BatteryPropertiesRegistrar : public BnBatteryPropertiesRegistrar,
                                    public IBinder::DeathRecipient {
 public:
-    BatteryPropertiesRegistrar(BatteryMonitor* monitor);
     void publish();
     void notifyListeners(struct BatteryProperties props);
 
 private:
-    BatteryMonitor* mBatteryMonitor;
     Mutex mRegistrationLock;
     Vector<sp<IBatteryPropertiesListener> > mListeners;
 
diff --git a/healthd/healthd.cpp b/healthd/healthd.cpp
index 900cc42..62f4d11 100644
--- a/healthd/healthd.cpp
+++ b/healthd/healthd.cpp
@@ -31,6 +31,7 @@
 #include <cutils/uevent.h>
 #include <sys/epoll.h>
 #include <sys/timerfd.h>
+#include <utils/Errors.h>
 
 using namespace android;
 
@@ -90,7 +91,11 @@
         KLOG_ERROR(LOG_TAG, "wakealarm_set_interval: timerfd_settime failed\n");
 }
 
-static void battery_update(void) {
+status_t healthd_get_property(int id, struct BatteryProperty *val) {
+    return gBatteryMonitor->getProperty(id, val);
+}
+
+void healthd_battery_update(void) {
     // Fast wake interval when on charger (watch for overheat);
     // slow wake interval when on battery (watch for drained battery).
 
@@ -115,7 +120,7 @@
 }
 
 static void periodic_chores() {
-    battery_update();
+    healthd_battery_update();
 }
 
 static void uevent_init(void) {
@@ -145,7 +150,7 @@
 
     while (*cp) {
         if (!strcmp(cp, "SUBSYSTEM=" POWER_SUPPLY_SUBSYSTEM)) {
-            battery_update();
+            healthd_battery_update();
             break;
         }
 
diff --git a/healthd/healthd.h b/healthd/healthd.h
index 5a1ad18..dc3f67e 100644
--- a/healthd/healthd.h
+++ b/healthd/healthd.h
@@ -18,6 +18,7 @@
 #define _HEALTHD_H_
 
 #include <batteryservice/BatteryService.h>
+#include <utils/Errors.h>
 #include <utils/String8.h>
 
 // periodic_chores_interval_fast, periodic_chores_interval_slow: intervals at
@@ -65,6 +66,12 @@
     android::String8 batteryChargeCounterPath;
 };
 
+// Global helper functions
+
+void healthd_battery_update();
+android::status_t healthd_get_property(int id,
+    struct android::BatteryProperty *val);
+
 // The following are implemented in libhealthd_board to handle board-specific
 // behavior.
 //