rurza / BatFi

Battery charging limiter for macOS.
MIT License
277 stars 33 forks source link

Battery Health #26

Closed rurza closed 8 months ago

rurza commented 8 months ago

@decodism Hey! I created this issue because I don't know any other way to contact you. I was searching for a way to obtain battery health that matches the value reported by the OS in the System Settings. I found a symbol called _getBatteryChargeAndPowerStateInfo in the same private framework we're using for the top coalition info, but I have no idea about the function's signature or how to check it. I would appreciate it if you could provide more information on this. There are also other interesting things I'd like to learn about, such as _systemstats_get_battery_charge_graph and _systemstats_get_coalition_battery_breakdown.

decodism commented 8 months ago

I have looked into it. You want the Battery Service State property of the power source. Its values should be 0 = Normal and 1 = Service Recommended. It seems that it is only accessible with the entitlement com.apple.private.iokit.batterydata. Fortunately, it can be retrieved by parsing the output of pmset -g batt -xml.

_getBatteryChargeAndPowerStateInfo only seems to do what BatFi already does. I've documented what I thought were the signatures of the other functions here.

rurza commented 8 months ago

I'm looking for the percentage health value that can be accessed from the System Settings. I understand that executing /usr/sbin/system_profiler SPPowerDataType and filtering for 'Maximum Capacity' from the output can solve the problem, although I encounter threading issues with NSTask. I was relying on getBatteryChargeAndPowerStateInfo, but it appears I can't make it function properly – dlsym finds the symbol, but I receive a nil result.

decodism commented 8 months ago

Then you want the Maximum Capacity Percent property. system_profiler works too and it has a -xml and -json option. In any case, Process is the way to go.

rurza commented 8 months ago

Will be fixed in 1.4.1

decodism commented 6 months ago

However, I noticed that system_profiler was ~15x slower than pmset on my machine.