bluetooth/btstack/l2cap/l2capSigStates.cpp
changeset 0 29b1cd4cb562
child 17 32ba20339036
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     1 // Copyright (c) 2004-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 //
       
    15 
       
    16 // Implements the signalling state machine for an L2Cap Channel
       
    17 #include <bluetooth/logger.h>
       
    18 
       
    19 #include "l2capSAPSignalHandler.h"
       
    20 #include "l2capSigStates.h"
       
    21 #include "l2capSigPacketConnection.h"
       
    22 #include "l2capSigPacketConfigure.h"
       
    23 #include "l2capSigPacketDisconnection.h"
       
    24 #include "l2capSigPacketEcho.h"
       
    25 #include "l2capSigPacketCommandReject.h"
       
    26 
       
    27 #include "l2capSigStates.h"
       
    28 #include "l2signalmgr.h"
       
    29 
       
    30 #include "l2capSAPSignalHandler.h"
       
    31 #include "l2cap.h"
       
    32 #include "l2sap.h"
       
    33 
       
    34 #include "l2util.h"
       
    35 
       
    36 #ifdef __FLOG_ACTIVE
       
    37 _LIT8(KLogComponent, LOG_COMPONENT_L2CAP);
       
    38 #endif
       
    39 
       
    40 /*************************************************************************/
       
    41 //
       
    42 // TL2CAPSigState Implementation
       
    43 // 
       
    44 
       
    45 TL2CAPSigState::TL2CAPSigState(const CL2CAPSignalStateFactory& aFactory):
       
    46 	iFactory(aFactory)
       
    47 	{
       
    48 	LOG_FUNC
       
    49 	}
       
    50 
       
    51 // Events from the SAP
       
    52 void TL2CAPSigState::CloseChannelRequest(CL2CapSAPSignalHandler& aSignalHandler) const
       
    53 	{
       
    54 	LOG_FUNC
       
    55 	// This is a standard action for most states.
       
    56 	// Disconnect received from the SAP.  Return to the
       
    57 	// closed state.
       
    58 	aSignalHandler.HandleLinkError();
       
    59 	aSignalHandler.SetState(iFactory.GetState(CL2CAPSignalStateFactory::EClosed));
       
    60 		
       
    61 	// Signal back to the SAP that disconnection is complete.
       
    62 	aSignalHandler.SAP()->ChannelClosed();
       
    63 	}
       
    64 
       
    65 void TL2CAPSigState::OpenChannelRequest(CL2CapSAPSignalHandler& /*aSignalHandler*/) const	
       
    66 	{
       
    67 	LOG_FUNC
       
    68 	PanicInState(EL2CAPUnexpectedSAPEvent);
       
    69 	}
       
    70 
       
    71 void TL2CAPSigState::ConnectRequestReceived(CL2CapSAPSignalHandler& /*aSignalHandler*/) const	
       
    72 	{
       
    73 	LOG_FUNC
       
    74 	PanicInState(EL2CAPUnexpectedSAPEvent);
       
    75 	}
       
    76 
       
    77 void TL2CAPSigState::ConfigureChannelRequest(CL2CapSAPSignalHandler& /*aSignalHandler*/) const	
       
    78 	{
       
    79 	LOG_FUNC
       
    80 	PanicInState(EL2CAPUnexpectedSAPEvent);
       
    81 	}
       
    82 
       
    83 // L2CAP commands received from the peer.
       
    84 void TL2CAPSigState::ConnectResponse(CL2CapSAPSignalHandler& /*aSignalHandler*/,
       
    85 						 			 HConnectionResponse* /*aConnectionResponse*/) const
       
    86 	{
       
    87 	LOG_FUNC
       
    88 	LOG1(_L("Unhandled Connect Response, state = 0x%08x"), this);
       
    89 	}
       
    90 
       
    91 
       
    92 void TL2CAPSigState::ConfigRequest(CL2CapSAPSignalHandler& /*aSignalHandler*/,
       
    93 										  HConfigureRequest* /*aConfigRequest*/) const
       
    94 	{
       
    95 	LOG_FUNC
       
    96 	LOG1(_L("Unhandled Config Req, state = 0x%08x"), this);
       
    97 	}
       
    98 
       
    99 void TL2CAPSigState::ConfigResponse(CL2CapSAPSignalHandler& /*aSignalHandler*/,
       
   100  	                					   HConfigureResponse* /*aConfigResponse*/) const
       
   101 	{
       
   102 	LOG_FUNC
       
   103 	LOG1(_L("Unhandled Config Response, state = 0x%08x"), this);
       
   104 	}
       
   105 	
       
   106 void TL2CAPSigState::DisconnectRequest(CL2CapSAPSignalHandler& /*aSignalHandler*/, 
       
   107                                               TUint8 /*aId*/) const
       
   108 	{
       
   109 	LOG_FUNC
       
   110 	LOG1(_L("Unhandled Disconnect Request, state = 0x%08x"), this);
       
   111 	}
       
   112 
       
   113 void TL2CAPSigState::DisconnectResponse(CL2CapSAPSignalHandler& /*aSignalHandler*/) const
       
   114 	{
       
   115 	LOG_FUNC
       
   116 	LOG1(_L("Unhandled Disconnect Response, state = 0x%08x"), this);
       
   117 	}
       
   118 
       
   119 // Events from the Mux	
       
   120 void TL2CAPSigState::Error(CL2CapSAPSignalHandler& aSignalHandler,
       
   121 	                       TInt aErrorCode, 
       
   122 	                       MSocketNotify::TOperationBitmasks aErrorAction) const
       
   123 	{
       
   124 	LOG_FUNC
       
   125 	// An error has occurred in the Mux or the signal handler.  This means no
       
   126 	// more signalling can be done.  Move to the closed state.  Signal link down
       
   127 	// to clear any pending commands and stop waiting for outstanding responses.
       
   128 	// Inform the SAP of the error.
       
   129 	aSignalHandler.HandleLinkError();
       
   130 	aSignalHandler.SetState(iFactory.GetState(CL2CAPSignalStateFactory::EClosed));
       
   131 
       
   132 	// This will delete the signal handler if the SAP has detached.
       
   133 	aSignalHandler.SignalHandlerErrorD(aErrorCode, aErrorAction);
       
   134 	}
       
   135 
       
   136 TInt TL2CAPSigState::UpdateChannelConfig(CL2CapSAPSignalHandler& aSignalHandler, const TL2CapConfig& aAPIConfig) const
       
   137 	{
       
   138 	LOG_FUNC
       
   139 	// The default action is to update the values.  Different actions are taken
       
   140 	// if configuration is in progress or the channel is open.
       
   141 	TBool reconfigRequired; //  this only matters when the channel is already Open, can ignore it here
       
   142 	return aSignalHandler.ChannelConfig().UpdateConfigAPIChange(aAPIConfig, reconfigRequired);
       
   143 	}
       
   144 
       
   145 TInt TL2CAPSigState::GetNegotiatedChannelMode(const CL2CapSAPSignalHandler& /*aSignalHandler*/, TL2CapChannelMode& /*aMode*/) const
       
   146 	{
       
   147 	return KErrNotReady;
       
   148 	}
       
   149 
       
   150 // Change of state events
       
   151 void TL2CAPSigState::Enter(CL2CapSAPSignalHandler& /*aSignalHandler*/) const
       
   152 	{
       
   153 	LOG_FUNC
       
   154 	}
       
   155 
       
   156 void TL2CAPSigState::Exit(CL2CapSAPSignalHandler& /*aSignalHandler*/) const
       
   157 	{
       
   158 	LOG_FUNC
       
   159 	}	
       
   160 
       
   161 // Signal command queue event
       
   162 void TL2CAPSigState::PendingCommandsDrained(CL2CapSAPSignalHandler& /*aSignalHandler*/) const
       
   163 	{
       
   164 	LOG_FUNC
       
   165 	PanicInState(EL2CAPUnexpectedSAPSignalHandlerEvent);
       
   166 	}
       
   167 
       
   168 
       
   169 // Timers
       
   170 void TL2CAPSigState::ConfigurationTimerExpiry(CL2CapSAPSignalHandler& /*aSignalHandler*/) const
       
   171 	{
       
   172 	LOG_FUNC
       
   173 	PanicInState(EL2CAPUnexpectedSAPSignalHandlerEvent);
       
   174 	}
       
   175 
       
   176 
       
   177 // Helpers		
       
   178 TBool TL2CAPSigState::IsChannelClosed() const
       
   179 	{
       
   180 	LOG_FUNC
       
   181 	// Default is false.  Only true in closed state.
       
   182 	return EFalse;
       
   183 	}
       
   184 
       
   185 TBool TL2CAPSigState::IsChannelOpen() const
       
   186 	{
       
   187 	// Only true in Open state.
       
   188 	return EFalse;
       
   189 	}
       
   190 
       
   191 void TL2CAPSigState::HandleDisconnectRequest(CL2CapSAPSignalHandler& aSignalHandler, TUint8 aId) const
       
   192 	{
       
   193 	LOG_FUNC
       
   194 	aSignalHandler.SetState(iFactory.GetState(CL2CAPSignalStateFactory::EWaitDisconnect));
       
   195 
       
   196 	TInt err = aSignalHandler.ConstructDisconnectResponse(aId);
       
   197 	if(err == KErrNone)
       
   198 		{
       
   199 		aSignalHandler.DrainPendingCommands();
       
   200 		}
       
   201 	else
       
   202 		{
       
   203 		// Failed to send the response.  Go directly into the closed state
       
   204 		// and inform the SAP that disconnection is complete.
       
   205 		aSignalHandler.SetState(iFactory.GetState(CL2CAPSignalStateFactory::EClosed));
       
   206 		aSignalHandler.HandleLinkError();
       
   207 		aSignalHandler.SAP()->ChannelClosed();
       
   208 		}
       
   209 	}
       
   210 
       
   211 void TL2CAPSigState::HandleSAPDisconnect(CL2CapSAPSignalHandler& aSignalHandler) const
       
   212 	{
       
   213 	LOG_FUNC
       
   214 	TInt err = KErrDisconnected;
       
   215 	
       
   216 	if(aSignalHandler.RemotePort() != 0)
       
   217 		{
       
   218 		// We have a remote CID from a response with result pending, issue a disconnect request
       
   219 		aSignalHandler.SetState(iFactory.GetState(CL2CAPSignalStateFactory::EWaitDisconnect));
       
   220 		err = aSignalHandler.ConstructDisconnectRequest();
       
   221 		}	
       
   222 
       
   223 	if(err != KErrNone)
       
   224 		{
       
   225 		// Failed to send the request or do not have a connection (without a valid CID). 
       
   226 		// Go directly into the closed state and inform the SAP that disconnection is complete.
       
   227 		aSignalHandler.HandleLinkError();
       
   228 		aSignalHandler.SetState(iFactory.GetState(CL2CAPSignalStateFactory::EClosed));
       
   229 		aSignalHandler.SAP()->ChannelClosed();
       
   230 		}
       
   231 	}
       
   232 
       
   233 TInt TL2CAPSigState::SendConfigRequest(CL2CapSAPSignalHandler& aSignalHandler) const
       
   234 	{
       
   235 	LOG_FUNC
       
   236 	return aSignalHandler.ConstructConfigRequest();
       
   237 	}
       
   238 
       
   239 TInt TL2CAPSigState::SendConfigResponse(CL2CapSAPSignalHandler& aSignalHandler, TConfigResponseResult aResult) const
       
   240 	{
       
   241 	LOG_FUNC
       
   242 	return aSignalHandler.ConstructConfigResponse(aSignalHandler.GetOutstandingRequestID(), 
       
   243 	                                              HConfigureResponse::KNoConfigurationFlags,
       
   244 	                                              aResult);
       
   245 	}
       
   246 
       
   247 void TL2CAPSigState::DisconnectWithError(CL2CapSAPSignalHandler& aSignalHandler, TInt aError) const
       
   248 	{
       
   249 	aSignalHandler.SetSignalHandlerErrorCode(aError);
       
   250 	aSignalHandler.SetSignalHandlerErrorAction(MSocketNotify::EErrorAllOperations);
       
   251 	aSignalHandler.SetState(iFactory.GetState(CL2CAPSignalStateFactory::EWaitDisconnect));
       
   252 	TInt err = aSignalHandler.ConstructDisconnectRequest();
       
   253 	if(err != KErrNone)
       
   254 		{
       
   255 		// Failed to send the request. Call the Error method, this
       
   256 		// will move the state to closed and forward the error to
       
   257 		// the SAP.
       
   258 		Error(aSignalHandler, aError, MSocketNotify::EErrorAllOperations);
       
   259 		}
       
   260 	}
       
   261 
       
   262 
       
   263 /**
       
   264 Calls the appropriate panic function to encode the panic
       
   265 code with the current state identifier.
       
   266 @param aPanic The panic code that the state is panicking with.
       
   267 */
       
   268 void TL2CAPSigState::PanicInState(TL2CAPPanic aPanic) const
       
   269 	{
       
   270 	LOG_FUNC
       
   271 	Panic(aPanic, iFactory.StateIndex(this));
       
   272 	}
       
   273 	
       
   274 /*************************************************************************/
       
   275 // Implementation of TL2CAPStateClosed
       
   276 //
       
   277 // Starting state and state re-entered when L2Cap logical link has been disconnected.
       
   278 //
       
   279 TL2CAPSigStateClosed::TL2CAPSigStateClosed(const CL2CAPSignalStateFactory& aFactory)
       
   280 	: TL2CAPSigState(aFactory)
       
   281 	{
       
   282 	LOG_FUNC
       
   283 	}
       
   284 
       
   285 void TL2CAPSigStateClosed::OpenChannelRequest(CL2CapSAPSignalHandler& aSignalHandler) const
       
   286 	{
       
   287 	LOG_FUNC
       
   288 	// The SAP has requested a channel be opened.
       
   289 
       
   290 	TInt err = aSignalHandler.ChannelConfig().UpdateLocalConfigWithEntityCapabilities();
       
   291 	if (err == KErrNone)
       
   292 		{
       
   293 		// Get a free source CID for this channel to use.
       
   294 		TL2CAPPort port;
       
   295 		err = aSignalHandler.Mux().GetFreeCID(port);
       
   296 		if(err == KErrNone)
       
   297 			{
       
   298 			// Update local Port.
       
   299 			aSignalHandler.SetLocalPort(port);
       
   300 	
       
   301 			// Override park to allow the connection to be made.
       
   302 			aSignalHandler.OverrideParkMode();
       
   303 	
       
   304 			// Active connection.  Send a Connect Request command.
       
   305 			err = aSignalHandler.ConstructConnectionRequest();
       
   306 			if(err == KErrNone)
       
   307 				{
       
   308 				aSignalHandler.SetState(iFactory.GetState(CL2CAPSignalStateFactory::EWaitConnectRsp));
       
   309 				}
       
   310 			}
       
   311 		}
       
   312 
       
   313 	if (err != KErrNone)
       
   314 		{
       
   315 		// The L2CAP connection could not be initiated.  Error the SAP.
       
   316 		Error(aSignalHandler, err, MSocketNotify::EErrorConnect);
       
   317 		}
       
   318 	}
       
   319 
       
   320 void TL2CAPSigStateClosed::ConnectRequestReceived(CL2CapSAPSignalHandler& aSignalHandler) const
       
   321 	{
       
   322 	LOG_FUNC
       
   323 	TInt err = KErrNone;
       
   324 	// An ACL has been established.
       
   325 	// Get a free source CID for this channel to use.
       
   326 	TL2CAPPort port;
       
   327 	err = aSignalHandler.Mux().GetFreeCID(port);
       
   328 	if(err == KErrNone)
       
   329 		{
       
   330 		// Update local Port.
       
   331 		aSignalHandler.SetLocalPort(port);
       
   332 
       
   333 		// Override park to allow the connection to be made.
       
   334 		aSignalHandler.OverrideParkMode();
       
   335 		// Override all LPMs (if timeout not set to zero)
       
   336 		aSignalHandler.OverrideLPMWithTimeout();
       
   337 
       
   338 		// Send a response with a 'pending' result and start security procedure.
       
   339 		err = aSignalHandler.ConstructConnectionResponse(aSignalHandler.GetOutstandingRequestID(),
       
   340 	                                                     EConnectPending,
       
   341 		                                                 EConnectPendAuthorization);
       
   342 		}
       
   343 
       
   344 	if(err == KErrNone)
       
   345 		{
       
   346 		aSignalHandler.SetState(iFactory.GetState(CL2CAPSignalStateFactory::EWaitConnect));
       
   347 		}
       
   348 	else
       
   349 		{
       
   350 		// The L2CAP connection could not be initiated.  Error the SAP.
       
   351 		Error(aSignalHandler, err, MSocketNotify::EErrorConnect);
       
   352 		}
       
   353 	}
       
   354 
       
   355 
       
   356 void TL2CAPSigStateClosed::CloseChannelRequest(CL2CapSAPSignalHandler& aSignalHandler) const
       
   357 	{
       
   358 	LOG_FUNC
       
   359 	// Detach from the Mux.  Inform the SAP that disconnection is complete.
       
   360 	aSignalHandler.DetachFromMux();
       
   361 	aSignalHandler.SAP()->ChannelClosed();
       
   362 	}
       
   363 
       
   364 void TL2CAPSigStateClosed::ConfigRequest(CL2CapSAPSignalHandler& aSignalHandler,
       
   365 								  		 HConfigureRequest* aConfigRequest) const
       
   366 	{
       
   367 	LOG_FUNC
       
   368 	aSignalHandler.SendInvalidCIDCommandReject(aConfigRequest->ID(),
       
   369 											   0,
       
   370 											   aConfigRequest->DestinationCID());
       
   371 	}
       
   372 
       
   373 TBool TL2CAPSigStateClosed::IsChannelClosed() const
       
   374 	{
       
   375 	LOG_FUNC
       
   376 	return ETrue;
       
   377 	}
       
   378 
       
   379 void TL2CAPSigStateClosed::Enter(CL2CapSAPSignalHandler& aSignalHandler) const
       
   380 	{
       
   381 	LOG_FUNC
       
   382 	// Cancel the configuration timer.
       
   383 	aSignalHandler.CancelTimer();
       
   384 
       
   385 	// If Park mode has been overridden during either channel establishment or
       
   386 	// channel disconnect, remove the override.
       
   387 	// NB It is safe to call this method multiple times.
       
   388 	aSignalHandler.UndoOverrideParkMode();
       
   389 
       
   390 	// If the signal handler has left the closed state, it will have been added
       
   391 	// to the SH list in the Muxer.  Now that the closed state is being
       
   392 	// re-entered, remove the handler from the Muxer queue.
       
   393 	// NB It is safe to call this method multiple times.
       
   394 	aSignalHandler.DetachFromMux();
       
   395 	}
       
   396 
       
   397 	
       
   398 /************************************************************/
       
   399 // Implementation of void TL2CAPSigStateWaitConnectRsp
       
   400 //
       
   401 // This represents an Active SAP that is waiting for a connection response
       
   402 TL2CAPSigStateWaitConnectRsp::TL2CAPSigStateWaitConnectRsp(const CL2CAPSignalStateFactory& aFactory)
       
   403 	: TL2CAPSigState(aFactory)
       
   404 	{
       
   405 	LOG_FUNC
       
   406 	}
       
   407 
       
   408 // L2CAP commands received from the peer.
       
   409 void TL2CAPSigStateWaitConnectRsp::ConnectResponse(CL2CapSAPSignalHandler& aSignalHandler,
       
   410 								 				   HConnectionResponse* aConnectionResponse) const
       
   411 
       
   412 	{
       
   413 	LOG_FUNC	
       
   414 	switch(aConnectionResponse->Result())
       
   415 		{
       
   416 		case EConnectSuccess:
       
   417 			aSignalHandler.SetRemotePort(aConnectionResponse->DestinationCID());
       
   418 			aSignalHandler.SetState(iFactory.GetState(CL2CAPSignalStateFactory::EWaitConfig));
       
   419 			aSignalHandler.SAP()->ChannelOpened();
       
   420 			break;
       
   421 
       
   422 		case EConnectPending:
       
   423 			aSignalHandler.SetRemotePort(aConnectionResponse->DestinationCID());
       
   424 			break;	// No other action here - the ERTx timer has been started.
       
   425 
       
   426 		default:
       
   427 			// One of the failed, non-pending results
       
   428 			
       
   429 			// The connect has failed
       
   430 			// We're re-connectable, so go back to orig state
       
   431 			// NB Error now clears muxer and sets state to closed
       
   432 			Error(aSignalHandler, ((aConnectionResponse->Result() == EConnectSecurityBlock) ? KErrL2CAPAccessRequestDenied : KErrCouldNotConnect), MSocketNotify::EErrorConnect);
       
   433 			break;
       
   434 		};
       
   435 	}
       
   436 	
       
   437 void TL2CAPSigStateWaitConnectRsp::ConfigRequest(CL2CapSAPSignalHandler& /*aSignalHandler*/,
       
   438 						 						 HConfigureRequest* /*aConfigRequest*/) const
       
   439 	{
       
   440 	LOG_FUNC
       
   441 	// Override the default panicing action for this state as we may validly be
       
   442 	// here.  This could happen if the remote has sent a connect response, followed
       
   443 	// by this config request but we have not successfully processed the connect 
       
   444 	// response.  This can happen when the response arrives after the rtx has
       
   445 	// expired but we haven't yet resent the connect request.
       
   446 	}
       
   447 
       
   448 // Events from the Mux
       
   449 void TL2CAPSigStateWaitConnectRsp::CloseChannelRequest(CL2CapSAPSignalHandler& aSignalHandler) const
       
   450 	{	
       
   451 	// Disconnect the channel.	
       
   452 	HandleSAPDisconnect(aSignalHandler);
       
   453 	}
       
   454 
       
   455 void TL2CAPSigStateWaitConnectRsp::Enter(CL2CapSAPSignalHandler& aSignalHandler) const
       
   456 	{
       
   457 	LOG_FUNC
       
   458 	//set the remote CID to 0
       
   459 	aSignalHandler.SetRemotePort(TL2CAPPort(0));
       
   460 	}
       
   461 
       
   462 /****************************************************************************/
       
   463 // 
       
   464 // Waiting for the SAP to indicate that the connection can be established.
       
   465 TL2CAPSigStateWaitConnect::TL2CAPSigStateWaitConnect(const CL2CAPSignalStateFactory& aFactory)
       
   466 	: TL2CAPSigState(aFactory)
       
   467 	{
       
   468 	LOG_FUNC
       
   469 	}
       
   470 	
       
   471 // Events from the SAP
       
   472 void TL2CAPSigStateWaitConnect::OpenChannelRequest(CL2CapSAPSignalHandler& aSignalHandler) const
       
   473 	{
       
   474 	LOG_FUNC
       
   475 
       
   476 	TInt updateConfigErr = aSignalHandler.ChannelConfig().UpdateLocalConfigWithEntityCapabilities();
       
   477 	TInt sendErr = KErrNone;
       
   478 	if (updateConfigErr == KErrNone)
       
   479 		{
       
   480 		// The connection has been allowed.  Send the connection response.
       
   481 		sendErr = aSignalHandler.ConstructConnectionResponse(aSignalHandler.GetOutstandingRequestID(),
       
   482 															 EConnectSuccess,
       
   483 															 EConnectPendNoFurtherInfo);
       
   484 		if (sendErr == KErrNone)
       
   485 			{
       
   486 			// Enter the initial config state and initiate local configuration.
       
   487 			aSignalHandler.SetState(iFactory.GetState(CL2CAPSignalStateFactory::EWaitConfig));
       
   488 			if(!aSignalHandler.DelayConfigRequest())
       
   489 				// We should never get in here! The timer, which allows the delay
       
   490 				// should have been started on entry to the state "WaitConfig". 
       
   491 				{
       
   492 				aSignalHandler.ConfigureChannelRequest();
       
   493 				}
       
   494 			}
       
   495 		} // Success Response
       
   496 	else
       
   497 		{
       
   498 		// We don't have a generic 'Just no' response code, 'No resources' comes closest.
       
   499 		sendErr = aSignalHandler.ConstructConnectionResponse(aSignalHandler.GetOutstandingRequestID(),
       
   500 														     EConnectNoResources,
       
   501 															 EConnectPendNoFurtherInfo);
       
   502 		if (sendErr == KErrNone)
       
   503 			{
       
   504 			// When the response is sent, PendingCommandsDrained() will call Error().
       
   505 			aSignalHandler.SetSignalHandlerErrorCode(updateConfigErr);
       
   506 			aSignalHandler.SetSignalHandlerErrorAction(MSocketNotify::EErrorConnect);
       
   507 			aSignalHandler.DrainPendingCommands();
       
   508 			}
       
   509 		} // Failure response
       
   510 
       
   511 	if (sendErr != KErrNone)
       
   512 		{
       
   513 		// The L2CAP connection could not be initiated.  Error the SAP.
       
   514 		Error(aSignalHandler, sendErr, MSocketNotify::EErrorConnect);
       
   515 		}
       
   516 	}
       
   517 
       
   518 // Events from the Mux
       
   519 void TL2CAPSigStateWaitConnect::CloseChannelRequest(CL2CapSAPSignalHandler& aSignalHandler) const
       
   520 	{
       
   521 	LOG_FUNC
       
   522 
       
   523 	// Tell the peer that there has been a security error
       
   524 	TInt err = aSignalHandler.ConstructConnectionResponse(aSignalHandler.GetOutstandingRequestID(),
       
   525 	                                                      				EConnectSecurityBlock,
       
   526 	                                                      				EConnectPendNoFurtherInfo);
       
   527 
       
   528 	if(err == KErrNone)
       
   529 		{
       
   530 		aSignalHandler.DrainPendingCommands();
       
   531 		}
       
   532 	else
       
   533 		{
       
   534 		// Failed to send the response.  Go directly into the closed state
       
   535 		// and inform the SAP that disconnection is complete.
       
   536 
       
   537 		// HandleLinkError flushes all the signal handler queues.  This should be done
       
   538 		// before making the transition to the EClosed state because the signal handler
       
   539 		// will be detached from the mux it is bound to, at which point we expect there
       
   540 		// to be no commands queued (as we will no longer be able to deal with them).
       
   541 		aSignalHandler.HandleLinkError();
       
   542 		aSignalHandler.SetState(iFactory.GetState(CL2CAPSignalStateFactory::EClosed));
       
   543 		aSignalHandler.SAP()->ChannelClosed();
       
   544 		}
       
   545 	}
       
   546 
       
   547 void TL2CAPSigStateWaitConnect::PendingCommandsDrained(CL2CapSAPSignalHandler& aSignalHandler) const
       
   548 	{
       
   549 	LOG_FUNC
       
   550 	if(aSignalHandler.SignalHandlerErrorCode() != KErrNone)
       
   551 		{
       
   552 		Error(aSignalHandler, aSignalHandler.SignalHandlerErrorCode(), aSignalHandler.SignalHandlerErrorAction());
       
   553 		}
       
   554 	else
       
   555 		{
       
   556 		// This will delete the signal handler if the SAP has detached.
       
   557 		aSignalHandler.SetState(iFactory.GetState(CL2CAPSignalStateFactory::EClosed));
       
   558 		aSignalHandler.SignalHandlerDisconnectedCanClose();
       
   559 		}
       
   560 	}
       
   561 
       
   562 // L2CAP commands received from the peer.
       
   563 void TL2CAPSigStateWaitConnect::DisconnectRequest(CL2CapSAPSignalHandler& aSignalHandler, TUint8 aId) const
       
   564 	{
       
   565 	LOG_FUNC
       
   566 	// Call a common helper function to handle the Disconnect Request.
       
   567 	HandleDisconnectRequest(aSignalHandler, aId);
       
   568 	}
       
   569 
       
   570 
       
   571 /**************************************************************************/
       
   572 // Implementation of TL2CAPSigStateConfigBase
       
   573 //
       
   574 // This represents a SAP that is currently undergoing 
       
   575 // configuration on the link whether it be initial or reconfiguration
       
   576 // This is the base class and will never itself be instantiated.
       
   577 
       
   578 TL2CAPSigStateConfigBase::TL2CAPSigStateConfigBase(const CL2CAPSignalStateFactory& aFactory)
       
   579 	: TL2CAPSigState(aFactory)
       
   580 	{
       
   581 	LOG_FUNC
       
   582 	}
       
   583 
       
   584 // L2CAP commands received from the peer.
       
   585 void TL2CAPSigStateConfigBase::DisconnectRequest(CL2CapSAPSignalHandler& aSignalHandler, TUint8 aId) const
       
   586 	{
       
   587 	LOG_FUNC
       
   588 	// FIRST - Flush all three signalling queues
       
   589 	aSignalHandler.HandleLinkError();
       
   590 	
       
   591 	// Call a common helper function to handle the Disconnect Request.
       
   592 	// NB This will add a new command to the pending command list.
       
   593 	HandleDisconnectRequest(aSignalHandler, aId);
       
   594 	}
       
   595 
       
   596 void TL2CAPSigStateConfigBase::Error(CL2CapSAPSignalHandler& aSignalHandler,
       
   597            							 TInt aErrorCode, 
       
   598            							 MSocketNotify::TOperationBitmasks aErrorAction) const
       
   599 	{
       
   600 	LOG_FUNC
       
   601 	// Cancel the configuration timer.
       
   602 	aSignalHandler.CancelTimer();
       
   603 
       
   604 	// Call the base class error method.
       
   605 	TL2CAPSigState::Error(aSignalHandler, aErrorCode, aErrorAction);
       
   606 	}      							 	
       
   607 
       
   608 // Events from the SAP
       
   609 void TL2CAPSigStateConfigBase::CloseChannelRequest(CL2CapSAPSignalHandler& aSignalHandler) const
       
   610 	{
       
   611 	LOG_FUNC
       
   612 	// Disconnect the channel.
       
   613 	HandleSAPDisconnect(aSignalHandler);
       
   614 	}
       
   615 
       
   616 TInt TL2CAPSigStateConfigBase::UpdateChannelConfig(CL2CapSAPSignalHandler& /*aSignalHandler*/, const TL2CapConfig& /*aAPIConfig*/) const
       
   617 	{
       
   618 	LOG_FUNC
       
   619 	// Configuration is in progress.
       
   620 	return KErrL2CAPConfigAlreadyInProgress;
       
   621 	}
       
   622 
       
   623 // Timer Events
       
   624 void TL2CAPSigStateConfigBase::ConfigurationTimerExpiry(CL2CapSAPSignalHandler& aSignalHandler) const
       
   625 	{
       
   626 	LOG_FUNC
       
   627 	CloseChannelRequest(aSignalHandler);
       
   628 	}
       
   629 	
       
   630 // Common handling of Config Request and Response commands.
       
   631 void TL2CAPSigStateConfigBase::ConfigRequest(CL2CapSAPSignalHandler& aSignalHandler,
       
   632 											 HConfigureRequest* aConfigRequest,
       
   633 											 CL2CAPSignalStateFactory::TSigStates aConfigSuccessState) const
       
   634 	{
       
   635 	LOG_FUNC
       
   636 	RMBufChain unknownOptions;
       
   637 	// Following the state machine, we know that OUR config request
       
   638 	// has not yet been sent (or even queued) if the next state
       
   639 	// (on success) is EWaitSendConfig
       
   640 	TBool configRequestSent = aConfigSuccessState != CL2CAPSignalStateFactory::EWaitSendConfig;
       
   641 
       
   642 	TInt err = aSignalHandler.ChannelConfig().HandleConfigRequest(*aConfigRequest, unknownOptions);
       
   643 	if(err == KErrNone)
       
   644 		{
       
   645 		// Check if the Config Request has been segmented using the continuation flag.
       
   646 		if(!(aConfigRequest->Flags() & KConfigOptionContinuationMask))
       
   647 			{
       
   648 			TConfigResponseResult result;
       
   649 			err = aSignalHandler.ChannelConfig().ConfigRequestComplete(configRequestSent, result);
       
   650 			if (err == KErrConfigRejected)
       
   651 				{
       
   652 				// 'State 2' negotiation channel mode conflict - disconnect when a request
       
   653 				// for anything other than our preferred channel mode is received.
       
   654 				// Don't send a Config Response, send a Disconnect Request immediately.
       
   655 				DisconnectWithError(aSignalHandler, err);
       
   656 				}
       
   657 			else
       
   658 				{
       
   659 				if (err == KErrNone)
       
   660 					{
       
   661 					aSignalHandler.SetOutstandingRequestID(aConfigRequest->ID());
       
   662 					// Send the config response.
       
   663 					err = SendConfigResponse(aSignalHandler, result);
       
   664 					if(err == KErrNone)
       
   665 						{
       
   666 						// Check if the peer config was acceptable.
       
   667 						switch(result)
       
   668 							{
       
   669 							case EConfigSuccess:
       
   670 								// Move to next state.
       
   671 								aSignalHandler.SetState(iFactory.GetState(aConfigSuccessState));
       
   672 								break;
       
   673 	
       
   674 							case EConfigUnacceptableParams:
       
   675 							case EConfigRejected:
       
   676 								// Stay in current state.
       
   677 								break;
       
   678 	
       
   679 							case EConfigUnknownOption: // impossible on this path
       
   680 							default:
       
   681 								__ASSERT_DEBUG(EFalse, PanicInState(EL2CAPInvalidConfigResponseCodeGenerated));
       
   682 								break;
       
   683 							}
       
   684 						} // err == KErrNone
       
   685 					} // err == KErrNone
       
   686 				if (err)
       
   687 					{
       
   688 					// The L2CAP connection could not be initiated.  Error the SAP.
       
   689 					Error(aSignalHandler, err, MSocketNotify::EErrorAllOperations);
       
   690 					}
       
   691 				} // err != KErrConfigRejected
       
   692 			} // continuation bit not set
       
   693 		else
       
   694 			{
       
   695 			// The config request has been segmented.  Send an empty Config Response
       
   696 			// message and wait for the final config request containing a C-Flag set to zero.
       
   697 			err = aSignalHandler.ConstructConfigResponse(aConfigRequest->ID(),
       
   698 			                                             KConfigOptionContinuationMask,
       
   699 			                                             EConfigSuccess);
       
   700 			if(err != KErrNone)
       
   701 				{
       
   702 				// The L2CAP connection could not be initiated.  Error the SAP.
       
   703 				Error(aSignalHandler, err, MSocketNotify::EErrorAllOperations);
       
   704 				}
       
   705 			}
       
   706 		} // no error when parsing request command
       
   707 	else
       
   708 		{
       
   709 		TBool segmentedRequest = aConfigRequest->Flags() & KConfigOptionContinuationMask;
       
   710 		TConfigFlags responseFlags = segmentedRequest ? KConfigOptionContinuationMask :
       
   711 														HConfigureResponse::KNoConfigurationFlags;
       
   712 
       
   713 		// Switch on the reason for the incoming request parsing failure.
       
   714 		switch(err)
       
   715 			{
       
   716 			case KErrBadPacketReceived:
       
   717 				// The options could not be decoded.  Send a response
       
   718 				// indicating that the config has been rejected.  Wait for
       
   719 				// the peer to take action.
       
   720 				err = aSignalHandler.ConstructConfigResponse(aConfigRequest->ID(),
       
   721 				                                             responseFlags,
       
   722 				                                             EConfigRejected);
       
   723 
       
   724 				if(err != KErrNone)
       
   725 					{
       
   726 					Error(aSignalHandler, KErrCouldNotConnect, MSocketNotify::EErrorConnect);
       
   727 					}
       
   728 				break;
       
   729 
       
   730 			case KErrConfigUnknownOptions:
       
   731 				err = aSignalHandler.ConstructConfigResponse(aConfigRequest->ID(),
       
   732 				                                             responseFlags,
       
   733 				                                             EConfigUnknownOption,
       
   734 				                                             unknownOptions);
       
   735 				if(err != KErrNone)
       
   736 					{
       
   737 					Error(aSignalHandler, KErrCouldNotConnect, MSocketNotify::EErrorConnect);
       
   738 					}
       
   739 				break;
       
   740 
       
   741 			default:
       
   742 				// The L2CAP connection could not be initiated.  Error the SAP.
       
   743 			    Error(aSignalHandler, err, MSocketNotify::EErrorConnect);
       
   744 				break;
       
   745 			}
       
   746 		} // parsing not successful
       
   747 	// Free the unknown options buffer.
       
   748 	unknownOptions.Free();
       
   749 	}
       
   750 
       
   751 void TL2CAPSigStateConfigBase::ConfigResponse(CL2CapSAPSignalHandler& aSignalHandler,
       
   752 											 HConfigureResponse* aConfigResponse,
       
   753 											 CL2CAPSignalStateFactory::TSigStates aConfigSuccessState) const
       
   754 	{
       
   755 	LOG_FUNC
       
   756 	RMBufChain unknownOptions;
       
   757 	TInt err = aSignalHandler.ChannelConfig().HandleConfigResponse(*aConfigResponse, unknownOptions);
       
   758 
       
   759 	if(err == KErrNone)
       
   760 		{
       
   761 		// Check if the Config Response has been segmented using the continuation flag.
       
   762 		if(!(aConfigResponse->Flags() & KConfigOptionContinuationMask))
       
   763 			{
       
   764 			// Check if the peer has accepted our config.
       
   765 			CL2CapChannelConfig::TChannelConfigStatus status = aSignalHandler.ChannelConfig().LocalConfigurationStatus();
       
   766 
       
   767 			// Modify the status according to the result in the response received.
       
   768 			if(aConfigResponse->Results() == EConfigUnacceptableParams)
       
   769 				{
       
   770 				// If the result was unacceptable parameters always reconfigure.
       
   771 				if(status == CL2CapChannelConfig::EChannelConfigComplete)
       
   772 					{
       
   773 					status = CL2CapChannelConfig::EChannelConfigOutstanding;
       
   774 					}
       
   775 				}
       
   776 			else
       
   777 				{
       
   778 				if(aConfigResponse->Results() != EConfigSuccess)
       
   779 					{
       
   780 					// Either the peer has rejected the last request or some of the
       
   781 					// parameters contained in the last request are not recognised.
       
   782 					// Ensure the channel is disconnected.
       
   783 					status = CL2CapChannelConfig::EChannelConfigFailed;
       
   784 					}
       
   785 				}
       
   786 
       
   787 			switch(status)
       
   788 				{
       
   789 				case CL2CapChannelConfig::EChannelConfigComplete:
       
   790 					// Configuration complete.  Switch to the next state.
       
   791 					aSignalHandler.SetState(iFactory.GetState(aConfigSuccessState));
       
   792 					break;
       
   793 
       
   794 				case CL2CapChannelConfig::EChannelConfigOutstanding:
       
   795 					// Local config should be restarted.
       
   796 					// Send another Config Request and stay in this state.
       
   797 					err = SendConfigRequest(aSignalHandler);
       
   798 					if(err != KErrNone)
       
   799 						{
       
   800 						Error(aSignalHandler, KErrCouldNotConnect, MSocketNotify::EErrorAllOperations);
       
   801 						}
       
   802 					break;
       
   803 
       
   804 				case CL2CapChannelConfig::EChannelConfigFailed:
       
   805 					// Configuration has failed.
       
   806 					// Store the error code and disconnect the channel.
       
   807 					DisconnectWithError(aSignalHandler, KErrConfigRejected);
       
   808 					break;
       
   809 
       
   810 				default:
       
   811 					__ASSERT_DEBUG(EFalse, PanicInState(EL2CAPInvalidPeerConfigCodeGenerated));
       
   812 					break;
       
   813 				};
       
   814 			}
       
   815 		// else - the config response has been segmented - wait for the final config
       
   816 		// response containing a C-Flag set to zero.
       
   817 		}
       
   818 	else
       
   819 		{
       
   820 		Error(aSignalHandler, KErrCouldNotConnect, MSocketNotify::EErrorAllOperations);
       
   821 		}
       
   822 	// Free the unknown options buffer.	
       
   823 	unknownOptions.Free();
       
   824 	}
       
   825 
       
   826 /**************************************************************************/
       
   827 // Implementation of TL2CAPSigStateWaitConfig
       
   828 //
       
   829 // No config requests/responses sent or received.
       
   830 // Waiting to receive peer L2Cap supported features.
       
   831 //
       
   832 TL2CAPSigStateWaitConfig::TL2CAPSigStateWaitConfig(const CL2CAPSignalStateFactory& aFactory)
       
   833 	: TL2CAPSigStateConfigBase(aFactory) 
       
   834 	{
       
   835 	LOG_FUNC
       
   836 	}
       
   837 
       
   838 // Events from the SAP
       
   839 void TL2CAPSigStateWaitConfig::ConfigureChannelRequest(CL2CapSAPSignalHandler& aSignalHandler) const
       
   840 	{
       
   841 	LOG_FUNC
       
   842 	// Local config can now be initiated.  The local config requirements must
       
   843 	// take into account the peer entity capabilities.
       
   844 	__ASSERT_DEBUG(aSignalHandler.ChannelConfig().IsPeerInfoDefined(),
       
   845 				   Panic(EL2CAPFecConfigAttemptWithoutPeerInfo));
       
   846 
       
   847 	if(!aSignalHandler.DelayConfigRequest())
       
   848 		{
       
   849 		// Send our config request.
       
   850 		TInt err = SendConfigRequest(aSignalHandler);
       
   851 		if(err == KErrNone)
       
   852 			{
       
   853 			aSignalHandler.SetState(iFactory.GetState(CL2CAPSignalStateFactory::EWaitConfigReqRsp));
       
   854 			}						
       
   855 		else
       
   856 			{
       
   857 			Error(aSignalHandler, KErrCouldNotConnect, MSocketNotify::EErrorAllOperations);
       
   858 			}
       
   859 		}
       
   860 	// Else wait until the delay timer expires.
       
   861 	}
       
   862 	
       
   863 // L2CAP commands received from the peer.
       
   864 void TL2CAPSigStateWaitConfig::ConfigRequest(CL2CapSAPSignalHandler& aSignalHandler,
       
   865 								  			 HConfigureRequest* aConfigRequest) const
       
   866 	{
       
   867 	LOG_FUNC
       
   868 	// Call the config base class helper method to process this command.
       
   869 	TL2CAPSigStateConfigBase::ConfigRequest(aSignalHandler, aConfigRequest, CL2CAPSignalStateFactory::EWaitSendConfig);
       
   870 	}
       
   871 		
       
   872 // Change of state events
       
   873 void TL2CAPSigStateWaitConfig::Enter(CL2CapSAPSignalHandler& aSignalHandler) const
       
   874 	{
       
   875 	LOG_FUNC
       
   876 	aSignalHandler.StartConfigurationTimer();
       
   877 	}
       
   878 
       
   879 /**************************************************************************/
       
   880 // Implementation of EWaitSendConfig
       
   881 //
       
   882 // Remote's config request received and responded to with 'success'.
       
   883 // Waiting to send outgoing config request...
       
   884 // ...waiting to receive peer L2Cap supported features.
       
   885 //
       
   886 TL2CAPSigStateWaitSendConfig::TL2CAPSigStateWaitSendConfig(const CL2CAPSignalStateFactory& aFactory)
       
   887   : TL2CAPSigStateConfigBase(aFactory) 
       
   888 	{
       
   889 	LOG_FUNC
       
   890 	}
       
   891 	
       
   892 // Events from the SAP
       
   893 void TL2CAPSigStateWaitSendConfig::ConfigureChannelRequest(CL2CapSAPSignalHandler& aSignalHandler) const
       
   894 	{
       
   895 	LOG_FUNC
       
   896 	// Local config can now be initiated.  The local config requirements must
       
   897 	// take into account the peer entity capabilities.
       
   898 	__ASSERT_DEBUG(aSignalHandler.ChannelConfig().IsPeerInfoDefined(),
       
   899 				   Panic(EL2CAPFecConfigAttemptWithoutPeerInfo));
       
   900 
       
   901 	// Send our config request.
       
   902 	TInt err = SendConfigRequest(aSignalHandler);
       
   903 	if(err == KErrNone)
       
   904 		{
       
   905 		aSignalHandler.SetState(iFactory.GetState(CL2CAPSignalStateFactory::EWaitConfigRsp));
       
   906 		}						
       
   907 	else
       
   908 		{
       
   909 		Error(aSignalHandler, KErrCouldNotConnect, MSocketNotify::EErrorAllOperations);
       
   910 		}
       
   911 	}
       
   912 
       
   913 /**************************************************************************/
       
   914 // Implementation of TL2CAPSigStateWaitConfigReqRsp
       
   915 //
       
   916 // Outgoing config request sent.
       
   917 // Waiting for remote's config request, and remote's config response.
       
   918 //
       
   919 TL2CAPSigStateWaitConfigReqRsp::TL2CAPSigStateWaitConfigReqRsp(const CL2CAPSignalStateFactory& aFactory)
       
   920 	: TL2CAPSigStateConfigBase(aFactory) 
       
   921 	{
       
   922 	LOG_FUNC
       
   923 	}
       
   924 
       
   925 // L2CAP commands received from the peer.
       
   926 void TL2CAPSigStateWaitConfigReqRsp::ConfigRequest(CL2CapSAPSignalHandler& aSignalHandler,
       
   927 								  	  			   HConfigureRequest* aConfigRequest) const
       
   928 	{
       
   929 	LOG_FUNC
       
   930 	// Call the config base class helper method to process this command.
       
   931 	TL2CAPSigStateConfigBase::ConfigRequest(aSignalHandler, aConfigRequest, CL2CAPSignalStateFactory::EWaitConfigRsp);
       
   932 	}
       
   933 
       
   934 void TL2CAPSigStateWaitConfigReqRsp::ConfigResponse(CL2CapSAPSignalHandler& aSignalHandler,
       
   935 					  		  	                    HConfigureResponse* aConfigResponse) const
       
   936 	{
       
   937 	LOG_FUNC
       
   938 	// Call the config base class helper method to process this command.
       
   939 	TL2CAPSigStateConfigBase::ConfigResponse(aSignalHandler, aConfigResponse, CL2CAPSignalStateFactory::EWaitConfigReq);
       
   940 	}
       
   941 
       
   942 /**************************************************************************/
       
   943 // Implementation of EWaitConfigRsp
       
   944 //
       
   945 // Remote's config request received and responded to with 'success'.
       
   946 // Outgoing config request sent.
       
   947 // Waiting for remote's config response.
       
   948 //
       
   949 TL2CAPSigStateWaitConfigRsp::TL2CAPSigStateWaitConfigRsp(const CL2CAPSignalStateFactory& aFactory)
       
   950 	: TL2CAPSigStateConfigBase(aFactory) 
       
   951 	{
       
   952 	LOG_FUNC
       
   953 	}
       
   954 
       
   955 void TL2CAPSigStateWaitConfigRsp::ConfigResponse(CL2CapSAPSignalHandler& aSignalHandler,
       
   956 					  		  	                 HConfigureResponse* aConfigResponse) const
       
   957 	{
       
   958 	LOG_FUNC
       
   959 	// Call the config base class helper method to process this command.
       
   960 	TL2CAPSigStateConfigBase::ConfigResponse(aSignalHandler, aConfigResponse, CL2CAPSignalStateFactory::EOpen);
       
   961 	}
       
   962 
       
   963 /**************************************************************************/
       
   964 // Implementation of EWaitConfigReq
       
   965 //
       
   966 // Outgoing config request sent, and responded to with 'success'.
       
   967 // Waiting for remote's config request.
       
   968 //
       
   969 TL2CAPSigStateWaitConfigReq::TL2CAPSigStateWaitConfigReq(const CL2CAPSignalStateFactory& aFactory)
       
   970 	: TL2CAPSigStateConfigBase(aFactory) 
       
   971 	{
       
   972 	LOG_FUNC
       
   973 	}
       
   974 
       
   975 // L2CAP commands received from the peer.
       
   976 void TL2CAPSigStateWaitConfigReq::ConfigRequest(CL2CapSAPSignalHandler& aSignalHandler,
       
   977 								  	  		    HConfigureRequest* aConfigRequest) const
       
   978 	{
       
   979 	LOG_FUNC
       
   980 	// Call the config base class helper method to process this command.
       
   981 	TL2CAPSigStateConfigBase::ConfigRequest(aSignalHandler, aConfigRequest, CL2CAPSignalStateFactory::EOpen);
       
   982 	}
       
   983 
       
   984 	
       
   985 
       
   986 /**************************************************************************/
       
   987 // Implementation of void TL2CAPSigStateOpen
       
   988 //
       
   989 // L2Cap logical link open. Data can be sent and received.
       
   990 //
       
   991 TL2CAPSigStateOpen::TL2CAPSigStateOpen(const CL2CAPSignalStateFactory& aFactory)
       
   992 	: TL2CAPSigState(aFactory) 
       
   993 	{
       
   994 	LOG_FUNC
       
   995 	}
       
   996 
       
   997 void TL2CAPSigStateOpen::ConfigRequest(CL2CapSAPSignalHandler& aSignalHandler,
       
   998 								  	   HConfigureRequest* aConfigRequest) const
       
   999 	{
       
  1000 	LOG_FUNC
       
  1001 	// Starts of reconfiguration instigated by remote device
       
  1002 	aSignalHandler.SetState(iFactory.GetState(CL2CAPSignalStateFactory::EWaitConfig));
       
  1003 
       
  1004 	// Inform the data plane [via the SAP] that reconfig is in progress and data
       
  1005 	// should not be sent until it completes.
       
  1006 	aSignalHandler.ReconfiguringChannel();
       
  1007 	static_cast<void>(aSignalHandler.HandleConfigureRequest(aConfigRequest));
       
  1008 
       
  1009 	// Start local reconfiguration.
       
  1010 	aSignalHandler.ConfigureChannelRequest();
       
  1011 	}
       
  1012 
       
  1013 TInt TL2CAPSigStateOpen::UpdateChannelConfig(CL2CapSAPSignalHandler& aSignalHandler, const TL2CapConfig& aAPIConfig) const
       
  1014 	{
       
  1015 	LOG_FUNC
       
  1016 	TBool reconfigRequired = EFalse;
       
  1017 	TInt rerr = aSignalHandler.ChannelConfig().UpdateConfigAPIChange(aAPIConfig, reconfigRequired);
       
  1018 	if (reconfigRequired && rerr == KErrNone)
       
  1019 		{
       
  1020 		aSignalHandler.ReconfiguringChannel();
       
  1021 		aSignalHandler.SetState(iFactory.GetState(CL2CAPSignalStateFactory::EWaitConfig));
       
  1022 		aSignalHandler.ConfigureChannelRequest();
       
  1023 		rerr = KErrL2CAPConfigPending;
       
  1024 		}
       
  1025 	return rerr;
       
  1026 	}
       
  1027 
       
  1028 
       
  1029 void TL2CAPSigStateOpen::CloseChannelRequest(CL2CapSAPSignalHandler& aSignalHandler) const
       
  1030 	{
       
  1031 	LOG_FUNC
       
  1032 	// Disconnect the channel.
       
  1033 	HandleSAPDisconnect(aSignalHandler);
       
  1034 	}
       
  1035 	
       
  1036 void TL2CAPSigStateOpen::DisconnectRequest(CL2CapSAPSignalHandler& aSignalHandler, TUint8 aId) const
       
  1037 	{
       
  1038 	LOG_FUNC
       
  1039 	// A Disconnect Request has been received and therefore no
       
  1040 	// more data can be sent.  Close the outgoing SDU queue.
       
  1041 	aSignalHandler.SAP()->CloseOutgoingSDUQueue();
       
  1042 
       
  1043 	// Call a common helper function to handle the Disconnect Request.
       
  1044 	HandleDisconnectRequest(aSignalHandler, aId);
       
  1045 	}
       
  1046 
       
  1047 // "Artificial" state events
       
  1048 void TL2CAPSigStateOpen::Enter(CL2CapSAPSignalHandler& aSignalHandler) const
       
  1049 	{
       
  1050 	LOG_FUNC
       
  1051 
       
  1052 	TBool downgrade = EFalse;
       
  1053 	TInt err = aSignalHandler.ChannelConfig().FecNegotiator().CheckNegotiatedChannelMode(downgrade);
       
  1054 	if (err != KErrNone)
       
  1055 		{
       
  1056 		DisconnectWithError(aSignalHandler, err);
       
  1057 		}
       
  1058 	else if (downgrade)
       
  1059    		{
       
  1060 		LOG(_L("CL2CapSigStateOpen::Enter doing reconfig"));
       
  1061 		// now get reconfiguring
       
  1062 		aSignalHandler.SetState(iFactory.GetState(CL2CAPSignalStateFactory::EWaitConfig));
       
  1063 		aSignalHandler.ConfigureChannelRequest();
       
  1064 		}
       
  1065 	else
       
  1066 		{
       
  1067 		// Remove and park override.
       
  1068 		aSignalHandler.UndoOverrideParkMode();
       
  1069 		
       
  1070 		// Cancel the configuration timer.
       
  1071 		aSignalHandler.CancelTimer();
       
  1072 		
       
  1073 		// Inform the SAP that the channel is now configured and ready to use.
       
  1074 		aSignalHandler.SAP()->ChannelConfigured(aSignalHandler.ChannelConfig(),
       
  1075 		                                        aSignalHandler.Mux(),
       
  1076 	    	                                    aSignalHandler.LocalPort(),
       
  1077 	        	                                aSignalHandler.RemotePort());
       
  1078 		}
       
  1079 	}
       
  1080 
       
  1081 TBool TL2CAPSigStateOpen::IsChannelOpen() const
       
  1082 	{
       
  1083 	return ETrue;
       
  1084 	}
       
  1085 
       
  1086 TInt TL2CAPSigStateOpen::GetNegotiatedChannelMode(const CL2CapSAPSignalHandler& aSignalHandler, TL2CapChannelMode& aMode) const
       
  1087 	{
       
  1088 	return aSignalHandler.ChannelConfig().GetChannelMode(aMode);
       
  1089 	}
       
  1090 
       
  1091 /**************************************************************************/
       
  1092 // Implementation of void TL2CAPSigStateWaitDisconnect
       
  1093 //
       
  1094 // Logical link still exists but is waiting to be disconnected. 
       
  1095 // NB Signalling can still be received, and should be handled.
       
  1096 //
       
  1097 TL2CAPSigStateWaitDisconnect::TL2CAPSigStateWaitDisconnect(const CL2CAPSignalStateFactory& aFactory) 
       
  1098 	: TL2CAPSigState(aFactory) 
       
  1099 	{
       
  1100 	LOG_FUNC
       
  1101 	}
       
  1102 
       
  1103 // L2CAP commands received from the peer.
       
  1104 void TL2CAPSigStateWaitDisconnect::DisconnectRequest(CL2CapSAPSignalHandler& aSignalHandler, TUint8 aId) const
       
  1105 	{
       
  1106 	LOG_FUNC
       
  1107 	// We may have a ConfigReq awaiting response. This DisconnectReq may be coming from
       
  1108 	// a State 2 peer that didn't like our ConfigReq and disconnected immediately instead
       
  1109 	// of sending a ConfigRsp. Which means we should flush the ConfigReq so that we don't
       
  1110 	// wait forever. We can delete all commands in WAIT_DISCONNECT anyway:
       
  1111 	// - the only response we really wait for in WAIT_DISCONNECT is the DisconnectRsp,
       
  1112 	//   but since peer has just sent us a DisconnectReq on its own, we know it's closing
       
  1113 	//   anyway and it doesn't matter if it's received our DisconnectReq;
       
  1114 	// - the rest of the commands - Connect, Configure - can be just flushed - they don't
       
  1115 	//   matter once we enter the disconnection path.
       
  1116 	aSignalHandler.FlushAllQueues();
       
  1117 
       
  1118 	// Call a common helper function to handle the Disconnect Request.
       
  1119 	HandleDisconnectRequest(aSignalHandler, aId);
       
  1120 	}
       
  1121 	
       
  1122 void TL2CAPSigStateWaitDisconnect::DisconnectResponse(CL2CapSAPSignalHandler& aSignalHandler) const
       
  1123 	{
       
  1124 	LOG_FUNC
       
  1125 	aSignalHandler.DrainPendingCommands();
       
  1126 	}
       
  1127 
       
  1128 // "Artificial" state events
       
  1129 void TL2CAPSigStateWaitDisconnect::PendingCommandsDrained(CL2CapSAPSignalHandler& aSignalHandler) const
       
  1130 	{
       
  1131 	LOG_FUNC
       
  1132 	// If an error condition has been recorded forward the error 
       
  1133 	// to the SAP.
       
  1134 	if(aSignalHandler.SignalHandlerErrorCode() != KErrNone)
       
  1135 		{
       
  1136 		Error(aSignalHandler, aSignalHandler.SignalHandlerErrorCode(), aSignalHandler.SignalHandlerErrorAction());
       
  1137 		}
       
  1138 	else
       
  1139 		{
       
  1140 		// This will delete the signal handler if the SAP has detached.
       
  1141 		aSignalHandler.SetState(iFactory.GetState(CL2CAPSignalStateFactory::EClosed));
       
  1142 		aSignalHandler.SignalHandlerDisconnectedCanClose();
       
  1143 		}
       
  1144 	}
       
  1145 		
       
  1146 void TL2CAPSigStateWaitDisconnect::Enter(CL2CapSAPSignalHandler& aSignalHandler) const
       
  1147 	{
       
  1148 	LOG_FUNC
       
  1149 	// Override park to allow the disconnect to continue.
       
  1150 	aSignalHandler.OverrideParkMode();
       
  1151 
       
  1152 	// Cancel the configuration timer.
       
  1153 	aSignalHandler.CancelTimer();
       
  1154 	}
       
  1155 
       
  1156 void TL2CAPSigStateWaitDisconnect::CloseChannelRequest(CL2CapSAPSignalHandler& /*aSignalHandler*/) const
       
  1157 	{
       
  1158 	LOG_FUNC
       
  1159 	// Already disconnecting.
       
  1160 	}
       
  1161 
       
  1162 TInt TL2CAPSigStateWaitDisconnect::UpdateChannelConfig(CL2CapSAPSignalHandler& /*aSignalHandler*/, const TL2CapConfig& /*aAPIConfig*/) const
       
  1163 	{
       
  1164 	LOG_FUNC
       
  1165 	// Disconnecting - no point.
       
  1166 	return KErrDisconnected;
       
  1167 	}
       
  1168 
       
  1169 void TL2CAPSigStateWaitDisconnect::ConnectResponse(CL2CapSAPSignalHandler& /*aSignalHandler*/,
       
  1170 								 HConnectionResponse* /*aConnectionResponse*/) const
       
  1171 	{
       
  1172 	LOG_FUNC
       
  1173 	// Could be here if we disconnect between receiving a pending connect response and
       
  1174 	// a final one. 
       
  1175 	}
       
  1176 
       
  1177 void TL2CAPSigStateWaitDisconnect::ConfigRequest(CL2CapSAPSignalHandler& /*aSignalHandler*/,
       
  1178 										  		 HConfigureRequest* /*aConfigRequest*/) const
       
  1179 	{
       
  1180 	LOG_FUNC
       
  1181 	// This often happens under the following scenario:
       
  1182 	// 1. Connection Req/Rsp are exchanged and the local entity sends a Config Req.
       
  1183 	// 2. Local entity sends Config Req first.
       
  1184 	// 3. Remote responds with Config Rsp Unacceptable Parameters which is not acceptable
       
  1185 	//    for us and hence we decide to disconnect, send a DisconnectReq and enter this
       
  1186 	//    state (WAIT_DISCONNECT).
       
  1187 	// 4. Remote legitimately sends ConfigReq before it gets the DisconnectReq and we end
       
  1188 	//    up here.
       
  1189 	//
       
  1190 	// The state tables in the spec say we should respond with a CmdRej Invalid CID but it is
       
  1191 	// unneeded and introduces a bit of unnecessary chaos. Let's ignore this ConfigReq and wait
       
  1192 	// until the remote acknowledges disconnection. If it retransmits the ConfigReq after that,
       
  1193 	// we'll then respond with a CmdRej Invalid CID which will be closer to the truth and won't
       
  1194 	// cause more unnecessary traffic.
       
  1195 	}
       
  1196 
       
  1197 void TL2CAPSigStateWaitDisconnect::ConfigResponse(CL2CapSAPSignalHandler& /*aSignalHandler*/,
       
  1198  	                					   		  HConfigureResponse* /*aConfigResponse*/) const
       
  1199 	{
       
  1200 	LOG_FUNC
       
  1201 	// Could well be in this state - just drop.
       
  1202 	}
       
  1203 
       
  1204 void TL2CAPSigStateWaitDisconnect::ConfigureChannelRequest(CL2CapSAPSignalHandler& /*aSignalHandler*/) const
       
  1205 	{
       
  1206 	LOG_FUNC
       
  1207 	// This may be called if an Information Request times out
       
  1208 	// waiting for an Information Response, and we have since
       
  1209 	// moved into the Wait Disconnect state.  Therefore don't
       
  1210 	// panic, however there is nothing else we can do here.
       
  1211 	}
       
  1212 
       
  1213 	
       
  1214 /**************************************************************************/
       
  1215 // CL2CAPSignalStateFactory implementation
       
  1216 //
       
  1217 // That which creates the states, and manages state change
       
  1218 //
       
  1219 CL2CAPSignalStateFactory::CL2CAPSignalStateFactory()
       
  1220 	{
       
  1221 	LOG_FUNC
       
  1222 	}
       
  1223 
       
  1224 CL2CAPSignalStateFactory::~CL2CAPSignalStateFactory()
       
  1225 	{
       
  1226 	LOG_FUNC
       
  1227 	// Destroy all the state objects
       
  1228 	iSigStates.DeleteAll();
       
  1229 	}
       
  1230 	
       
  1231 CL2CAPSignalStateFactory* CL2CAPSignalStateFactory::NewL()
       
  1232 	{
       
  1233 	LOG_STATIC_FUNC
       
  1234 	CL2CAPSignalStateFactory* factory= new (ELeave) CL2CAPSignalStateFactory();
       
  1235 	CleanupStack::PushL(factory);
       
  1236 	factory->ConstructL();
       
  1237 	CleanupStack::Pop();
       
  1238 	return factory;
       
  1239 	}
       
  1240 	
       
  1241 void CL2CAPSignalStateFactory::ConstructL()
       
  1242 	{
       
  1243 	LOG_FUNC
       
  1244 	// Create all the new states
       
  1245 	iSigStates[EClosed] 			= new (ELeave) TL2CAPSigStateClosed(*this);
       
  1246 	iSigStates[EWaitConnectRsp] 	= new (ELeave) TL2CAPSigStateWaitConnectRsp(*this);
       
  1247 	iSigStates[EWaitConnect] 		= new (ELeave) TL2CAPSigStateWaitConnect(*this);
       
  1248 	iSigStates[EWaitConfig] 		= new (ELeave) TL2CAPSigStateWaitConfig(*this);
       
  1249 	iSigStates[EWaitSendConfig] 	= new (ELeave) TL2CAPSigStateWaitSendConfig(*this);
       
  1250 	iSigStates[EWaitConfigReqRsp]	= new (ELeave) TL2CAPSigStateWaitConfigReqRsp(*this);
       
  1251 	iSigStates[EWaitConfigRsp] 		= new (ELeave) TL2CAPSigStateWaitConfigRsp(*this);
       
  1252 	iSigStates[EWaitConfigReq] 		= new (ELeave) TL2CAPSigStateWaitConfigReq(*this);
       
  1253 	iSigStates[EOpen] 				= new (ELeave) TL2CAPSigStateOpen(*this);
       
  1254 	iSigStates[EWaitDisconnect]		= new (ELeave) TL2CAPSigStateWaitDisconnect(*this);
       
  1255 	
       
  1256 	LOG1(_L("L2CAP SigState Closed = 0x%08x"), iSigStates[EClosed]);
       
  1257 	LOG1(_L("L2CAP SigState WaitConnectRsp = 0x%08x"), iSigStates[EWaitConnectRsp]);
       
  1258 	LOG1(_L("L2CAP SigState WaitConnect = 0x%08x"), iSigStates[EWaitConnect]);
       
  1259 	LOG1(_L("L2CAP SigState WaitConfig = 0x%08x"), iSigStates[EWaitConfig]);
       
  1260 	LOG1(_L("L2CAP SigState WaitSendConfig= 0x%08x"), iSigStates[EWaitSendConfig]);
       
  1261 	LOG1(_L("L2CAP SigState WaitConfigReqRsp = 0x%08x"), iSigStates[EWaitConfigReqRsp]);
       
  1262 	LOG1(_L("L2CAP SigState WaitConfigRsp = 0x%08x"), iSigStates[EWaitConfigRsp]);
       
  1263 	LOG1(_L("L2CAP SigState WaitConfigReq = 0x%08x"), iSigStates[EWaitConfigReq]);
       
  1264 	LOG1(_L("L2CAP SigState Open = 0x%08x"), iSigStates[EOpen]);
       
  1265 	LOG1(_L("L2CAP SigState WaitDisconnect = 0x%08x"), iSigStates[EWaitDisconnect]);
       
  1266 	}	
       
  1267 
       
  1268 TL2CAPSigState& CL2CAPSignalStateFactory::GetState(const TSigStates aState) const
       
  1269 	{
       
  1270 	LOG_FUNC
       
  1271 	__ASSERT_DEBUG(aState != EMaxState, Panic(EL2CAPMuxerStateOutOfBounds));
       
  1272 	return *iSigStates[aState];
       
  1273 	}
       
  1274 	
       
  1275 TInt CL2CAPSignalStateFactory::StateIndex(const TL2CAPSigState* aState) const
       
  1276 	{
       
  1277 	LOG_FUNC
       
  1278 	TInt state;
       
  1279 	for (state = 0; state < EMaxState; state++)
       
  1280 		{
       
  1281 		if (iSigStates[state] == aState)
       
  1282 			{
       
  1283 			return state;
       
  1284 			}
       
  1285 		}
       
  1286 	
       
  1287 	return KL2UnknownState;
       
  1288 	}
       
  1289