RE: [battery] Alternative design proposal (was: addEventListener side effects, ordering & boundary crossing ...)

Frederick wrote:
> Does a laptop actually know there is no battery, or just that the
> battery level is zero (when the battery is physically removed)?

In short, yes.

I wrote out a longer reply, and then decided it'd be easier to provide parts of an html example to go with. The code isn't wonderful (and is somewhat careless with its timers), but it somewhat works in Firefox and IE.


In short, yes. 

On Windows, there is in fact a third state for GUID_ACDC_POWER_SOURCE when normal A/C - PoAc and battery - PoDc are unavailable and system is using a UPS [1] - PoDc. MSDN [2] is pretty nice.
 
Windows also knows if it has 1 battery or 2 batteries [3] (in theory, I suppose it could tell you there are more, but I've never seen that, I've personally used laptops with 0, 1 and 2 batteries -- one laptop, I could swap the CD bay for a battery, and I could remove the primary battery, and if I was stupid, I could trip over the power cord). It might even know if they're high capacity batteries.
 

GUID_ACDC_POWER_SOURCE
PoAc (has A/C power)
PoDc (is using a battery)
PoHot (is using a UPS -- i.e. it's really unhappy and if you aren't critical you should please get off the system so it can safely power down)

GUID_BATTERY_PERCENTAGE_REMAINING
A percentage (granularity no better than 1%)
 
You can do better with IOCTL_BATTERY_QUERY_STATUS / BATTERY_STATUS [4], which can give you voltage, capacity, rate, and of course find out that the battery is really unhappy (BATTERY_CRITICAL).
 
Note that the IOCTL shows that one can generally get useful data in Time units on Windows instead of as a percentage. This argues in favor of an API which I described before of the form "I need to be able to do X minutes of work, let me know if this will be a problem".
 GUID_SYSTEM_AWAYMODE
Vista feature for Media PCs [5], in short the computer looks off to the user, but is still technically on. A DVR in such a state could continue to record programming, but shouldn't bother trying to send content to output devices (display, speakers).

GUID_MONITOR_POWER_ON
Primary display is on/off - if your application is running on the primary display, this is kinda useful to know about (stop using the GPU when this switches to off).

PBT_APMBATTERYLOW
Battery is low

PBT_APMRESUMECRITICAL
System is resuming after killing suspending because it ran out of power.
 
Since I'm doing the homework now, let's look at OS X too... The primary source for this content is IOKit/IOPowerSources [6].
 
The general content says that IOPSGetPowerSourceDescription will sometimes offer "Time Remaining To Empty", it seems to indicate that e.g. A/C won't support that :).
 
IOPSGetProvidingPowerSourceType() like the Windows API will tell you if you're on A/C (call this "infinite"), D/C (long term battery), or UPS (emergency).
 
IOPSGetTimeRemainingEstimate() will return a battery time estimate. This again supports my argument that the grown up platforms will offer a time remaining and would much rather let users and applications deal in that than a percentage which is absolutely useless to everyone (who would then just have to calculate this anyway to do anything remotely useful).
 
Anssi: of note, battery percentages can vary wildly if someone is amusing and swaps battery sources, a system can easily go from 100% charged to 50% charged (insert an empty battery), or from 99% charged to 33% charged (insert an empty high capacity battery). For more fun, it could go from 40% charged (kinda low), to 15% charged (insert an empty high capacity battery while trying to give more power to the system). In each of these cases, the actual amount of time remaining for the system hasn't changed, but you're going to cause any application listening to a percentage to panic.
 
Next stop: Android 

The Android BatteryManager [7]. I think that for Android one could get a rough estimate of time remaining by adapting the stack overflow sample [8]: Get the current voltage, sleep time X, get the new voltage, calculate the burn rate, estimate how long it would take to burn the remaining voltage.
 
TimeRemaining = VoltageNow * (VoltageOrginal - VoltageNow) / (TimeNow - TimeOriginal) 

For most platforms one can get a rough estimate while the device is on battery. Sure one can't necessarily get a decent answer on some platforms while charging, but if the Host was alive before A/C was added, one can cache that answer and say "I think I have that + 5 minutes, and oh, I'm on A/C".

In any case, if one has a listener who says "Tell me if the system will run out of power before {Date.now() + 5 minutes}", then the host can calculate the most demanding listener (this list is sortable, sort by Date and take the biggest one), and consider its current estimate for time remaining. It can choose to sleep battery polling for a few minutes if at its last sampling, things seemed to be ok for a couple of minutes. It would only need to do work when it gets a signal that battery remaining has dropped sufficient that it would endanger the task which expects to take the longest to complete. The system can also fire an event when that Task's completion time happens saying "Hi, your request for cpu cycles has expired, if you need more guaranteed cycles, you will need to make a new reservation request".

Note that percentages are really fickle, since devices with aggressive power management and aggressive other applications can easily cause them to move rather quickly (as can the removal of whichever battery has most of the charge in a system). If each web application is forced to listen to percentage changes and calculate what that means (and they'll be wrong), nothing good comes out of it (beyond lots of expensive useless work on the part of applications).


[1] http://msdn.microsoft.com/en-us/library/aa373195%28v=VS.85%29.aspx

[2] http://msdn.microsoft.com/en-us/library/aa373247%28v=VS.85%29.aspx

[3] http://msdn.microsoft.com/en-us/library/bb204769%28v=vs.85%29.aspx

[4] http://msdn.microsoft.com/en-us/library/aa372671%28v=vs.85%29.aspx


[5] http://download.microsoft.com/download/9/c/5/9c5b2167-8017-4bae-9fde-d599bac8184a/AwayMode.doc

[6] http://developer.apple.com/library/mac/#documentation/Darwin/Reference/IOKit/IOPowerSources_h/index.html

[7] http://developer.android.com/reference/android/os/BatteryManager.html

[8] http://stackoverflow.com/questions/1805895/android-battery-in-sdk/5097828#5097828



---------------------------------------------------------------------
This transmission (including any attachments) may contain confidential information, privileged material (including material protected by the solicitor-client or other applicable privileges), or constitute non-public information. Any use of this information by anyone other than the intended recipient is prohibited. If you have received this transmission in error, please immediately reply to the sender and delete this information from your system. Use, dissemination, distribution, or reproduction of this transmission by unintended recipients is not authorized and may be unlawful.

Received on Wednesday, 14 September 2011 14:21:21 UTC