bluetooth/btstack/rfcomm/rfcommstates.cpp
changeset 21 5e5528a288fe
parent 16 0089b2f7ebd8
child 45 99439b07e980
equal deleted inserted replaced
19:4b81101308c6 21:5e5528a288fe
  1057 
  1057 
  1058 	aSAP.SetProxyForRemoteCredit(params.iInitialCredit); // set up our proxy
  1058 	aSAP.SetProxyForRemoteCredit(params.iInitialCredit); // set up our proxy
  1059 #ifdef _DEBUG
  1059 #ifdef _DEBUG
  1060 	aSAP.iProxyForRemoteCreditsSupplied = params.iInitialCredit;
  1060 	aSAP.iProxyForRemoteCreditsSupplied = params.iInitialCredit;
  1061 #endif
  1061 #endif
  1062 	aSAP.iMTU=params.iMaxFrameSize;  // Remember the newly calculated MTU
  1062 	aSAP.iNegotiatedMTU=params.iMaxFrameSize;  // Remember the newly calculated MTU
  1063 	LOG2(_L("RFCOMM: Sending PN (Calculated MTU %d from frame size %d)"), aSAP.iMTU, aSAP.iMux->GetMaxDataSize());	
  1063 	aSAP.iOptimalMTUForSending=params.iMaxFrameSize;  // Remember the newly calculated MTU
       
  1064 
       
  1065 	LOG2(_L("RFCOMM: Sending PN (Calculated MTU %d from frame size %d)"), aSAP.iNegotiatedMTU, aSAP.iMux->GetMaxDataSize());	
  1064 	
  1066 	
  1065 	TInt error=aSAP.iMux->SendPN(aSAP, params);
  1067 	TInt error=aSAP.iMux->SendPN(aSAP, params);
  1066 
  1068 
  1067 	if(error!=KErrNone)
  1069 	if(error!=KErrNone)
  1068 		{
  1070 		{
  1104 		TPckgBuf<TInt> restrictedMtu(aParams.iMaxFrameSize + KMaxFrameOverhead);
  1106 		TPckgBuf<TInt> restrictedMtu(aParams.iMaxFrameSize + KMaxFrameOverhead);
  1105 		error = aSAP.GetOption(KSolBtL2CAP, KL2CAPOutboundMTUForBestPerformanceWithRestriction, restrictedMtu);
  1107 		error = aSAP.GetOption(KSolBtL2CAP, KL2CAPOutboundMTUForBestPerformanceWithRestriction, restrictedMtu);
  1106 
  1108 
  1107 		// If we get an error continue but with a possible non-optimal MTU value. iMTU is the max
  1109 		// If we get an error continue but with a possible non-optimal MTU value. iMTU is the max
  1108 		// data size so remove the max Rfcomm header size as these will get added later.
  1110 		// data size so remove the max Rfcomm header size as these will get added later.
  1109  		aSAP.iMTU = (error == KErrNone) ? restrictedMtu() - KMaxFrameOverhead : aParams.iMaxFrameSize;
  1111  		aSAP.iOptimalMTUForSending = (error == KErrNone) ? restrictedMtu() - KMaxFrameOverhead : aParams.iMaxFrameSize;
       
  1112  		aSAP.iNegotiatedMTU = aParams.iMaxFrameSize;
  1110  		error = KErrNone;
  1113  		error = KErrNone;
  1111  		
  1114  		
  1112 		}
  1115 		}
  1113 	else
  1116 	else
  1114 		{
  1117 		{
  1154 		}
  1157 		}
  1155 	else
  1158 	else
  1156 		{
  1159 		{
  1157 		TRAPD(err, 
  1160 		TRAPD(err, 
  1158 			  aSAP.iDataBuffer.SetLengthL(aSAP.iMux->FlowStrategy()->DataBufferMultiple()*
  1161 			  aSAP.iDataBuffer.SetLengthL(aSAP.iMux->FlowStrategy()->DataBufferMultiple()*
  1159 										  aSAP.iMTU));
  1162 										  aSAP.iNegotiatedMTU));
  1160 		if(err != KErrNone)
  1163 		if(err != KErrNone)
  1161 			{
  1164 			{
  1162 			// Failed to alloc, so fail this connect and error
  1165 			// Failed to alloc, so fail this connect and error
  1163 			SignalError(aSAP, err, MSocketNotify::EErrorConnect);
  1166 			SignalError(aSAP, err, MSocketNotify::EErrorConnect);
  1164 			ChangeState(aSAP, CRfcommStateFactory::EClosed);
  1167 			ChangeState(aSAP, CRfcommStateFactory::EClosed);
  1165 			return;
  1168 			return;
  1166 			}
  1169 			}
  1167 		
  1170 		
  1168 		aSAP.iHighTideMark=KRfcommSAPBufferHighMultiple*(aSAP.iMTU);
  1171 		aSAP.iHighTideMark=KRfcommSAPBufferHighMultiple*(aSAP.iNegotiatedMTU);
  1169 		aSAP.iLowTideMark=KRfcommSAPBufferLowMultiple*(aSAP.iMTU);
  1172 		aSAP.iLowTideMark=KRfcommSAPBufferLowMultiple*(aSAP.iNegotiatedMTU);
  1170 
  1173 
  1171 		
  1174 		
  1172 		ChangeState(aSAP, CRfcommStateFactory::EOpen);
  1175 		ChangeState(aSAP, CRfcommStateFactory::EOpen);
  1173 		}
  1176 		}
  1174 	}
  1177 	}
  1408 void TRfcommStateWaitForSABM::SABM(CRfcommSAP& aSAP, CRfcommMuxer& aMux, TUint8 aDLCI)
  1411 void TRfcommStateWaitForSABM::SABM(CRfcommSAP& aSAP, CRfcommMuxer& aMux, TUint8 aDLCI)
  1409 	/**
  1412 	/**
  1410 	We received a SABM - Tell our parent SAP that we're connected
  1413 	We received a SABM - Tell our parent SAP that we're connected
  1411 	**/
  1414 	**/
  1412 	{
  1415 	{
  1413 	if(aSAP.iMTU==0)	//	i.e. We've not entered into any negotiations...
  1416 	if(aSAP.iNegotiatedMTU==0)	//	i.e. We've not entered into any negotiations...
  1414 		aSAP.iMTU=KRfcommDefaultMTU;	//	...so set to default.
  1417 		{
       
  1418 		aSAP.iNegotiatedMTU=KRfcommDefaultMTU;	//	...so set to default.
       
  1419 		aSAP.iOptimalMTUForSending=KRfcommDefaultMTU;
       
  1420 		}
  1415 
  1421 
  1416 	//	We can set up the Data Buffer since MTU negotiations are now over.
  1422 	//	We can set up the Data Buffer since MTU negotiations are now over.
  1417 	//TRAPD(err, aSAP.iDataBuffer.SetLengthL(KRfcommSAPBufferMultiple*aSAP.iMTU));
  1423 	//TRAPD(err, aSAP.iDataBuffer.SetLengthL(KRfcommSAPBufferMultiple*aSAP.iMTU));
  1418 		TRAPD(err, 
  1424 		TRAPD(err, 
  1419 			  aSAP.iDataBuffer.SetLengthL(aMux.FlowStrategy()->DataBufferMultiple()*
  1425 			  aSAP.iDataBuffer.SetLengthL(aMux.FlowStrategy()->DataBufferMultiple()*
  1420 										  aSAP.iMTU));
  1426 										  aSAP.iNegotiatedMTU));
  1421 	if(err != KErrNone)
  1427 	if(err != KErrNone)
  1422 		{
  1428 		{
  1423 		// Failed to alloc, so fail this connect and error
  1429 		// Failed to alloc, so fail this connect and error
  1424 		aMux.SendDM(aDLCI);
  1430 		aMux.SendDM(aDLCI);
  1425 		aSAP.iParentSAP->ChildConnectFailed(aSAP);	//	Will delete us...	
  1431 		aSAP.iParentSAP->ChildConnectFailed(aSAP);	//	Will delete us...	
  1426 		return;
  1432 		return;
  1427 		}
  1433 		}
  1428 	
  1434 	
  1429 	aSAP.iHighTideMark=KRfcommSAPBufferHighMultiple*(aSAP.iMTU);
  1435 	aSAP.iHighTideMark=KRfcommSAPBufferHighMultiple*(aSAP.iNegotiatedMTU);
  1430 	aSAP.iLowTideMark=KRfcommSAPBufferLowMultiple*(aSAP.iMTU);
  1436 	aSAP.iLowTideMark=KRfcommSAPBufferLowMultiple*(aSAP.iNegotiatedMTU);
  1431 
  1437 
  1432 	ChangeState(aSAP,CRfcommStateFactory::EWaitForIncomingSecurityCheck);
  1438 	ChangeState(aSAP,CRfcommStateFactory::EWaitForIncomingSecurityCheck);
  1433 	}
  1439 	}
  1434 
  1440 
  1435 #ifndef _DEBUG
  1441 #ifndef _DEBUG
  1439 void TRfcommStateWaitForSABM::PN(CRfcommSAP& aSAP, TRfcommPortParams& aParams, 
  1445 void TRfcommStateWaitForSABM::PN(CRfcommSAP& aSAP, TRfcommPortParams& aParams, 
  1440 								 CRfcommMuxer& aMux, TUint8 /*aDLCI*/)
  1446 								 CRfcommMuxer& aMux, TUint8 /*aDLCI*/)
  1441 #endif
  1447 #endif
  1442 	{
  1448 	{
  1443 	__ASSERT_DEBUG(aSAP.iMux==&aMux,PanicInState(ERfcommInvalidMuxInSAP));
  1449 	__ASSERT_DEBUG(aSAP.iMux==&aMux,PanicInState(ERfcommInvalidMuxInSAP));
  1444 	if(aSAP.iMTU==0)
  1450 	if(aSAP.iNegotiatedMTU==0)
  1445 		{
  1451 		{
  1446 		//	We have no MTU set for the SAP at the moment. Try to determine
  1452 		//	We have no MTU set for the SAP at the moment. Try to determine
  1447 		//	the upper limit of what we can handle
  1453 		//	the upper limit of what we can handle
  1448 		aSAP.iMTU=aSAP.MaximumMTU();
  1454 		aSAP.iNegotiatedMTU=aSAP.MaximumMTU();
       
  1455 		aSAP.iOptimalMTUForSending= aSAP.NegotiatedMTU();
  1449 		}
  1456 		}
  1450 		
  1457 		
  1451 	if(aParams.iMaxFrameSize>aSAP.iMTU || aParams.iMaxFrameSize<=0)
  1458 	if(aParams.iMaxFrameSize>aSAP.iNegotiatedMTU || aParams.iMaxFrameSize<=0)
  1452 		{
  1459 		{
  1453 		//	Either the remote device wants a larger MTU or has provided us
  1460 		//	Either the remote device wants a larger MTU or has provided us
  1454 		//	with a nonsensical <=0 one.
  1461 		//	with a nonsensical <=0 one.
  1455 		//	Either way, try to negotiate the MTU to our maximum
  1462 		//	Either way, try to negotiate the MTU to our maximum
  1456 		aParams.iMaxFrameSize=aSAP.iMTU;
  1463 		aParams.iMaxFrameSize=aSAP.iNegotiatedMTU;
  1457 		}
  1464 		}
  1458 	else
  1465 	else
  1459 		{
  1466 		{
  1460 		
  1467 		
  1461 		// We have an MTU value so ask L2Cap to work out the optimal MTU value based on the one
  1468 		// We have an MTU value so ask L2Cap to work out the optimal MTU value based on the one
  1466 		TInt err = aSAP.GetOption(KSolBtL2CAP, KL2CAPOutboundMTUForBestPerformanceWithRestriction, restrictedMtu);
  1473 		TInt err = aSAP.GetOption(KSolBtL2CAP, KL2CAPOutboundMTUForBestPerformanceWithRestriction, restrictedMtu);
  1467 
  1474 
  1468 		
  1475 		
  1469 		// If we get an error continue but with a possible non-optimal MTU value. iMTU is the max
  1476 		// If we get an error continue but with a possible non-optimal MTU value. iMTU is the max
  1470 		// data size so remove the max Rfcomm header size as these will get added later.
  1477 		// data size so remove the max Rfcomm header size as these will get added later.
  1471  		aSAP.iMTU = (err == KErrNone) ? restrictedMtu() - KMaxFrameOverhead : aParams.iMaxFrameSize;
  1478  		aSAP.iOptimalMTUForSending = (err == KErrNone) ? restrictedMtu() - KMaxFrameOverhead : aParams.iMaxFrameSize;
       
  1479  		aSAP.iNegotiatedMTU = aParams.iMaxFrameSize;
  1472  		}	
  1480  		}	
  1473 
  1481 
  1474 	if(aParams.iCreditIndicator == (KCBFCCommandFlag >> 4))
  1482 	if(aParams.iCreditIndicator == (KCBFCCommandFlag >> 4))
  1475 		{
  1483 		{
  1476 		// Remote device is using CBFC. Set up the credit counts
  1484 		// Remote device is using CBFC. Set up the credit counts
  1907 		}
  1915 		}
  1908 	
  1916 	
  1909 	TInt storelen=aSAP.DataBuffer().Add(aData.Ptr(), aData.Length());
  1917 	TInt storelen=aSAP.DataBuffer().Add(aData.Ptr(), aData.Length());
  1910 	if(storelen != aData.Length())
  1918 	if(storelen != aData.Length())
  1911 		{
  1919 		{
       
  1920 		__ASSERT_DEBUG(EFalse, PanicInState(ERfCommStateBufferFull));
  1912 		// We've not got room for some of the data
  1921 		// We've not got room for some of the data
  1913 		LOG(_L("RFCOMM: Warning received data loss"));
  1922 		LOG(_L("RFCOMM: Received data loss - disconnecting"));
       
  1923 		aSAP.iSocket->Disconnect();
       
  1924 		ChangeState(aSAP, CRfcommStateFactory::EClosed);
       
  1925 		return;
  1914 		}
  1926 		}
  1915 
  1927 
  1916 	// Check to see if we need to quench the source
  1928 	// Check to see if we need to quench the source
  1917 	LOG1(_L("RFCOMM: Data buffer level %d"), aSAP.DataBuffer().Count());
  1929 	LOG1(_L("RFCOMM: Data buffer level %d"), aSAP.DataBuffer().Count());
  1918 
  1930