-
Notifications
You must be signed in to change notification settings - Fork 0
/
BluntServer.h
241 lines (189 loc) · 5.83 KB
/
BluntServer.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
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
#ifndef __BLUNTSERVER_H
#define __BLUNTSERVER_H
#include <UserTasks.h>
#include <SerialChipV2.h>
#include "Definitions.h"
#include "EventsCommands.h"
#include "CircleBuf.h"
#include "Logger.h"
class BluntServer;
class Handler;
class HCI;
class L2CAP;
class RFCOMM;
class SDP;
#define DRIVER_GENERIC 0
#define DRIVER_PICO 1
#define DRIVER_TAIYO_YUDEN 2
#define DRIVER_BT2000 3
#define DRIVER_BT2000E 4
#define DRIVER_950_7MHZ 5
#define DRIVER_950_14MHZ 6
#define DRIVER_950_18MHZ 7
#define DRIVER_950_32MHZ 8
enum {
M_DATA,
M_COMMAND,
M_QUIT,
M_TIMER
};
typedef enum {
CONN_RESET,
CONN_IDLE,
CONN_BIND,
CONN_CONNECT,
CONN_DISCONNECT,
CONN_LISTEN, // 5
CONN_ACCEPT,
CONN_CONNECTED,
CONN_CLOSE,
CONN_PAIR_INCOMING,
CONN_PAIR_OUTGOING, // 10
CONN_DISCOVER,
CONN_SDP,
CONN_REMOVE
} ConnectionState;
typedef enum {
H_SERVER,
H_HCI,
H_L2CAP,
H_SDP,
H_RFCOMM
} HandlerType;
struct DiscoveredDevice {
UByte fBdAddr[6];
UByte fPageScanRepetitionMode;
UByte fPageScanPeriodMode;
UByte fPageScanMode;
UByte fClass[3];
UByte fName[249];
};
// ================================================================================
// ¥ TSerialChip16450
// ================================================================================
class TSerialChip16450
{
public:
void WriteSerReg (ULong, UByte);
};
// ================================================================================
// ¥ TProtocolHander classes
// ================================================================================
class Handler
{
public:
Long fLogLevel;
BluntServer *fServer;
Handler *fParentHandler;
Handler *fHandlers[MAX_HANDLERS];
Long fNumHandlers;
TObjectId fTool;
HandlerType fHandlerType;
int fState;
ConnectionState fConnectionState;
Handler ();
~Handler ();
void AddHandler (Handler *handler, BluntServer *server);
void RemoveHandler (Handler *handler);
virtual void HCIClearToSend (Boolean isClear);
void Print (int depth);
void SetLogLevel (Byte level[6]);
void Log (Long logLevel, char* format, ...);
};
// ================================================================================
// ¥ BluntServer
// ================================================================================
class BluntServer: public TUTaskWorld
{
public:
BluntServer* fServer;
Long fLogLevel;
Byte fDefaultLogLevel[5];
TSerialChip* fChip;
Logger* fLogger;
ULong fLastRead;
ULong fRxStatus;
ULong fRxErrorStatus;
ULong fRxBufFull;
volatile ULong fInputHead;
ULong fInputTail;
ULong fOutputHead;
ULong fOutputTail;
Boolean fRxDMA;
Boolean fTxDMA;
Boolean fBufferOutput;
TExtendedCircleBuf* fRxDMABuffer;
TExtendedCircleBuf* fTxDMABuffer;
UByte* fOutputBuffer;
UByte* fInputBuffer;
TUPort fPort;
TUAsyncMessage fIntMessage;
UByte fMessage[MAX_MESSAGE];
ULong fOutstandingPackets;
ULong fDriver;
ULong fLocation;
ULong fSpeed;
Handler **fHandlers;
Long fNumHandlers;
Long fSemaphore;
Long fDebug;
ULong fBytesRead;
// Device address
UByte fBdAddr[6];
UByte fName[65];
// Discovery data
DiscoveredDevice *fDiscoveredDevices;
Byte fNumDiscoveredDevices;
Byte fCurrentDevice;
// Services data
ULong *fQueriedServices;
Byte fCurrentService;
Byte fNumQueriedServices;
ConnectionState fConnectionState;
NewtonErr Initialize (ULong location, ULong driver, ULong speed, Long logLevel);
void AddHandler (Handler *handler);
void RemoveHandler (Handler *handler);
void DeferredRemoveHandler (Handler *handler);
void HandleData ();
void HandleCommand (BluntCommand* command);
void HandleTimer (BluntTimerEvent *event);
void SetupProtocolStack (BluntConnectionCommand* command);
NewtonErr PeekInputBuffer (UByte *data, int offset, int len);
NewtonErr ReadInputBuffer (UByte *data, int len);
void ConsumeInputBuffer (int len);
ULong InputBufferCount ();
void ResetInputBuffer ();
Boolean RcvBufferLevelCritical ();
Boolean RcvBufferLevelOk ();
HCI* SetupHCILayer (TObjectId port, UChar* addr, UByte psRepMode, UByte psMode, UByte* linkKey);
L2CAP* SetupL2CAPLayer (TObjectId port, Short protocol, HCI* hci);
SDP* SetupSDPLayer (TObjectId port, L2CAP* l2cap);
RFCOMM* SetupRFCOMMLayer (TObjectId port, UByte comPort, L2CAP* l2cap);
void InitiatePairing (BluntInitiatePairingCommand* command);
void InitiateServiceRequest (BluntServiceRequestCommand* command);
void SendData (BluntDataCommand* command);
void Status ();
virtual ULong GetSizeOf ();
virtual long TaskConstructor ();
virtual void TaskDestructor ();
virtual void TaskMain ();
void DriverReset (void);
void DriverSendDelay (void);
void TxBEmptyIntHandler (void);
void ExtStsIntHandler (void);
void RxCAvailIntHandler (void);
void RxCSpecialIntHandler (void);
void RxDMAIntHandler (RxErrorStatus);
void TxDMAIntHandler ();
void SetPacketStart ();
void BufferOutput (Boolean buffer);
void StartOutput ();
void Output (UByte*, ULong, Boolean packetStart = true);
void SendEvent (BluntEvent* event);
void SetTimer (Handler *handler, int milliSecondDelay, void *userData = NULL);
void Print (void);
void Log (Long logLevel, char* format, ...);
void LogInputBuffer (Long logLevel);
static TObjectId Port (void);
};
#endif