|
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 // |
|
15 |
|
16 #include <bluetooth/logger.h> |
|
17 |
|
18 #include "l2capSigPacketConnection.h" |
|
19 #include "l2capSignalHandler.h" |
|
20 |
|
21 #ifdef __FLOG_ACTIVE |
|
22 _LIT8(KLogComponent, LOG_COMPONENT_L2CAP); |
|
23 #endif |
|
24 |
|
25 // CONNECTION REQUEST COMMAND |
|
26 |
|
27 /*static*/ HConnectionRequest* HConnectionRequest::New(TL2CAPPort aLocalPort, TL2CAPPort aRemotePort, |
|
28 TUint8 aRTXTimerDuration, |
|
29 TUint16 aERTXTimerDuration) |
|
30 { |
|
31 LOG_STATIC_FUNC |
|
32 HConnectionRequest* cmd = NULL; |
|
33 RMBufChain buf; |
|
34 TRAPD(rerr, buf.AllocL(KConnectRequestLength)); |
|
35 if(rerr == KErrNone) |
|
36 { |
|
37 cmd = new HConnectionRequest(buf, aRTXTimerDuration, aERTXTimerDuration); |
|
38 if(cmd) |
|
39 { |
|
40 // Setup message contents. |
|
41 cmd->SetCode(EConnectionRequest); |
|
42 cmd->SetID(KUninitialisedID); |
|
43 cmd->WriteDataLength(); |
|
44 cmd->SetPSM(aRemotePort); |
|
45 cmd->SetSourceCID(aLocalPort); |
|
46 } |
|
47 else |
|
48 { |
|
49 // Free the allocated buffer. |
|
50 buf.Free(); |
|
51 } |
|
52 } |
|
53 return cmd; |
|
54 } |
|
55 |
|
56 /** |
|
57 Verifies that the buffer contains a structurally correct command. |
|
58 This does not ensure that the content of the command is semantically valid. |
|
59 |
|
60 @param aCommmand A new HL2CapCommand if the buffer contains a structurally valid command. |
|
61 @param aBuffer The buffer containing the command |
|
62 @return KErrNone if the command if created. |
|
63 KErrNoMemory if the command structure is valid but cannot be created. |
|
64 KErrCorrupt if the command structure is invalid. |
|
65 */ |
|
66 TInt HConnectionRequest::NewCommandIfValid(RMBufChain& aBuffer, HL2CapCommand*& aCommand) |
|
67 { |
|
68 LOG_STATIC_FUNC |
|
69 // Firstly align the MBufChain. The maximum size we can align the |
|
70 // MBufChain to is the maximum MBuf size |
|
71 __ASSERT_COMPILE(KConnectRequestLength <= KMBufSmallSize); |
|
72 |
|
73 TInt length = aBuffer.Length(); |
|
74 if(length == KConnectRequestLength) |
|
75 { |
|
76 // Don't need to check result as we know that the MBufChain contains |
|
77 // at least length bytes as we asked first. |
|
78 (void)aBuffer.Align(length); |
|
79 aCommand = new HConnectionRequest(aBuffer); |
|
80 if(aCommand) |
|
81 { |
|
82 return KErrNone; |
|
83 } |
|
84 else |
|
85 { |
|
86 return KErrNoMemory; |
|
87 } |
|
88 } |
|
89 else |
|
90 { |
|
91 // Dodge length! |
|
92 return KErrCorrupt; |
|
93 } |
|
94 } |
|
95 |
|
96 HConnectionRequest::HConnectionRequest(RMBufChain& aCommand, |
|
97 TUint8 aRTXTimerDuration, |
|
98 TUint16 aERTXTimerDuration) |
|
99 : HL2CapCommand(aCommand, aRTXTimerDuration, aERTXTimerDuration) |
|
100 { |
|
101 LOG_FUNC |
|
102 } |
|
103 |
|
104 HConnectionRequest::~HConnectionRequest() |
|
105 { |
|
106 LOG_FUNC |
|
107 } |
|
108 |
|
109 |
|
110 TBool HConnectionRequest::ProcessCommand(CL2CapSignalHandler& aSignalHandler) |
|
111 { |
|
112 LOG_FUNC |
|
113 if(aSignalHandler.HandleConnectionRequest(this)) |
|
114 { |
|
115 // The command has been handled. Delete it. |
|
116 delete this; |
|
117 return ETrue; |
|
118 } |
|
119 else |
|
120 { |
|
121 return EFalse; |
|
122 } |
|
123 } |
|
124 |
|
125 // CONNECTION RESPONSE COMMAND |
|
126 //Constructor to deal command coming in from remote side |
|
127 HConnectionResponse::HConnectionResponse(RMBufChain& aCommand) |
|
128 : HL2CapCommand(aCommand) |
|
129 { |
|
130 LOG_FUNC |
|
131 } |
|
132 |
|
133 /*static*/ HConnectionResponse* HConnectionResponse::New(TUint8 aId, TL2CAPPort aLocalPort, TL2CAPPort aRemotePort, TConnectResponseResult aResult, TConnectResponseStatus aStatus) |
|
134 { |
|
135 LOG_STATIC_FUNC |
|
136 HConnectionResponse* cmd = NULL; |
|
137 RMBufChain buf; |
|
138 TRAPD(rerr, buf.AllocL(KConnectResponseLength)); |
|
139 if(rerr == KErrNone) |
|
140 { |
|
141 cmd = new HConnectionResponse(buf); |
|
142 if(cmd) |
|
143 { |
|
144 // Setup message contents. |
|
145 cmd->SetCode(EConnectionResponse); |
|
146 cmd->SetID(aId); |
|
147 cmd->WriteDataLength(); |
|
148 |
|
149 cmd->SetDestinationCID(aLocalPort); |
|
150 cmd->SetSourceCID(aRemotePort); |
|
151 cmd->SetResult(aResult); |
|
152 cmd->SetStatus(aStatus); |
|
153 } |
|
154 else |
|
155 { |
|
156 // Free the allocated buffer. |
|
157 buf.Free(); |
|
158 } |
|
159 } |
|
160 return cmd; |
|
161 } |
|
162 |
|
163 /** |
|
164 Verifies that the buffer contains a structurally correct command. |
|
165 This does not ensure that the content of the command is semantically valid. |
|
166 |
|
167 @param aCommmand A new HL2CapCommand if the buffer contains a structurally valid command. |
|
168 @param aBuffer The buffer containing the command |
|
169 @return KErrNone if the command if created. |
|
170 KErrNoMemory if the command structure is valid but cannot be created. |
|
171 KErrCorrupt if the command structure is invalid.valid command. |
|
172 */ |
|
173 TInt HConnectionResponse::NewCommandIfValid(RMBufChain& aBuffer, HL2CapCommand*& aCommand) |
|
174 { |
|
175 LOG_STATIC_FUNC |
|
176 // Firstly align the MBufChain. The maximum size we can align the |
|
177 // MBufChain to is the maximum MBuf size |
|
178 __ASSERT_COMPILE(KConnectResponseLength <= KMBufSmallSize); |
|
179 |
|
180 TInt length = aBuffer.Length(); |
|
181 if(length == KConnectResponseLength) |
|
182 { |
|
183 // Don't need to check result as we know that the MBufChain contains |
|
184 // at least length bytes as we asked first. |
|
185 (void)aBuffer.Align(length); |
|
186 aCommand = new HConnectionResponse(aBuffer); |
|
187 if(aCommand) |
|
188 { |
|
189 return KErrNone; |
|
190 } |
|
191 else |
|
192 { |
|
193 return KErrNoMemory; |
|
194 } |
|
195 } |
|
196 else |
|
197 { |
|
198 // Dodge length! |
|
199 return KErrCorrupt; |
|
200 } |
|
201 } |
|
202 |
|
203 HConnectionResponse::~HConnectionResponse() |
|
204 { |
|
205 LOG_FUNC |
|
206 } |
|
207 |
|
208 TBool HConnectionResponse::ProcessCommand(CL2CapSignalHandler& aSignalHandler) |
|
209 { |
|
210 LOG_FUNC |
|
211 if(aSignalHandler.HandleConnectionResponse(this)) |
|
212 { |
|
213 // The command has been handled. Delete it. |
|
214 delete this; |
|
215 return ETrue; |
|
216 } |
|
217 else |
|
218 { |
|
219 return EFalse; |
|
220 } |
|
221 } |
|
222 |