Wednesday, January 21, 2009

Source Code : Get Hardware Info of iPhone

This is a collection of open source codes to print out the hardware info of iPhone. If you know more, please post link here in comment and I will update it.

Battery Info source (requires IOKit header files from SimulatorSDK)
http://forums.macrumors.com/showthread.php?t=474628

Memory & CPU
http://furbo.org/2007/08/21/what-the-iphone-specs-dont-tell-you/

Process
Landon Fuller, iphonesdk@googlegroups.com

hwinfo.c Select all

#import <stdio.h>
#import <string.h>

#import <mach/mach_host.h>
#import <sys/sysctl.h>

#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/ps/IOPowerSources.h>
#include <IOKit/ps/IOPSKeys.h>

#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>

void printMemoryInfo()
{
size_t length;
int mib[6];
int result;

printf("Memory Info\n");
printf("-----------\n");

int pagesize;
mib[0] = CTL_HW;
mib[1] = HW_PAGESIZE;
length = sizeof(pagesize);
if (sysctl(mib, 2, &pagesize, &length, NULL, 0) < 0)
{
perror("getting page size");
}
printf("Page size = %d bytes\n", pagesize);
printf("\n");

mach_msg_type_number_t count = HOST_VM_INFO_COUNT;

vm_statistics_data_t vmstat;
if (host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vmstat, &count) != KERN_SUCCESS)
{
printf("Failed to get VM statistics.");
}

double total = vmstat.wire_count + vmstat.active_count + vmstat.inactive_count + vmstat.free_count;
double wired = vmstat.wire_count / total;
double active = vmstat.active_count / total;
double inactive = vmstat.inactive_count / total;
double free = vmstat.free_count / total;

printf("Total = %8d pages\n", vmstat.wire_count + vmstat.active_count + vmstat.inactive_count + vmstat.free_count);
printf("\n");
printf("Wired = %8d bytes\n", vmstat.wire_count * pagesize);
printf("Active = %8d bytes\n", vmstat.active_count * pagesize);
printf("Inactive = %8d bytes\n", vmstat.inactive_count * pagesize);
printf("Free = %8d bytes\n", vmstat.free_count * pagesize);
printf("\n");
printf("Total = %8d bytes\n", (vmstat.wire_count + vmstat.active_count + vmstat.inactive_count + vmstat.free_count) * pagesize);
printf("\n");
printf("Wired = %0.2f %%\n", wired * 100.0);
printf("Active = %0.2f %%\n", active * 100.0);
printf("Inactive = %0.2f %%\n", inactive * 100.0);
printf("Free = %0.2f %%\n", free * 100.0);
printf("\n");

mib[0] = CTL_HW;
mib[1] = HW_PHYSMEM;
length = sizeof(result);
if (sysctl(mib, 2, &result, &length, NULL, 0) < 0)
{
perror("getting physical memory");
}
printf("Physical memory = %8d bytes\n", result);
mib[0] = CTL_HW;
mib[1] = HW_USERMEM;
length = sizeof(result);
if (sysctl(mib, 2, &result, &length, NULL, 0) < 0)
{
perror("getting user memory");
}
printf("User memory = %8d bytes\n", result);
printf("\n");
}

void printProcessorInfo()
{
size_t length;
int mib[6];
int result;

printf("Processor Info\n");
printf("--------------\n");

mib[0] = CTL_HW;
mib[1] = HW_CPU_FREQ;
length = sizeof(result);
if (sysctl(mib, 2, &result, &length, NULL, 0) < 0)
{
perror("getting cpu frequency");
}
printf("CPU Frequency = %d hz\n", result);

mib[0] = CTL_HW;
mib[1] = HW_BUS_FREQ;
length = sizeof(result);
if (sysctl(mib, 2, &result, &length, NULL, 0) < 0)
{
perror("getting bus frequency");
}
printf("Bus Frequency = %d hz\n", result);
printf("\n");
}

