deviceencryption/DevEncUi/src/DevEncUiMemoryEntity.cpp
changeset 0 6a9f87576119
equal deleted inserted replaced
-1:000000000000 0:6a9f87576119
       
     1 /*
       
     2 * Copyright (c) 2005 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 of CDevEncUiMemoryEntity.
       
    15 *
       
    16 */
       
    17 
       
    18 #include "DevEncLog.h"
       
    19 #include "DevEncUiMemoryEntity.h"
       
    20 #include "DevEncUiMemInfoObserver.h"
       
    21 
       
    22 #include "DevEncDiskUtils.h"
       
    23 #include "DevEncSession.h"
       
    24 #include <f32file.h> // for TDriveNumer
       
    25 #include <DevEncEngineConstants.h>
       
    26 #include <TerminalControl3rdPartyAPI.h>
       
    27 
       
    28 // Local definitions
       
    29 const TInt KPercent100( 100 );
       
    30 const TTimeIntervalMicroSeconds32 KProgressInterval( 500000 ); // ms, 0.5 seconds
       
    31 const TTimeIntervalMicroSeconds32 KPollInterval( 2000000 ); // ms, 2 seconds
       
    32 
       
    33 // --------------------------------------------------------------------------
       
    34 // CDevEncUiMemoryEntity::CDevEncUiMemoryEntity()
       
    35 // 
       
    36 // --------------------------------------------------------------------------
       
    37 CDevEncUiMemoryEntity::CDevEncUiMemoryEntity(
       
    38                         CEikonEnv* aEikonEnv,
       
    39                         TDevEncUiMemoryType aType )
       
    40     : iEikEnv( aEikonEnv ),
       
    41       iType( aType )
       
    42     {
       
    43     // TBI: Get the real states from the underlying SW levels
       
    44 	iState = EDecrypted;
       
    45     }
       
    46 
       
    47 
       
    48 // --------------------------------------------------------------------------
       
    49 // CDevEncUiMemoryEntity::~CDevEncUiMemoryEntity()
       
    50 // 
       
    51 // --------------------------------------------------------------------------
       
    52 CDevEncUiMemoryEntity::~CDevEncUiMemoryEntity()
       
    53     {
       
    54     if ( iPeriodic )
       
    55         {
       
    56         iPeriodic->Cancel();
       
    57         delete iPeriodic;
       
    58         }
       
    59     iObservers.Close();
       
    60     if ( iSession )
       
    61         {
       
    62         iSession->Close();
       
    63         delete iSession;
       
    64         }
       
    65     delete iDiskStatusObserver;
       
    66     }
       
    67 
       
    68 // --------------------------------------------------------------------------
       
    69 // CDevEncUiMemoryEntity::NewL()
       
    70 // 
       
    71 // --------------------------------------------------------------------------
       
    72 CDevEncUiMemoryEntity* CDevEncUiMemoryEntity::NewL(
       
    73                                 CEikonEnv* aEikonEnv,
       
    74                                 TDevEncUiMemoryType aType )
       
    75 	{
       
    76 	CDevEncUiMemoryEntity* self =
       
    77 	    CDevEncUiMemoryEntity::NewLC( aEikonEnv, aType );
       
    78 	CleanupStack::Pop( self );
       
    79 	return self;
       
    80 	}
       
    81 
       
    82 // --------------------------------------------------------------------------
       
    83 // CDevEncUiMemoryEntity::NewLC()
       
    84 // 
       
    85 // --------------------------------------------------------------------------
       
    86 CDevEncUiMemoryEntity* CDevEncUiMemoryEntity::NewLC(
       
    87                                 CEikonEnv* aEikonEnv,
       
    88                                 TDevEncUiMemoryType aType )
       
    89 	{
       
    90 	CDevEncUiMemoryEntity* self =
       
    91 	    new ( ELeave ) CDevEncUiMemoryEntity( aEikonEnv, aType );
       
    92 	CleanupStack::PushL( self );
       
    93 	self->ConstructL();
       
    94 	return self;
       
    95 	}
       
    96 
       
    97 // --------------------------------------------------------------------------
       
    98 // CDevEncUiMemoryEntity::ConstructL()
       
    99 // 
       
   100 // --------------------------------------------------------------------------
       
   101 void CDevEncUiMemoryEntity::ConstructL()
       
   102 	{
       
   103     DFLOG2( ">>CDevEncUiMemoryEntity::ConstructL, drive = %d", iType );
       
   104 
       
   105     switch( iType )
       
   106         {
       
   107     	case EPhoneMemory: iSession = new ( ELeave ) CDevEncSession( /*EDriveC*/EDriveE );
       
   108                            iDiskStatusObserver = CDiskStatusObserver::NewL( this, /*EDriveC*/EDriveE ); 
       
   109     		               break;
       
   110     		               
       
   111     	case EPrimaryPhoneMemory: iSession = new ( ELeave ) CDevEncSession( EDriveC );
       
   112                           iDiskStatusObserver = CDiskStatusObserver::NewL( this, EDriveC );
       
   113     	    		      break;
       
   114     	    		      
       
   115     	case EMemoryCard: iSession = new ( ELeave ) CDevEncSession( /*EDriveE*/EDriveF );
       
   116                           iDiskStatusObserver = CDiskStatusObserver::NewL( this, /*EDriveE*/EDriveF );
       
   117     	    		      break;    	    		               
       
   118         }
       
   119 
       
   120     // Get initial memory state
       
   121     TInt nfeDiskStatus( EUnmounted );
       
   122 #ifndef __WINS__
       
   123     User::LeaveIfError( iSession->Connect() );
       
   124     TInt err = iSession->DiskStatus( nfeDiskStatus );
       
   125     if ( err )
       
   126         {
       
   127         DFLOG2( "Could not get disk status, error %d", err );
       
   128         nfeDiskStatus = EDecrypted;
       
   129         //User::Leave( err );
       
   130         }
       
   131 #else
       
   132     nfeDiskStatus = EDecrypted;
       
   133 #endif
       
   134     SetState( nfeDiskStatus );
       
   135 
       
   136     if ( ( iState == EEncrypting ) ||
       
   137          ( iState == EDecrypting ) )
       
   138         {
       
   139         // If the app was started in the middle of an ongoing operation,
       
   140         // start polling the progress
       
   141         StartPolling( KProgressInterval );
       
   142         }
       
   143     else
       
   144         {
       
   145         // Otherwise poll every once in a while to see if the status changes
       
   146         //StartPolling( KPollInterval );
       
   147         }
       
   148 
       
   149     DFLOG( "<<CDevEncUiMemoryEntity::ConstructL" );
       
   150 	}
       
   151 
       
   152 // --------------------------------------------------------------------------
       
   153 // CDevEncUiMemoryEntity::AddObserverL()
       
   154 // 
       
   155 // --------------------------------------------------------------------------
       
   156 void CDevEncUiMemoryEntity::AddObserverL( MDevEncUiMemInfoObserver* aInfoObserver )
       
   157     {
       
   158     DFLOG( "CDevEncUiMemoryEntity::AddObserverL" );
       
   159     if ( !aInfoObserver )
       
   160         {
       
   161         User::Leave( KErrArgument );
       
   162         }
       
   163     iObservers.AppendL( aInfoObserver );
       
   164 
       
   165     // The new observer probably wants to know the current states
       
   166     UpdateMemoryInfo();
       
   167     }
       
   168 
       
   169 // --------------------------------------------------------------------------
       
   170 // CDevEncUiMemoryEntity::RemoveObserver()
       
   171 // 
       
   172 // --------------------------------------------------------------------------
       
   173 void CDevEncUiMemoryEntity::RemoveObserver( MDevEncUiMemInfoObserver* aInfoObserver )
       
   174     {
       
   175     if ( !aInfoObserver )
       
   176         {
       
   177         return;
       
   178         }
       
   179     TInt index( iObservers.Find( aInfoObserver ) );
       
   180     if ( index != KErrNotFound )
       
   181         {
       
   182         iObservers.Remove( index );
       
   183         }
       
   184     }
       
   185 
       
   186 // --------------------------------------------------------------------------
       
   187 // CDevEncUiMemoryEntity::StartEncryptionL()
       
   188 // 
       
   189 // --------------------------------------------------------------------------
       
   190 void CDevEncUiMemoryEntity::StartEncryptionL()
       
   191     {
       
   192     DFLOG( ">>CDevEncUiMemoryEntity::StartEncryptionL" );
       
   193 
       
   194 #ifndef __WINS__
       
   195 
       
   196     if ( ! iSession->Connected() )
       
   197         {
       
   198         User::LeaveIfError( iSession->Connect() );
       
   199         DFLOG( "Connected" );
       
   200         }
       
   201 
       
   202     // Make sure the memory is decrypted
       
   203     TInt nfeDiskStatus( EUnmounted );
       
   204     TInt err = iSession->DiskStatus( nfeDiskStatus );
       
   205     if ( err )
       
   206         {
       
   207         DFLOG2( "Could not get disk status, error %d", err );
       
   208         User::Leave( err );
       
   209         }
       
   210     if ( nfeDiskStatus != EDecrypted )
       
   211         {
       
   212         DFLOG2( "Memory is in wrong state (%d), leaving", nfeDiskStatus );
       
   213         User::Leave( KErrNotReady );
       
   214         }
       
   215 
       
   216     // Launch the encryption process
       
   217     err = iSession->StartDiskEncrypt();
       
   218     if ( err )
       
   219         {
       
   220         DFLOG2( "Could not start encryption, error %d", err );
       
   221         User::Leave( err );
       
   222         }
       
   223 
       
   224     // Set our internal state and update UI
       
   225     iPercentDone = 0;
       
   226     SetState( EEncrypting );
       
   227 
       
   228     // Start a timer to periodically update the memory state in the UI
       
   229     StartPolling( KProgressInterval );
       
   230 
       
   231 #endif // __WINS__
       
   232 
       
   233     DFLOG( "<<CDevEncUiMemoryEntity::StartEncryptionL" );
       
   234     }
       
   235 
       
   236 // --------------------------------------------------------------------------
       
   237 // CDevEncUiMemoryEntity::StartPolling()
       
   238 // Starts a timer to periodically update the memory state in the UI
       
   239 // --------------------------------------------------------------------------
       
   240 void CDevEncUiMemoryEntity::StartPolling(
       
   241                                       TTimeIntervalMicroSeconds32 aInterval )
       
   242     {
       
   243     TInt error( KErrNone );
       
   244     TRAP( error, DoStartPollingL( aInterval ) );
       
   245     DFLOG2( "CDevEncUiMemoryEntity::StartPolling result %d", error );
       
   246     }
       
   247 
       
   248 // --------------------------------------------------------------------------
       
   249 // CDevEncUiMemoryEntity::DoStartPollingL()
       
   250 // Starts a timer to periodically update the memory state in the UI
       
   251 // --------------------------------------------------------------------------
       
   252 void CDevEncUiMemoryEntity::DoStartPollingL(
       
   253                                       TTimeIntervalMicroSeconds32 aInterval )
       
   254     {
       
   255     TCallBack pollCallBack( PollTick, static_cast<TAny*>( this ) );
       
   256     TCallBack progressCallBack( ProgressTick, static_cast<TAny*>( this ) );
       
   257 
       
   258     if ( !iPeriodic )
       
   259         {
       
   260         iPeriodic = CPeriodic::NewL( EPriorityNormal );
       
   261         }
       
   262     iPeriodic->Cancel();
       
   263 
       
   264     if ( aInterval == KProgressInterval )
       
   265         {
       
   266         iPeriodic->Start( 0, aInterval, progressCallBack );
       
   267         }
       
   268     else
       
   269         {
       
   270         iPeriodic->Start( 0, aInterval, pollCallBack );
       
   271         }
       
   272     }
       
   273 
       
   274 // --------------------------------------------------------------------------
       
   275 // CDevEncUiMemoryEntity::StartDecryptionL()
       
   276 // 
       
   277 // --------------------------------------------------------------------------
       
   278 void CDevEncUiMemoryEntity::StartDecryptionL()
       
   279     {
       
   280     DFLOG( ">>CDevEncUiMemoryEntity::StartDecryptionL" );
       
   281 
       
   282 #ifndef __WINS__
       
   283 
       
   284     if ( ! iSession->Connected() )
       
   285         {
       
   286         User::LeaveIfError( iSession->Connect() );
       
   287         DFLOG( "Connected " );
       
   288         }
       
   289 
       
   290     // Make sure the memory is encrypted
       
   291     TInt nfeDiskStatus( EUnmounted );
       
   292     TInt err = iSession->DiskStatus( nfeDiskStatus );
       
   293     if ( err )
       
   294         {
       
   295         DFLOG2( "Could not get disk status, error %d", err );
       
   296         //User::Leave( err );
       
   297         }
       
   298     if ( nfeDiskStatus != EEncrypted )
       
   299         {
       
   300         DFLOG2( "Memory is in wrong state (%d), leaving", nfeDiskStatus );
       
   301         User::Leave( KErrNotReady );
       
   302         }
       
   303 
       
   304     // Launch the decryption process
       
   305     err = iSession->StartDiskDecrypt();
       
   306     if ( err )
       
   307         {
       
   308         DFLOG2( "Could not start decryption, error %d", err );
       
   309         User::Leave( err );
       
   310         }
       
   311 
       
   312     // Set our internal state and update UI
       
   313     iPercentDone = 0;
       
   314     SetState( EDecrypting );
       
   315 
       
   316     // Start a timer to periodically update the memory state in the UI
       
   317     StartPolling( KProgressInterval );
       
   318 
       
   319 #endif // __WINS__
       
   320     
       
   321     DFLOG( "<<CDevEncUiMemoryEntity::StartDecryptionL" );
       
   322     }
       
   323 
       
   324 // --------------------------------------------------------------------------
       
   325 // CDevEncUiMemoryEntity::ProgressTick()
       
   326 // 
       
   327 // --------------------------------------------------------------------------
       
   328 TInt CDevEncUiMemoryEntity::ProgressTick( TAny* aPtr )
       
   329     {
       
   330     CDevEncUiMemoryEntity* self = static_cast<CDevEncUiMemoryEntity*>( aPtr );
       
   331     self->DoProgressTick();
       
   332     return 0;
       
   333     }
       
   334 
       
   335 // --------------------------------------------------------------------------
       
   336 // CDevEncUiMemoryEntity::DoProgressTick()
       
   337 // 
       
   338 // --------------------------------------------------------------------------
       
   339 void CDevEncUiMemoryEntity::DoProgressTick()
       
   340     {
       
   341     CheckProgress();
       
   342     }
       
   343 
       
   344 // --------------------------------------------------------------------------
       
   345 // CDevEncUiMemoryEntity::PollTick()
       
   346 // 
       
   347 // --------------------------------------------------------------------------
       
   348 TInt CDevEncUiMemoryEntity::PollTick( TAny* aPtr )
       
   349     {
       
   350     CDevEncUiMemoryEntity* self = static_cast<CDevEncUiMemoryEntity*>( aPtr );
       
   351     TInt error( KErrNone );
       
   352     TRAP( error, self->DoPollTickL() );
       
   353     DFLOG2( "CDevEncUiMemoryEntity::PollTick result %d", error );
       
   354     return 0;
       
   355     }
       
   356 
       
   357 // --------------------------------------------------------------------------
       
   358 // CDevEncUiMemoryEntity::DoPollTickL()
       
   359 // 
       
   360 // --------------------------------------------------------------------------
       
   361 void CDevEncUiMemoryEntity::DoPollTickL()
       
   362     {
       
   363     DFLOG( "CDevEncUiMemoryEntity::DoPollTickL" );
       
   364     TInt nfeDiskStatus( EUnmounted );
       
   365     TInt err = iSession->DiskStatus( nfeDiskStatus );
       
   366     if ( err )
       
   367         {
       
   368         DFLOG2( "Could not get disk status, error %d", err );
       
   369         // Ignore error
       
   370         return;
       
   371         }
       
   372     if ( ( nfeDiskStatus == EEncrypting ) ||
       
   373          ( nfeDiskStatus == EDecrypting ) )
       
   374         {
       
   375         // Some other component has started an encryption operation.
       
   376         // Indicate this to the UI and start polling the progress.
       
   377         DFLOG2( "CDevEncUiMemoryEntity::DoPollTick: New operation %d",
       
   378                 nfeDiskStatus );
       
   379         SetState( nfeDiskStatus );
       
   380         StartPolling( KProgressInterval );
       
   381         }
       
   382     else
       
   383         {
       
   384         if ( iState != nfeDiskStatus )
       
   385             {
       
   386             DFLOG( "CDevEncUiMemoryEntity::DoPollTickL => SetState" );
       
   387             // The Mmc status has changed, but we are not in the middle of
       
   388             // any operation. Just set the new state.
       
   389             SetState( nfeDiskStatus );
       
   390             }
       
   391         }
       
   392     }
       
   393 
       
   394 // --------------------------------------------------------------------------
       
   395 // CDevEncUiMemoryEntity::SetState()
       
   396 // 
       
   397 // --------------------------------------------------------------------------
       
   398 void CDevEncUiMemoryEntity::SetState( TUint aState )
       
   399     {
       
   400     DFLOG3( "CDevEncUiMemoryEntity::SetState, prev %d, new %d",
       
   401             iState, aState );
       
   402     iState = aState;
       
   403     UpdateMemoryInfo();
       
   404     }
       
   405 
       
   406 // --------------------------------------------------------------------------
       
   407 // CDevEncUiMemoryEntity::CheckProgress()
       
   408 // 
       
   409 // --------------------------------------------------------------------------
       
   410 void CDevEncUiMemoryEntity::CheckProgress()
       
   411     {
       
   412     ASSERT( iSession );
       
   413     ASSERT( iSession->Connected() );
       
   414 
       
   415     TInt err = iSession->Progress( iPercentDone );
       
   416 
       
   417     if ( err )
       
   418         {
       
   419         DFLOG( "Could not get progress" );
       
   420         iPeriodic->Cancel();
       
   421         }
       
   422     else if ( iPercentDone >= KPercent100 )
       
   423         {
       
   424         DFLOG( "Operation complete" );
       
   425         iPeriodic->Cancel();
       
   426         iPercentDone = 0;
       
   427 
       
   428         // The disk needs to be finalized, but the finalization is done in
       
   429         // the starter component.
       
   430         
       
   431         DFLOG2( "CDevEncUiMemoryEntity::CheckProgress => iSession->DriveNumber() = %d", iSession->DriveNumber()  );
       
   432 
       
   433         if ( iState == EDecrypted && iSession->DriveNumber() == EDriveC )
       
   434             {
       
   435             DFLOG( "CDevEncUiMemoryEntity::CheckProgress => RestoreAutolockSettings" );
       
   436             RestoreAutolockSettings();
       
   437             }
       
   438         // Restart the polling at a slower pace
       
   439         // StartPolling( KPollInterval );
       
   440         }
       
   441     else // Not yet finished...
       
   442         {
       
   443         UpdateMemoryInfo();
       
   444         }
       
   445     }
       
   446 
       
   447 // --------------------------------------------------------------------------
       
   448 // CDevEncUiMemoryEntity::RestoreAutolockSettings()
       
   449 // 
       
   450 // --------------------------------------------------------------------------
       
   451 void CDevEncUiMemoryEntity::RestoreAutolockSettings()
       
   452     {
       
   453 #ifndef __WINS__
       
   454     //the user chose to decrypt the phone memory. restore the previuos autolock settings
       
   455     RTerminalControl terminalCtrl;
       
   456     TInt ctrlConnect = terminalCtrl.Connect();
       
   457     DFLOG2( "DEVENC: terminal control connected %d", ctrlConnect );
       
   458     
       
   459     RTerminalControl3rdPartySession session;
       
   460     TInt retValue = session.Open( terminalCtrl );
       
   461     DFLOG2( "DEVENC: terminal control session open %d", retValue );
       
   462     
       
   463     //read the current autolock period
       
   464     TBuf8<21> autoLockPeriodBuf;
       
   465     TInt err2 = session.GetDeviceLockParameter( RTerminalControl3rdPartySession::ETimeout, autoLockPeriodBuf );
       
   466     DFLOG2( "DEVENC: max value get returned value %d", err2 );
       
   467     
       
   468     //this is set to 0 because the Tarm control prevent the autolock from being disabled otherwise.
       
   469     TBuf8<21> oldMaxPeriodBuf;
       
   470     oldMaxPeriodBuf.AppendNum( 0 );
       
   471     TInt err = session.SetDeviceLockParameter( RTerminalControl3rdPartySession::EMaxTimeout, oldMaxPeriodBuf );
       
   472     DFLOG2( "DEVENC: max value set returned value %d", err );
       
   473     
       
   474     //set the autolock period as it was previously
       
   475     err = session.SetDeviceLockParameter( RTerminalControl3rdPartySession::ETimeout, autoLockPeriodBuf );
       
   476     DFLOG2( "DEVENC: max value set returned value %d", err );
       
   477     
       
   478     session.Close();
       
   479     terminalCtrl.Close();
       
   480 #endif
       
   481     }
       
   482 
       
   483 // --------------------------------------------------------------------------
       
   484 // CDevEncUiMemoryEntity::UpdateMemoryInfo()
       
   485 // 
       
   486 // --------------------------------------------------------------------------
       
   487 void CDevEncUiMemoryEntity::UpdateMemoryInfo()
       
   488     {
       
   489     DFLOG( "CDevEncUiMemoryEntity::UpdateMemoryInfo" );
       
   490     for ( TInt i = 0; i < iObservers.Count(); i++ )
       
   491         {
       
   492         iObservers[i]->UpdateInfo( iType, iState, iPercentDone );
       
   493         }
       
   494     }
       
   495 
       
   496 // --------------------------------------------------------------------------
       
   497 // CDevEncUiMemoryEntity::State()
       
   498 // 
       
   499 // --------------------------------------------------------------------------
       
   500 TUint CDevEncUiMemoryEntity::State() const
       
   501     {
       
   502     return iState;
       
   503     }
       
   504 
       
   505 // --------------------------------------------------------------------------
       
   506 // CDevEncUiMemoryEntity::DiskStatusChangedL()
       
   507 // From MDiskStatusObserver
       
   508 // --------------------------------------------------------------------------
       
   509 void CDevEncUiMemoryEntity::DiskStatusChangedL( TInt aNfeStatus )
       
   510     {
       
   511     if ( ( aNfeStatus == EEncrypting ) ||
       
   512          ( aNfeStatus == EDecrypting ) )
       
   513         {
       
   514         // Some other component has started an encryption operation.
       
   515         // Indicate this to the UI and start polling the progress.
       
   516         DFLOG2( "CDevEncStarterMemoryEntity::DoPollTick: New operation %d",
       
   517         		aNfeStatus );
       
   518         SetState( aNfeStatus );
       
   519         StartPolling( KProgressInterval );
       
   520         }
       
   521     else
       
   522         {
       
   523         if ( iState != aNfeStatus )
       
   524             {
       
   525             // The Mmc status has changed, but we are not in the middle of
       
   526             // any operation. Just set the new state.
       
   527             SetState( aNfeStatus );
       
   528             }
       
   529         }
       
   530     }
       
   531 
       
   532 // End of File
       
   533 
       
   534 
       
   535