bluetoothengine/btsac/src/btsacStateConfigured.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, avdtp link is ready to be used and btsac opens
       
    15 *				 audio automatically if there is request pending otherwise
       
    16 *				 it will wait open audio request.
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 // INCLUDE FILES
       
    24 #include "btsacStateConfigured.h"
       
    25 #include "btsacStateListening.h"
       
    26 #include "btsacStateStreaming.h"
       
    27 #include "btsacStateAborting.h"
       
    28 #include "btsacactive.h"
       
    29 #include "btsacGavdp.h"
       
    30 #include "btsacSEPManager.h"
       
    31 #include "btsacStreamerController.h"
       
    32 #include "debug.h"
       
    33 
       
    34 // A2DP codec-specific element definitions
       
    35 // in bluetoothAV.h
       
    36 using namespace SymbianBluetoothAV;   
       
    37 
       
    38 // Subband codec-specific values
       
    39 // in bluetoothAV.h
       
    40 using namespace SymbianSBC;   
       
    41 
       
    42 const TInt KStartIndicationDelay = 1000000; // 1 sec
       
    43 
       
    44 // ================= MEMBER FUNCTIONS =======================
       
    45 
       
    46 // -----------------------------------------------------------------------------
       
    47 // CBtsacConfigured::NewL
       
    48 // -----------------------------------------------------------------------------
       
    49 //
       
    50 CBtsacConfigured* CBtsacConfigured::NewL(CBTSAController& aParent, RSocket aNewSocket,
       
    51 	TAudioOpenedBy aAudioOpenedBy, TStreamConfiguredBy aStreamConfiguredBy)
       
    52     {
       
    53    	CBtsacConfigured* self = new( ELeave ) CBtsacConfigured(aParent, aNewSocket, aAudioOpenedBy, aStreamConfiguredBy);
       
    54     CleanupStack::PushL(self);
       
    55     self->ConstructL();
       
    56     CleanupStack::Pop(self);
       
    57     return self;
       
    58    	}
       
    59 
       
    60 // -----------------------------------------------------------------------------
       
    61 // CBtsacConfigured::CBtsacConfigured
       
    62 // -----------------------------------------------------------------------------
       
    63 //
       
    64 CBtsacConfigured::CBtsacConfigured(CBTSAController& aParent, RSocket aNewSocket,
       
    65 	TAudioOpenedBy aAudioOpenedBy, TStreamConfiguredBy aStreamConfiguredBy)
       
    66 :   CBtsacState(aParent, EStateConfigured), iAudioOpenedBy(aAudioOpenedBy),
       
    67 	iStreamConfiguredBy(aStreamConfiguredBy), iStartStreamStatus(EStartNone)
       
    68     {
       
    69     TRACE_ASSERT(Parent().iStreamingSockets.Count() == 0, EBTPanicSocketExists)
       
    70     TRACE_INFO((_L("[SOCKET] created.")))
       
    71     Parent().iStreamingSockets.Append(aNewSocket);
       
    72     }
       
    73 
       
    74 // -----------------------------------------------------------------------------
       
    75 // CBtsacConfigured::ConstructL
       
    76 // -----------------------------------------------------------------------------
       
    77 //
       
    78 void CBtsacConfigured::ConstructL()
       
    79 	{
       
    80 	iTimerActive = CBtsacActive::NewL(*this, CActive::EPriorityStandard, KRequestIdTimer);
       
    81 	iTimer.CreateLocal();
       
    82 	}
       
    83   
       
    84 // -----------------------------------------------------------------------------
       
    85 // CBtsacConfigured::~CBtsacConfigured
       
    86 // -----------------------------------------------------------------------------
       
    87 //    
       
    88 CBtsacConfigured::~CBtsacConfigured()
       
    89     {
       
    90     TRACE_FUNC
       
    91     delete iTimerActive;
       
    92     iTimerActive = NULL;
       
    93     iTimer.Close();
       
    94     }
       
    95 
       
    96 // -----------------------------------------------------------------------------
       
    97 // CBtsacConfigured::EnterL
       
    98 // -----------------------------------------------------------------------------
       
    99 //
       
   100 void CBtsacConfigured::EnterL()
       
   101     {
       
   102 	TRACE_STATE(_L("[BTSAC State] Configured"))
       
   103 	_LIT(KName, "CBtsacStateConfigured");
       
   104 	const TDesC& Name = KName;
       
   105 	Parent().iGavdp->RegisterObserver(this, Name);
       
   106 
       
   107   	// ** OPEN AUDIO automatically **
       
   108   	if(iStreamConfiguredBy == EStreamConfiguredBySrc)
       
   109 	  	{
       
   110 	  	if(Parent().IsOpenAudioReqFromAccFWPending())
       
   111 		  	{
       
   112 		  	TInt err;
       
   113 	  		TRAP(err, OpenAudioLinkL(Parent().GetRemoteAddr()));
       
   114 	  		if( err )
       
   115 	  			{
       
   116 	  			Parent().CompletePendingRequests(KOpenAudioReq, err);
       
   117 	  			}
       
   118 		  	}
       
   119 	  	}
       
   120 	 else // Stream configured by sink
       
   121 	 	{
       
   122 	 	if(iAudioOpenedBy == EAudioOpenedByAcc)
       
   123 		 	{
       
   124 		 	GAVDP_StartIndication(TSEID(1, ETrue));
       
   125 		 	}
       
   126  		else if(Parent().IsOpenAudioReqFromAccFWPending())
       
   127 	 		{
       
   128 	 		// Start timer to protect such a situation that we will not receive StartIndication from remote end.
       
   129   			StartTimer(KStartIndicationDelay);
       
   130 	 		}
       
   131 	 	}	  
       
   132     }
       
   133 
       
   134 // -----------------------------------------------------------------------------
       
   135 // CBtsacConfigured::StartTimer
       
   136 // -----------------------------------------------------------------------------
       
   137 //
       
   138 void CBtsacConfigured::StartTimer(TTimeIntervalMicroSeconds32 aTimeout)
       
   139 	{
       
   140 	TRACE_FUNC
       
   141 	if (iTimerActive)
       
   142     	{
       
   143 	    if(!iTimerActive->IsActive())
       
   144 		    {	    	
       
   145 	        iTimer.After(iTimerActive->iStatus, aTimeout);
       
   146 	        iTimerActive->GoActive();
       
   147 		    }
       
   148         }
       
   149     else
       
   150     	{
       
   151     	TRACE_INFO((_L("CBtsacConfigured::StartTimer, Timer Active doesn't exist.")))
       
   152     	}
       
   153 	}
       
   154 
       
   155 // -----------------------------------------------------------------------------
       
   156 // CBtsacConfigured::StopTimer
       
   157 // -----------------------------------------------------------------------------
       
   158 //
       
   159 void CBtsacConfigured::StopTimer()
       
   160 	{
       
   161 	TRACE_FUNC
       
   162 	if (iTimerActive)
       
   163     	{
       
   164 		iTimerActive->Cancel();
       
   165         }
       
   166 	}
       
   167 
       
   168 // -----------------------------------------------------------------------------
       
   169 // CBtsacConfigured::CancelActionL
       
   170 // -----------------------------------------------------------------------------
       
   171 //
       
   172 void CBtsacConfigured::CancelActionL(TInt aError, TBTSACGavdpResetReason aGavdpReset)
       
   173     {
       
   174     TRACE_FUNC
       
   175 	Parent().CompletePendingRequests(KOpenAudioReq, aError);
       
   176 	Parent().ChangeStateL(CBtsacListening::NewL(Parent(), aGavdpReset, aError));
       
   177     }
       
   178 
       
   179 // -----------------------------------------------------------------------------
       
   180 // CBtsacConfigured::CancelConnectL
       
   181 // -----------------------------------------------------------------------------
       
   182 //
       
   183 void CBtsacConfigured::CancelConnectL()
       
   184     {
       
   185 	TRACE_FUNC	
       
   186 	CancelActionL(KErrCancel, EGavdpResetReasonGeneral);
       
   187     }
       
   188 
       
   189 // -----------------------------------------------------------------------------
       
   190 // CBtsacConfigured::OpenAudioLinkL
       
   191 // -----------------------------------------------------------------------------
       
   192 //    
       
   193 void CBtsacConfigured::OpenAudioLinkL(const TBTDevAddr& aAddr)
       
   194 	{  
       
   195 	TRACE_FUNC
       
   196 	if((iStreamConfiguredBy == EStreamConfiguredBySrc) || (iAudioOpenedBy == EAudioOpenedByAFW))
       
   197 		{		
       
   198 		TAvdtpSEPInfo SEPInfo;
       
   199 		iStartStreamStatus = EStartSendBySrc;
       
   200 		if (Parent().iRemoteSEPs->GetInfo(Parent().GetSEPIndex(), SEPInfo) || (aAddr != Parent().GetRemoteAddr() ))
       
   201 			{
       
   202 			TRACE_INFO((_L("TStateConfigured::OpenAudio Couldn't retrieve SEP Info !")))
       
   203 	       	Parent().CompletePendingRequests(KOpenAudioReq, KErrGeneral);
       
   204 	       	return;
       
   205 			}
       
   206 		Parent().SetRemoteAddr(aAddr); 	
       
   207 		TSEID remoteSEPid = SEPInfo.SEID(); 	
       
   208 		Parent().iGavdp->StartStreams(remoteSEPid);
       
   209 		}
       
   210 	else
       
   211 		{
       
   212 		// Remote end (sink) has configured us. We can't call StartStreams. Complete audio req later,
       
   213 		// after we have received start indication from the remote (sink).
       
   214 
       
   215 		// Start timer to protect such a situation that we will not receive StartIndication from remote end.
       
   216 		StartTimer(KStartIndicationDelay);
       
   217 		}
       
   218 	}
       
   219 
       
   220 // -----------------------------------------------------------------------------
       
   221 // CBtsacConfigured::CancelOpenAudioLinkL()
       
   222 // -----------------------------------------------------------------------------
       
   223 //    
       
   224 void CBtsacConfigured::CancelOpenAudioLinkL()
       
   225     {
       
   226     TRACE_FUNC
       
   227 	CancelActionL(KErrCancel, EGavdpResetReasonGeneral);
       
   228     }
       
   229 
       
   230 // -----------------------------------------------------------------------------
       
   231 // CBtsacConfigured::DisconnectL
       
   232 // -----------------------------------------------------------------------------
       
   233 //
       
   234 void CBtsacConfigured::DisconnectL()
       
   235 	{
       
   236 	TRACE_FUNC
       
   237 	Parent().CompletePendingRequests(KDisconnectReq, KErrNone);
       
   238 	// Cancel all other requests
       
   239 	Parent().CompletePendingRequests(KCompleteAllReqs, KErrCancel);
       
   240 	Parent().ChangeStateL(CBtsacListening::NewL(Parent(), EGavdpResetReasonGeneral, KErrNone));
       
   241 	}
       
   242 
       
   243 // -----------------------------------------------------------------------------
       
   244 // CBtsacConfigured::GAVDP_StartStreamsConfirm()
       
   245 // -----------------------------------------------------------------------------
       
   246 //    
       
   247 void CBtsacConfigured::GAVDP_StartStreamsConfirm()
       
   248 	{
       
   249 	TRACE_FUNC
       
   250 	if ( Parent().iStreamer->StartStream(Parent().iStreamingSockets[0], Parent().iStreamer->FrameLength()) != KErrNone )
       
   251 	 	{
       
   252 		TRACE_INFO((_L("CBtsacConfigured::GAVDP_StartStreamsConfirm() [ERR] Could not start streaming!")))
       
   253 		TInt err = Parent().AbortStream();
       
   254 		if(err)
       
   255 			{
       
   256 			TRACE_INFO((_L("CBtsacConfigured::GAVDP_StartStreamsConfirm() [ERR] Couldn't abort stream.")))
       
   257 			}
       
   258 		TRAP_IGNORE(CancelActionL(KErrNotReady, EGavdpResetReasonGeneral));
       
   259 	 	}
       
   260 	 else
       
   261 	 	{
       
   262 	 	TBool Collision = iStartStreamStatus == EStartCollision ? ETrue : EFalse;
       
   263 		Parent().CompletePendingRequests(KOpenAudioReq, KErrNone);
       
   264 		TRAP_IGNORE(Parent().ChangeStateL(CBtsacStreaming::NewL(Parent(), EAudioOpenedByAFW, Collision)));
       
   265 	 	}
       
   266 	}
       
   267 
       
   268 // -----------------------------------------------------------------------------
       
   269 // CBtsacConfigured::GAVDP_StartIndication
       
   270 // -----------------------------------------------------------------------------
       
   271 //
       
   272 TInt CBtsacConfigured::GAVDP_StartIndication(TSEID aLocalSEID)
       
   273 	{
       
   274 	TRACE_INFO((_L("CBtsacConfigured::GAVDP_StartIndication() aLocalSEID:%d"), aLocalSEID.SEID()))
       
   275 	(void) aLocalSEID;
       
   276 	StopTimer();
       
   277 	// Check if acc fw has already requested to open audio
       
   278 	if(Parent().IsOpenAudioReqFromAccFWPending())
       
   279 		{
       
   280 		if(iStartStreamStatus == EStartSendBySrc)
       
   281 			{
       
   282 			// Src has also sent start stream cmd. 
       
   283 			iStartStreamStatus = EStartCollision;
       
   284 			}
       
   285 			
       
   286 		GAVDP_StartStreamsConfirm();
       
   287 		}
       
   288 	else
       
   289 		{
       
   290     	// Accessory has send this indication.
       
   291 		// CBtsacStreaming state can use this indication to start audio automatically then later.
       
   292  		TRAPD(err, Parent().ChangeStateL(CBtsacStreaming::NewL(Parent(), EAudioOpenedByAcc, EFalse)));
       
   293 		if (err)
       
   294 			{
       
   295 			TRACE_INFO((_L("CBtsacConfigured::GAVDP_StartIndication() Couldn't change state.")))
       
   296 			return KErrNoMemory;
       
   297 			}
       
   298 		}	
       
   299 	return KErrNone;
       
   300 	}
       
   301 
       
   302 // -----------------------------------------------------------------------------
       
   303 // CBtsacConfigured::RequestCompletedL
       
   304 // -----------------------------------------------------------------------------
       
   305 //
       
   306 void CBtsacConfigured::RequestCompletedL(CBtsacActive& aActive)
       
   307     {
       
   308 	TRACE_FUNC
       
   309 	switch(aActive.RequestId())
       
   310 		{
       
   311 		case KRequestIdTimer:
       
   312 			{
       
   313 	  		if(Parent().IsOpenAudioReqFromAccFWPending())
       
   314 		  		{
       
   315 		  		// Well, seems that accessory didn't send us GAVDP_StartIndication. Let's try to open audio.
       
   316 				// Over write iAudioOpenedBy previous value and call open audio
       
   317 		  		iAudioOpenedBy = EAudioOpenedByAFW;
       
   318 				OpenAudioLinkL(Parent().GetRemoteAddr());
       
   319 		  		}
       
   320 			break;
       
   321 			}
       
   322 		default:
       
   323 			{
       
   324 			TRACE_INFO((_L("CBtsacConfigured::RequestCompletedL() Unknown request")))
       
   325 			break;
       
   326 			}				
       
   327 		}
       
   328     }
       
   329 
       
   330 // -----------------------------------------------------------------------------
       
   331 // CBtsacConfigured::CancelRequest
       
   332 // -----------------------------------------------------------------------------
       
   333 //
       
   334 void CBtsacConfigured::CancelRequest(CBtsacActive& aActive)
       
   335 	{
       
   336 	TRACE_FUNC
       
   337 	if(aActive.RequestId() == KRequestIdTimer )
       
   338 		{		
       
   339 		iTimer.Cancel();
       
   340 		}
       
   341 	else
       
   342 		{
       
   343 		TRACE_INFO((_L("CBtsacConfigured::CancelRequest() Unknown request")))
       
   344 		}
       
   345 	}    
       
   346 
       
   347 // -----------------------------------------------------------------------------
       
   348 // CBtsacConfigured::HandleGavdpErrorL
       
   349 // -----------------------------------------------------------------------------
       
   350 //	
       
   351 void CBtsacConfigured::HandleGavdpErrorL(TInt aError)
       
   352 	{
       
   353 	TRACE_FUNC
       
   354 	StopTimer();
       
   355 	switch (aError)
       
   356 		{
       
   357 		case KErrAvdtpRequestTimeout: // -18005
       
   358 		case (KErrAvdtpSignallingErrorBase - EAvdtpBadState): // 18094
       
   359 			{
       
   360 			TRACE_INFO((_L("CBtsacConfigured::HandleGavdpErrorL() Request TIMEOUT/Bad state")))
       
   361 			TInt err = Parent().AbortStream();
       
   362 			if(!err)
       
   363 				{
       
   364 				// Complete Connect/Audio requests in Aborting state
       
   365 				Parent().ChangeStateL(CBtsacAborting::NewL(Parent()));
       
   366 				}
       
   367 			else
       
   368 				{
       
   369 				CancelActionL(KErrDisconnected, EGavdpResetReasonGeneral);
       
   370 				}
       
   371 			break;
       
   372 			}
       
   373 		case KErrHCILinkDisconnection: // -6305
       
   374 		case KErrDisconnected: // -36
       
   375 			{
       
   376 			TRACE_INFO((_L("CBtsacConfigured::HandleGavdpErrorL() Signalling disconnected.")))
       
   377 			CancelActionL(aError, EGavdpResetReasonNone);
       
   378 			break;
       
   379 			}
       
   380 		default:
       
   381 			{
       
   382 			CancelActionL(KErrDisconnected, EGavdpResetReasonGeneral);
       
   383 			break;
       
   384 			}
       
   385 		}
       
   386 	}
       
   387     
       
   388 //  End of File