multimediacommsengine/tsrc/testdriver/testclient/filehandler/src/RTcFileHandler.cpp
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 2004 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Implementation
       
    15 *
       
    16 */
       
    17 
       
    18 #include "RTcFileHandler.h"
       
    19 #include "FileHandlerConstants.h"
       
    20 
       
    21 EXPORT_C RTcFileHandler::RTcFileHandler()
       
    22 	: iIsConnected( EFalse )
       
    23 	{
       
    24 	}
       
    25 
       
    26 EXPORT_C void RTcFileHandler::Close()
       
    27 	{
       
    28 	// Close the session if we are connected
       
    29 	if( iIsConnected )
       
    30 		{
       
    31 		SendReceive( ECloseSession, TIpcArgs() );
       
    32 				
       
    33 		RHandleBase::Close();
       
    34 		iIsConnected = EFalse;
       
    35 		}
       
    36 	}
       
    37 
       
    38 EXPORT_C TInt RTcFileHandler::Connect()
       
    39 	{
       
    40 	// Are we already connected?
       
    41 	if( iIsConnected )
       
    42 		{
       
    43 		return KErrAlreadyExists;
       
    44 		}
       
    45 
       
    46 	// Try starting the server, it is safe
       
    47 	// to call StartServer() even if a server is already running.
       
    48 	TRAPD( status, StartServerL() );
       
    49 
       
    50 	// If server was started (or was already running),
       
    51 	// establish a session to it.
       
    52 	if( status == KErrNone )
       
    53 		{
       
    54 		status = CreateSession( KTcFileHandlerName,
       
    55 								Version(),
       
    56 								KTcFileHandlerMessageSlots );
       
    57 
       
    58 		// if connection was established succesfully, go configure the session.
       
    59 		if( status == KErrNone )
       
    60 			{
       
    61 			iIsConnected = ETrue;
       
    62 
       
    63 			return SendReceive( EConfigAndStart,
       
    64 								TIpcArgs() );
       
    65 			}
       
    66 		}
       
    67 
       
    68 	return status;
       
    69 	}
       
    70 
       
    71 EXPORT_C TInt RTcFileHandler::CreateFile( 
       
    72     const TDesC8& aDestinationPath, 
       
    73     const TDesC8& aData )
       
    74 	{
       
    75 	return SendReceive( ECreateFile, TIpcArgs( &aDestinationPath, &aData ) );
       
    76 	}
       
    77 	
       
    78 EXPORT_C TInt RTcFileHandler::CopyFile( 
       
    79     const TDesC8& aSourcePath,
       
    80     const TDesC8& aDestinationPath )
       
    81     {
       
    82     return SendReceive( ECopyFile, TIpcArgs( &aSourcePath, &aDestinationPath ) );
       
    83     }
       
    84 	
       
    85 EXPORT_C TInt RTcFileHandler::DeleteFile( const TDesC8& aDestinationPath )
       
    86 	{
       
    87 	return SendReceive( EDeleteFile, TIpcArgs( &aDestinationPath ) );
       
    88 	}
       
    89 
       
    90 EXPORT_C TVersion RTcFileHandler::Version() const
       
    91 	{
       
    92 	TVersion version( KTcFileHandlerMajorVersion,
       
    93 					  KTcFileHandlerMinorVersion,
       
    94 					  KTcFileHandlerBuildVersion );
       
    95 	return version;
       
    96 	}
       
    97 
       
    98 void RTcFileHandler::StartServerL()
       
    99 	{
       
   100 	if( !IsServerStarted() )
       
   101 		{
       
   102 		// Create a global semaphore for waiting the server to start
       
   103 		RSemaphore semaphore;
       
   104 		TInt status = semaphore.OpenGlobal( KTcFileHandlerName );
       
   105 		if( status == KErrNotFound )
       
   106 			{
       
   107 			User::LeaveIfError( semaphore.CreateGlobal( KTcFileHandlerName, 0 ) );
       
   108 			}
       
   109 		CleanupClosePushL( semaphore );
       
   110 
       
   111 		// We need to create a thread on WINS and a new process on ARMI/THUMB.
       
   112 #if ( defined (__WINS__) && !defined (EKA2) )
       
   113 
       
   114 		// Load the server dll
       
   115 		RLibrary library;
       
   116 		User::LeaveIfError( library.Load( KTcFileHandlerName,
       
   117 							   TUidType( KNullUid, KNullUid, KNullUid ) ) );
       
   118 		CleanupClosePushL( library );
       
   119 
       
   120 		// Look up the entry point function (always at ordinal #1)
       
   121 		TLibraryFunction first = library.Lookup( 1 );
       
   122 		TThreadFunction entry = reinterpret_cast< TThreadFunction >( first() );
       
   123 		if( !entry )
       
   124 			{
       
   125 			User::Leave( KErrBadLibraryEntryPoint );
       
   126 			}
       
   127 
       
   128 		// We have to simulate the process with a thread on WINS
       
   129 		RThread server;
       
   130 		User::LeaveIfError( server.Create( KTcFileHandlerName,
       
   131 										   entry,
       
   132 										   KDefaultStackSize,
       
   133 										   NULL,
       
   134 										   &library,
       
   135 										   NULL,
       
   136 										   0x4000,
       
   137 										   0x100000,
       
   138 										   EOwnerProcess
       
   139 										   ) );
       
   140         
       
   141 										   
       
   142 		// If successful, server thread has handle to library now
       
   143 		CleanupStack::PopAndDestroy();	// library
       
   144 
       
   145 #else
       
   146 		// Load the server executable.
       
   147 		// Path (\System\Programs or \sys\bin) and extension (.exe) are added automatically
       
   148 		RProcess server;
       
   149 		User::LeaveIfError( server.Create( KTcFileHandlerName, 
       
   150 		                                   KNullDesC, 
       
   151 		                                   TUidType( KNullUid, KNullUid, KNullUid ) ) );
       
   152 #endif
       
   153 
       
   154 		CleanupClosePushL( server );
       
   155 
       
   156 		// Start executing the server.
       
   157 		server.Resume();
       
   158 		// Wait until it has initialized.
       
   159 		semaphore.Wait();
       
   160 
       
   161 		// Close handles
       
   162 		CleanupStack::PopAndDestroy( 2 );	// server, semaphore
       
   163 		}
       
   164 
       
   165 	}
       
   166 
       
   167 TBool RTcFileHandler::IsServerStarted() const
       
   168 	{
       
   169 	TFindServer findServer( KTcFileHandlerName );
       
   170 	TFullName name;
       
   171 	return ( findServer.Next( name ) == KErrNone );
       
   172 	}