bluetooth/btcomm/src/btcomm.h
changeset 0 29b1cd4cb562
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     1 // Copyright (c) 1997-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 BTCOMM_H__
       
    17 #define BTCOMM_H__
       
    18 
       
    19 #include <es_sock.h>
       
    20 #include <cs_port.h>
       
    21 
       
    22 #include <bt_sock.h>
       
    23 #include <btmanclient.h>
       
    24 #include <btdefcommport.h>
       
    25 
       
    26 
       
    27 static const TInt KBTCOMMRecvBufferLength=1024;
       
    28 static const TInt KBTCOMMSendBufferLength=1024;
       
    29 static const TInt KBTCOMMCircularBufferLength=4096;
       
    30 static const TInt KBTCOMMCircularBufferHighWatermark=3072;
       
    31 static const TInt KBTCOMMCircularBufferLowWatermark=2048;
       
    32 
       
    33 const TUint KCommLowUnit=0;
       
    34 const TInt KBtcommShutdownTimer=6000000; //2 secs
       
    35 
       
    36 
       
    37 
       
    38 //  CONCRETE FACTORY CLASSES FOR BTMODEM CSY
       
    39 
       
    40 #define SERIAL_DESCRIPTION _L("BT RFComm Serial Emulation")
       
    41 #define SERIAL_NAME _S("BTCOMM")
       
    42 
       
    43 // Concrete factory makes the CPort which does the work
       
    44 // This uses the "highlander pattern".  ie there can only
       
    45 // be one factory. Note that the one factory instance 
       
    46 // can make many CPort instances however.
       
    47 class CBTPortStateFactory;
       
    48 NONSHARABLE_CLASS(CBTPortFactory) : public CSerial
       
    49 	{
       
    50 public:
       
    51 	~CBTPortFactory();
       
    52 	static CBTPortFactory* NewL();
       
    53 	virtual CPort* NewPortL(const TUint aUnit);
       
    54 	virtual void Info(TSerialInfo& aSerialInfo);
       
    55 	inline CBTPortStateFactory* GetPortStateFactory() {return iPortStateFactory;};
       
    56 protected:
       
    57 	//From CSerial
       
    58     virtual TSecurityPolicy PortPlatSecCapability(TUint aPort) const;
       
    59 private:
       
    60 	void ConstructL();
       
    61 	CBTPortFactory();
       
    62 private:
       
    63 	CBTPortStateFactory* iPortStateFactory;
       
    64 	};
       
    65 
       
    66 
       
    67 class CBTPortProxy;
       
    68 class CBTPortLocker;
       
    69 class CBTPortReader;
       
    70 class CBTPortWriter;
       
    71 NONSHARABLE_CLASS(CBTPort) : public CPort
       
    72 /**
       
    73 	Concrete implementation of the BTComm CSY.
       
    74 	This is in effect the server of the RComm client requests.
       
    75 */
       
    76 	{
       
    77 	friend class CBTPortProxy;
       
    78 public:
       
    79 	static CBTPort* NewL(TUint aUint,CBTPortStateFactory* aFactory);
       
    80 public:
       
    81 	virtual void StartRead(const TAny* aClientBuffer,TInt aLength);
       
    82 	virtual void ReadCancel();
       
    83 	virtual TInt QueryReceiveBuffer(TInt& aLength) const;
       
    84 	virtual void ResetBuffers(TUint aFlags);
       
    85 	virtual void StartWrite(const TAny* aClientBuffer,TInt aLength);
       
    86 	virtual void WriteCancel();
       
    87 	virtual void Break(TInt aTime);
       
    88 	virtual void BreakCancel();
       
    89 	virtual TInt GetConfig(TDes8& aPackage) const;
       
    90 	virtual TInt SetConfig(const TDesC8& aPackage);
       
    91 	virtual TInt SetServerConfig(const TDesC8& aPackage);
       
    92 	virtual TInt GetServerConfig(TDes8& aPackage);
       
    93 	virtual TInt GetCaps(TDes8& aPackage);
       
    94 	virtual TInt GetSignals(TUint& aSignals);
       
    95 	virtual TInt SetSignalsToMark(TUint aSignals);
       
    96 	virtual TInt SetSignalsToSpace(TUint aSignals);
       
    97 	virtual TInt GetReceiveBufferLength(TInt& aLength) const;
       
    98 	virtual TInt SetReceiveBufferLength(TInt aLength);
       
    99     virtual ~CBTPort();
       
   100 	virtual void Destruct();
       
   101 	virtual void FreeMemory();
       
   102 	// extra functions introduced by v6 c32
       
   103 	virtual void NotifySignalChange(TUint aSignalMask);
       
   104 	virtual void NotifySignalChangeCancel();
       
   105 	virtual void NotifyConfigChange();
       
   106 	virtual void NotifyConfigChangeCancel();
       
   107 	virtual void NotifyFlowControlChange();
       
   108 	virtual void NotifyFlowControlChangeCancel();
       
   109 	virtual void NotifyBreak();
       
   110 	virtual void NotifyBreakCancel();
       
   111 	virtual void NotifyDataAvailable();
       
   112 	virtual void NotifyDataAvailableCancel();
       
   113 	virtual void NotifyOutputEmpty();
       
   114 	virtual void NotifyOutputEmptyCancel();
       
   115 	virtual TInt GetFlowControlStatus(TFlowControl& aFlowControl);
       
   116 	virtual TInt GetRole(TCommRole& aRole);
       
   117 	virtual TInt SetRole(TCommRole aRole);
       
   118 	//
       
   119 private:
       
   120     CBTPort();
       
   121 	void ConstructL(TUint aUnit,CBTPortStateFactory *aFactory);
       
   122 	void DestructNow(); //called by CBTPortproxy to signal shutdown
       
   123 private:
       
   124 	TCommRole			iRole;
       
   125 	TCommConfigV01		iConfig;
       
   126 	TCommCapsV02		iCaps;
       
   127 	CBTPortProxy		*iPortProxy;
       
   128 	};
       
   129 
       
   130 NONSHARABLE_CLASS(TReadTerminator)
       
   131 	{
       
   132 public:
       
   133 	TInt	iTerminatorCount; //Number of terminator characters
       
   134 	TText8	iTerminatorChars[KConfigMaxTerminators];
       
   135 	TInt    iNextCharToFindIdx; // index in the array of the next char in the sequence
       
   136 								// to search for
       
   137 	};
       
   138 
       
   139 class CBTTimerSimple;
       
   140 class CBTPortBuffer;
       
   141 class TBTPortState;
       
   142 class CSockServConnector;
       
   143 NONSHARABLE_CLASS(CBTPortProxy) : public CActive
       
   144 // Context class in state pattern.
       
   145 // Friendship is extended to this class so that it 
       
   146 // can see the CPort instance private data.
       
   147 	{
       
   148 	friend class CBTPort;
       
   149 	friend class CBTPortLocker;
       
   150 	friend class CBTPortWriter;
       
   151 	friend class CBTPortReader;
       
   152 	friend class CBTTimerSimple;
       
   153 	friend class TBTPortDefaultState;
       
   154 	friend class TBTPortCommonBaseState;
       
   155 	friend class TBTPortStateIdle;
       
   156 	friend class TBTPortStateLoadingProtocol;
       
   157 	friend class TBTPortStateDiscovering;
       
   158 	friend class TBTPortStateSDPConnected;
       
   159 	friend class TBTPortStateSDPServiceQuery;
       
   160 	friend class TBTPortStateSDPAttributeListRetrieved;
       
   161 	friend class TBTPortStateConnectionSecuritySetup;
       
   162 	friend class TBTPortStateConnecting;
       
   163 	friend class TBTPortStateOpen;
       
   164 	friend class TBTPortStateClosing;
       
   165 	friend class TBTPortErrorState;
       
   166 	friend class TBTPortStateServiceIDListRetrieved;
       
   167 public:
       
   168 	static CBTPortProxy* NewL(TUint32 aPortNo, CBTPort *aParent,CBTPortStateFactory *aFactory);
       
   169 	~CBTPortProxy();
       
   170 	inline void SetState(TBTPortState& aState){ iState=&aState;};
       
   171 	void MoveToErrorState();
       
   172 	// from CActive
       
   173 	virtual void RunL();
       
   174 	virtual void DoCancel();
       
   175 	// interface of proxy
       
   176 	void Read(const TAny* aClientBuffer,TInt aLength);
       
   177 	void ReadCancel();
       
   178 	void Write(const TAny* aClientBuffer,TInt aLength);
       
   179 	void WriteCancel();
       
   180 	TInt QueryReceiveBuffer(TInt &aLength);
       
   181 	TInt GetReceiveBufferLength(TInt &aLength);
       
   182 	void ResetRxBuffer();
       
   183 	void Close();
       
   184 private:
       
   185 	CBTPortProxy(TUint32 aPortNo);
       
   186 	void InitL(CBTPort *aParent,CBTPortStateFactory *aFactory);
       
   187 	void DestructContext();
       
   188 	// context locker AO
       
   189 	void StartLocker();
       
   190 	void DoLockedAction();
       
   191 	TBool IsLockerOn();
       
   192 	void StopLocker();
       
   193 	// context writer AO
       
   194 	void StartWriter();
       
   195 	void DoWriteCompleted(TInt aError);
       
   196 	void StopWriter();
       
   197 	// context reader AO
       
   198 	void StartReader();
       
   199 	void DoReadCompleted(TInt aError);
       
   200 	void StopReader();
       
   201 	// To be called by the active friendly states
       
   202 	void DoWriteCancel();
       
   203 	// void DoReadCancel(); not needed since read cancels are not propagated to the underlying socket
       
   204 	void StartShutdownTimerL();
       
   205 	void CancelShutDownTimer();
       
   206 	void ShutdownAlarm();
       
   207 	// To be called by the Open state only
       
   208 	// void SetReadCancelPending(); not needed since read cancels are not propagated to the underlying socket
       
   209 	void SetWriteCancelPending();
       
   210 	TBool IsWriteCancelPending();
       
   211 	// TBool IsReadCancelPending(); not needed since read cancels are not propagated to the underlying socket
       
   212 	TBool ReadInBufferLowWatermarkReached();
       
   213 	TBool ReadInBufferHighWatermarkReached();
       
   214 	// To be called by Closing and SDP Discovering states only
       
   215 	TBool IsNetDbInUse();
       
   216 	void SetNetDbInUse();
       
   217 	void SetNetDbNotInUse();
       
   218 private:
       
   219 	TInt					iLastError;
       
   220 	TBTPortState*			iState;
       
   221 	CBTPort*				iPort;
       
   222 	TUint32					iPortNo;
       
   223 	CBTPortStateFactory*	iPortStateFactory;
       
   224     CBTPortLocker*			iPortLocker;
       
   225 	CBTPortReader*			iPortReader;
       
   226 	CBTPortWriter*			iPortWriter;
       
   227 	CSockServConnector*		iSockServConnector;
       
   228 	RSocketServ				iSockServ;
       
   229 	RSocket					iSocket;
       
   230 	CBTPortBuffer*			iCircularReadBuf;
       
   231 	HBufC8*					iSendBuf;
       
   232 	TPtr8					iSendBufPtr;
       
   233 	HBufC8*					iReadBuf;
       
   234 	TPtr8					iReadBufPtr;
       
   235 
       
   236 							// temp FIX
       
   237 	HBufC8*					iReadOutBuf; // to be used for reading from the circular buf to the client
       
   238 	
       
   239 	TBool					iMoreSendsToCome;
       
   240 	TAny*					iClientWritePtr;
       
   241 	TInt					iClientWriteLength;
       
   242 	TInt					iClientLengthWrittenSoFar;
       
   243 	TAny*					iClientReadPtr;
       
   244 	TInt					iClientReadLength;
       
   245 	TInt					iClientRemainderToRead;
       
   246 	TInt					iClientWriteOffset;
       
   247 	TBool					iClientReadOneOrMore;
       
   248 	RNetDatabase			iNetDatabase;
       
   249 	TNameEntry				iLog;
       
   250 	TUint					iRemoteDevAddr;
       
   251 	TBool					iTerminatedReads;
       
   252 	TReadTerminator			iTerm;
       
   253 	TInt					iReadTerminatorOffset;
       
   254 
       
   255 	// TBool					iReadCancelationPending; not needed because the read cancel is not propagated to the underlying socket
       
   256 	TBool					iWriteCancelationPending;
       
   257 	TRfcommSockAddr			iAddr;
       
   258 	TBuf8<512>				iSDPResult;
       
   259 	TBuf8<512>				iSDPServRecordHandle;
       
   260 	TBuf8<255>				iSDPRequest;
       
   261 	TUint8					iRemoteRfcommPortNumber;
       
   262 	TBTDevAddr				iBdaddr;
       
   263 	TBool					iNetDbInUse;
       
   264 
       
   265 	RBTRegServ				iRegServ;
       
   266 	RBTCommPortSettings		iPortSettings;
       
   267 	TBTCommPortSettings   	iDefaultService;
       
   268 	CBTTimerSimple*			iShutdownTimer;
       
   269 
       
   270 #ifdef _DEBUG
       
   271 	TInt					iReadsPending; //for debugging calls to the reader
       
   272 #endif
       
   273 };
       
   274 
       
   275 #endif