fotaapplication/fotaserver/FotaServer/src/DevEncController.cpp
changeset 50 a36219ae6585
parent 49 689a71addb96
child 51 2e64dc50f295
equal deleted inserted replaced
49:689a71addb96 50:a36219ae6585
     1 /*
       
     2 * Copyright (c) 2009 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:   CDevEncController definitions Part of ES System Application
       
    15 *
       
    16 */
       
    17 
       
    18 #include <apgcli.h> // for RApaLsSession
       
    19 #include <apacmdln.h> // for CApaCommandLine
       
    20 #include <centralrepository.h>
       
    21 #include <featmgr.h> // for checking DE feature
       
    22 #include "DevEncController.h"
       
    23 #include "FotaServer.h"
       
    24 #include "FotaSrvDebug.h"
       
    25 #include "DevEncSession.h"
       
    26 #include <DevEncEngineConstants.h>
       
    27 #include "DevEncProgressObserver.h"
       
    28 #include <fotaserver.rsg>
       
    29 #include <DevEncProtectedPSKey.h>
       
    30 #define __LEAVE_IF_ERROR(x) if(KErrNone!=x) {FLOG(_L("LEAVE in %s: %d"), __FILE__, __LINE__); User::Leave(x); }
       
    31 
       
    32 // ================= MEMBER FUNCTIONS =======================
       
    33 //
       
    34 // ----------------------------------------------------------
       
    35 // CDevEncController::NewL
       
    36 // Instancies CDevEncController object
       
    37 // ----------------------------------------------------------
       
    38 //
       
    39 CDevEncController* CDevEncController::NewL(CFotaServer* aCallback )
       
    40     {
       
    41     FLOG(_L("CDevEncController::NewL >>"));
       
    42 
       
    43     CDevEncController* self = CDevEncController::NewLC(aCallback);
       
    44     CleanupStack::Pop();
       
    45 
       
    46     FLOG(_L("CDevEncController::NewL <<"));
       
    47     return self;
       
    48     }
       
    49 
       
    50 
       
    51 // ----------------------------------------------------------
       
    52 // CDevEncController::NewL
       
    53 // Instancies CDevEncController object
       
    54 // ----------------------------------------------------------
       
    55 //
       
    56 CDevEncController* CDevEncController::NewLC(CFotaServer* aCallback )
       
    57     {
       
    58     FLOG(_L("CDevEncController::NewLC >>"));
       
    59 
       
    60     CDevEncController* self = new ( ELeave ) CDevEncController(aCallback);
       
    61     CleanupStack::PushL ( self );
       
    62     self->ConstructL();
       
    63 
       
    64     FLOG(_L("CDevEncController::NewC <<"));
       
    65     return self;
       
    66     }
       
    67 
       
    68 // ----------------------------------------------------------
       
    69 // CDevEncController::ConstructL()
       
    70 // Initializes data objects
       
    71 // ----------------------------------------------------------
       
    72 //
       
    73 void CDevEncController::ConstructL()
       
    74     {
       
    75     FLOG(_L("CDevEncController::ConstructL >>"));
       
    76 
       
    77     if (!IsDeviceEncryptionSupportedL())
       
    78         {
       
    79         FLOG(_L("Device doesn't support encryption!!"));
       
    80         User::Leave(KErrNotSupported);
       
    81         }
       
    82     FLOG(_L("CDevEncController::ConstructL <<"));
       
    83     }
       
    84 
       
    85 
       
    86 TBool CDevEncController::IsDeviceEncryptionSupportedL()
       
    87     {
       
    88     FLOG(_L("CDevEncController::IsDeviceEncryptionSupportedL >>"));
       
    89 
       
    90     TBool ret(EFalse);
       
    91 
       
    92     FeatureManager::InitializeLibL();
       
    93     ret = FeatureManager::FeatureSupported( KFeatureIdFfDeviceEncryptionFeature);
       
    94     FeatureManager::UnInitializeLib();
       
    95 
       
    96     FLOG(_L("CDevEncController::IsDeviceEncryptionSupportedL, ret = %d <<"), ret);
       
    97     return ret;
       
    98     }
       
    99 // ----------------------------------------------------------
       
   100 // CDevEncController::CDevEncController()
       
   101 // Constructor
       
   102 // ----------------------------------------------------------
       
   103 //
       
   104 CDevEncController::CDevEncController(CFotaServer* aCallback): iCallback (aCallback),
       
   105 iEncMemorySession(NULL),
       
   106 iDevEncObserver (NULL),
       
   107 iDevEncOperation(EIdle)
       
   108         {
       
   109 
       
   110         }
       
   111 
       
   112 // ----------------------------------------------------------
       
   113 // CDevEncController::CDevEncController()
       
   114 // Destructor
       
   115 // ----------------------------------------------------------
       
   116 //
       
   117 CDevEncController::~CDevEncController()
       
   118     {
       
   119     FLOG(_L("CDevEncController::~CDevEncController >>"));
       
   120 
       
   121     if (iEncMemorySession)
       
   122         {
       
   123         iEncMemorySession->Close();
       
   124         delete iEncMemorySession;
       
   125         iEncMemorySession = NULL;
       
   126         }
       
   127 
       
   128     if (iDevEncObserver)
       
   129         {
       
   130         delete iDevEncObserver;
       
   131         iDevEncObserver = NULL;
       
   132         }
       
   133 
       
   134     FLOG(_L("CDevEncController::~CDevEncController <<"));
       
   135     }
       
   136 
       
   137 TBool CDevEncController::NeedToDecryptL(const TDriveNumber &aDrive)
       
   138     {
       
   139     FLOG(_L("CDevEncController::NeedToDecryptL, drive = %d >>"), (TInt) aDrive);
       
   140 
       
   141     TBool ret (EFalse);
       
   142     TInt err (KErrNone);
       
   143     TInt status (KErrNone);
       
   144 
       
   145     TInt deoperation (EOpIdle);  
       
   146 
       
   147     RProperty::Get(KDevEncProtectedUid, KDevEncOperationKey, deoperation );
       
   148 
       
   149     if (deoperation != EOpIdle)
       
   150         {
       
   151         FLOG(_L("Some disk operation is ongoing. Hence Fota is not possible."));
       
   152         User::Leave(KErrNotReady);
       
   153         }
       
   154 
       
   155     if (!iEncMemorySession)
       
   156         iEncMemorySession = new (ELeave) CDevEncSession( aDrive );
       
   157 
       
   158     err = iEncMemorySession->Connect();
       
   159     if (err != KErrNone)
       
   160         {
       
   161         FLOG(_L("Error in connecting to devencdisk session = %d"), err);
       
   162         User::Leave(err);
       
   163         }
       
   164 
       
   165     err = iEncMemorySession->DiskStatus(status);
       
   166 
       
   167     if (!err && (status == EEncrypted || status == EEncrypting))
       
   168         {
       
   169         FLOG(_L("Drive %d is encrypted"), (TInt) aDrive);
       
   170         ret = ETrue;
       
   171         }
       
   172     else
       
   173         {
       
   174         CRepository *centrep = CRepository::NewL( KCRUidFotaServer );
       
   175         err = centrep->Set(  KDriveToEncrypt, KErrNotFound );
       
   176         delete centrep; centrep = NULL;
       
   177         }
       
   178 
       
   179 #if defined(__WINS__)
       
   180     ret = ETrue;
       
   181 #endif
       
   182 
       
   183     iEncMemorySession->Close();
       
   184     delete iEncMemorySession; iEncMemorySession = NULL;
       
   185 
       
   186     FLOG(_L("CDevEncController::NeedToDecrypt, ret = %d <<"), ret);
       
   187     return ret;
       
   188     }
       
   189 
       
   190 void CDevEncController::DoStartDecryptionL(const TDriveNumber &aDrive)
       
   191     {
       
   192     FLOG(_L("CDevEncController::DoStartDecryptionL >>"));
       
   193 
       
   194     TInt deoperation (EOpIdle);  
       
   195 
       
   196     RProperty::Get(KDevEncProtectedUid, KDevEncOperationKey, deoperation );
       
   197 
       
   198     if (deoperation != EOpIdle)
       
   199         {
       
   200         FLOG(_L("Some disk operation is ongoing. Hence Fota is not possible."));
       
   201         User::Leave(KErrNotReady);
       
   202         }
       
   203 
       
   204     iDevEncOperation = EDecryption;
       
   205     iStorageDrive = aDrive;
       
   206 
       
   207 
       
   208     StartDecryptionL();
       
   209 
       
   210     FLOG(_L("CDevEncController::DoStartDecryptionL <<"));
       
   211     }
       
   212 
       
   213 
       
   214 void CDevEncController::StartDecryptionL()
       
   215     {
       
   216     FLOG(_L("CDevEncController::StartDecryptionL >>"));
       
   217 
       
   218     TInt status (KErrNone);
       
   219 
       
   220     if (!iEncMemorySession)
       
   221         iEncMemorySession = new (ELeave) CDevEncSession( iStorageDrive );
       
   222 
       
   223     __LEAVE_IF_ERROR(iEncMemorySession->Connect());
       
   224 
       
   225     __LEAVE_IF_ERROR(iEncMemorySession->DiskStatus(status));
       
   226 
       
   227     FLOG(_L("Status = %d"), status);
       
   228 
       
   229     if (status == EEncrypted)
       
   230         {
       
   231         if (CheckBatteryL())
       
   232             {
       
   233             FLOG(_L("Started decryption of drive %d..."), iStorageDrive);
       
   234 
       
   235             if (!iDevEncObserver)
       
   236                 iDevEncObserver = CDevEncProgressObserver::NewL(this, R_FOTASERVER_DECRYPTION_PROGRESS_DIALOG);
       
   237 
       
   238             __LEAVE_IF_ERROR(iEncMemorySession->StartDiskDecrypt());
       
   239 
       
   240             FLOG(_L("Monitor for completion of the decryption operation..."));
       
   241 
       
   242             iDevEncObserver->StartMonitoringL(iEncMemorySession);
       
   243             }
       
   244         else
       
   245             {
       
   246             FLOG(_L("Battery low for performing decryption!"));
       
   247 
       
   248             iDevEncOperation = EIdle;
       
   249             iEncMemorySession->Close();
       
   250             delete iEncMemorySession;  iEncMemorySession = NULL;
       
   251 
       
   252             iCallback->HandleDecryptionCompleteL(KErrBadPower, EBatteryLevelLevel4);
       
   253             }
       
   254         }
       
   255     else if(status == EDecrypted)
       
   256         {
       
   257         FLOG(_L("Device is already decrypted"));
       
   258 
       
   259         iDevEncOperation = EIdle;
       
   260         iEncMemorySession->Close();
       
   261         delete iEncMemorySession;  iEncMemorySession = NULL;
       
   262 
       
   263         iCallback->HandleDecryptionCompleteL(KErrNone);
       
   264         }
       
   265     else
       
   266         {
       
   267         // status is either unmounted, encrypting, decrypting, wiping or corrupted
       
   268         FLOG(_L("Can't proceed as disk status is %d"), status);
       
   269 
       
   270         iDevEncOperation = EIdle;
       
   271         iEncMemorySession->Close();
       
   272         delete iEncMemorySession;  iEncMemorySession = NULL;
       
   273 
       
   274         iCallback->HandleDecryptionCompleteL(KErrNotReady);
       
   275         }
       
   276 
       
   277     FLOG(_L("CDevEncController::StartDecryptionL <<"));
       
   278     }
       
   279 
       
   280 void CDevEncController::ReportDevEncOpnCompleteL(TInt aResult)
       
   281     {
       
   282     FLOG(_L("CDevEncController::ReportDevEncOpnCompleteL, result = %d >>"), aResult);
       
   283 
       
   284     TInt err (KErrNone);
       
   285 
       
   286     if (iEncMemorySession)
       
   287         {
       
   288         iEncMemorySession->Close();
       
   289         delete iEncMemorySession;  iEncMemorySession = NULL;
       
   290         }
       
   291 
       
   292     CRepository *centrep = CRepository::NewL( KCRUidFotaServer );
       
   293 
       
   294     if (iDevEncOperation == EDecryption)
       
   295         {
       
   296         err = centrep->Set(  KDriveToEncrypt, iStorageDrive );
       
   297         if (err != KErrNone)
       
   298             {
       
   299             FLOG(_L("Setting drive to encrypt as %d after firmware update, error = %d"), (TInt) iStorageDrive, err);
       
   300             }
       
   301         }
       
   302     else if (iDevEncOperation == EEncryption)
       
   303         {
       
   304         err = centrep->Set(  KDriveToEncrypt, KErrNotFound );
       
   305         if (err != KErrNone)
       
   306             {
       
   307             FLOG(_L("Setting no drive, error = %d"), err);
       
   308             }
       
   309         }
       
   310     delete centrep; centrep = NULL;
       
   311 
       
   312     if (iDevEncOperation == EDecryption)
       
   313         {
       
   314         FLOG(_L("Starting update..."));
       
   315         iCallback->HandleDecryptionCompleteL(KErrNone);
       
   316         iDevEncOperation = EIdle;
       
   317         }
       
   318     else if (iDevEncOperation == EEncryption)
       
   319         {
       
   320         FLOG(_L("Encryption ended"));
       
   321         iCallback->HandleEncryptionCompleteL(KErrNone);
       
   322         iDevEncOperation = EIdle;
       
   323         }
       
   324     else
       
   325         {
       
   326         //should not land here!
       
   327         }
       
   328 
       
   329     FLOG(_L("CDevEncController::ReportDevEncOpnCompleteL <<"));
       
   330     }
       
   331 
       
   332 TBool CDevEncController::NeedToEncryptL(TDriveNumber &aDrive)
       
   333     {
       
   334     FLOG(_L("CDevEncController::NeedToEncryptL >> "));
       
   335     TBool ret (EFalse);
       
   336 
       
   337     CRepository *centrep = CRepository::NewL( KCRUidFotaServer );
       
   338     TInt drive (KErrNotFound);
       
   339     TInt err = centrep->Get(  KDriveToEncrypt, drive );
       
   340     if (drive != KErrNotFound)
       
   341         {
       
   342         aDrive = (TDriveNumber) drive;
       
   343         ret = ETrue;
       
   344         }
       
   345 
       
   346     delete centrep; centrep = NULL;
       
   347 
       
   348     FLOG(_L("CDevEncController::NeedToEncryptL, ret = %d, err = %d << "), ret, err);
       
   349     return ret;
       
   350     }
       
   351 
       
   352 void CDevEncController::DoStartEncryptionL(const TDriveNumber &aDrive)
       
   353     {
       
   354     FLOG(_L("CDevEncController::DoStartEncryptionL, drive = %d >>"), (TInt) aDrive);
       
   355 
       
   356     iDevEncOperation = EEncryption;
       
   357     iStorageDrive = aDrive;
       
   358     StartEncryptionL();
       
   359 
       
   360     FLOG(_L("CDevEncController::DoStartEncryptionL <<"));
       
   361     }
       
   362 
       
   363 void CDevEncController::StartEncryptionL()
       
   364     {
       
   365     FLOG(_L("CDevEncController::StartEncryptionL >>"));
       
   366 
       
   367     TInt status (KErrNone);
       
   368 
       
   369     if (!iEncMemorySession)
       
   370         iEncMemorySession = new (ELeave) CDevEncSession( iStorageDrive );
       
   371 
       
   372     __LEAVE_IF_ERROR(iEncMemorySession->Connect());
       
   373 
       
   374     __LEAVE_IF_ERROR(iEncMemorySession->DiskStatus(status));
       
   375 
       
   376     FLOG(_L("Status = %d"), status);
       
   377 
       
   378     if (status == EDecrypted)
       
   379         {
       
   380         FLOG(_L("Started encryption of drive %d..."), iStorageDrive);
       
   381 
       
   382         if (CheckBatteryL())
       
   383             {
       
   384             if (!iDevEncObserver)
       
   385                 iDevEncObserver = CDevEncProgressObserver::NewL(this, R_FOTASERVER_ENCRYPTION_PROGRESS_DIALOG);
       
   386 
       
   387             __LEAVE_IF_ERROR(iEncMemorySession->StartDiskEncrypt());
       
   388 
       
   389             FLOG(_L("Monitor for completion of the decryption operation..."));
       
   390 
       
   391             iDevEncObserver->StartMonitoringL(iEncMemorySession);
       
   392             }
       
   393         else
       
   394             {
       
   395             FLOG(_L("Battery low for performing encryption!"));
       
   396 
       
   397             iDevEncOperation = EIdle;
       
   398             iEncMemorySession->Close();
       
   399             delete iEncMemorySession;  iEncMemorySession = NULL;
       
   400 
       
   401             iCallback->HandleEncryptionCompleteL(KErrBadPower);
       
   402             }
       
   403         }
       
   404     else if (status == EEncrypted)
       
   405         {
       
   406         FLOG(_L("Memory is already encrypted"));
       
   407 
       
   408         iDevEncOperation = EIdle;
       
   409         iEncMemorySession->Close();
       
   410         delete iEncMemorySession;  iEncMemorySession = NULL;
       
   411 
       
   412         iCallback->HandleEncryptionCompleteL(KErrNone);
       
   413         }
       
   414     else
       
   415         {
       
   416         //status is either encrypting, decrypting, wiping, corrupted
       
   417         FLOG(_L("Can't proceed as disk status is %d"), status);
       
   418 
       
   419         iDevEncOperation = EIdle;
       
   420         iEncMemorySession->Close();
       
   421         delete iEncMemorySession;  iEncMemorySession = NULL;
       
   422 
       
   423         iCallback->HandleEncryptionCompleteL(KErrNotReady);
       
   424         }
       
   425     FLOG(_L("CDevEncController::StartEncryptionL <<"));
       
   426     }
       
   427 
       
   428 TBool CDevEncController::CheckBatteryL()
       
   429     {
       
   430     FLOG(_L("CDevEncController::CheckBatteryL >>"));
       
   431 #ifdef __WINS__
       
   432 
       
   433     // In the emulator, the battery level is always 0 and the charger is never
       
   434     // connected, so just return ETrue.
       
   435     return ETrue;
       
   436 
       
   437 #else // __WINS__
       
   438 
       
   439     // Running on target. Check the real battery and charger status
       
   440 
       
   441     TInt chargingstatus( EChargingStatusError );
       
   442     TInt batterylevel( 1 );
       
   443     TBool enoughPower( EFalse );
       
   444 
       
   445     // Read battery
       
   446     FLOG( _L("CDevEncUiEncryptionOperator::CheckBatteryL" ));
       
   447     RProperty pw;
       
   448     User::LeaveIfError( pw.Attach( KPSUidHWRMPowerState, KHWRMBatteryLevel ) );
       
   449     User::LeaveIfError( pw.Get( batterylevel ) );
       
   450     pw.Close();
       
   451 
       
   452     User::LeaveIfError( pw.Attach( KPSUidHWRMPowerState, KHWRMChargingStatus ) );
       
   453     User::LeaveIfError( pw.Get( chargingstatus ));
       
   454     pw.Close();
       
   455 
       
   456     // Too low battery, power insufficient
       
   457     if ( batterylevel >= EBatteryLevelLevel4 )
       
   458         {
       
   459         enoughPower = ETrue;
       
   460         }
       
   461     // But charger is connected, power sufficient
       
   462     if ( ( chargingstatus != EChargingStatusError ) &&
       
   463             ( chargingstatus != EChargingStatusNotConnected ) )
       
   464         {
       
   465         enoughPower = ETrue;
       
   466         }
       
   467 
       
   468     FLOG( _L("Battery level: %d  (0..7), chargingstatus %d"),
       
   469             batterylevel, chargingstatus );
       
   470     FLOG( _L("CDevEncController::CheckBatteryL, ret=%d <<"), ( enoughPower ? 1 : 0 ) );
       
   471     return enoughPower;
       
   472 
       
   473 #endif // __WINS__
       
   474     }
       
   475 
       
   476 TInt CDevEncController::GetDEOperation()
       
   477     {
       
   478     return iDevEncOperation;
       
   479     }
       
   480 
       
   481 // End of file
       
   482