datasourcemodules/bluetoothgpspositioningmodule/btgpspsy/src/Connecting/btgpscheckdevicepairing.cpp
changeset 36 b47902b73a93
parent 0 9cfd9a3ee49c
equal deleted inserted replaced
35:a2efdd544abf 36:b47902b73a93
       
     1 // Copyright (c) 2008-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 // ECheckDevicePairing state in the PSY Connecting State Machine; checks to
       
    15 // see whether the current device is paired with
       
    16 //
       
    17 
       
    18 #include <e32std.h>
       
    19 #include <bt_sock.h>
       
    20 #include "btgpscheckdevicepairing.h"
       
    21 #include "BTGPSDeviceManager.h"
       
    22 #include "BTGPSConnectManagerExt.h"
       
    23 #include "BTGPSHandlerFactory.h"
       
    24 #include "BTGPSLogging.h"
       
    25 
       
    26 //Constants
       
    27 const TInt KBTGPSDeviceArrayGranularity = 1;
       
    28 
       
    29 /** Static constructor
       
    30  */
       
    31 CBTGPSCheckDevicePairing* CBTGPSCheckDevicePairing::NewL(MBTGPSConnectManagerExt& aManagerExt)
       
    32 	{
       
    33 	CBTGPSCheckDevicePairing* self = new (ELeave) CBTGPSCheckDevicePairing(aManagerExt);
       
    34 	CleanupStack::PushL(self);
       
    35 	self->ConstructL();
       
    36 	CleanupStack::Pop(self);
       
    37 	return self;
       
    38 	}
       
    39 
       
    40 /** Destructor
       
    41  */
       
    42 CBTGPSCheckDevicePairing::~CBTGPSCheckDevicePairing()
       
    43 	{
       
    44 	if(iIdle)
       
    45 		{
       
    46 		iIdle->Cancel();
       
    47 		delete iIdle;
       
    48 		}
       
    49 	if(iDeviceArray)
       
    50 		{
       
    51         iDeviceArray->ResetAndDestroy();
       
    52         delete iDeviceArray;
       
    53         }
       
    54 	delete iBtDevMan;
       
    55 	}
       
    56 
       
    57 /** Class constructor
       
    58  */
       
    59 CBTGPSCheckDevicePairing::CBTGPSCheckDevicePairing(MBTGPSConnectManagerExt& aManagerExt)
       
    60 : iManagerExt(aManagerExt),
       
    61 iError(KErrNone)
       
    62 	{
       
    63 	}
       
    64 
       
    65 /** Second phase constructor
       
    66  */
       
    67 void CBTGPSCheckDevicePairing::ConstructL()
       
    68 	{
       
    69 	TRACESTRING("CBTGPSCheckDevicePairing::ConstructL start...")
       
    70 	
       
    71 	//construct idle object
       
    72 	iIdle = CIdle::NewL(CActive::EPriorityStandard);
       
    73 	
       
    74 	TBTDeviceType deviceType = iManagerExt.DeviceManager().DeviceType();
       
    75 	
       
    76 	if(deviceType == EBTDeviceTypeNonNokGps || deviceType == EBTDeviceTypeNokGps)
       
    77 		{
       
    78 		//Check to see whether a pairing exists
       
    79 		TRACESTRING("Checking device pairing...")
       
    80 		
       
    81 		//construct BT device manager
       
    82 		iBtDevMan = CBTGPSDevMan::NewL(this);
       
    83 		iDeviceArray = new (ELeave) RBTDeviceArray(KBTGPSDeviceArrayGranularity);
       
    84 		
       
    85 		//Get paired devices
       
    86 		TBTRegistrySearch searchPattern;
       
    87 		searchPattern.FindBonded();
       
    88 		iBtDevMan->GetDevices(searchPattern, iDeviceArray);
       
    89 		}
       
    90 	else
       
    91 		{
       
    92 		//Device type is unknown, so the PSY has not connected to this device before,
       
    93 		// so do not check the device pairing, always go through the pairing process
       
    94 		TRACESTRING("Unknown device, starting pairing process...")
       
    95 		iError = KErrNotFound;
       
    96 		iIdle->Start(TCallBack(HandlerCompleteCallback, this));
       
    97 		}
       
    98 	
       
    99 	TRACESTRING("CBTGPSCheckDevicePairing::ConstructL end...")
       
   100 	}
       
   101 
       
   102 /** Callback function which is called when a list of the currently paired devices has
       
   103  been compiled.  This function loops through the array past into the function to see if
       
   104  the device we are currently using is in this list of paired devices.  If it is, the
       
   105  device is paired with.
       
   106  */
       
   107 void CBTGPSCheckDevicePairing::HandleGetDevicesComplete(TInt aErr, RBTDeviceArray* aDeviceArray)
       
   108 	{
       
   109 	TRACESTRING2("CBTGPSCheckDevicePairing::HandleGetDevicesComplete start...%d", aErr)
       
   110 	TInt err = KErrNotFound;
       
   111 	
       
   112 	if((aErr == KErrNone) && (aDeviceArray))
       
   113 		{
       
   114 		TInt count = aDeviceArray->Count();
       
   115 		TBTSockAddr deviceAddr;
       
   116 		iManagerExt.DeviceManager().BtSockAddr(deviceAddr);
       
   117 		
       
   118 		//Loop through the devices to see if the current device is paired
       
   119 		for (TInt i = 0; i < count; i++)
       
   120 			{
       
   121 			const TBTDevAddr& dev = (*aDeviceArray)[i]->BDAddr();
       
   122 			if(dev == deviceAddr.BTAddr())
       
   123 				{
       
   124 				//BT Device is paired, return with no error
       
   125 				err = KErrNone;
       
   126 				iManagerExt.DeviceManager().SetPaired(ETrue);
       
   127 				
       
   128 				break;
       
   129 				}
       
   130 			}
       
   131 		}
       
   132 	
       
   133 	TRACESTRING2("CBTGPSCheckDevicePairing::HandleGetDevicesComplete end...%d", err)
       
   134 	
       
   135 	if(err != KErrNone)
       
   136 		{
       
   137 		//Device pairing does not exist for this device, so set device type back to unknown
       
   138 		iManagerExt.DeviceManager().SetBTDeviceType(EBTDeviceTypeUnknown);
       
   139 		}
       
   140 	
       
   141 	iError = err;
       
   142 	iIdle->Start(TCallBack(HandlerCompleteCallback, this));
       
   143 	}
       
   144 
       
   145 
       
   146 void CBTGPSCheckDevicePairing::HandleAddDeviceComplete(TInt /*aErr*/)
       
   147 	{
       
   148 	//Do Nothing
       
   149 	}
       
   150 
       
   151 
       
   152 void CBTGPSCheckDevicePairing::HandleDeleteDevicesComplete(TInt /*aErr*/)
       
   153 	{
       
   154 	//Do Nothing
       
   155 	}
       
   156 
       
   157 /** CIdle Handler Complete Callback
       
   158  */
       
   159 TInt CBTGPSCheckDevicePairing::HandlerCompleteCallback(TAny* aAny)
       
   160     {
       
   161     reinterpret_cast<CBTGPSCheckDevicePairing*>(aAny)->HandlerCompleteNotify();
       
   162         
       
   163     return KErrNone;
       
   164     }
       
   165 
       
   166 /** Handler Complete
       
   167  */
       
   168 void CBTGPSCheckDevicePairing::HandlerCompleteNotify()
       
   169     {
       
   170     iManagerExt.HandlerComplete(ECheckDevicePairing, iError);
       
   171     }
       
   172