bluetoothmgmt/btmgr/BTManServer/btmanserverburmgr.cpp
changeset 21 5e5528a288fe
parent 17 907b2fb7aa8e
child 32 f72906e669b4
equal deleted inserted replaced
19:4b81101308c6 21:5e5528a288fe
   304 		}
   304 		}
   305 	}
   305 	}
   306 
   306 
   307 /**
   307 /**
   308 Receives notification that a restore file has been provided by the Secure Backup Engine.
   308 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 
   309 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
   310 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.
   311 so any ongoing BT connections are not affected.
   312 **/
   312 **/
   313 void CBTManServerBURMgr::RestoreFileReady()
   313 void CBTManServerBURMgr::RestoreFileReady()
   314 	{
   314 	{
   315 	LOG_FUNC
   315 	LOG_FUNC
   316 	__ASSERT_DEBUG(iStateMachine->GetCurrentState() == EBTBURStateRestoreOngoing, InvalidStatePanic(EBTBURStateRestoreOngoing, iStateMachine->GetCurrentState()));
   316 	__ASSERT_DEBUG(iStateMachine->GetCurrentState() == EBTBURStateRestoreOngoing, InvalidStatePanic(EBTBURStateRestoreOngoing, iStateMachine->GetCurrentState()));
   317 
   317 
   318 	// Rename restore file
   318 	// Rename restore file
   319 	RenameBackupFileForRestore();
   319 	RenameBackupFileForRestore();
   320 	// Attempt to update local device name in the registry (best efforts only)
       
   321 	TRAP_IGNORE(UpdateLocalDeviceNameL());
       
   322 	}
   320 	}
   323 
   321 
   324 void CBTManServerBURMgr::HandleStateNormal()
   322 void CBTManServerBURMgr::HandleStateNormal()
   325 	{
   323 	{
   326 	LOG_FUNC
   324 	LOG_FUNC
   522 	LOG_FUNC
   520 	LOG_FUNC
   523 	__ASSERT_DEBUG(iStateMachine->GetCurrentState() == EBTBURStateProcessRestoreFile, InvalidStatePanic(EBTBURStateProcessRestoreFile, iStateMachine->GetCurrentState()));
   521 	__ASSERT_DEBUG(iStateMachine->GetCurrentState() == EBTBURStateProcessRestoreFile, InvalidStatePanic(EBTBURStateProcessRestoreFile, iStateMachine->GetCurrentState()));
   524 
   522 
   525 	// Start restore file processing
   523 	// Start restore file processing
   526 	iRestoreHandler = CBTRestoreHandler::NewL(*this, iBTManServer);
   524 	iRestoreHandler = CBTRestoreHandler::NewL(*this, iBTManServer);
   527 	iRestoreHandler->RestoreRemoteDeviceTableL(iLocalAddr);
   525 	iRestoreHandler->RestoreRegistryL(iLocalAddr);
   528 
   526 
   529 	iStateMachine->TransitionState(EBTBUREventProcessRestoreFileComplete);
   527 	iStateMachine->TransitionState(EBTBUREventProcessRestoreFileComplete);
   530 	}
   528 	}
   531 
   529 
   532 void CBTManServerBURMgr::HandleStateProcessRestoreFileError(TInt aError)
   530 void CBTManServerBURMgr::HandleStateProcessRestoreFileError(TInt aError)
   706 	if (fsSession.Connect() == KErrNone)
   704 	if (fsSession.Connect() == KErrNone)
   707 		{
   705 		{
   708 		static_cast<void>(fsSession.Rename(KBTManServerBackupFileName, KBTManServerRestoreFileName));
   706 		static_cast<void>(fsSession.Rename(KBTManServerBackupFileName, KBTManServerRestoreFileName));
   709 		fsSession.Close();
   707 		fsSession.Close();
   710 		}
   708 		}
   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 	}
   709 	}
   730 
   710 
   731 void CBTManServerBURMgr::RunL()
   711 void CBTManServerBURMgr::RunL()
   732 	{
   712 	{
   733 	LOG_FUNC
   713 	LOG_FUNC
   863 	LOG_FUNC
   843 	LOG_FUNC
   864 
   844 
   865 	delete iRegistryData;
   845 	delete iRegistryData;
   866 	}
   846 	}
   867 
   847 
   868 void CBTRestoreHandler::RestoreLocalDeviceNameL()
   848 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 	{
   849 	{
   885 	LOG_FUNC
   850 	LOG_FUNC
   886 	__ASSERT_DEBUG(aLocalAddr != TBTDevAddr(), PANIC(KBTBackupPanicCat, EBTBURMgrMissingLocalAddress));
   851 	__ASSERT_DEBUG(aLocalAddr != TBTDevAddr(), PANIC(KBTBackupPanicCat, EBTBURMgrMissingLocalAddress));
   887 
   852 
   888 	LoadRestoreDataL();
   853 	LoadRestoreDataL();
   889     
   854     
   890 	// Compare local address held in restore file with our local address
   855 	// Compare local address held in restore file with our local address
   891 	if (iRegistryData->IsLocalAddressEqualL(aLocalAddr))
   856 	if (iRegistryData->IsLocalAddressEqualL(aLocalAddr))
   892 		{
   857 		{
   893     		// Proceed with restore of remote devices table
   858 		// Proceed with restore
   894 		CBTRegistry& registry = iManServer.Registry();
   859 		CBTRegistry& registry = iManServer.Registry();
       
   860 
       
   861 		if (iRegistryData->WriteLocalDeviceNameToRegistryL(registry))
       
   862 			{
       
   863 			NotifyLocalTableChange();
       
   864 			NotifyLocalDeviceNameChange(iRegistryData->GetLocalDeviceNameL());
       
   865 			}
   895 
   866 
   896 		TInt noRemoteDevices = iRegistryData->CountRemoteDevicesL();
   867 		TInt noRemoteDevices = iRegistryData->CountRemoteDevicesL();
   897 		for (TInt i = 0; i < noRemoteDevices; i++)
   868 		for (TInt i = 0; i < noRemoteDevices; i++)
   898 			{
   869 			{
   899 			if (iRegistryData->WriteRemoteDeviceToRegistryL(registry, i))
   870 			if (iRegistryData->WriteRemoteDeviceToRegistryL(registry, i))
   934 	{
   905 	{
   935 	LOG_FUNC
   906 	LOG_FUNC
   936 	
   907 	
   937 	// Notify the P&S key that the remote devices table has changed
   908 	// Notify the P&S key that the remote devices table has changed
   938 	iManServer.Publish(KPropertyKeyBluetoothGetRegistryTableChange, KRegistryChangeLocalTable);
   909 	iManServer.Publish(KPropertyKeyBluetoothGetRegistryTableChange, KRegistryChangeLocalTable);
       
   910 	}
       
   911 
       
   912 /**
       
   913 Sends a notification that the local device name has been changed.
       
   914 This notification is observable through the P&S key KPRopertyKeyBluetoothSetDeviceName.
       
   915 @param aLocalName The modified local device name as an 8-bit descriptor.
       
   916 **/
       
   917 void CBTRestoreHandler::NotifyLocalDeviceNameChange(const TDesC8& aLocalName)
       
   918 	{
       
   919 	LOG_FUNC
       
   920 	
       
   921 	// The P&S key requires the local device name in unicode format.
       
   922 	TBuf16<KMaxBluetoothNameLen> localNameUniCode;
       
   923 	localNameUniCode.Copy(aLocalName);
       
   924 	
       
   925 	NotifyLocalDeviceNameChange(localNameUniCode);
       
   926 	}
       
   927 
       
   928 /**
       
   929 Sends a notification that the local device name has been changed.
       
   930 This notification is observable through the P&S key KPRopertyKeyBluetoothSetDeviceName.
       
   931 @param aLocalName The modified local device name in unicode format.
       
   932 **/
       
   933 void CBTRestoreHandler::NotifyLocalDeviceNameChange(const TDesC16& aLocalName)
       
   934 	{
       
   935 	LOG_FUNC
       
   936 	
       
   937 	// The KPropertyKeyBluetoothSetDeviceName P&S key may or may not exist at this point.
       
   938 	TInt err = RProperty::Set( KPropertyUidBluetoothCategory, KPropertyKeyBluetoothSetDeviceName, aLocalName);
       
   939 	if (err != KErrNone && err != KErrNotFound)
       
   940 		{
       
   941 		LOG1(_L("CBTRegistryBURData::NotifyLocalDeviceNameChange() - RProperty::Set() failed with %d"), err);
       
   942 		}
   939 	}
   943 	}
   940 
   944 
   941 /**
   945 /**
   942 Sends a notification that a device in the remote devices table has been changed.
   946 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. 
   947 The notification is observable through the P&S key KPropertyKeyBluetoothGetRegistryTableChange. 
  1542 	// Mark that we now have registry data 
  1546 	// Mark that we now have registry data 
  1543 	iHasRegistryData = ETrue;
  1547 	iHasRegistryData = ETrue;
  1544 	}
  1548 	}
  1545 
  1549 
  1546 /**
  1550 /**
  1547 Updates the persistence table of the registry with local device name held in this instance
  1551 Updates the persistence table of the registry with local device name held in this instance.
  1548 if the registry currently holds a default name.
  1552 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.
  1553 @param aRegistry The CBTRegistry instance to use for registry access.
  1550 @return ETrue if an update was made to the registry.
  1554 @return ETrue if an update was made to the registry.
  1551 **/
  1555 **/
  1552 TBool CBTRegistryBURData::WriteLocalDeviceNameToRegistryL(CBTRegistry& aRegistry) const
  1556 TBool CBTRegistryBURData::WriteLocalDeviceNameToRegistryL(CBTRegistry& aRegistry) const
  1553 	{
  1557 	{
  1554 	LOG_FUNC
  1558 	LOG_FUNC
  1555 
  1559 
  1556 	TBool updateDone = EFalse;
  1560 	TBool updateDone = EFalse;
  1557 
  1561 	
  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();
  1562 	TBTLocalDevice* localDevice = aRegistry.GetLocalDeviceL();
  1569 	CleanupStack::PushL(localDevice);
  1563 	CleanupStack::PushL(localDevice);
  1570 
  1564 
  1571 	if (localDevice->DeviceName() == defaultDevice.DeviceName())
  1565 	const TDesC8& localName = GetLocalDeviceNameL();
  1572 		{
  1566 
  1573 		// Local device name is default, update with restored value.
  1567 	if (localDevice->DeviceName() != localName)
  1574 		localDevice->SetDeviceName(GetLocalDeviceNameL());
  1568 		{
       
  1569 		localDevice->SetDeviceName(localName);
  1575 		aRegistry.UpdateLocalDeviceL(*localDevice);
  1570 		aRegistry.UpdateLocalDeviceL(*localDevice);
  1576 		updateDone = ETrue;
  1571 		updateDone = ETrue;
  1577 		}
  1572 		}
  1578 
  1573 
  1579 	CleanupStack::PopAndDestroy(localDevice);
  1574 	CleanupStack::PopAndDestroy(localDevice);
  1598 	
  1593 	
  1599 	// Get device and SID
  1594 	// Get device and SID
  1600 	const CBTDevice& nextRemDevice = GetRemoteDeviceL(aDeviceIndex);
  1595 	const CBTDevice& nextRemDevice = GetRemoteDeviceL(aDeviceIndex);
  1601 	TSecureId nextRemDeviceSid = GetRemoteDeviceEntrySidL(aDeviceIndex);
  1596 	TSecureId nextRemDeviceSid = GetRemoteDeviceEntrySidL(aDeviceIndex);
  1602 
  1597 
  1603 	// Try to add device to registry. If this fails with KErrAlreadExists, then update existing device.
  1598 	// Try to add device to registry. If this fails with KErrAlreadyExists, then update existing device.
  1604 	TRAPD(err, aRegistry.CreateDeviceL(nextRemDevice, nextRemDevice.IsValidUiCookie(), nextRemDeviceSid));
  1599 	TRAPD(err, aRegistry.CreateDeviceL(nextRemDevice, nextRemDevice.IsValidUiCookie(), nextRemDeviceSid));
  1605 
  1600 
  1606 	if (err == KErrNone)
  1601 	if (err == KErrNone)
  1607 		{
  1602 		{
  1608 		// New device added successfully
  1603 		// New device added successfully