Skip to content

Instantly share code, notes, and snippets.

@crespoxiao
Forked from shenqiliang/baseband
Last active August 29, 2015 13:57
Show Gist options
  • Save crespoxiao/9425988 to your computer and use it in GitHub Desktop.
Save crespoxiao/9425988 to your computer and use it in GitHub Desktop.
//http://blog.xcodev.com/archives/iphone-baseband-intro/
#import <Foundation/Foundation.h>
#import <termios.h>
#import <time.h>
#import <sys/ioctl.h>
NSString *sendATCommand(NSFileHandle *baseBand, NSString *atCommand){
NSLog(@"SEND AT: %@", atCommand);
[baseBand writeData:[atCommand dataUsingEncoding:NSASCIIStringEncoding]];
NSMutableString *result = [NSMutableString string];
NSData *resultData = [baseBand availableData];
while ([resultData length]) {
[result appendString:[[NSString alloc] initWithData:resultData encoding:NSASCIIStringEncoding]];
if ([result hasSuffix:@"OK\r\n"]||[result hasSuffix:@"ERROR\r\n"]) {
NSLog(@"RESULT: %@", result);
return [NSString stringWithString:result];
}
else{
resultData = [baseBand availableData];
}
}
return nil;
}
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSFileHandle *baseBand = [NSFileHandle fileHandleForUpdatingAtPath:@"/dev/dlci.spi-baseband.extra_0"];
if (baseBand == nil) {
NSLog(@"Can't open baseband.");
}
int fd = [baseBand fileDescriptor];
ioctl(fd, TIOCEXCL);
fcntl(fd, F_SETFL, 0);
static struct termios term;
tcgetattr(fd, &term);
cfmakeraw(&term);
cfsetspeed(&term, 115200);
term.c_cflag = CS8 | CLOCAL | CREAD;
term.c_iflag = 0;
term.c_oflag = 0;
term.c_lflag = 0;
term.c_cc[VMIN] = 0;
term.c_cc[VTIME] = 0;
tcsetattr(fd, TCSANOW, &term);
//IMEI
NSString *result = sendATCommand(baseBand, @"AT+CGSN\r");
//IMSI
result = sendATCommand(baseBand, @"AT+CIMI\r");
//ICCID
result = sendATCommand(baseBand, @"AT+CCID\r");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment