healthd: Set fixed battery level and temperature via properties

setprop persist.sys.battery.capacity 77
setprop persist.sys.battery.temperature 123

and reboot to cause a fixed battery level of 77% and temperature of 12.3C
to be reported to Android.

Typically used on power evaluation boards without batteries connected.

Bug: 14839868
Change-Id: Ibae5e16429d05891cb0787d74a2fe93b07013699
diff --git a/healthd/BatteryMonitor.cpp b/healthd/BatteryMonitor.cpp
index 4ee6249..1ee33a1 100644
--- a/healthd/BatteryMonitor.cpp
+++ b/healthd/BatteryMonitor.cpp
@@ -27,6 +27,7 @@
 #include <unistd.h>
 #include <batteryservice/BatteryService.h>
 #include <cutils/klog.h>
+#include <cutils/properties.h>
 #include <sys/types.h>
 #include <utils/Errors.h>
 #include <utils/String8.h>
@@ -184,10 +185,14 @@
     else
         props.batteryPresent = mBatteryDevicePresent;
 
-    props.batteryLevel = getIntField(mHealthdConfig->batteryCapacityPath);
+    props.batteryLevel = mBatteryFixedCapacity ?
+        mBatteryFixedCapacity :
+        getIntField(mHealthdConfig->batteryCapacityPath);
     props.batteryVoltage = getIntField(mHealthdConfig->batteryVoltagePath) / 1000;
 
-    props.batteryTemperature = getIntField(mHealthdConfig->batteryTemperaturePath);
+    props.batteryTemperature = mBatteryFixedTemperature ?
+        mBatteryFixedTemperature :
+        getIntField(mHealthdConfig->batteryTemperaturePath);
 
     const int SIZE = 128;
     char buf[SIZE];
@@ -367,6 +372,7 @@
 
 void BatteryMonitor::init(struct healthd_config *hc) {
     String8 path;
+    char pval[PROPERTY_VALUE_MAX];
 
     mHealthdConfig = hc;
     DIR* dir = opendir(POWER_SUPPLY_SYSFS_PATH);
@@ -523,6 +529,12 @@
         if (mHealthdConfig->batteryTechnologyPath.isEmpty())
             KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n");
     }
+
+    if (property_get("persist.sys.battery.capacity", pval, NULL) > 0)
+        mBatteryFixedCapacity = (int) strtol(pval, NULL, 10);
+
+    if (property_get("persist.sys.battery.temperature", pval, NULL) > 0)
+        mBatteryFixedTemperature = (int) strtol(pval, NULL, 10);
 }
 
 }; // namespace android