|
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 "hctlti.h" |
|
22 |
|
23 #include "hctltisender.h" |
|
24 #include "hctltireceiver.h" |
|
25 #include "controllermanager.h" |
|
26 |
|
27 #include "hctltiutils.h" |
|
28 |
|
29 #include <bluetooth/hci/hctleventobserver.h> |
|
30 #include <bluetooth/hci/hctldataobserver.h> |
|
31 #include <bluetooth/hci/hctlchannelobserver.h> |
|
32 #include <bluetooth/logger.h> |
|
33 |
|
34 |
|
35 #ifdef __FLOG_ACTIVE |
|
36 _LIT8(KLogComponent, LOG_COMPONENT_HCTL_TI); |
|
37 #endif |
|
38 |
|
39 CHCTLTi::CHCTLTi() |
|
40 { |
|
41 LOG_FUNC |
|
42 } |
|
43 |
|
44 CHCTLTi::~CHCTLTi() |
|
45 { |
|
46 LOG_FUNC |
|
47 HCI_LOG_UNLOAD(this); |
|
48 delete iReceiver; |
|
49 delete iSender; |
|
50 delete iControllerMan; |
|
51 } |
|
52 |
|
53 CHCTLTi* CHCTLTi::NewL() |
|
54 { |
|
55 LOG_STATIC_FUNC |
|
56 |
|
57 CHCTLTi* self = new(ELeave) CHCTLTi(); |
|
58 CleanupStack::PushL(self); |
|
59 self->ConstructL(); |
|
60 CleanupStack::Pop(); |
|
61 return self; |
|
62 } |
|
63 |
|
64 void CHCTLTi::ConstructL() |
|
65 { |
|
66 LOG_FUNC |
|
67 HCI_LOG_LOADL(this, KHCILoggerDatalinkTypeH4); |
|
68 |
|
69 // Initialises iSender and iReceiver via the PortOpenedL method. |
|
70 BaseConstructL(KIniFileName()); |
|
71 |
|
72 iControllerMan = CControllerManager::NewL(*this); |
|
73 } |
|
74 |
|
75 TAny* CHCTLTi::Interface(TUid aUid) |
|
76 { |
|
77 LOG_FUNC |
|
78 |
|
79 TAny* ret = NULL; |
|
80 switch(aUid.iUid) |
|
81 { |
|
82 case KHCTLInterfaceUid: |
|
83 ret = reinterpret_cast<TAny*>(static_cast<MHCTLInterface*>(this)); |
|
84 break; |
|
85 |
|
86 case KHCTLPowerInterfaceUid: |
|
87 ret = reinterpret_cast<TAny*>(static_cast<MHCTLPowerInterface*>(iControllerMan)); |
|
88 break; |
|
89 |
|
90 case KHctlConfigInterfaceUid: |
|
91 ret = reinterpret_cast<TAny*>(static_cast<MHctlConfigInterface*>(this)); |
|
92 break; |
|
93 |
|
94 case KHCHardResetUid: |
|
95 ret = reinterpret_cast<TAny*>(static_cast<MHardResetInitiator*>(this)); |
|
96 break; |
|
97 |
|
98 default: |
|
99 break; |
|
100 } |
|
101 |
|
102 return ret; |
|
103 } |
|
104 |
|
105 void CHCTLTi::DoConfigL() |
|
106 { |
|
107 LOG_FUNC |
|
108 |
|
109 Port().ResetBuffers(); |
|
110 TCommConfig conf; |
|
111 Port().Config(conf); |
|
112 |
|
113 // Get reference to TCommConfig config |
|
114 TCommConfigV01& config = conf(); |
|
115 |
|
116 // Set Config to 8bit char, 1 stop bit and no parity |
|
117 config.iDataBits = EData8; |
|
118 config.iStopBits = EStop1; |
|
119 config.iParity = EParityNone; |
|
120 config.iParityError = KConfigParityErrorIgnore; |
|
121 config.iHandshake = KConfigObeyCTS; |
|
122 |
|
123 LEAVEIFERRORL(Port().SetConfig(conf)); |
|
124 |
|
125 // allows HC to talk back to us! |
|
126 Port().SetSignals(KSignalRTS, KSignalDTEInputs); |
|
127 } |
|
128 |
|
129 //Implementation of pure virtuals from MHCTLInterface |
|
130 TInt CHCTLTi::MhiWriteAclData(const TDesC8& aData) |
|
131 { |
|
132 LOG_FUNC |
|
133 |
|
134 TInt rerr = KErrNotReady; |
|
135 // Send if the power is on. |
|
136 if(iCurrentPowerState == EBTOn) |
|
137 { |
|
138 // Add the packet indicator to the first byte of the buffer. |
|
139 SetPacketIndicator(EACLDataPacket, aData); |
|
140 rerr = iSender->Write(aData); |
|
141 HCI_LOG_FRAME_IF_NO_ERROR(rerr, this, KHCILoggerHostToController | KHCILoggerACLDataFrame, aData); |
|
142 } |
|
143 |
|
144 return rerr; |
|
145 } |
|
146 |
|
147 TInt CHCTLTi::MhiWriteSynchronousData(const TDesC8& aData) |
|
148 { |
|
149 LOG_FUNC |
|
150 |
|
151 TInt rerr = KErrNotReady; |
|
152 // Send if the power is on. |
|
153 if(iCurrentPowerState == EBTOn) |
|
154 { |
|
155 // Add the packet indicator to the first byte of the buffer. |
|
156 SetPacketIndicator(ESynchronousDataPacket, aData); |
|
157 rerr = iSender->Write(aData); |
|
158 HCI_LOG_FRAME_IF_NO_ERROR(rerr, this, KHCILoggerHostToController | KHCILoggerSynchronousDataFrame, aData); |
|
159 } |
|
160 |
|
161 return rerr; |
|
162 } |
|
163 |
|
164 TInt CHCTLTi::MhiWriteCommand(const TDesC8& aData) |
|
165 { |
|
166 LOG_FUNC |
|
167 |
|
168 TInt rerr = KErrNotReady; |
|
169 // Send if the power is on. |
|
170 if(iCurrentPowerState == EBTOn) |
|
171 { |
|
172 // Add the packet indicator to the first byte of the buffer. |
|
173 SetPacketIndicator(ECommandPacket, aData); |
|
174 rerr = iSender->Write(aData); |
|
175 HCI_LOG_FRAME_IF_NO_ERROR(rerr, this, KHCILoggerHostToController | KHCILoggerCommandOrEvent, aData); |
|
176 } |
|
177 |
|
178 return rerr; |
|
179 } |
|
180 |
|
181 void CHCTLTi::MhiSetQdpPluginInterfaceFinder(MQdpPluginInterfaceFinder& aQdpPluginInterfaceFinder) |
|
182 { |
|
183 iQdpPluginInterfaceFinder = &aQdpPluginInterfaceFinder; |
|
184 } |
|
185 |
|
186 void CHCTLTi::MhriStartHardReset() |
|
187 { |
|
188 __ASSERT_DEBUG(iControllerMan, PANIC(KTiPanic, ENoControllerManager)); |
|
189 iControllerMan->HardReset(); |
|
190 } |
|
191 |
|
192 void CHCTLTi::MhiGetAclDataTransportOverhead(TUint& aHeaderSize, TUint& aTrailerSize) const |
|
193 { |
|
194 // Return the transport overhead for ACL data. |
|
195 aHeaderSize = KHCTLAclDataHeaderSize; |
|
196 aTrailerSize = KHCTLAclDataTrailerSize; |
|
197 } |
|
198 |
|
199 |
|
200 void CHCTLTi::MhiGetSynchronousDataTransportOverhead(TUint& aHeaderSize, TUint& aTrailerSize) const |
|
201 { |
|
202 // Return the transport overhead for Synchronous data. |
|
203 aHeaderSize = KHCTLSynchronousDataHeaderSize; |
|
204 aTrailerSize = KHCTLSynchronousDataTrailerSize; |
|
205 } |
|
206 |
|
207 void CHCTLTi::MhiGetCommandTransportOverhead(TUint& aHeaderSize, TUint& aTrailerSize) const |
|
208 { |
|
209 // Return the transport overhead for HCI commands data. |
|
210 aHeaderSize = KHCTLCommandHeaderSize; |
|
211 aTrailerSize = KHCTLCommandTrailerSize; |
|
212 } |
|
213 |
|
214 |
|
215 /** |
|
216 This function is used by the receiver for informing HCI that ACL data has been received |
|
217 The receiver doesn't have reference to iDataObserver. So this is merely a wrapper for iDataObserver |
|
218 */ |
|
219 void CHCTLTi::ProcessACLData(const TDesC8& aData) |
|
220 { |
|
221 iDataObserver->MhdoProcessAclData(aData); |
|
222 } |
|
223 |
|
224 /** |
|
225 This function is used by the receiver for informing HCI that Synchronous data has been received |
|
226 The receiver doesn't have reference to iDataObserver. So this is merely a wrapper for iDataObserver |
|
227 */ |
|
228 void CHCTLTi::ProcessSynchronousData(const TDesC8& aData) |
|
229 { |
|
230 iDataObserver->MhdoProcessSynchronousData(aData); |
|
231 } |
|
232 |
|
233 /** |
|
234 This function is used by the receiver for informing HCI that event has been received |
|
235 The receiver doesn't have reference to iEventObserver. So this is merely a wrapper for iDataObserver |
|
236 */ |
|
237 void CHCTLTi::ProcessEvent(const TDesC8& aEvent) |
|
238 { |
|
239 iEventObserver->MheoProcessEvent(aEvent); |
|
240 } |
|
241 |
|
242 /** |
|
243 QdpPluginInterfaceFinder getter. |
|
244 |
|
245 @return returns iQdpPluginInterfaceFinder which could be NULL |
|
246 if it has not been given one. |
|
247 */ |
|
248 MQdpPluginInterfaceFinder* CHCTLTi::QdpPluginInterfaceFinder() |
|
249 { |
|
250 return iQdpPluginInterfaceFinder; |
|
251 } |
|
252 |
|
253 /** |
|
254 Sets the packet type indicator at the begining of the HCI packet. |
|
255 |
|
256 Hence the HC can recognise whether this packet is a command, ACL/SCO data. |
|
257 The packet preamble/indication is different between different HCTL |
|
258 implementations and are totally dependent upon the meduim used (UART, R2232 etc). |
|
259 */ |
|
260 /*static*/ void CHCTLTi::SetPacketIndicator(THctlPacketType aType, const TDesC8& aData) |
|
261 { |
|
262 LOG_STATIC_FUNC |
|
263 |
|
264 TUint8* ptr = const_cast<TUint8*>(aData.Ptr()); |
|
265 *ptr = static_cast<TUint8>(aType); |
|
266 } |
|
267 |
|
268 // Implementation of pure virtual from CHCTLUartBase |
|
269 void CHCTLTi::PortOpenedL() |
|
270 { |
|
271 LOG_FUNC |
|
272 |
|
273 __ASSERT_DEBUG(Port().Handle(), PANIC(KTiPanic, EPortNotOpen)); |
|
274 |
|
275 if (iSender || iReceiver) |
|
276 { |
|
277 LEAVEIFERRORL(KErrAlreadyExists); |
|
278 } |
|
279 |
|
280 DoConfigL(); |
|
281 iCurrentPowerState = EBTOn; |
|
282 // The sender Active Object must be added to the Active Scheduler before |
|
283 // the receiver Active Object so that it gets preferential treatment. It |
|
284 // is reported that otherwise the response from a command can come in |
|
285 // before the sending client is told that the send has completed! |
|
286 iSender = CHCTLTiSender::NewL(Port()); |
|
287 iReceiver = CHCTLTiReceiver::NewL(*this, Port()); |
|
288 |
|
289 // Start reading from the UART |
|
290 iReceiver->Start(); |
|
291 } |
|
292 |
|
293 |
|
294 void CHCTLTi::MhiSetDataObserver(MHCTLDataObserver& aDataObserver) |
|
295 { |
|
296 iDataObserver = &aDataObserver; |
|
297 } |
|
298 |
|
299 void CHCTLTi::MhiSetEventObserver(MHCTLEventObserver& aEventObserver) |
|
300 { |
|
301 iEventObserver = &aEventObserver; |
|
302 } |
|
303 |
|
304 void CHCTLTi::MhiSetChannelObserver(MHCTLChannelObserver& aChannelObserver) |
|
305 { |
|
306 iChannelObserver = &aChannelObserver; |
|
307 iSender->SetChannelObserver(aChannelObserver); |
|
308 } |
|
309 |
|
310 void CHCTLTi::MhiSetControllerStateObserver(MControllerStateObserver& aControllerStateObserver) |
|
311 { |
|
312 iControllerStateObserver = &aControllerStateObserver; |
|
313 iControllerMan->SetControllerStateObserver(aControllerStateObserver); |
|
314 } |
|
315 |
|
316 void CHCTLTi::HandlePowerOff() |
|
317 { |
|
318 iCurrentPowerState = EBTOff; |
|
319 // Close all channels and cancel the sender and receiver. |
|
320 iSender->Cancel(); |
|
321 iReceiver->Cancel(); |
|
322 iChannelObserver->MhcoChannelClosed(KHCITransportAllChannels); |
|
323 } |
|
324 |
|
325 void CHCTLTi::HandlePowerOn() |
|
326 { |
|
327 iCurrentPowerState = EBTOn; |
|
328 iChannelObserver->MhcoChannelOpen(KHCITransportAllChannels); |
|
329 |
|
330 // Clear any spurious data from the buffer. |
|
331 Port().ResetBuffers(); |
|
332 |
|
333 iReceiver->Start(); |
|
334 // The sender will be activated when the first frame needs to be sent. |
|
335 } |
|
336 |
|
337 TBTPowerState CHCTLTi::CurrentPowerState() const |
|
338 { |
|
339 return iCurrentPowerState; |
|
340 } |
|
341 |
|
342 TInt CHCTLTi::MhciUpdateBaudRate(TUint32 aBaudRate) |
|
343 { |
|
344 // Call the base class method to process this change. |
|
345 TRAPD(rerr, SetPortBaudRateL(aBaudRate)); |
|
346 return rerr; |
|
347 } |