|
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 "Broadcom_Hctl_H4_receiver.h" |
|
22 |
|
23 #include "Broadcom_Hctl_H4.h" |
|
24 #include "Broadcom_Hctl_H4_utils.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_BCM_H4); |
|
38 #endif |
|
39 |
|
40 |
|
41 CHCTLBcmH4Receiver::CHCTLBcmH4Receiver(CHCTLBcmH4& aHCTLBcmH4,RBusDevComm& aPort) |
|
42 : CActive(EPriorityStandard), |
|
43 iHCTLBcmH4(aHCTLBcmH4), |
|
44 iState(EWaitingForHctlHeaderByte), |
|
45 iReceiveBufPtr(NULL,0), |
|
46 iPort(aPort) |
|
47 { |
|
48 LOG_FUNC |
|
49 CActiveScheduler::Add(this); |
|
50 } |
|
51 |
|
52 CHCTLBcmH4Receiver::~CHCTLBcmH4Receiver() |
|
53 { |
|
54 LOG_FUNC |
|
55 |
|
56 Cancel(); |
|
57 iReceiveBuffer.Close(); |
|
58 |
|
59 HCI_LOG_UNLOAD(this); |
|
60 } |
|
61 |
|
62 CHCTLBcmH4Receiver* CHCTLBcmH4Receiver::NewL(CHCTLBcmH4& aHCTLBcmH4, RBusDevComm& aPort) |
|
63 { |
|
64 LOG_STATIC_FUNC |
|
65 |
|
66 CHCTLBcmH4Receiver* self=new(ELeave)CHCTLBcmH4Receiver(aHCTLBcmH4, aPort); |
|
67 CleanupStack::PushL(self); |
|
68 self->ConstructL(); |
|
69 CleanupStack::Pop(); |
|
70 return self; |
|
71 } |
|
72 |
|
73 void CHCTLBcmH4Receiver::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 CHCTLBcmH4Receiver::QueueReadForNextFrame(TUint16 aOffset, TUint16 aBytesRequired) |
|
88 { |
|
89 LOG_FUNC |
|
90 |
|
91 __ASSERT_DEBUG(!IsActive(), PANIC(KBcmHctlH4Panic, EPortReadAttemptWhenReadOutstanding)); |
|
92 __ASSERT_DEBUG(aBytesRequired != 0, PANIC(KBcmHctlH4Panic, EAttemptToReadZeroBytes)); |
|
93 __ASSERT_DEBUG(aOffset + aBytesRequired <= iReceiveBuffer.MaxLength(), PANIC(KBcmHctlH4Panic, 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 CHCTLBcmH4Receiver::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(KBcmHctlH4Panic, 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 CHCTLBcmH4::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 CHCTLBcmH4::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 CHCTLBcmH4::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 /////////////////////////////////////// |
|
158 //CHRIS MODIF |
|
159 if((iHCTLBcmH4.iInitpluginState == EMiniDrvToHcdConfigFileDelay) || (iHCTLBcmH4.iInitpluginState == EMiniDrvToBinConfigFileDelay)) |
|
160 { |
|
161 if((iCurrentHCIPacketType == 0x34) || (iCurrentHCIPacketType == 0x31) ) |
|
162 { |
|
163 bytesRequiredForNextRead = 1; |
|
164 iState = EWaitingForHctlHeaderByte; |
|
165 break; |
|
166 } |
|
167 } |
|
168 else |
|
169 { |
|
170 ///////////////////////////////////////// |
|
171 // unexpected/unsupported data Received |
|
172 iState = EInvalidDataReceived; |
|
173 } |
|
174 break; |
|
175 }; |
|
176 } |
|
177 break; |
|
178 |
|
179 case EWaitingForHciHeader: |
|
180 { |
|
181 iState = EWaitingForHciPayload; |
|
182 |
|
183 // Read the packet length. |
|
184 switch(iCurrentHCIPacketType) |
|
185 { |
|
186 case CHCTLBcmH4::EACLDataPacket: |
|
187 { |
|
188 __ASSERT_ALWAYS(iReceiveBufPtr.Length() == CHctlAclDataFrame::KHCIACLDataPacketHeaderLength, |
|
189 PANIC(KBcmHctlH4Panic, EReadCompletedWithInsufficientBytes)); |
|
190 |
|
191 bytesRequiredForNextRead = LittleEndian::Get16(iReceiveBufPtr.Ptr() + CHctlDataFrameBase::KHCIDataPacketLengthFieldOffset); |
|
192 |
|
193 // Check that the size of the ACL data does not exceed the internal buffer. |
|
194 if(bytesRequiredForNextRead > (KHCTLRecvBufSize - CHctlAclDataFrame::KHCIACLDataPacketHeaderLength)) |
|
195 { |
|
196 // This is unexpected from the controller. Try restarting it. |
|
197 iState = EInvalidDataReceived; |
|
198 } |
|
199 else |
|
200 { |
|
201 HCI_LOG_FRAME(this, |
|
202 KHCILoggerControllerToHost | KHCILoggerACLDataFrame | KHCILoggerFrameFragmented, |
|
203 iReceiveBufPtr.Left(CHctlAclDataFrame::KHCIACLDataPacketHeaderLength)); |
|
204 currentReadOffset = CHctlAclDataFrame::KHCIACLDataPacketHeaderLength; |
|
205 } |
|
206 } |
|
207 break; |
|
208 |
|
209 case CHCTLBcmH4::ESynchronousDataPacket: |
|
210 { |
|
211 __ASSERT_ALWAYS(iReceiveBufPtr.Length() == CHctlSynchronousDataFrame::KHCISynchronousDataPacketHeaderLength, |
|
212 PANIC(KBcmHctlH4Panic, EReadCompletedWithInsufficientBytes)); |
|
213 |
|
214 bytesRequiredForNextRead = iReceiveBufPtr[CHctlDataFrameBase::KHCIDataPacketLengthFieldOffset]; |
|
215 HCI_LOG_FRAME(this, |
|
216 KHCILoggerControllerToHost | KHCILoggerSynchronousDataFrame | KHCILoggerFrameFragmented, |
|
217 iReceiveBufPtr.Left(CHctlSynchronousDataFrame::KHCISynchronousDataPacketHeaderLength)); |
|
218 currentReadOffset = CHctlSynchronousDataFrame::KHCISynchronousDataPacketHeaderLength; |
|
219 } |
|
220 break; |
|
221 |
|
222 case CHCTLBcmH4::EEventPacket: |
|
223 { |
|
224 __ASSERT_ALWAYS(iReceiveBufPtr.Length() == THCIEventBase::KEventCommonFieldsLength, |
|
225 PANIC(KBcmHctlH4Panic, EReadCompletedWithInsufficientBytes)); |
|
226 |
|
227 bytesRequiredForNextRead = iReceiveBufPtr[THCIEventBase::KTotalParameterLengthOffset]; |
|
228 HCI_LOG_FRAME(this, |
|
229 KHCILoggerControllerToHost | KHCILoggerCommandOrEvent | KHCILoggerFrameFragmented, |
|
230 iReceiveBufPtr.Left(THCIEventBase::KEventCommonFieldsLength)); |
|
231 currentReadOffset = THCIEventBase::KEventCommonFieldsLength; |
|
232 } |
|
233 break; |
|
234 |
|
235 default: |
|
236 { |
|
237 // Invalid state. |
|
238 PANIC(KBcmHctlH4Panic, EIllegalState); |
|
239 break; |
|
240 } |
|
241 }; |
|
242 } |
|
243 break; |
|
244 |
|
245 case EWaitingForHciPayload: |
|
246 { |
|
247 TUint16 payloadLength = iReceiveBufPtr.Length(); |
|
248 switch(iCurrentHCIPacketType) |
|
249 { |
|
250 case CHCTLBcmH4::EACLDataPacket: |
|
251 { |
|
252 HCI_LOG_FRAME(this, |
|
253 KHCILoggerControllerToHost | KHCILoggerACLDataFrame, |
|
254 iReceiveBufPtr); |
|
255 iHCTLBcmH4.ProcessACLData(iReceiveBuffer.Left(payloadLength + CHctlAclDataFrame::KHCIACLDataPacketHeaderLength)); |
|
256 } |
|
257 break; |
|
258 |
|
259 case CHCTLBcmH4::ESynchronousDataPacket: |
|
260 { |
|
261 HCI_LOG_FRAME(this, |
|
262 KHCILoggerControllerToHost | KHCILoggerSynchronousDataFrame, |
|
263 iReceiveBufPtr); |
|
264 iHCTLBcmH4.ProcessSynchronousData(iReceiveBuffer.Left(payloadLength + CHctlSynchronousDataFrame::KHCISynchronousDataPacketHeaderLength)); |
|
265 } |
|
266 break; |
|
267 |
|
268 case CHCTLBcmH4::EEventPacket: |
|
269 { |
|
270 HCI_LOG_FRAME(this, |
|
271 KHCILoggerControllerToHost | KHCILoggerCommandOrEvent, |
|
272 iReceiveBufPtr); |
|
273 iHCTLBcmH4.ProcessEvent(iReceiveBuffer.Left(payloadLength + THCIEventBase::KEventCommonFieldsLength)); |
|
274 } |
|
275 break; |
|
276 |
|
277 default: |
|
278 // Invalid state |
|
279 PANIC(KBcmHctlH4Panic, EIllegalState); |
|
280 }; |
|
281 |
|
282 // Starting a new Packet. Bytes Required now is 1, offset is 0 |
|
283 // We finished with this packet so we're back in 'wait for next mode' |
|
284 currentReadOffset = 0; |
|
285 bytesRequiredForNextRead = KHctlHeaderSize; |
|
286 iState = EWaitingForHctlHeaderByte; |
|
287 } |
|
288 break; |
|
289 |
|
290 default: |
|
291 // must never get here |
|
292 PANIC(KBcmHctlH4Panic, EIllegalState); |
|
293 break; |
|
294 } |
|
295 |
|
296 if(iState == EInvalidDataReceived) |
|
297 { |
|
298 // The HCTL can not recover from this. Reset the controller and restart the host. |
|
299 iHCTLBcmH4.MhriStartHardReset(); |
|
300 } |
|
301 else |
|
302 { |
|
303 // Request the next read on the port. |
|
304 QueueReadForNextFrame(currentReadOffset, bytesRequiredForNextRead); |
|
305 } |
|
306 } |
|
307 |
|
308 |
|
309 void CHCTLBcmH4Receiver::RunL() |
|
310 { |
|
311 LOG_FUNC |
|
312 LOG1(_L8("\tiStatus = %d"), iStatus.Int()); |
|
313 |
|
314 // Only process the read if it has completed successfully. |
|
315 if (iStatus.Int() == KErrNone) |
|
316 { |
|
317 ProcessData(); |
|
318 } |
|
319 else |
|
320 { |
|
321 // The HCTL can not recover from this. Reset the controller and restart the host. |
|
322 iHCTLBcmH4.MhriStartHardReset(); |
|
323 } |
|
324 } |
|
325 |
|
326 void CHCTLBcmH4Receiver::DoCancel() |
|
327 { |
|
328 LOG_FUNC |
|
329 |
|
330 // Cancel the outstanding read request. |
|
331 iPort.ReadCancel(); |
|
332 } |
|
333 |
|
334 void CHCTLBcmH4Receiver::Start() |
|
335 { |
|
336 LOG_FUNC |
|
337 __ASSERT_DEBUG(!IsActive(), PANIC(KBcmHctlH4Panic, EStartCalledWhenReadOutstanding)); |
|
338 |
|
339 // Reset this object state and make an initial read on the UART. |
|
340 iState = EWaitingForHctlHeaderByte; |
|
341 |
|
342 // Read first byte on next frame (HCI packet type) into offset zero of the buffer. |
|
343 QueueReadForNextFrame(0, KHctlHeaderSize); |
|
344 } |