int printBatteryInfo()
{
CFTypeRef blob = IOPSCopyPowerSourcesInfo();
CFArrayRef sources = IOPSCopyPowerSourcesList(blob);

CFDictionaryRef pSource = NULL;
const void *psValue;

int numOfSources = CFArrayGetCount(sources);
if (numOfSources == 0) {
perror("Error getting battery info");
return 1;
}

printf("Battery Info\n");
printf("------------\n");

for (int i = 0 ; i < numOfSources ; i++)
{
pSource = IOPSGetPowerSourceDescription(blob, CFArrayGetValueAtIndex(sources, i));
if (!pSource) {
perror("Error getting battery info");
return 2;
}
psValue = (CFStringRef)CFDictionaryGetValue(pSource, CFSTR(kIOPSNameKey));

int curCapacity = 0;
int maxCapacity = 0;
int percent;

psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSCurrentCapacityKey));
CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &curCapacity);

psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSMaxCapacityKey));
CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &maxCapacity);

percent = (int)((double)curCapacity/(double)maxCapacity * 100);

printf ("powerSource %d of %d: percent: %d/%d = %d%%\n", i+1, CFArrayGetCount(sources), curCapacity, maxCapacity, percent);
printf("\n");

}

}

int printProcessInfo() {
int mib[5];
struct kinfo_proc *procs = NULL, *newprocs;
int i, st, nprocs;
size_t miblen, size;

/* Set up sysctl MIB */
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_ALL;
mib[3] = 0;
miblen = 4;

/* Get initial sizing */
st = sysctl(mib, miblen, NULL, &size, NULL, 0);

/* Repeat until we get them all ... */
do {
/* Room to grow */
size += size / 10;
newprocs = realloc(procs, size);
if (!newprocs) {
if (procs) {
free(procs);
}
perror("Error: realloc failed.");
return (0);
}
procs = newprocs;
st = sysctl(mib, miblen, procs, &size, NULL, 0);
} while (st == -1 && errno == ENOMEM);

if (st != 0) {
perror("Error: sysctl(KERN_PROC) failed.");
return (0);
}

/* Do we match the kernel? */
assert(size % sizeof(struct kinfo_proc) == 0);

nprocs = size / sizeof(struct kinfo_proc);

if (!nprocs) {
perror("Error: printProcessInfo.");
return(0);
}
printf(" PID\tName\n");
printf("-----\t--------------\n");
for (i = nprocs-1; i >=0; i--) {
printf("%5d\t%s\n",(int)procs[i].kp_proc.p_pid, procs[i].kp_proc.p_comm);
}
free(procs);
return (0);
}


int main(int argc, char **argv)
{
printf("iPhone Hardware Info\n");
printf("====================\n");
printf("\n");

printMemoryInfo();
printProcessorInfo();
printBatteryInfo();
printProcessInfo();
return (0);
}


MAC Address & IP address of network interfaces
original source from devforums.apple.com and modified to show IP address and exclude loopback addresses

macaddress.c Select all

#include <stdio.h>
#include <stdlib.h>
#include <ifaddrs.h>
#include <string.h>
#include <stdbool.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <arpa/inet.h>
#include <ifaddrs.h>

#if ! defined(IFT_ETHER)
#define IFT_ETHER 0x6/* Ethernet CSMACD */
#endif

int main(int argc, char **argv)
{
bool success;
struct ifaddrs *addrs;
const struct ifaddrs *cursor;
const struct sockaddr_dl *dlAddr;
const uint8_t *base;
int i;

success = getifaddrs(&addrs) == 0;
if (success) {
cursor = addrs;
while (cursor != NULL) {
if ((cursor->ifa_flags & IFF_LOOPBACK) == 0 ) {
printf("%s ", (char *)cursor->ifa_name);
printf("%s\n",inet_ntoa(((struct sockaddr_in *)cursor->ifa_addr)->sin_addr));
}
if ( (cursor->ifa_addr->sa_family == AF_LINK)
&& (((const struct sockaddr_dl *) cursor->ifa_addr)->sdl_type ==IFT_ETHER)
) {
dlAddr = (const struct sockaddr_dl *) cursor->ifa_addr;
// fprintf(stderr, " sdl_nlen = %d\n", dlAddr->sdl_nlen);
// fprintf(stderr, " sdl_alen = %d\n", dlAddr->sdl_alen);
base = (const uint8_t *) &dlAddr->sdl_data[dlAddr->sdl_nlen];
printf(" MAC address ");
for (i = 0; i < dlAddr->sdl_alen; i++) {
if (i != 0) {
printf(":");
}
printf("%02x", base[i]);
}
printf("\n");
}
cursor = cursor->ifa_next;
}
}
return 0;
}

 
 
 
There are also iPhone device info from the SDK in UIDevice.h
UIDevice.h Select all

