Update contrib.
     1 // Copyright (c) 1999-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".
 
     8 // Initial Contributors:
 
     9 // Nokia Corporation - initial contribution.
 
    21 #include <sys/errno.h>
 
    22 #include <sys/serial.h>
 
    24 // System Interface for thread-local form of STDLIB
 
    27 Two-phase constructor method to create a CLocalSystemInterface instance.
 
    29 CLocalSystemInterface* CLocalSystemInterface::NewL()
 
    31 	CLocalSystemInterface* self = new (ELeave) CLocalSystemInterface();
 
    32 	CleanupStack::PushL(self);
 
    34 	CleanupStack::Pop(self);
 
    41 CLocalSystemInterface::CLocalSystemInterface()
 
    46 Second-phase constructor.
 
    47 Initialises some data members.
 
    48 This function leaves if any system-wide error happens.
 
    50 void CLocalSystemInterface::ConstructL()		
 
    54 	CTtyDesc* console=new CTtyDesc;
 
    55 	User::LeaveIfNull(console);
 
    57 	iFids.Default(console);
 
    60 	User::LeaveIfError(iFs.Connect());
 
    61 	User::LeaveIfError(PosixFilesystem::SetDefaultDir(iFs));
 
    64 CLocalSystemInterface::~CLocalSystemInterface()
 
    66 // Shut down all server connections in use
 
    75 MSystemInterface& CLocalSystemInterface::Clone()
 
    81 void CLocalSystemInterface::Release()
 
    87 void CLocalSystemInterface::TerminateProcess(int status)
 
    90 	RProcess().Terminate(status);
 
    94 // Simple layer over PosixFilesystem
 
    96 wchar_t * CLocalSystemInterface::getcwd (wchar_t* buf, unsigned long len, int& anErrno)
 
    98 	return PosixFilesystem::getcwd(iFs,buf,len,anErrno);
 
   101 int CLocalSystemInterface::chdir (const wchar_t* aPath, int& anErrno)
 
   103 	return PosixFilesystem::chdir(iFs,aPath,anErrno);
 
   106 int CLocalSystemInterface::rmdir (const wchar_t* aPath, int& anErrno)
 
   108 	return PosixFilesystem::rmdir(iFs,aPath,anErrno);
 
   111 int CLocalSystemInterface::mkdir (const wchar_t* aPath, int perms, int& anErrno)
 
   113 	return PosixFilesystem::mkdir(iFs,aPath,perms,anErrno);
 
   116 int CLocalSystemInterface::stat (const wchar_t* name, struct stat *st, int& anErrno)
 
   118 	return PosixFilesystem::stat(iFs,name,st,anErrno);
 
   121 int CLocalSystemInterface::chmod (const wchar_t* name, int perms, int& anErrno)
 
   123 	return PosixFilesystem::chmod(iFs,name,perms,anErrno);
 
   126 int CLocalSystemInterface::unlink (const wchar_t* name, int& anErrno)
 
   128 	return PosixFilesystem::unlink(iFs,name,anErrno);
 
   131 int CLocalSystemInterface::rename (const wchar_t* oldname, const wchar_t* newname, int& anErrno)
 
   133 	return PosixFilesystem::rename(iFs,oldname,newname,anErrno);
 
   137 TInt CLocalSystemInterface::ResolvePath (TParse& aResult, const wchar_t* path, TDes* aFilename)
 
   139 	return PosixFilesystem::ResolvePath(iFs, aResult, path, aFilename);
 
   142 // Simple layer over CFileTable synchronous routines
 
   144 int CLocalSystemInterface::open (const wchar_t* name, int mode, int perms, int& anErrno)
 
   146 	//at this point we need to know if it is a serial port or file
 
   147 	if	((L'C' == name[0]) && (L'O' == name[1]) && (L'M' == name[2]) && (L':' == name[4]) && ((name[3] >= L'1') && (name[3] <= L'9')) ||
 
   148 			(L'I' == name[0]) && (L'R' == name[1]) && (L'C' == name[2]) && (L'O' == name[3]) && (L'M' == name[4]) && (L':' == name[6]) && ((name[5] >= L'1') && (name[5] <= L'9')))
 
   149 		return iFids.open(name,mode,perms,anErrno, iCs);
 
   151 		return iFids.open(name,mode,perms,anErrno, iFs);
 
   154 int CLocalSystemInterface::dup (int fid, int& anErrno)
 
   156 	return iFids.dup(fid,anErrno);
 
   159 int CLocalSystemInterface::dup2 (int fid, int fid2, int& anErrno)
 
   161 	return iFids.dup2(fid,fid2,anErrno);
 
   164 int CLocalSystemInterface::close (int fid, int& anErrno)
 
   166 //	return iFids.close(fid,anErrno);
 
   167 	return iFids.userclose(fid,anErrno);
 
   170 int CLocalSystemInterface::lseek (int fid, int offset, int whence, int& anErrno)
 
   172 	return iFids.lseek(fid,offset,whence,anErrno);
 
   175 int CLocalSystemInterface::fstat (int fid, struct stat *st, int& anErrno)
 
   177 	return iFids.fstat(fid,st,anErrno);
 
   180 int CLocalSystemInterface::socket (int family, int style, int protocol, int& anErrno)
 
   182 	return iFids.socket(family,style,protocol,anErrno,iSs);
 
   185 int CLocalSystemInterface::listen (int fid, int n, int& anErrno)
 
   187 	return iFids.listen(fid,n,anErrno);
 
   190 int CLocalSystemInterface::bind (int fid, struct sockaddr* addr, unsigned long size, int& anErrno)
 
   192 	TUSockAddr address(addr,size);
 
   193 	return iFids.bind(fid,address,anErrno);
 
   196 int CLocalSystemInterface::sockname (int fid, struct sockaddr* addr, unsigned long* size, int anEnd, int& anErrno)
 
   198 	TUSockAddr address(addr);
 
   199 	TInt ret=iFids.sockname(fid,address,anEnd,anErrno);
 
   201 		address.Get(addr,size);
 
   205 int CLocalSystemInterface::getsockopt (int fid, int level, int opt, void* buf, unsigned long* len, int& anErrno)
 
   207 	return iFids.getsockopt(fid,level,opt,buf,len,anErrno);
 
   210 int CLocalSystemInterface::setsockopt (int fid, int level, int opt, void* buf, unsigned long len, int& anErrno)
 
   212 	return iFids.setsockopt(fid,level,opt,buf,len,anErrno);
 
   215 wchar_t* CLocalSystemInterface::getenv (const wchar_t* name)
 
   217 	return iEnv.getenv(name);
 
   220 void CLocalSystemInterface::unsetenv (const wchar_t* name)
 
   225 int CLocalSystemInterface::setenv (const wchar_t* name, const wchar_t* value, int rewrite, int& anErrno)
 
   227 	return iEnv.setenv(name,value,rewrite,anErrno);
 
   231 int CLocalSystemInterface::popen3 (const wchar_t *, const wchar_t *, const wchar_t *, wchar_t**, int [3], int& anErrno)
 
   233 	return MapError(KErrNotSupported,anErrno);
 
   236 int CLocalSystemInterface::waitpid (int, int*, int, int& anErrno)
 
   238 	return MapError(KErrNotSupported,anErrno);
 
   241 // Synchronous layer over CFileTable asynchronous routines
 
   243 int CLocalSystemInterface::read (int fid, char* buf, unsigned long len, int& anErrno)
 
   246 	TBool patchErr = EFalse;
 
   248 	TInt err=iFids.Asynch(fid,f);
 
   251 		TRequestStatus readStatus;
 
   252 		TRequestStatus timerStatus(KRequestPending);
 
   254 		TBool timerRunning = EFalse;
 
   256 		TPtr8 ptr((TText8 *)buf, len);
 
   260 			TTimeIntervalMicroSeconds32 timeout(f->TimeoutValue()*1000);
 
   261 			theTimer.CreateLocal();
 
   262 			theTimer.After(timerStatus, timeout);
 
   263 			timerRunning = ETrue;
 
   266 		f->Read(ptr, readStatus);
 
   268 		User::WaitForRequest(readStatus, timerStatus);
 
   272 			if (timerStatus.Int() != KRequestPending)
 
   274 				//cancel the read and wait for it
 
   276 				patchErr = ETrue;	//report this as a timeout not a cancel!!
 
   277 				User::WaitForRequest(readStatus);
 
   281 				//if the timer was in operation
 
   284 				User::WaitForRequest(timerStatus);
 
   289 		err=f->ReadCompletion(ptr, readStatus.Int());
 
   290 		f->Close();	// balances the Dup() in CFileTable::Asynch()
 
   297 	return MapError(err,anErrno);
 
   300 int CLocalSystemInterface::write (int fid, const char* buf, unsigned long len, int& anErrno)
 
   303 	TInt err=iFids.Asynch(fid,f);
 
   306 		TRequestStatus status;
 
   307 		TPtr8 ptr((TText8 *)buf, len, len);
 
   308 		f->Write(ptr,status);
 
   309 		User::WaitForRequest(status);
 
   310 		err=f->WriteCompletion(ptr, status.Int());
 
   311 		f->Close();	// balances the Dup() in CFileTable::Asynch()
 
   315 	return MapError(err,anErrno);
 
   318 int CLocalSystemInterface::recvfrom (int fid, char* buf, unsigned long len, int flags, struct sockaddr* from, unsigned long* fromsize, int& anErrno)
 
   321 	TInt err=iFids.Asynch(fid,f);
 
   324 		TRequestStatus status;
 
   325 		TPtr8 ptr((TText8 *)buf, len);
 
   326 		TUSockAddr addr(from);
 
   327 		f->RecvFrom(ptr,addr,flags,status);
 
   328 		User::WaitForRequest(status);
 
   330 		err=f->RecvFromCompletion(ret, status.Int());
 
   331 		f->Close();	// balances the Dup() in CFileTable::Asynch()
 
   334 			addr.Get(from,fromsize);
 
   338 	return MapError(err,anErrno);
 
   341 int CLocalSystemInterface::sendto (int fid, const char* buf, unsigned long len, int flags, struct sockaddr* to, unsigned long tosize, int& anErrno)
 
   344 	TInt err=iFids.Asynch(fid,f);
 
   347 		TRequestStatus status;
 
   348 		TPtr8 ptr((TText8 *)buf, len, len);
 
   349 		TUSockAddr addr(to,tosize);
 
   350 		f->SendTo(ptr,addr,flags,status);
 
   351 		User::WaitForRequest(status);
 
   352 		err=f->SendToCompletion(ptr, status.Int());
 
   353 		f->Close();	// balances the Dup() in CFileTable::Asynch()
 
   357 	return MapError(err,anErrno);
 
   360 int CLocalSystemInterface::fsync (int fid, int& anErrno)
 
   363 	TInt err=iFids.Asynch(fid,f);
 
   366 		TRequestStatus status;
 
   368 		User::WaitForRequest(status);
 
   369 		f->Close();	// balances the Dup() in CFileTable::Asynch()
 
   372 	return MapError(err,anErrno);
 
   375 int CLocalSystemInterface::shutdown (int fid, int how, int& anErrno)
 
   378 	TInt err=iFids.Asynch(fid,f);
 
   381 		TRequestStatus status;
 
   382 		f->Shutdown(how,status);
 
   383 		User::WaitForRequest(status);
 
   384 		f->Close();	// balances the Dup() in CFileTable::Asynch()
 
   387 	return MapError(err,anErrno);
 
   390 int CLocalSystemInterface::connect (int fid, struct sockaddr* addr, unsigned long size, int& anErrno)
 
   393 	TInt err=iFids.Asynch(fid,f);
 
   396 		TRequestStatus status;
 
   397 		TUSockAddr address(addr,size);
 
   398 		f->Connect(address,status);
 
   399 		User::WaitForRequest(status);
 
   400 		f->Close();	// balances the Dup() in CFileTable::Asynch()
 
   403 	return MapError(err,anErrno);
 
   406 int CLocalSystemInterface::accept (int fid, int& anErrno)
 
   408 // The CSocketDesc performing the Accept is responsible for creating the new CSocketDesc
 
   412 	TInt err=iFids.Asynch(fid,f);
 
   416 		int fd=iFids.Reserve();
 
   420 			TRequestStatus status;
 
   421 			f->Accept(newf,status,iSs);
 
   422 			CleanupStack::PushL(newf);
 
   423 			User::WaitForRequest(status);
 
   424 			f->Close();	// balances the Dup() in CFileTable::Asynch()
 
   428 				err=iFids.Attach(fd,newf);
 
   431 					CleanupStack::PopAndDestroy(newf);
 
   435 			iFids.Attach(fd,0);	// cancel the reservation
 
   436 			CleanupStack::PopAndDestroy(newf);
 
   439 	return MapError(err,anErrno);
 
   442 int CLocalSystemInterface::ioctl (int fid, int cmd, void* param, int& anErrno)
 
   444 	TRequestStatus ioctlStatus;
 
   445 	TInt err=ioctl(fid,cmd,param,ioctlStatus,anErrno);
 
   448 		User::WaitForRequest(ioctlStatus);
 
   449 		err=ioctl_complete(fid,cmd,param,ioctlStatus,anErrno);
 
   454 // C++ version of asynchronous ioctl
 
   456 int CLocalSystemInterface::ioctl (int fid, int cmd, void* param, TRequestStatus& aStatus, int& anErrno)
 
   459 	TInt err=iFids.Asynch(fid,f);
 
   462 		f->Ioctl(cmd,param,aStatus);
 
   463 		f->Close();	// balances the Dup() in CFileTable::Asynch() - live dangerously!
 
   465 	return MapError(err,anErrno);
 
   468 int CLocalSystemInterface::ioctl_complete (int fid, int cmd, void* param, TRequestStatus& aStatus, int& anErrno)
 
   470 	return iFids.ioctlcomplete(fid,cmd,param,aStatus,anErrno);
 
   473 int CLocalSystemInterface::ioctl_cancel (int fid, int& anErrno)
 
   475 	return iFids.ioctlcancel(fid,anErrno);