Conversation
Peripherals with their own cell — wireless keyboards and mice, headsets,
controllers — register in /sys/class/power_supply with type "Battery".
ListBatteries returned them alongside the real system battery, so `set`,
`apply` and `persist enable` tried to write charge thresholds to, say, a
Logitech keyboard's hidpp_battery_0, which exposes no threshold attributes:
Error on hidpp_battery_0: writing "40" to /sys/class/power_supply/
hidpp_battery_0/charge_control_start_threshold: permission denied
`set` aborts on that error, so the thresholds it had already written to BAT0
were never saved to /etc/batctl.conf and silently reverted on the next boot.
The failure is intermittent because these devices leave sysfs when the
peripheral sleeps or is unplugged.
The kernel already marks such supplies: it reports POWER_SUPPLY_SCOPE_DEVICE
through the "scope" attribute (hid-logitech-hidpp hardcodes it). Skip anything
device-scoped. Batteries without a "scope" attribute are still treated as
system batteries, so laptop batteries — including those on platform backends
such as IdeaPad, Huawei, Samsung, Sony and Acer, which drive thresholds
through a platform device and expose no per-battery attributes — are
unaffected.
The error also lied about the cause. SysfsWriteString used os.WriteFile, which
passes O_CREATE; sysfs refuses to create files and answers EACCES rather than
ENOENT, so a missing attribute looked like a privilege problem even under
sudo. Open without O_CREATE so the real "no such file or directory" surfaces.
The two write tests wrote to paths that did not exist, relying on the
O_CREATE that caused the bug. sysfs attributes always exist ahead of time, so
they now pre-create the attribute, matching real behaviour.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
jash8506
force-pushed
the
fix/skip-device-scoped-batteries
branch
from
September 14, 2026 02:07
cd58b4b to
b8c160c
Compare
Author
|
Closing — this overlaps #13, which predates it by twelve days and fixes the same root cause. I found it only after opening this. Apologies for the noise. The parts that were additive rather than duplicative are now stacked on top of that branch as lucserre#1: @lucserre's approach is the right one for the write path — |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #14.
Peripherals with their own cell — wireless keyboards and mice, headsets,
controllers — register in
/sys/class/power_supplywithtypeofBattery.ListBatteriesreturned them alongside the real system battery, soset,applyand the TUI tried to write charge thresholds to, for example, aLogitech keyboard's
hidpp_battery_0, which exposes no threshold attributes:Skip device-scoped supplies
The kernel already marks these: it reports
POWER_SUPPLY_SCOPE_DEVICEthroughthe
scopeattribute.hid-logitech-hidpphardcodes it, and the file neverexposes
charge_control_*at all.ListBatteriesnow skips anything device-scoped. A battery with noscopeattribute is still treated as a system battery —
scopeis optional and mostlaptop batteries omit it (my
BAT0does), so this deliberately does notrequire the attribute to be present.
I chose
scopeover "does this battery exposecharge_control_*?" preciselybecause the latter would break the platform backends: IdeaPad, Huawei, Samsung,
Sony and Acer drive thresholds through a platform device and their batteries
expose no per-battery threshold attributes, so an attribute-based filter would
return an empty list and break them. Filtering on
scopeleaves them untouched.Report missing attributes honestly
SysfsWriteStringusedos.WriteFile, which passesO_CREATE. sysfs refusesto create files and answers
EACCESrather thanENOENT, so a missingattribute was reported as
permission deniedeven as root — which sends peoplelooking at privileges instead of at the attribute. The read path in the same run
reported
no such file or directoryfor the identical path.Opening with
O_WRONLYand noO_CREATEsurfaces the real error.Tests
TestListBatteries— fixture tree with a scopeless battery, aSystembattery, a
Device-scopedhidpp_battery_0, aMainssupply and aDevice-scoped USB supply; asserts only the first two are returned.TestIsDeviceScoped— scopeless,System,Device, and an unreadable path.TestSysfsWriteStringMissingAttribute— asserts a missing attribute yieldsfs.ErrNotExist, explicitly notfs.ErrPermission, and that the attributeis not created.
Both new tests fail against
masterand pass with this change:TestSysfsWriteIntandTestSysfsWriteStringwrote to paths that did notexist, relying on the
O_CREATEthis PR removes. sysfs attributes always existahead of time, so they now pre-create the attribute before writing, which is
what the real thing looks like.
PowerSupplyBasebecame avarso tests can point it at a fixture directory.Verification
go build,go vetandgo test ./...all pass.On the affected hardware (ThinkPad, ThinkPad backend),
masterpicks theperipheral up while the keyboard is connected:
The exclusion itself is covered by
TestListBatteries, which builds a fixturetree containing a
Device-scopedhidpp_battery_0and asserts it is dropped;it fails against
masterwith exactly the list above. I have not captured alive before/after of the fixed binary with the keyboard reattached — happy to
add that if useful.
Scope
This removes the condition that triggered the failure — the peripheral never
enters the battery list, so the loop in
applyAndSavehas nothing to fail on.It does not change how that loop behaves if some other battery fails, which
remains as it is today.
🤖 Generated with Claude Code