Skip to content

Instantly share code, notes, and snippets.

@JRHeaton
Created October 21, 2011 05:24
Show Gist options
  • Save JRHeaton/1303158 to your computer and use it in GitHub Desktop.
Save JRHeaton/1303158 to your computer and use it in GitHub Desktop.
//
// AMDevice.m
// Can I Jailbreak?
//
// Created by John Heaton on 10/1/11.
// Copyright 2011 GJB Software. All rights reserved.
//
#import "AMDevice.h"
@implementation AMDevice
@synthesize connected;
@synthesize device;
- (id)initWithDevice:(AMDeviceRef)dev {
if((device = dev) != nil && (self = [super init]) != nil) {
AMDeviceRetain(device);
connected = NO;
}
return self;
}
- (BOOL)enterRecovery {
return AMDeviceEnterRecovery(device) == kAMStatusSuccess;
}
- (BOOL)connect {
if(![self isConnected]) {
if(AMDeviceConnect(device) != kAMStatusSuccess)
return NO;
if(!AMDeviceIsPaired(device)) {
if(AMDevicePair(device) != kAMStatusSuccess)
return NO;
}
if(AMDeviceValidatePairing(device) != kAMStatusSuccess)
return NO;
if(AMDeviceStartSession(device) != kAMStatusSuccess)
return NO;
connected = YES;
return connected;
}
return NO;
}
- (void)disconnect {
if([self isConnected]) {
AMDeviceStopSession(device);
AMDeviceUnpair(device);
AMDeviceDisconnect(device);
connected = NO;
}
}
- (NSSocketNativeHandle)startService:(NSString *)serviceName {
NSSocketNativeHandle sock;
if([self isConnected]) {
if(AMDeviceStartService(device, (CFStringRef)serviceName, &sock) != kAMStatusSuccess)
return -1;
return sock;
}
return -1;
}
- (NSString *)UDID {
if(!cachedUDID) {
cachedUDID = (NSString *)AMDeviceCopyDeviceIdentifier(device);
}
return [[cachedUDID copy] autorelease];
}
- (NSString *)name {
return [self copyDeviceValueForDomain:nil name:@"DeviceName"];
}
- (NSString *)boardModel {
return [self copyDeviceValueForDomain:nil name:@"HardwareModel"];
}
- (NSString *)deviceClass {
return [self copyDeviceValueForDomain:nil name:@"DeviceClass"];
}
- (NSString *)color {
return [self copyDeviceValueForDomain:nil name:@"DeviceColor"];
}
- (NSString *)cpuArchitecture {
return [self copyDeviceValueForDomain:nil name:@"CPUArchitecture"];
}
- (NSString *)basebandVersion {
return [self copyDeviceValueForDomain:nil name:@"BasebandVersion"];
}
- (NSString *)firmwareVersion {
return [self copyDeviceValueForDomain:nil name:@"ProductVersion"];
}
- (NSString *)firmwareBuild {
return [self copyDeviceValueForDomain:nil name:@"BuildVersion"];
}
- (NSString *)productType {
return [self copyDeviceValueForDomain:nil name:@"ProductType"];
}
- (NSString *)serialNumber {
return [self copyDeviceValueForDomain:nil name:@"SerialNumber"];
}
- (NSString *)wifiAddress {
return [self copyDeviceValueForDomain:nil name:@"WiFiAddress"];
}
- (NSString *)bluetoothAddress {
return [self copyDeviceValueForDomain:nil name:@"BluetoothAddress"];
}
- (NSString *)phoneNumber {
return [self copyDeviceValueForDomain:nil name:@"PhoneNumber"];
}
- (id)copyDeviceValueForDomain:(NSString *)domain name:(NSString *)name {
NSString *formattedKeyString = [NSString stringWithFormat:@"%@:%@", (!domain ? @"" : domain), (!name ? @"" : name)];
id value = nil;
if(!cachedDeviceValues) {
cachedDeviceValues = [[NSMutableDictionary dictionaryWithCapacity:0] retain];
}
if((value = [cachedDeviceValues objectForKey:formattedKeyString]) == nil) {
value = (id)AMDeviceCopyValue(device, (CFStringRef)domain, (CFStringRef)name);
[cachedDeviceValues setObject:value forKey:formattedKeyString];
}
return [[value copy] autorelease];
}
- (void)dealloc {
if(cachedUDID)
[cachedUDID release];
if(cachedDeviceValues)
[cachedDeviceValues release];
AMDeviceRelease(device);
[self disconnect];
[super dealloc];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment