filesystemuis/memscaneng/clientsrc/memscanclient.cpp
changeset 0 6a9f87576119
equal deleted inserted replaced
-1:000000000000 0:6a9f87576119
       
     1 /*
       
     2 * Copyright (c) 2006-2006 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:  Memory Scan Client
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // SYSTEM INCLUDES
       
    21 #include <e32svr.h>
       
    22 #include <s32mem.h> // RBufReadStream
       
    23 
       
    24 // USER INCLUDES
       
    25 #include "memscanclient.h"
       
    26 #include "memscanutils.h" // traces
       
    27 
       
    28 
       
    29 // Standard server startup code
       
    30 // 
       
    31 static TInt StartServer()
       
    32 	{
       
    33 	TRACES( RDebug::Print(_L("MemScanClient: Starting server...")) );
       
    34   
       
    35 	RProcess server;
       
    36 	TInt r=server.Create(KMemScanServImg,KNullDesC);
       
    37 
       
    38 	if (r!=KErrNone)
       
    39 		{
       
    40 		TRACES( RDebug::Print(_L("MemScanClient: server start failed %d"),r) );
       
    41 		return r;
       
    42 		}
       
    43 	TRequestStatus stat;
       
    44 	server.Rendezvous(stat);
       
    45 	if (stat!=KRequestPending)
       
    46 		server.Kill(0);		// abort startup
       
    47 	else
       
    48 		server.Resume();	// logon OK - start the server
       
    49 	TRACES( RDebug::Print(_L("MemScanClient: Started")) );
       
    50 	User::WaitForRequest(stat);		// wait for start or death
       
    51 	// we can't use the 'exit reason' if the server panicked as this
       
    52 	// is the panic 'reason' and may be '0' which cannot be distinguished
       
    53 	// from KErrNone
       
    54 	r=(server.ExitType()==EExitPanic) ? KErrGeneral : stat.Int();
       
    55 	server.Close();
       
    56 	return r;
       
    57 	}
       
    58 
       
    59 EXPORT_C RMemScanClient::RMemScanClient()
       
    60 : iScanEventPackagePointer( NULL, 0, 0 )
       
    61     {
       
    62     }
       
    63     
       
    64 
       
    65 // This is the standard retry pattern for server connection
       
    66 EXPORT_C TInt RMemScanClient::Connect()
       
    67 	{
       
    68 	TVersion version(KMemScanServMajor, KMemScanServMinor, KMemScanServBuild);
       
    69 	TInt retry=2;
       
    70 	for (;;)
       
    71 		{
       
    72 		TInt r=CreateSession(KMemScanServName, version, 1);
       
    73 		if (r!=KErrNotFound && r!=KErrServerTerminated)
       
    74 			return r;
       
    75 		if (--retry==0)
       
    76 			return r;
       
    77 		r=StartServer();
       
    78 		if (r!=KErrNone && r!=KErrAlreadyExists)
       
    79 			return r;
       
    80 		}
       
    81 	}
       
    82 	
       
    83 EXPORT_C void RMemScanClient::Close()
       
    84 	{
       
    85 	RSessionBase::Close();  //basecall
       
    86 	}
       
    87 
       
    88 
       
    89 // ***************************************************************************
       
    90 // Client Server functions
       
    91 // ***************************************************************************
       
    92 
       
    93 
       
    94 // ---------------------------------------------------------------------------
       
    95 // RMemScanClient::DataGroupsL()
       
    96 //
       
    97 // ---------------------------------------------------------------------------
       
    98 EXPORT_C CDesCArray* RMemScanClient::DataGroupsL() const
       
    99     {
       
   100     // Ask server to externalize the data group array to buffer
       
   101     // and to provide size of buffer 
       
   102     TPckgBuf<TInt> bufferSize;
       
   103     User::LeaveIfError(SendReceive( EMemScanPrepareDataGroups, TIpcArgs(&bufferSize) ));
       
   104     
       
   105     // Create a buffer of sufficient size in order to fetch the
       
   106 	// buffer from the server
       
   107 	CBufBase* buffer = CBufFlat::NewL( bufferSize() ); //buffer granularity
       
   108 	CleanupStack::PushL( buffer );
       
   109 	buffer->ResizeL( bufferSize() ); //buffer size
       
   110 
       
   111 	// Now fetch the transfer buffer from the server
       
   112 	TPtr8 pBuffer(buffer->Ptr(0));
       
   113 	User::LeaveIfError(SendReceive( EMemScanGetDataGroups, TIpcArgs(&pBuffer) ));
       
   114     
       
   115     // *** Start internalizing the buffer ***
       
   116     
       
   117     RBufReadStream stream(*buffer);
       
   118     CleanupClosePushL(stream);
       
   119 	
       
   120 	// Read the number of data groups from the beginning of the stream
       
   121 	const TInt count = stream.ReadInt32L();
       
   122 	
       
   123     // Create the array for the data group names with appropriate granularity
       
   124 	CDesCArray* dataGroupNameArray = new (ELeave) CDesCArrayFlat(count);
       
   125     CleanupStack::PushL( dataGroupNameArray );
       
   126     
       
   127 	
       
   128 	// Read each group name to array
       
   129     for(TInt i=0; i<count; i++)
       
   130         {
       
   131         // Read number of bytes in varying length descriptor
       
   132         const TInt length = stream.ReadInt32L();
       
   133         // Read datagroup name
       
   134         HBufC* group = HBufC::NewLC( stream, length );
       
   135         dataGroupNameArray->AppendL( *group );
       
   136         CleanupStack::PopAndDestroy( group );
       
   137         }
       
   138     
       
   139     // *** Internalizing done ***    
       
   140     
       
   141     CleanupStack::Pop( dataGroupNameArray );
       
   142     CleanupStack::PopAndDestroy( &stream );   
       
   143     CleanupStack::PopAndDestroy( buffer );
       
   144 
       
   145      
       
   146     return dataGroupNameArray;
       
   147     }       
       
   148 
       
   149 
       
   150 // ---------------------------------------------------------------------------
       
   151 // RMemScanClient::ScanResultL()
       
   152 //
       
   153 // ---------------------------------------------------------------------------
       
   154 EXPORT_C CArrayFix<TInt64>* RMemScanClient::ScanResultL() const
       
   155 	{
       
   156     // Ask server to externalize the scan result array to buffer
       
   157     // and to provide size of buffer
       
   158 	TPckgBuf<TInt> bufferSize;
       
   159 	User::LeaveIfError(SendReceive(EMemScanPrepareScanResults, TIpcArgs( &bufferSize )));
       
   160 	
       
   161 	// Create a buffer of sufficient size in order to fetch the
       
   162 	// buffer from the server
       
   163 	CBufBase* buffer = CBufFlat::NewL( bufferSize() ); //buffer granularity
       
   164 	CleanupStack::PushL( buffer );
       
   165 	buffer->ResizeL( bufferSize() ); //buffer size
       
   166 
       
   167 	// Now fetch the transfer buffer from the server
       
   168 	TPtr8 pBuffer(buffer->Ptr(0));
       
   169 	TIpcArgs args(&pBuffer);
       
   170 	User::LeaveIfError(SendReceive( EMemScanGetScanResults, args ));
       
   171 	
       
   172 	// *** Start internalizing the buffer ***
       
   173 	
       
   174     RBufReadStream stream(*buffer);
       
   175     CleanupClosePushL(stream);
       
   176     
       
   177 	// Read the number of results from the beginning of the stream
       
   178 	const TInt count = stream.ReadInt32L();
       
   179 	
       
   180     // Create the result array with such granularity
       
   181     // that reallocations do not happen
       
   182     CArrayFix<TInt64>* resultArray = new (ELeave) CArrayFixFlat<TInt64>(count);
       
   183     CleanupStack::PushL(resultArray);
       
   184 	
       
   185 	
       
   186     // Read scan results for each data group and write them to result array
       
   187     for(TInt i=0; i<count; i++)
       
   188         {    
       
   189         TInt64 result;
       
   190         stream >> result;
       
   191         resultArray->AppendL(result);
       
   192         }
       
   193         
       
   194     // *** Internalizing done ***        
       
   195     
       
   196     CleanupStack::Pop( resultArray );
       
   197     CleanupStack::PopAndDestroy( &stream );
       
   198     CleanupStack::PopAndDestroy( buffer );
       
   199 	
       
   200 	
       
   201 	return resultArray;
       
   202 	}
       
   203 
       
   204 // ---------------------------------------------------------------------------
       
   205 // RMemScanClient::Scan()
       
   206 //
       
   207 // ---------------------------------------------------------------------------
       
   208 EXPORT_C TInt RMemScanClient::Scan(TDriveNumber aDrive)
       
   209 	{
       
   210 	TInt err = SendReceive( EMemScanStartScan, TIpcArgs( aDrive ) );
       
   211 	return err;
       
   212 	}
       
   213 
       
   214 
       
   215 // ---------------------------------------------------------------------------
       
   216 // RMemScanClient::RequestScanEvents()
       
   217 //
       
   218 // ---------------------------------------------------------------------------
       
   219 EXPORT_C void RMemScanClient::RequestScanEvents( TInt& aError, TRequestStatus& aRequestStatus )
       
   220     {
       
   221 	iScanEventPackagePointer.Set( (TUint8*) &aError, sizeof(TInt), sizeof(TInt) );
       
   222 
       
   223 	TIpcArgs args( &iScanEventPackagePointer );
       
   224 	SendReceive( EMemScanRequestScanEvents, args, aRequestStatus );
       
   225     }
       
   226 
       
   227 
       
   228 // ---------------------------------------------------------------------------
       
   229 // RMemScanClient::RequestScanEventsCancel()
       
   230 //
       
   231 // ---------------------------------------------------------------------------
       
   232 EXPORT_C void RMemScanClient::RequestScanEventsCancel()
       
   233     {
       
   234     // There is nothing client can do if cancelling went wrong, 
       
   235     // so we don't return any error
       
   236 	SendReceive( EMemScanRequestScanEventsCancel );
       
   237     }
       
   238 	
       
   239 
       
   240 // ---------------------------------------------------------------------------
       
   241 // RMemScanClient::ScanInProgress()
       
   242 //
       
   243 // ---------------------------------------------------------------------------	
       
   244 EXPORT_C TBool RMemScanClient::ScanInProgress() const
       
   245     {
       
   246     TBool result = EFalse;
       
   247     TInt err = SendReceive(EMemScanInProgress);
       
   248 	if( err < 0 )
       
   249 	    {
       
   250 	    result = EFalse;
       
   251 	    }
       
   252 	else
       
   253 	    {
       
   254 	    result = static_cast<TBool> (err);
       
   255 	    }
       
   256 	return result;
       
   257     }
       
   258 
       
   259 
       
   260 // End of File