+ (UIDevice *)currentDevice;
@property(nonatomic,readonly,retain) NSString *name; // e.g. "My iPhone"
@property(nonatomic,readonly,retain) NSString *model; // e.g. @"iPhone", @"iPod Touch"
@property(nonatomic,readonly,retain) NSString *localizedModel; // localized version of model
@property(nonatomic,readonly,retain) NSString *systemName; // e.g. @"iPhone OS"
@property(nonatomic,readonly,retain) NSString *systemVersion; // e.g. @"2.0"
@property(nonatomic,readonly) UIDeviceOrientation orientation; // return current device orientation
@property(nonatomic,readonly,retain) NSString *uniqueIdentifier; // a string unique to each device based on various hardware info.

undocumented
- (id)buildVersion;
- (int)batteryState;





 
 
 

8 comments:

Unknown said...

you code works in iphone simulator, however, can't compile for iphone device,

I select Device - 2.0 | Debug, then cmd+b, it give errors...

gtalk: eric.archangel@gmail.com

Unknown said...

Hi, thanks the codes very much,

the codes works well for simulator, however can not compile for device?

the error is about IOkit.framework,
How you compile the codes in Device,

my gtalk is : eric.archangel@gmail.com

javacom said...

The IOKit iPhone binary and the IOKit headers files are missing from your Mac.

For example for SDK2.0, you must copy it back from to avoid linker error

/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.0.sdk/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit

to

/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.0.sdk/System/Library/Frameworks/IOKit.framework/IOKit

and you also need to put back the header files from iPhoneimulator to iPhoneOS

Apple remove these as IOKit is not intended to be for App Store

FireFly said...

hi, i have follow your comment,
but my xcode still report link error,
could you please help me to fix it.

Rennen said...

What do you mean "Apple remove these as IOKit is not intended to be for App Store" ???
If I use it, than Apple will not let my app enter the AppStore?

Zibri said...

Undefined symbols:
"_CFArrayGetValueAtIndex", referenced from:
_printBatteryInfo in cc4MY8pl.o
"___CFConstantStringClassReference", referenced from:
cfstring=Name in cc4MY8pl.o
cfstring=Current Capacity in cc4MY8pl.o
cfstring=Max Capacity in cc4MY8pl.o
"_CFNumberGetValue", referenced from:
_printBatteryInfo in cc4MY8pl.o
_printBatteryInfo in cc4MY8pl.o
"_IOPSGetPowerSourceDescription", referenced from:
_printBatteryInfo in cc4MY8pl.o
"_CFDictionaryGetValue", referenced from:
_printBatteryInfo in cc4MY8pl.o
_printBatteryInfo in cc4MY8pl.o
_printBatteryInfo in cc4MY8pl.o
"_IOPSCopyPowerSourcesList", referenced from:
_printBatteryInfo in cc4MY8pl.o
"_CFArrayGetCount", referenced from:
_printBatteryInfo in cc4MY8pl.o
_printBatteryInfo in cc4MY8pl.o
"_IOPSCopyPowerSourcesInfo", referenced from:
_printBatteryInfo in cc4MY8pl.o

Meghna Baijal said...

Hi, i am completely new to the ios platform. just wanted to know are these codes using public or private APIs>

Saroj Saini said...

So useful info, knowing hardware features is kind of very interesting.

iphone application development