Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get raw battery max and current capacity on macOS #7721

Merged
merged 1 commit into from
Aug 9, 2022

Conversation

artemist-work
Copy link
Contributor

Apple's x86 laptops report battery status in mAh but its ARM laptops use percent.
This commit uses an undocumented raw capacity field which reports mAh on both x86 and ARM laptops. I've tested on a 2022 M2 MacBook Pro and a 2012 i5 MacBook Pro, both running macOS 12. However, this needs more testing on more systems.

This will partially fix #6800 but will not fix the incorrect health column. I've spent a while trying to figure out how the Battery.prefPane and coconutBattery get correct results but so far I've only found that Battery.prefPane uses IOPSCopyPowerSourcesByType(1) then gets the Battery Service State field from the first battery, which does not exist for me

Apple's x86 laptops report battery status in mAh but its ARM laptops use percent.
This commit uses an undocumented raw capacity field which reports mAh on both x86 and ARM laptops
@artemist-work artemist-work requested review from a team as code owners August 8, 2022 18:36
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Aug 8, 2022

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: artemist-work / name: Artemis Tosini (1e91b26)

@directionless
Copy link
Member

This commit uses an undocumented raw capacity field which reports mAh on both x86 and ARM laptops. I've tested on a 2022 M2 MacBook Pro and a 2012 i5 MacBook Pro, both running macOS 12. However, this needs more testing on more systems.

You mention undocumented. Do you think it's going to stick around?

@zwass
Copy link
Member

zwass commented Aug 8, 2022

Can confirm I get the exact same results before/after change on 2020 Intel MBP running 12.5.

Can confirm the change provides numbers that appear to be mAh on M1 running 13 beta 3.

@zwass
Copy link
Member

zwass commented Aug 8, 2022

Do you think it's going to stick around?

I'm not sure what basis we would have to make such a determination... Osquery does use tons of undocumented and private APIs to get at the data we provide, so it seems reasonable enough to me to do so here.

Copy link
Member

@directionless directionless left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I'm game

@directionless
Copy link
Member

I guess the other question is whether we should expose both.

@zwass
Copy link
Member

zwass commented Aug 9, 2022

I'd say leave it as-is? The percentage can be calculated from these values in both cases.

@mike-myers-tob
Copy link
Member

I've tested this PR using select * from battery; successfully on:

  • MacBook Pro 2020 (x86), macOS 12.5. "Designed capacity" for this battery is not published by Apple anywhere I can find, but unofficially it is 5,086 mAh which is what osquery now outputs with this PR.
  • Mac Mini M1, macOS 12.5. No battery. The table reports nothing (no rows), as before.

@mike-myers-tob mike-myers-tob merged commit 0aff33e into osquery:master Aug 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Battery table misreports on apple silicon m1 machines
4 participants