bluetoothengine/btsac/src/btsacStateListening.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, BTSAC is listening for remote connection
       
    15 *								 from a stereo audio accessory
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 // INCLUDE FILES
       
    23 #include "btsacStateListening.h"
       
    24 #include "btsacStateConnected.h"
       
    25 #include "btsacStateConnecting.h"
       
    26 #include "btsacStateConfiguring.h"
       
    27 #include "btsacactive.h"
       
    28 #include "btsacGavdp.h"
       
    29 #include "debug.h"
       
    30 
       
    31 // ================= MEMBER FUNCTIONS =======================
       
    32 
       
    33 // -----------------------------------------------------------------------------
       
    34 // CBtsacListening::NewL
       
    35 // -----------------------------------------------------------------------------
       
    36 //
       
    37 CBtsacListening* CBtsacListening::NewL(CBTSAController& aParent,
       
    38 	TBTSACGavdpResetReason aGavdpResetReason, TInt aDisconnectReason)
       
    39     {
       
    40     CBtsacListening* self = new( ELeave ) CBtsacListening(aParent, aGavdpResetReason, aDisconnectReason);
       
    41     CleanupStack::PushL(self);
       
    42     self->ConstructL();
       
    43     CleanupStack::Pop(self);
       
    44     return self;
       
    45     }
       
    46 
       
    47 // -----------------------------------------------------------------------------
       
    48 // CBtsacListening::CBtsacListening
       
    49 // -----------------------------------------------------------------------------
       
    50 //
       
    51 CBtsacListening::CBtsacListening(CBTSAController& aParent,
       
    52 	TBTSACGavdpResetReason aGavdpResetReason, TInt aDisconnectReason)
       
    53 : 	CBtsacState(aParent, EStateListening), iGavdpResetReason(aGavdpResetReason),
       
    54 	iDisconnectReason(aDisconnectReason), iPendingRequests(KRequestNone),
       
    55 	iInitializationProcedure(EInitProcedureOngoing), iUnfinishedGavdpError(EFalse)
       
    56     {
       
    57     }
       
    58 
       
    59 // -----------------------------------------------------------------------------
       
    60 // CBtsacListening::ConstructL
       
    61 // -----------------------------------------------------------------------------
       
    62 //
       
    63 void CBtsacListening::ConstructL()
       
    64 	{
       
    65 	iSelfCompleteActive = CBtsacActive::NewL(*this, CActive::EPriorityStandard, KRequestIdSelfComplete);
       
    66 	}
       
    67 
       
    68 // -----------------------------------------------------------------------------
       
    69 // CBtsacListening::~CBtsacListening
       
    70 // -----------------------------------------------------------------------------
       
    71 //    
       
    72 CBtsacListening::~CBtsacListening()
       
    73     {
       
    74     TRACE_FUNC
       
    75     delete iSelfCompleteActive;
       
    76     iSelfCompleteActive = NULL;
       
    77     }
       
    78 
       
    79 // -----------------------------------------------------------------------------
       
    80 // CBtsacListening::EnterL
       
    81 // -----------------------------------------------------------------------------
       
    82 //
       
    83 void CBtsacListening::EnterL()
       
    84     {
       
    85 	TRACE_STATE(_L("[BTSAC State] Listening"))
       
    86 	_LIT(KName, "CBtsacStateListening");
       
    87 	const TDesC& Name = KName;	
       
    88 	Parent().iGavdp->RegisterObserver(this, Name);
       
    89 	
       
    90 	Parent().ResetRemoteCache();
       
    91 	Parent().CleanSockets();
       
    92 	
       
    93 	// RGavdp.Close (which is called from ResetGavdp method) should not be called from 
       
    94 	// within a client's implementation of any MGavdpUser (upcall) method. Do self
       
    95 	// complete first then call ResetGavdp.
       
    96 	
       
    97 	// Need for self completion is gavdp user up calls but we will do self complete every time
       
    98 	// when we enter into listening state.
       
    99 	DoSelfComplete(KErrNone);
       
   100     }
       
   101 
       
   102 // -----------------------------------------------------------------------------
       
   103 // CBtsacListening::ConnectL
       
   104 // -----------------------------------------------------------------------------
       
   105 //
       
   106 void CBtsacListening::ConnectL(const TBTDevAddr& aAddr)
       
   107     {
       
   108     TRACE_FUNC
       
   109     iPendingRequests = KConnectReq;
       
   110     if(iInitializationProcedure == EInitProcedureDone)
       
   111 		{
       
   112 		// we have a connection already with this accessory
       
   113 		if ( Parent().GetRemoteAddr() == aAddr )
       
   114 			{
       
   115 			Parent().ChangeStateL(CBtsacConnected::NewL(Parent(), EOutgoingConnNoDiscovery));
       
   116 			}
       
   117 		else
       
   118 			{
       
   119 		    Parent().SetRemoteAddr(aAddr);	// first update address cause InitializeSEPManager uses it
       
   120 		    Parent().InitializeSEPManager(); // initializes iRemoteSEPs
       
   121 		    Parent().ChangeStateL(CBtsacConnecting::NewL(Parent(), EOutgoingConn));
       
   122 			}
       
   123 		}
       
   124 	else
       
   125 		{
       
   126 		// Save the address, reset procedure is not completed yet.
       
   127 		Parent().SetRemoteAddr(aAddr);
       
   128 		}
       
   129     }
       
   130 
       
   131 // -----------------------------------------------------------------------------
       
   132 // CBtsacListening::OpenAudioLinkL
       
   133 // -----------------------------------------------------------------------------
       
   134 //    
       
   135 void CBtsacListening::OpenAudioLinkL(const TBTDevAddr& aAddr)
       
   136 	{  
       
   137 	TRACE_FUNC
       
   138 	iPendingRequests = KOpenAudioReq;
       
   139 	if(iInitializationProcedure == EInitProcedureDone)
       
   140 		{
       
   141 	    // we have a connection already with this accessory
       
   142 	    if ( Parent().GetRemoteAddr() == aAddr )
       
   143 	    	{
       
   144 			Parent().ChangeStateL(CBtsacConnected::NewL(Parent(), EOutgoingConnWithAudioNoDiscovery));
       
   145 	    	}
       
   146 	    else
       
   147 	    	{
       
   148 	        Parent().SetRemoteAddr(aAddr);	// first update address cause InitializeSEPManager uses it
       
   149 		    Parent().InitializeSEPManager(); // initializes iRemoteSEPs
       
   150 		    Parent().ChangeStateL(CBtsacConnecting::NewL(Parent(), EOutgoingConnWithAudio));
       
   151 	    	}
       
   152 		}
       
   153 	else
       
   154 		{
       
   155 		// Save the address, reset procedure is not completed yet.
       
   156 		Parent().SetRemoteAddr(aAddr);
       
   157 		}	
       
   158 	}
       
   159 
       
   160 // -----------------------------------------------------------------------------
       
   161 // CBtsacListening::CancelOpenAudioLinkL
       
   162 // -----------------------------------------------------------------------------
       
   163 //    
       
   164 void CBtsacListening::CancelOpenAudioLinkL()
       
   165     {
       
   166     TRACE_FUNC
       
   167     // We can have only one request active at the time, so we can reset pending requests
       
   168     iPendingRequests = KRequestNone;
       
   169 	GoListen();
       
   170 	Parent().CompletePendingRequests(KOpenAudioReq, KErrCancel);
       
   171     }	
       
   172 
       
   173 // -----------------------------------------------------------------------------
       
   174 // CBtsacListening::DeleyedOpenAudioAndConnectL
       
   175 // -----------------------------------------------------------------------------
       
   176 //	
       
   177 void CBtsacListening::DeleyedOpenAudioAndConnectL()
       
   178 	{
       
   179 	TRACE_FUNC
       
   180 	if(iPendingRequests & (KConnectReq | KOpenAudioReq))
       
   181 		{				
       
   182 		Parent().InitializeSEPManager();
       
   183 		if(iPendingRequests & KOpenAudioReq)
       
   184 			{			
       
   185 			Parent().ChangeStateL(CBtsacConnecting::NewL(Parent(), EOutgoingConnWithAudio));
       
   186 			}
       
   187 		else
       
   188 			{			
       
   189 			Parent().ChangeStateL(CBtsacConnecting::NewL(Parent(), EOutgoingConn));
       
   190 			}				
       
   191 		}
       
   192 	}
       
   193 
       
   194 // -----------------------------------------------------------------------------
       
   195 // CBtsacListening::GAVDP_ConfigurationConfirm
       
   196 // -----------------------------------------------------------------------------
       
   197 //
       
   198 void CBtsacListening::GAVDP_ConfigurationConfirm()
       
   199 	{
       
   200 	TRACE_INFO((_L("CBtsacListening::GAVDP_ConfigurationConfirm() Local SEPs registered successfully.")))
       
   201 	// Local SEPs got registered, so start listening
       
   202 	TInt err = Parent().iGavdp->Listen();
       
   203 	if (err)
       
   204 		{
       
   205 		TRACE_INFO((_L("CBtsacListening::GAVDP_ConfigurationConfirm() Listen returned error:%d."), err))
       
   206 		ResetGavdp(EGavdpResetReasonGeneral);
       
   207 		return;
       
   208 		}
       
   209 	if(iInitializationProcedure == EInitProcedureWaitingConfConfirmed)
       
   210 		{
       
   211 		iInitializationProcedure = EInitProcedureDone;
       
   212 		TRAP_IGNORE(DeleyedOpenAudioAndConnectL());
       
   213 		}
       
   214 	}
       
   215 
       
   216 // -----------------------------------------------------------------------------
       
   217 // CBtsacListening::GAVDP_ConnectConfirm
       
   218 // -----------------------------------------------------------------------------
       
   219 //	
       
   220 void CBtsacListening::GAVDP_ConnectConfirm(const TBTDevAddr& aAddr)
       
   221 	{
       
   222 	TRACE_INFO((_L("CBtsacListening::GAVDP_ConnectConfirm() Accessory made connection.")))
       
   223 	Parent().SetRemoteAddr(aAddr);  // first update address cause InitializeSEPManager uses it
       
   224 	Parent().InitializeSEPManager(); // initializes iRemoteSEPs
       
   225 	TRAPD(err, Parent().ChangeStateL(CBtsacConnected::NewL(Parent(), EIncomingConn)));
       
   226 	if(err)
       
   227 		{
       
   228 		TRACE_INFO((_L("CBtsacListening::GAVDP_ConnectConfirm(), NewL failed.")))
       
   229 		}
       
   230 	}
       
   231 
       
   232 // -----------------------------------------------------------------------------
       
   233 // CBtsacListening::GAVDP_AbortStreamConfirm
       
   234 // -----------------------------------------------------------------------------
       
   235 //	
       
   236 void CBtsacListening::GAVDP_AbortStreamConfirm()
       
   237 	{
       
   238 	TRACE_FUNC
       
   239 	GoListen();
       
   240 	}
       
   241 	
       
   242 // -----------------------------------------------------------------------------
       
   243 // CBtsacListening::GAVDP_ConfigurationStartIndication
       
   244 // -----------------------------------------------------------------------------
       
   245 //	
       
   246 void CBtsacListening::GAVDP_ConfigurationStartIndication(TSEID aLocalSEID, TSEID aRemoteSEID)
       
   247 	{
       
   248     TRACE_FUNC
       
   249     Parent().InitializeSEPManager();
       
   250 	TRAP_IGNORE(Parent().ChangeStateL(CBtsacConfiguring::NewL(Parent(), aLocalSEID, aRemoteSEID)));
       
   251 	}
       
   252 
       
   253 // -----------------------------------------------------------------------------
       
   254 // CBtsacListening::RequestCompletedL
       
   255 // -----------------------------------------------------------------------------
       
   256 //
       
   257 void CBtsacListening::RequestCompletedL(CBtsacActive& aActive)
       
   258     {
       
   259 	TRACE_FUNC
       
   260 	switch(aActive.RequestId())
       
   261 		{
       
   262 		case KRequestIdSelfComplete:
       
   263 			{
       
   264 			TBTDevAddr remoteAddr = Parent().GetRemoteAddr();
       
   265 			if(iUnfinishedGavdpError)
       
   266 				{
       
   267 				// We have unfinished gavdp error, force gavdp reset
       
   268 				iGavdpResetReason = EGavdpResetReasonGeneral;
       
   269 				}
       
   270 			if(iGavdpResetReason != EGavdpResetReasonNone)
       
   271 				{
       
   272 				ResetGavdp(iGavdpResetReason);
       
   273 				}
       
   274 			else
       
   275 				{
       
   276 				GoListen();
       
   277 				}				
       
   278 			if(iDisconnectReason == KErrDisconnected || iDisconnectReason == KErrHCILinkDisconnection)
       
   279 				{
       
   280 				if(remoteAddr != TBTDevAddr())
       
   281 					{						
       
   282 					Parent().DisconnectedFromRemote(remoteAddr, iDisconnectReason);
       
   283 					}
       
   284 				Parent().SetResetAudioInput(ETrue);
       
   285 				}
       
   286 			if(Parent().GetResetAudioInput())
       
   287 				{
       
   288 				Parent().SetResetAudioInput(EFalse);
       
   289 				Parent().iStreamer->ResetAudioInput();
       
   290 				}
       
   291 			if(iInitializationProcedure != EInitProcedureWaitingConfConfirmed)
       
   292 				{
       
   293 				iInitializationProcedure = EInitProcedureDone;
       
   294 				DeleyedOpenAudioAndConnectL();
       
   295 				}
       
   296 			break;
       
   297 			}
       
   298 		default:
       
   299 			{
       
   300 			TRACE_INFO((_L("CBtsacListening::RequestCompletedL() Unknown request")))
       
   301 			break;
       
   302 			}	
       
   303 		}
       
   304     }
       
   305 
       
   306 // -----------------------------------------------------------------------------
       
   307 // CBtsacListening::GoListen
       
   308 // -----------------------------------------------------------------------------
       
   309 //	
       
   310 void CBtsacListening::GoListen()
       
   311 	{
       
   312 	TRACE_INFO((_L("CBtsacListening::GoListen(), Disconnect signalling channel")))
       
   313 	if(iPendingRequests == KRequestNone)
       
   314 		{					
       
   315 		Parent().SetRemoteAddr(TBTDevAddr());
       
   316 		}
       
   317 	TInt err = Parent().iGavdp->Shutdown();
       
   318 	if(!err)
       
   319 		{
       
   320 		TRACE_INFO((_L("CBtsacListening::GoListen(), Signalling disconnected, Re-listen...")))
       
   321 		// Starts to listen for inbound signalling channel connections.
       
   322 		err = Parent().iGavdp->Listen();
       
   323 		}
       
   324 	if(err)
       
   325 		{
       
   326 		// Shutdown failed, reset gavdp
       
   327 		TRACE_INFO((_L("CBtsacListening::GoListen(), error = %d."), err))
       
   328 		ResetGavdp(EGavdpResetReasonGeneral);
       
   329 		}		
       
   330 	}
       
   331 
       
   332 // -----------------------------------------------------------------------------
       
   333 // CBtsacListening::ResetGavdp
       
   334 // -----------------------------------------------------------------------------
       
   335 //
       
   336 TInt CBtsacListening::ResetGavdp(TBTSACGavdpResetReason aReason)
       
   337 	{
       
   338 	TRACE_FUNC
       
   339 	Parent().iGavdp->Close();
       
   340 	if( Parent().iGavdp->Open() == KErrNone )
       
   341 		{
       
   342 		iInitializationProcedure = EInitProcedureWaitingConfConfirmed;
       
   343 		if(aReason != EGavdpResetReasonCancelOpenAudio)
       
   344 			{
       
   345 			if(iPendingRequests == KRequestNone)
       
   346 				{
       
   347 				TRACE_INFO((_L("CBtsacListening::ResetGavdp() Remote Addr reseted.")))
       
   348 				Parent().SetRemoteAddr(TBTDevAddr());
       
   349 				}
       
   350 			}
       
   351 		return Parent().iGavdp->RegisterSEPs(*Parent().iLocalSEPs, Parent().iStreamer->GetCaps());
       
   352 		}
       
   353 	else
       
   354 		{
       
   355 		TRACE_INFO((_L("CBtsacListening::ResetGavdp() Couldn't open gavdp.")))
       
   356 		return KErrGeneral;
       
   357 		}		
       
   358 	}
       
   359 
       
   360 // -----------------------------------------------------------------------------
       
   361 // CBtsacListening::CancelRequest
       
   362 // -----------------------------------------------------------------------------
       
   363 //	
       
   364 void CBtsacListening::CancelRequest(CBtsacActive& /*aActive*/)
       
   365 	{
       
   366 	TRACE_FUNC
       
   367 	}
       
   368 
       
   369 // -----------------------------------------------------------------------------
       
   370 // CBtsacListening::DoSelfComplete
       
   371 // -----------------------------------------------------------------------------
       
   372 //
       
   373 void CBtsacListening::DoSelfComplete(TInt aError) 
       
   374 	{
       
   375 	TRACE_FUNC
       
   376 	if(iSelfCompleteActive)
       
   377 		{		
       
   378 		if (!iSelfCompleteActive->IsActive())
       
   379 		    {		    
       
   380 		    TRequestStatus* sta = &(iSelfCompleteActive->iStatus);
       
   381 		    iSelfCompleteActive->iStatus = KRequestPending;
       
   382 		    User::RequestComplete(sta, aError);
       
   383 		    iSelfCompleteActive->GoActive();
       
   384 		    }
       
   385 		}
       
   386 	else
       
   387 		{
       
   388 		TRACE_INFO((_L("CBtsacListening::DoSelfComplete() AO doesn't exist.")))
       
   389 		}
       
   390 	}
       
   391     
       
   392 // -----------------------------------------------------------------------------
       
   393 // CBtsacListening::HandleGavdpErrorL
       
   394 // -----------------------------------------------------------------------------
       
   395 //	
       
   396 void CBtsacListening::HandleGavdpErrorL(TInt aError)
       
   397 	{
       
   398 	TRACE_FUNC
       
   399     if(iInitializationProcedure != EInitProcedureDone)
       
   400 	    {
       
   401 	    TRACE_INFO((_L("CBtsacListening::HandleGavdpErrorL() error ignored, initialization procedure ongoing.")))
       
   402 	    iUnfinishedGavdpError = ETrue;	    
       
   403 	    return;
       
   404 	    }
       
   405 	switch(aError)
       
   406 		{
       
   407 		case KErrHCILinkDisconnection: // -6305
       
   408 		case KErrDisconnected: // -36
       
   409 			{
       
   410 			// If address is zero, initialization procedure is already executed, so there is no need
       
   411 			// to tell gavdp to go listen and no need to inform btaudioman about the disconnection
       
   412 			if(Parent().GetRemoteAddr() != TBTDevAddr())
       
   413 				{
       
   414 				GoListen();
       
   415 				// for both outgoing or incoming connection, if we have an error, 
       
   416 				// this means there is disconnection		 	
       
   417 			 	Parent().DisconnectedFromRemote(Parent().GetRemoteAddr(), aError);	  	  	    	
       
   418 				Parent().SetRemoteAddr(TBTDevAddr());
       
   419 				Parent().iStreamer->ResetAudioInput();
       
   420 			 	}
       
   421 			break;
       
   422 			}
       
   423 		
       
   424 		case KErrCouldNotConnect: // -34
       
   425 			{
       
   426 			GoListen();
       
   427 			break;
       
   428 			}
       
   429 		
       
   430 		default:
       
   431 		// KErrAbort -39
       
   432 		// KErrDied -13
       
   433 		// KErrInUse -14
       
   434 			{
       
   435 			ResetGavdp(EGavdpResetReasonGeneral);			
       
   436 			break;
       
   437 			}
       
   438 		}
       
   439 	Parent().CompletePendingRequests(KCompleteAllReqs, aError);
       
   440 	}
       
   441 
       
   442 //  End of File