Merge ~ghadi-rahme/ubuntu/+source/powermgmt-base:lp-1980991 into ubuntu/+source/powermgmt-base:ubuntu/devel

Proposed by Ghadi Rahme
Status: Needs review
Proposed branch: ~ghadi-rahme/ubuntu/+source/powermgmt-base:lp-1980991
Merge into: ubuntu/+source/powermgmt-base:ubuntu/devel
Diff against target: 115 lines (+54/-34)
2 files modified
debian/changelog (+7/-0)
on_ac_power (+47/-34)
Reviewer Review Type Date Requested Status
Ubuntu Sponsors Pending
git-ubuntu import Pending
Review via email: mp+482635@code.launchpad.net

Commit message

on_ac_power: improve reporting of AC power status in more scenarios

- If no battery is detected, do not report that the machine is offline.
    This fixes an issue with intel nuc devices that incorrectly report
    their typec ports as sink ports.
- Improve how the typec power role is determined by checking only
    the typec ports that are part of the UCSI device rather than checking
    the entire array of typec ports.

(LP: #1980991)

Description of the change

This adds a battery check that is already available in systemd: https://github.com/systemd/systemd/pull/24220
The only difference with this implementation is that it will run the other checks instead of directly assuming that the power is online. If other checks fail 255 will be returned.

It also makes typec power role checks tighter by only checking the typec ports related to each UCSI interface instead of checking all of them.

These two changes should massively improve the accuracy of on_ac_power

To post a comment you must log in.

Unmerged commits

e669f9e... by Ghadi Rahme

update changelog

a017489... by Ghadi Rahme

on_ac_power: improve reporting of AC power status in more scenarios

- If no battery is detected, do not report that the machine is offline.
This fixes an issue with intel nuc devices that incorrectly report
their typec ports as sink ports.
- Improve how the typec power role is determined by checking only
the typec ports that are part of the UCSI device rather than checking
the entire array of typec ports.

(LP: #1980991)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/debian/changelog b/debian/changelog
2index 883a5a6..2b2dc32 100644
3--- a/debian/changelog
4+++ b/debian/changelog
5@@ -1,3 +1,10 @@
6+powermgmt-base (1.38+nmu1) unstable; urgency=medium
7+
8+ * on_ac_power: improve reporting of AC power status in more scenarios
9+ (LP: #1980991)
10+
11+ -- Ghadi Elie Rahme <ghadi.rahme@canonical.com> Tue, 11 Mar 2025 17:49:39 +0000
12+
13 powermgmt-base (1.38) unstable; urgency=medium
14
15 * Team upload, salsa.d.o debian namespace
16diff --git a/on_ac_power b/on_ac_power
17index 2e65fc0..700861e 100755
18--- a/on_ac_power
19+++ b/on_ac_power
20@@ -23,48 +23,61 @@ set -e
21 OFF_LINE_P=no
22 USB_IS_SINK=no
23 USB_IS_SOURCE=no
24-power_type="Mains"
25+POWER_TYPE="Mains"
26+HAS_BATTERY=no
27
28 # USB-C sysfs documentation:
29 # https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-class-typec
30 #
31-# We verify first if there are any USB-C devices in power sink mode to
32-# check if we should consider USB as a power source. If no source ports
33-# are found assume they are sink ports.
34-#
35-if [ -d /sys/class/typec/ ]; then
36- for FN in /sys/class/typec/*; do
37- if test -d "${FN}" && test -r "${FN}/power_role"; then
38- power_role="$(cat "${FN}/power_role")"
39- if echo $power_role | grep -q '\[source\]'; then
40- USB_IS_SOURCE=yes
41- elif echo $power_role | grep -q '\[sink\]'; then
42- USB_IS_SINK=yes
43- fi
44- fi
45- done
46- # Add USB* power type if any USB ports are sink or no source is found
47- if [ "${USB_IS_SINK}" = "yes" ] || [ "${USB_IS_SOURCE}" = "no" ]; then
48- power_type="USB*"
49- fi
50-fi
51-
52+# We verify first if the USB-C port is is in sink or source mode before
53+# checking it's connection status. Many typec devices might be linked to
54+# one ucsi. If no source is found, treat it like a sink.
55+check_usb_power_role() {
56+ USB_IS_SINK=no
57+ USB_IS_SOURCE=no
58+ POWER_TYPE="Mains"
59+ if [ -d $1/device/typec ]; then
60+ for FN in $1/device/typec/*; do
61+ if test -d "${FN}" && test -r "${FN}/power_role"; then
62+ power_role="$(cat "${FN}/power_role")"
63+ if echo $power_role | grep -q '\[source\]'; then
64+ USB_IS_SOURCE=yes
65+ elif echo $power_role | grep -q '\[sink\]'; then
66+ USB_IS_SINK=yes
67+ fi
68+ fi
69+ done
70+ # Add USB* power type if any USB ports are sink or no source is found
71+ if [ "${USB_IS_SINK}" = "yes" ] || [ "${USB_IS_SOURCE}" = "no" ]; then
72+ POWER_TYPE="USB*"
73+ fi
74+ fi
75+}
76
77 if [ -d /sys/class/power_supply/ ]; then
78 for FN in /sys/class/power_supply/*; do
79- if test -d "${FN}" && test -r "${FN}/type"; then
80- type="$(cat "${FN}/type")"
81- case "${type}" in
82- Mains|$power_type|BrickID|Wireless)
83- if [ -r "${FN}/online" ]; then
84- online="$(cat "${FN}/online")"
85- [ "$online" = 1 ] && exit 0
86- [ "$online" = 0 ] && OFF_LINE_P=yes
87- fi;;
88- esac
89- fi
90+ if test -d "${FN}" && test -r "${FN}/type"; then
91+ type="$(cat "${FN}/type")"
92+ # check if the device has a battery
93+ if echo "$FN" | grep -qE '/BAT[0-9]+$' && [ "$type" = "Battery" ]; then
94+ HAS_BATTERY=yes
95+ fi
96+ # check if it's a typec and it's power role if it is.
97+ # Also set $POWER_TYPE accordingly and reset variables after last run
98+ check_usb_power_role "$FN"
99+ case "${type}" in
100+ Mains|$POWER_TYPE|BrickID|Wireless)
101+ if [ -r "${FN}/online" ]; then
102+ online="$(cat "${FN}/online")"
103+ [ "$online" = 1 ] && exit 0
104+ [ "$online" = 0 ] && OFF_LINE_P=yes
105+ fi;;
106+ esac
107+ fi
108 done
109- [ "${OFF_LINE_P}" = "yes" ] && exit 1
110+ # if no battery is present then we cannot be offline. Run the other checks in case
111+ # the true state can be determined.
112+ [ "${OFF_LINE_P}" = "yes" ] && [ "${HAS_BATTERY}" = "yes" ] && exit 1
113 fi
114
115 # PMU

Subscribers

People subscribed via source and target branches