devencdiskutils/DevEncStarter/src/DevEncStarterMmcObserver.cpp
changeset 0 164170e6151a
equal deleted inserted replaced
-1:000000000000 0:164170e6151a
       
     1 /*
       
     2 * Copyright (c) 2007 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 CDevEncStarterMmcObserver
       
    15 *
       
    16 *  Version    : %version: 9 %, %date_modified: Fri Nov 13 11:16:40 2009 % by %derived_by: e0326969 %
       
    17 */
       
    18 
       
    19 
       
    20 #include <centralrepository.h>
       
    21 #include <DevEncExternalCRKeys.h>
       
    22 #include <DevEncEngineConstants.h>
       
    23 #include "DevEncLog.h"
       
    24 #include "DevEncMmcObserver.h"
       
    25 #include "DevEncStarterMmcObserver.h"
       
    26 #include "DevEncStarterMemoryEntity.h"
       
    27 #include "DevEncStarterUtils.h"
       
    28 #include "DevEncUids.hrh"
       
    29 #include "MmcInfo.h"
       
    30 
       
    31 const TInt mmcDrive( /*EDriveE*/EDriveF );
       
    32 //const TInt KPowerKeyCode( EKeyPowerOff );
       
    33 //const TInt KPowerKeyScanCode( EStdKeyPowerOff );
       
    34 
       
    35 // -----------------------------------------------------------------------------
       
    36 // CDevEncStarterMmcObserver::NewL
       
    37 // Two-phased constructor.
       
    38 // -----------------------------------------------------------------------------
       
    39 CDevEncStarterMmcObserver* CDevEncStarterMmcObserver::NewL(
       
    40                                                CDevEncStarterUtils*& aUtils )
       
    41     {
       
    42     CDevEncStarterMmcObserver* self =
       
    43         new ( ELeave ) CDevEncStarterMmcObserver( aUtils );
       
    44     CleanupStack::PushL( self );
       
    45     self->ConstructL();
       
    46     CleanupStack::Pop( self );
       
    47     return self;
       
    48     }
       
    49 
       
    50 // -----------------------------------------------------------------------------
       
    51 // CDevEncStarterMmcObserver::CDevEncStarterMmcObserver
       
    52 // 
       
    53 // -----------------------------------------------------------------------------
       
    54 CDevEncStarterMmcObserver::CDevEncStarterMmcObserver(
       
    55                                                CDevEncStarterUtils*& aUtils )
       
    56     : iUtils( aUtils )
       
    57     {
       
    58     }
       
    59 
       
    60 // -----------------------------------------------------------------------------
       
    61 // CDevEncStarterMmcObserver::ConstructL
       
    62 // 
       
    63 // -----------------------------------------------------------------------------
       
    64 void CDevEncStarterMmcObserver::ConstructL()
       
    65     {
       
    66     DFLOG( "CDevEncStarterMmcObserver::ConstructL" );
       
    67     // Central repository settings
       
    68     iCrSettings = CRepository::NewL( TUid::Uid( KCRDevEncUiSettings ) );
       
    69 
       
    70     DFLOG( "ObsCon 1" );
       
    71     
       
    72     // Create objects representing the different types of memory
       
    73     // The order is important
       
    74     CDevEncStarterMemoryEntity* phoneMemory =
       
    75         CDevEncStarterMemoryEntity::NewLC( EPhoneMemory );
       
    76     iMemEntities.AppendL( phoneMemory );
       
    77     CleanupStack::Pop( phoneMemory );
       
    78 
       
    79         DFLOG( "ObsCon 2" );
       
    80 
       
    81     CDevEncStarterMemoryEntity* memoryCard =
       
    82         CDevEncStarterMemoryEntity::NewLC( EMemoryCard );
       
    83     iMemEntities.AppendL( memoryCard );
       
    84     CleanupStack::Pop( memoryCard );
       
    85 
       
    86         DFLOG( "ObsCon 3" );
       
    87     CDevEncStarterMemoryEntity* massMemory =
       
    88                 CDevEncStarterMemoryEntity::NewLC( EPrimaryPhoneMemory );
       
    89     
       
    90     iMemEntities.AppendL( massMemory );
       
    91     CleanupStack::Pop( massMemory );
       
    92 
       
    93     // Create an MMC file system observer
       
    94     User::LeaveIfError( iFs.Connect() );
       
    95     iObserver = CMmcObserver::NewL( this, &iFs );
       
    96     iObserver->StartObserver();
       
    97     
       
    98         DFLOG( "ObsCon 4" );
       
    99 
       
   100     // Call the MMC callback function once to get the initial card status
       
   101     MMCStatusChangedL();
       
   102 
       
   103         DFLOG( "ObsCon 5" );
       
   104 
       
   105     // This class needs to observe memory card status changes.
       
   106     // (This call results in a call to UpdateInfo, hence we need to be fully
       
   107     // constructed before this.)
       
   108     iMemEntities[ EMemoryCard ]->AddObserverL( this );
       
   109 
       
   110         DFLOG( "ObsCon 6" );
       
   111 
       
   112     }
       
   113 
       
   114 // -----------------------------------------------------------------------------
       
   115 // CDevEncStarterMmcObserver::~CDevEncStarterMmcObserver
       
   116 // 
       
   117 // -----------------------------------------------------------------------------
       
   118 CDevEncStarterMmcObserver::~CDevEncStarterMmcObserver()
       
   119     {
       
   120     delete iObserver;
       
   121     iFs.Close();
       
   122 
       
   123     if ( iMemEntities.Count() > EMemoryCard ) 
       
   124 	    {
       
   125         iMemEntities[ EMemoryCard ]->RemoveObserver( this );
       
   126         for ( TInt i = 0; i < iMemEntities.Count(); i++ )
       
   127             {
       
   128             delete iMemEntities[i];
       
   129             }
       
   130         iMemEntities.Close();
       
   131 		}
       
   132     delete iCrSettings;
       
   133     }
       
   134 
       
   135 // -----------------------------------------------------------------------------
       
   136 // CDevEncStarterMmcObserver::MMCStatusChangedL
       
   137 // From MMemoryCardObserver
       
   138 // -----------------------------------------------------------------------------
       
   139 void CDevEncStarterMmcObserver::MMCStatusChangedL()
       
   140     {
       
   141     DFLOG( "CDevEncStarterMmcObserver::MMCStatusChangedL" );
       
   142     TVolumeInfo volInfo;
       
   143 
       
   144     TInt err = iFs.Volume( volInfo, /*EDriveE*/EDriveF );
       
   145     switch ( err )
       
   146         {
       
   147         case KErrNone:
       
   148             {
       
   149             // Readable MMC inserted
       
   150             DFLOG( "MMC inserted" );
       
   151             iMmcStatus = EMmcOk;
       
   152             
       
   153             break;
       
   154             }
       
   155 
       
   156         case KErrNotReady:
       
   157             {
       
   158             // MMC ejected
       
   159             DFLOG( "MMC ejected" );
       
   160             iMmcStatus = EMmcNotPresent;
       
   161             //This is because for some reason when the memory card is ejected the Disk status observer is not notified
       
   162             //thus the Ui is started even if the MMC is removed.
       
   163             iMemEntities[ EMemoryCard ]->SetState( EUnmounted );
       
   164             break;
       
   165             }
       
   166 
       
   167         case KErrCorrupt:
       
   168             {
       
   169             // Corrupt or unformatted MMC, or wrong key
       
   170             DFLOG( "MMC corrupt, unformatted or encrypted with other key" );
       
   171             iMmcStatus = EMmcNotReadable;
       
   172 
       
   173                 break;
       
   174             }
       
   175 
       
   176         default:
       
   177             {
       
   178             DFLOG2( "RFs::Volume returned error %d", err );
       
   179             break;
       
   180             }
       
   181         } // switch
       
   182     }
       
   183 
       
   184 // -----------------------------------------------------------------------------
       
   185 // CDevEncStarterMmcObserver::GetInfo
       
   186 // From MMemoryCardObserver
       
   187 // -----------------------------------------------------------------------------
       
   188 TInt CDevEncStarterMmcObserver::GetInfo( TMmcInfo& aInfo )
       
   189     {
       
   190     TVolumeInfo volInfo;
       
   191     TDriveInfo driveInfo;
       
   192 
       
   193     // Check if MMC mounted
       
   194     TInt err = CheckMMC();
       
   195 
       
   196     DFLOG2( "CDevEncStarterMmcObserver::GetInfo - CheckMMC returned %d", err );
       
   197     
       
   198     if( !err )
       
   199         {
       
   200         // MMC is mounted, get further information
       
   201         err = iFs.Drive( driveInfo, mmcDrive );
       
   202         
       
   203         DFLOG2( "CDevEncStarterMmcObserver::GetInfo - iFs.Drive returned %d", err );
       
   204         DFLOG3( "CDevEncStarterMmcObserver::GetInfo - driveInfo.iDriveAtt %d, driveInfo.iMediaAtt %d",
       
   205                 driveInfo.iDriveAtt,
       
   206                 driveInfo.iMediaAtt );
       
   207 
       
   208         if ( !err )
       
   209             {
       
   210             aInfo.iIsFormattable = driveInfo.iMediaAtt & KMediaAttFormattable;
       
   211             aInfo.iIsLockable = driveInfo.iMediaAtt & KMediaAttLockable;
       
   212             aInfo.iIsLocked = driveInfo.iMediaAtt & KMediaAttLocked;
       
   213             aInfo.iIsReadOnly = driveInfo.iMediaAtt & KMediaAttWriteProtected;
       
   214             aInfo.iIsPasswordSet = driveInfo.iMediaAtt & KMediaAttHasPassword;
       
   215 
       
   216             if ( !aInfo.iIsLocked )
       
   217                 {
       
   218                 // Then get volume info for name etc.
       
   219                 err = iFs.Volume( volInfo, mmcDrive );
       
   220 //#if defined _DEBUG
       
   221 #if 0
       
   222                 TBuf<50> tempDes1; // helpers for showing valid memory amount, which do not fit in 32 bits
       
   223                 TBuf<50> tempDes2;
       
   224 
       
   225                 tempDes1.Num( volInfo.iSize );
       
   226                 tempDes2.Num( volInfo.iFree );
       
   227                 DFLOG2( "CDevEncStarterMmcObserver::GetInfo - iFs.Volume returned %d", err );
       
   228                 DFLOG2( "CDevEncStarterMmcObserver::GetInfo - volInfo iUniqueID %d, iSize, iFree, iName:", 
       
   229                         volInfo.iUniqueID );
       
   230                 DFLOGBUF( tempDes1 );
       
   231                 DFLOGBUF( tempDes2 );
       
   232                 DFLOGBUF( volInfo.iName );
       
   233 #endif // _DEBUG
       
   234                 }
       
   235             else
       
   236                 {
       
   237                 err = KErrLocked;
       
   238                 }
       
   239             }
       
   240         else
       
   241             {
       
   242             err = KErrNotReady;
       
   243             }
       
   244         }
       
   245 
       
   246     DFLOG2( "CDevEncStarterMmcObserver::GetInfo - err %d", err );
       
   247     
       
   248     if( !err )
       
   249         {
       
   250         // MMC ok, get rest of the information
       
   251         aInfo.iIsInserted = ETrue;
       
   252         aInfo.iIsInUse = EFalse;
       
   253         aInfo.iName = volInfo.iName;
       
   254         aInfo.iUid = volInfo.iUniqueID;
       
   255         // Use all 64 bits
       
   256         aInfo.iCapacity = volInfo.iSize;
       
   257         aInfo.iSpaceFree = volInfo.iFree;
       
   258 
       
   259         // Check if the volume is formatted
       
   260         aInfo.iIsFormatted = volInfo.iSize > 0;
       
   261 
       
   262         // Test if a previous backup exists
       
   263         aInfo.iBackupExists = EFalse; //BaflUtils::FileExists( iFs, aBackupFile );
       
   264         }
       
   265     else
       
   266         {
       
   267         // By default set values as 
       
   268         aInfo.iIsInserted = EFalse;
       
   269         aInfo.iIsInUse = EFalse;
       
   270         aInfo.iIsFormatted = EFalse;
       
   271         aInfo.iBackupExists = EFalse;
       
   272 
       
   273         switch( err )
       
   274             {
       
   275             case KErrInUse:
       
   276                 {
       
   277                 aInfo.iIsInserted = ETrue;
       
   278                 aInfo.iIsInUse = ETrue;
       
   279                 break;
       
   280                 }
       
   281             case KErrNotReady:
       
   282                 {
       
   283                 // If card is not ready, but media attributes set, it is reserved.
       
   284                 if ( driveInfo.iMediaAtt )
       
   285                     {
       
   286                     aInfo.iIsInserted = ETrue;
       
   287                     aInfo.iIsInUse = ETrue;
       
   288                     }
       
   289                 break;
       
   290                 }
       
   291             case KErrCorrupt:
       
   292                 {
       
   293                 aInfo.iIsInserted = ETrue;
       
   294                 break;
       
   295                 }
       
   296             case KErrLocked:
       
   297                 {
       
   298                 // Could not mount due card locked or volume info query returned card locked.
       
   299                 // Hence info-flags possibly set accoring to iMediaAtt are overridden.
       
   300                 aInfo.iIsInserted = ETrue;
       
   301                 aInfo.iIsFormatted = ETrue;
       
   302                 aInfo.iIsLocked = ETrue;
       
   303                 aInfo.iIsPasswordSet = ETrue;
       
   304                 aInfo.iIsLockable = ETrue;
       
   305                 break;
       
   306                 }
       
   307             default:
       
   308                 {
       
   309                 break;
       
   310                 }
       
   311             }
       
   312         }
       
   313 
       
   314     return err;
       
   315     }
       
   316 
       
   317 // -----------------------------------------------------------------------------
       
   318 // CDevEncStarterMmcObserver::CheckMMC
       
   319 // 
       
   320 // -----------------------------------------------------------------------------
       
   321 TInt CDevEncStarterMmcObserver::CheckMMC()
       
   322     {
       
   323     // Check if MMC mounted, mount if required
       
   324     TFullName fsname;
       
   325 
       
   326     // If can't use the drive, returns KErrNotFound
       
   327     TInt err = iFs.FileSystemName( fsname, mmcDrive );
       
   328 
       
   329     if ( !err )
       
   330         {
       
   331         // MMC found. Checking if it is mounted
       
   332         if ( fsname.Length( ) == 0 )
       
   333             {
       
   334             // MMC not mounted ( file system name is empty ) 
       
   335             DFLOG( "Mmc not mounted" );
       
   336             err = KErrDisMounted;
       
   337             }
       
   338         }
       
   339     return err;
       
   340     }
       
   341 
       
   342 // -----------------------------------------------------------------------------
       
   343 // CDevEncStarterMmcObserver::CheckMMC
       
   344 // 
       
   345 // -----------------------------------------------------------------------------
       
   346 void CDevEncStarterMmcObserver::LogInfo( const TMmcInfo& aInfo ) const
       
   347     {
       
   348     DFLOG2( "aInfo.iIsFormattable %d", aInfo.iIsFormattable ? 1 : 0 );
       
   349     DFLOG2( "aInfo.iIsFormatted %d", aInfo.iIsFormatted ? 1 : 0 );
       
   350     DFLOG2( "aInfo.iIsLockable %d", aInfo.iIsLockable ? 1 : 0 );
       
   351     DFLOG2( "aInfo.iIsLocked %d", aInfo.iIsLocked ? 1 : 0 );
       
   352     DFLOG2( "aInfo.iIsReadOnly %d", aInfo.iIsReadOnly ? 1 : 0 );
       
   353     DFLOG2( "aInfo.iIsPasswordSet %d", aInfo.iIsPasswordSet ? 1 : 0 );
       
   354     DFLOG2( "aInfo.iIsInserted %d", aInfo.iIsInserted ? 1 : 0 );
       
   355     DFLOG2( "aInfo.iBackupExists %d", aInfo.iBackupExists ? 1 : 0 );
       
   356     DFLOG2( "aInfo.iIsInUse %d", aInfo.iIsInUse ? 1 : 0 );
       
   357     }
       
   358 
       
   359 // --------------------------------------------------------------------------
       
   360 // Called by memory entity when memory status changes.
       
   361 // 
       
   362 // --------------------------------------------------------------------------
       
   363 void CDevEncStarterMmcObserver::UpdateInfo( TDevEncUiMemoryType aType,
       
   364                                             TUint aState,
       
   365                                             TUint /*aProgress*/ )
       
   366     {
       
   367     DFLOG3( "CDevEncStarterMmcObserver::UpdateInfo, type %d, state %d",
       
   368             aType, aState );
       
   369     DFLOG2( "Phone state %d", iPhoneEncState );
       
   370     DFLOG2( "Mmc state %d", iMmcStatus );
       
   371 
       
   372     TBool startUi = NeedToStartUi( aType, aState );
       
   373 
       
   374     if ( aType == EPhoneMemory )
       
   375         {
       
   376         iPhoneEncState = aState;
       
   377         }
       
   378     else // EMemoryCard
       
   379         {
       
   380         iMmcEncState = aState;    
       
   381         }
       
   382 
       
   383     if ( startUi )
       
   384         {
       
   385         TInt error( KErrNone );
       
   386         TRAP( error, iUtils->StartAppL( KDevEncUiUid,
       
   387                                         this ) );
       
   388         DFLOG2( "App start requested, result %d", error );
       
   389         }
       
   390     }
       
   391 
       
   392 // -----------------------------------------------------------------------------
       
   393 // CDevEncStarterMmcObserver::NeedToStartUi
       
   394 // 
       
   395 // -----------------------------------------------------------------------------
       
   396 TBool CDevEncStarterMmcObserver::NeedToStartUi( TDevEncUiMemoryType aType,
       
   397                                                 TUint aState )
       
   398     {
       
   399     TBool startUi( EFalse );
       
   400     
       
   401     DFLOG2( "CDevEncStarterMmcObserver::NeedToStartUi => aState = %d",
       
   402     		aState );
       
   403     DFLOG2( "CDevEncStarterMmcObserver::NeedToStartUi => iMmcStatus = %d",
       
   404     		iMmcStatus );
       
   405     DFLOG2( "CDevEncStarterMmcObserver::NeedToStartUi => iMmcEncState = %d",
       
   406     		iMmcEncState );
       
   407 
       
   408     // Check encryption key status
       
   409     TInt mmcKeyInDriver( 0 );
       
   410     TInt error = iCrSettings->Get( KDevEncUserSettingMemoryCard,
       
   411                                    mmcKeyInDriver );
       
   412     if ( error )
       
   413         {
       
   414         DFLOG2( "Cenrep get error %d", error );
       
   415         return startUi;
       
   416         }
       
   417     
       
   418     if ( ( iMmcStatus == EMmcOk ) && 
       
   419          ( iMmcEncState == EUnmounted ) &&
       
   420          ( aState == EDecrypted ) )
       
   421         {
       
   422         // A readable memory card has just been inserted
       
   423     
       
   424         if ( mmcKeyInDriver )
       
   425             {
       
   426             // We have a valid encryption key, but the newly inserted
       
   427             // card is decrypted. Ask if the user wants to encrypt.
       
   428             // (see UI spec 2.13)
       
   429             startUi = ETrue;
       
   430             }
       
   431         }
       
   432 
       
   433     if ( ( iMmcStatus == EMmcNotReadable ) && 
       
   434          ( iMmcEncState == EUnmounted ) &&
       
   435          ( aState == ECorrupted ) )
       
   436         {
       
   437         // An unreadable memory card has just been inserted
       
   438         
       
   439         if ( mmcKeyInDriver )
       
   440             {
       
   441             // We seem to have the wrong encryption key in the driver.
       
   442             // Inform the user that the card cannot be used.
       
   443             // (see UI spec 2.16)
       
   444             startUi = ETrue;
       
   445             }
       
   446         else
       
   447             {
       
   448             // We have no encryption key in the driver.
       
   449             // Ask if the user wants to import a key and decrypt.
       
   450             // (see UI spec 2.17)
       
   451             startUi = ETrue;
       
   452             }
       
   453         }
       
   454 
       
   455     if ( ( iMmcStatus == EMmcNotReadable ) && 
       
   456          ( iMmcEncState == ECorrupted ) &&
       
   457          ( aState == ECorrupted ) )
       
   458         {
       
   459         // We have an unreadable memory card, and tried to load a new key
       
   460         // but the card is still unreadable. Inform the user and let the user
       
   461         // select some other key. (see UI spec 2.10)
       
   462         startUi = ETrue;
       
   463         }
       
   464 
       
   465     if ( ( iMmcStatus == EMmcOk ) && 
       
   466          ( iMmcEncState == ECorrupted ) &&
       
   467          ( aState == EEncrypted ) )
       
   468         {
       
   469         // We have just imported the correct key and the card is now
       
   470         // readable. Show a note to the user.
       
   471         // (see UI spec 2.7)
       
   472         startUi = ETrue;
       
   473         }
       
   474     return startUi;
       
   475     }
       
   476 
       
   477 
       
   478 // -----------------------------------------------------------------------------
       
   479 // CDevEncStarterMmcObserver::AppStarted
       
   480 // 
       
   481 // -----------------------------------------------------------------------------
       
   482 void CDevEncStarterMmcObserver::AppStarted( const TUint32& aUid )
       
   483     {
       
   484     if ( !iUtils )
       
   485         {
       
   486         DFLOG( "CDevEncStarterMmcObserver::AppStarted error: no utils" );
       
   487         return;
       
   488         }
       
   489     }
       
   490 
       
   491 // End of file