bluetooth/btstack/linkmgr/eSCOSAP.cpp
changeset 0 29b1cd4cb562
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     1 // Copyright (c) 2003-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 // Implements the eSCO SAP class
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <bluetooth/logger.h>
       
    19 #include <bt_sock.h>
       
    20 #include <bluetooth/hci/hciframe.h>
       
    21 
       
    22 #include "SCOSAP.h"
       
    23 #include "physicallinksmanager.h"
       
    24 #include "physicallinks.h"
       
    25 #include "hcifacade.h"
       
    26 #include "linkutil.h"
       
    27 #include "linkmuxer.h"
       
    28 
       
    29 #ifdef __FLOG_ACTIVE
       
    30 _LIT8(KLogComponent, LOG_COMPONENT_LINKMGR);
       
    31 #endif
       
    32 
       
    33 // ----------------------------------------------------------------------------
       
    34 // eSCO link
       
    35 // ----------------------------------------------------------------------------
       
    36 CeSCOLink* CeSCOLink::NewLC(CPhysicalLinksManager& aConnectionMan, CPhysicalLink* aConnection)
       
    37 	{
       
    38 	CeSCOLink* s = new(ELeave) CeSCOLink(aConnectionMan, aConnection);
       
    39 	CleanupStack::PushL(s);
       
    40 	s->ConstructL();
       
    41 	return s;
       
    42 	}
       
    43 
       
    44 
       
    45 CeSCOLink* CeSCOLink::NewL(CPhysicalLinksManager& aLinksMan, CPhysicalLink* aPhysicalLink)
       
    46 	{
       
    47 	CeSCOLink* s = NewLC(aLinksMan, aPhysicalLink);
       
    48 	CleanupStack::Pop();
       
    49 	return s;
       
    50 	}
       
    51 
       
    52 CeSCOLink::CeSCOLink(CPhysicalLinksManager& aLinksMan, CPhysicalLink* aPhysicalLink)
       
    53 : CBTSynchronousLink(aLinksMan, aPhysicalLink, EeSCOLink)
       
    54 	{
       
    55 	iUserPacketTypes = TBTSyncPackets::ESyncAnyESCOPacket;
       
    56 	}
       
    57 
       
    58 void CeSCOLink::ConstructL()
       
    59 	{
       
    60 	CBTSynchronousLink::ConstructL();
       
    61 	}
       
    62 
       
    63 CeSCOLink::~CeSCOLink()
       
    64 	{
       
    65 	}
       
    66 
       
    67 TInt CeSCOLink::GetOption(TUint aLevel, TUint aName, TDes8& aOption) const
       
    68 	{
       
    69 	if (aLevel != KSolBtESCO)
       
    70 		{
       
    71 		return KErrNotSupported;
       
    72 		}
       
    73 	
       
    74 	switch (aName)
       
    75 		{
       
    76 		case EeSCOExtOptions:
       
    77 			{
       
    78 			TBTeSCOLinkParams* options = reinterpret_cast<TBTeSCOLinkParams*>(const_cast<TUint8*>(aOption.Ptr()));
       
    79 			options->iBandwidth.iTransmit = iTransmitBandwidth;
       
    80 			options->iBandwidth.iReceive = iReceiveBandwidth;
       
    81 			options->iLatency = iMaxLatency;
       
    82 			options->iRetransmissionEffort = iRetransmissionEffort;
       
    83 			options->iCoding = iVoiceSettings;
       
    84 			return KErrNone;
       
    85 			}
       
    86 		
       
    87 		default:
       
    88 			return KErrNotSupported;
       
    89 		}
       
    90 	}
       
    91 
       
    92 TInt CeSCOLink::SAPSetOption(TUint aLevel, TUint aName, const TDesC8& aOption)
       
    93 /**
       
    94 	Case ESyncSetUserPacketTypes:
       
    95 	Store the user specified pakcet types if at least one of the eSCO packet types is set
       
    96 	The packet types bitmask will be passed as a parameter of CPhysicalLink::Connect(const TBTSyncPackets aUserPacketTypes)
       
    97 	in DoActiveOpenL()
       
    98 */
       
    99 	{
       
   100 	switch (aLevel)
       
   101 		{
       
   102 		case KSolBtSCO:
       
   103 			switch (aName)
       
   104 				{
       
   105 				case ESyncUserPacketTypes:
       
   106 					{
       
   107 					TBTSyncPacketTypes optPacketTypes = *reinterpret_cast<const TBTSyncPacketTypes*>(aOption.Ptr());
       
   108 					if (TBTSyncPackets::ESyncAnyESCOPacket & optPacketTypes) //make sure at least one packet type is supported
       
   109 						{
       
   110 						iUserPacketTypes = static_cast<TBTSyncPacketTypes>(optPacketTypes & (TBTSyncPackets::ESyncAnyESCOPacket));//Just eSCO packet types are stored
       
   111 						}
       
   112 					else
       
   113 						{
       
   114 						return KErrNotSupported;
       
   115 						}
       
   116 					
       
   117 					break;
       
   118 					}
       
   119 				
       
   120 				default:
       
   121 					return KErrNotSupported;
       
   122 				}
       
   123 			break;
       
   124 			
       
   125 		case KSolBtESCO:
       
   126 			switch (aName)
       
   127 				{
       
   128 				case EeSCOExtOptions:
       
   129 					{
       
   130 					TBTeSCOLinkParams options = *reinterpret_cast<const TBTeSCOLinkParams*>(aOption.Ptr());
       
   131 					
       
   132 					iTransmitBandwidth	= Max(options.iBandwidth.iTransmit, KMinESCOBandwidth);
       
   133 					iReceiveBandwidth	= Max(options.iBandwidth.iReceive, KMinESCOBandwidth);
       
   134 					iMaxLatency 		= Max(options.iLatency, KMinESCOLatency);
       
   135 					iRetransmissionEffort = options.iRetransmissionEffort;
       
   136 					iVoiceSettings		= options.iCoding;
       
   137 					
       
   138 					break;
       
   139 					}
       
   140 
       
   141 				default:
       
   142 					return KErrNotSupported;
       
   143 				}
       
   144 			break;
       
   145 			
       
   146 		default:
       
   147 			return KErrNotSupported;
       
   148 		}
       
   149 	
       
   150 	return KErrNone;
       
   151 	}
       
   152 
       
   153 
       
   154 TUint CeSCOLink::DoWrite(const TDesC8& aData, TUint /*aOptions*/, TSockAddr* /*aAddr*/)
       
   155 /**
       
   156 	Actually do the write - has been checked for length
       
   157 	In future could write data on a specific SCO "subchannel" if we do that sort of thing
       
   158 	between devices - possibly then use aAddr or aOptions
       
   159 **/
       
   160 	{
       
   161 	TUint retVal = 0;
       
   162 	TInt err = KErrNone;
       
   163 	
       
   164 #ifdef STACK_SCO_DATA
       
   165 	// Trim length of data if necessary
       
   166 	TPtrC8 data = aData.Left(CHctlSynchronousDataFrame::KHCTLMaxSynchronousDataSize);
       
   167 	if (iLinksMan.LinkManagerProtocol().LinkMuxer().CanWriteSCOData())
       
   168 		{
       
   169 		// for now just try to write - if it fails then it doesnt matter - its
       
   170 		// a lot more *s*ynchronous than sticking it in a queue ;-)
       
   171 		LOG(_L("CSCOLink: Can write SCO data..."));
       
   172 		delete iOutboundSCOFrame;
       
   173 		iOutboundSCOFrame = NULL;
       
   174 		const CHCIFacade& facade = iLinksMan.HCIFacade(); 
       
   175 		TRAP(err, iOutboundSCOFrame = facade.NewSynchronousDataFrameL(static_cast<TUint8>(data.Length())));
       
   176 		if(iOutboundSCOFrame && err == KErrNone)
       
   177 			{
       
   178 			// got the frame - so go and use it
       
   179 			facade.FormatSCOData(*iOutboundSCOFrame, iHandle, data);
       
   180 			if (facade.WriteSCOData(*iOutboundSCOFrame) == KErrNone)
       
   181 				{
       
   182 				retVal = data.Length(); // we sent this many bytes
       
   183 				}
       
   184 			}
       
   185 		}
       
   186 	else
       
   187 		{
       
   188 		LOG(_L("CSCOLink: ERROR Cannot write SCO data."));
       
   189 		Socket()->Error(((err == KErrNone) ? KErrInUse : err), MSocketNotify::EErrorSend);
       
   190 		}
       
   191 #else
       
   192 	LOG(_L("CSCOLink: ERROR SCO data via stack not supported.")))
       
   193 	Socket()->Error(KErrNotSupported, MSocketNotify::EErrorSend);
       
   194 #endif
       
   195 	return retVal;
       
   196 	}
       
   197 
       
   198 void CeSCOLink::PacketTypeChange(THCIErrorCode aErr, THCIConnHandle aConnH, TUint16 aNewPacket)
       
   199 	{
       
   200 	iState->PacketTypeChange(*this, aErr, aConnH, aNewPacket);
       
   201 	}
       
   202 
       
   203 TBool CeSCOLink::IsLinkProbablyPossible()
       
   204 	{
       
   205 	TUint minBandwidth = MinBandwidth();
       
   206 	if ((iTransmitBandwidth < minBandwidth) || (iReceiveBandwidth < minBandwidth))
       
   207 		{
       
   208 		return EFalse;
       
   209 		}
       
   210 	
       
   211 	TUint maxBandwidth = MaxBandwidth();
       
   212 	if ((iTransmitBandwidth != KESCOBandwidthDontCare) && (iTransmitBandwidth > maxBandwidth))
       
   213 		{
       
   214 		return EFalse;
       
   215 		}
       
   216 	
       
   217 	if ((iReceiveBandwidth != KESCOBandwidthDontCare) && (iReceiveBandwidth > maxBandwidth))
       
   218 		{
       
   219 		return EFalse;
       
   220 		}
       
   221 	
       
   222 	if (iMaxLatency < KMinESCOLatency)
       
   223 		{
       
   224 		return EFalse;
       
   225 		}
       
   226 	
       
   227 	TBool airCodingOK = EFalse;
       
   228 	TBTFeatures airCodings = iLinksMan.LinkManagerProtocol().PacketsSupportedLocally();
       
   229 	switch (iVoiceSettings & KAirCodingFormatBits)
       
   230 		{
       
   231 		case ECVSD:
       
   232 			airCodingOK = airCodings[ESupportedCVSDBit];
       
   233 			break;
       
   234 			
       
   235 		case EuLawLog:
       
   236 			airCodingOK = airCodings[ESupportedu_lawLogBit];
       
   237 			break;
       
   238 		
       
   239 		case EALawLog:
       
   240 			airCodingOK = airCodings[ESupportedA_lawLogBit];
       
   241 			break;
       
   242 		
       
   243 		case ETransparentData:
       
   244 			airCodingOK = airCodings[ESupportedTransparentSCODataBit];
       
   245 			break;
       
   246 		
       
   247 		default:
       
   248 			break;
       
   249 		}
       
   250 
       
   251 	return airCodingOK;
       
   252 	}
       
   253 
       
   254 
       
   255 TUint CeSCOLink::MinBandwidth()
       
   256 	{
       
   257 	TReal bandwidth = ((KMinESCOPacketSize *  KMilliSecondsFactor) / iMaxLatency);
       
   258 	return static_cast<TUint>(bandwidth);
       
   259 	}
       
   260 
       
   261 
       
   262 TUint CeSCOLink::MaxBandwidth()
       
   263 	{
       
   264 	TInt maxPacketSize = 0;
       
   265 	TInt interval = 1;
       
   266 	
       
   267 	if (iUserPacketTypes & TBTSyncPackets::ESyncPacketsEV5)
       
   268 		{
       
   269 		maxPacketSize = KMaxEV5Size;
       
   270 		interval = KEV5Slots + KEV3Slots;
       
   271 		}
       
   272 	else if (iUserPacketTypes & TBTSyncPackets::ESyncPacketsEV4)
       
   273 		{
       
   274 		maxPacketSize = KMaxEV4Size;
       
   275 		interval = KEV4Slots + KEV3Slots;
       
   276 		}
       
   277 	else if (iUserPacketTypes & TBTSyncPackets::ESyncPacketsEV3)
       
   278 		{
       
   279 		maxPacketSize = KMaxEV3Size;
       
   280 		interval = KEV3Slots + KEV3Slots;
       
   281 		}
       
   282 	
       
   283 	// Simple, easy to read version...
       
   284 	//TReal bandwidth = (maxPacketSize / (KBasebandSlotTime * tesco));
       
   285 	
       
   286 	// Faster, still correct but harder to understand...  Add 1 to force rounding up, just in case.
       
   287 	TUint bandwidth = ((KBasebandSlotsPerSecond * maxPacketSize) / interval) + 1;
       
   288 	return bandwidth;
       
   289 	}
       
   290 
       
   291 
       
   292 TInt CeSCOLink::MakeConnection(CPhysicalLink* aPhysLink)
       
   293 	{
       
   294 	return aPhysLink->SynchronousConnect(iTransmitBandwidth, iReceiveBandwidth,
       
   295 		iMaxLatency, iVoiceSettings,
       
   296 		iRetransmissionEffort, iUserPacketTypes);
       
   297 	}
       
   298 
       
   299 
       
   300 TDes8& CeSCOLink::InboundFrame()
       
   301 	{
       
   302 	return iInboundSCOFrame;
       
   303 	}
       
   304 
       
   305 void CeSCOLink::SetExtOptions(TBTSyncConnectOpts aSyncOpts)
       
   306 	{
       
   307 	iVoiceSettings = aSyncOpts.iAirMode & KAirCodingFormatBits;
       
   308 	
       
   309 	// Using nasty magic numbers as we have a naming clash...  Responses to a command
       
   310 	// use a different numbering scheme to setup commands.
       
   311 	const TInt KCVSDResponse = 2;
       
   312 	if (iVoiceSettings <= KCVSDResponse)
       
   313 		{
       
   314 		if (iVoiceSettings == KCVSDResponse)
       
   315 			{
       
   316 			iVoiceSettings = ECVSD;
       
   317 			}
       
   318 		else
       
   319 			{
       
   320 			iVoiceSettings++;
       
   321 			}
       
   322 		}
       
   323 	
       
   324 	TInt interval = aSyncOpts.iTransmissionInterval;
       
   325 	
       
   326 	TReal latency = interval * KBasebandSlotTime * KMilliSecondsFactor;
       
   327 	iMaxLatency = static_cast<TInt16>(latency + 1);
       
   328 	
       
   329 	iTransmitBandwidth = static_cast<TUint>((KBasebandSlotsPerSecond * aSyncOpts.iTxPacketLength / interval) + 1);
       
   330 	iReceiveBandwidth = static_cast<TUint>((KBasebandSlotsPerSecond * aSyncOpts.iRxPacketLength / interval) + 1);
       
   331 	iRetransmissionEffort = aSyncOpts.iRetransmissionWindow;
       
   332 	}
       
   333 
       
   334 TUint16 CeSCOLink::GetPacketMask() const
       
   335 	{
       
   336 	return iUserPacketTypes;
       
   337 	}
       
   338 
       
   339 void CeSCOLink::GetExtOptions(TBTeSCOLinkParams& aOptions) const
       
   340 	{
       
   341 	aOptions.iBandwidth.iTransmit = iTransmitBandwidth;
       
   342 	aOptions.iBandwidth.iReceive = iReceiveBandwidth;
       
   343 	aOptions.iLatency = iMaxLatency;
       
   344 	aOptions.iRetransmissionEffort = iRetransmissionEffort;
       
   345 	aOptions.iCoding = iVoiceSettings;
       
   346 	}