forked from bps/scrobblepod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MobileDeviceSupport.m
337 lines (277 loc) · 11.5 KB
/
MobileDeviceSupport.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
//
// MobileDeviceSupport.m
// iScrobbler
//
// Created by Brian Bergstrand on 7/12/2007.
// Copyright 2008 Brian Bergstrand.
//
// Released under the GPL, license details available in res/gpl.txt
//
#import <sys/errno.h>
#import <dlfcn.h>
#import <mach/error.h>
#import <IOKit/IOMessage.h>
#import <IOKit/usb/IOUSBLib.h>
#import <Foundation/Foundation.h>
// Private MobileDevice framework support
// Device notification types
enum {
kDeviceNotificationConnected = 1,
kDeviceNotificationDisconnected = 2,
};
enum {
kProduct_iPodTouch = 4753,
kProduct_iPodTouch2G = 4755,
kProduct_iPodTouch3G = 4761,
kProduct_iPodTouch4G = 4766,
kProduct_iPhone = 4752,
kProduct_iPhone3G = 4754,
kProduct_iPhone3GS = 4756,
kProduct_iPhone4 = 4759,
kProduct_iPhone4CDMA = 4764,
kProduct_iPad = 4762,
kProduct_iPad2WiFi = 4767,
kProduct_iPad2GSM = 4770,
kProduct_iPad2CDMA = 4771,
};
struct AMDevice
{
// Thanks to iFunbox.dev
unsigned int unknown_header[2];
unsigned int unknown0[4];
unsigned int deviceID;
unsigned int unknown5;
unsigned int productID;
char *serial;
unsigned int lockdown_conn;
unsigned char unknown3[8];
unsigned char unknown4[6*16+1];
unsigned char padding[8];
unsigned char safe_extending[256];
} __attribute__ ((packed));
struct AMDeviceCallbackInfo {
struct AMDevice *device; // device description
unsigned int type; // notification type
} __attribute__ ((packed));
struct AMDeviceNotification {
unsigned int unknown0;
unsigned int unknown1;
unsigned int unknown2;
void *callback;
unsigned int unknown3;
} __attribute__ ((packed));
struct AMDeviceConnection {
unsigned char private[44];
} __attribute__ ((packed));
typedef void(*AMDeviceNotificationCallback)(struct AMDeviceCallbackInfo *);
typedef mach_error_t (*AMDeviceNotificationSubscribe)(AMDeviceNotificationCallback callback,
unsigned int unknown, unsigned int unknown1, unsigned int unkonwn3,
struct AMDeviceNotification **notification);
typedef CFStringRef (*AMDeviceCopyDeviceIdentifier)(struct AMDevice *);
typedef void* (*AMDeviceCopyValue)(struct AMDevice *, int unknown, CFStringRef str);
// Private MobileDevice framework support
#if !defined(__LP64__)
static void DeviceNotificationCallback_(struct AMDeviceCallbackInfo *info);
static void *libHandle = nil;
static void CFHandleCallback(CFNotificationCenterRef center, void *observer, CFStringRef name,
const void *object, CFDictionaryRef userInfo);
NSMutableDictionary *devicesAttachedBeforeLaunch = nil;
static NSMutableDictionary* FindAttachedDevices(void);
#endif
__private_extern__
int IntializeMobileDeviceSupport(const char *path, void **handle)
{
int err;
#if !defined(__LP64__)
if (libHandle) {
if (handle)
*handle = NULL;
return (0);
}
devicesAttachedBeforeLaunch = [FindAttachedDevices() retain];
if ((libHandle = dlopen(path, RTLD_LAZY|RTLD_LOCAL))) {
AMDeviceNotificationSubscribe subscribe;
if ((subscribe = dlsym(libHandle, "AMDeviceNotificationSubscribe"))) {
struct AMDeviceNotification *note;
err = subscribe(DeviceNotificationCallback_, 0, 0, 0, ¬e);
// Don't know how to get a sync start/finished notification from the MB framework
// so rely on sync services notifications.
// XXX: do these occur if the user turned off all shared data syncing (mail/contacts/etc)?
CFNotificationCenterAddObserver(CFNotificationCenterGetDistributedCenter(), NULL,
CFHandleCallback, CFSTR("com.apple.syncservices.iPodSync.SyncStatusChangedNotification"), NULL, 0);
} else {
dlclose(libHandle);
libHandle = NULL;
err = cfragNoSymbolErr;
}
} else
err = cfragNoLibraryErr;
if (handle)
*handle = NULL; // XXX: for now don't publish the handle
#else
err = cfragFragmentFormatErr; // MobileDevice framework is 32bit only (as of iTunes 7.7)
#endif
if (err) {
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"org.bergstrand.amds.intializeDidFail"
object:[NSNumber numberWithInt:err]];
}
return (err);
}
#if !defined(__LP64__)
static NSMutableDictionary *connectedDevices = nil;
static void CFHandleCallback(CFNotificationCenterRef center, void *observer, CFStringRef name,
const void *object, CFDictionaryRef userInfo)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
@try {
if ([@"com.apple.syncservices.iPodSync.SyncStatusChangedNotification" isEqualToString:(id)name]) {
NSString *serial = [(id)object lowercaseString];
NSDictionary *d;
if (nil == (d = [connectedDevices objectForKey:serial]))
return;
NSString *state = [(id)userInfo objectForKey:@"State"];
if ([state isEqualToString:@"Finished"]) {
[[NSDistributedNotificationCenter defaultCenter]
postNotificationName:@"org.bergstrand.amds.syncDidFinish"
object:serial userInfo:d];
} else if ([state isEqualToString:@"Starting"]) {
[[NSDistributedNotificationCenter defaultCenter]
postNotificationName:@"org.bergstrand.amds.syncDidStart"
object:serial userInfo:d];
}
}
} @catch(NSException *e) {}
[pool drain];
}
static void DeviceNotificationCallback_(struct AMDeviceCallbackInfo *info)
{
if (!info)
return;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
@try {
if (!connectedDevices)
connectedDevices = [[NSMutableDictionary alloc] init];
NSDictionary *productNameMap = [NSDictionary dictionaryWithObjectsAndKeys:
@"iPod Touch", [NSNumber numberWithUnsignedInt:kProduct_iPodTouch],
@"iPod Touch 2G", [NSNumber numberWithUnsignedInt:kProduct_iPodTouch2G],
@"iPod Touch 3G", [NSNumber numberWithUnsignedInt:kProduct_iPodTouch3G],
@"iPod Touch 4G", [NSNumber numberWithUnsignedInt:kProduct_iPodTouch4G],
@"iPhone", [NSNumber numberWithUnsignedInt:kProduct_iPhone],
@"iPhone 3G", [NSNumber numberWithUnsignedInt:kProduct_iPhone3G],
@"iPhone 3GS", [NSNumber numberWithUnsignedInt:kProduct_iPhone3GS],
@"iPhone 4", [NSNumber numberWithUnsignedInt:kProduct_iPhone4],
@"iPhone 4", [NSNumber numberWithUnsignedInt:kProduct_iPhone4CDMA],
@"iPad", [NSNumber numberWithUnsignedInt:kProduct_iPad],
@"iPad 2", [NSNumber numberWithUnsignedInt:kProduct_iPad2WiFi],
@"iPad 2", [NSNumber numberWithUnsignedInt:kProduct_iPad2GSM],
@"iPad 2", [NSNumber numberWithUnsignedInt:kProduct_iPad2CDMA],
nil];
NSString *deviceName;
AMDeviceCopyDeviceIdentifier copyID = dlsym(libHandle, "AMDeviceCopyDeviceIdentifier");
#ifdef notyet
// CopyValue requires a connection to the device
AMDeviceCopyValue copyVal = dlsym(libHandle, "AMDeviceCopyValue");
CFStringRef kAMDDeviceName = dlsym(libHandle, "kAMDDeviceName");
if (copyVal && kAMDDeviceName && info->device)
deviceName = [(id)copyVal(info->device, 0, kAMDDeviceName) autorelease];
else
#endif
deviceName = @"";
NSString *serial;
if (copyID && info->device && (serial = (id)copyID(info->device))) {
serial = [[serial autorelease] lowercaseString];
} else {
serial = info->device ? [[NSString stringWithUTF8String:info->device->serial] lowercaseString] : @"";
}
NSNumber *productID = info->device ? [NSNumber numberWithUnsignedInt:info->device->productID] : (id)@"";
NSString *productName = [productNameMap objectForKey:productID];
if (!productName)
productName = @"Unknown Mobile Device";
BOOL connectedBeforeLaunch = nil != [devicesAttachedBeforeLaunch objectForKey:serial];
if (connectedBeforeLaunch)
[devicesAttachedBeforeLaunch removeObjectForKey:serial];
NSDictionary *d = [NSDictionary dictionaryWithObjectsAndKeys:
serial, @"serial",
[NSNumber numberWithBool:connectedBeforeLaunch], @"connectedBeforeLaunch",
deviceName, @"name",
productName, @"product",
productID, @"productID",
#if defined(ISDEBUG) || defined(DEBUG)
[NSNumber numberWithUnsignedInt:info->type], @"eventID",
#endif
nil];
#if defined(ISDEBUG) || defined(DEBUG)
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"org.bergstrand.amds.debug"
object:serial userInfo:d];
#endif
switch (info->type) {
case kDeviceNotificationConnected:
[connectedDevices setObject:d forKey:serial];
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"org.bergstrand.amds.connect"
object:serial userInfo:d];
break;
case kDeviceNotificationDisconnected:
[connectedDevices removeObjectForKey:serial];
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"org.bergstrand.amds.disconnect"
object:serial userInfo:d];
break;
default:
break;
}
} @catch(NSException *e) {}
[pool drain];
}
static BOOL IsMassStorageDevice(io_object_t entry)
{
io_iterator_t i;
BOOL canMount = NO;
kern_return_t kr = IORegistryEntryGetChildIterator(entry, kIOServicePlane, &i);
if (0 == kr && 0 != i) {
io_object_t iobj;
while ((iobj = IOIteratorNext(i))) {
// the iterator is for 1st generation children only, to get all descendants we have to go recursive
if (IOObjectConformsTo(iobj, "IOUSBMassStorageClass") || IsMassStorageDevice(iobj)) {
canMount = YES;
IOObjectRelease(iobj);
break;
}
IOObjectRelease(iobj);
}
IOObjectRelease(i);
}
return (canMount);
}
static NSMutableDictionary* FindAttachedDevices(void)
{
NSMutableDictionary *devices = [NSMutableDictionary dictionary];
io_iterator_t i;
kern_return_t kr = IOServiceGetMatchingServices(kIOMasterPortDefault, IOServiceMatching(kIOUSBDeviceClassName), &i);
if (0 == kr && 0 != i) {
io_object_t iobj;
while ((iobj = IOIteratorNext(i))) {
NSDictionary *properties;
IORegistryEntryCreateCFProperties(iobj, (CFMutableDictionaryRef*)&properties, kCFAllocatorDefault, 0);
if (properties) {
// XXX: hack!
NSString *productName = [properties objectForKey:@"USB Product Name"];
NSString *productID = [properties objectForKey:@"idProduct"];
NSString *serial = [[properties objectForKey:@"USB Serial Number"] lowercaseString];
if (productID && productName && (NSOrderedSame == [productName caseInsensitiveCompare:@"iPhone"]
|| (NSOrderedSame == [productName caseInsensitiveCompare:@"iPod"]
&& NO == IsMassStorageDevice(iobj)))) {
NSDictionary *d = [NSDictionary dictionaryWithObjectsAndKeys:
productName, @"product",
serial, @"serial",
productID, @"productID",
nil];
[devices setObject:d forKey:serial];
}
[properties release];
}
IOObjectRelease(iobj);
}
IOObjectRelease(i);
}
return (devices);
}
#endif // LP64