bluetoothmgmt/btmgr/BTManServer/btmanserverburmgr.cpp
changeset 33 4e80e1b997a8
parent 17 907b2fb7aa8e
child 51 20ac952a623c
equal deleted inserted replaced
19:4b81101308c6 33:4e80e1b997a8
    17 #include <e32std.h>
    17 #include <e32std.h>
    18 #include <e32base.h>
    18 #include <e32base.h>
    19 #include <e32property.h>
    19 #include <e32property.h>
    20 #include <f32file.h>
    20 #include <f32file.h>
    21 #include <s32file.h>
    21 #include <s32file.h>
       
    22 #include <bluetooth/hci/hciconsts.h>
    22 #include "btmanserverutil.h"
    23 #include "btmanserverutil.h"
    23 #include "btmanserverburmgr.h"
    24 #include "btmanserverburmgr.h"
    24 #include "BTManServer.h"
    25 #include "BTManServer.h"
    25 
    26 
    26 #include "btmanclient.h"
    27 #include "btmanclient.h"
   304 		}
   305 		}
   305 	}
   306 	}
   306 
   307 
   307 /**
   308 /**
   308 Receives notification that a restore file has been provided by the Secure Backup Engine.
   309 Receives notification that a restore file has been provided by the Secure Backup Engine.
   309 Upon receiving this notification, the restore file is renamed to have the appropriate extension and 
   310 Upon receiving this notification, the restore file is renamed to have the appropriate extension 
   310 the local device name is updated in the registry. The restore of the remote devices table is postponed
   311 The actual restore to the registry is postponed until the next time BTManServer starts, 
   311 until the next time BTManServer starts, so any ongoing BT connections are not affected.
   312 so any ongoing BT connections are not affected.
   312 **/
   313 **/
   313 void CBTManServerBURMgr::RestoreFileReady()
   314 void CBTManServerBURMgr::RestoreFileReady()
   314 	{
   315 	{
   315 	LOG_FUNC
   316 	LOG_FUNC
   316 	__ASSERT_DEBUG(iStateMachine->GetCurrentState() == EBTBURStateRestoreOngoing, InvalidStatePanic(EBTBURStateRestoreOngoing, iStateMachine->GetCurrentState()));
   317 	__ASSERT_DEBUG(iStateMachine->GetCurrentState() == EBTBURStateRestoreOngoing, InvalidStatePanic(EBTBURStateRestoreOngoing, iStateMachine->GetCurrentState()));
   317 
   318 
   318 	// Rename restore file
   319 	// Rename restore file
   319 	RenameBackupFileForRestore();
   320 	RenameBackupFileForRestore();
   320 	// Attempt to update local device name in the registry (best efforts only)
       
   321 	TRAP_IGNORE(UpdateLocalDeviceNameL());
       
   322 	}
   321 	}
   323 
   322 
   324 void CBTManServerBURMgr::HandleStateNormal()
   323 void CBTManServerBURMgr::HandleStateNormal()
   325 	{
   324 	{
   326 	LOG_FUNC
   325 	LOG_FUNC
   522 	LOG_FUNC
   521 	LOG_FUNC
   523 	__ASSERT_DEBUG(iStateMachine->GetCurrentState() == EBTBURStateProcessRestoreFile, InvalidStatePanic(EBTBURStateProcessRestoreFile, iStateMachine->GetCurrentState()));
   522 	__ASSERT_DEBUG(iStateMachine->GetCurrentState() == EBTBURStateProcessRestoreFile, InvalidStatePanic(EBTBURStateProcessRestoreFile, iStateMachine->GetCurrentState()));
   524 
   523 
   525 	// Start restore file processing
   524 	// Start restore file processing
   526 	iRestoreHandler = CBTRestoreHandler::NewL(*this, iBTManServer);
   525 	iRestoreHandler = CBTRestoreHandler::NewL(*this, iBTManServer);
   527 	iRestoreHandler->RestoreRemoteDeviceTableL(iLocalAddr);
   526 	iRestoreHandler->RestoreRegistryL(iLocalAddr);
   528 
   527 
   529 	iStateMachine->TransitionState(EBTBUREventProcessRestoreFileComplete);
   528 	iStateMachine->TransitionState(EBTBUREventProcessRestoreFileComplete);
   530 	}
   529 	}
   531 
   530 
   532 void CBTManServerBURMgr::HandleStateProcessRestoreFileError(TInt aError)
   531 void CBTManServerBURMgr::HandleStateProcessRestoreFileError(TInt aError)
   706 	if (fsSession.Connect() == KErrNone)
   705 	if (fsSession.Connect() == KErrNone)
   707 		{
   706 		{
   708 		static_cast<void>(fsSession.Rename(KBTManServerBackupFileName, KBTManServerRestoreFileName));
   707 		static_cast<void>(fsSession.Rename(KBTManServerBackupFileName, KBTManServerRestoreFileName));
   709 		fsSession.Close();
   708 		fsSession.Close();
   710 		}
   709 		}
   711 	}
       
   712 
       
   713 /**
       
   714 Parses the restore file and updates the loal device name in the registry.
       
   715 This update takes place as soon as the restore file is available, regardless of whether or not the local device
       
   716 address matches that held in the registry. If the local device name has already been set to a non-default value,
       
   717 then it is not modified.
       
   718 **/
       
   719 void CBTManServerBURMgr::UpdateLocalDeviceNameL()
       
   720 	{
       
   721 	LOG_FUNC
       
   722 		
       
   723 	CBTRestoreHandler* restoreHandler = CBTRestoreHandler::NewL(*this, iBTManServer);
       
   724 	CleanupStack::PushL(restoreHandler);
       
   725 
       
   726 	restoreHandler->RestoreLocalDeviceNameL();
       
   727 
       
   728 	CleanupStack::PopAndDestroy(restoreHandler);
       
   729 	}
   710 	}
   730 
   711 
   731 void CBTManServerBURMgr::RunL()
   712 void CBTManServerBURMgr::RunL()
   732 	{
   713 	{
   733 	LOG_FUNC
   714 	LOG_FUNC
   863 	LOG_FUNC
   844 	LOG_FUNC
   864 
   845 
   865 	delete iRegistryData;
   846 	delete iRegistryData;
   866 	}
   847 	}
   867 
   848 
   868 void CBTRestoreHandler::RestoreLocalDeviceNameL()
   849 void CBTRestoreHandler::RestoreRegistryL(TBTDevAddr& aLocalAddr)
   869 	{
       
   870 	LOG_FUNC
       
   871 
       
   872 	LoadRestoreDataL();
       
   873 
       
   874 	// If the local device name is still default, restore without validating the local address
       
   875 	// (otherwise we will not be able to restore this field before the next stack start, which may cause problems with some UIs)
       
   876 	CBTRegistry& registry = iManServer.Registry();
       
   877 	if (iRegistryData->WriteLocalDeviceNameToRegistryL(registry))
       
   878 		{
       
   879 		NotifyLocalTableChange();
       
   880 		}
       
   881 	}
       
   882 
       
   883 void CBTRestoreHandler::RestoreRemoteDeviceTableL(TBTDevAddr& aLocalAddr)
       
   884 	{
   850 	{
   885 	LOG_FUNC
   851 	LOG_FUNC
   886 	__ASSERT_DEBUG(aLocalAddr != TBTDevAddr(), PANIC(KBTBackupPanicCat, EBTBURMgrMissingLocalAddress));
   852 	__ASSERT_DEBUG(aLocalAddr != TBTDevAddr(), PANIC(KBTBackupPanicCat, EBTBURMgrMissingLocalAddress));
   887 
   853 
   888 	LoadRestoreDataL();
   854 	LoadRestoreDataL();
   889     
   855     
   890 	// Compare local address held in restore file with our local address
   856 	// Compare local address held in restore file with our local address
   891 	if (iRegistryData->IsLocalAddressEqualL(aLocalAddr))
   857 	if (iRegistryData->IsLocalAddressEqualL(aLocalAddr))
   892 		{
   858 		{
   893     		// Proceed with restore of remote devices table
   859 		// Proceed with restore
   894 		CBTRegistry& registry = iManServer.Registry();
   860 		CBTRegistry& registry = iManServer.Registry();
       
   861 
       
   862 		if (iRegistryData->WriteLocalDeviceNameToRegistryL(registry))
       
   863 			{
       
   864 			NotifyLocalTableChange();
       
   865 			NotifyLocalDeviceNameChange(iRegistryData->GetLocalDeviceNameL());
       
   866 			}
   895 
   867 
   896 		TInt noRemoteDevices = iRegistryData->CountRemoteDevicesL();
   868 		TInt noRemoteDevices = iRegistryData->CountRemoteDevicesL();
   897 		for (TInt i = 0; i < noRemoteDevices; i++)
   869 		for (TInt i = 0; i < noRemoteDevices; i++)
   898 			{
   870 			{
   899 			if (iRegistryData->WriteRemoteDeviceToRegistryL(registry, i))
   871 			if (iRegistryData->WriteRemoteDeviceToRegistryL(registry, i))
   934 	{
   906 	{
   935 	LOG_FUNC
   907 	LOG_FUNC
   936 	
   908 	
   937 	// Notify the P&S key that the remote devices table has changed
   909 	// Notify the P&S key that the remote devices table has changed
   938 	iManServer.Publish(KPropertyKeyBluetoothGetRegistryTableChange, KRegistryChangeLocalTable);
   910 	iManServer.Publish(KPropertyKeyBluetoothGetRegistryTableChange, KRegistryChangeLocalTable);
       
   911 	}
       
   912 
       
   913 /**
       
   914 Sends a notification that the local device name has been changed.
       
   915 This notification is observable through the P&S key KPRopertyKeyBluetoothSetDeviceName.
       
   916 @param aLocalName The modified local device name as an 8-bit descriptor.
       
   917 **/
       
   918 void CBTRestoreHandler::NotifyLocalDeviceNameChange(const TDesC8& aLocalName)
       
   919 	{
       
   920 	LOG_FUNC
       
   921 	
       
   922 	// The P&S key requires the local device name in unicode format.
       
   923 	TBuf16<KMaxBluetoothNameLen> localNameUniCode;
       
   924 	localNameUniCode.Copy(aLocalName);
       
   925 	
       
   926 	NotifyLocalDeviceNameChange(localNameUniCode);
       
   927 	}
       
   928 
       
   929 /**
       
   930 Sends a notification that the local device name has been changed.
       
   931 This notification is observable through the P&S key KPRopertyKeyBluetoothSetDeviceName.
       
   932 @param aLocalName The modified local device name in unicode format.
       
   933 **/
       
   934 void CBTRestoreHandler::NotifyLocalDeviceNameChange(const TDesC16& aLocalName)
       
   935 	{
       
   936 	LOG_FUNC
       
   937 	
       
   938 	// The KPropertyKeyBluetoothSetDeviceName P&S key may or may not exist at this point.
       
   939 	TInt err = RProperty::Set( KPropertyUidBluetoothCategory, KPropertyKeyBluetoothSetDeviceName, aLocalName);
       
   940 	if (err != KErrNone && err != KErrNotFound)
       
   941 		{
       
   942 		LOG1(_L("CBTRegistryBURData::NotifyLocalDeviceNameChange() - RProperty::Set() failed with %d"), err);
       
   943 		}
   939 	}
   944 	}
   940 
   945 
   941 /**
   946 /**
   942 Sends a notification that a device in the remote devices table has been changed.
   947 Sends a notification that a device in the remote devices table has been changed.
   943 The notification is observable through the P&S key KPropertyKeyBluetoothGetRegistryTableChange. 
   948 The notification is observable through the P&S key KPropertyKeyBluetoothGetRegistryTableChange. 
  1542 	// Mark that we now have registry data 
  1547 	// Mark that we now have registry data 
  1543 	iHasRegistryData = ETrue;
  1548 	iHasRegistryData = ETrue;
  1544 	}
  1549 	}
  1545 
  1550 
  1546 /**
  1551 /**
  1547 Updates the persistence table of the registry with local device name held in this instance
  1552 Updates the persistence table of the registry with local device name held in this instance.
  1548 if the registry currently holds a default name.
  1553 The update is only performed if the local name held in this instance differs from that currently held in the registry.
  1549 @param aRegistry The CBTRegistry instance to use for registry access.
  1554 @param aRegistry The CBTRegistry instance to use for registry access.
  1550 @return ETrue if an update was made to the registry.
  1555 @return ETrue if an update was made to the registry.
  1551 **/
  1556 **/
  1552 TBool CBTRegistryBURData::WriteLocalDeviceNameToRegistryL(CBTRegistry& aRegistry) const
  1557 TBool CBTRegistryBURData::WriteLocalDeviceNameToRegistryL(CBTRegistry& aRegistry) const
  1553 	{
  1558 	{
  1554 	LOG_FUNC
  1559 	LOG_FUNC
  1555 
  1560 
  1556 	TBool updateDone = EFalse;
  1561 	TBool updateDone = EFalse;
  1557 
  1562 	
  1558 	// Update device name only if the registry has a default name 
       
  1559 	TBTLocalDevice defaultDevice;
       
  1560 	TRAP_IGNORE(aRegistry.GetDefaultDeviceFromIniL(defaultDevice));
       
  1561 
       
  1562 	if (!defaultDevice.IsValidDeviceName())
       
  1563 		{
       
  1564 		// Could not obtain a default name - use KDefaultLocalName instead
       
  1565 		defaultDevice.SetDeviceName(KDefaultLocalName);
       
  1566 		}
       
  1567 
       
  1568 	TBTLocalDevice* localDevice = aRegistry.GetLocalDeviceL();
  1563 	TBTLocalDevice* localDevice = aRegistry.GetLocalDeviceL();
  1569 	CleanupStack::PushL(localDevice);
  1564 	CleanupStack::PushL(localDevice);
  1570 
  1565 
  1571 	if (localDevice->DeviceName() == defaultDevice.DeviceName())
  1566 	const TDesC8& localName = GetLocalDeviceNameL();
  1572 		{
  1567 
  1573 		// Local device name is default, update with restored value.
  1568 	if (localDevice->DeviceName() != localName)
  1574 		localDevice->SetDeviceName(GetLocalDeviceNameL());
  1569 		{
       
  1570 		localDevice->SetDeviceName(localName);
  1575 		aRegistry.UpdateLocalDeviceL(*localDevice);
  1571 		aRegistry.UpdateLocalDeviceL(*localDevice);
  1576 		updateDone = ETrue;
  1572 		updateDone = ETrue;
  1577 		}
  1573 		}
  1578 
  1574 
  1579 	CleanupStack::PopAndDestroy(localDevice);
  1575 	CleanupStack::PopAndDestroy(localDevice);
  1598 	
  1594 	
  1599 	// Get device and SID
  1595 	// Get device and SID
  1600 	const CBTDevice& nextRemDevice = GetRemoteDeviceL(aDeviceIndex);
  1596 	const CBTDevice& nextRemDevice = GetRemoteDeviceL(aDeviceIndex);
  1601 	TSecureId nextRemDeviceSid = GetRemoteDeviceEntrySidL(aDeviceIndex);
  1597 	TSecureId nextRemDeviceSid = GetRemoteDeviceEntrySidL(aDeviceIndex);
  1602 
  1598 
  1603 	// Try to add device to registry. If this fails with KErrAlreadExists, then update existing device.
  1599 	// Try to add device to registry. If this fails with KErrAlreadyExists, then update existing device.
  1604 	TRAPD(err, aRegistry.CreateDeviceL(nextRemDevice, nextRemDevice.IsValidUiCookie(), nextRemDeviceSid));
  1600 	TRAPD(err, aRegistry.CreateDeviceL(nextRemDevice, nextRemDevice.IsValidUiCookie(), nextRemDeviceSid));
  1605 
  1601 
  1606 	if (err == KErrNone)
  1602 	if (err == KErrNone)
  1607 		{
  1603 		{
  1608 		// New device added successfully
  1604 		// New device added successfully