powerd's PowerSupply class is responsible for reading information about power supplies (e.g. line power and batteries) from device directories under
/sys/class/power_supply in sysfs. The kernel documentation contains incomplete information about these directories.
powerd reads power supply information at startup and then polls periodically at 30-second intervals. The information is also refreshed immediately in response to several events:
The information is copied to PowerSupplyProperties protocol buffers that are included in
PowerSupplyPoll D-Bus signals.
“Line power” refers to chargers or other external power sources; it's sometimes also referred to as “AC” in the code. Information about line power sources is read by
PowerSupply::ReadLinePowerDirectory. The following sysfs nodes are particularly relevant:
typedescribes the connected device's type. Notable values include:
Unknown- A sink-only device that can't supply power to the Chrome OS device.
Mains- A dedicated charger or direct power source. Most pre-USB-C chargers use this type. This type may also appear in conjunction with an
0when nothing is connected to a dedicated charging port on a pre-USB-C device.
USB_DCP- A low-power USB BC1.2 power supply. USB-C ports with nothing connected to them may report a
USBtype in conjunction with an
USB_PD- A USB Type-C power supply.
USB_PD_DRP- A dual-role USB Type-C device (i.e. one capable of either delivering or receiving power).
BrickID- An Apple legacy USB charger. May be either USB-A or USB-C.
onlinetypically describes whether anything is connected to the port; a value of
1indicates a connection. If
0indicates that a dual-role device is connected but that it is not currently delivering power to the Chrome OS device.
statusdescribes the status of a bidirectional/dual-role port. The node is expected to be nonexistent or to contain an empty value for non-dual-role ports. If the node is present, a value of
Chargingindicates that the port is delivering power to the Chrome OS device.
current_maxare used to compute the maximum power that can be delivered by the power supply.
current_noware used to compute the instantaneous power.
If multiple line power directories are found, powerd will report all of them. This typically happens in the case of Chrome OS devices with multiple USB Type-C ports.
Only one power source may deliver power at a given time; Chrome displays the active source at
chrome://settings/power. If multiple sources are available, the user may use this page to select which one to use. Chrome makes a
SetPowerSource D-Bus method call to powerd containing the ID of the desired source (taken from an earlier PowerSupplyProperties protobuf), and
0 to the device‘s
charge_control_limit_max sysfs node. Chrome can also pass an empty ID to switch to battery power; powerd writes
-1 to the active power source’s node in this case.
Battery information is read by
PowerSupply::ReadBatteryDirectory. The following sysfs nodes are relevant:
statusdescribes the battery‘s current status. The kernel documentation describes possible values. powerd does not distinguish between
Fulland instead uses the battery’s reported charge to determine if it is full.
voltage_nowcontains the instantaneous voltage.
voltage_max_designare read to determine the nominal voltage.
charge_fullare read on
charge_batterysystems to get the battery's current and charge.
energy_fullare read on
energy_batterysystems to get the battery's current and charge.
PowerSupply::UpdateBatteryPercentagesAndState uses the above information to determine the battery status and charge that should be displayed by the UI. Many batteries charge more slowly as they approach a full charge, so the
power_supply_full_factor powerd pref is used to set the charge percentage at which powerd should report the battery as fully-charged. Similarly, powerd will automatically shut the system down before the battery has fully discharged, so the displayed percentage is also scaled based on the
PowerSupply::UpdateBatteryTimeEstimates attempts to estimate the time until the battery will be fully charged or until the system will shut down due to a low charge. To do this,
PowerSupply maintains a rolling average of samples of the battery‘s current. Sampling is temporarily deferred after events like system resume, but these estimates may still be noisy, particularly when the system’s workload is changing frequently.
The Battery Notifications document contains more information about how these estimates are computed and used.
multiple_batteries pref is set, powerd will use
PowerSupply::ReadMultipleBatteryDirectories to read multiple battery power supply directories if present. Most statistics, including the charge and full charge, are just summed across all batteries. If any batteries are charging, the overall state is reported as charging. The UI doesn't currently differentiate between a single battery and multiple batteries; the main difference in the latter scenario is that the displayed charge percentage may change when a battery is connected or disconnected.