-
Notifications
You must be signed in to change notification settings - Fork 1
/
RscMgr.h
187 lines (138 loc) · 4.2 KB
/
RscMgr.h
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
//
// RscMgr.h
//
// Copyright © 2011 Redpark All Rights Reserved
//
#import <Foundation/Foundation.h>
#import <ExternalAccessory/ExternalAccessoryDefines.h>
#import <ExternalAccessory/EAAccessoryManager.h>
#import <ExternalAccessory/EAAccessory.h>
#import <ExternalAccessory/EASession.h>
#include "redparkSerial.h"
enum
{
kMsrCts = 0x01,
kMsrRi = 0x02,
kMsrDsr = 0x04,
kMsrDcd = 0x08,
};
enum {
kRSC_StreamBufferSize = 1024,
kRSC_MaxMessageDataLength = 230,
kRSC_SerialReadBufferSize = 1024,
kRSC_NoPasscode = 0
};
typedef enum DataSizeType
{
kDataSize7 = SERIAL_DATABITS_7,
kDataSize8 = SERIAL_DATABITS_8
} DataSizeType;
typedef enum ParityType
{
kParityNone = SERIAL_PARITY_NONE,
kParityOdd = SERIAL_PARITY_ODD,
kParityEven = SERIAL_PARITY_EVEN
} ParityType;
typedef enum StopBitsType
{
kStopBits1 = STOPBITS_1,
kStopBits2 = STOPBITS_2
} StopBitsType;
typedef struct rscMsgHdr
{
unsigned char hdr1;
unsigned char hdr2;
unsigned char len; // of data following 'cmd'
unsigned char cmd;
} rscMsgHdr;
@protocol RscMgrDelegate;
@interface RscMgr : NSObject <NSStreamDelegate> {
id <RscMgrDelegate> theDelegate;
// EA api variables
EASession *theSession;
EAAccessory *theAccessory;
NSArray *supportedProtocols;
NSString *connectedProtocol;
// rsc port control/info structures
serialPortConfig portConfig;
serialPortStatus portStatus;
serialPortControl portControl;
// EASession stream handling
// for collecting RSC Messages
unsigned char *rxStreamBuffer;
int rxCount;
int rxCountTotal;
int txCountTotal;
int rxRemain;
int readLen;
unsigned char *txStreamBuffer;
int txIn;
int txOut;
int txStreamEmpty;
// internal dtr and rts state bits
int dtrState;
int rtsState;
// serial data buffer
// for collecting serial bytes received from the serial port
UInt8 *serialReadBuffer;
int serialReadIn;
int serialReadOut;
int serialReadBytesAvailable;
BOOL encodingEnabled;
UInt32 thePasscode;
}
- (void) setDelegate:(id <RscMgrDelegate>) delegate;
// Initializes the RscMgr and reigsters for accessory connect/disconnect notifications.
- (id) init;
// establish communication with the Redpark Serial Cable. This call will also
// configure the serial port based on defaults or prior calls to set the port config
// (see setBaud, setDataSize, ...)
- (void) open;
// simple serial port config interface
// can be called anytime (even after open: call)
- (void) setBaud:(int)baud;
- (void) setDataSize:(DataSizeType)dataSize;
- (void) setParity:(ParityType)parity;
- (void) setStopBits:(StopBitsType)stopBits;
// read write serial bytes
- (int) write:(UInt8 *)data Length:(UInt32)length;
- (int) read:(UInt8 *)data Length:(UInt32)length;
- (int) getReadBytesAvailable;
/*
returns a bit field (see redparkSerial.h)
0-3 current modem status bits for CTS, RI, DSR, DCD, 4-7 previous modem status bits
MODEM_STAT_CTS 0x01
MODEM_STAT_RI 0x02
MODEM_STAT_DSR 0x04
MODEM_STAT_DCD 0x08
*/
- (int) getModemStatus;
// returns true if DTR is asserted
- (BOOL) getDtr;
// returns true if RTS is asserted
- (BOOL) getRts;
// set DTR state
- (void) setDtr:(BOOL)enable;
// set RTS state
- (void) setRts:(BOOL)enable;
// advanced (full) serial port config interface (see redparkSerial.h)
- (void) setPortConfig:(serialPortConfig *)config RequestStatus:(BOOL)reqStatus;
- (void) setPortControl:(serialPortControl *)control RequestStatus:(BOOL)reqStatus;
- (void) getPortConfig:(serialPortConfig *) portConfig;
- (void) getPortStatus:(serialPortStatus *) portStatus;
// advanced advanced
// write a raw message
- (int) writeRscMessage:(int)cmd Length:(int)len MsgData:(UInt8 *)msgData;
@end
@protocol RscMgrDelegate <NSObject>
// Redpark Serial Cable has been connected and/or application moved to foreground.
// protocol is the string which matched from the protocol list passed to initWithProtocol:
- (void) cableConnected:(NSString *)protocol;
// Redpark Serial Cable was disconnected and/or application moved to background
- (void) cableDisconnected;
// serial port status has changed
// user can call getModemStatus or getPortStatus to get current state
- (void) portStatusChanged;
// bytes are available to be read (user calls read:)
- (void) readBytesAvailable:(UInt32)length;
@end