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