|
1 // Copyright (c) 2004-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 // Implements the handling of l2cap signal packets (both inbound and outbound). |
|
15 // |
|
16 // |
|
17 |
|
18 /** |
|
19 @file |
|
20 @internalComponent |
|
21 */ |
|
22 |
|
23 #include <bluetooth/logger.h> |
|
24 #include "l2capCommand.h" |
|
25 |
|
26 #include "l2capSigPacketConnection.h" |
|
27 #include "l2capSigPacketConfigure.h" |
|
28 #include "l2capSigPacketDisconnection.h" |
|
29 #include "l2capSigPacketEcho.h" |
|
30 #include "l2capSigPacketCommandReject.h" |
|
31 #include "l2capSigPacketInformation.h" |
|
32 #include "l2capSignalHandler.h" |
|
33 |
|
34 #include "btsockettimer.h" |
|
35 |
|
36 #include "l2signalmgr.h" |
|
37 |
|
38 #include "L2CapDebugControlInterface.h" |
|
39 |
|
40 #ifdef __FLOG_ACTIVE |
|
41 _LIT8(KLogComponent, LOG_COMPONENT_L2CAP); |
|
42 #endif |
|
43 |
|
44 // Static function that decodes the RMBufChain into the correct command returns pointer to base class |
|
45 TInt HL2CapCommand::DecodeCommand(RMBufChain& aCommandString, HL2CapCommand*& aCommand) |
|
46 { |
|
47 LOG_STATIC_FUNC |
|
48 aCommand = NULL; |
|
49 TInt rerr = KErrNone; |
|
50 RMBufChain remainingBuffer; |
|
51 |
|
52 if(aCommandString.Length() >= KL2CapCommandHeaderLength) |
|
53 { |
|
54 aCommandString.Align(KL2CapCommandHeaderLength); |
|
55 TUint8 code = (aCommandString.First())->Get(KCodeOffset); |
|
56 TUint16 len = LittleEndian::Get16((aCommandString.First())->Ptr()+KLengthOffset); |
|
57 |
|
58 // Check the command has a valid length. |
|
59 if(len + KL2CapCommandHeaderLength <= aCommandString.Length()) |
|
60 { |
|
61 TRAP(rerr, aCommandString.SplitL(len + KL2CapCommandHeaderLength, remainingBuffer)); |
|
62 if(rerr == KErrNone) |
|
63 { |
|
64 TInt cmdErr = KErrNone; |
|
65 switch(code) |
|
66 { |
|
67 case ECommandReject: |
|
68 (void)HCommandReject::NewCommandIfValid(aCommandString, aCommand); |
|
69 break; |
|
70 case EConnectionRequest: |
|
71 cmdErr = HConnectionRequest::NewCommandIfValid(aCommandString, aCommand); |
|
72 break; |
|
73 case EConnectionResponse: |
|
74 (void)HConnectionResponse::NewCommandIfValid(aCommandString, aCommand); |
|
75 break; |
|
76 case EConfigureRequest: |
|
77 cmdErr = HConfigureRequest::NewCommandIfValid(aCommandString, aCommand); |
|
78 break; |
|
79 case EConfigureResponse: |
|
80 (void)HConfigureResponse::NewCommandIfValid(aCommandString, aCommand); |
|
81 break; |
|
82 case EDisconnectionRequest: |
|
83 cmdErr = HDisconnectRequest::NewCommandIfValid(aCommandString, aCommand); |
|
84 break; |
|
85 case EDisconnectionResponse: |
|
86 (void)HDisconnectResponse::NewCommandIfValid(aCommandString, aCommand); |
|
87 break; |
|
88 case EEchoRequest: |
|
89 cmdErr = HEchoRequest::NewCommandIfValid(aCommandString, aCommand); |
|
90 break; |
|
91 case EEchoResponse: |
|
92 (void)HEchoResponse::NewCommandIfValid(aCommandString, aCommand); |
|
93 break; |
|
94 case EInformationRequest: |
|
95 cmdErr = HInformationRequest::NewCommandIfValid(aCommandString, aCommand); |
|
96 break; |
|
97 case EInformationResponse: |
|
98 (void)HInformationResponse::NewCommandIfValid(aCommandString, aCommand); |
|
99 break; |
|
100 |
|
101 default: |
|
102 // This is not a recognised command. |
|
103 LOG1(_L("DecodeCommand Unrecognised Command, code = %d"), code); |
|
104 aCommand = new HInvalidCommand(aCommandString); |
|
105 break; |
|
106 }; |
|
107 |
|
108 if(cmdErr == KErrCorrupt) |
|
109 { |
|
110 aCommand = new HInvalidCommand(aCommandString); |
|
111 } |
|
112 |
|
113 if(!aCommand) |
|
114 { |
|
115 aCommandString.Free(); |
|
116 } |
|
117 |
|
118 aCommandString.Assign(remainingBuffer); |
|
119 } |
|
120 } |
|
121 else |
|
122 { |
|
123 // Invalid command length. |
|
124 rerr = KErrCorrupt; |
|
125 } |
|
126 } |
|
127 else |
|
128 { |
|
129 rerr = KErrCompletion; |
|
130 } |
|
131 return rerr; |
|
132 } |
|
133 |
|
134 |
|
135 HL2CapCommand::~HL2CapCommand() |
|
136 { |
|
137 LOG_FUNC |
|
138 CancelResponseTimer(); |
|
139 iLink.Deque(); |
|
140 iCommand.Free(); |
|
141 |
|
142 L2CAP_DEBUG(ObjectAllocation(L2capDebugInfo::EL2CapCommand, |
|
143 L2capDebugInfo::EDeleted)); |
|
144 } |
|
145 |
|
146 HL2CapCommand::HL2CapCommand(RMBufChain& aCommand, |
|
147 TUint8 aRTXTimerDuration, |
|
148 TUint16 aERTXTimerDuration) |
|
149 : iResponseTimerRunning(EFalse), |
|
150 iRetransmissionCounter(0), |
|
151 iRTXTimerDuration(aRTXTimerDuration), |
|
152 iERTXTimerDuration(aERTXTimerDuration) |
|
153 { |
|
154 LOG_FUNC |
|
155 TInt length; |
|
156 iCommand.Assign(aCommand); |
|
157 length = iCommand.Length(); |
|
158 |
|
159 if (length > KMBufSmallSize) |
|
160 { |
|
161 length = KMBufSmallSize; // alignment of longer packets, causes problems |
|
162 } |
|
163 iCommand.Align(length); |
|
164 |
|
165 |
|
166 L2CAP_DEBUG(ObjectAllocation(L2capDebugInfo::EL2CapCommand, |
|
167 L2capDebugInfo::EAllocated)); |
|
168 } |
|
169 |
|
170 TInt HL2CapCommand::GetCommand(CL2CAPMux& aMuxer, RMBufChain& aBuffer) |
|
171 { |
|
172 LOG_FUNC |
|
173 // Set the ID if this is a request. If the ID has already |
|
174 // been initialised do not overwrite it. This could be |
|
175 // a retransmission. |
|
176 if(IsRequest() && ID() == KUninitialisedID) |
|
177 { |
|
178 SetID(aMuxer.NextSigId()); |
|
179 } |
|
180 |
|
181 TRAPD(rerr, iCommand.CopyL(aBuffer)); |
|
182 return rerr; |
|
183 } |
|
184 |
|
185 |
|
186 /*static*/ TInt HL2CapCommand::ResponseTimerExpired(TAny* aL2CapCommand) |
|
187 { |
|
188 LOG_STATIC_FUNC |
|
189 HL2CapCommand* cmd = reinterpret_cast<HL2CapCommand*>(aL2CapCommand); |
|
190 cmd->HandleResponseTimerExpiry(); |
|
191 return EFalse; |
|
192 } |
|
193 |
|
194 void HL2CapCommand::HandleResponseTimerExpiry() |
|
195 { |
|
196 LOG_FUNC |
|
197 __ASSERT_DEBUG(iResponseSignalHandler != 0, Panic(EL2CAPNoResponseSignalHandler)); |
|
198 // Deque the command from the response queue. |
|
199 iLink.Deque(); |
|
200 iResponseTimerRunning = EFalse; |
|
201 |
|
202 if(iRetransmissionCounter >= KNumberOfRetransmissions) |
|
203 { |
|
204 iResponseSignalHandler->CommandResponseFailure(this); |
|
205 } |
|
206 else |
|
207 { |
|
208 iResponseSignalHandler->AddToOutgoingQueue(this); |
|
209 iRetransmissionCounter++; |
|
210 } |
|
211 } |
|
212 |
|
213 void HL2CapCommand::StartRTXTimer(TRTXTimerType aType, CL2CapSignalHandler& aSignalHandler) |
|
214 { |
|
215 LOG_FUNC |
|
216 // Check that it is valid to start a timer on this message. |
|
217 __ASSERT_DEBUG(IsRequest(), Panic(EL2CAPAttemptToStartRTxTimerForResponseCommand)); |
|
218 |
|
219 // Cancel any existing timers. |
|
220 CancelResponseTimer(); |
|
221 |
|
222 TCallBack cb(ResponseTimerExpired, this); |
|
223 iResponseTimerEntry.Set(cb); |
|
224 |
|
225 TUint16 timerDuration; |
|
226 if(aType == ERTXTimer) |
|
227 { |
|
228 timerDuration = static_cast<TUint16>((iRetransmissionCounter + 1) * aSignalHandler.Mux().AdjustRTXTimerForSniffMode(iRTXTimerDuration)); |
|
229 } |
|
230 else |
|
231 { |
|
232 timerDuration = iERTXTimerDuration; |
|
233 iRetransmissionCounter = KNumberOfRetransmissions; |
|
234 } |
|
235 |
|
236 BTSocketTimer::Queue(timerDuration * KL2ProtocolSecondTimerMultiplier, iResponseTimerEntry); |
|
237 iResponseTimerRunning = ETrue; |
|
238 |
|
239 iResponseSignalHandler = &aSignalHandler; |
|
240 } |
|
241 |
|
242 void HL2CapCommand::CancelResponseTimer() |
|
243 { |
|
244 LOG_FUNC |
|
245 if(iResponseTimerRunning) |
|
246 { |
|
247 BTSocketTimer::Remove(iResponseTimerEntry); |
|
248 } |
|
249 } |
|
250 |
|
251 TBool HL2CapCommand::StatefulResponse() |
|
252 { |
|
253 LOG_FUNC |
|
254 return (Code() == EConfigureResponse) || (Code() == EConnectionResponse) || (Code() == EDisconnectionResponse); |
|
255 } |
|
256 |
|
257 HInvalidCommand::HInvalidCommand(RMBufChain& aCommand) |
|
258 : HL2CapCommand(aCommand) |
|
259 { |
|
260 LOG_FUNC |
|
261 } |
|
262 |
|
263 HInvalidCommand::~HInvalidCommand() |
|
264 { |
|
265 LOG_FUNC |
|
266 } |
|
267 |
|
268 TBool HInvalidCommand::ProcessCommand(CL2CapSignalHandler& aSignalHandler) |
|
269 { |
|
270 LOG_FUNC |
|
271 if(aSignalHandler.HandleInvalidCommand(this)) |
|
272 { |
|
273 // The command has been handled. Delete it. |
|
274 delete this; |
|
275 return ETrue; |
|
276 } |
|
277 else |
|
278 { |
|
279 return EFalse; |
|
280 } |
|
281 } |
|
282 |
|
283 |
|
284 /*static*/ void HL2CapCommand::PutByte(TUint8 aByte, TInt aOffset, RMBufChain& aCommand) |
|
285 { |
|
286 LOG_STATIC_FUNC |
|
287 __ASSERT_ALWAYS(aCommand.First() && (aCommand.First())->Length() > aOffset, |
|
288 Panic(EL2CapPDUInvalidLength)); |
|
289 aCommand.First()->Put(aByte, aOffset); |
|
290 } |
|
291 |
|
292 /*static*/ void HL2CapCommand::PutLittleEndian16(TUint16 aShort, TInt aOffset, RMBufChain& aCommand) |
|
293 { |
|
294 LOG_STATIC_FUNC |
|
295 __ASSERT_ALWAYS(aCommand.First() && (aCommand.First())->Length() > aOffset+1, |
|
296 Panic(EL2CapPDUInvalidLength)); |
|
297 LittleEndian::Put16((aCommand.First())->Ptr()+aOffset, aShort); |
|
298 } |
|
299 |
|
300 |
|
301 /*static*/ void HL2CapCommand::PutLittleEndian32(TUint32 aLong, TInt aOffset, RMBufChain& aCommand) |
|
302 { |
|
303 LOG_STATIC_FUNC |
|
304 __ASSERT_ALWAYS(aCommand.First() && (aCommand.First())->Length() > aOffset+3, |
|
305 Panic(EL2CapPDUInvalidLength)); |
|
306 LittleEndian::Put32((aCommand.First())->Ptr()+aOffset, aLong); |
|
307 } |
|
308 |
|
309 /*static*/ TUint8 HL2CapCommand::GetByte(TInt aOffset, const RMBufChain& aCommand) |
|
310 { |
|
311 LOG_STATIC_FUNC |
|
312 __ASSERT_ALWAYS(aCommand.First() && (aCommand.First())->Length() > aOffset, |
|
313 Panic(EL2CapPDUInvalidLength)); |
|
314 return aCommand.First()->Get(aOffset); |
|
315 } |
|
316 |
|
317 /*static*/ TUint16 HL2CapCommand::GetLittleEndian16(TInt aOffset, const RMBufChain& aCommand) |
|
318 { |
|
319 LOG_STATIC_FUNC |
|
320 __ASSERT_ALWAYS(aCommand.First() && (aCommand.First())->Length() > aOffset+1, |
|
321 Panic(EL2CapPDUInvalidLength)); |
|
322 return (LittleEndian::Get16((aCommand.First())->Ptr()+aOffset)); |
|
323 } |
|
324 |
|
325 /*static*/ TUint32 HL2CapCommand::GetLittleEndian32(TInt aOffset, const RMBufChain& aCommand) |
|
326 { |
|
327 LOG_STATIC_FUNC |
|
328 __ASSERT_ALWAYS(aCommand.First() && (aCommand.First())->Length() > aOffset+3, |
|
329 Panic(EL2CapPDUInvalidLength)); |
|
330 return(LittleEndian::Get32((aCommand.First())->Ptr()+aOffset)); |
|
331 } |