bluetooth/btstack/rfcomm/rfcommsap.h
changeset 0 29b1cd4cb562
child 13 16aa830c86c8
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     1 // Copyright (c) 2000-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 #ifndef RFCOMMSAP_H
       
    17 #define RFCOMMSAP_H
       
    18 
       
    19 #include <es_prot.h>
       
    20 #include <bttypes.h>
       
    21 #include <bt_sock.h>
       
    22 #include "BtSap.h"
       
    23 #include "rfcommtypes.h"
       
    24 #include "secman.h"
       
    25 
       
    26 class CRfcommMuxer;
       
    27 class CRfcommProtocol;
       
    28 class TRfcommState;
       
    29 class CAsyncErrorKicker;
       
    30 
       
    31 /**
       
    32    The socket service access point.
       
    33    This class forms the major interaction point with sockets.
       
    34 **/
       
    35 NONSHARABLE_CLASS(CRfcommSAP) : public CBluetoothSAP
       
    36 	{
       
    37 friend class CRfcommProtocol;
       
    38 friend class CRfcommMuxer;
       
    39 
       
    40 	// All states are friends
       
    41 friend class TRfcommState;
       
    42 friend class TRfcommStateError;
       
    43 friend class TRfcommStateDefault;
       
    44 friend class TRfcommStateClosed;
       
    45 friend class TRfcommStateWaitForMux;
       
    46 friend class TRfcommStateWaitForPNResp;
       
    47 friend class TRfcommStateWaitForUA;
       
    48 friend class TRfcommStateListening;
       
    49 friend class TRfcommStateCloned;
       
    50 friend class TRfcommStateWaitForSABM;
       
    51 friend class TRfcommStateIncomingSecurityCheck;
       
    52 friend class TRfcommStateOutgoingSecurityCheck;
       
    53 friend class TRfcommStateWaitForStart;
       
    54 friend class TRfcommStateCloseOnStart;
       
    55 friend class TRfcommStateConnecting;
       
    56 friend class TRfcommStateOpen;
       
    57 friend class TRfcommStateDisconnect;
       
    58 friend class TRfcommStateDisconnecting;
       
    59 public:
       
    60 	// Creation
       
    61 	static CRfcommSAP* NewL(CRfcommProtocol& aProtocol);
       
    62 	~CRfcommSAP();
       
    63 	
       
    64 	// CServProviderBase functions
       
    65 	virtual void Start();
       
    66 	virtual void LocalName(TSockAddr& aAddr) const;
       
    67 	virtual TInt SetLocalName(TSockAddr& aAddr);
       
    68 	virtual void RemName(TSockAddr& aAddr) const;
       
    69 	virtual TInt SetRemName(TSockAddr& aAddr);
       
    70 	virtual TInt GetOption(TUint aLevel,TUint aName,TDes8& aOption) const;
       
    71 	virtual TInt SAPSetOption(TUint aLevel,TUint aName,const TDesC8 &aOption);
       
    72 	virtual void Ioctl(TUint aLevel,TUint aName,TDes8* aOption);
       
    73 	virtual void CancelIoctl(TUint aLevel,TUint aName);
       
    74 
       
    75 #ifndef __WINS__
       
    76 #ifndef __GCC32__
       
    77 	//unhide the other overloads
       
    78 	using CServProviderBase::Write;
       
    79 	using CServProviderBase::GetData;
       
    80 #endif
       
    81 #endif
       
    82 
       
    83 	virtual TUint Write(const TDesC8& aDesc,TUint aOptions, TSockAddr* aAddr=NULL);
       
    84 	virtual void GetData(TDes8& aDesc,TUint aOptions,TSockAddr* aAddr=NULL);
       
    85 
       
    86 	virtual void ActiveOpen();
       
    87 	virtual void ActiveOpen(const TDesC8& aConnectionData);
       
    88 	virtual TInt PassiveOpen(TUint aQueSize);
       
    89 	virtual TInt PassiveOpen(TUint aQueSize,const TDesC8& aConnectionData);
       
    90 	virtual void Shutdown(TCloseType aOption);
       
    91 	virtual void Shutdown(TCloseType aOption,const TDesC8& aDisconnectionData);
       
    92 	virtual void AutoBind();
       
    93 	// End CServProviderBase functions
       
    94 	
       
    95 	// Notifications from the mux
       
    96 	void LinkDown();
       
    97 	void DISC();
       
    98 	void UA();
       
    99 	void DM();
       
   100 	void RPN(const TRfcommRPNTransaction* aRPNTransactionPtr, CRfcommMuxer& aMuxer, TUint8 aDLCI);
       
   101 	void RPNRsp(const TRfcommRPNTransaction& aRPNTransaction);
       
   102 	void PN(TRfcommPortParams& aParams, CRfcommMuxer& aMuxer, TUint8 aDLCI);
       
   103 	void PNResp(TRfcommPortParams& aParams);
       
   104 	void MSC(TUint8 aSignals);
       
   105 	void RLS(TUint8 aStatus);
       
   106 	void Data(const TDesC8& aData);
       
   107 	void CanSend();
       
   108 	void IoctlComplete(TInt aErr, TUint aLevel, TUint aName, TDesC8* aBuf);
       
   109 
       
   110 	void SABM(CRfcommMuxer& aMuxer, TUint8 aDLCI);
       
   111 
       
   112 	// Notifications from the protocol
       
   113 	void MuxUp();
       
   114 	
       
   115 	// Notifications from sent frames awaiting a response
       
   116 	TBool HandleFrameResponseTimeout();
       
   117 
       
   118     // Other functions
       
   119 	enum TErrorTypes
       
   120 		{
       
   121 		EErrorOperation,   // The operation in progress failed
       
   122 		EErrorGeneral,      // This screws up all operations
       
   123 		EErrorFatal         // Very bad ju-ju
       
   124 		};
       
   125 	void Error(TInt aErrorCode, TErrorTypes aType);
       
   126 	TBool ServerChannelValid() const;
       
   127 	TRfcommChannel ServerChannel() const;
       
   128 	TUint8 DLCI() const;
       
   129 	TUint16 MaximumMTU() const;
       
   130 	TBool IsCloned();
       
   131 	
       
   132 	TBool CTS() const //< Are we clear to send?
       
   133 			{return iClearToSend;}
       
   134 	void CTS(TBool aState)
       
   135 			{iClearToSend=aState;}
       
   136 	TBool CTR() const  //< Are we clear to receive?
       
   137 			{return iClearToRecv;}
       
   138 	void CTR(TBool aState)
       
   139 			{iClearToRecv=aState;}
       
   140 	enum TSendBlockedBy
       
   141 		{
       
   142 		ESendOK = 0,
       
   143 		EBlockedByMux = 0x01,
       
   144 		EBlockedByRemote = 0x02,
       
   145 		EBlockedByBoth = 0x03
       
   146 		};
       
   147 	
       
   148 	TInt SendBlocked() const  //< Is send currently blocked
       
   149 			{return iSendBlocked;}
       
   150 	void SendBlocked(TInt aState)
       
   151 			{iSendBlocked=aState;}
       
   152 
       
   153 	TRfcommRemotePortParams LocalPortParams()
       
   154 		{return iLocalPortParams;}
       
   155 	void SetLocalPortParams(TRfcommRemotePortParams aPortParams)
       
   156 		{iLocalPortParams = aPortParams;}
       
   157 
       
   158 	TInt LowTideMark()
       
   159 		{return iLowTideMark;};
       
   160 	void SetLowTideMark(TInt aLowTideMark)
       
   161 		{iLowTideMark = aLowTideMark;};
       
   162 	TInt HighTideMark()
       
   163 		{return iHighTideMark;};
       
   164 	void SetHighTideMark(TInt aHighTideMark)
       
   165 		{iHighTideMark = aHighTideMark;};
       
   166 	TUint8 Signals()
       
   167 		{return iSignals;};
       
   168 	void SetSignals(TUint8 aSignals)
       
   169 		{iSignals = aSignals;};
       
   170 
       
   171 	CRfcommMuxer* Mux();
       
   172 
       
   173 	CCirBuffer& DataBuffer();
       
   174 	TInt UnusedBufferSpace() const;
       
   175 
       
   176 	TUint16 NegotiatedMTU() const; //< Returns the negotiated MTU
       
   177 	TUint16 UsableMTU(TUint8 aCredit) const; //< Returns the actual maximum information field size available (which with CBFC will be one less than the negotiated MTU)
       
   178 
       
   179 	TBool ListeningTo(const TBTSockAddr& aBTSockAddr) const;
       
   180 	inline const CRfcommSAP& ListeningSAP() const {return *iParentSAP;};
       
   181 //TRY_CBFC
       
   182 	void DisallowCBFC();
       
   183 	void AllowCBFC();
       
   184 	TUint8 FreeCredit();
       
   185 	TUint8 FreeCreditCalculation() const;
       
   186 	TInt ProxyForRemoteCredit() const;
       
   187 	void SetProxyForRemoteCredit(TInt aCredit);
       
   188 	void ProxyForRemoteCreditDecrement();
       
   189 	void ProxyForRemoteCreditAddCredit(TUint8 aCredit);
       
   190 	TInt LocalCredit() const;
       
   191 	void SetInitialLocalCredit(TInt aCredit);
       
   192 	void LocalCreditDecrement();
       
   193 	void LocalCreditAddCredit(TUint8 aCredit);
       
   194 	TBool BoundTo(const TBTSockAddr& aBTSockAddr) const;
       
   195 private:
       
   196 	// Creation
       
   197 	CRfcommSAP(CRfcommProtocol& aProt);
       
   198 	void ConstructL();
       
   199 
       
   200 	TInt PassiveAutoBind(TRfcommSockAddr& aAddress);
       
   201 
       
   202 	//	Cloning support
       
   203 	//	(NB. Only a SAP may clone itself)
       
   204 	CRfcommSAP* CloneMe();
       
   205 
       
   206 	//	Functions called on a Parent SAP by a cloned child
       
   207 	void ChildConnected(CRfcommSAP& aRfcommSAP);
       
   208 	void ChildConnectFailed(CRfcommSAP& aRfcommSAP);
       
   209 	void ChildStarted(CRfcommSAP& aRfcommSAP);
       
   210 
       
   211 	//	From MAccessRequestResponseHandler
       
   212 	void AccessRequestComplete(TInt aResult);
       
   213 
       
   214 	//	Functions called on cloned child SAPs by their parent
       
   215 	void ParentClosed();
       
   216 
       
   217 	//
       
   218 	void StopListening();
       
   219 	TBool IsValidServerChannel(TInt aChan) const;
       
   220 	static TInt NotifyNewDataCallback(TAny* aSAP);
       
   221 
       
   222 	// Data incoming
       
   223 	CCirBuffer iDataBuffer;
       
   224 	TInt       iHighTideMark;
       
   225 	TInt       iLowTideMark;
       
   226 
       
   227 	CAsyncCallBack*		iNotifyNewDataCallback;
       
   228 	CAsyncErrorKicker*	iErrorKicker;			//	Used to asynchronously error a SAP
       
   229 
       
   230 	TInt		iNewDataToNotify;
       
   231 
       
   232 	TUint8     iDLCI;
       
   233 	TUint8     iServerChannel;
       
   234 	
       
   235 	TBool      iClearToSend;   //< Set to true if we can send
       
   236 	TBool      iClearToRecv;  //< True if we haven't switched off the remote
       
   237 	TBool	   iReceivedMSC;  //< For a new connection, we should get an MSC before any user data
       
   238 	TInt       iSendBlocked;  //< Reason sending by ESOCK is blocked, if at all
       
   239 	//TRY_CBFC
       
   240 	TInt	   iLocalCredit;	// in units of the MTU
       
   241 	TInt	   iProxyForRemoteCredit;		// in units of the MTU - is it symmetric ?
       
   242 	#ifdef _DEBUG
       
   243 	TInt	   iLocalCreditsSupplied; //total credits received in incoming frames
       
   244 	TInt	   iLocalCreditsUsed; //total credits used in sending data packets
       
   245 	TInt	   iProxyForRemoteCreditsSupplied; //total credits sent in outcoming frames
       
   246 	TInt	   iProxyForRemoteCreditsUsed; //total credits used in receiving data packets
       
   247 	#endif
       
   248 	TUint8     iSignals;  //< Current signals that we send for MSC cmd
       
   249 	TUint8	   iRLSstatus;	//< Current status that we send for RLS cmd
       
   250 	TUint8	   iRemoteModemStatus; //< Remote end's signals from last MSC received
       
   251 
       
   252 	TBool iClosePending;
       
   253 	// Links
       
   254 	CRfcommProtocol& iProtocol;
       
   255 	CRfcommMuxer*  iMux;
       
   256 	TRfcommState*  iState;
       
   257 	TDblQueLink    iLink;	//	Used by Muxer to keep track of the SAPs it is linked to
       
   258 	TDblQueLink    iListeningLink;	//	Used by the protocol to keep track of listening SAPs
       
   259 	TDblQueLink	   iBoundLink;		//	Used by the protocol to keep track of bound SAPs
       
   260 	TUint16			iMTU;	//	Maximum data size for this SAP
       
   261 	TUint16			iUserDefinedMTU;	//	User defined MTU (0=No restriction)
       
   262 	//	Members used by listening SAP
       
   263 	TInt			iMaxClonesWaitingForStart;
       
   264 	TInt			iClonesWaitingForStart; // SAPs that were listening, have connected but haven't been Accept()ed - owned by ESOCK
       
   265 	RPointerArray<CRfcommSAP> iClonedChildren; // SAPs that are listening but not connected - owned by parent SAP
       
   266 
       
   267 
       
   268 	//	Member used by cloned SAP
       
   269 	CRfcommSAP*		iParentSAP;
       
   270 
       
   271 	TBool iCloned; // Indicates if this is a cloned SAP
       
   272 
       
   273 	TRfcommRemotePortParams iLocalPortParams;	// Local Port Params for RPN commands/responses
       
   274 
       
   275 	TUint8			iDisconnectMSCSignal;	// signal state that will cause a disconnect error
       
   276 
       
   277 	// Used only when CBFC is employed.
       
   278 	// Defaults to EFalse.
       
   279 	// When EFalse, if the remote overflows us, we disconnect.
       
   280 	// When ETrue, if the remote overflows us, we just drop the data and 
       
   281 	// maintain the connection.
       
   282 	// See option KRFCOMMForgiveCBFCOverflow. The default behaviour is for to 
       
   283 	// be used by clients which require reliable transfer, e.g. OBEX. The 
       
   284 	// 'forgiving' behaviour is to be used by clients which aren't worried 
       
   285 	// about reliability at our level and would rather the connection stayed 
       
   286 	// up. 
       
   287 	TBool iForgiveCBFCOverflow;	
       
   288 	};
       
   289 
       
   290 #endif