Created
March 19, 2023 14:34
-
-
Save HalCanary/3c5c0fff74b1237102f22424d719e539 to your computer and use it in GitHub Desktop.
get_battery_health.m
This file contains 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
#include <CoreFoundation/CoreFoundation.h> | |
#include <Foundation/NSObjCRuntime.h> | |
#include <IOKit/ps/IOPSKeys.h> | |
#include <IOKit/ps/IOPowerSources.h> | |
#include <assert.h> | |
int main() { | |
CFTypeRef psInfo = IOPSCopyPowerSourcesInfo(); | |
assert(psInfo != NULL); | |
CFArrayRef list = IOPSCopyPowerSourcesList(psInfo); | |
assert(list != NULL); | |
long count = CFArrayGetCount(list); | |
for(long i = 0; i < count; i++) { | |
CFDictionaryRef ps = IOPSGetPowerSourceDescription(psInfo, CFArrayGetValueAtIndex(list, i)); | |
assert(ps != NULL); | |
CFStringRef deviceName = (CFStringRef)CFDictionaryGetValue(ps, CFSTR(kIOPSNameKey)); | |
assert(deviceName != NULL); | |
CFStringRef serialNumber = (CFStringRef)CFDictionaryGetValue(ps, CFSTR(kIOPSHardwareSerialNumberKey)); | |
assert(serialNumber != NULL); | |
CFStringRef health = (CFStringRef)CFDictionaryGetValue(ps, CFSTR(kIOPSBatteryHealthKey)); | |
assert(health != NULL); | |
NSLog(@"Name=\"%@\" serialNumber=\"%@\" BatteryHealth=\"%@\"\n", | |
(__bridge NSString *)deviceName, | |
(__bridge NSString *)serialNumber, | |
(__bridge NSString *)health); | |
} | |
CFRelease(list); | |
CFRelease(psInfo); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment