Skip to content

Instantly share code, notes, and snippets.

@HalCanary
Created March 19, 2023 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HalCanary/3c5c0fff74b1237102f22424d719e539 to your computer and use it in GitHub Desktop.
Save HalCanary/3c5c0fff74b1237102f22424d719e539 to your computer and use it in GitHub Desktop.
get_battery_health.m
#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