bluetooth/btstack/linkmgr/SCOSAP.h
changeset 0 29b1cd4cb562
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 SCO SAP class, and SCO State Classes (using State and Flyweight pattern)
       
    15 // 
       
    16 //
       
    17 
       
    18 #ifndef _SCOSAP_H_
       
    19 #define _SCOSAP_H_
       
    20 
       
    21 #include "debug.h"
       
    22 #include "basebandsap.h"
       
    23 #include "notification.h"
       
    24 #include "Basebandmodel.h"
       
    25 #include "internaltypes.h"
       
    26 #include "linkutil.h"
       
    27 
       
    28 class CPhysicalLink;
       
    29 class CHctlSynchronousDataFrame;
       
    30 
       
    31 static const TInt KInboundSCODataSize  = 255;
       
    32 static const TInt KMinESCOPacketSize = 1;
       
    33 static const TInt KMinESCOBandwidth = 1;
       
    34 static const TInt KMilliSecondsFactor = 1000;
       
    35 static const TInt KBasebandSlotsPerSecond = 1600;
       
    36 
       
    37 static const TInt KMaxEV3Size = 30;
       
    38 static const TInt KMaxEV4Size = 120;
       
    39 static const TInt KMaxEV5Size = 180;
       
    40 
       
    41 static const TInt KEV3Slots = 1;
       
    42 static const TInt KEV4Slots = 3;
       
    43 static const TInt KEV5Slots = 3;
       
    44 
       
    45 
       
    46 class TSyncLinkState;
       
    47 
       
    48 NONSHARABLE_CLASS(CBTSynchronousLink) : public CBTBasebandSAP, public MLogicalLink
       
    49 	{
       
    50 	friend class TSyncLinkState;
       
    51 	friend class TSCOLinkStateClosed;
       
    52 	friend class TSCOLinkStateWaitForSCO;
       
    53 	friend class TSCOLinkStateListening;
       
    54 	friend class TSCOLinkStateAccepting;
       
    55 	friend class TSCOLinkStateWaitForStart;
       
    56 	friend class TSCOLinkStateWaitForStartError;
       
    57 	friend class TSCOLinkStateOpen;
       
    58 	friend class TSCOLinkStateClosing;
       
    59 	
       
    60 	public:
       
    61 		~CBTSynchronousLink();
       
    62 
       
    63 		virtual void Start();
       
    64 		virtual void ActiveOpen();
       
    65 		virtual TInt PassiveOpen(TUint aQueSize);
       
    66 		virtual void Shutdown(TCloseType option);
       
    67 		virtual void Shutdown(TCloseType aClose, const TDesC8& /*aDisconnectionData*/);
       
    68 		virtual void AutoBind();
       
    69 		virtual TUint Write(const TDesC8& aDesc,TUint aOptions, TSockAddr* aAddr);
       
    70 		virtual void GetData(TDes8& aDesc,TUint options,TSockAddr* aAddr=NULL);
       
    71 		virtual TInt SetLocalName(TSockAddr& aAddr);
       
    72 		virtual TInt SetRemName(TSockAddr& aAddr);
       
    73 		
       
    74 		virtual TInt SAPSetOption(TUint level, TUint name, const TDesC8& aOption) =0;
       
    75 		virtual TInt GetOption(TUint level, TUint name, TDes8& aOption) const =0;
       
    76 		
       
    77 		// Pure virtuals from base classes
       
    78 		virtual void Timeout(TBasebandTimeout aTimeout);
       
    79 		virtual void PacketsSent(THCIConnHandle /*aHandle*/, TUint16 /*aNumPackets*/);
       
    80 
       
    81 		void PhysicalLinkChange(const TBTBasebandEventNotification& aEvent, CPhysicalLink& aPhysicalLink);
       
    82 
       
    83 
       
    84 		virtual TBool ConnectRequest(const TBTConnect& aConnect, const CPhysicalLink& aPhysicalLink);
       
    85 		virtual void ConnectComplete(const TBTConnect& aSCOLink);
       
    86 		virtual void SyncConnectComplete(const TBTConnect& aSCOLink, const TBTSyncConnectOpts& aSyncOpts);
       
    87 		virtual void Disconnection();
       
    88 		virtual void Error(TInt aErr);
       
    89 		virtual void DataReceived(THCIConnHandle aConnH, TUint8 aIgnored, const TDesC8& aData);
       
    90 		virtual TBool IsIdle() const;
       
    91 		virtual TBool IsOpen() const;
       
    92 	
       
    93 		TInt CommonShutdown();
       
    94 		void DoActiveOpenL();
       
    95 		void ReleaseAndUnbind();
       
    96 		
       
    97 		virtual TUint DoWrite(const TDesC8& aData, TUint aOptions, TSockAddr* aAddr) =0;
       
    98 		virtual TDes8& InboundFrame() =0;
       
    99 		virtual TBool IsLinkProbablyPossible() =0;
       
   100 		virtual TInt MakeConnection(CPhysicalLink* aPhysLink) =0;
       
   101 		virtual void SetExtOptions(TBTSyncConnectOpts aSyncOpts) =0;
       
   102 		
       
   103 		inline const TLinkType& LinkType() { return iLinkType; };
       
   104 		
       
   105 		void ParentClosing();
       
   106 		TBool ConnectPending() const;
       
   107 	
       
   108 	protected:
       
   109 		CBTSynchronousLink(CPhysicalLinksManager& aLinksMan, CPhysicalLink* aPhysicalLink, TLinkType aLinkType);
       
   110 		void ConstructL();
       
   111 
       
   112 
       
   113 	protected:
       
   114 		TSyncLinkState*	iState;
       
   115 		TBool			iClosePending;
       
   116 		TLinkType		iLinkType;
       
   117 	};
       
   118 
       
   119 
       
   120 NONSHARABLE_CLASS(CSCOLink) : public CBTSynchronousLink
       
   121 /**
       
   122 	Provides a representation of the SCO link(s)
       
   123 
       
   124 	This are created when the (linkmgr) protocol is asked
       
   125 	for a SAP with KBTSCOLink type.
       
   126 
       
   127 
       
   128 **/
       
   129 	{
       
   130 public:
       
   131 	static CSCOLink* NewLC(CPhysicalLinksManager& aConnectionMan, CPhysicalLink* aConnection);
       
   132 	static CSCOLink* NewL(CPhysicalLinksManager& aConnectionMan, CPhysicalLink* aConnection);
       
   133 	~CSCOLink();
       
   134 	// from CServProviderBase
       
   135 	virtual TInt SAPSetOption(TUint aLevel, TUint aName, const TDesC8& aOption);
       
   136 	virtual TInt GetOption(TUint level, TUint name, TDes8& aOption) const;
       
   137 	
       
   138 // events for the SCO Link (not PHY)
       
   139 	void PacketTypeChange(THCIErrorCode aErr, THCIConnHandle aConnH, TUint16 aNewPacket);
       
   140 
       
   141 	TDes8& InboundFrame();
       
   142 	TBool IsLinkProbablyPossible();
       
   143 	TInt MakeConnection(CPhysicalLink* aPhysLink);	
       
   144 	
       
   145 private:
       
   146 	CSCOLink(CPhysicalLinksManager& aConnectionMan, CPhysicalLink* aConnection);
       
   147 	void ConstructL();
       
   148 	TUint DoWrite(const TDesC8& aData, TUint aOptions, TSockAddr* aAddr);
       
   149 	void SetExtOptions(TBTSyncConnectOpts aSyncOpts);
       
   150 
       
   151 
       
   152 private:
       
   153 	CHctlSynchronousDataFrame*	iOutboundSCOFrame;
       
   154 	TBuf8<KInboundSCODataSize>	iInboundSCOFrame;
       
   155 	TBTSyncPacketTypes			iUserHVPacketTypes;
       
   156 	};
       
   157 
       
   158 
       
   159 NONSHARABLE_CLASS(CeSCOLink) : public CBTSynchronousLink
       
   160 /**
       
   161 	Provides a representation of the eSCO link(s)
       
   162 
       
   163 	This are created when the (linkmgr) protocol is asked
       
   164 	for a SAP with KBTeSCOLink type.
       
   165 **/
       
   166 	{
       
   167 public:
       
   168 	static CeSCOLink* NewLC(CPhysicalLinksManager& aConnectionMan, CPhysicalLink* aConnection);
       
   169 	static CeSCOLink* NewL(CPhysicalLinksManager& aConnectionMan, CPhysicalLink* aConnection);
       
   170 	~CeSCOLink();
       
   171 	// from CServProviderBase
       
   172 	virtual TInt SAPSetOption(TUint aLevel, TUint aName, const TDesC8& aOption);
       
   173 	virtual TInt GetOption(TUint level, TUint name, TDes8& aOption) const;
       
   174 	
       
   175 // events for the SCO Link (not PHY)
       
   176 	void PacketTypeChange(THCIErrorCode aErr, THCIConnHandle aConnH, TUint16 aNewPacket);
       
   177 
       
   178 	TDes8& InboundFrame();
       
   179 	TBool IsLinkProbablyPossible();
       
   180 	TInt MakeConnection(CPhysicalLink* aPhysLink);	
       
   181 	void SetExtOptions(TBTSyncConnectOpts aSyncOpts);
       
   182 	
       
   183 	TUint16 GetPacketMask() const;
       
   184 	void GetExtOptions(TBTeSCOLinkParams& aOptions) const;
       
   185 
       
   186 private:
       
   187 	CeSCOLink(CPhysicalLinksManager& aConnectionMan, CPhysicalLink* aConnection);
       
   188 	void ConstructL();
       
   189 	TUint DoWrite(const TDesC8& aData, TUint aOptions, TSockAddr* aAddr);
       
   190 
       
   191 	TUint MinBandwidth();
       
   192 	TUint MaxBandwidth();
       
   193 
       
   194 private:
       
   195 	CHctlSynchronousDataFrame*	iOutboundSCOFrame;
       
   196 	TBuf8<KInboundSCODataSize>	iInboundSCOFrame;
       
   197 	TBTSyncPacketTypes			iUserPacketTypes;
       
   198 	TUint						iTransmitBandwidth;
       
   199 	TUint						iReceiveBandwidth;
       
   200 	TUint16						iMaxLatency;
       
   201 	TUint16						iVoiceSettings;
       
   202 	TUint8						iRetransmissionEffort;
       
   203 	};
       
   204 
       
   205 
       
   206 
       
   207 //--------------------------------------------------
       
   208 // STATE FACTORY
       
   209 //--------------------------------------------------
       
   210 
       
   211 /**
       
   212    Factory for the ACL Link states
       
   213 
       
   214    The states are flyweight classes
       
   215 **/
       
   216 NONSHARABLE_CLASS(CSyncLinkStateFactory) : public CBase
       
   217 	{
       
   218 public:
       
   219 	static CSyncLinkStateFactory* NewL();
       
   220 	~CSyncLinkStateFactory();
       
   221 	enum TSyncLinkStates
       
   222 		{
       
   223 		EClosed,
       
   224 		EListening,
       
   225 		EAccepting,
       
   226 		EWaitForStart,
       
   227 		EWaitForStartError,
       
   228 		EWaitForSCO,
       
   229 		EOpen,	
       
   230 		EClosing,
       
   231 	// *** keep next one last ***
       
   232 		ESyncLinkMaxState,
       
   233 		};
       
   234 	
       
   235 	TSyncLinkState& GetState(TSyncLinkStates aState);
       
   236 	TInt StateIndex(const TSyncLinkState* aState) const;
       
   237 private:
       
   238 	void ConstructL();
       
   239 	TFixedArray<TSyncLinkState*, ESyncLinkMaxState> iStates;
       
   240 	};
       
   241 
       
   242 
       
   243 
       
   244 //--------------------------------------------------
       
   245 // STATES 
       
   246 //--------------------------------------------------
       
   247 
       
   248 NONSHARABLE_CLASS(TSyncLinkState)
       
   249 	{
       
   250 public:
       
   251 	TSyncLinkState(CSyncLinkStateFactory& aFactory);
       
   252 
       
   253 	virtual void Enter(CBTSynchronousLink& aContext) const;
       
   254 
       
   255 	virtual void ActiveOpen(CBTSynchronousLink& aContext) const;
       
   256 	virtual TInt PassiveOpen(CBTSynchronousLink& aContext, TUint aQueSize) const;
       
   257 	virtual void Shutdown(CBTSynchronousLink& aContext, CServProviderBase::TCloseType aCloseType) const;
       
   258 
       
   259 	virtual void DataReceived(CBTSynchronousLink& aContext, const TDesC8& aData) const;
       
   260 	virtual void ConnectComplete(CBTSynchronousLink& aContext, const TBTConnect& aSCOInfo, const TBTSyncConnectOpts& aSyncOpts) const;
       
   261 	virtual void Disconnection(CBTSynchronousLink& aContext) const;
       
   262 	virtual void Error(CBTSynchronousLink& aContext, TInt aErr) const;
       
   263 	virtual TBool ConnectRequest(CBTSynchronousLink& aContext, const TBTConnect& aSCOLink, const CPhysicalLink& aPhysicalLink) const;
       
   264 
       
   265 	virtual void PacketTypeChange(CBTSynchronousLink& aContext, THCIErrorCode aErr, THCIConnHandle aConnH, TUint16 aNewPacket) const;
       
   266 	virtual TUint Write(CBTSynchronousLink& aContext, const TDesC8& aData, TUint aOptions, TSockAddr* aAddr) const;
       
   267 	virtual void Start(CBTSynchronousLink& aContext) const;
       
   268 	virtual void Exit(CBTSynchronousLink& aContext) const;
       
   269 	virtual void Deletion(CBTSynchronousLink& aContext) const;
       
   270 	virtual TBool IsIdle() const;
       
   271 	virtual TBool IsOpen() const;
       
   272 	virtual void Timeout(CBTSynchronousLink& aContext, TBasebandTimeout aTimeout) const;
       
   273 	virtual void ParentClosing(CBTSynchronousLink& aContext) const;
       
   274 	virtual TBool IsConnectPending() const;
       
   275 
       
   276 protected:
       
   277 	// Exits old state, sets the new state, and enters it.
       
   278 	void ChangeState(CBTSynchronousLink& aContext, CSyncLinkStateFactory::TSyncLinkStates aState) const;
       
   279 	void PanicInState(TLinkPanic aPanic) const;
       
   280 
       
   281 protected:
       
   282 	CSyncLinkStateFactory& iFactory;
       
   283 #ifdef __FLOG_ACTIVE
       
   284 	TBuf<48> iName;
       
   285 #endif
       
   286 	};
       
   287 
       
   288 
       
   289 NONSHARABLE_CLASS(TSCOLinkStateClosed) : public TSyncLinkState
       
   290 	{
       
   291 public:
       
   292 	TSCOLinkStateClosed(CSyncLinkStateFactory& aFactory);
       
   293 
       
   294 	void Enter(CBTSynchronousLink& aContext) const;
       
   295 
       
   296 	void Shutdown(CBTSynchronousLink& aContext, CServProviderBase::TCloseType aCloseType) const;
       
   297 	void Deletion(CBTSynchronousLink& aContext) const;
       
   298 	void ActiveOpen(CBTSynchronousLink& aContext) const;
       
   299 	TInt PassiveOpen(CBTSynchronousLink& aContext, TUint aQueSize) const;
       
   300 	void Disconnection(CBTSynchronousLink& aContext) const;
       
   301 	void Start(CBTSynchronousLink& aContext) const;
       
   302 	TBool IsIdle() const;
       
   303 	};
       
   304 
       
   305 
       
   306 NONSHARABLE_CLASS(TSCOLinkStateWaitForSCO) : public TSyncLinkState
       
   307 	{
       
   308 public:
       
   309 	TSCOLinkStateWaitForSCO(CSyncLinkStateFactory& aFactory);
       
   310 
       
   311 	void Shutdown(CBTSynchronousLink& aContext, CServProviderBase::TCloseType aCloseType) const;
       
   312 	void ConnectComplete(CBTSynchronousLink& aContext, const TBTConnect& aSCOInfo, const TBTSyncConnectOpts& aSyncOpts) const;
       
   313 	void Disconnection(CBTSynchronousLink& aContext) const;
       
   314 	void Error(CBTSynchronousLink& aContext, TInt aErr) const;
       
   315 	TBool IsConnectPending() const;
       
   316 	};
       
   317 
       
   318 
       
   319 NONSHARABLE_CLASS(TSCOLinkStateAccepting) : public TSyncLinkState
       
   320 	{
       
   321 public:
       
   322 	TSCOLinkStateAccepting(CSyncLinkStateFactory& aFactory);
       
   323 
       
   324 	void Enter(CBTSynchronousLink& aContext) const;
       
   325 	void Exit(CBTSynchronousLink& aContext) const;	
       
   326 	void Shutdown(CBTSynchronousLink& aContext, CServProviderBase::TCloseType aCloseType) const;
       
   327 	void Disconnection(CBTSynchronousLink& aContext) const;
       
   328 
       
   329 // the logical link
       
   330 	void ConnectComplete(CBTSynchronousLink& aContext, const TBTConnect& aSCOInfo, const TBTSyncConnectOpts& aSyncOpts) const;
       
   331 	void Error(CBTSynchronousLink& aContext, TInt aError) const;
       
   332 	void Timeout(CBTSynchronousLink& aContext, TBasebandTimeout aTimeout) const;
       
   333 
       
   334 	void Deletion(CBTSynchronousLink& aContext) const;
       
   335 	void ParentClosing(CBTSynchronousLink& aContext) const;
       
   336 	};
       
   337 
       
   338 
       
   339 NONSHARABLE_CLASS(TSCOLinkStateListening) : public TSyncLinkState
       
   340 	{
       
   341 public:
       
   342 	TSCOLinkStateListening(CSyncLinkStateFactory& aFactory);
       
   343 
       
   344 	void Shutdown(CBTSynchronousLink& aContext, CServProviderBase::TCloseType aCloseType) const;
       
   345 	void Deletion(CBTSynchronousLink& aContext) const;
       
   346 	TBool ConnectRequest(CBTSynchronousLink& aContext, const TBTConnect& aSCOLink, const CPhysicalLink& aPhysicalLink) const;
       
   347 
       
   348 	void Exit(CBTSynchronousLink& aContext) const;
       
   349 
       
   350 private:
       
   351 	void SpawnL(CBTSynchronousLink& aContext, CPhysicalLink& aPhysicalLink) const;
       
   352 	};
       
   353 
       
   354 
       
   355 NONSHARABLE_CLASS(TSCOLinkStateWaitForStart) : public TSyncLinkState
       
   356 	{
       
   357 public:
       
   358 	TSCOLinkStateWaitForStart(CSyncLinkStateFactory& aFactory);
       
   359 
       
   360 	void Start(CBTSynchronousLink& aContext) const;
       
   361 	void Shutdown(CBTSynchronousLink& aContext, CServProviderBase::TCloseType aCloseType) const;
       
   362 	
       
   363 	void Deletion(CBTSynchronousLink& aContext) const;
       
   364 	void Disconnection(CBTSynchronousLink& aContext) const;
       
   365 
       
   366 	void Error(CBTSynchronousLink& aContext, TInt aError) const;
       
   367 	};
       
   368 
       
   369 NONSHARABLE_CLASS(TSCOLinkStateWaitForStartError) : public TSyncLinkState
       
   370 	{
       
   371 public:
       
   372 	TSCOLinkStateWaitForStartError(CSyncLinkStateFactory& aFactory);
       
   373 	void Shutdown(CBTSynchronousLink& aContext, CServProviderBase::TCloseType aCloseType) const;
       
   374 
       
   375 	void Start(CBTSynchronousLink& aContext) const;
       
   376 	void Deletion(CBTSynchronousLink& aContext) const;
       
   377 	};
       
   378 
       
   379 NONSHARABLE_CLASS(TSCOLinkStateOpen) : public TSyncLinkState
       
   380 	{
       
   381 public:
       
   382 	TSCOLinkStateOpen(CSyncLinkStateFactory& aFactory);
       
   383 
       
   384 	void Shutdown(CBTSynchronousLink& aContext, CServProviderBase::TCloseType aCloseType) const;
       
   385 	void DataReceived(CBTSynchronousLink& aContext, const TDesC8& aData) const;
       
   386 	void Disconnection(CBTSynchronousLink& aContext) const;
       
   387 	void Error(CBTSynchronousLink& aContext, TInt aErr) const;
       
   388 	void PacketTypeChange(CBTSynchronousLink& aContext, THCIErrorCode aErr, THCIConnHandle aConnH, TUint16 aNewPacket) const;
       
   389 	TUint Write(CBTSynchronousLink& aContext, const TDesC8& aData, TUint aOptions, TSockAddr* aAddr) const;
       
   390 	TBool IsOpen() const;
       
   391 
       
   392 	void Exit(CBTSynchronousLink& aContext) const;
       
   393 	};
       
   394 
       
   395 
       
   396 
       
   397 NONSHARABLE_CLASS(TSCOLinkStateClosing) : public TSyncLinkState
       
   398 	{
       
   399 public:
       
   400 	TSCOLinkStateClosing(CSyncLinkStateFactory& aFactory);
       
   401 	void Shutdown(CBTSynchronousLink& aContext, CServProviderBase::TCloseType aCloseType) const;
       
   402 	void ConnectComplete(CBTSynchronousLink& aContext, const TBTConnect& /*aSCOInfo*/, const TBTSyncConnectOpts& /*aSyncOpts*/) const;
       
   403 	void ActiveOpen(CBTSynchronousLink& aContext) const;
       
   404 	void Error(CBTSynchronousLink& /*aContext*/, TInt /*aError*/) const;
       
   405 	void Deletion(CBTSynchronousLink& /*aContext*/) const;
       
   406 	void Disconnection(CBTSynchronousLink& aContext) const;
       
   407 	TBool IsIdle() const;
       
   408 	};
       
   409 
       
   410 #ifdef __FLOG_ACTIVE
       
   411 #define STATENAME(x)  iName=_L(x)
       
   412 #else
       
   413 #define STATENAME(x)
       
   414 #endif
       
   415 
       
   416 #endif // _SCOSAP_H_