forked from mattconnolly/uidevice-extension
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUIDevice-IOKitExtensions.m
156 lines (129 loc) · 3.75 KB
/
UIDevice-IOKitExtensions.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
Erica Sadun, http://ericasadun.com
iPhone Developer's Cookbook, 3.0 Edition
BSD License, Use at your own risk
*/
#import "UIDevice-IOKitExtensions.h"
#include <sys/types.h>
#include <sys/sysctl.h>
#import <mach/mach_host.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <ifaddrs.h>
#if SUPPORTS_IOKIT_EXTENSIONS
#pragma mark IOKit miniheaders
#define kIODeviceTreePlane "IODeviceTree"
enum {
kIORegistryIterateRecursively = 0x00000001,
kIORegistryIterateParents = 0x00000002
};
typedef mach_port_t io_object_t;
typedef io_object_t io_registry_entry_t;
typedef char io_name_t[128];
typedef UInt32 IOOptionBits;
/*
extern id lockdown_connect();
extern id lockdown_copy_value(id, id, id);
extern void lockdown_disconnect();
extern NSString *kLockdownDeviceColorKey;
*/
CFTypeRef
IORegistryEntrySearchCFProperty(
io_registry_entry_t entry,
const io_name_t plane,
CFStringRef key,
CFAllocatorRef allocator,
IOOptionBits options );
kern_return_t
IOMasterPort( mach_port_t bootstrapPort,
mach_port_t * masterPort );
io_registry_entry_t
IORegistryGetRootEntry(
mach_port_t masterPort );
CFTypeRef
IORegistryEntrySearchCFProperty(
io_registry_entry_t entry,
const io_name_t plane,
CFStringRef key,
CFAllocatorRef allocator,
IOOptionBits options );
kern_return_t mach_port_deallocate
(ipc_space_t task,
mach_port_name_t name);
@implementation UIDevice (IOKit_Extensions)
#pragma mark IOKit Utils
NSArray *getValue(NSString *iosearch)
{
mach_port_t masterPort;
CFTypeID propID = (CFTypeID) NULL;
unsigned int bufSize;
kern_return_t kr = IOMasterPort(MACH_PORT_NULL, &masterPort);
if (kr != noErr) return nil;
io_registry_entry_t entry = IORegistryGetRootEntry(masterPort);
if (entry == MACH_PORT_NULL) return nil;
CFTypeRef prop = IORegistryEntrySearchCFProperty(entry, kIODeviceTreePlane, (__bridge CFStringRef) iosearch, nil, kIORegistryIterateRecursively);
if (!prop) return nil;
propID = CFGetTypeID(prop);
if (!(propID == CFDataGetTypeID()))
{
mach_port_deallocate(mach_task_self(), masterPort);
return nil;
}
CFDataRef propData = (CFDataRef) prop;
if (!propData) return nil;
bufSize = CFDataGetLength(propData);
if (!bufSize) return nil;
NSString *p1 = [[NSString alloc] initWithBytes:CFDataGetBytePtr(propData) length:bufSize encoding:1];
mach_port_deallocate(mach_task_self(), masterPort);
return [p1 componentsSeparatedByString:@"\0"];
}
- (NSString *) imei
{
NSArray *results = getValue(@"device-imei");
if (results) return [results objectAtIndex:0];
return nil;
}
- (NSString *) serialnumber
{
NSArray *results = getValue(@"serial-number");
if (results) return [results objectAtIndex:0];
return nil;
}
- (NSString *) backlightlevel
{
NSArray *results = getValue(@"backlight-level");
if (results) return [results objectAtIndex:0];
return nil;
}
- (NSString *) modelnumber{
NSArray *results = getValue(@"model-number");
if (results){
NSString *mn=[[results objectAtIndex:0] copy];
return mn;
}
return nil;
}
- (NSString *) RegionInfo{
NSArray *results = getValue(@"region-info");
if (results){
NSString *mn=[[results objectAtIndex:0] copy];
return mn;
}
return nil;
}
/*
- (NSString *) CopyDeviceColor {
id connection = lockdown_connect();
NSString *color = lockdown_copy_value(connection, nil, kLockdownDeviceColorKey);
//NSLog(@"color = %@", color);
lockdown_disconnect(connection);
return color;
}
*/
@end
#endif