os/ossrv/genericopenlibs/openenvcore/backend/inc/fdesc.h
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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 // Abstraction representing a UNIX file descriptor
    15 // CFileDescBase maintains the simple semantics of
    16 // Dup() and Close(), so the final Close() will
    17 // delete the object and hence call the destructor.
    18 // 
    19 //
    20 
    21 #ifndef _FDESC_H
    22 #define _FDESC_H
    23 
    24 #include <e32std.h>
    25 #include <e32atomics.h>
    26 #include <e32cons.h>
    27 #include <f32file.h>
    28 #include <es_sock.h>
    29 #include <c32comm.h>
    30 #include <stddef.h>
    31 #include <cdblen.h>
    32 #include <commdb.h>
    33 #include <commdbconnpref.h>
    34 #include <sys/stat.h>
    35 #include <fcntl.h>
    36 #include <rpipe.h>
    37 #include <in_sock.h>
    38 
    39 #include "base.h"
    40 #include "StdioClient.h"
    41 
    42 //FD attributes used internally by the backend
    43 const TUint KInvalidFd 		= 0x00000001;
    44 const TUint KMMapedFd 		= 0x00000002;
    45 const TUint KCloseonExec    = 0x00000004;
    46 const TUint KPipeFd         = 0x00000008;
    47 const TUint KIoctlOutstanding = 0x00000010;
    48 const TUint KConsoleFd         = 0x00000020;
    49 const TUint KFifoFd			= 0x00000030;
    50 const TUint KSpawnCloseInChild = 0x00000040;
    51 
    52 #define SET_CLOSE_ON_EXEC_FLG   (1 << 2) 
    53 
    54 #if defined(SYMBIAN_OE_LARGE_FILE_SUPPORT) && !defined(SYMBIAN_OE_NO_LFS)
    55 
    56 //Large File Support - Use RFile64 and 64 bit file sizes
    57 #define RFILE 	RFile64
    58 #define FSIZE	TInt64
    59 
    60 #else
    61 
    62 //No large file support - Use RFile and 32 bit file sizes
    63 #define RFILE 	RFile
    64 #define FSIZE	TInt
    65 
    66 #endif //SYMBIAN_OE_LARGE_FILE_SUPPORT && !SYMBIAN_OE_NO_LFS
    67 
    68 // Atomic socket operations support
    69 
    70 #define ATOMICSOCKETOP(realcall,on_error) \
    71     { \
    72     if (__e32_atomic_tau_ord32((void *)&iCount, 0x8000, 0, 1) >= 0x8000) \
    73         { \
    74         on_error; \
    75         } \
    76     else \
    77         { \
    78         realcall; \
    79         __e32_atomic_tas_ord32((void *)&iCount, 0, -1, 0); \
    80         } \
    81     }
    82 
    83 #define NOP 
    84 
    85 //Enumarations for Polling
    86 enum TPollMode
    87     {
    88     EReadyForReading = 1,
    89     EReadyForWriting = 2,
    90     EAnyException = 4
    91     };
    92   
    93 //Enumarations for Redirection Server
    94 enum TRedirConnState
    95 	{
    96 	ENotConnected,
    97 	EConnected,
    98 	ENoServer
    99 	};
   100 
   101 //Enumerations for the console echo
   102 enum TConsoleEcho
   103 	{
   104 	EOff = 0, //Turn Off the echo
   105 	EOn = 1,  //Turn On the echo
   106 	EPrintValid = 33, //Printable ascii character set <33-126>
   107 	EPrintInvalid=127 
   108 	};
   109 	
   110 //  The base class for all flavours of FileDescriptor
   111 //
   112 class CSocketDesc;
   113 
   114 NONSHARABLE_CLASS(CFileDescBase) : public CBase
   115 /*
   116 @internalComponent
   117 */
   118 	{
   119 public:
   120 
   121 	static CFileDescBase* Open(const wchar_t* name, int mode, int perms, TInt& err);
   122 	static CFileDescBase* Socket(RSocketServ& aSs, int family, int style, int protocol, TInt& err);
   123 
   124 	virtual void Read(TDes8& aDesc, TRequestStatus& aStatus);
   125 	virtual TInt ReadCompletion(TDes8& aDesc, TInt aStatus);
   126 	virtual void ReadCancel();
   127 
   128 	virtual void Write(TDes8& aDesc, TRequestStatus& aStatus);
   129 	virtual TInt WriteCompletion(TDes8& aDesc, TInt aStatus);
   130 	virtual void WriteCancel();
   131 	virtual TInt SetAtt(TUint aSetAttMask, TUint aClearAttMask);
   132 
   133 	virtual void Ioctl(int aCmd, void* aParam, TRequestStatus& aStatus);
   134 	virtual TInt IoctlCompletion(int aCmd, void* aParam, TInt aStatus);
   135 	virtual void IoctlCancel();
   136 
   137 	virtual void RecvFrom(TDes8& aDesc, TSockAddr& from, int flags, TRequestStatus& aStatus);
   138 	virtual void RecvFromCancel();
   139 
   140 	virtual void SendTo(TDes8& aDesc, const struct sockaddr* anAddr, unsigned long aAddrLen,int flags, TRequestStatus& aStatus);
   141 
   142 	virtual void SendToCancel();
   143 
   144 	virtual TInt CompletionStatus(TInt& aLength, TInt aStatus);
   145 
   146 	virtual void Sync(TRequestStatus& aStatus);
   147 	virtual void SyncCancel();
   148 
   149 	virtual void Accept(CFileDescBase*& aNewSocket, TRequestStatus& aStatus, RSocketServ& aSs,TSockAddr *aAddr);
   150 	virtual void AcceptCancel();
   151 
   152 	virtual void Connect(const struct sockaddr* aAddr,unsigned long size,TRequestStatus& aStatus);
   153 	virtual void ConnectCancel();
   154 	virtual TBool GetConnectionProgress();
   155 	virtual void SetConnectionProgress( TBool aInProgress );
   156 
   157 	virtual void Shutdown(TUint aHow,TRequestStatus& aStatus);
   158 	virtual void ShutdownCancel();
   159 	
   160 	virtual TBool TimedRead() {return EFalse;}	//default implementation
   161 	TInt TimeoutValue() const {return iReadTimeout;}
   162 
   163 	virtual TInt LSeek(off_t& offset, int whence);
   164 	virtual TInt FStat(struct stat* st);
   165 	virtual TInt Bind(const struct sockaddr* addr, unsigned long size);
   166 	virtual TInt Listen(TUint qSize);
   167 	virtual TInt SockName(int anEnd, struct sockaddr* anAddr,unsigned long* aSize);
   168 	virtual TInt GetSockOpt(TInt anOptionName,TInt anOptionLevel,TDes8& anOption);
   169 	virtual TInt SetSockOpt(TInt anOptionName,TInt anOptionLevel,TDesC8& anOption);
   170 	virtual TInt Fcntl(TUint anArg, TUint aCmd);
   171 	
   172 	virtual TInt Poll(TPollMode aMode,TBool& aReadyStatus,TInt& aErrno);
   173 	virtual TInt Poll(TUint aEvents);
   174 	virtual TInt NotifyActivity(TUint aEvents, TRequestStatus& aRequest, TTimeIntervalMicroSeconds32 timeout);
   175 	virtual void TweakWatchedEvents(TUint& events);
   176 	virtual TInt TweakReadyEvents(TInt errval);
   177 	virtual void CancelNotify();
   178 
   179 	//Truncate the File
   180 	virtual TInt Truncate(off_t /*anOffset*/)
   181 		{
   182 		return KErrNotSupported;
   183 		}
   184 	//Type of the Descriptor
   185 	virtual TInt Type()  
   186 		{
   187 		return EBaseDesc;
   188 		}
   189 		
   190 	//Set/reset the Attributes of the FD
   191 	 inline void SetAttributes( TUint32 aBit, TBool aFlag )
   192 		{
   193 		if( aFlag )
   194 			{
   195 			//Set the bit to 1
   196 			iFdAttrib |= aBit;
   197 			}
   198 		else
   199 			{
   200 			//Reset the bit to 0
   201 			iFdAttrib &= ~aBit;
   202 			}
   203 		}
   204 
   205 	//Get the Attributes of the FD
   206 	inline const TUint32 Attributes() const
   207 		{
   208 		return iFdAttrib;
   209 		}
   210 
   211 	inline const TUint32 FcntlFlag() const
   212 		{
   213 		return iFcntlFlag;
   214 		}
   215 
   216 	static inline void Cleanup(TAny *aPtr)
   217 		{
   218 		((CFileDescBase*)aPtr)->Close();
   219 		}
   220 		
   221 	inline void PushLC() 
   222 		{
   223 		CleanupStack::PushL(TCleanupItem(Cleanup,this));
   224 		}
   225 
   226 	IMPORT_C TInt Close();
   227 	virtual void UserClose() {return;}
   228 	inline CFileDescBase* Dup();
   229 	inline void SetFid(TInt aFid) { iFid = aFid; }
   230 	virtual void SetFids(void *aFids);
   231 	
   232 	inline void operator delete(TAny *aPtr) __NO_THROW
   233 		{
   234 		Backend()->Free(aPtr);
   235 		}
   236 		
   237 	inline void operator delete(TAny *aPtr, TLeave) __NO_THROW
   238 		{
   239 		Backend()->Free(aPtr);
   240 		}
   241 		
   242 	inline void operator delete(TAny *aPtr, TAny*) __NO_THROW
   243 		{
   244 		Backend()->Free(aPtr);
   245 		}
   246 		
   247 	TBool ReadIsTimed;
   248 	TBool ReadWasCancelled;
   249 
   250 protected:
   251 	CFileDescBase( TUint aFcntl = 0, TUint32 aFdAttrib = 0 );
   252 	virtual TInt FinalClose();
   253 	static void Complete(TRequestStatus& aStatus, TInt aResult);
   254 	TInt iReadTimeout;
   255 	//Used in case of CRedirDesc
   256 	TInt iFid;
   257 	//For FD's fcntl
   258 	TUint iFcntlFlag;
   259 	//For FD's other attributes
   260 	TUint32 iFdAttrib;
   261 	
   262 private:
   263 	//Check if the path corresponds to a directory
   264 	static TBool CheckIfDirectory(const TDesC& aPath, RFs& aFs);
   265 	
   266 public:
   267     enum TDescType
   268         {
   269         EBaseDesc,
   270         EFileDesc,
   271         EFileTempDesc,
   272         ESocketDesc,
   273         ESerialDesc,
   274         EDirDesc,
   275         ETtyDesc,
   276         EPopenPipeDesc,
   277         EPipeDesc,
   278         ERedirDesc,
   279         EStdErrDesc,
   280         EFileSocketDesc
   281         };
   282 private:
   283     TInt iDupCount;
   284 public:
   285 	// Poll is required to return any of the requested events. 
   286 	// In case of any error, "iPollErr" should be checked by the socket-descriptor
   287 	// to take any corrective action.
   288 	TInt iPollErr;
   289 	};
   290 
   291 
   292 inline CFileDescBase* CFileDescBase::Dup () 
   293 	{ iDupCount += 1; return this; }
   294 
   295 
   296 NONSHARABLE_CLASS(CTtyDesc) : public CFileDescBase
   297 /*
   298 Abstraction of a teletype device, which will be used for
   299 the console.
   300 @internalComponent
   301 */
   302 	{
   303 public:
   304 	inline CTtyDesc(CConsoleBase *c);
   305 	inline CTtyDesc();
   306 	~CTtyDesc();
   307      virtual TInt Type()
   308 	    {
   309 	    return ETtyDesc;
   310 	    }
   311 	void Read(TDes8& aDesc, TRequestStatus &aStatus);
   312 	void ReadCancel();
   313 	TInt ReadCompletion(TDes8& aDesc, TInt aStatus);
   314 	void Write(TDes8& aDesc, TRequestStatus& aStatus);
   315 	void Ioctl(int aCmd, void* aParam, TRequestStatus& aStatus);
   316 	TInt IoctlCompletion(int aCmd, void* aParam, TInt aStatus);
   317 	TInt SetEcho(TUint8 aEcho);
   318 protected:
   319 	virtual TInt FinalClose();
   320 private:
   321 	void MapCodeAndEcho(TDes8& aDesc, TKeyCode aCode);
   322 	void CheckConsoleCreated();
   323 	void Write(TDes8& aDesc);
   324 	CConsoleBase *iConsole;
   325 	//To store last non-modifiable data position and flag for reading data
   326 	TInt  iCurPosX;
   327 	TInt  iCurPosY;
   328 	TBool iReadingData;
   329 	TUint8 iEchoVal;
   330 	};
   331 
   332 inline CTtyDesc::CTtyDesc(CConsoleBase *c) : CFileDescBase(), iConsole(c), iEchoVal(EOn){}
   333 inline CTtyDesc::CTtyDesc() : CFileDescBase(), iEchoVal(EOn) {}
   334 
   335 
   336 NONSHARABLE_CLASS(CRedirDesc) : public CFileDescBase
   337 /*
   338 client-side CRedirDesc desc class which will be used to connecting to the Redirection Server
   339 @internalComponent
   340 */
   341 	{
   342 public:
   343 	CRedirDesc();
   344 	virtual TInt Type()
   345 	    {
   346 	    return ERedirDesc;
   347 	    }
   348 	virtual void Read(TDes8& aDesc, TRequestStatus& aStatus);
   349 	virtual void Write(TDes8& aDesc, TRequestStatus& aStatus);
   350 	TInt WriteCompletion(TDes8& aDesc, TInt /*aStatus*/);
   351 	TInt ReadCompletion(TDes8& aDesc, TInt /*aStatus*/);
   352 	virtual TInt Fcntl(TUint anArg, TUint aCmd);
   353 	TInt FinalClose();
   354 	TInt Configure();
   355 	TInt Poll(TUint aEvents);
   356 	TInt NotifyActivity(TUint aEvents, TRequestStatus& aRequest, TTimeIntervalMicroSeconds32 timeout);
   357 	TInt TweakReadyEvents(TInt errval);
   358 	void CancelNotify();
   359 	TInt SetEcho(TUint8 aEcho);
   360 	TBool iReadNone;
   361 	TBool iWriteNone;
   362 private:
   363 	//Handle to StdioSession
   364 	RStdioSession iSession;
   365 	TRedirConnState iStatus;
   366 	RFastLock iLock;
   367 	};
   368 
   369 
   370 NONSHARABLE_CLASS(CStdErrDesc) : public CFileDescBase
   371 
   372 /*
   373 client-side Standard Error desc class which will be used to put all stderr to RDebug::Printf
   374 @internalComponent
   375 */
   376 	{
   377 public:
   378 	CStdErrDesc();
   379 	virtual void Write(TDes8& aDesc, TRequestStatus& aStatus);
   380 	TInt WriteCompletion(TDes8& aDesc, TInt /*aStatus*/);
   381 	virtual TInt Fcntl(TUint anArg, TUint aCmd);
   382 	virtual TInt Type()
   383 	    {
   384 	    return EStdErrDesc;
   385 	    }
   386 	};
   387 
   388 NONSHARABLE_CLASS(CFileDesc) : public CFileDescBase
   389 /*
   390 Abstractions for a plain file and a temporary file
   391 @internalComponent
   392 */
   393 	{
   394 public:
   395 	CFileDesc();
   396 	~CFileDesc();
   397 
   398 	void SetState(const TDes& params);
   399 
   400 	TInt Open(RFs& aSession, const TDesC& aName, int mode, int perms);
   401 	TInt LSeek(off_t& offset, int whence);
   402 	void Read(TDes8& aDesc, TRequestStatus& aStatus);
   403 	void Write(TDes8& aDesc, TRequestStatus& aStatus);
   404 	TInt FStat(struct stat *st);
   405 	void Sync(TRequestStatus &aStatus);
   406 	TInt IoctlCompletion(int aCmd, void* aParam, TInt aStatus);
   407 	TInt Fcntl(TUint anArg, TUint aCmd);
   408 	static void MapStat(struct stat& st, const TTime& aModTime, TUint& aAttr, const mode_t aMode = S_IFREG);
   409 	TInt Truncate(off_t anOffset);
   410 	TInt SetAtt(TUint aSetAttMask, TUint aClearAttMask);
   411 	TInt ProcessLockParams(FSIZE& pos, FSIZE &lock_len, TInt& lock_type, struct flock* anArg);
   412 	inline int CreateLock() 
   413 		{
   414 		return iLock.CreateLocal();
   415 		}
   416 
   417 	inline RFILE& FileHandle()
   418 		{
   419 		return iFile;
   420 		}
   421 	
   422 	inline const FSIZE Offset() const
   423 		{
   424 		return iPos;
   425 		}
   426 	
   427     inline const FSIZE Size() const
   428 		{
   429 			return iSize;
   430 		}
   431 	inline void setSize(TUint32 param)
   432 		{
   433 		iSize = param;
   434 		}
   435 	inline const FSIZE Extent() const
   436 		{
   437 		return iExt;
   438 		}
   439      virtual TInt Type()
   440 	    {
   441 	    return EFileDesc;
   442 	    }
   443 protected:
   444 	virtual TInt FinalClose();
   445 	TInt Alloc();
   446 private:
   447 	TInt FileRead(TUint8* aPtr,TInt aLength);
   448 	TInt FileWrite(TUint8* aPtr,TInt aLength);
   449 	TInt Flush();
   450 	TInt DoSync();
   451 	TInt DoRead(TDes8& aDesc);
   452 	TInt DoWrite(TDes8& aDesc);
   453 	FSIZE Pos();
   454 	FSIZE Ext();
   455 	
   456 private:
   457 	enum {EAlloc,EReading,EWriting};
   458 	enum {EBufferSize = 0x600,EReadAhead = 0x200};
   459 protected:
   460 	RFILE	iFile;
   461 	TInt16	iDrive;	// for use with stat()
   462 private:
   463 	TUint8 iState;
   464 	TUint8* iBuffer;
   465 	TUint8* iPtr;
   466 	TUint8* iEnd;
   467 	
   468 	FSIZE iSize;
   469 	FSIZE iPos;
   470 	FSIZE iExt;
   471 	
   472 	//For locking the descriptor before any operation.
   473 	//To make it thread safe.
   474 	RFastLock iLock;
   475 	friend class RFileDesTransferSession;
   476 	friend class CFileDesTransferSession;
   477 	};
   478 
   479 NONSHARABLE_CLASS(CTempFileDesc) : public CFileDesc
   480 /*
   481 @internalComponent
   482 */
   483 	{
   484 public:
   485 	TInt Open(const wchar_t* aName, TInt mode);
   486 	virtual TInt Type()
   487 	    {
   488 	    return EFileTempDesc;
   489 	    }
   490 protected:
   491 	virtual TInt FinalClose();
   492 private:
   493 	RFs iSession;
   494 	TFileName iName;
   495 	};
   496 
   497 /*
   498 Implements common functionality between CSocketDesc and CFileSocketDesc.
   499 Not meant to be instantaible.
   500 */	
   501 NONSHARABLE_CLASS(CSockDescBase) : public CFileDescBase
   502 	{
   503 public:
   504 	 virtual TInt Socket(RSocketServ& aSs, int family, int style, int protocol);
   505 	 TInt Fcntl(TUint anArg, TUint aCmd);
   506 	 TInt FStat(struct stat *st);
   507 	 TInt FinalClose();
   508 	 void Read(TDes8& aDesc, TRequestStatus& aStatus);
   509 	 void Write(TDes8& aDesc, TRequestStatus& aStatus);
   510 	 void RecvFrom(TDes8& aDesc, TSockAddr& from, int flags, TRequestStatus& aStatus);
   511 	 void SendTo(TDes8& aDesc, TSockAddr& to, int flags, TRequestStatus& aStatus);
   512 	 TInt Poll(TUint aEvents);
   513 	 TInt NotifyActivity(TUint aEvents, TRequestStatus& aRequest, TTimeIntervalMicroSeconds32 aTimeout);
   514 	 void TweakWatchedEvents(TUint& events);
   515 	 TInt TweakReadyEvents(TInt errval);
   516 	 void CancelNotify();
   517 	 TInt Listen(TUint qSize);
   518 	 void ReadCancel();
   519 	 TInt ReadCompletion(TDes8& aBuf, TInt aStatus);
   520 	 void RecvFromCancel();
   521 	 void SendToCancel();
   522 	 void WriteCancel();
   523 	 TInt SockName(int anEnd, TSockAddr& anAddr);
   524 	 void Shutdown(TUint aHow,TRequestStatus& aStatus);
   525 	 TInt CompletionStatus(TInt& aLength, TInt aStatus);
   526 	 const TUint& GetSelectEvents()
   527 		 {
   528 		 iIoctlLock.Signal();
   529 		 return iSelectEvents();
   530 		 }
   531 	 
   532 	 inline TInt CreateLock() 
   533 		 {
   534 	     TInt err = iIoctlLock.CreateLocal(1) || iReadLock.CreateLocal() || iWriteLock.CreateLocal();
   535 	     if (err != KErrNone)
   536 	         {
   537              // closes on unopened handles are safe
   538              iIoctlLock.Close();
   539              iReadLock.Close();
   540              iWriteLock.Close();
   541 	         }
   542 	     return err;
   543 		 }
   544 		 
   545 	 inline RSemaphore& GetIoctlLock()
   546 		 {
   547 		 return iIoctlLock;
   548 		 }
   549 	 inline TBool GetConnectionProgress()
   550 		 {
   551 		 return iConnectInProgress;
   552 		 }
   553 
   554 	 inline void SetConnectionProgress( TBool aInProgress )
   555 		 {
   556 		 iConnectInProgress = aInProgress;
   557 		 }
   558 	 
   559 protected:
   560 		//rearrange these 
   561 	CSockDescBase():iAddrFamily(-1),iProtocol(KUndefinedProtocol),iIoctlFlag(EFalse),iCount(0)
   562 	{
   563 	
   564 	}	
   565 	RSocket iSocket;
   566 	TSockXfrLength iLength;
   567 	TFileName iPath;
   568 	TPckgBuf<TUint> iSelectEvents;
   569 	TInt iAddrFamily;
   570 	TInt iProtocol;
   571 	TInt iStyle;
   572 	// For preventing simultaneous ioctl calls.
   573 	// No native support.
   574 	RSemaphore iIoctlLock;
   575 	// Is this required?
   576 	TBool iIoctlFlag;
   577 	// Safeguard against issue of a second Read/Write request while the first is pending.
   578 	// The iReadLock also guards OpenUsingPreference
   579 	//For locking the descriptor before any operation.
   580 	//To make it thread safe.
   581 	RFastLock iReadLock;
   582 	RFastLock iWriteLock;
   583 
   584 	//Flag to mark the connect status of a non-blocking socket as "in progress"
   585 	//to prevent duplicate connection request
   586 	TBool iConnectInProgress;
   587 	volatile TInt iCount;
   588 protected:
   589 
   590 	inline TInt isStream() const	// inline, but private
   591 		{ 
   592 		return iStyle==SOCK_STREAM; 
   593 		}
   594 	};
   595 
   596 NONSHARABLE_CLASS(CSocketDesc) : public CSockDescBase
   597 /*
   598 Abstraction for a socket
   599 @internalComponent
   600 */
   601 	{
   602 public:
   603 	CSocketDesc();
   604 	TInt Socket(RSocketServ& aSs, int family, int style, int protocol);
   605 	void Read(TDes8& aDesc, TRequestStatus& aStatus);
   606 	TInt ReadCompletion (TDes8& /*aBuf*/, TInt aStatus);	
   607 	void ReadCancel();
   608 	void Write(TDes8& aDesc, TRequestStatus& aStatus);
   609 	void WriteCancel();
   610 	void Sync(TRequestStatus& aStatus);
   611 	void Ioctl(int aCmd, void* aParam, TRequestStatus& aStatus);
   612 	TInt IoctlCompletion(int aCmd, void* aParam, TInt aStatus);
   613 	void IoctlCancel();
   614 
   615 	void RecvFrom(TDes8& aDesc, TSockAddr& from, int flags, TRequestStatus& aStatus);
   616 	void RecvFromCancel();
   617 	void SendTo(TDes8& aDesc, const struct sockaddr* anAddr, unsigned long aAddrLen, int flags, TRequestStatus& aStatus);
   618 
   619 	void SendToCancel();
   620 
   621 	void Shutdown(TUint aHow,TRequestStatus& aStatus);
   622 	void Accept(CFileDescBase*& aNewSocket, TRequestStatus& aStatus, RSocketServ& aSs,TSockAddr *aAddr);
   623 	void AcceptCancel();
   624 	void Connect(const struct sockaddr* aAddr,unsigned long size,TRequestStatus& aStatus);
   625 	void ConnectCancel();
   626 
   627 	TInt Bind(const struct sockaddr* addr, unsigned long size);
   628 	TInt Listen(TUint qSize);
   629 	TInt SockName(int anEnd, struct sockaddr* anAddr,unsigned long* aSize);
   630 	TInt GetSockOpt(TInt anOptionName,TInt anOptionLevel,TDes8& anOption);
   631 	TInt SetSockOpt(TInt anOptionName,TInt anOptionLevel,TDesC8& anOption);
   632 	TInt Fcntl(TUint anArg, TUint aCmd);
   633 	
   634 	TInt Poll(TPollMode aMode,TBool& aReadyStatus,TInt& aErrno);
   635 	TInt Poll(TUint aEvents);
   636 	void CancelNotify();
   637 	void TempClose();
   638 	
   639 	void SetFids(void *aFids);
   640 	virtual TInt Type()
   641 	    {
   642 	    return ESocketDesc;
   643 	    }
   644 	
   645 protected:
   646 	TInt FinalClose();
   647 private:
   648 	
   649     TInt GetInterfaceIndex(TUint32 aAddr);
   650 	TInt GetInterfaceList(void *aParam);
   651 	TInt GetActiveInterface( void *aParam);
   652 	TInt GetInterfaceIndexByName(void *aParam);
   653 	TInt GetActiveInterfaceList(void *aParam);
   654 	TInt SetInterfaceByName(void *aParam);
   655 	TInt StartInterface(void *aParam);
   656 	TInt StartActiveInterface(void *aParam);
   657 	TInt StopInterface(void *aParam);
   658 	TInt OpenUsingPreference();
   659 	void AccessPointListL(CArrayFixFlat<TAccessPointRecord> *&aRecordPtr, TInt &aLength);
   660 	void AccessPointCountL(TInt &aCount);
   661 	void ReadRecordFromIapTableL(CCommsDbTableView* aView, TAccessPointRecord &aRecord);
   662 	void OpenIapTableLC(CCommsDatabase **aIapDatabase, CCommsDbTableView **aView);
   663 	TInt ActiveConnectionCount(TInt &aCount);
   664 	TInt ActiveConnectionListL(CArrayFixFlat<TAccessPointRecord> *aRecordPtr, TInt &aLength);
   665 	TInt GetRConnectionDetails(RConnection *aRc, TAccessPointRecord &aApr);
   666 	TInt StartConnection(void *aParam);
   667 	TInt GetConnectionPreference(TCommDbConnPref &aApr);
   668 	void GetIapDetailsByNameL(TBuf<KCommsDbSvrMaxColumnNameLength> aIapName, TAccessPointRecord &aRecord);
   669 	void StopSubConnection();
   670 	void StopConnection();
   671 	TInt StartSubConnection(void *aParam);
   672 	TInt GetInterface(void *Param, TInt aType);
   673 	TInt GetInterafceNumber(void *aParam);
   674 	TInt GetIpAddress( void *aParam );
   675 	
   676 	TInt RouteRequest(TInt aReq, void *aParam);
   677 	TInt GetInterfaceHWAddress(void *aParam);
   678 	
   679 	TInt ConvertRtEntry(TSoInetRouteInfo& aRouteInfo, struct rtentry *aRouteEntry);
   680 	TInt ConvertSockAddr(TInetAddr& aInetAddr, struct sockaddr_in *aSockAddr);	
   681 		
   682 	TInt GetInterfaceByName(const TDesC& aIfName, TPckgBuf<TSoInetInterfaceInfo>& aIface);
   683 	TInt Copy(TInetAddr& aDest, TInetAddr& aSrc);
   684 	
   685 	TInt GetRemoteIpAddress( void *aParam );
   686 	TInt GetInterafceParamInfo( void *aParam,TInt aType);
   687 	void FindConnectionDetailsL(CArrayFixFlat<TAccessPointRecord>*& aRecordPtr, TInt& aCount);    
   688 	void FindConnectionInfoL(TAccessPointRecord &aRecord,char *ptr);
   689 	TInt GetInterfaceDetails( void *aParam ,TInt aFlag, TInt aType );
   690 	TInt SetInterafceParamInfo( void *aParam,TInt aType);
   691 	TInt SetInterfaceDetails( void *aParam ,TInt aFlag, TInt aType );
   692 	
   693 	TInt maybe_reopen_socket()
   694 	    {
   695 	    TInt err = KErrNone;
   696 	    if (!__e32_atomic_load_acq32(&iSocketPtr))
   697 	        {
   698             iReadLock.Wait();
   699             if (!iSocketPtr)
   700                 {
   701                 err = OpenUsingPreference();
   702                 }
   703             iReadLock.Signal();
   704 	        }
   705 	    return err;
   706 	    }
   707 	
   708 	enum InterfaceType
   709 		{
   710 		EACCESS_POINT,
   711 		EACTIVE_CONNECTION,
   712 		EACTIVE_GETIP,
   713 		EACCESS_GETMETRIC,
   714 		EACCESS_GETMTU,
   715 		EACCESS_GETNETMASK,
   716 		EACCESS_GETBROADCAST,
   717 		EACCESS_GETPHYSADDR,
   718 		EACCESS_GETFLAGS,
   719 		EACCESS_SETMETRIC,
   720 		EACCESS_SETMTU,
   721 		EACCESS_SETFLAGS,
   722 		EACCESS_SETPHYSADDR,
   723 		EACTIVE_SETIP,
   724 		EACCESS_SETNETMASK,
   725 		EACCESS_SETBROADCAST
   726 		};
   727 
   728 	TPtr8 iIoctlBuf;
   729 	RSocket *iSocketPtr;
   730 	TAccessPointRecord iConnPref;
   731 	RConnection iConnection;
   732 	RConnection *iConnectionPtr;
   733 	RSubConnection iSubConnection;
   734 	RSubConnection *iSubConnectionPtr;
   735 	TInt iRConnectionIndex;
   736 	RSocketServ *iSockServPtr;
   737 	void *iFids;
   738 
   739 	};
   740 
   741 
   742 class CNotifier;
   743 
   744 NONSHARABLE_CLASS(CSerialDesc) : public CFileDescBase
   745 /*
   746 @internalComponent
   747 */
   748 	{
   749 
   750 	friend class CNotifier;
   751 
   752 	public:
   753 	CSerialDesc() : CFileDescBase(), iReadThreshold(-1), iRequestedSignals(0),
   754 					iNotifyStatus(NULL), iNotifyParamPtr(NULL) {}
   755 
   756 	TInt Open(const wchar_t* name, int mode, int perms);
   757 	TInt FinalClose();
   758 
   759 	void Read(TDes8& aDesc, TRequestStatus& aStatus);
   760 	void ReadCancel();
   761 	
   762 	TInt& TimeoutValue() const;
   763 	void Write(TDes8& aDesc, TRequestStatus& aStatus);
   764 	TInt WriteCompletion(TDes8& aDesc, TInt aStatus);
   765 
   766 	
   767 	void Ioctl(int aCmd, void* aParam, TRequestStatus& aStatus);
   768 	TInt IoctlCompletion(int aCmd, void* aParam, TInt aStatus);
   769 	void IoctlCancel();
   770 
   771 	TBool TimedRead();
   772 	void UserClose();
   773 	virtual TInt Type()
   774 	    {
   775 	    return  ESerialDesc;
   776 	    }
   777 
   778 protected:
   779 
   780 private:
   781 
   782 	void NotifyDataAvailable(TRequestStatus& aStatus);
   783 	void NotifyOutputEmpty(TRequestStatus& aStatus);
   784 	void NotifyBreak(TRequestStatus& aStatus);
   785 	void NotifyWriteErrors(TRequestStatus& aStatus, TUint* aRequestParams, TUint aSignalsMask);
   786 	void NotifySignalChange(TRequestStatus& iStatus, TUint& aRequestParam, TUint aSignalsMask);
   787 	void NotifyDataAvailableCancel();
   788 	void NotifyOutputEmptyCancel();
   789 	void NotifyBreakCancel();
   790 	void NotifyWriteErrorsCancel();
   791 	void NotifySignalChangeCancel();
   792 	void Notify(TInt aVal);		//complete the notify request
   793 	TInt NotifiesSupported();	//return the notifies supported at the moment
   794 	TBool RequestedNotifiesSupported(TInt aRequested);	//see if the notifies requested are supported
   795 	void CancelNotifiers(const CNotifier* aCompletedNotifier);	//cancel them all apart from the passed one
   796 	TUint Signals();
   797 
   798 	TInt ReadCompletion (TDes8& aBuf, TInt aStatus);	
   799 
   800 	TInt iReadThreshold;
   801 	RComm iCommPort;
   802 
   803 	TInt iRequestedSignals;
   804 	TRequestStatus* iNotifyStatus;
   805 	TUint* iNotifyParamPtr;
   806 	TUint* iRequestDataPtr;
   807 
   808 	CNotifier* iDataAvailableNotifier;
   809 	CNotifier* iOutputEmptyNotifier;
   810 	CNotifier* iBreakNotifier;
   811 	CNotifier* iErrorsNotifier;
   812 	CNotifier* iSignalsNotifier;
   813 
   814 	};
   815 
   816 	
   817 NONSHARABLE_CLASS(CPipeDescBase) : public CFileDescBase
   818 /*
   819 Base class with the RPipe handle and common methods
   820 @internalComponent
   821 */
   822 	{
   823 public:
   824 	CPipeDescBase(const TUint16 aMode = 0) : CFileDescBase()
   825 		{
   826 		iFdAttrib |= KPipeFd;
   827 		if (aMode & O_NONBLOCK)
   828 			{
   829 			iFcntlFlag |= O_NONBLOCK;
   830 		}
   831 		}
   832 		
   833 	TInt Fcntl(TUint anArg, TUint aCmd);
   834 	TInt FStat(struct stat *st);
   835 	void Ioctl(int aCmd, void* aParam, TRequestStatus& aStatus);
   836 	TInt FinalClose();
   837 	
   838 	RPipe& Handle()
   839 		{
   840 		return iHandle;
   841 		}
   842 		
   843 	TUint& FcntlFlag()
   844 		{
   845 		return iFcntlFlag;
   846 		}
   847 		
   848 	TUint32& FDAttrib()
   849 		{
   850 		return iFdAttrib;
   851 		}
   852 	virtual TInt Type()
   853 	    {
   854 	    return EPipeDesc;
   855 	    }
   856 
   857 protected:
   858 	RPipe iHandle;
   859 	};
   860 	
   861 NONSHARABLE_CLASS(CPipeReadDesc) : public CPipeDescBase
   862 /*
   863 Handle to the read-end of an RPipe object.
   864 @internalComponent
   865 */
   866 	{
   867 public:
   868 	CPipeReadDesc(const TUint16 aMode = 0) : CPipeDescBase(aMode)
   869 		{
   870 		iFcntlFlag |= O_RDONLY;
   871 		}
   872 		
   873 	void Read(TDes8& aDesc, TRequestStatus& aStatus);
   874 	void Write(TDes8& aDesc, TRequestStatus& aStatus);
   875 	TInt Poll(TUint aEvents);
   876 	TInt NotifyActivity(TUint aEvents, TRequestStatus& aRequest, TTimeIntervalMicroSeconds32 /*timeout*/);
   877 	TInt TweakReadyEvents(TInt errval);
   878 	void CancelNotify();
   879 	
   880 protected:
   881 	TInt DoRead(TDes8& aDesc);
   882 	};
   883 	
   884 NONSHARABLE_CLASS(CPipeWriteDesc) : public CPipeDescBase
   885 /*
   886 Handle to the write-end of an RPipe object.
   887 @internalComponent
   888 */
   889 	{
   890 public:
   891 	CPipeWriteDesc(const TUint16 aMode = 0) : CPipeDescBase(aMode)
   892 		{
   893 		iFcntlFlag |= O_WRONLY;
   894 		}
   895 		
   896 	void Write(TDes8& aDesc, TRequestStatus& aStatus);
   897 	void Read(TDes8& aDesc, TRequestStatus& aStatus);
   898 	TInt Poll(TUint aEvents);
   899 	TInt NotifyActivity(TUint aEvents, TRequestStatus& aRequest, TTimeIntervalMicroSeconds32 timeout);
   900 	TInt TweakReadyEvents(TInt errval);
   901 	void CancelNotify();
   902 	
   903 protected:
   904 	TInt DoWrite(TDes8& aDesc);
   905 	};
   906 NONSHARABLE_CLASS(CFileSocketDesc) : public CSockDescBase
   907 /*
   908 Class representing AF_UNIX socket descriptors
   909 Implemented using 2 FIFOs
   910 @internalComponent
   911 */
   912 	{
   913 public:
   914 	CFileSocketDesc(const TUint16 aMode = 0) 
   915 		{
   916 		if (aMode & O_NONBLOCK)
   917 			{
   918 			iFcntlFlag |= O_NONBLOCK;
   919 			}
   920 		iFcntlFlag |= O_RDWR;
   921 		iPath.Zero();
   922 		}
   923 		
   924 	void Ioctl(int aCmd, void* aParam, TRequestStatus& aStatus);
   925 	TInt FinalClose();
   926 	void Read(TDes8& aDesc, TRequestStatus& aStatus);
   927 	void Write(TDes8& aDesc, TRequestStatus& aStatus);
   928 	void RecvFrom(TDes8& aDesc, TSockAddr& from, int flags, TRequestStatus& aStatus);
   929 	void SendTo(TDes8& aDesc, const struct sockaddr* anAddr, unsigned long aAddrLen, int flags, TRequestStatus& aStatus);
   930 	TInt Listen(TUint qSize);
   931 	TInt GetSockOpt(TInt anOptionName,TInt anOptionLevel,TDes8& anOption);
   932 	TInt SetSockOpt(TInt anOptionName,TInt anOptionLevel,TDesC8& anOption);
   933 	TInt Bind(const struct sockaddr* addr, unsigned long size);
   934 	void Connect(const struct sockaddr* aAddr,unsigned long size, TRequestStatus& aStatus);
   935 	void Accept(CFileDescBase*& aNewSocket, TRequestStatus& aStatus, RSocketServ& aSs, TSockAddr *);
   936 	TInt Socket(RSocketServ& aSs, int family, int style, int protocol);
   937 	TInt SockName(int anEnd, struct sockaddr* anAddr,unsigned long* aSize);
   938 	void AcceptCancel();
   939 	void ConnectCancel();
   940 
   941 	TUint& FcntlFlag()
   942 		{
   943 		return iFcntlFlag;
   944 		}
   945 		
   946 	TUint32& FDAttrib()
   947 		{
   948 		return iFdAttrib;
   949 		}
   950 	virtual TInt Type()
   951 	    {
   952 	    return EFileSocketDesc;
   953 	    }
   954 
   955 	TInt GetLocalSockAddrByPort(struct sockaddr_un* aAddr,unsigned long* aAddrLen,TUint aPortNum);
   956 	TInt GetLocalSockPortByPath(const struct sockaddr_un* aAddr,unsigned long aAddrLen,TUint& aPortNum);
   957 
   958 private:
   959 	TInt ValidateAddress(const struct sockaddr_un* aAddr,unsigned long* aAddrLen);
   960 	TInt RemoveLocalSockAddr();
   961 private:
   962 	TBuf8<KMaxFileName> iPath;
   963 	RFastLock iReadLock;
   964 	RFastLock iWriteLock;
   965 	class TAutoFastLock
   966 		{
   967 	public:
   968 		TAutoFastLock(RFastLock &aLock):iAutoLock(aLock)
   969 			{
   970 			iAutoLock.Wait() ;
   971 			}
   972 		~TAutoFastLock()
   973 			{
   974 			iAutoLock.Signal() ;
   975 			}
   976 	private:
   977 		RFastLock &iAutoLock;
   978 		};
   979 	};
   980 	
   981 NONSHARABLE_CLASS(CDirectoryDesc) : public CFileDescBase
   982 /*
   983 Abstractions for a directory 
   984 @internalComponent
   985 */
   986 	{
   987 public:
   988 	CDirectoryDesc();
   989 	~CDirectoryDesc();
   990 
   991 	TInt Open(RFs& aSession, const TDesC& aName, int mode, int perms);
   992 	void Read(TDes8& aDesc, TRequestStatus& aStatus);
   993 	void Write(TDes8& aDesc, TRequestStatus& aStatus);
   994 	TInt Fcntl(TUint anArg, TUint aCmd);
   995 	TInt LSeek(off_t& offset, int whence);
   996 	TInt FStat(struct stat *st);
   997 	//Get the Name of the directory
   998 	inline const wchar_t* GetDirName()
   999 		{
  1000 		return (const wchar_t*)iDirName.Ptr();
  1001 		}
  1002 	virtual TInt Type()
  1003 	    {
  1004 	    return EDirDesc;
  1005 	    }
  1006 
  1007 private:
  1008 	RDir	iDir;
  1009 	//Name of the directory
  1010 	TFileName iDirName;
  1011 	};
  1012 
  1013 #ifdef SYMBIAN_OE_LIBRT
  1014 NONSHARABLE_CLASS(CSharedMemDesc) : public CFileDescBase
  1015 /*
  1016 Abstractions for shared memory 
  1017 @internalComponent
  1018 */
  1019 	{
  1020 public:
  1021 	CSharedMemDesc();
  1022 	~CSharedMemDesc();
  1023 
  1024 	TInt Open(const wchar_t* aName, int mode, int perms);
  1025 	void Read(TDes8& aDesc, TRequestStatus& aStatus);
  1026 	void Write(TDes8& aDesc, TRequestStatus& aStatus);
  1027 	TInt Fcntl(TUint anArg, TUint aCmd);
  1028 	TInt FStat(struct stat *st);
  1029 	TInt LSeek(off_t& offset, int whence);
  1030 private:
  1031 	TInt ShmRead(TUint8* aPtr,TInt aLength);
  1032 	TInt ShmWrite(TUint8* aPtr,TInt aLength);
  1033 	TInt DoShmRead(TDes8& aDesc);
  1034 	TInt DoShmWrite(TDes8& aDesc);
  1035 	TInt Pos();
  1036 	TInt Ext();	
  1037 protected:
  1038 	TInt FinalClose();	
  1039 private:
  1040 	RFastLock iLock;	
  1041 	void* iPtr;
  1042 	TInt iSize;
  1043 	TInt iPos;
  1044 	TInt iExt;
  1045 	TInt iPerms;
  1046 	TInt iKey;
  1047 	};
  1048 #endif //SYMBIAN_OE_LIBRT
  1049 #endif // !_FDESC_H