Skip to content

Instantly share code, notes, and snippets.

@OlivierLi
Created December 14, 2020 20:53
Show Gist options
  • Save OlivierLi/a24675726379ce6e3ee09b72f9b13e9c to your computer and use it in GitHub Desktop.
Save OlivierLi/a24675726379ce6e3ee09b72f9b13e9c to your computer and use it in GitHub Desktop.
//
// main.m
// test_dict
//
// Created by Jessica Munster on 2020-06-05.
// Copyright © 2020 Jessica Munster. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <Foundation/Foundation.h>
#include <dlfcn.h>
NSDictionary *FCPrivateBatteryStatus()
{
static mach_port_t *s_kIOMasterPortDefault;
static kern_return_t (*s_IORegistryEntryCreateCFProperties)(mach_port_t entry, CFMutableDictionaryRef *properties, CFAllocatorRef allocator, UInt32 options);
static mach_port_t (*s_IOServiceGetMatchingService)(mach_port_t masterPort, CFDictionaryRef matching CF_RELEASES_ARGUMENT);
static CFMutableDictionaryRef (*s_IOServiceMatching)(const char *name);
static CFMutableDictionaryRef g_powerSourceService;
static mach_port_t g_platformExpertDevice;
static BOOL foundSymbols = NO;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
void* handle = dlopen("/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit", RTLD_LAZY);
s_IORegistryEntryCreateCFProperties = dlsym(handle, "IORegistryEntryCreateCFProperties");
s_kIOMasterPortDefault = dlsym(handle, "kIOMasterPortDefault");
s_IOServiceMatching = dlsym(handle, "IOServiceMatching");
s_IOServiceGetMatchingService = dlsym(handle, "IOServiceGetMatchingService");
if (s_IORegistryEntryCreateCFProperties && s_IOServiceMatching && s_IOServiceGetMatchingService) {
g_powerSourceService = s_IOServiceMatching("IOPMPowerSource");
g_platformExpertDevice = s_IOServiceGetMatchingService(*s_kIOMasterPortDefault, g_powerSourceService);
foundSymbols = (g_powerSourceService && g_platformExpertDevice);
}
});
if (! foundSymbols) return nil;
CFMutableDictionaryRef prop = NULL;
s_IORegistryEntryCreateCFProperties(g_platformExpertDevice, &prop, 0, 0);
return prop ? ((NSDictionary *) CFBridgingRelease(prop)) : nil;
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSDictionary* dict = FCPrivateBatteryStatus();
for(NSString *key in [dict allKeys]) {
NSLog(@"%@ : %@",key, [dict objectForKey:key]);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment