bluetoothengine/btsac/src/btsacStateConfiguring.cpp
changeset 0 f63038272f30
child 1 6a1fe72036e3
equal deleted inserted replaced
-1:000000000000 0:f63038272f30
       
     1 /*
       
     2 * Copyright (c) 2002-2005 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  In this state, a stereo audio accessory is configuring us.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include "btsacStateConfiguring.h"
       
    23 #include "btsacStateConfigured.h"
       
    24 #include "btsacStateListening.h"
       
    25 #include "btsacStateAborting.h"
       
    26 #include "btsacactive.h"
       
    27 #include "btsacGavdp.h"
       
    28 #include "btsacSEPManager.h"
       
    29 #include "btsacStreamerController.h"
       
    30 #include "debug.h"
       
    31 
       
    32 // A2DP codec-specific element definitions
       
    33 // in bluetoothAV.h
       
    34 using namespace SymbianBluetoothAV;   
       
    35 
       
    36 // Subband codec-specific values
       
    37 // in bluetoothAV.h
       
    38 using namespace SymbianSBC;   
       
    39 
       
    40 
       
    41 // ================= MEMBER FUNCTIONS =======================
       
    42 
       
    43 // -----------------------------------------------------------------------------
       
    44 // CBtsacConfiguring::NewL
       
    45 // -----------------------------------------------------------------------------
       
    46 //
       
    47 CBtsacConfiguring* CBtsacConfiguring::NewL(CBTSAController& aParent, TSEID aLocalSEID, TSEID aRemoteSEID)
       
    48     {
       
    49    	CBtsacConfiguring* self = new( ELeave ) CBtsacConfiguring(aParent, aLocalSEID, aRemoteSEID);
       
    50     return self;
       
    51    	}
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // CBtsacConfiguring::CBtsacConfiguring
       
    55 // -----------------------------------------------------------------------------
       
    56 //
       
    57 CBtsacConfiguring::CBtsacConfiguring(CBTSAController& aParent, TSEID aLocalSEID, TSEID aRemoteSEID)
       
    58 :   CBtsacState(aParent, EStateConfiguring), iLocalSEID(aLocalSEID), iRemoteSEID(aRemoteSEID),
       
    59 	iSEPFound(EFalse), iRemoteSEPIndex(0), iAudioOpenedBy(EAudioOpenedByNone)
       
    60     {
       
    61     }
       
    62   
       
    63 // -----------------------------------------------------------------------------
       
    64 // CBtsacConfiguring::~CBtsacConfiguring
       
    65 // -----------------------------------------------------------------------------
       
    66 //    
       
    67 CBtsacConfiguring::~CBtsacConfiguring()
       
    68     {
       
    69     TRACE_FUNC
       
    70     }
       
    71 
       
    72 // -----------------------------------------------------------------------------
       
    73 // CBtsacConfiguring::EnterL
       
    74 // -----------------------------------------------------------------------------
       
    75 //
       
    76 void CBtsacConfiguring::EnterL()
       
    77     {
       
    78 	TRACE_STATE(_L("[BTSAC State] Configuring"))
       
    79 	_LIT(KName, "CBtsacStateConfiguring");
       
    80 	const TDesC& Name = KName;
       
    81 	Parent().iGavdp->RegisterObserver(this, Name);
       
    82 	
       
    83 	if ( iLocalSEID.SEID() != 1)	// we have only one local SEP, SBC
       
    84 		{
       
    85 		TRACE_INFO((_L("CBtsacConfiguring::EnterL() Wrong Local SEP being configured !!!")))
       
    86 		}
       
    87 	
       
    88 	// we have registered only one local SBC SEP, so aRemoteSEID should be a SBC Audio SEP, 
       
    89 	// atleast we assume that. So stash it as SBC Audio SEP
       
    90 	TAvdtpSEPInfo info = TAvdtpSEPInfo();
       
    91 	info.SetIsSink(ETrue); 
       
    92 	info.SetMediaType(EAvdtpMediaTypeAudio);
       
    93 	info.SetSEID(iRemoteSEID);
       
    94 	TRAPD(err, Parent().iRemoteSEPs->NewSEPL(info));
       
    95 	if (!err)
       
    96 		{
       
    97 		Parent().iRemoteSEPs->SetState(info.SEID(), CBTSACStreamEndPoint::EDiscoveredRemote, &info);
       
    98 		}
       
    99 	else // internal problem
       
   100 		{
       
   101   		CancelActionL(KErrCancel, EGavdpResetReasonGeneral);
       
   102 		}
       
   103     }
       
   104 
       
   105 // -----------------------------------------------------------------------------
       
   106 // CBtsacConfiguring::CancelActionL
       
   107 // -----------------------------------------------------------------------------
       
   108 //
       
   109 void CBtsacConfiguring::CancelActionL(TInt aError, TBTSACGavdpResetReason aGavdpReset)
       
   110     {
       
   111     TRACE_FUNC
       
   112 	Parent().CompletePendingRequests((KConnectReq | KOpenAudioReq), aError);
       
   113     Parent().ChangeStateL(CBtsacListening::NewL(Parent(), aGavdpReset, aError));
       
   114     }
       
   115 
       
   116 // -----------------------------------------------------------------------------
       
   117 // CBtsacConfiguring::CancelConnectL
       
   118 // -----------------------------------------------------------------------------
       
   119 //
       
   120 void CBtsacConfiguring::CancelConnectL()
       
   121     {
       
   122     TRACE_FUNC
       
   123 	CancelActionL(KErrCancel, EGavdpResetReasonGeneral); // do no cancel till gavdp_error call back recieved
       
   124     }
       
   125 
       
   126 // -----------------------------------------------------------------------------
       
   127 // CBtsacConfiguring::OpenAudioLinkL
       
   128 // -----------------------------------------------------------------------------
       
   129 //    
       
   130 void CBtsacConfiguring::OpenAudioLinkL(const TBTDevAddr& /*aAddr*/)
       
   131 	{  
       
   132 	TRACE_FUNC
       
   133 	// Handle open audio completion later, after configuration is ready (completion will happen
       
   134 	// in CBtsacConfigured state after we receive GAVDP_StartIndication).
       
   135 	}
       
   136 	
       
   137 // -----------------------------------------------------------------------------
       
   138 // CBtsacConfiguring::DisconnectL
       
   139 // -----------------------------------------------------------------------------
       
   140 //
       
   141 void CBtsacConfiguring::DisconnectL()	
       
   142 	{
       
   143 	TRACE_FUNC
       
   144 	Parent().CompletePendingRequests(KDisconnectReq, KErrNone);
       
   145 	CancelActionL(KErrCancel, EGavdpResetReasonGeneral);
       
   146 	}	
       
   147 
       
   148 // -----------------------------------------------------------------------------
       
   149 // CBtsacConfiguring::GAVDP_BearerReady
       
   150 // -----------------------------------------------------------------------------
       
   151 //
       
   152 void CBtsacConfiguring::GAVDP_BearerReady(RSocket aNewSocket, const TAvdtpSockAddr& aAddr)
       
   153 	{
       
   154 	TRACE_INFO((_L("CBtsacConfiguring::GAVDP_BearerReady() SEID: %d"), aAddr.SEID().SEID()))
       
   155 	(void) aAddr.SEID(); 
       
   156 	Parent().NewAccessory(Parent().GetRemoteAddr());
       
   157 	
       
   158 	// If there is pending connect request, complete it.
       
   159 	Parent().CompletePendingRequests(KConnectReq, KErrNone);
       
   160 	
       
   161 	// only one socket in this implementation at the moment, 
       
   162 	// as we dont have recovery and reporting yet
       
   163 	TRAP_IGNORE(Parent().ChangeStateL(CBtsacConfigured::NewL(Parent(), aNewSocket, iAudioOpenedBy, EStreamConfiguredBySink)));
       
   164 	}
       
   165 
       
   166 // -----------------------------------------------------------------------------
       
   167 // CBtsacConfiguring::GAVDP_ConfigurationIndication
       
   168 // -----------------------------------------------------------------------------
       
   169 //
       
   170 TInt CBtsacConfiguring::GAVDP_ConfigurationIndication(TAvdtpServiceCapability* aCapability)
       
   171 	{
       
   172 	TRACE_FUNC
       
   173 	Parent().iRemoteSEPs->StoreCaps(iRemoteSEPIndex, aCapability);
       
   174 	if ( aCapability->Category() == EServiceCategoryMediaCodec )
       
   175 		{
       
   176 		TAvdtpMediaCodecCapabilities& codecCaps = static_cast<TAvdtpMediaCodecCapabilities&>(*aCapability);
       
   177 		if ( codecCaps.MediaCodecType() == EAudioCodecSBC ) // found SEP that we are interested in
       
   178 			{
       
   179 			TRACE_INFO((_L("CBtsacConfiguring::GAVDP_SEPCapability() Found SBC Audio Sink SEP !!!")))
       
   180 			iSEPFound = ETrue;
       
   181 			Parent().SetSEPIndex(iRemoteSEPIndex);
       
   182 			}
       
   183 		}
       
   184 	return KErrNone;
       
   185 	}
       
   186 	
       
   187 // -----------------------------------------------------------------------------
       
   188 // CBtsacConfiguring::GAVDP_ConfigurationEndIndication
       
   189 // -----------------------------------------------------------------------------
       
   190 //	
       
   191 TInt CBtsacConfiguring::GAVDP_ConfigurationEndIndication()
       
   192 	{
       
   193 	TRACE_FUNC
       
   194 	if ( iSEPFound ) // proposed SEP has SBC (and media transport!) caps
       
   195 		{
       
   196 		RPointerArray<TAvdtpServiceCapability> SEPCapabilities;
       
   197 	    if ((Parent().iRemoteSEPs->GetCaps(Parent().GetSEPIndex(), SEPCapabilities)) )
       
   198 			{
       
   199 			TRACE_INFO((_L("CBtsacConfiguring::GAVDP_ConfigurationEndIndication() Couldn't retrieve Capabilities !")))
       
   200 		   	SEPCapabilities.Close();
       
   201 		   	TRAPD(err, CancelActionL(KErrCancel, EGavdpResetReasonGeneral));
       
   202 		   	if(err)
       
   203 		   		{
       
   204 		   		return KErrNoMemory;
       
   205 		   		}
       
   206 			return KErrGeneral; 
       
   207 			}
       
   208 		TRACE_INFO((_L("CBtsacConfiguring::GAVDP_ConfigurationEndIndication() Retrieve %d capabilities"), SEPCapabilities.Count()))
       
   209 	   	// loop through all capablities to find sbc codec capablities
       
   210 	   	TSBCCodecCapabilities SBCCaps; 
       
   211 	   	TAvdtpMediaTransportCapabilities MedTransCaps; 
       
   212 	   	for (TInt index=0; index < SEPCapabilities.Count(); index++)		
       
   213 	   		{
       
   214 			TAvdtpServiceCapability* cap = SEPCapabilities[index];
       
   215 			TAvdtpServiceCategory cat = cap->Category();
       
   216 			if ( cat == EServiceCategoryMediaCodec )
       
   217 				{
       
   218 			    SBCCaps = static_cast<TSBCCodecCapabilities&>(*cap);	
       
   219 			    TRACE_INFO((_L("CBtsacConfiguring::GAVDP_ConfigurationEndIndication() SBC Caps retrieved.")))
       
   220 				}
       
   221 			else if ( cat == EServiceCategoryMediaTransport )
       
   222 				{
       
   223 				MedTransCaps = static_cast<TAvdtpMediaTransportCapabilities&>(*cap);
       
   224 				TRACE_INFO((_L("CBtsacConfiguring::GAVDP_ConfigurationEndIndication() Media Transport Caps retrieved.")))
       
   225 				}
       
   226 	   		}
       
   227 	   	SEPCapabilities.Close();
       
   228 		
       
   229 		//Parent().StoreAccInfo();  // stores iRemoteSEPs (SEPManager) into database
       
   230 		
       
   231 		// Check if headset's capabilities suits us			
       
   232 		TRACE_INFO((_L("CBtsacConfiguring::GAVDP_ConfigurationEndIndication() Accessory Sampling Frequencies: %d"), SBCCaps.SamplingFrequencies()))
       
   233 		TRACE_INFO((_L("CBtsacConfiguring::GAVDP_ConfigurationEndIndication() Accessory Channel modes: %d"), SBCCaps.ChannelModes()))
       
   234 		TRACE_INFO((_L("CBtsacConfiguring::GAVDP_ConfigurationEndIndication() Accessory Blocks: %d"), SBCCaps.BlockLengths()))
       
   235 		TRACE_INFO((_L("CBtsacConfiguring::GAVDP_ConfigurationEndIndication() Accessory SubBands: %d"), SBCCaps.Subbands()))
       
   236 		TRACE_INFO((_L("CBtsacConfiguring::GAVDP_ConfigurationEndIndication() Accessory Alloc method: %d"), SBCCaps.AllocationMethods()))
       
   237 		TRACE_INFO((_L("CBtsacConfiguring::GAVDP_ConfigurationEndIndication() Accessory Max bitpool: %d"), SBCCaps.MaxBitpoolValue()))
       
   238 		TRACE_INFO((_L("CBtsacConfiguring::GAVDP_ConfigurationEndIndication() Accessory Min bitpool: %d"), SBCCaps.MinBitpoolValue()))
       
   239 		if (Parent().iStreamer->ConfigureSEP(SBCCaps) )
       
   240 			{
       
   241 	   		TRACE_INFO((_L("CBtsacConfiguring::GAVDP_ConfigurationEndIndication() Streamer couldn't configure SEP !")))
       
   242 	        TRAPD(err, CancelActionL(KErrCancel, EGavdpResetReasonGeneral));
       
   243 	        if(err)
       
   244 	        	{
       
   245 	        	return KErrNoMemory;
       
   246 	        	}
       
   247 	        return KErrGeneral;   // capabilites doesn't suit us
       
   248 			}
       
   249 		// Everyting has been done on behalf of us, let's wait GAVDP_BearerReady indication.
       
   250 		TRACE_INFO((_L("CBtsacConfiguring::GAVDP_ConfigurationEndIndication() completed")))
       
   251 		return KErrNone;
       
   252 		}
       
   253 	else 
       
   254 	 	{
       
   255 	 	return KErrNotFound; // remote is missing SBC (or media transport!) caps in SEP it proposed
       
   256 	 	}
       
   257 	}
       
   258 
       
   259 // -------------------------------------------------------------
       
   260 // CBtsacConfiguring::GAVDP_StartIndication
       
   261 // -------------------------------------------------------------
       
   262 //
       
   263 TInt CBtsacConfiguring::GAVDP_StartIndication(TSEID aLocalSEID)
       
   264 	{
       
   265 	TRACE_INFO((_L("CBtsacConfiguring::GAVDP_StartIndication(), LocalSEID:%d"), aLocalSEID.SEID()))
       
   266 	(void)aLocalSEID;
       
   267 	
       
   268 	// accessory has send this indication right after GAVDP_ConfigurationEndIndication
       
   269 	// but we must be waiting for Gavdp_BearerReady. In order to solve this, just flag 
       
   270 	// our indication that an open audio request exists
       
   271 	// CBtsacConfigured state can use this indication to start audio automatically then later
       
   272 	
       
   273 	// If we have already audio open request pending (from accessory FW), let keep it that way.
       
   274 	// We have to complete open audio request later
       
   275 	if(!Parent().IsOpenAudioReqFromAccFWPending())
       
   276 		{		
       
   277 		iAudioOpenedBy = EAudioOpenedByAcc;
       
   278 		}
       
   279 	return KErrNone;
       
   280 	}
       
   281     
       
   282 // -----------------------------------------------------------------------------
       
   283 // CBtsacConfiguring::HandleGavdpErrorL
       
   284 // -----------------------------------------------------------------------------
       
   285 //	
       
   286 void CBtsacConfiguring::HandleGavdpErrorL(TInt aError)
       
   287 	{
       
   288 	TRACE_FUNC
       
   289 	switch(aError)
       
   290 		{
       
   291 		case KErrAvdtpRequestTimeout: // -18005
       
   292 			{
       
   293 			TRACE_INFO((_L("CBtsacConfiguring::HandleGavdpErrorL() Request TIMEOUT")))
       
   294 			// Go to listening state, gavdp will be shutdown in listening state
       
   295 			CancelActionL(KErrDisconnected, EGavdpResetReasonNone);
       
   296 			break;
       
   297 			}
       
   298 			
       
   299 		case KErrHCILinkDisconnection: // -6305
       
   300 		case KErrDisconnected: // -36
       
   301 			{
       
   302 			TRACE_INFO((_L("CBtsacConfiguring::HandleGavdpErrorL() Signalling disconnected.")))
       
   303 			// for both outgoing or incoming connection, if we have an error, 
       
   304 			// this means there is disconnection
       
   305 			CancelActionL(aError, EGavdpResetReasonNone);
       
   306 			break;
       
   307 			}
       
   308 		
       
   309 		default:			
       
   310 		//case KErrNotReady: // -18
       
   311 		//case KErrInUse: // -14
       
   312 			{
       
   313 			CancelActionL(KErrDisconnected, EGavdpResetReasonGeneral);
       
   314 			break;
       
   315 			}
       
   316 		}
       
   317 	}
       
   318     
       
   319 //  End of File