halfbyte / stickler

A replacement for stupid software provided by mobile providers

This URL has Read+Write access

stickler / AppController.m
100644 90 lines (75 sloc) 2.464 kb
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
//
// AppController.m
// Stickler
//
// Created by Jan Krutisch on 01.02.09.
// Copyright 2009 mindmatters GmbH & Co. KG. All rights reserved.
//
 
#import "AppController.h"
#import "AMSerialPortList.h"
#import "AMSerialPortAdditions.h"
 
 
@implementation AppController
 
- (void)awakeFromNib
{
// register for port add/remove notification
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didAddPorts:) name:AMSerialPortListDidAddPortsNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRemovePorts:) name:AMSerialPortListDidRemovePortsNotification object:nil];
[AMSerialPortList sharedPortList]; // initialize port list to arm notifications
// initializing the fields
[operatorName setStringValue:@"--"];
[signalStrength setIntValue:0];
[self initPort];
if ([port open]) {
[port writeString:@"AT+CPIN?\r\n" usingEncoding:NSUTF8StringEncoding error:NULL];
}
}
 
 
- (AMSerialPort *)port
{
    return port;
}
 
- (void)setPort:(AMSerialPort *)newPort
{
    id old = nil;
 
    if (newPort != port) {
        old = port;
        port = [newPort retain];
        [old release];
    }
}
 
 
- (void)initPort {
NSString *deviceName = @"/dev/cu.HUAWEIMobile-Pcui";
if (![deviceName isEqualToString:[port bsdPath]]) {
[port close];
 
[self setPort:[[[AMSerialPort alloc] init:deviceName withName:deviceName type:(NSString*)CFSTR(kIOSerialBSDModemType)] autorelease]];
 
// register as self as delegate for port
[port setDelegate:self];
if ([port open]) {
[port readDataInBackground];
} else { // an error occured while creating port
[self setPort:nil];
}
}
}
 
- (void)serialPortReadData:(NSDictionary *)dataDictionary {
// this method is called if data arrives
// @"data" is the actual data, @"serialPort" is the sending port
AMSerialPort *sendPort = [dataDictionary objectForKey:@"serialPort"];
NSData *data = [dataDictionary objectForKey:@"data"];
NSLog(@"received %@", [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]);
if ([data length] > 0) {
// continue listening
[sendPort readDataInBackground];
} else { // port closed
}
}
 
- (void)didAddPorts:(NSNotification *)theNotification
{
// dunno what to do here
NSLog(@"did add ports: %@", theNotification);
}
- (void)didRemovePorts:(NSNotification *)theNotification
{
// dunno what to do here
NSLog(@"did remove ports: %@", theNotification);
}
@end