bluetooth/btstack/linkmgr/ACLSAP.h
changeset 0 29b1cd4cb562
child 8 2b6718f05bdb
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     1 // Copyright (c) 2003-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 // Defines the ACL SAP class, and ACL State Classes (using State and Flyweight pattern)
       
    15 // 
       
    16 //
       
    17 
       
    18 #ifndef _ACLSAP_H
       
    19 #define _ACLSAP_H
       
    20 
       
    21 #include "debug.h"
       
    22 #include <es_prot.h>
       
    23 #include <es_mbuf.h>
       
    24 #include <bluetooth/aclsockaddr.h>
       
    25 #include "basebandsap.h"
       
    26 #include "notification.h"
       
    27 #include "linkutil.h"
       
    28 
       
    29 class CLinkMgrProtocol;
       
    30 class CPhysicalLink;
       
    31 class CPhysicalLinksManager;
       
    32 
       
    33 class TACLLinkState;
       
    34 class CACLLink;
       
    35 class CACLDataItem;
       
    36 class CACLDataQController;
       
    37 
       
    38 /**
       
    39    Factory for the ACL Link states
       
    40 
       
    41    The states are flyweight classes
       
    42 **/
       
    43 NONSHARABLE_CLASS(CACLLinkStateFactory) : public CBase
       
    44 	{
       
    45 public:
       
    46 	static CACLLinkStateFactory* NewL();
       
    47 	~CACLLinkStateFactory();
       
    48 	enum TACLLinkStates
       
    49 		{
       
    50 		EClosed,
       
    51 		EListening,
       
    52 		EAccepting,
       
    53 		EWaitForLink,
       
    54 		EWaitForStart,
       
    55 		EWaitForStartError,
       
    56 		EWaitForStartParked,
       
    57 		EOpen,	
       
    58 		EOpenParked,
       
    59 		EClosing,
       
    60 	// *** keep next one last ***
       
    61 		EACLLinkMaxState,
       
    62 		};
       
    63 	
       
    64 	TACLLinkState& GetState(TACLLinkStates aState);
       
    65 	TInt StateIndex(const TACLLinkState* aState) const;
       
    66 private:
       
    67 	void ConstructL();
       
    68 	TFixedArray<TACLLinkState*, EACLLinkMaxState> iStates;
       
    69 	};
       
    70 
       
    71 
       
    72 NONSHARABLE_CLASS(TACLLinkState)
       
    73 	{
       
    74 public:
       
    75 	TACLLinkState(CACLLinkStateFactory& aFactory);
       
    76 
       
    77 	virtual void Enter(CACLLink& aContext) const;
       
    78 
       
    79 	virtual void ActiveOpen(CACLLink& aContext) const;
       
    80 	virtual TInt PassiveOpen(CACLLink& aContext, TUint aQueSize) const;
       
    81 	virtual void Shutdown(CACLLink& aContext, CServProviderBase::TCloseType aCloseType) const;
       
    82 	virtual void DataReceived(CACLLink& aContext, TUint8 aFlag, const TDesC8& aData) const;
       
    83 	virtual TUint Write(CACLLink& aContext, const TDesC8& aData, TUint aOptions, TSockAddr* aAddr) const;
       
    84 	virtual void Start(CACLLink& aContext) const;
       
    85 	virtual TBool ConnectRequest(CACLLink& aContext, const TBTConnect& aConnectDetails, const CPhysicalLink& aPhysicalLink) const;
       
    86 	virtual void ConnectComplete(CACLLink& aContext, const TBTConnect& aConnectDetails) const;
       
    87 	virtual void Disconnection(CACLLink& aContext) const;
       
    88 	virtual void Error(CACLLink& aContext, TInt aError) const;
       
    89 
       
    90 	virtual void Timeout(CACLLink& aContext, TBasebandTimeout aTimeout) const;
       
    91 	virtual void Exit(CACLLink& aContext) const;
       
    92 	virtual void Deletion(CACLLink& aContext) const;
       
    93 
       
    94 	virtual TBool IsIdle() const;
       
    95 	virtual void Park(CACLLink& aContext) const;
       
    96 	virtual void Unpark(CACLLink& aContext) const;
       
    97 protected:
       
    98 	// Exits old state, sets the new state, and enters it.
       
    99 	void ChangeState(CACLLink& aContext, CACLLinkStateFactory::TACLLinkStates aState) const;
       
   100 	void PanicInState(TLinkPanic aPanic) const;
       
   101 
       
   102 protected:
       
   103 	CACLLinkStateFactory& iFactory;
       
   104 #ifdef __FLOG_ACTIVE
       
   105 	TBuf<48> iName;
       
   106 #endif
       
   107 	};
       
   108 
       
   109 NONSHARABLE_CLASS(TACLLinkStateListening) : public TACLLinkState
       
   110 	{
       
   111 public:
       
   112 	TACLLinkStateListening(CACLLinkStateFactory& aFactory);
       
   113 
       
   114 	void Enter(CACLLink& aContext) const;
       
   115 
       
   116 	void Shutdown(CACLLink& aContext, CServProviderBase::TCloseType aCloseType) const;
       
   117 	TBool ConnectRequest(CACLLink& aContext, const TBTConnect& aConnect, const CPhysicalLink& aPhysicalLink) const;
       
   118 
       
   119 	void Deletion(CACLLink& aContext) const;
       
   120 
       
   121 	void Exit(CACLLink& aContext) const;
       
   122 	
       
   123 	void Error(CACLLink& aContext, TInt aError) const;
       
   124 private:
       
   125 	void SpawnL(CACLLink& aContext, CPhysicalLink& aPhysicalLink) const;
       
   126 	};
       
   127 
       
   128 NONSHARABLE_CLASS(TACLLinkStateAccepting) : public TACLLinkState
       
   129 	{
       
   130 public:
       
   131 	TACLLinkStateAccepting(CACLLinkStateFactory& aFactory);
       
   132 	void Enter(CACLLink& aContext) const;
       
   133 	void Exit(CACLLink& aContext) const;
       
   134 	void ConnectComplete(CACLLink& aContext, const TBTConnect& aConnect) const;
       
   135 	void Error(CACLLink& aContext, TInt aError) const;
       
   136 	void Disconnection(CACLLink& aContext) const;
       
   137 	void Deletion(CACLLink& aContext) const;
       
   138 	void Timeout(CACLLink& aContext, TBasebandTimeout aTimeout) const;
       
   139 	void Shutdown(CACLLink& aContext, CServProviderBase::TCloseType aCloseType) const;
       
   140 	};
       
   141 
       
   142 NONSHARABLE_CLASS(TACLLinkStateWaitForLink) : public TACLLinkState
       
   143 	{
       
   144 public:
       
   145 	TACLLinkStateWaitForLink(CACLLinkStateFactory& aFactory);
       
   146 
       
   147 	void Shutdown(CACLLink& aContext, CServProviderBase::TCloseType aCloseType) const;
       
   148 	void ConnectComplete(CACLLink& aContext, const TBTConnect& aConnectDetails) const;
       
   149 	void Error(CACLLink& aContext, TInt aError) const;
       
   150 	void Disconnection(CACLLink& aContext) const;
       
   151 	void Exit(CACLLink& aContext) const;
       
   152 	};
       
   153 
       
   154 NONSHARABLE_CLASS(TACLLinkStateWaitForStart) : public TACLLinkState
       
   155 	{
       
   156 public:
       
   157 	TACLLinkStateWaitForStart(CACLLinkStateFactory& aFactory);
       
   158 	void Enter(CACLLink& aContext) const;
       
   159 	void Start(CACLLink& aContext) const;
       
   160 	void Error(CACLLink& aContext, TInt aError) const;
       
   161 	void Deletion(CACLLink& aContext) const;
       
   162 	void Disconnection(CACLLink& aContext) const;
       
   163 	void Park(CACLLink& aContext) const;
       
   164 	void Shutdown(CACLLink& aContext, CServProviderBase::TCloseType aCloseType) const;
       
   165 	};
       
   166 
       
   167 NONSHARABLE_CLASS(TACLLinkStateWaitForStartError) : public TACLLinkState
       
   168 	{
       
   169 public:
       
   170 	TACLLinkStateWaitForStartError(CACLLinkStateFactory& aFactory);
       
   171 	void Start(CACLLink& aContext) const;
       
   172 	void Disconnection(CACLLink& aContext) const;
       
   173 	void Error(CACLLink& aContext, TInt aError) const;
       
   174 	void Deletion(CACLLink& aContext) const;
       
   175 	};
       
   176 
       
   177 NONSHARABLE_CLASS(TACLLinkStateWaitForStartParked) : public TACLLinkStateWaitForStart
       
   178 	{
       
   179 public:
       
   180 	TACLLinkStateWaitForStartParked(CACLLinkStateFactory& aFactory);
       
   181 	void Unpark(CACLLink& aContext) const;
       
   182 	void Start(CACLLink& aContext) const;
       
   183 	};
       
   184 
       
   185 
       
   186 NONSHARABLE_CLASS(TACLLinkStateOpen) : public TACLLinkState
       
   187 	{
       
   188 public:
       
   189 	TACLLinkStateOpen(CACLLinkStateFactory& aFactory);
       
   190 	void Enter(CACLLink& aContext) const;
       
   191 
       
   192 	TUint Write(CACLLink& aContext, const TDesC8& aData, TUint aOptions, TSockAddr* aAddr) const;
       
   193 	void DataReceived(CACLLink& aContext, TUint8 aFlag, const TDesC8& aData) const;	
       
   194 	void Disconnection(CACLLink& aContext) const;
       
   195 	void Shutdown(CACLLink& aContext, CServProviderBase::TCloseType aCloseType) const;
       
   196 	void Park(CACLLink& aContext) const;
       
   197 	void Error(CACLLink& aContext, TInt aError) const;
       
   198 	void Exit(CACLLink& aContext) const;
       
   199 	};
       
   200 
       
   201 NONSHARABLE_CLASS(TACLLinkStateOpenParked) : public TACLLinkStateOpen
       
   202 	{
       
   203 public:
       
   204 	TACLLinkStateOpenParked(CACLLinkStateFactory& aFactory);
       
   205 	void Enter(CACLLink& aContext) const;
       
   206 
       
   207 	TUint Write(CACLLink& aContext, const TDesC8& aData, TUint aOptions, TSockAddr* aAddr) const;
       
   208 	void DataReceived(CACLLink& aContext, TUint8 aFlag, const TDesC8& aData) const;	
       
   209 	void Unpark(CACLLink& aContext) const;
       
   210 
       
   211 	void Exit(CACLLink& aContext) const;
       
   212 	};
       
   213 
       
   214 NONSHARABLE_CLASS(TACLLinkStateClosing) : public TACLLinkState
       
   215 	{
       
   216 public:
       
   217 	TACLLinkStateClosing(CACLLinkStateFactory& aFactory);
       
   218 
       
   219 	void ActiveOpen(CACLLink& aContext) const;
       
   220 	void Shutdown(CACLLink& aContext, CServProviderBase::TCloseType aCloseType) const;
       
   221 	void Disconnection(CACLLink& aContext) const;
       
   222 	void DataReceived(CACLLink& aContext, TUint8 aFlag, const TDesC8& aData) const;
       
   223 	TUint Write(CACLLink& aContext, const TDesC8& aData, TUint aOptions, TSockAddr* aAddr) const;
       
   224 	void Enter(CACLLink& aContext) const;
       
   225 	void Deletion(CACLLink& aContext) const;
       
   226 
       
   227 	TBool IsIdle() const;
       
   228 	
       
   229 	void Error(CACLLink& aContext, TInt aError) const;
       
   230 	};
       
   231 
       
   232 NONSHARABLE_CLASS(TACLLinkStateClosed) : public TACLLinkState
       
   233 	{
       
   234 public:
       
   235 	TACLLinkStateClosed(CACLLinkStateFactory& aFactory);
       
   236 
       
   237 	void Start(CACLLink& aContext) const; // CSockets Start
       
   238 	void Enter(CACLLink& aContext) const;
       
   239 	void ActiveOpen(CACLLink& aContext) const;
       
   240 	TInt PassiveOpen(CACLLink& aContext, TUint aQueSize) const;
       
   241 	void Shutdown(CACLLink& aContext, CServProviderBase::TCloseType aCloseType) const;
       
   242 
       
   243 	void Deletion(CACLLink& aContext) const;
       
   244 	TBool IsIdle() const;
       
   245 	};
       
   246 
       
   247 
       
   248 NONSHARABLE_CLASS(CACLLink) : public CBTBasebandSAP, public MLogicalLink
       
   249 	{
       
   250 friend class TACLLinkState;
       
   251 friend class TACLLinkStateOpen;
       
   252 friend class TACLLinkStateOpenParked;
       
   253 friend class TACLLinkStateListening;
       
   254 friend class TACLLinkStateAccepting;
       
   255 friend class TACLLinkStateWaitForStart;
       
   256 friend class TACLLinkStateWaitForStartParked;
       
   257 friend class TACLLinkStateWaitForLink;
       
   258 friend class TACLLinkStateClosing;
       
   259 friend class TACLLinkStateClosed;
       
   260 public:
       
   261 	static CACLLink* NewL(CPhysicalLinksManager& aLinksMan,
       
   262 						  CPhysicalLink* aConnection,
       
   263 						  CLinkMgrProtocol& aProtocol);
       
   264 	static CACLLink* NewLC(CPhysicalLinksManager& aLinksMan,
       
   265 						   CPhysicalLink* aConnection,
       
   266 						   CLinkMgrProtocol& aProtocol);
       
   267 	~CACLLink();
       
   268 	// from SAP
       
   269 	virtual void Start();
       
   270 	virtual void AutoBind();
       
   271 	virtual void LocalName(TSockAddr& anAddr) const ;
       
   272 	virtual TInt SetLocalName(TSockAddr& anAddr);
       
   273 	virtual void RemName(TSockAddr& anAddr) const ;
       
   274 	virtual TInt SetRemName(TSockAddr& anAddr);
       
   275 	virtual TInt GetOption(TUint level,TUint name,TDes8& anOption)const ;
       
   276 	virtual void Ioctl(TUint level,TUint name,TDes8* anOption);
       
   277 	virtual void CancelIoctl(TUint aLevel,TUint aName);
       
   278 	virtual TInt SAPSetOption(TUint level,TUint name,const TDesC8 &anOption);
       
   279 
       
   280 	virtual void ActiveOpen();
       
   281 	virtual TInt PassiveOpen(TUint aQueSize);
       
   282 	virtual void Shutdown(TCloseType option);
       
   283 	virtual void Shutdown(TCloseType option,const TDesC8& aDisconnectionData);
       
   284 
       
   285 
       
   286 	TUint Write(const TDesC8& aDesc,TUint aOptions, TSockAddr* aAddr);
       
   287 
       
   288 	void GetData(TDes8& aDesc,TUint aOptions,TSockAddr* anAddr=NULL);
       
   289 	TInt GetData(RMBufChain& aData, TUint aLength, TUint aOptions, TSockAddr* aAddr=NULL);
       
   290 
       
   291 	void Timeout(TBasebandTimeout aTimeout);
       
   292 
       
   293 	TBool IsIdle() const;
       
   294 	TBool IsLocallyInitiated() const;
       
   295 
       
   296 	void ACLDataReceived(THCIConnHandle aConnH, TUint8 aFlag, const TDesC8& aData);
       
   297 
       
   298 // from MLogicalLink
       
   299 	virtual TBool ConnectRequest(const TBTConnect& aConnect, const CPhysicalLink& aPhysicalLink);
       
   300 	virtual void ConnectComplete(const TBTConnect& aConnect);
       
   301 	virtual void Disconnection();
       
   302 	virtual void Error(TInt aError);
       
   303 	virtual void DataReceived(THCIConnHandle aConnH, TUint8 aFlag, const TDesC8& aData);
       
   304 	
       
   305 	void PacketsSent(THCIConnHandle aConnH, TUint16 aNumPackets);
       
   306 	void PhysicalLinkChange(const TBTBasebandEventNotification& aEvent, CPhysicalLink& aPhysicalLink);
       
   307 	void TryToSend();
       
   308 	
       
   309 private:
       
   310 	CACLLink(CPhysicalLinksManager& aManager,
       
   311 			 CPhysicalLink* aConnection,
       
   312 			 CLinkMgrProtocol& aProtocol);
       
   313 	void ConstructL();
       
   314 	void NotifyDataToSocket(TUint8 aFlag, const TDesC8& aData);
       
   315 	void CloseLogicalLink();
       
   316 	TInt OpenLogicalLink();
       
   317 	void ParkLogicalLink();
       
   318 	void UnparkLogicalLink();
       
   319 	void BindAndConnectPhysicalLinkL();
       
   320 	void ClearPhysicalLink();
       
   321 	TInt DoACLGetOption(TUint aName,TDes8& aOption) const;
       
   322 	TInt DoACLSetOption(TUint aName, const TDesC8& aOption);
       
   323 	TUint DoWrite(CACLDataItem& aFrame, CACLDataQController& aQctrl,
       
   324 		   	   const TDesC8& aData, TUint8 aOptions);
       
   325 	TBool CanHandleFlags(TUint8 aFlag) const;
       
   326 	
       
   327 	TInt ProcessFlush();
       
   328 	
       
   329 private: // from MAccessRequestResponseHandler
       
   330 	void AccessRequestComplete(TInt aResult);
       
   331 	
       
   332 private:
       
   333 	enum TInitiationState
       
   334 		{
       
   335 		EInvalidInitiationState	= 0x00,
       
   336 		ELocallyInitiated		= 0x01,
       
   337 		ERemotelyInitiated		= 0x02,
       
   338 		};
       
   339 	void SetInitiationState(TInitiationState aState);
       
   340 	void ClearInitiationState();
       
   341 	
       
   342 private:
       
   343 	CLinkMgrProtocol&		iProtocol;
       
   344 	TACLLinkState*			iState;
       
   345 
       
   346 	RMBufPktQ				iInboundBuffer;
       
   347 
       
   348 	TACLPort			iLocalPort;		// based on PB flags
       
   349 	TBool				iSocketBlocked;
       
   350 
       
   351 	TInitiationState	iInitiationState; // simpler than modelling in state machine.
       
   352 	};
       
   353 
       
   354 #ifdef __FLOG_ACTIVE
       
   355 #define STATENAME(x)  iName=_L(x)
       
   356 #else
       
   357 #define STATENAME(x)
       
   358 #endif
       
   359 
       
   360 #endif // _ACLSAP_H