mtpfws/mtpfw/daemon/client/src/rmtpclient.cpp
changeset 0 d0791faffa3f
child 3 8b094906a049
equal deleted inserted replaced
-1:000000000000 0:d0791faffa3f
       
     1 // Copyright (c) 2006-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 //The minumum free RAM for mtp server startup, in Bytes.
       
    17 //If current free RAM is lower than this threhold value, mtp server
       
    18 //will not get started.
       
    19 #define SYMBIAN_MTP_RAM_THRESHOLD                4*1024*1024 //4MB
       
    20 
       
    21 //The minimum free space in phone memory for mtp server startup, in Bytes.
       
    22 //If current free space in phone memory is lower than this threhold value,
       
    23 //mtp server will not get started.
       
    24 #define SYMBIAN_MTP_PHONE_MEMORY_THRESHOLD       512*1024 //512KB
       
    25 
       
    26 //The drive used as phone memory in target device
       
    27 #define SYMBIAN_PHONE_MEMORY_DRIVE_NAME          EDriveC
       
    28 
       
    29 #include <e32cmn.h>
       
    30 #include <e32uid.h>
       
    31 #include <hal.h>  //for HAL to get free RAM
       
    32 #include <f32file.h>  //for RFs to get free disk space in phone memory
       
    33 #include <mtp/rmtpclient.h>
       
    34 #include "mtpclientserver.h"
       
    35 
       
    36 /**
       
    37 Get the free RAM.
       
    38 @param on return, 'aFree' contains the free memory in bytes.
       
    39 @return ErrNone on success, other system-wide error on failure.
       
    40  */
       
    41 static TInt GetFreeRAM(TInt &aFree)
       
    42     {
       
    43     TInt err = HAL::Get(HAL::EMemoryRAMFree,aFree);
       
    44     __ASSERT_DEBUG(err != KErrArgument,User::Invariant());
       
    45     return err;
       
    46     }
       
    47 
       
    48 /**
       
    49 Get the free disk space in phone memory.
       
    50 @param on return,'aFree' contains the free space in bytes.
       
    51 @return ErrNone on success, other system-wide error on failure.
       
    52  */
       
    53 static TInt GetFreeSpaceInPhoneMemory(TInt64 &aFree)
       
    54     {
       
    55     TVolumeInfo info;
       
    56     RFs rfs;
       
    57     TInt err = rfs.Connect();
       
    58     if (err != KErrNone)
       
    59         {
       
    60         return err;
       
    61         }
       
    62 
       
    63     err = rfs.Volume(info,SYMBIAN_PHONE_MEMORY_DRIVE_NAME);
       
    64     aFree = info.iFree;
       
    65     rfs.Close();
       
    66     return err;
       
    67     }
       
    68 
       
    69 /**
       
    70 Starts the MTP daemon server process.
       
    71 @return KErrNone if the MTP daemon server process is started successfully, 
       
    72 otherwise one of the system wide error codes.
       
    73 */
       
    74 
       
    75 LOCAL_D TInt StartServer()
       
    76 	{
       
    77 	const TUidType serverUid(KNullUid, KNullUid, KMTPServerUid3);
       
    78 	
       
    79 	//Firstly, check the free ram
       
    80 	TInt freeRam = 0;
       
    81 	TInt err = GetFreeRAM(freeRam);
       
    82 	
       
    83 	//Ignore the error code and move ahead
       
    84 	if (err == KErrNone && freeRam < SYMBIAN_MTP_RAM_THRESHOLD)
       
    85 	    {
       
    86 	    return KErrNoMemory;
       
    87 	    }
       
    88 	    
       
    89 	//Secondly,check the free disk space in phone memory
       
    90 	TInt64 freeDisk = 0;
       
    91 	err = GetFreeSpaceInPhoneMemory(freeDisk);
       
    92     
       
    93 	//Ignore the error code and move ahead
       
    94 	if (err == KErrNone && freeDisk < SYMBIAN_MTP_PHONE_MEMORY_THRESHOLD)
       
    95 	    {
       
    96 	    return KErrDiskFull;
       
    97 	    }
       
    98 	
       
    99 	// Create the server process.
       
   100 	RProcess server;
       
   101 	TInt ret(server.Create(KMTPServerName, KNullDesC, serverUid));
       
   102 	if (ret == KErrNone)
       
   103 	    {	
       
   104     	TRequestStatus status;
       
   105     	server.Rendezvous(status);
       
   106     	
       
   107     	if (status != KRequestPending)
       
   108     	    {
       
   109     		// Abort the server process.
       
   110     	    server.Kill(status.Int());
       
   111     	    }
       
   112     	else
       
   113     	    {
       
   114     	    // Run the server process.
       
   115     	    server.Resume();
       
   116     	    }
       
   117 		// Wait for the server start up to complete.
       
   118     	User::WaitForRequest(status);
       
   119     	ret = (server.ExitType() == EExitPanic) ? KErrGeneral : status.Int();
       
   120     	server.Close();
       
   121 	    }
       
   122 	    
       
   123 	return ret;
       
   124 	}
       
   125 
       
   126 
       
   127 
       
   128 /**
       
   129 Constructor.
       
   130 */
       
   131 EXPORT_C RMTPClient::RMTPClient() :
       
   132     iVersion(KMTPVersionMajor, KMTPVersionMinor, KMTPVersionBuild)
       
   133     {
       
   134 
       
   135     }
       
   136    
       
   137 /**
       
   138 Closes a connection to the MTP daemon server.
       
   139 */ 
       
   140 EXPORT_C void RMTPClient::Close()
       
   141     {
       
   142     RSessionBase::Close();
       
   143     }
       
   144 	
       
   145 /**
       
   146 Opens a connection to the MTP daemon server.
       
   147 @return KErrNone if the connection is made, otherwise one of the system wide 
       
   148 error codes.
       
   149 */
       
   150 EXPORT_C TInt RMTPClient::Connect()
       
   151 	{	
       
   152 	TInt ret(KErrNone);
       
   153 	const TSecurityPolicy policy(static_cast<TSecureId>(KMTPServerUid3));
       
   154     const static TInt KMaxRetries(3);
       
   155 	for (TInt retry(KMaxRetries); ((retry > 0) && ((ret == KErrNone) || (ret == KErrAlreadyExists))); retry--)
       
   156 		{
       
   157 		ret = CreateSession(KMTPServerName, iVersion, KMTPASyncMessageSlots, EIpcSession_Unsharable, &policy);
       
   158 		
       
   159         if ((ret == KErrNotFound) || (ret == KErrServerTerminated))
       
   160 		    {
       
   161 		    ret = StartServer();
       
   162 		    }
       
   163 		else 
       
   164 			{ 
       
   165 			// Return if there's any err other than KErrNotFound or KErrServerTerminated.
       
   166 			retry = 0;
       
   167 			}
       
   168 		}
       
   169 	return ret;
       
   170 	} 
       
   171 
       
   172 /**
       
   173 Starts up the specified MTP transport protocol.
       
   174 @param aTransport The implementation UID of the transport protocol implemetation.
       
   175 @return KErrNone if the transport is started successfully, otherwise one of the 
       
   176 system wide error codes.
       
   177 */
       
   178 EXPORT_C TInt RMTPClient::StartTransport(TUid aTransport)
       
   179     {
       
   180     TIpcArgs args( static_cast< TInt >( aTransport.iUid ), &KNullDesC8 );
       
   181     return SendReceive( EMTPClientStartTransport, args );
       
   182     }
       
   183 
       
   184 /**
       
   185 Starts up the specified MTP BLUETOOTH transport protocol.
       
   186 @param aTransport The implementation UID of the transport protocol implemetation.
       
   187 @return KErrNone if the transport is started successfully, otherwise one of the 
       
   188 system wide error codes.
       
   189 */
       
   190 EXPORT_C TInt RMTPClient::StartTransport(TUid aTransport, const TDesC8& aParameter)
       
   191     {
       
   192     TIpcArgs args(static_cast<TInt>(aTransport.iUid), &aParameter);
       
   193     return SendReceive(EMTPClientStartTransport,args);
       
   194     }
       
   195  
       
   196 /**
       
   197 Shuts down the specified MTP transport protocol.
       
   198 @param aTransport The implementation UID of the transport protocol implemetation.
       
   199 @return KErrNone if the transport is started successfully, otherwise one of the 
       
   200 system wide error codes.
       
   201 */
       
   202 EXPORT_C TInt RMTPClient::StopTransport(TUid aTransport)
       
   203     {
       
   204     return SendReceive(EMTPClientStopTransport, TIpcArgs(static_cast<TInt>(aTransport.iUid)));
       
   205     }
       
   206 
       
   207 /**
       
   208 Provides the minimum MTP daemon version with which the MTP client API is 
       
   209 compatible.
       
   210 @return The minimum MTP daemon version.
       
   211 */    
       
   212 EXPORT_C const TVersion& RMTPClient::Version() const
       
   213 	{
       
   214     return iVersion;
       
   215 	}
       
   216 
       
   217 /**
       
   218 Checks whether MTP Framework shall allow the StartTransport Command or not.
       
   219 @param aTransport The implementation UID of the transport protocol implementation.
       
   220 @return KErrNone if the MTP FW is available for the given Transport,KErrAlreadyExists- If the same transport is running.
       
   221 KErrServerBusy- If the Transport can not be started.
       
   222 */    
       
   223 EXPORT_C TInt RMTPClient::IsAvailable(TUid aTransport)
       
   224     {
       
   225 	return  SendReceive(EMTPClientIsAvailable, TIpcArgs(static_cast<TInt>(aTransport.iUid)));
       
   226     }
       
   227 
       
   228 /**
       
   229 Checks  whether the mtpserver.exe process is running or not.
       
   230 @returns KErrNone if mtpserver.exe is runnig, KErrNotFound if the process is not running.
       
   231 */        
       
   232 EXPORT_C TInt RMTPClient::IsProcessRunning()
       
   233     {
       
   234 		TFindServer findProc;
       
   235 		findProc.Find(KMTPServerName);
       
   236 		TFullName procName;
       
   237 		return findProc.Next(procName);		
       
   238     
       
   239     }
       
   240