bluetooth/btstack/l2cap/l2capSigStates.h
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 // Implements the signalling state machine for an L2Cap Channel
       
    15 // 
       
    16 //
       
    17 
       
    18 #ifndef L2CAPSIGSTATES_H
       
    19 #define L2CAPSIGSTATES_H
       
    20 
       
    21 
       
    22 #include <e32base.h>
       
    23 #include <bt_sock.h>
       
    24 #include <es_prot.h>
       
    25 
       
    26 #include "L2CapChannelConfig.h"
       
    27 
       
    28 class CL2CapSAPSignalHandler;
       
    29 class CL2CAPSignalStateFactory;
       
    30 class CL2CAPConnectionSAP;
       
    31 class TL2CAPSigState;
       
    32 
       
    33 class HConnectionResponse;
       
    34 class HConfigureRequest;
       
    35 class HConfigureResponse;
       
    36 
       
    37 
       
    38 // Factory class for the L2CAP state machine.
       
    39 NONSHARABLE_CLASS(CL2CAPSignalStateFactory) : public CBase
       
    40 	{
       
    41 public:
       
    42 	enum TSigStates
       
    43 		{
       
    44 		EClosed,
       
    45 		EWaitConnectRsp,
       
    46 		EWaitConnect,
       
    47 		EWaitConfig,
       
    48 		EWaitSendConfig,
       
    49 		EWaitConfigReqRsp,
       
    50 		EWaitConfigRsp,
       
    51 		EWaitConfigReq,
       
    52 		EOpen,
       
    53 		EWaitDisconnect,
       
    54 		EMaxState,
       
    55 		};
       
    56 
       
    57 	TL2CAPSigState& GetState(const TSigStates aState) const;
       
    58 	TInt StateIndex(const TL2CAPSigState* aState) const;
       
    59 	
       
    60 	static CL2CAPSignalStateFactory* NewL();
       
    61 	~CL2CAPSignalStateFactory();
       
    62 
       
    63 private:
       
    64 	CL2CAPSignalStateFactory();
       
    65 	void ConstructL();
       
    66 
       
    67 private:	
       
    68 	TFixedArray<TL2CAPSigState*, EMaxState> iSigStates;
       
    69 	};
       
    70 
       
    71 
       
    72 // Base class for the L2CAP state machine.
       
    73 NONSHARABLE_CLASS(TL2CAPSigState)
       
    74 	{
       
    75 public:
       
    76 	friend class CL2CapSAPSignalHandler;
       
    77 
       
    78 	TL2CAPSigState(const CL2CAPSignalStateFactory& aFactory);
       
    79 	
       
    80 	// Events from the SAP
       
    81 	virtual void CloseChannelRequest(CL2CapSAPSignalHandler& aSignalHandler) const;
       
    82 	virtual void OpenChannelRequest(CL2CapSAPSignalHandler& aSignalHandler) const;
       
    83 	virtual void ConnectRequestReceived(CL2CapSAPSignalHandler& aSignalHandler) const;
       
    84 	virtual void ConfigureChannelRequest(CL2CapSAPSignalHandler& aSignalHandler) const;
       
    85 	virtual TInt UpdateChannelConfig(CL2CapSAPSignalHandler& aSignalHandler, const TL2CapConfig& aAPIConfig) const;
       
    86 
       
    87 	virtual TInt GetNegotiatedChannelMode(const CL2CapSAPSignalHandler& aSignalHandler, TL2CapChannelMode& aMode) const;
       
    88 
       
    89 	// L2CAP commands received from the peer.
       
    90 	virtual void ConnectResponse(CL2CapSAPSignalHandler& aSignalHandler,
       
    91 								 HConnectionResponse* aConnectionResponse) const;
       
    92 	
       
    93 	virtual void ConfigRequest(CL2CapSAPSignalHandler& aSignalHandler,
       
    94 							   HConfigureRequest* aConfigRequest) const;
       
    95 	virtual void ConfigResponse(CL2CapSAPSignalHandler& aSignalHandler,
       
    96  		                        HConfigureResponse* aConfigResponse) const;
       
    97 	virtual void DisconnectRequest(CL2CapSAPSignalHandler& aSignalHandler, TUint8 aId) const;
       
    98 	virtual void DisconnectResponse(CL2CapSAPSignalHandler& aSignalHandler) const;
       
    99 
       
   100 	// Events from the Mux
       
   101 	virtual void Error(CL2CapSAPSignalHandler& aSignalHandler,
       
   102 	                   TInt aErrorCode, 
       
   103 	                   MSocketNotify::TOperationBitmasks aErrorAction) const;
       
   104 
       
   105 	// Change of state events
       
   106 	virtual void Enter(CL2CapSAPSignalHandler& aSignalHandler) const;
       
   107 	virtual void Exit(CL2CapSAPSignalHandler& aSignalHandler) const;
       
   108 	
       
   109 	// Signal command queue event
       
   110 	virtual void PendingCommandsDrained(CL2CapSAPSignalHandler& aSignalHandler) const;
       
   111 
       
   112 	// Timers
       
   113 	virtual void ConfigurationTimerExpiry(CL2CapSAPSignalHandler& aSignalHandler) const;
       
   114 
       
   115 	// Helpers
       
   116 	virtual TBool IsChannelClosed() const;
       
   117 	virtual TBool IsChannelOpen() const;
       
   118 	void HandleDisconnectRequest(CL2CapSAPSignalHandler& aSignalHandler, TUint8 aId) const;
       
   119 	void HandleSAPDisconnect(CL2CapSAPSignalHandler& aSignalHandler) const;
       
   120 	TInt SendConfigRequest(CL2CapSAPSignalHandler& aSignalHandler) const;
       
   121 	TInt SendConfigResponse(CL2CapSAPSignalHandler& aSignalHandler, TConfigResponseResult aResult) const;
       
   122 	void DisconnectWithError(CL2CapSAPSignalHandler& aSignalHandler, TInt aError) const;
       
   123 
       
   124 protected:
       
   125 	void PanicInState(TL2CAPPanic aPanic) const;
       
   126 
       
   127 protected:
       
   128 	const CL2CAPSignalStateFactory& iFactory;  // Get states
       
   129 	};
       
   130 
       
   131 
       
   132 /**
       
   133    The closed state.
       
   134 
       
   135    This is the state that newly created signal handlers start in, and the state
       
   136    they end in after a communication has closed down or a connection
       
   137    failed.
       
   138 */
       
   139 NONSHARABLE_CLASS(TL2CAPSigStateClosed) : public TL2CAPSigState
       
   140 	{
       
   141 public:
       
   142 	TL2CAPSigStateClosed(const CL2CAPSignalStateFactory& aFactory);
       
   143 
       
   144 	// Events from the SAP
       
   145 	void OpenChannelRequest(CL2CapSAPSignalHandler& aSignalHandler) const;
       
   146 	void ConnectRequestReceived(CL2CapSAPSignalHandler& aSignalHandler) const;
       
   147 	void CloseChannelRequest(CL2CapSAPSignalHandler& aSignalHandler) const;
       
   148 
       
   149 	void ConfigRequest(CL2CapSAPSignalHandler& aSignalHandler,
       
   150 					   HConfigureRequest* aConfigRequest) const;
       
   151 
       
   152 	// Change of state state events
       
   153 	void Enter(CL2CapSAPSignalHandler& aSignalHandler) const;
       
   154 
       
   155 	// Helpers
       
   156 	TBool IsChannelClosed() const;
       
   157 	};
       
   158 
       
   159 NONSHARABLE_CLASS(TL2CAPSigStateWaitConnectRsp) : public TL2CAPSigState
       
   160 /*
       
   161    Waiting for a response to our ConnectRequest.
       
   162 */
       
   163 	{
       
   164 public:
       
   165 	TL2CAPSigStateWaitConnectRsp(const CL2CAPSignalStateFactory& aFactory);
       
   166 
       
   167 	// Events from the SAP
       
   168 	void CloseChannelRequest(CL2CapSAPSignalHandler& aSignalHandler) const;
       
   169 
       
   170 	// L2CAP commands received from the peer.
       
   171 	void ConnectResponse(CL2CapSAPSignalHandler& aSignalHandler,
       
   172 						 HConnectionResponse* aConnectionResponse) const;
       
   173 	void ConfigRequest(CL2CapSAPSignalHandler& aSignalHandler,
       
   174 						 HConfigureRequest* aConfigRequest) const;				 
       
   175 	
       
   176 	// Change of state state events
       
   177 	void Enter(CL2CapSAPSignalHandler& aSignalHandler) const;
       
   178 	};
       
   179 
       
   180 NONSHARABLE_CLASS(TL2CAPSigStateWaitConnect) : public TL2CAPSigState
       
   181  	{
       
   182 public:
       
   183 	TL2CAPSigStateWaitConnect(const CL2CAPSignalStateFactory& aFactory);	
       
   184 
       
   185 	// Events from the SAP
       
   186 	void OpenChannelRequest(CL2CapSAPSignalHandler& aSignalHandler) const;
       
   187 	void CloseChannelRequest(CL2CapSAPSignalHandler& aSignalHandler) const;
       
   188 	
       
   189 	// L2CAP commands received from the peer.
       
   190 	void DisconnectRequest(CL2CapSAPSignalHandler& aSignalHandler, TUint8 aId) const;
       
   191 
       
   192 	// Change of state state events
       
   193 	void PendingCommandsDrained(CL2CapSAPSignalHandler& aSignalHandler) const;
       
   194 	
       
   195 	};
       
   196 	
       
   197 NONSHARABLE_CLASS(TL2CAPSigStateConfigBase) : public TL2CAPSigState
       
   198 /*
       
   199 	This is the base class for the config states. It will NEVER be instantiated itself.
       
   200 */
       
   201 	{
       
   202 public:
       
   203 	using TL2CAPSigState::ConfigRequest;
       
   204 	using TL2CAPSigState::ConfigResponse;
       
   205 	TL2CAPSigStateConfigBase(const CL2CAPSignalStateFactory& aFactory);	
       
   206 
       
   207 	// L2CAP commands received from the peer.
       
   208 	void DisconnectRequest(CL2CapSAPSignalHandler& aSignalHandler, TUint8 aId) const;
       
   209 
       
   210 	void Error(CL2CapSAPSignalHandler& aSignalHandler,
       
   211 	           TInt aErrorCode, 
       
   212 	           MSocketNotify::TOperationBitmasks aErrorAction) const;
       
   213 
       
   214 	// Events from the SAP
       
   215 	void CloseChannelRequest(CL2CapSAPSignalHandler& aSignalHandler) const;
       
   216 	TInt UpdateChannelConfig(CL2CapSAPSignalHandler& aSignalHandler, const TL2CapConfig& aAPIConfig) const;
       
   217 
       
   218 	// Timer Events
       
   219 	void ConfigurationTimerExpiry(CL2CapSAPSignalHandler& aSignalHandler) const;
       
   220 
       
   221 	// Common handling of Config Request and Response commands.
       
   222 	virtual void ConfigRequest(CL2CapSAPSignalHandler& aSignalHandler,
       
   223 					   HConfigureRequest* aConfigRequest,
       
   224 					   CL2CAPSignalStateFactory::TSigStates aConfigSuccessState) const;
       
   225 	virtual void ConfigResponse(CL2CapSAPSignalHandler& aSignalHandler,
       
   226  		                HConfigureResponse* aConfigResponse,
       
   227  		                CL2CAPSignalStateFactory::TSigStates aConfigSuccessState) const;
       
   228 	};
       
   229 
       
   230 	
       
   231 NONSHARABLE_CLASS(TL2CAPSigStateWaitConfig) : public TL2CAPSigStateConfigBase
       
   232 /*
       
   233 	No config requests/responses sent or received.
       
   234 	Waiting to receive peer L2Cap supported features.
       
   235 */
       
   236 	{
       
   237 public:
       
   238 	TL2CAPSigStateWaitConfig(const CL2CAPSignalStateFactory& aFactory);	
       
   239 
       
   240 	// Events from the SAP
       
   241 	void ConfigureChannelRequest(CL2CapSAPSignalHandler& aSignalHandler) const;
       
   242 
       
   243 	// L2CAP commands received from the peer.
       
   244 	void ConfigRequest(CL2CapSAPSignalHandler& aSignalHandler,
       
   245 					   HConfigureRequest* aConfigRequest) const;
       
   246 
       
   247 	// Change of state state events
       
   248 	void Enter(CL2CapSAPSignalHandler& aSignalHandler) const;
       
   249 	};
       
   250 
       
   251 NONSHARABLE_CLASS(TL2CAPSigStateWaitSendConfig) : public TL2CAPSigStateConfigBase
       
   252 /*
       
   253 	Remote's config request received and responded to with 'success'.
       
   254 	Waiting to send outgoing config request...
       
   255 	...waiting to receive peer L2Cap supported features.
       
   256 */
       
   257 	{
       
   258 public:
       
   259 	TL2CAPSigStateWaitSendConfig(const CL2CAPSignalStateFactory& aFactory);	
       
   260 
       
   261 	// Events from the SAP
       
   262 	void ConfigureChannelRequest(CL2CapSAPSignalHandler& aSignalHandler) const;
       
   263 	};
       
   264 
       
   265 NONSHARABLE_CLASS(TL2CAPSigStateWaitConfigReqRsp) : public TL2CAPSigStateConfigBase
       
   266 /*
       
   267 	Outgoing config request sent.
       
   268 	Waiting for remote's config request, and remote's config response.
       
   269 */
       
   270 	{
       
   271 public:
       
   272 	TL2CAPSigStateWaitConfigReqRsp(const CL2CAPSignalStateFactory& aFactory);	
       
   273 
       
   274 	// L2CAP commands received from the peer.
       
   275 	void ConfigRequest(CL2CapSAPSignalHandler& aSignalHandler,
       
   276 					   HConfigureRequest* aConfigRequest) const;
       
   277 	void ConfigResponse(CL2CapSAPSignalHandler& aSignalHandler,
       
   278  	                    HConfigureResponse* aConfigResponse) const;
       
   279 	};
       
   280 
       
   281 NONSHARABLE_CLASS(TL2CAPSigStateWaitConfigRsp) : public TL2CAPSigStateConfigBase
       
   282 /*
       
   283 	Remote's config request received and responded to with 'success'.
       
   284 	Outgoing config request sent.
       
   285 	Waiting for remote's config response.
       
   286 */
       
   287 	{
       
   288 public:
       
   289 	TL2CAPSigStateWaitConfigRsp(const CL2CAPSignalStateFactory& aFactory);	
       
   290 
       
   291 	// L2CAP commands received from the peer.
       
   292 	void ConfigResponse(CL2CapSAPSignalHandler& aSignalHandler,
       
   293  	                    HConfigureResponse* aConfigResponse) const;
       
   294 	};
       
   295 
       
   296 NONSHARABLE_CLASS(TL2CAPSigStateWaitConfigReq) : public TL2CAPSigStateConfigBase
       
   297 /*
       
   298 	Outgoing config request sent, and responded to with 'success'.
       
   299 	Waiting for remote's config request.
       
   300 */
       
   301 	{
       
   302 public:
       
   303 	TL2CAPSigStateWaitConfigReq(const CL2CAPSignalStateFactory& aFactory);	
       
   304 
       
   305 	// L2CAP commands received from the peer.
       
   306 	void ConfigRequest(CL2CapSAPSignalHandler& aSignalHandler,
       
   307 					   HConfigureRequest* aConfigRequest) const;
       
   308 	};
       
   309 	
       
   310 
       
   311 NONSHARABLE_CLASS(TL2CAPSigStateOpen) : public TL2CAPSigState
       
   312 /*
       
   313 	L2Cap logical link open. Data can be sent and received.
       
   314 */
       
   315 	{
       
   316 public:
       
   317 	TL2CAPSigStateOpen(const CL2CAPSignalStateFactory& aFactory);	
       
   318 
       
   319 	// Events from the SAP
       
   320 	void CloseChannelRequest(CL2CapSAPSignalHandler& aSignalHandler) const;
       
   321 
       
   322 	// L2CAP commands received from the peer.
       
   323 	void DisconnectRequest(CL2CapSAPSignalHandler& aSignalHandler, TUint8 aId) const;
       
   324 	void ConfigRequest(CL2CapSAPSignalHandler& aSignalHandler,HConfigureRequest* aConfigRequest) const; 
       
   325 
       
   326 	// Config state events
       
   327 	TInt UpdateChannelConfig(CL2CapSAPSignalHandler& aSignalHandler, const TL2CapConfig& aAPIConfig) const;
       
   328 
       
   329 	// Change of state state events
       
   330 	void Enter(CL2CapSAPSignalHandler& aSignalHandler) const;
       
   331 
       
   332 	TBool IsChannelOpen() const;
       
   333 	TInt GetNegotiatedChannelMode(const CL2CapSAPSignalHandler& aSignalHandler, TL2CapChannelMode& aMode) const;
       
   334 	};
       
   335 
       
   336 NONSHARABLE_CLASS(TL2CAPSigStateWaitDisconnect) : public TL2CAPSigState
       
   337 /*
       
   338 	Logical link still exists but is waiting to be disconnected. 
       
   339 	NB Signalling can still be received, and should be handled.
       
   340 */
       
   341 	{
       
   342 public:
       
   343 	TL2CAPSigStateWaitDisconnect(const CL2CAPSignalStateFactory& aFactory);	
       
   344 
       
   345 	// Events from the SAP
       
   346 	void CloseChannelRequest(CL2CapSAPSignalHandler& aSignalHandler) const;
       
   347 	TInt UpdateChannelConfig(CL2CapSAPSignalHandler& aSignalHandler, const TL2CapConfig& aAPIConfig) const;
       
   348 
       
   349 	// State changes from the Mux
       
   350 	void DisconnectRequest(CL2CapSAPSignalHandler& aSignalHandler, TUint8 aId) const;
       
   351 	void DisconnectResponse(CL2CapSAPSignalHandler& aSignalHandler) const;
       
   352 
       
   353 	// Change of state state events
       
   354 	void Enter(CL2CapSAPSignalHandler& aSignalHandler) const;
       
   355 	void PendingCommandsDrained(CL2CapSAPSignalHandler& aSignalHandler) const;
       
   356 	void ConnectResponse(CL2CapSAPSignalHandler& aSignalHandler,
       
   357 					   HConnectionResponse* aConnectionResponse) const;
       
   358 	void ConfigRequest(CL2CapSAPSignalHandler& aSignalHandler,
       
   359 					   HConfigureRequest* aConfigRequest) const;
       
   360 
       
   361 	void ConfigResponse(CL2CapSAPSignalHandler& /*aSignalHandler*/,
       
   362  	                	HConfigureResponse* /*aConfigResponse*/) const;
       
   363 	void ConfigureChannelRequest(CL2CapSAPSignalHandler& /*aSignalHandler*/) const;
       
   364 	};
       
   365 
       
   366 #endif