|
1 // Copyright (c) 2005-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 |
|
18 // INCLUDE FILES |
|
19 #include <e32std.h> |
|
20 #include <bt_sock.h> |
|
21 #include <btdevice.h> |
|
22 #include "BTGPSPanic.h" |
|
23 #include "BTGPSConnectManager.h" |
|
24 #include "BTGPSSettingManager.h" |
|
25 #include "BTGPSHandlerFactory.h" |
|
26 #include "BTGPSDeviceManager.h" |
|
27 #include "BTGPSMessageSender.h" |
|
28 #include "BTGPSMessageReceiver.h" |
|
29 #include "BTGPSPanic.h" |
|
30 #include "BTGPSLogging.h" |
|
31 #include "btgpsdevicelistmanager.h" |
|
32 |
|
33 // EXTERNAL DATA STRUCTURES |
|
34 |
|
35 // EXTERNAL FUNCTION PROTOTYPES |
|
36 |
|
37 // CONSTANTS |
|
38 |
|
39 // MACROS |
|
40 |
|
41 // LOCAL CONSTANTS AND MACROS |
|
42 |
|
43 //Process state enumeration |
|
44 enum THandlerProcessState |
|
45 { |
|
46 EExitSuccess = -1, |
|
47 EExitFailed = -2, //Connection failed, device is error |
|
48 EExitDisconnected = -3 //Connection can't be made, device is disconnected |
|
49 }; |
|
50 |
|
51 //There are two different connecting state machines |
|
52 #ifdef SYMBIAN_LOCATION_BTGPSCONFIG |
|
53 const struct THandlerProcessItem KConnectingProcess[] = |
|
54 { |
|
55 {EGetNextDevice, 1, 7}, //0 |
|
56 {ECheckDevicePairing, 2, 3}, //1 |
|
57 {ESocketHandler, EExitSuccess, 0}, //2 |
|
58 {ESdpHandler, 4, 0}, //3 |
|
59 {EPairingHandler, 6, 5}, //4 |
|
60 {ESocketHandler, 6, 0}, //5 |
|
61 {EInitHandler, EExitSuccess, 0}, //6 |
|
62 {EDeviceSelector, 8, EExitFailed}, //7 |
|
63 {ESdpHandler, 9, 7}, //8 |
|
64 {EPairingHandler, 11, 10}, //9 |
|
65 {ESocketHandler, 11, 7}, //10 |
|
66 {EInitHandler, EExitSuccess, 7}, //11 |
|
67 }; |
|
68 #else |
|
69 //This struct defines the decision tree of the connection process |
|
70 const struct THandlerProcessItem KConnectingProcess[] = |
|
71 { |
|
72 {EDeviceInfoHandler, 1, 2}, //0 |
|
73 {ESocketHandler, EExitSuccess, 2}, //1 |
|
74 {EDeviceSelector, 3, EExitFailed}, //2 |
|
75 {ESdpHandler, 4, 2}, //3 |
|
76 {EPairingHandler, 6, 5}, //4 |
|
77 {ESocketHandler, 6, 2}, //5 |
|
78 {EInitHandler, EExitSuccess, 2}, //6 |
|
79 }; |
|
80 |
|
81 //Reconnecting if socket error |
|
82 const struct THandlerProcessItem KErrorRecoveryProcess[]= |
|
83 { |
|
84 {ESocketHandler, EExitSuccess, EExitDisconnected} |
|
85 }; |
|
86 #endif |
|
87 |
|
88 // MODULE DATA STRUCTURES |
|
89 |
|
90 // LOCAL FUNCTION PROTOTYPES |
|
91 |
|
92 // FORWARD DECLARATIONS |
|
93 |
|
94 // ============================= LOCAL FUNCTIONS =============================== |
|
95 |
|
96 // ============================ MEMBER FUNCTIONS =============================== |
|
97 |
|
98 |
|
99 // ----------------------------------------------------------------------------- |
|
100 // CBTGPSConnectManager::NewL |
|
101 // ----------------------------------------------------------------------------- |
|
102 CBTGPSConnectManager* CBTGPSConnectManager::NewL( |
|
103 CBTGPSSettingManager& aSettingManager, |
|
104 CBTGPSDeviceManager& aDeviceManager, |
|
105 CBTGPSConstantsManager& aConstantsManager, |
|
106 CBTGPSDeviceListManager& aDeviceListManager) |
|
107 { |
|
108 CBTGPSConnectManager* self = new (ELeave) CBTGPSConnectManager( |
|
109 aSettingManager, |
|
110 aDeviceManager, |
|
111 aConstantsManager, |
|
112 aDeviceListManager); |
|
113 CleanupStack::PushL(self); |
|
114 self->ConstructL(); |
|
115 CleanupStack::Pop(); |
|
116 return self; |
|
117 } |
|
118 |
|
119 // ----------------------------------------------------------------------------- |
|
120 // CBTGPSConnectManager::~CBTGPSConnectManager |
|
121 // ----------------------------------------------------------------------------- |
|
122 CBTGPSConnectManager::~CBTGPSConnectManager() |
|
123 { |
|
124 delete iMessageReceiver; |
|
125 delete iMessageSender; |
|
126 |
|
127 delete iActiveHandler; |
|
128 |
|
129 if(iIdle!=NULL) |
|
130 { |
|
131 iIdle->Cancel(); |
|
132 delete iIdle; |
|
133 } |
|
134 |
|
135 //Close socket connection |
|
136 iSocket.Close(); |
|
137 iSocketServ.Close(); |
|
138 } |
|
139 |
|
140 |
|
141 // ----------------------------------------------------------------------------- |
|
142 // CBTGPSConnectManager::ConstructL |
|
143 // ----------------------------------------------------------------------------- |
|
144 void CBTGPSConnectManager::ConstructL() |
|
145 { |
|
146 iMessageSender = CBTGPSMessageSender::NewL(iSocket, iDeviceManager); |
|
147 iMessageReceiver = CBTGPSMessageReceiver::NewL(iSocket, iDeviceManager); |
|
148 iIdle = CIdle::NewL(CActive::EPriorityStandard); |
|
149 } |
|
150 |
|
151 // ----------------------------------------------------------------------------- |
|
152 // CBTGPSConnectManager::CBTGPSConnectManager |
|
153 // C++ default constructor can NOT contain any code, that |
|
154 // might leave. |
|
155 // ----------------------------------------------------------------------------- |
|
156 CBTGPSConnectManager::CBTGPSConnectManager( |
|
157 CBTGPSSettingManager& aSettingManager, |
|
158 CBTGPSDeviceManager& aDeviceManager, |
|
159 CBTGPSConstantsManager& aConstantsManager, |
|
160 CBTGPSDeviceListManager& aDeviceListManager) |
|
161 :iSettingManager(aSettingManager), |
|
162 iDeviceManager(aDeviceManager), |
|
163 iConstantsManager(aConstantsManager), |
|
164 iDeviceListManager(aDeviceListManager) |
|
165 { |
|
166 } |
|
167 |
|
168 |
|
169 // ----------------------------------------------------------------------------- |
|
170 // CBTGPSConnectManager::Connecting |
|
171 // ----------------------------------------------------------------------------- |
|
172 TInt CBTGPSConnectManager::Connecting() |
|
173 { |
|
174 TRACESTRING("CBTGPSConnectManager::ConnectingL start...") |
|
175 //Get device connect status |
|
176 TBTDeviceConnectStatus connectStatus = |
|
177 iDeviceManager.DeviceConnectStatus(); |
|
178 |
|
179 switch (connectStatus) |
|
180 { |
|
181 case EBTDeviceDisconnected: |
|
182 //start to connect to BT device |
|
183 break; |
|
184 case EBTDeviceConnecting: |
|
185 case EBTDeviceConnected: |
|
186 //do nothing if it's in connecting or connected status |
|
187 return KErrNone; |
|
188 case EBTDeviceConnectError: |
|
189 //If device error then fail the request with KErrQualityLoss |
|
190 return KErrCouldNotConnect; |
|
191 default: |
|
192 //should never hapen |
|
193 Panic(EPanicInvalidCase); |
|
194 break; |
|
195 } |
|
196 |
|
197 //Start to connect to BT Device |
|
198 StartConnectDevice(); |
|
199 TRACESTRING("CBTGPSConnectManager::ConnectingL end") |
|
200 return KErrNone; |
|
201 } |
|
202 |
|
203 // ----------------------------------------------------------------------------- |
|
204 // CBTGPSConnectManager::Reconnecting |
|
205 // ----------------------------------------------------------------------------- |
|
206 void CBTGPSConnectManager::Reconnecting() |
|
207 { |
|
208 //Get device connect status |
|
209 TBTDeviceConnectStatus connectStatus = |
|
210 iDeviceManager.DeviceConnectStatus(); |
|
211 |
|
212 switch (connectStatus) |
|
213 { |
|
214 case EBTDeviceDisconnected: |
|
215 case EBTDeviceConnectError: |
|
216 //start to connect to BT device |
|
217 break; |
|
218 case EBTDeviceConnecting: |
|
219 case EBTDeviceConnected: |
|
220 //do nothing if it's in connecting or connected status |
|
221 return; |
|
222 default: |
|
223 //should never hapen |
|
224 Panic(EPanicInvalidCase); |
|
225 break; |
|
226 } |
|
227 |
|
228 //Start to connect to BT Device |
|
229 StartConnectDevice(); |
|
230 } |
|
231 |
|
232 // ----------------------------------------------------------------------------- |
|
233 // CBTGPSConnectManager::StartConnectDevice |
|
234 // ----------------------------------------------------------------------------- |
|
235 void CBTGPSConnectManager::StartConnectDevice() |
|
236 { |
|
237 //This function should be return imediately. Thus real functoinalities |
|
238 //are moved to idle callback function |
|
239 |
|
240 //Set connection status |
|
241 iDeviceManager.SetBTDeviceConnectStatus(EBTDeviceConnecting); |
|
242 |
|
243 |
|
244 if(!iIdle->IsActive()) |
|
245 { |
|
246 iIdle->Start(TCallBack(StaticIdleCallback, this)); |
|
247 } |
|
248 } |
|
249 |
|
250 // ----------------------------------------------------------------------------- |
|
251 // CBTGPSConnectManager::StartConnectIdle |
|
252 // ----------------------------------------------------------------------------- |
|
253 void CBTGPSConnectManager::StartConnectIdle() |
|
254 { |
|
255 TRAPD(err, StartConnectIdleL()); |
|
256 if(err!=KErrNone) |
|
257 { |
|
258 iDeviceManager.SetBTDeviceConnectStatus(EBTDeviceConnectError, err); |
|
259 } |
|
260 } |
|
261 |
|
262 // ----------------------------------------------------------------------------- |
|
263 // CBTGPSConnectManager::StartConnectIdleL |
|
264 // ----------------------------------------------------------------------------- |
|
265 void CBTGPSConnectManager::StartConnectIdleL() |
|
266 { |
|
267 //Cancel idle object |
|
268 iIdle->Cancel(); |
|
269 |
|
270 #ifdef SYMBIAN_LOCATION_BTGPSCONFIG |
|
271 //Reset the BT GPS device list |
|
272 iDeviceListManager.RefreshDeviceList(); |
|
273 |
|
274 //Always start the full connecting process even when attempting to reconnect. |
|
275 // This will cause the PSY to attempt to reconnect to the device it was just connected |
|
276 // to (as it will be at the top of the list). Failure to reconnect to this device once |
|
277 // will cause the PSY to continue attempting to connect to the remaining devices in the list. |
|
278 StartConnectingProcessL(KConnectingProcess); |
|
279 |
|
280 #else |
|
281 |
|
282 if(iDeviceManager.IsGpsConnected()) |
|
283 { |
|
284 //Connection error case. Device has already connected, but |
|
285 //might some error happens in socket connection. |
|
286 //Set device disconnected |
|
287 DisconnectBtDevice(); |
|
288 StartConnectingProcessL(KErrorRecoveryProcess); |
|
289 } |
|
290 else |
|
291 { |
|
292 //start connecting process |
|
293 StartConnectingProcessL(KConnectingProcess); |
|
294 } |
|
295 #endif |
|
296 } |
|
297 |
|
298 // ----------------------------------------------------------------------------- |
|
299 // CBTGPSConnectManager::StaticIdleCallback |
|
300 // ----------------------------------------------------------------------------- |
|
301 TInt CBTGPSConnectManager::StaticIdleCallback(TAny* aAny) |
|
302 { |
|
303 reinterpret_cast<CBTGPSConnectManager*>(aAny)->StartConnectIdle(); |
|
304 return KErrNone; //shall not be called again |
|
305 } |
|
306 |
|
307 |
|
308 // ----------------------------------------------------------------------------- |
|
309 // CBTGPSConnectManager::CancelConnecting |
|
310 // ----------------------------------------------------------------------------- |
|
311 void CBTGPSConnectManager::CancelConnecting() |
|
312 { |
|
313 //Stop idle |
|
314 iIdle->Cancel(); |
|
315 |
|
316 //Stop current handler |
|
317 delete iActiveHandler; |
|
318 iActiveHandler = NULL; |
|
319 |
|
320 //Stop current process |
|
321 iActiveProcess = NULL; |
|
322 |
|
323 DisconnectBtDevice(); |
|
324 } |
|
325 |
|
326 // ----------------------------------------------------------------------------- |
|
327 // CBTGPSConnectManager::DisconnectBtDevice |
|
328 // ----------------------------------------------------------------------------- |
|
329 void CBTGPSConnectManager::DisconnectBtDevice() |
|
330 { |
|
331 TRACESTRING("CBTGPSConnectManager::DisconnectBtDevice start...") |
|
332 TInt connectStatus = iDeviceManager.DeviceConnectStatus(); |
|
333 if(connectStatus==EBTDeviceConnected || |
|
334 connectStatus==EBTDeviceConnectError |
|
335 && iSocket.SubSessionHandle()!=0) |
|
336 { |
|
337 //Disconnect from GPS |
|
338 //Update device connection status |
|
339 iDeviceManager.SetBTDeviceConnectStatus( |
|
340 EBTDeviceDisconnected, |
|
341 KErrCancel); |
|
342 |
|
343 //Close socket connection |
|
344 iSocket.Close(); |
|
345 |
|
346 } |
|
347 } |
|
348 |
|
349 // ----------------------------------------------------------------------------- |
|
350 // CBTGPSConnectManager::StartConnectingProcessL |
|
351 // ----------------------------------------------------------------------------- |
|
352 void CBTGPSConnectManager::StartConnectingProcessL(const THandlerProcessItem* aProcess) |
|
353 { |
|
354 //Just construct the first item in the process |
|
355 if(iActiveHandler!=NULL) |
|
356 { |
|
357 delete iActiveHandler; |
|
358 iActiveHandler = NULL; |
|
359 } |
|
360 |
|
361 iActiveProcess = aProcess; |
|
362 |
|
363 TBTGPSHandlerFactory handlerFactory( |
|
364 *this); |
|
365 |
|
366 iCurrentNode = 0; |
|
367 iActiveHandler = handlerFactory.ConstructHandlerL(aProcess->iHandlerId); |
|
368 } |
|
369 |
|
370 // ----------------------------------------------------------------------------- |
|
371 // CBTGPSConnectManager::DeviceManager |
|
372 // ----------------------------------------------------------------------------- |
|
373 CBTGPSDeviceManager& CBTGPSConnectManager::DeviceManager() const |
|
374 { |
|
375 return iDeviceManager; |
|
376 } |
|
377 |
|
378 // ----------------------------------------------------------------------------- |
|
379 // CBTGPSConnectManager::ConstantsManager |
|
380 // ----------------------------------------------------------------------------- |
|
381 CBTGPSConstantsManager& CBTGPSConnectManager::ConstantsManager() const |
|
382 { |
|
383 return iConstantsManager; |
|
384 } |
|
385 |
|
386 // ----------------------------------------------------------------------------- |
|
387 // CBTGPSSettingManager::SettingManager |
|
388 // ----------------------------------------------------------------------------- |
|
389 CBTGPSSettingManager& CBTGPSConnectManager::SettingManager() const |
|
390 { |
|
391 return iSettingManager; |
|
392 } |
|
393 |
|
394 // ----------------------------------------------------------------------------- |
|
395 // CBTGPSSettingManager::DeviceListManager |
|
396 // ----------------------------------------------------------------------------- |
|
397 CBTGPSDeviceListManager& CBTGPSConnectManager::DeviceListManager() const |
|
398 { |
|
399 return iDeviceListManager; |
|
400 } |
|
401 |
|
402 // ----------------------------------------------------------------------------- |
|
403 // CBTGPSConnectManager::GetSocket |
|
404 // ----------------------------------------------------------------------------- |
|
405 void CBTGPSConnectManager::GetSocket( |
|
406 RSocketServ*& aSocketServ, |
|
407 RSocket*& aSocket) |
|
408 { |
|
409 aSocketServ = &iSocketServ; |
|
410 aSocket = &iSocket; |
|
411 } |
|
412 |
|
413 // ----------------------------------------------------------------------------- |
|
414 // CBTGPSConnectManager::HandlerComplete |
|
415 // ----------------------------------------------------------------------------- |
|
416 void CBTGPSConnectManager::HandlerComplete(TInt aId, TInt aErr) |
|
417 { |
|
418 TRACESTRING("CBTGPSConnectManager::HandlerComplete start..") |
|
419 TRACESTRING2("node = %d", iCurrentNode) |
|
420 TRACESTRING2("err= %d", aErr) |
|
421 //Find node in process decision tree |
|
422 const THandlerProcessItem* item = iActiveProcess+iCurrentNode; |
|
423 |
|
424 //Panic in debug if process defination is incorrect |
|
425 __ASSERT_DEBUG(item!=NULL, Panic(EPanicProcessDefination)); |
|
426 |
|
427 //If socket handler succeeds, then set device as connected |
|
428 if( ( aId == ESocketHandler || aId == EPairingHandler ) && aErr==KErrNone) |
|
429 { |
|
430 iDeviceManager.SetBTDeviceConnectStatus( |
|
431 EBTDeviceConnected, |
|
432 aErr); |
|
433 } |
|
434 |
|
435 iLastError = aErr; |
|
436 |
|
437 if(aErr==KErrNone) |
|
438 { |
|
439 iCurrentNode = item->iSuccessNodeIndex; |
|
440 } |
|
441 else |
|
442 { |
|
443 iCurrentNode = item->iFailedNodeIndex; |
|
444 } |
|
445 |
|
446 StartNextHandler(); |
|
447 TRACESTRING("CBTGPSConnectManager::HandlerComplete end") |
|
448 } |
|
449 |
|
450 // ----------------------------------------------------------------------------- |
|
451 // CBTGPSConnectManager:: |
|
452 // ----------------------------------------------------------------------------- |
|
453 void CBTGPSConnectManager::StartNextHandler() |
|
454 { |
|
455 //Delete current handler |
|
456 if(iActiveHandler != NULL) |
|
457 { |
|
458 delete iActiveHandler; |
|
459 iActiveHandler = NULL; |
|
460 } |
|
461 |
|
462 switch(iCurrentNode) |
|
463 { |
|
464 case EExitFailed: |
|
465 case EExitSuccess: |
|
466 case EExitDisconnected: |
|
467 ConnectComplete(iCurrentNode); |
|
468 break; |
|
469 default: |
|
470 { |
|
471 //Start next item |
|
472 const THandlerProcessItem* item = iActiveProcess+iCurrentNode; |
|
473 TBTGPSHandlerFactory handlerFactory(*this); |
|
474 TRAPD(handlerErr, |
|
475 iActiveHandler = handlerFactory.ConstructHandlerL( |
|
476 item->iHandlerId)); |
|
477 if(handlerErr != KErrNone) |
|
478 { |
|
479 iLastError = handlerErr; |
|
480 //handler construction failed, start next handler |
|
481 iCurrentNode = item->iFailedNodeIndex; |
|
482 StartNextHandler(); |
|
483 } |
|
484 } |
|
485 } |
|
486 } |
|
487 |
|
488 // ----------------------------------------------------------------------------- |
|
489 // CBTGPSConnectManager::ConnectComplete |
|
490 // ----------------------------------------------------------------------------- |
|
491 void CBTGPSConnectManager::ConnectComplete(TInt aErr) |
|
492 { |
|
493 //Clear active process |
|
494 iActiveProcess = NULL; |
|
495 |
|
496 switch( aErr ) |
|
497 { |
|
498 case EExitSuccess: |
|
499 //Do nothing here. the device is connected |
|
500 break; |
|
501 case EExitDisconnected: |
|
502 //Set device as disconected. Next location |
|
503 //request will trigger the connecting procedure. |
|
504 iDeviceManager.SetBTDeviceConnectStatus( |
|
505 EBTDeviceDisconnected, |
|
506 iLastError); |
|
507 break; |
|
508 case EExitFailed: |
|
509 default: |
|
510 //Set device as connect error. Next new |
|
511 //subsession will trigger the connecting procedure. |
|
512 iDeviceManager.SetBTDeviceConnectStatus( |
|
513 EBTDeviceConnectError, |
|
514 iLastError); |
|
515 break; |
|
516 } |
|
517 } |
|
518 |
|
519 |
|
520 // ----------------------------------------------------------------------------- |
|
521 // CBTGPSConnectManager::AddMessageListenerL |
|
522 // ----------------------------------------------------------------------------- |
|
523 void CBTGPSConnectManager::AddMessageListenerL(MBTGPSMessageListener& aListener) |
|
524 { |
|
525 iMessageReceiver->AddMessageListenerL(aListener); |
|
526 } |
|
527 |
|
528 // ----------------------------------------------------------------------------- |
|
529 // CBTGPSConnectManager::RemoveMessageListener |
|
530 // ----------------------------------------------------------------------------- |
|
531 void CBTGPSConnectManager::RemoveMessageListener(MBTGPSMessageListener& aListener) |
|
532 { |
|
533 iMessageReceiver->RemoveMessageListener(aListener); |
|
534 } |
|
535 |
|
536 // ----------------------------------------------------------------------------- |
|
537 // CBTGPSConnectManager::SendMessageL |
|
538 // ----------------------------------------------------------------------------- |
|
539 void CBTGPSConnectManager::SendMessageL(const TDesC8& aMsg) |
|
540 { |
|
541 iMessageSender->SendL(aMsg); |
|
542 } |
|
543 |
|
544 // ----------------------------------------------------------------------------- |
|
545 // CBTGPSConnectManager::SendMessage |
|
546 // ----------------------------------------------------------------------------- |
|
547 TInt CBTGPSConnectManager::SendMessage(const TDesC8& aMsg) |
|
548 { |
|
549 TRAPD(err, SendMessageL(aMsg)); |
|
550 return err; |
|
551 } |
|
552 |
|
553 // End of File |
|
554 |
|
555 |
|
556 |