|
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 "linkestablishment.h" |
|
22 |
|
23 #include "hctlbcsplinkstate.h" |
|
24 #include "hctlbcspconsts.h" |
|
25 #include "hctlbcsp.h" |
|
26 #include "debug.h" |
|
27 |
|
28 #include <bluetooth/hci/delay.h> |
|
29 |
|
30 CLinkEstablishment::CLinkEstablishment(CHCTLBcsp& aHCTLBcsp) |
|
31 :iHctlBcsp(aHCTLBcsp) |
|
32 { |
|
33 LOG_FUNC |
|
34 } |
|
35 |
|
36 CLinkEstablishment* CLinkEstablishment::NewL(CHCTLBcsp& aHCTLbcsp) |
|
37 { |
|
38 LOG_STATIC_FUNC |
|
39 |
|
40 CLinkEstablishment* self=new(ELeave) CLinkEstablishment(aHCTLbcsp); |
|
41 CleanupStack::PushL(self); |
|
42 self->ConstructL(); |
|
43 CleanupStack::Pop(); |
|
44 return self; |
|
45 } |
|
46 |
|
47 void CLinkEstablishment::ConstructL() |
|
48 { |
|
49 LOG_FUNC |
|
50 TCallBack cb = TCallBack(CLinkEstablishment::ShyTimeout, this); |
|
51 iShyTimer = CDelay::NewL(cb, CActive::EPriorityLow); |
|
52 |
|
53 cb = TCallBack(CLinkEstablishment::ConfTimeout, this); |
|
54 iConfTimer = CDelay::NewL(cb, CActive::EPriorityLow); |
|
55 |
|
56 iLinkStateFactory = CLinkStateFactory::NewL(*this); |
|
57 |
|
58 //Set first state to EShy however do not call the Exit function |
|
59 iLinkState = &(iLinkStateFactory->State(CLinkStateFactory::EShy)); |
|
60 |
|
61 //Call Entry point to first state i.e. TLinkStateShy |
|
62 iLinkState->Entry(); |
|
63 |
|
64 } |
|
65 |
|
66 CLinkEstablishment::~CLinkEstablishment() |
|
67 { |
|
68 LOG_FUNC |
|
69 |
|
70 delete iShyTimer; |
|
71 delete iConfTimer; |
|
72 delete iLinkStateFactory; |
|
73 } |
|
74 |
|
75 /** |
|
76 Simple stub method that passes a received @param aLinkMsg() |
|
77 from the receiver and forwards it to the CLinkEstblishment::LinkStateMachine() |
|
78 method |
|
79 */ |
|
80 void CLinkEstablishment::ProcessLinkMsg(const TDesC8& aLinkMsg) |
|
81 { |
|
82 LOG_FUNC |
|
83 |
|
84 LinkStateMachine(aLinkMsg); |
|
85 } |
|
86 |
|
87 /** |
|
88 LinkStateMachine method filters @param aLinkMsg by comparing with the 4 BCSP link message |
|
89 constants and then calling the appropriate handling methods which then call in to the State Pattern |
|
90 link state machine |
|
91 */ |
|
92 void CLinkEstablishment::LinkStateMachine(const TDesC8& aLinkMsg) |
|
93 { |
|
94 LOG_FUNC |
|
95 |
|
96 if ( aLinkMsg.Compare(KBcspLinkMsg_Sync) == 0 ) |
|
97 { |
|
98 LOG(_L8("CLinkEstablishment::LinkStateMachine() KBcspLinkMsgSync RECVD")); |
|
99 HandleLinkMsgSync(); |
|
100 } |
|
101 else if ( aLinkMsg.Compare(KBcspLinkMsg_SyncResp) == 0 ) |
|
102 { |
|
103 LOG(_L8("CLinkEstablishment::LinkStateMachine() KBcspLinkMsgSyncResp RECVD")); |
|
104 HandleLinkMsgSyncResp(); |
|
105 } |
|
106 else if ( aLinkMsg.Compare(KBcspLinkMsg_Conf) == 0 ) |
|
107 { |
|
108 LOG(_L8("CLinkEstablishment::LinkStateMachine() KBcspLinkMsgConf RECVD")); |
|
109 HandleLinkMsgConf(); |
|
110 } |
|
111 else if ( aLinkMsg.Compare(KBcspLinkMsg_ConfResp) == 0 ) |
|
112 { |
|
113 LOG(_L8("CLinkEstablishment::LinkStateMachine() KBcspLinkMsgConfResp RECVD")); |
|
114 HandleLinkMsgConfResp(); |
|
115 } |
|
116 else |
|
117 { |
|
118 LOG(_L8("CLinkEstablishment::LinkStateMachine() UNRECOGNIZED")); |
|
119 } |
|
120 } |
|
121 |
|
122 |
|
123 /** |
|
124 Start the shy timer |
|
125 */ |
|
126 void CLinkEstablishment::StartShyTimer() |
|
127 { |
|
128 LOG_FUNC |
|
129 iShyTimer->After(KTShyTimeout); |
|
130 } |
|
131 |
|
132 /** |
|
133 Simple stub method to allow state machine to make a call to Start the ConfTimer |
|
134 */ |
|
135 void CLinkEstablishment::StartConfTimer() |
|
136 { |
|
137 LOG_FUNC |
|
138 //reset the confirmation counter |
|
139 iConfCnt = 0; |
|
140 //start the timer |
|
141 iConfTimer->After(KTConfTimeout); |
|
142 } |
|
143 |
|
144 /** |
|
145 State Pattern method to switch state |
|
146 @param aLinkState |
|
147 */ |
|
148 void CLinkEstablishment::SetState(const TLinkState& aLinkState) |
|
149 { |
|
150 LOG_FUNC |
|
151 |
|
152 iLinkState->Exit(); |
|
153 iLinkState = &aLinkState; |
|
154 iLinkState->Entry(); |
|
155 } |
|
156 |
|
157 TBool CLinkEstablishment::Muzzled() |
|
158 { |
|
159 return (iHctlBcsp.Muzzled()); |
|
160 } |
|
161 |
|
162 void CLinkEstablishment::ResetMuzzled() |
|
163 { |
|
164 iHctlBcsp.ResetMuzzled(); |
|
165 } |
|
166 |
|
167 void CLinkEstablishment::HandleLinkMsgConf() |
|
168 { |
|
169 LOG_FUNC |
|
170 |
|
171 iLinkState->HandleLinkMsgConf(); |
|
172 } |
|
173 |
|
174 void CLinkEstablishment::HandleLinkMsgConfResp() |
|
175 { |
|
176 LOG_FUNC |
|
177 |
|
178 iLinkState->HandleLinkMsgConfResp(); |
|
179 } |
|
180 |
|
181 void CLinkEstablishment::HandleLinkMsgSync() |
|
182 { |
|
183 LOG_FUNC |
|
184 |
|
185 ResetMuzzled(); |
|
186 iLinkState->HandleLinkMsgSync(); |
|
187 } |
|
188 |
|
189 void CLinkEstablishment::HandleLinkMsgSyncResp() |
|
190 { |
|
191 LOG_FUNC |
|
192 |
|
193 iLinkState->HandleLinkMsgSyncResp(); |
|
194 } |
|
195 |
|
196 void CLinkEstablishment::StopShyTimer() |
|
197 { |
|
198 LOG_FUNC |
|
199 |
|
200 iShyTimer->Cancel(); |
|
201 } |
|
202 |
|
203 void CLinkEstablishment::StopConfTimer() |
|
204 { |
|
205 LOG_FUNC |
|
206 |
|
207 iConfTimer->Cancel(); |
|
208 } |
|
209 |
|
210 void CLinkEstablishment::UnChoke() |
|
211 { |
|
212 LOG_FUNC |
|
213 |
|
214 iHctlBcsp.UnChoke(); |
|
215 } |
|
216 |
|
217 void CLinkEstablishment::Choke() |
|
218 { |
|
219 LOG_FUNC |
|
220 |
|
221 iHctlBcsp.Choke(); |
|
222 } |
|
223 |
|
224 void CLinkEstablishment::QueueReadForNextFrame() |
|
225 { |
|
226 LOG_FUNC |
|
227 |
|
228 iHctlBcsp.QueueReadForNextFrame(); |
|
229 } |
|
230 |
|
231 void CLinkEstablishment::SendLinkMsg(const TDesC8& aLinkMsg) |
|
232 { |
|
233 LOG_FUNC |
|
234 |
|
235 iHctlBcsp.TxLinkMsg(aLinkMsg); |
|
236 } |
|
237 |
|
238 /** |
|
239 Handle a peer reset detection by passing it up the stack |
|
240 */ |
|
241 void CLinkEstablishment::HandlePeerReset() |
|
242 { |
|
243 LOG_FUNC |
|
244 |
|
245 iHctlBcsp.HandlePeerReset(); |
|
246 } |
|
247 |
|
248 /** |
|
249 Reset the link establishment state machine to its default state |
|
250 */ |
|
251 void CLinkEstablishment::Reset() |
|
252 { |
|
253 LOG_FUNC |
|
254 |
|
255 SetState(iLinkStateFactory->State(CLinkStateFactory::EShy)); |
|
256 } |
|
257 |
|
258 void CLinkEstablishment::HandleConfTimeout() |
|
259 { |
|
260 LOG_FUNC |
|
261 |
|
262 if(iConfCnt < KBcspConfCntMax) |
|
263 { |
|
264 SendLinkMsg(KBcspLinkMsg_Conf); |
|
265 iConfTimer->After(KTConfTimeout); |
|
266 iConfCnt++; |
|
267 } |
|
268 else |
|
269 { |
|
270 iConfTimer->Cancel(); |
|
271 } |
|
272 } |
|
273 |
|
274 void CLinkEstablishment::HandleShyTimeout() |
|
275 { |
|
276 LOG_FUNC |
|
277 |
|
278 SendLinkMsg(KBcspLinkMsg_Sync); |
|
279 StartShyTimer(); |
|
280 } |
|
281 |
|
282 /** |
|
283 Confirmation timeout, we will try send another conf message if the max number |
|
284 of confirmation is not reached. Otherwise we stop doing so and just wait silently |
|
285 for the confirmation response. |
|
286 */ |
|
287 /*static*/ TInt CLinkEstablishment::ConfTimeout(TAny* aLink) |
|
288 { |
|
289 reinterpret_cast<CLinkEstablishment*>(aLink)->HandleConfTimeout(); |
|
290 return KErrNone; |
|
291 } |
|
292 |
|
293 /** |
|
294 Shy timer expired, we just keep sending more sync message until we get response |
|
295 */ |
|
296 /*static*/ TInt CLinkEstablishment::ShyTimeout(TAny* aLink) |
|
297 { |
|
298 reinterpret_cast<CLinkEstablishment*>(aLink)->HandleShyTimeout(); |
|
299 return KErrNone; |
|
300 } |