|
1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 /** |
|
17 @file |
|
18 @internalComponent |
|
19 */ |
|
20 |
|
21 #include "hctltireceiver.h" |
|
22 |
|
23 #include "hctlti.h" |
|
24 #include "hctltiutils.h" |
|
25 |
|
26 #include <d32comm.h> |
|
27 #include <es_prot.h> // For GetLittleEndian methods |
|
28 #include <bluetooth/logger.h> |
|
29 |
|
30 // These files are included to get HCI specification defined constants. |
|
31 #include <bluetooth/hci/hciframe.h> |
|
32 #include <bluetooth/hci/event.h> |
|
33 |
|
34 |
|
35 |
|
36 #ifdef __FLOG_ACTIVE |
|
37 _LIT8(KLogComponent, LOG_COMPONENT_HCTL_TI); |
|
38 #endif |
|
39 |
|
40 |
|
41 CHCTLTiReceiver::CHCTLTiReceiver(CHCTLTi& aHCTLTi,RBusDevComm& aPort) |
|
42 : CActive(EPriorityStandard), |
|
43 iHCTLTi(aHCTLTi), |
|
44 iState(EWaitingForHctlHeaderByte), |
|
45 iReceiveBufPtr(NULL,0), |
|
46 iPort(aPort) |
|
47 { |
|
48 LOG_FUNC |
|
49 CActiveScheduler::Add(this); |
|
50 } |
|
51 |
|
52 CHCTLTiReceiver::~CHCTLTiReceiver() |
|
53 { |
|
54 LOG_FUNC |
|
55 |
|
56 Cancel(); |
|
57 iReceiveBuffer.Close(); |
|
58 |
|
59 HCI_LOG_UNLOAD(this); |
|
60 } |
|
61 |
|
62 CHCTLTiReceiver* CHCTLTiReceiver::NewL(CHCTLTi& aHCTLTi, RBusDevComm& aPort) |
|
63 { |
|
64 LOG_STATIC_FUNC |
|
65 |
|
66 CHCTLTiReceiver* self=new(ELeave)CHCTLTiReceiver(aHCTLTi, aPort); |
|
67 CleanupStack::PushL(self); |
|
68 self->ConstructL(); |
|
69 CleanupStack::Pop(); |
|
70 return self; |
|
71 } |
|
72 |
|
73 void CHCTLTiReceiver::ConstructL() |
|
74 { |
|
75 LOG_FUNC |
|
76 |
|
77 HCI_LOG_LOADL(this, KHCILoggerDatalinkTypeH4); |
|
78 |
|
79 // Create the receive buffer |
|
80 HBufC8* buf = HBufC8::NewMaxL(KHCTLRecvBufSize); |
|
81 |
|
82 // HBufC8 is not modifiable. Create a RBuf8 object that |
|
83 // allows the data to be modified. |
|
84 iReceiveBuffer.Assign(buf); |
|
85 } |
|
86 |
|
87 void CHCTLTiReceiver::QueueReadForNextFrame(TUint16 aOffset, TUint16 aBytesRequired) |
|
88 { |
|
89 LOG_FUNC |
|
90 |
|
91 __ASSERT_DEBUG(!IsActive(), PANIC(KTiPanic, EPortReadAttemptWhenReadOutstanding)); |
|
92 __ASSERT_DEBUG(aBytesRequired != 0, PANIC(KTiPanic, EAttemptToReadZeroBytes)); |
|
93 __ASSERT_DEBUG(aOffset + aBytesRequired <= iReceiveBuffer.MaxLength(), PANIC(KTiPanic, EHctlReceiverBufferOverflow)); |
|
94 |
|
95 SetActive(); |
|
96 |
|
97 // Read the required number of bytes into the buffer starting at the specified offset. |
|
98 iReceiveBufPtr.Set(iReceiveBuffer.MidTPtr(aOffset, aBytesRequired)); |
|
99 iPort.Read(iStatus, iReceiveBufPtr, aBytesRequired); |
|
100 } |
|
101 |
|
102 void CHCTLTiReceiver::ProcessData() |
|
103 { |
|
104 LOG_FUNC |
|
105 |
|
106 TUint16 bytesRequiredForNextRead = 0; |
|
107 TUint16 currentReadOffset = 0; |
|
108 |
|
109 switch (iState) |
|
110 { |
|
111 case EWaitingForHctlHeaderByte: |
|
112 { |
|
113 // reads the first byte of the Packet, to decide the |
|
114 // type of Packet and set the next state |
|
115 __ASSERT_ALWAYS(iReceiveBufPtr.Length() == KHctlHeaderSize, |
|
116 PANIC(KTiPanic, EReadCompletedWithInsufficientBytes)); |
|
117 |
|
118 // Store the HCI packet. |
|
119 iCurrentHCIPacketType = iReceiveBufPtr[KHctlPacketTypeOffset]; |
|
120 iState = EWaitingForHciHeader; |
|
121 |
|
122 // Request the appropriate number of header bytes. The HCI packet |
|
123 // type will be overwritten when the HCI header bytes are read. |
|
124 switch(iCurrentHCIPacketType) |
|
125 { |
|
126 case CHCTLTi::EACLDataPacket: |
|
127 { |
|
128 HCI_LOG_FRAME(this, |
|
129 KHCILoggerControllerToHost | KHCILoggerACLDataFrame | KHCILoggerFrameFragmented, |
|
130 iReceiveBufPtr.Left(KHctlHeaderSize)); |
|
131 |
|
132 bytesRequiredForNextRead = CHctlAclDataFrame::KHCIACLDataPacketHeaderLength; |
|
133 } |
|
134 break; |
|
135 |
|
136 case CHCTLTi::ESynchronousDataPacket: |
|
137 { |
|
138 HCI_LOG_FRAME(this, |
|
139 KHCILoggerControllerToHost | KHCILoggerSynchronousDataFrame | KHCILoggerFrameFragmented, |
|
140 iReceiveBufPtr.Left(KHctlHeaderSize)); |
|
141 |
|
142 bytesRequiredForNextRead = CHctlSynchronousDataFrame::KHCISynchronousDataPacketHeaderLength; |
|
143 } |
|
144 break; |
|
145 |
|
146 case CHCTLTi::EEventPacket: |
|
147 { |
|
148 HCI_LOG_FRAME(this, |
|
149 KHCILoggerControllerToHost | KHCILoggerCommandOrEvent | KHCILoggerFrameFragmented, |
|
150 iReceiveBufPtr.Left(KHctlHeaderSize)); |
|
151 |
|
152 bytesRequiredForNextRead = THCIEventBase::KEventCommonFieldsLength; |
|
153 } |
|
154 break; |
|
155 |
|
156 default: |
|
157 // unexpected/unsupported data Received |
|
158 iState = EInvalidDataReceived; |
|
159 break; |
|
160 }; |
|
161 } |
|
162 break; |
|
163 |
|
164 case EWaitingForHciHeader: |
|
165 { |
|
166 iState = EWaitingForHciPayload; |
|
167 |
|
168 // Read the packet length. |
|
169 switch(iCurrentHCIPacketType) |
|
170 { |
|
171 case CHCTLTi::EACLDataPacket: |
|
172 { |
|
173 __ASSERT_ALWAYS(iReceiveBufPtr.Length() == CHctlAclDataFrame::KHCIACLDataPacketHeaderLength, |
|
174 PANIC(KTiPanic, EReadCompletedWithInsufficientBytes)); |
|
175 |
|
176 bytesRequiredForNextRead = LittleEndian::Get16(iReceiveBufPtr.Ptr() + CHctlDataFrameBase::KHCIDataPacketLengthFieldOffset); |
|
177 |
|
178 // Check that the size of the ACL data does not exceed the internal buffer. |
|
179 if(bytesRequiredForNextRead > (KHCTLRecvBufSize - CHctlAclDataFrame::KHCIACLDataPacketHeaderLength)) |
|
180 { |
|
181 // This is unexpected from the controller. Try restarting it. |
|
182 iState = EInvalidDataReceived; |
|
183 } |
|
184 else |
|
185 { |
|
186 HCI_LOG_FRAME(this, |
|
187 KHCILoggerControllerToHost | KHCILoggerACLDataFrame | KHCILoggerFrameFragmented, |
|
188 iReceiveBufPtr.Left(CHctlAclDataFrame::KHCIACLDataPacketHeaderLength)); |
|
189 currentReadOffset = CHctlAclDataFrame::KHCIACLDataPacketHeaderLength; |
|
190 } |
|
191 } |
|
192 break; |
|
193 |
|
194 case CHCTLTi::ESynchronousDataPacket: |
|
195 { |
|
196 __ASSERT_ALWAYS(iReceiveBufPtr.Length() == CHctlSynchronousDataFrame::KHCISynchronousDataPacketHeaderLength, |
|
197 PANIC(KTiPanic, EReadCompletedWithInsufficientBytes)); |
|
198 |
|
199 bytesRequiredForNextRead = iReceiveBufPtr[CHctlDataFrameBase::KHCIDataPacketLengthFieldOffset]; |
|
200 HCI_LOG_FRAME(this, |
|
201 KHCILoggerControllerToHost | KHCILoggerSynchronousDataFrame | KHCILoggerFrameFragmented, |
|
202 iReceiveBufPtr.Left(CHctlSynchronousDataFrame::KHCISynchronousDataPacketHeaderLength)); |
|
203 currentReadOffset = CHctlSynchronousDataFrame::KHCISynchronousDataPacketHeaderLength; |
|
204 } |
|
205 break; |
|
206 |
|
207 case CHCTLTi::EEventPacket: |
|
208 { |
|
209 __ASSERT_ALWAYS(iReceiveBufPtr.Length() == THCIEventBase::KEventCommonFieldsLength, |
|
210 PANIC(KTiPanic, EReadCompletedWithInsufficientBytes)); |
|
211 |
|
212 bytesRequiredForNextRead = iReceiveBufPtr[THCIEventBase::KTotalParameterLengthOffset]; |
|
213 HCI_LOG_FRAME(this, |
|
214 KHCILoggerControllerToHost | KHCILoggerCommandOrEvent | KHCILoggerFrameFragmented, |
|
215 iReceiveBufPtr.Left(THCIEventBase::KEventCommonFieldsLength)); |
|
216 currentReadOffset = THCIEventBase::KEventCommonFieldsLength; |
|
217 } |
|
218 break; |
|
219 |
|
220 default: |
|
221 { |
|
222 // Invalid state. |
|
223 PANIC(KTiPanic, EIllegalState); |
|
224 break; |
|
225 } |
|
226 }; |
|
227 } |
|
228 break; |
|
229 |
|
230 case EWaitingForHciPayload: |
|
231 { |
|
232 TUint16 payloadLength = iReceiveBufPtr.Length(); |
|
233 switch(iCurrentHCIPacketType) |
|
234 { |
|
235 case CHCTLTi::EACLDataPacket: |
|
236 { |
|
237 HCI_LOG_FRAME(this, |
|
238 KHCILoggerControllerToHost | KHCILoggerACLDataFrame, |
|
239 iReceiveBufPtr); |
|
240 iHCTLTi.ProcessACLData(iReceiveBuffer.Left(payloadLength + CHctlAclDataFrame::KHCIACLDataPacketHeaderLength)); |
|
241 } |
|
242 break; |
|
243 |
|
244 case CHCTLTi::ESynchronousDataPacket: |
|
245 { |
|
246 HCI_LOG_FRAME(this, |
|
247 KHCILoggerControllerToHost | KHCILoggerSynchronousDataFrame, |
|
248 iReceiveBufPtr); |
|
249 iHCTLTi.ProcessSynchronousData(iReceiveBuffer.Left(payloadLength + CHctlSynchronousDataFrame::KHCISynchronousDataPacketHeaderLength)); |
|
250 } |
|
251 break; |
|
252 |
|
253 case CHCTLTi::EEventPacket: |
|
254 { |
|
255 HCI_LOG_FRAME(this, |
|
256 KHCILoggerControllerToHost | KHCILoggerCommandOrEvent, |
|
257 iReceiveBufPtr); |
|
258 iHCTLTi.ProcessEvent(iReceiveBuffer.Left(payloadLength + THCIEventBase::KEventCommonFieldsLength)); |
|
259 } |
|
260 break; |
|
261 |
|
262 default: |
|
263 // Invalid state |
|
264 PANIC(KTiPanic, EIllegalState); |
|
265 }; |
|
266 |
|
267 // Starting a new Packet. Bytes Required now is 1, offset is 0 |
|
268 // We finished with this packet so we're back in 'wait for next mode' |
|
269 currentReadOffset = 0; |
|
270 bytesRequiredForNextRead = KHctlHeaderSize; |
|
271 iState = EWaitingForHctlHeaderByte; |
|
272 } |
|
273 break; |
|
274 |
|
275 default: |
|
276 // must never get here |
|
277 PANIC(KTiPanic, EIllegalState); |
|
278 break; |
|
279 } |
|
280 |
|
281 if(iState == EInvalidDataReceived) |
|
282 { |
|
283 // The HCTL can not recover from this. Reset the controller and restart the host. |
|
284 iHCTLTi.MhriStartHardReset(); |
|
285 } |
|
286 else |
|
287 { |
|
288 // Request the next read on the port. |
|
289 QueueReadForNextFrame(currentReadOffset, bytesRequiredForNextRead); |
|
290 } |
|
291 } |
|
292 |
|
293 |
|
294 void CHCTLTiReceiver::RunL() |
|
295 { |
|
296 LOG_FUNC |
|
297 LOG1(_L8("\tiStatus = %d"), iStatus.Int()); |
|
298 |
|
299 // Only process the read if it has completed successfully. |
|
300 if (iStatus.Int() == KErrNone) |
|
301 { |
|
302 ProcessData(); |
|
303 } |
|
304 else |
|
305 { |
|
306 // The HCTL can not recover from this. Reset the controller and restart the host. |
|
307 iHCTLTi.MhriStartHardReset(); |
|
308 } |
|
309 } |
|
310 |
|
311 void CHCTLTiReceiver::DoCancel() |
|
312 { |
|
313 LOG_FUNC |
|
314 |
|
315 // Cancel the outstanding read request. |
|
316 iPort.ReadCancel(); |
|
317 } |
|
318 |
|
319 void CHCTLTiReceiver::Start() |
|
320 { |
|
321 LOG_FUNC |
|
322 __ASSERT_DEBUG(!IsActive(), PANIC(KTiPanic, EStartCalledWhenReadOutstanding)); |
|
323 |
|
324 // Reset this object state and make an initial read on the UART. |
|
325 iState = EWaitingForHctlHeaderByte; |
|
326 |
|
327 // Read first byte on next frame (HCI packet type) into offset zero of the buffer. |
|
328 QueueReadForNextFrame(0, KHctlHeaderSize); |
|
329 } |