bluetooth/btstack/avctp/avctpmuxerstates.cpp
changeset 0 29b1cd4cb562
child 11 20fda83a6398
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     1 // Copyright (c) 2005-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 // Implement the AVCTP Muxer state classes.
       
    15 // These are CAvctpMuxerStateFactory, TAvctpMuxerState (abstract) and its derived
       
    16 // classes 
       
    17 // Together, these classes and the Muxer implement the State pattern
       
    18 // (GOF).  The states themselves are implemented using the Flyweight
       
    19 // pattern.  Each state is a Flyweight object, and CAvctpMuxerStateFactory
       
    20 // is manager of these objects.  As a result of being a flyweight, no
       
    21 // state object may have state that can't be shared between all
       
    22 // possible users of the state (i.e. no per-Muxer state)
       
    23 // 
       
    24 //
       
    25 
       
    26 /**
       
    27  @file
       
    28  @internalComponent
       
    29 */
       
    30 #include <bluetooth/logger.h>
       
    31 
       
    32 #include <es_prot.h>
       
    33 #include <es_mbuf.h>
       
    34 
       
    35 #include "avctpmuxerstates.h" 
       
    36 #include "bt_subconn_levels.h"
       
    37 #include "Avctp.h"
       
    38 #include "avctpmuxer.h"
       
    39 #include "avctputils.h"
       
    40 #include "avctpconstants.h"
       
    41 #include "avctpPacketMgr.h"
       
    42 
       
    43 #ifdef __FLOG_ACTIVE
       
    44 _LIT8(KLogComponent, LOG_COMPONENT_AVCTP);
       
    45 #endif
       
    46 
       
    47 using namespace SymbianAvctp;
       
    48 
       
    49 //
       
    50 //                                                                                       //
       
    51 // TAvctpMuxerState Implementation                                                       //
       
    52 //                                                                                       //
       
    53 //
       
    54 
       
    55 /** 
       
    56 Default constructor.
       
    57 
       
    58 @internalComponent
       
    59 @param aFactory The Muxer state factory
       
    60 */
       
    61 TAvctpMuxerState::TAvctpMuxerState(CAvctpMuxerStateFactory& aFactory)
       
    62 	: iFactory(aFactory)
       
    63 	{
       
    64 	LOG_FUNC
       
    65 	}
       
    66 
       
    67 /** 
       
    68 Change a Muxer from one state to another.
       
    69 
       
    70 We Exit() the first state and then Enter() the second
       
    71 
       
    72 @internalComponent
       
    73 @param aTransport The Muxer whose state is to be changed
       
    74 @param aNewState The index for the new state the Muxer is to transition to
       
    75 */
       
    76 void TAvctpMuxerState::ChangeState(CAvctpTransport& aTransport, CAvctpMuxerStateFactory::TAvctpMuxerStates aNewState) const
       
    77 	{
       
    78 	LOG_FUNC
       
    79 
       
    80 	aTransport.iState->Exit(aTransport);
       
    81 
       
    82 #ifdef __FLOG_ACTIVE
       
    83 	TAvctpMuxerState& state = iFactory.GetState(aNewState);
       
    84 #endif
       
    85 	LOG3(_L("Muxer 0x%08x : State %S -> %S"), &aTransport, &(aTransport.iState->iName), &(state.iName));
       
    86 	
       
    87 	aTransport.iState = &iFactory.GetState(aNewState);
       
    88 	aTransport.iState->Enter(aTransport);
       
    89 	}
       
    90 
       
    91 /**
       
    92 Calls the appropriate panic function to encode the panic
       
    93 code with the current state identifier.
       
    94 @param aPanic The panic code that the state is panicking with.
       
    95 */
       
    96 void TAvctpMuxerState::PanicInState(SymbianAvctp::TPanic aPanic) const
       
    97 	{
       
    98 	Panic(aPanic, iFactory.StateIndex(this));
       
    99 	}
       
   100 	
       
   101 /**
       
   102  Close the secondary l2cap channel with or without notifying clients.
       
   103  @param aTransport the transport the state is related to
       
   104  @param aNotifyClient if it's ETrue clients are notified.
       
   105  */
       
   106 void TAvctpMuxerState::ShutDownSecondarySap(CAvctpTransport& aTransport, TBool aNotifyClient) const
       
   107 	{
       
   108 	LOG_FUNC
       
   109 	
       
   110 	CServProviderBase* sap = aTransport.iChannelSAPs[KAvctpSecondaryChannel];
       
   111 	if (sap)
       
   112 		{
       
   113 		sap->Shutdown(CServProviderBase::EImmediate);
       
   114 		delete sap;
       
   115 		sap = NULL;
       
   116 		aTransport.iChannelSAPs[KAvctpSecondaryChannel] = NULL;
       
   117 		if (aNotifyClient)
       
   118 			{
       
   119 			aTransport.NotifyLinkDown(aTransport.iRemoteAddr, KAvctpSecondaryChannel);
       
   120 			}
       
   121 		ChangeState(aTransport, CAvctpMuxerStateFactory::EOpen);
       
   122 		}
       
   123 	}
       
   124 
       
   125 /**
       
   126 Default state entry function.
       
   127 
       
   128 @internalComponent
       
   129 @param aTransport The Muxer whose state is changing
       
   130 */
       
   131 void TAvctpMuxerState::Enter(CAvctpTransport& /*aTransport*/) const
       
   132 	{
       
   133 	LOG_FUNC
       
   134 	}
       
   135 
       
   136 /**
       
   137 Default state exit function.
       
   138 
       
   139 @internalComponent
       
   140 @param aTransport The Muxer whose state is changing
       
   141 */
       
   142 void TAvctpMuxerState::Exit(CAvctpTransport& /*aTransport*/) const
       
   143 	{
       
   144 	LOG_FUNC	
       
   145 	}
       
   146 
       
   147 
       
   148 /**
       
   149 Default Muxer IsIdle
       
   150 
       
   151 The muxer is asking its state to determine whether it's idle.
       
   152 
       
   153 @internalComponent
       
   154 @param aTransport The Muxer which is idle
       
   155 */
       
   156 TBool TAvctpMuxerState::IsIdle(CAvctpTransport& aTransport) const
       
   157 	{
       
   158 	LOG_FUNC
       
   159 	TBool hasData = aTransport.HasDataToSend();
       
   160 	TBool pidCount = aTransport.ClientCount(); 
       
   161 	LOG1(_L("Is Idle: %d"), (!hasData && pidCount == 0));
       
   162 	return (!hasData && pidCount == 0);
       
   163 	}
       
   164 
       
   165 /**
       
   166 Default Muxer IdleTimerExpired
       
   167 
       
   168 This is a confirmation that the muxer isn't wanted and so should go
       
   169 to the closed state and die.
       
   170 
       
   171   @internalComponent
       
   172   @param aTransport The Muxer which is idle
       
   173 */
       
   174 void TAvctpMuxerState::IdleTimerExpired(CAvctpTransport& aTransport) const
       
   175 	{
       
   176 	LOG_FUNC
       
   177 
       
   178 	ChangeState(aTransport, CAvctpMuxerStateFactory::EClosed);
       
   179 	}
       
   180 
       
   181 /**
       
   182   Default Muxer State Start (outgoing) function.
       
   183 
       
   184   We've been asked to start a connection to the specified address
       
   185   The default is to assume that nothing needs to be done.
       
   186   
       
   187   @internalComponent
       
   188   @param aTransport The Muxer which is to start up
       
   189   @param aAddr  The remote address to start up
       
   190   @return System wide error code
       
   191 */
       
   192 TInt TAvctpMuxerState::Start(CAvctpTransport& aTransport, const TBTDevAddr& aAddr, TUint16 /*aClientId*/) const
       
   193 	{
       
   194 	LOG_FUNC
       
   195 	__ASSERT_DEBUG(aAddr != TBTDevAddr(0) && aTransport.DevAddr() == aAddr, PanicInState(EWrongBTAddress));
       
   196 	(void)aAddr;
       
   197 	(void)aTransport;
       
   198 	return KErrNone;
       
   199 	}
       
   200 	
       
   201 /**
       
   202   Default Muxer State Start (incoming) function.
       
   203 
       
   204   We've been asked to start a connection using the given L2CAP sap
       
   205   However, this is only valid when we're in the Closed state so 
       
   206   panic
       
   207   
       
   208   @internalComponent
       
   209   @param aTransport The Muxer which is to start up
       
   210   @param aL2CAPConSAP The existing connection
       
   211   @return System wide error code
       
   212 */
       
   213 TInt TAvctpMuxerState::StartIncoming(CAvctpTransport& /*aTransport*/, const TBTDevAddr& /*aAddr*/, CServProviderBase* /*aL2CAPConSAP*/) const
       
   214 	{
       
   215 	LOG_FUNC
       
   216 	PanicInState(EUnexpectedMuxerEvent);
       
   217 	return KErrNone;
       
   218 	}
       
   219 
       
   220 TInt TAvctpMuxerState::AddSecondChannel(CAvctpTransport& /*aTransport*/, CServProviderBase& /*aSAP*/) const
       
   221 	{
       
   222 	LOG_FUNC
       
   223 	return KErrNotReady;
       
   224 	}
       
   225 
       
   226 void TAvctpMuxerState::RemoveSecondChannel(CAvctpTransport& /*aTransport*/) const
       
   227 	{
       
   228 	LOG_FUNC
       
   229 	}
       
   230 
       
   231 TInt TAvctpMuxerState::SecondChannelRemoved(CAvctpTransport& /*aTransport*/) const
       
   232 	{
       
   233 	LOG_FUNC
       
   234 	return KErrNotReady;
       
   235 	}
       
   236 
       
   237 /**
       
   238   Default Muxer State CancelConnectRequest function.
       
   239 
       
   240   This is called when a control client wants to cancel a connection to a particular PID.
       
   241   The connection will actually only be cancelled if there are no remaining control
       
   242   clients for this connection.
       
   243         
       
   244   @internalComponent
       
   245   @param aTransport The Muxer which is affected
       
   246 */		
       
   247 TInt TAvctpMuxerState::CancelConnectRequest(CAvctpTransport& /*aTransport*/, TUint16 /*aClientId*/) const
       
   248 	{
       
   249 	LOG_FUNC
       
   250 	return KErrNotReady;
       
   251 	}
       
   252 
       
   253 
       
   254 /**
       
   255 Default Muxer Shutdown function.
       
   256 
       
   257   @internalComponent
       
   258   @param aTransport The Muxer which is to shutdown
       
   259 */
       
   260 void TAvctpMuxerState::Shutdown(CAvctpTransport& /*aTransport*/, TInt /*aError*/) const
       
   261 	{
       
   262 	LOG_FUNC
       
   263 	PanicInState(EUnexpectedMuxerEvent);
       
   264 	}
       
   265 
       
   266 /**
       
   267 Default Muxer Disconnect function.
       
   268 
       
   269   @internalComponent
       
   270   @param aTransport The Muxer whose L2CAP link has just gone down
       
   271 */
       
   272 void TAvctpMuxerState::Disconnect(CAvctpTransport& /*aTransport*/) const
       
   273 	{
       
   274 	LOG_FUNC
       
   275 	PanicInState(EUnexpectedMuxerEvent);
       
   276 	}
       
   277 
       
   278 /**
       
   279 Default Muxer ConnectComplete function.
       
   280 
       
   281   @internalComponent
       
   282   @param aTransport The Muxer whose L2CAP connection has just come up
       
   283 */
       
   284 void TAvctpMuxerState::ConnectComplete(CAvctpTransport& /*aTransport*/) const
       
   285 	{
       
   286 	LOG_FUNC
       
   287 	PanicInState(EUnexpectedMuxerEvent);
       
   288 	}
       
   289 
       
   290 /**
       
   291 Default Muxer Error function
       
   292 
       
   293   @internalComponent
       
   294   @param aTransport The Muxer which has just received an error from it's channel sap
       
   295   @param anError The error
       
   296   @param anOperationMask The effected operation
       
   297 */
       
   298 void TAvctpMuxerState::Error(CAvctpTransport& /*aTransport*/, TInt /*aError*/,TUint /*aOperationMask*/, TInt /*aChannel*/) const
       
   299 	{
       
   300 	LOG_FUNC
       
   301 	PanicInState(EUnexpectedMuxerEvent);
       
   302 	}
       
   303 
       
   304 /**
       
   305 Default Muxer new data function
       
   306 
       
   307   @internalComponent
       
   308   @param aTransport The Muxer which has received the notification of new data.
       
   309 */
       
   310 TInt TAvctpMuxerState::NewData(CAvctpTransport& /*aTransport*/, TUint /*aMtu*/, TInt /*aChannel*/) const
       
   311 	{
       
   312 	LOG_FUNC
       
   313 	PanicInState(EUnexpectedMuxerEvent);
       
   314 	return KErrNone;
       
   315 	}
       
   316 
       
   317 /**
       
   318 Default Muxer can send function
       
   319 
       
   320   @internalComponent
       
   321   @param aTransport The Muxer which can now send
       
   322 */
       
   323 void TAvctpMuxerState::CanSend(CAvctpTransport& /*aTransport*/,TInt /*aChannel*/) const
       
   324 	{
       
   325 	LOG_FUNC
       
   326 	PanicInState(EUnexpectedMuxerEvent);
       
   327 	}
       
   328 
       
   329 //
       
   330 //                                                                                       //
       
   331 // Implementation of TAvctpStateClosed.                                                  //
       
   332 //                                                                                       //
       
   333 //
       
   334 
       
   335 /**
       
   336 Default constructor
       
   337 
       
   338   @internalComponent
       
   339   @param aFactory The Muxer state factory
       
   340 */
       
   341 TAvctpStateClosed::TAvctpStateClosed(CAvctpMuxerStateFactory& aFactory)
       
   342 	: TAvctpMuxerState(aFactory)
       
   343 
       
   344 	{
       
   345 	LOG_FUNC
       
   346 
       
   347 	STATENAME("Closed");
       
   348 	}
       
   349 	
       
   350 /**
       
   351 Closed state entry function.
       
   352 
       
   353 This is only called on re-entering the Closed state, so we're not needed so delete ourselves
       
   354 
       
   355 @internalComponent
       
   356 @param aTransport The Muxer whose state is changing
       
   357 */
       
   358 void TAvctpStateClosed::Enter(CAvctpTransport& aTransport) const
       
   359 	{
       
   360 	LOG_FUNC
       
   361 
       
   362 	delete &aTransport;
       
   363 	}
       
   364 
       
   365 /**
       
   366 Closed state exit function.
       
   367 
       
   368 @internalComponent
       
   369 @param aTransport The Muxer whose state is changing
       
   370 */
       
   371 void TAvctpStateClosed::Exit(CAvctpTransport& aTransport) const
       
   372 	{
       
   373 	LOG_FUNC
       
   374 	__ASSERT_ALWAYS(aTransport.DevAddr() != TBTDevAddr(0), PanicInState(ENullTBTDevAddr));
       
   375 	}
       
   376 
       
   377 /**
       
   378 Closed Muxer IdleTimerExpired
       
   379 
       
   380 The idle timer should only expire in a non closed state so panic
       
   381 if we get it in this state.
       
   382 
       
   383   @internalComponent
       
   384   @param aTransport The Muxer which is idle
       
   385 */
       
   386 void TAvctpStateClosed::IdleTimerExpired(CAvctpTransport& /*aTransport*/) const
       
   387 	{
       
   388 	LOG_FUNC
       
   389 	
       
   390 	PanicInState(EUnexpectedMuxerEvent);
       
   391 	}
       
   392 
       
   393 /**
       
   394   Closed Muxer State Start (outgoing) function.
       
   395 
       
   396   This function asks us to start a connection to the specified address
       
   397   
       
   398   @internalComponent
       
   399   @param aTransport The Muxer which is to start up
       
   400   @param aAddr  The remote address to start up
       
   401   @return System wide error code
       
   402 */
       
   403 TInt TAvctpStateClosed::Start(CAvctpTransport& aTransport, const TBTDevAddr& aAddr, TUint16 /*aClientId*/) const
       
   404 	{
       
   405 	LOG_FUNC
       
   406 
       
   407 	__ASSERT_DEBUG(aTransport.DevAddr() == TBTDevAddr(0), PanicInState(ETBTDevAddrNotNull));
       
   408 
       
   409 	TInt ret = KErrNone;
       
   410 	if (aAddr != TBTDevAddr(0))
       
   411 		{
       
   412 		// New this up just in time.
       
   413 		TRAP(ret, aTransport.iChannelSAPs[KAvctpPrimaryChannel] = aTransport.iProtocol.LowerProtocol()->NewSAPL(KSockSeqPacket));
       
   414 		if (ret == KErrNone)
       
   415 			{
       
   416 			aTransport.AssignToDevice(aAddr);			
       
   417 			ChangeState(aTransport, CAvctpMuxerStateFactory::ELinkPending);		
       
   418 			}
       
   419 		}
       
   420 	else
       
   421 		{
       
   422 		// do nothing - stay closed
       
   423 		ret = KErrNone;
       
   424 		}
       
   425 
       
   426 	if (ret != KErrNone)
       
   427 		{
       
   428 		delete &aTransport;
       
   429 		}
       
   430 	return ret;
       
   431 	}
       
   432 	
       
   433 /**
       
   434   Closed Muxer State Start (incoming) function.
       
   435 
       
   436   We've been asked to start a connection using the given L2CAP sap
       
   437   
       
   438   @internalComponent
       
   439   @param aTransport The Muxer which is to start up
       
   440   @param aL2CAPConSAP The existing connection
       
   441   @return System wide error code
       
   442 */
       
   443 TInt TAvctpStateClosed::StartIncoming(CAvctpTransport& aTransport, const TBTDevAddr& aAddr, CServProviderBase* aL2CAPConSAP) const
       
   444 	{
       
   445 	LOG_FUNC
       
   446 
       
   447 	TInt ret = SymbianAvctp::KErrBadAddress;
       
   448 	
       
   449 	// If the following is not true we'll Kern-Exec 3 anyway so assert always
       
   450 	__ASSERT_ALWAYS(aL2CAPConSAP, PanicInState(ENullLowerProtocolSap));
       
   451 	__ASSERT_DEBUG(aTransport.DevAddr() == TBTDevAddr(0), PanicInState(ETBTDevAddrNotNull));
       
   452 
       
   453 	if (aAddr != TBTDevAddr(0))
       
   454 		{
       
   455 		aTransport.AssignToDevice(aAddr);		
       
   456 		aTransport.iChannelSAPs[KAvctpPrimaryChannel] = aL2CAPConSAP;
       
   457 		aTransport.iChannelSAPs[KAvctpPrimaryChannel]->SetNotify(&aTransport);
       
   458 		
       
   459 		aTransport.iChannelSAPs[KAvctpPrimaryChannel]->Start();
       
   460 		ChangeState(aTransport, CAvctpMuxerStateFactory::EOpen);		
       
   461 		ret = KErrNone;
       
   462 		}
       
   463 	// else covered by ret
       
   464 
       
   465 	if (ret != KErrNone)
       
   466 		{
       
   467 		delete &aTransport;
       
   468 		}
       
   469 	return ret;
       
   470 	}
       
   471 	
       
   472 //
       
   473 //                                                                                       //
       
   474 // Implementation of void TAvctpStateLinkPending                                         //
       
   475 //                                                                                       //
       
   476 // Represents a muxer waiting for the L2CAP link to be created.                          //
       
   477 //                                                                                       //
       
   478 //
       
   479 
       
   480 /**
       
   481 Default constructor
       
   482 
       
   483   @internalComponent
       
   484   @param aFactory The Muxer state factory
       
   485 */
       
   486 TAvctpStateLinkPending::TAvctpStateLinkPending(CAvctpMuxerStateFactory& aFactory)
       
   487 	: TAvctpMuxerState(aFactory)
       
   488 
       
   489 	{
       
   490 	LOG_FUNC
       
   491 	STATENAME("LinkPending");
       
   492 	}
       
   493 
       
   494 /**
       
   495 Link Pending state entry function.
       
   496 
       
   497 We request the lower protocol SAP to bring up the link.
       
   498 
       
   499 @internalComponent
       
   500 @param aTransport The Muxer which is waiting for a link
       
   501 */
       
   502 void TAvctpStateLinkPending::Enter(CAvctpTransport& aTransport) const
       
   503 	{
       
   504 	LOG_FUNC
       
   505 
       
   506 	__ASSERT_ALWAYS(aTransport.iChannelSAPs[KAvctpPrimaryChannel], PanicInState(ENullLowerProtocolSap));
       
   507 	
       
   508 	aTransport.DequeIdleTimer();
       
   509 	// we become the socket for iChannelsSAPs[0]
       
   510 	aTransport.iChannelSAPs[KAvctpPrimaryChannel]->SetNotify(&aTransport);
       
   511 	
       
   512 	TL2CAPSockAddr addr;
       
   513 	addr.SetBTAddr(aTransport.DevAddr());
       
   514 	addr.SetPort(KAVCTP);
       
   515 
       
   516 	// the security settings are:	
       
   517 	// (though see :Preauthorise() for the authentication exceptions due to avdtp authentication)
       
   518 	TBTServiceSecurity sec;
       
   519 	sec.SetAuthentication(KOutboundAuthenticationDefault);
       
   520 	sec.SetAuthorisation(KOutboundAuthoristationDefault);
       
   521 	sec.SetEncryption(KOutboundEncryptionDefault);
       
   522 	sec.SetDenied(EFalse);
       
   523 	sec.SetUid(KAvctpServiceUid);	
       
   524 	addr.SetSecurity(sec);
       
   525 	
       
   526 	TInt err = aTransport.iChannelSAPs[KAvctpPrimaryChannel]->SetRemName(addr);
       
   527 	__ASSERT_DEBUG(!err, PanicInState(EErrorSettingAddress));
       
   528 	
       
   529 	// Bring up the L2CAP link
       
   530 	aTransport.iChannelSAPs[KAvctpPrimaryChannel]->ActiveOpen();	
       
   531 	}
       
   532 
       
   533 // TAvctpStateLinkPending::Exit - we'd lke to call aTransport.CheckForIdle(0) but cause
       
   534 // in this state IsIdle always returns EFalse that's not possible. Hence do the
       
   535 // CheckForIdle in the Enter of the next state currently Open or Closed.
       
   536 
       
   537 
       
   538 
       
   539 
       
   540 /**
       
   541 Link Pending Muxer IsIdle
       
   542 
       
   543 The muxer is asking it's state to determine whether it's idle. Cause the link is pending
       
   544 we can't be idle whatever happens. 
       
   545 
       
   546 Note that this means we must call CheckForIdle on aTransport on entering the next state.
       
   547 
       
   548   @internalComponent
       
   549   @param aTransport The Muxer which is idle
       
   550 */
       
   551 TBool TAvctpStateLinkPending::IsIdle(CAvctpTransport& /*aTransport*/) const
       
   552 	{
       
   553 	LOG_FUNC
       
   554 	return EFalse;
       
   555 	}
       
   556 	
       
   557 /**
       
   558   Link Pending Muxer State CancelConnnectRequest function.
       
   559 
       
   560   This is called when a control client wants to cancel a connection to a particular PID.
       
   561   The connection will actually only be cancelled if there are no remaining control
       
   562   clients for this connection.
       
   563   
       
   564   This function just removes the saplinksmgr on aPid from the list of control
       
   565   clients since it no longer is interested in this remote device and shouldn't 
       
   566   receive any more events related to it.
       
   567     
       
   568   @internalComponent
       
   569   @param aTransport The Muxer which is affected
       
   570   @param aPid The pid to cancel the connection too
       
   571 */		
       
   572 TInt TAvctpStateLinkPending::CancelConnectRequest(CAvctpTransport& /*aTransport*/, TUint16 /*aClientId*/) const
       
   573 	{
       
   574 	LOG_FUNC
       
   575 	return KErrNotSupported;
       
   576 	}
       
   577 
       
   578 
       
   579 /**
       
   580 Link Pending Muxer ConnectComplete function.
       
   581 
       
   582 This is the successful result of a connection attempt to a remote device. 
       
   583 (A incoming connection wouldn't have resulted in a BearerConnectComplete on the
       
   584 protocol instead)
       
   585 
       
   586   @internalComponent
       
   587   @param aTransport The Muxer whose L2CAP connection has just come up
       
   588 */
       
   589 void TAvctpStateLinkPending::ConnectComplete(CAvctpTransport& aTransport) const
       
   590 	{
       
   591 	LOG_FUNC
       
   592 
       
   593 	ChangeState(aTransport, CAvctpMuxerStateFactory::EOpen);
       
   594 	aTransport.NotifyLinkUp(aTransport.iRemoteAddr);
       
   595 	}
       
   596 
       
   597 /**
       
   598 Link Pending Muxer Error function
       
   599 
       
   600 This is called after some kind of error in the lower protocol Sap.
       
   601 
       
   602   @internalComponent
       
   603   @param aTransport The Muxer which has just received an error from it's iChannelsSAPs[0]
       
   604   @param anError The error
       
   605   @param anOperationMask The effected operation
       
   606 */
       
   607 void TAvctpStateLinkPending::Error(CAvctpTransport& aTransport, TInt aError,TUint aOperationMask, TInt aChannel) const
       
   608 	{
       
   609 	LOG_FUNC
       
   610 
       
   611 	__ASSERT_DEBUG(aChannel == KAvctpPrimaryChannel, PanicInState(EAvctpInvalidChannel));
       
   612 	
       
   613 	if (aOperationMask & (MSocketNotify::EErrorFatal   | 
       
   614 	  				      MSocketNotify::EErrorConnect | 
       
   615 	  				      MSocketNotify::EErrorClose   | 
       
   616 					 	  MSocketNotify::EErrorAllOperations))
       
   617 		{
       
   618 		// The connect failed, so we'd better go to the Closed state
       
   619 		// and tell clients what's happened.
       
   620 		// Notifications are made before changing the state because the Enter() method of the Close state
       
   621 		// delete the transport, so we can't call anything after changing the state.
       
   622 
       
   623 		aTransport.NotifyLinkError(aError, aChannel == KAvctpSecondaryChannel);
       
   624 		ChangeState(aTransport, CAvctpMuxerStateFactory::EClosed);
       
   625 		}
       
   626 	else if (aOperationMask & (MSocketNotify::EErrorSend  | 
       
   627 	  	 	 		       	   MSocketNotify::EErrorRecv  |
       
   628 	  	 	 		       	   MSocketNotify::EErrorIoctl))
       
   629 		{
       
   630 		// Since the connection isn't up, we shouldn't be getting a send or recv error
       
   631 		// nor do we currently support ioctls in the muxer so:
       
   632 		PanicInState(EUnexpectedMuxerEvent);
       
   633 		}		
       
   634 	}
       
   635 
       
   636 //
       
   637 //                                                                                       //
       
   638 // Implementation of state TAvctpStateOpen                                               //
       
   639 //                                                                                       //
       
   640 // This represents a fully connected Muxer that can send and receive data                //
       
   641 //                                                                                       //
       
   642 //
       
   643 
       
   644 /**
       
   645 Default constructor
       
   646 
       
   647   @internalComponent
       
   648   @param aFactory The Muxer state factory
       
   649 */
       
   650 TAvctpStateOpen::TAvctpStateOpen(CAvctpMuxerStateFactory& aFactory)
       
   651 	: TAvctpMuxerState(aFactory)
       
   652 
       
   653 	{
       
   654 	LOG_FUNC
       
   655 	STATENAME("Open");
       
   656 	}
       
   657 	
       
   658 /**
       
   659 Open state entry function.
       
   660 
       
   661 In case a Avctp Sap asked to send data we need to signal them that they can now send
       
   662 
       
   663 @internalComponent
       
   664 @param aTransport The Muxer whose state is changing
       
   665 */
       
   666 void TAvctpStateOpen::Enter(CAvctpTransport& aTransport) const
       
   667 	{
       
   668 	LOG_FUNC
       
   669 	
       
   670 	aTransport.SetClearToSend(KAvctpPrimaryChannel);	//only first channel is open so far
       
   671 	aTransport.PacketMgr().CanSend(KAvctpPrimaryChannel);
       
   672 	
       
   673 	// for stereo headset usecase, pre-authorise device for AVDTP 
       
   674 	aTransport.iProtocol.SetPreauthorisation(aTransport.iRemoteAddr, ETrue);
       
   675 	}
       
   676 
       
   677 /**
       
   678 Open state exit function.
       
   679 
       
   680 @internalComponent
       
   681 @param aTransport The Muxer whose state is changing
       
   682 */
       
   683 void TAvctpStateOpen::Exit(CAvctpTransport& aTransport) const
       
   684 	{
       
   685 	LOG_FUNC
       
   686 
       
   687 	// for stereo headset usecase, de-pre-authorise device for AVDTP 
       
   688 	aTransport.iProtocol.SetPreauthorisation(aTransport.iRemoteAddr, EFalse);
       
   689 	}
       
   690 
       
   691 
       
   692 /**
       
   693   Open Muxer State Start (outgoing) function.
       
   694 
       
   695   We've been asked to start a connection to the specified address
       
   696   The connection is already up so immediately send a ConnectCfm event
       
   697   
       
   698   @internalComponent
       
   699   @param aTransport The Muxer which is to start up
       
   700   @param aAddr  The remote address to start up
       
   701   @return System wide error code
       
   702 */
       
   703 TInt TAvctpStateOpen::Start(CAvctpTransport& aTransport, const TBTDevAddr& __DEBUG_ONLY(aAddr), TUint16 aClientId) const
       
   704 	{
       
   705 	LOG_FUNC
       
   706 	__ASSERT_DEBUG(aAddr != TBTDevAddr(0) && aTransport.DevAddr() == aAddr, PanicInState(EWrongBTAddress));
       
   707  
       
   708 	aTransport.NotifyAttachConfirm(aClientId, KErrNone, EFalse);
       
   709 	return KErrNone;
       
   710 	}
       
   711 	
       
   712 	
       
   713 /**
       
   714 Open Muxer Shutdown function.
       
   715 
       
   716 This function allows the muxer to be shutdown irrespective of whether 
       
   717 it is idle. This is intended to be used to punish a remote device that
       
   718 has done something naughty.
       
   719 
       
   720 Notify all clients of the problem and go to the closed state.
       
   721 
       
   722   @internalComponent
       
   723   @param aTransport The Muxer which is sending data
       
   724 */
       
   725 void TAvctpStateOpen::Shutdown(CAvctpTransport& aTransport, TInt /*aError*/) const
       
   726 	{
       
   727 	LOG_FUNC
       
   728 
       
   729 	ChangeState(aTransport, CAvctpMuxerStateFactory::EClosed);
       
   730 	}
       
   731 
       
   732 
       
   733 /**
       
   734 Open Muxer Disconnect function.
       
   735 
       
   736 This is the result of a remote device disconnecting from us.
       
   737 Let all interested parties know.
       
   738 
       
   739   @internalComponent
       
   740   @param aTransport The Muxer whose L2CAP link has just gone down
       
   741 */
       
   742 void TAvctpStateOpen::Disconnect(CAvctpTransport& aTransport) const
       
   743 	{
       
   744 	LOG_FUNC
       
   745 
       
   746 	aTransport.iProtocol.PrimaryChannelIncomingRemoteDisconnection(aTransport.iRemoteAddr); 
       
   747 	ChangeState(aTransport, CAvctpMuxerStateFactory::EClosed);		
       
   748 	}
       
   749 
       
   750 /**
       
   751 Open Muxer Error function
       
   752 
       
   753 This is called after some kind of error in the lower protocol Sap.
       
   754 
       
   755   @internalComponent
       
   756   @param aTransport The Muxer which has just received an error from it's channel sap
       
   757   @param anError The error
       
   758   @param anOperationMask The affected operation
       
   759 */	
       
   760 void TAvctpStateOpen::Error(CAvctpTransport& aTransport, TInt aError,TUint aOperationMask, TInt aChannel) const
       
   761 	{	
       
   762 	LOG_FUNC
       
   763 
       
   764 	__ASSERT_DEBUG(aChannel == KAvctpPrimaryChannel, PanicInState(EAvctpInvalidChannel));
       
   765 	
       
   766 	if (aOperationMask & (MSocketNotify::EErrorRecv |
       
   767 						  MSocketNotify::EErrorSend))
       
   768 		{
       
   769 		// the packet mgr is only interested in data plane errors
       
   770 		aTransport.PacketMgr().SignalMuxerError(aError, aOperationMask);
       
   771 		}	
       
   772 
       
   773 	if (aOperationMask & (MSocketNotify::EErrorFatal   | 
       
   774 	  				      MSocketNotify::EErrorClose   | 
       
   775 					 	  MSocketNotify::EErrorAllOperations))
       
   776 		{
       
   777 		// The connection has gone down so we inform our control clients of this fact	
       
   778 		aTransport.NotifyLinkDown(aTransport.iAddress, aChannel, aError);
       
   779 		}
       
   780 	else if (aOperationMask & (MSocketNotify::EErrorConnect |
       
   781 							   MSocketNotify::EErrorIoctl))
       
   782 		{
       
   783 		// Since the connection is already up, we shouldn't be getting a connect error
       
   784 		// nor do we currently support ioctls
       
   785 		PanicInState(EUnexpectedMuxerEvent);
       
   786 		}
       
   787 	ChangeState(aTransport, CAvctpMuxerStateFactory::EClosed);
       
   788 	}
       
   789 
       
   790 TInt TAvctpStateOpen::AddSecondChannel(CAvctpTransport& aTransport, CServProviderBase& aSAP) const
       
   791 	{
       
   792 	LOG_FUNC
       
   793 	// can bind socket to SAP now
       
   794 	aTransport.BindSecondaryChannelSap(aSAP);
       
   795 	
       
   796 	// see if it is connected or not
       
   797 	TL2CAPSockAddr addr;
       
   798 	aSAP.RemName(addr);
       
   799 	
       
   800 	if (addr.BTAddr()==TBTDevAddr(0))
       
   801 		{
       
   802 		addr.SetBTAddr(aTransport.iRemoteAddr);
       
   803 		addr.SetPort(KAvctpSecondChannelPSM);
       
   804 
       
   805 		// the security settings are:	
       
   806 		// (though see :Preauthorise() for the authentication exceptions due to avdtp authentication)
       
   807 		TBTServiceSecurity sec;
       
   808 		sec.SetAuthentication(KSecondaryChannelAuthenticationDefault);
       
   809 		sec.SetAuthorisation(KSecondaryChannelAuthoristationDefault);
       
   810 		sec.SetEncryption(KOutboundEncryptionDefault);
       
   811 		sec.SetDenied(EFalse);
       
   812 		sec.SetUid(KAvctpServiceUid);	
       
   813 		addr.SetSecurity(sec);
       
   814 		
       
   815 		TInt err = aTransport.iChannelSAPs[KAvctpSecondaryChannel]->SetRemName(addr);
       
   816 		__ASSERT_DEBUG(err == KErrNone, PanicInState(ESetRemNameError));
       
   817 
       
   818 		const TUint KDefaultMtu = 335;
       
   819 		
       
   820 		TPckgBuf<TL2CapConfig> config;
       
   821 		config().SetMaxTransmitUnitSize(KAvctpSecondaryChannelInboundMTU);
       
   822 		config().SetMinMTU(KDefaultMtu);
       
   823 		config().SetMaxReceiveUnitSize(KAvctpSecondaryChannelInboundMTU);
       
   824 		config().SetMinMRU(KDefaultMtu);
       
   825 
       
   826 		err = aTransport.iChannelSAPs[KAvctpSecondaryChannel]->SetOption(KSolBtL2CAP, KL2CAPUpdateChannelConfig, config); 
       
   827 		__ASSERT_DEBUG(err == KErrNone, Panic(ESetOptionError));
       
   828 
       
   829 		aTransport.iChannelSAPs[KAvctpSecondaryChannel]->ActiveOpen();
       
   830 		ChangeState(aTransport, CAvctpMuxerStateFactory::ESecondLinkPending);
       
   831 		}
       
   832 	else
       
   833 		{
       
   834 		aTransport.iChannelSAPs[KAvctpSecondaryChannel]->Start();
       
   835 		aTransport.SetClearToSend(KAvctpSecondaryChannel);
       
   836 		ChangeState(aTransport, CAvctpMuxerStateFactory::EFullyOpen);
       
   837 		aTransport.NotifyLinkUp(addr.BTAddr(), ETrue);
       
   838 		}
       
   839 	return KErrNone;
       
   840 	}
       
   841 
       
   842 /**
       
   843 Open Muxer new data function
       
   844 
       
   845 The lower protocol sap has notified the muxer of new data that is 
       
   846 waiting for retrieval.
       
   847 
       
   848 This is called once for each new packet of data that L2CAP has notified the muxer of.
       
   849 We get to the data by calling GetData on the lower protocol SAP 
       
   850 and give the resulting data to the Avctp Protocol, letting it workout who wants it.
       
   851 
       
   852 This assumes a packet interface from L2CAP.
       
   853 
       
   854   @internalComponent
       
   855   @param aTransport The Muxer which has received the notification of new data.
       
   856   @param aMtu The length of the data packet to be read. 
       
   857 */
       
   858 TInt TAvctpStateOpen::NewData(CAvctpTransport& aTransport, TUint aMtu, TInt aChannel) const
       
   859 	{
       
   860 	LOG_FUNC
       
   861 	__ASSERT_DEBUG(aChannel==KAvctpPrimaryChannel, PanicInState(EUnexpectedMuxerEvent));
       
   862 	__ASSERT_DEBUG(aTransport.iChannelSAPs[aChannel], PanicInState(ENullLowerProtocolSap));
       
   863 	
       
   864 	TInt err = KErrNone;
       
   865 	// Read data into the buffer
       
   866 	// the transport feeds us synchronously for first channel
       
   867 	RMBufChain inboundFragment;
       
   868 	TInt dataAvailable = aTransport.iChannelSAPs[KAvctpPrimaryChannel]->GetData(inboundFragment, aMtu, 0);
       
   869 	if (dataAvailable > 0)
       
   870 		{
       
   871 		err = aTransport.iPacketMgr->NewData(inboundFragment, aChannel); // ownership xferred;
       
   872 		
       
   873 		//NewData() only returns an error in the case where the data header gave a parse error.
       
   874 		//In this case the muxer is shutdown, so the remaining packets cannot be read.
       
   875 		//In the case where NewData() failed to pass on onwnership, an attempt should be made
       
   876 		//to read remaining packets, so no error is returned.
       
   877 		if(err != KErrNone)
       
   878 			{
       
   879 			inboundFragment.Free();
       
   880 			}
       
   881 		}
       
   882 	else if (dataAvailable < KErrNone)	// some error occurred reading the data. shutdown the muxer and report the error
       
   883 		{
       
   884 		aTransport.Shutdown(dataAvailable);
       
   885 		inboundFragment.Free();
       
   886 		err = KErrMuxerShutDown;
       
   887 		}
       
   888 	// otherwise, no data available
       
   889 	return err;
       
   890 	}
       
   891 		
       
   892 /**
       
   893 Open Muxer can send function
       
   894 
       
   895 This is called after a blocked Write on the lower protocol SAP. This is a notification
       
   896 that the lower protocol SAP is ready to receive more data.
       
   897 
       
   898   @internalComponent
       
   899   @param aTransport The Muxer which can now send
       
   900 */
       
   901 void TAvctpStateOpen::CanSend(CAvctpTransport& aTransport, TInt aChannel) const
       
   902 	{
       
   903 	LOG_FUNC
       
   904 
       
   905 	aTransport.SetClearToSend(aChannel);	
       
   906 	aTransport.PacketMgr().CanSend(aChannel);
       
   907 	}
       
   908 
       
   909 TAvctpStateFullyOpen::TAvctpStateFullyOpen(CAvctpMuxerStateFactory& aFactory)
       
   910 :TAvctpStateOpen(aFactory)
       
   911 	{
       
   912 	LOG_FUNC
       
   913 	STATENAME("FullyOpen");
       
   914 	}
       
   915 
       
   916 void TAvctpStateFullyOpen::Enter(CAvctpTransport& aTransport) const
       
   917 	{
       
   918 	// should check the sap psm really
       
   919 	aTransport.SetClearToSend(KAvctpSecondaryChannel);
       
   920 	aTransport.PacketMgr().CanSend(KAvctpSecondaryChannel);
       
   921 	}
       
   922 
       
   923 /**
       
   924  The method is defined and left empty on purpose (because this class derives from TAvctpStateOpen 
       
   925  but we don't wont its method to be called.
       
   926  */
       
   927 void TAvctpStateFullyOpen::Exit(CAvctpTransport& /*aTransport*/) const
       
   928 	{
       
   929 	}
       
   930 
       
   931 void TAvctpStateFullyOpen::Error(CAvctpTransport& aTransport, TInt aError, TUint aErrorMask, TInt aChannel) const
       
   932 	{
       
   933 	LOG_FUNC
       
   934 
       
   935     if (aErrorMask & (MSocketNotify::EErrorRecv |
       
   936                           MSocketNotify::EErrorSend))
       
   937         {
       
   938         // the packet mgr is only interested in data plane errors
       
   939         aTransport.PacketMgr().SignalMuxerError(aError, aErrorMask);
       
   940         }   
       
   941 
       
   942     if (aErrorMask & (MSocketNotify::EErrorFatal   | 
       
   943                           MSocketNotify::EErrorClose   | 
       
   944                           MSocketNotify::EErrorAllOperations))
       
   945         {
       
   946         // The connection has gone down so we inform our control clients of this fact   
       
   947         aTransport.NotifyLinkDown(aTransport.iAddress, aChannel, aError);
       
   948         }
       
   949     else if (aErrorMask & (MSocketNotify::EErrorConnect |
       
   950                                MSocketNotify::EErrorIoctl))
       
   951         {
       
   952         // Since the connection is already up, we shouldn't be getting a connect error
       
   953         // nor do we currently support ioctls
       
   954         PanicInState(EUnexpectedMuxerEvent);
       
   955         }
       
   956     ChangeState(aTransport, aChannel == KAvctpSecondaryChannel ? CAvctpMuxerStateFactory::EOpen : CAvctpMuxerStateFactory::EClosed);
       
   957 	}
       
   958 
       
   959 void TAvctpStateFullyOpen::Disconnect(CAvctpTransport& aTransport) const
       
   960 	{
       
   961 	aTransport.iProtocol.PrimaryChannelIncomingRemoteDisconnection(aTransport.iRemoteAddr); 
       
   962 	ChangeState(aTransport, CAvctpMuxerStateFactory::EClosed);		
       
   963 	}
       
   964 
       
   965 void TAvctpStateFullyOpen::CanSend(CAvctpTransport& aTransport,TInt aChannel) const
       
   966 	{
       
   967 	aTransport.SetClearToSend(aChannel);	
       
   968 	aTransport.PacketMgr().CanSend(aChannel);
       
   969 	}
       
   970 
       
   971 TInt TAvctpStateFullyOpen::NewData(CAvctpTransport& aTransport, TUint aMtu, TInt aChannel) const
       
   972 	{
       
   973 	RMBufChain inboundFragment;
       
   974 	__ASSERT_DEBUG(aTransport.iChannelSAPs[aChannel], PanicInState(ENullLowerProtocolSap));
       
   975 	aTransport.iChannelSAPs[aChannel]->GetData(inboundFragment, aMtu, 0);
       
   976 	
       
   977 	TInt err = aTransport.iPacketMgr->NewData(inboundFragment, aChannel); // ownership xferred;
       
   978 	
       
   979 	if(err != KErrNone)
       
   980 		{
       
   981 		inboundFragment.Free();
       
   982 		}
       
   983 	return err;
       
   984 	}
       
   985 
       
   986 TInt TAvctpStateFullyOpen::Start(CAvctpTransport& aTransport, const TBTDevAddr& /*aAddr*/, TUint16 aClientId) const
       
   987 	{
       
   988 	aTransport.NotifyAttachConfirm(aClientId, KErrNone, EFalse);
       
   989 	return KErrNone;
       
   990 	}
       
   991 
       
   992 void TAvctpStateFullyOpen::RemoveSecondChannel(CAvctpTransport& aTransport) const
       
   993 	{
       
   994 	LOG_FUNC
       
   995 	// shutdown secondary channel without notifying clients (second param is EFalse)
       
   996 	ShutDownSecondarySap(aTransport, EFalse);
       
   997 	}
       
   998 
       
   999 TInt TAvctpStateFullyOpen::SecondChannelRemoved(CAvctpTransport& aTransport) const
       
  1000 	{
       
  1001 	LOG_FUNC
       
  1002 	// shutdown secondary channel notifying clients (second param is ETrue)
       
  1003 	ShutDownSecondarySap(aTransport, ETrue);
       
  1004 	return KErrNone;
       
  1005 	}
       
  1006 
       
  1007 void TAvctpStateFullyOpen::Shutdown(CAvctpTransport& aTransport, TInt /*aError*/) const
       
  1008 	{
       
  1009 	ChangeState(aTransport, CAvctpMuxerStateFactory::EClosed);
       
  1010 	}
       
  1011 
       
  1012 
       
  1013 /**
       
  1014  The method is declared and left empty on purpose (because this class derives from TAvctpStateOpen 
       
  1015  but we don't wont its method to be called.
       
  1016  */
       
  1017 void TAvctpStateSecondChannelPending::Enter(CAvctpTransport& /*aTransport*/) const
       
  1018 	{
       
  1019 	}
       
  1020 
       
  1021 /**
       
  1022  The method is declared and left empty on purpose (because this class derives from TAvctpStateOpen 
       
  1023  but we don't wont its method to be called.
       
  1024  */
       
  1025 void TAvctpStateSecondChannelPending::Exit(CAvctpTransport& /*aTransport*/) const
       
  1026 	{
       
  1027 	}
       
  1028 
       
  1029 void TAvctpStateSecondChannelPending::Error(CAvctpTransport& aTransport, TInt aError, TUint /*aErrorMask*/, TInt aChannel) const
       
  1030 	{
       
  1031 	aTransport.NotifyLinkError(aError, aChannel == KAvctpSecondaryChannel);
       
  1032 	ChangeState(aTransport, CAvctpMuxerStateFactory::EOpen);
       
  1033 	}
       
  1034 
       
  1035 TAvctpStateSecondChannelPending::TAvctpStateSecondChannelPending(CAvctpMuxerStateFactory& aFactory)
       
  1036 :TAvctpStateOpen(aFactory)
       
  1037 	{
       
  1038 	LOG_FUNC
       
  1039 	STATENAME("SecondLinkPending");
       
  1040 	}
       
  1041 
       
  1042 void TAvctpStateSecondChannelPending::ConnectComplete(CAvctpTransport& aTransport) const
       
  1043 	{
       
  1044 	LOG_FUNC
       
  1045 	aTransport.NotifyLinkUp(aTransport.iRemoteAddr, ETrue);
       
  1046 	ChangeState(aTransport, CAvctpMuxerStateFactory::EFullyOpen);
       
  1047 	}
       
  1048 
       
  1049 void TAvctpStateSecondChannelPending::Disconnect(CAvctpTransport& aTransport) const
       
  1050 	{
       
  1051 	LOG_FUNC
       
  1052 	
       
  1053 	// In this state there is at least one outstanding ioctl on the secondary ctrl sap that 
       
  1054 	// we must complete
       
  1055 	
       
  1056 	aTransport.NotifyAttachConfirm(KErrMuxerShutDown, ETrue);
       
  1057 	aTransport.iProtocol.PrimaryChannelIncomingRemoteDisconnection(aTransport.iRemoteAddr); 
       
  1058 	ChangeState(aTransport, CAvctpMuxerStateFactory::EClosed);		
       
  1059 	}
       
  1060 
       
  1061 void TAvctpStateSecondChannelPending::CanSend(CAvctpTransport& aTransport, TInt __DEBUG_ONLY(aChannel)) const
       
  1062 	{
       
  1063 	LOG_FUNC
       
  1064 	__ASSERT_DEBUG(aChannel == KAvctpPrimaryChannel, PanicInState(EAvctpInvalidChannel));
       
  1065 	aTransport.SetClearToSend(KAvctpPrimaryChannel);	
       
  1066 	aTransport.PacketMgr().CanSend(KAvctpPrimaryChannel);
       
  1067 	}
       
  1068 
       
  1069 TInt TAvctpStateSecondChannelPending::NewData(CAvctpTransport& aTransport, TUint aMtu, TInt aChannel) const
       
  1070 	{
       
  1071 	LOG_FUNC
       
  1072 	__ASSERT_DEBUG(aChannel==KAvctpPrimaryChannel, PanicInState(EUnexpectedMuxerEvent));
       
  1073 	__ASSERT_DEBUG(aTransport.iChannelSAPs[aChannel], PanicInState(ENullLowerProtocolSap));
       
  1074 	
       
  1075 	// Read data into the buffer
       
  1076 	// the transport feeds us synchronously for first channel
       
  1077 	RMBufChain inboundFragment;
       
  1078 	aTransport.iChannelSAPs[KAvctpPrimaryChannel]->GetData(inboundFragment, aMtu, 0);
       
  1079 
       
  1080 	TInt err = aTransport.iPacketMgr->NewData(inboundFragment, aChannel); // ownership xferred;
       
  1081 	
       
  1082 	//NewData() only returns an error in the case where the data header gave a parse error.
       
  1083 	//In this case the muxer is shutdown, so the remaining packets cannot be read.
       
  1084 	//In the case where NewData() failed to pass on onwnership, an attempt should be made
       
  1085 	//to read remaining packets, so no error is returned.
       
  1086 	if(err != KErrNone)
       
  1087 		{
       
  1088 		inboundFragment.Free();
       
  1089 		}
       
  1090 	return err;
       
  1091 	}
       
  1092 
       
  1093 TInt TAvctpStateSecondChannelPending::Start(CAvctpTransport& /*aTransport*/, const TBTDevAddr& /*aAddr*/, TUint16 /*aClientId*/) const
       
  1094 	{
       
  1095 	return KErrNotReady;
       
  1096 	}
       
  1097 
       
  1098 void TAvctpStateSecondChannelPending::Shutdown(CAvctpTransport& aTransport, TInt /*aError*/) const
       
  1099 	{
       
  1100 	LOG_FUNC
       
  1101 	ChangeState(aTransport, CAvctpMuxerStateFactory::EClosed);
       
  1102 	}
       
  1103 
       
  1104 void TAvctpStateSecondChannelPending::RemoveSecondChannel(CAvctpTransport& aTransport) const
       
  1105 	{
       
  1106 	LOG_FUNC
       
  1107 	// shutdown secondary channel without notifying clients (second param is EFalse)
       
  1108 	ShutDownSecondarySap(aTransport, EFalse);
       
  1109 	}
       
  1110 //
       
  1111 //                                                                                       //
       
  1112 // CAvctpMuxerStateFactory implementation                                                     //
       
  1113 //                                                                                       //
       
  1114 //
       
  1115 
       
  1116 /**
       
  1117 Default constructor
       
  1118 
       
  1119   @internalComponent
       
  1120 */
       
  1121 CAvctpMuxerStateFactory::CAvctpMuxerStateFactory()
       
  1122 	{
       
  1123 	LOG_FUNC
       
  1124 	}
       
  1125 
       
  1126 /**
       
  1127 Destructor to free resources.
       
  1128 
       
  1129 Delete all our lightweight states
       
  1130 
       
  1131   @internalComponent
       
  1132 */
       
  1133 CAvctpMuxerStateFactory::~CAvctpMuxerStateFactory()
       
  1134 	{
       
  1135 	LOG_FUNC
       
  1136 
       
  1137 	iStates.DeleteAll();
       
  1138 	}
       
  1139 
       
  1140 /**
       
  1141 Static factory factory function (think about it... ;-)
       
  1142 
       
  1143 Creates an array of lightweight states for Muxers
       
  1144 
       
  1145   @internalComponent
       
  1146   @leave KErrNoMemory if the CAvctpMuxerStateFactory object could not be created
       
  1147   @return A pointer to the new state factory
       
  1148 */
       
  1149 CAvctpMuxerStateFactory* CAvctpMuxerStateFactory::NewL()
       
  1150 	{
       
  1151 	LOG_STATIC_FUNC
       
  1152 
       
  1153 	CAvctpMuxerStateFactory* factory = new(ELeave) CAvctpMuxerStateFactory();
       
  1154 	CleanupStack::PushL(factory);
       
  1155 
       
  1156 	// Create all the new states
       
  1157 	factory->iStates[EClosed]	  	 = new(ELeave) TAvctpStateClosed(*factory);
       
  1158 	factory->iStates[ELinkPending]	 = new(ELeave) TAvctpStateLinkPending(*factory);
       
  1159 	factory->iStates[EOpen]		  	 = new(ELeave) TAvctpStateOpen(*factory);
       
  1160 	factory->iStates[ESecondLinkPending] = new(ELeave) TAvctpStateSecondChannelPending(*factory);
       
  1161 	factory->iStates[EFullyOpen] = new(ELeave) TAvctpStateFullyOpen(*factory);
       
  1162 	
       
  1163 	CleanupStack::Pop(factory);
       
  1164 	return factory;
       
  1165 	}
       
  1166 
       
  1167 /**
       
  1168 Utility function to get a lightweight state
       
  1169 
       
  1170   @internalComponent
       
  1171   @param aState Index to the state required
       
  1172   @return A lightweight Muxer state
       
  1173 */
       
  1174 TAvctpMuxerState& CAvctpMuxerStateFactory::GetState(const TAvctpMuxerStates aState) const
       
  1175 	{
       
  1176 	LOG_FUNC
       
  1177 	__ASSERT_DEBUG(aState != EMaxStates, Panic(EAvctpMuxerStateOutOfBounds));
       
  1178 	return *iStates[aState];
       
  1179 	}
       
  1180 
       
  1181 TInt CAvctpMuxerStateFactory::StateIndex(const TAvctpMuxerState* aState) const
       
  1182 	{
       
  1183 	TInt state;
       
  1184 	for (state = 0; state < EMaxStates; state++)
       
  1185 		{
       
  1186 		if (iStates[state] == aState)
       
  1187 			{
       
  1188 			return state;
       
  1189 			}
       
  1190 		}
       
  1191 	
       
  1192 	return KUnknownState;
       
  1193 	}