usbclasses/usbmscpersonality/src/CUsbActiveMscHandlerMdrv.cpp
changeset 0 1e05558e2206
child 3 47c263f7e521
equal deleted inserted replaced
-1:000000000000 0:1e05558e2206
       
     1 /*
       
     2 * Copyright (c) 2006-2008 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:  Main class for UsbMscPersonality
       
    15 *
       
    16 */
       
    17 
       
    18 #include <centralrepository.h>
       
    19 #include <AknSkinsInternalCRKeys.h>
       
    20 #include <usbman.h>
       
    21 #include <e32property.h>
       
    22 #include <tusbpersonalityparams.h>
       
    23 #include <startupdomainpskeys.h>
       
    24 #include <SettingsInternalCRKeys.h>
       
    25 #include "CUsbActiveMscHandlerMdrv.h"
       
    26 #include "CUsbMscPersonalityTimer.h"
       
    27 #include "debug.h"
       
    28 
       
    29 #include <featmgr.h> // for checking DE feature
       
    30 #include <DevEncSessionBase.h>
       
    31 #include <DevEncEngineConstants.h>
       
    32 
       
    33 // LITERALS
       
    34 _LIT( KMsFs,"MSFS.FSY" );
       
    35 _LIT( KMsFsyName, "MassStorageFileSystem" );
       
    36 _LIT( KFatFsyName, "Fat" );
       
    37 
       
    38 const TUint32 KDismountFatTimeoutValue = 5000000; // 5 seconds
       
    39 const TInt KMscDismountRetryCount = 3;
       
    40 const TUint32 KWaitMscToComplete = 50000; // 50 ms
       
    41 
       
    42 // ============================ MEMBER FUNCTIONS ==============================
       
    43 
       
    44 // ----------------------------------------------------------------------------
       
    45 // C++ default constructor can NOT contain any code, that
       
    46 // might leave.
       
    47 // ----------------------------------------------------------------------------
       
    48 //
       
    49 CUsbActiveMscHandler::CUsbActiveMscHandler(TUsbPersonalityParams& aPersonalityParams)
       
    50     : CUsbPersonalityPlugin(aPersonalityParams), iMountChanged(EFalse)
       
    51     {
       
    52     CActiveScheduler::Add( this );
       
    53     }
       
    54 
       
    55 // ----------------------------------------------------------------------------
       
    56 // Symbian 2nd phase constructor can leave.
       
    57 // ----------------------------------------------------------------------------
       
    58 //
       
    59 void CUsbActiveMscHandler::ConstructL()
       
    60     {
       
    61     FLOG( _L( "[USBWATCHER]\tCUsbActiveMscHandler: ConstructL" ) );
       
    62                     
       
    63     iDismountFatTimer = new (ELeave) CUsbMscPersonalityTimer(
       
    64         TCallBack(DismountFatCallBack, this), KDismountFatTimeoutValue);
       
    65     
       
    66     iMscState = EUsbMscStateIdle;
       
    67     User::LeaveIfError(iFs.Connect());
       
    68     iRepository = CRepository::NewL(KCRUidSecuritySettings); 
       
    69     LoadDevEncSessionL();
       
    70     }
       
    71 
       
    72 // ----------------------------------------------------------------------------
       
    73 // Two-phased constructor.
       
    74 // ----------------------------------------------------------------------------
       
    75 //
       
    76 CUsbActiveMscHandler* CUsbActiveMscHandler::NewL(TUsbPersonalityParams& aPersonalityParams)
       
    77     {
       
    78     CUsbActiveMscHandler* self 
       
    79         = new ( ELeave ) CUsbActiveMscHandler(aPersonalityParams);
       
    80     CleanupStack::PushL( self );
       
    81     self->ConstructL();
       
    82     CleanupStack::Pop(); // pop self
       
    83     return self;
       
    84     }
       
    85     
       
    86 // ----------------------------------------------------------------------------
       
    87 // Destructor
       
    88 // ----------------------------------------------------------------------------
       
    89 //
       
    90 CUsbActiveMscHandler::~CUsbActiveMscHandler()
       
    91     {
       
    92     FLOG( _L( "[USBWATCHER]\tCUsbActiveMscHandler::~CUsbActiveMscHandler" ) );
       
    93     
       
    94     if ( iMountChanged ) 
       
    95         {        
       
    96         UnmountMassStorage();
       
    97         }
       
    98 
       
    99     RemoveMassStorageFileSystem();
       
   100     
       
   101     Cancel();
       
   102     delete iDismountFatTimer;
       
   103 
       
   104     iDrives.Close();
       
   105     iFs.Close();
       
   106 
       
   107     delete iRepository;
       
   108     UnloadDevEncSession();    
       
   109     }
       
   110 
       
   111 // ----------------------------------------------------------------------------
       
   112 // State machine for the class.
       
   113 // ----------------------------------------------------------------------------
       
   114 //
       
   115 void CUsbActiveMscHandler::RunL()
       
   116     {
       
   117     FTRACE( FPrint(
       
   118         _L( "[USBWATCHER]\tCUsbActiveMscHandler::RunL error = %d" ),
       
   119         iStatus.Int() ) );
       
   120     FTRACE( FPrint(
       
   121         _L( "[USBWATCHER]\tCUsbActiveMscHandler::RunL iMscState = %d" ),
       
   122         iMscState ) );
       
   123     switch (iMscState)
       
   124         {
       
   125         case EUsbMscStateMounting:
       
   126             // DismountFatTimer is not exprired but RunL called with error
       
   127             if (KErrNone!=iStatus.Int())
       
   128               {
       
   129               /* Print error code, drive name and wait for timer to be expired expired (5sec) 
       
   130                * which will call forcibly dismount*/
       
   131               if(KErrNone!=iStatus.Int() )
       
   132                {
       
   133                FLOG( _L( "[USBWATCHER]\tCUsbActiveMscHandler::RunL*** Drive Dismounting failed ") );
       
   134                FTRACE( FPrint(
       
   135                    _L( "[USBWATCHER]\tCUsbActiveMscHandler::RunL*** Dismount failed on DriveIndex= %d" ),
       
   136                    iDriveIndex));
       
   137                FTRACE( FPrint(
       
   138                    _L( "[USBWATCHER]\tCUsbActiveMscHandler::RunL*** Dismount failed on Drive = %d" ),
       
   139                    iDrives[iDriveIndex]) );
       
   140                }
       
   141               }
       
   142             else
       
   143               {
       
   144               //dismount FAT done for one drive
       
   145               if (iDismountFatTimer)
       
   146                {
       
   147                iDismountFatTimer->Cancel();
       
   148                } 
       
   149               StartDismountFat();
       
   150               }
       
   151             break;
       
   152         case EUsbMscStateForciblyDismounting:
       
   153             // If Even ForciblyDismount failed with error we cannot do much here, and just print Error message 
       
   154             if(KErrNone!=iStatus.Int() )
       
   155               {
       
   156               FLOG( _L( "[USBWATCHER]\tCUsbActiveMscHandler::RunL*** FS has seroius dismounting problem" ) );
       
   157               }
       
   158             // we change the state and continue with other drives 
       
   159             iMscState = EUsbMscStateMounting;                 
       
   160             StartDismountFat();
       
   161             break; 
       
   162         default:
       
   163             break;
       
   164         }
       
   165     }
       
   166 
       
   167 // ----------------------------------------------------------------------------
       
   168 // Not possible to come here.
       
   169 // ----------------------------------------------------------------------------
       
   170 //
       
   171 TInt CUsbActiveMscHandler::RunError( TInt /*aError*/ )
       
   172     {
       
   173     return KErrNone;
       
   174     }
       
   175 
       
   176 // ----------------------------------------------------------------------------
       
   177 // This method always confirms the personality unloading.
       
   178 // ----------------------------------------------------------------------------
       
   179 //
       
   180 void CUsbActiveMscHandler::ConfirmPersonalityUnload(TRequestStatus& aStatus)
       
   181     {
       
   182     FTRACE( FPrint(
       
   183         _L( "[USBWATCHER]\tCUsbActiveMscHandler: ConfirmPersonalityUnload iMscState = %d" ),
       
   184         iMscState ) );
       
   185     iRequestStatus = &aStatus;
       
   186     aStatus = KRequestPending;
       
   187     CompleteRequest(KErrNone);
       
   188     }
       
   189 
       
   190 // ----------------------------------------------------------------------------
       
   191 // Called by personality handler when personality start needs to be
       
   192 // prepared. Adds mass storage file system. 
       
   193 // ----------------------------------------------------------------------------
       
   194 //
       
   195 void CUsbActiveMscHandler::PreparePersonalityStart(TRequestStatus& aStatus)
       
   196     {
       
   197     TInt ret = KErrNone;
       
   198     FLOG( _L( "[USBWATCHER]\tCUsbActiveMscHandler: PreparePersonalityStart" ) );
       
   199     iRequestStatus = &aStatus;
       
   200     aStatus = KRequestPending;
       
   201     iMscState = EUsbMscStateStarting;
       
   202     ret = AddMassStorageFileSystem();
       
   203     CompleteRequest(ret);
       
   204     }
       
   205 
       
   206 // ----------------------------------------------------------------------------
       
   207 // Called by personality handler when personality start needs to be finished.
       
   208 // ----------------------------------------------------------------------------
       
   209 //
       
   210 void CUsbActiveMscHandler::FinishPersonalityStart(TRequestStatus& aStatus)
       
   211     {
       
   212     FLOG( _L( "[USBWATCHER]\tCUsbActiveMscHandler: FinishPersonalityStart" ) );
       
   213     iRequestStatus = &aStatus;
       
   214     aStatus = KRequestPending;
       
   215     CompleteRequest(KErrNone);
       
   216     }
       
   217 
       
   218 // ----------------------------------------------------------------------------
       
   219 // Called by personality handler when personality stop needs to be prepared. 
       
   220 // Changes state of the personality.
       
   221 // ----------------------------------------------------------------------------
       
   222 //
       
   223 void CUsbActiveMscHandler::PreparePersonalityStop(TRequestStatus& aStatus)
       
   224     {
       
   225     FLOG( _L( "[USBWATCHER]\tCUsbActiveMscHandler: PreparePersonalityStop" ) );
       
   226 
       
   227     //Mounting may be ongoing
       
   228     iRequestStatus = NULL; //do not complete in DoCancel
       
   229     Cancel(); 
       
   230     
       
   231     iRequestStatus = &aStatus;
       
   232     aStatus = KRequestPending;
       
   233 
       
   234     iMscState = EUsbMscStateStopping;
       
   235 
       
   236     CompleteRequest(KErrNone);
       
   237     }
       
   238     
       
   239 // ----------------------------------------------------------------------------
       
   240 // Called by personality handler when personality stop needs to be finished. 
       
   241 // ----------------------------------------------------------------------------
       
   242 //
       
   243 void CUsbActiveMscHandler::FinishPersonalityStop(TRequestStatus& aStatus)
       
   244     {
       
   245     FLOG(_L("[USBWATCHER]\tCUsbActiveMscHandler: FinishPersonalityStop"));
       
   246 
       
   247     //Mounting may be ongoing
       
   248     iRequestStatus = NULL; //do not complete in DoCancel
       
   249     Cancel(); 
       
   250     
       
   251     //unmount in case device state not yet Undefined
       
   252     if (iMountChanged)
       
   253         {
       
   254         UnmountMassStorage();
       
   255         }
       
   256         
       
   257     RemoveMassStorageFileSystem();
       
   258 
       
   259     // Remove all queries shown by this personality
       
   260     iPersonalityParams.PersonalityNotifier().CancelQuery(KQueriesNotifier);
       
   261 
       
   262     iMscState = EUsbMscStateIdle;
       
   263 
       
   264     iRequestStatus = &aStatus;
       
   265     aStatus = KRequestPending;
       
   266     CompleteRequest(KErrNone);
       
   267     }
       
   268 
       
   269 // ----------------------------------------------------------------------------
       
   270 // Indicates USB device state change to personality. 
       
   271 // There is no need to Cancel, because Confirm unload can not be ongoing 
       
   272 // before Address state. 
       
   273 // ----------------------------------------------------------------------------
       
   274 // 
       
   275 void CUsbActiveMscHandler::StateChangeNotify( TUsbDeviceState aState )
       
   276     {
       
   277     FTRACE( FPrint(
       
   278         _L( "[USBWATCHER]\tCUsbActiveMscHandler::StateChangeNotify aState = %d" ),
       
   279         aState ) );
       
   280     switch( aState )
       
   281         {        
       
   282         //Note that Address state may be caused also by cable disconnection.
       
   283         case EUsbDeviceStateAddress: 
       
   284             {
       
   285             //Do not start mounting if already ongoing 
       
   286             //e.g. fast state changes Address-->Suspended-->Address
       
   287             if ( !iMountChanged && (GetDrives() == KErrNone) )
       
   288                 {
       
   289                 if (iDrives.Count())
       
   290                     {
       
   291                     iDriveIndex = iDrives.Count();
       
   292                     StartDismountFat();
       
   293                     }
       
   294                 else
       
   295                     {
       
   296                     if ( GlobalSystemState() == EUsbGSStateCategoryNormal )
       
   297                         {
       
   298                         // if the error is something abnormal, note still needs to be shown
       
   299                         iQueryParams().iQuery = EUSBStorageMediaFailure;
       
   300                         iPersonalityParams.PersonalityNotifier().ShowQuery(
       
   301                             KQueriesNotifier, iQueryParams, iDummy);
       
   302                         }            
       
   303                     }    
       
   304                 }
       
   305             
       
   306             }
       
   307             break;
       
   308         case EUsbDeviceStateUndefined: 
       
   309             {
       
   310             if (iMountChanged)
       
   311                 {
       
   312                 UnmountMassStorage();
       
   313                 iMscState = EUsbMscStateIdle;
       
   314                 }
       
   315             }
       
   316             break;
       
   317         default:
       
   318             break;
       
   319         }
       
   320     FLOG(_L("[USBWATCHER]\tCUsbActiveMscHandler::StateChangeNotify completed"));
       
   321     }
       
   322 
       
   323 // ----------------------------------------------------------------------------
       
   324 // Start FAT dismounting sequence.
       
   325 // ----------------------------------------------------------------------------
       
   326 // 
       
   327 void CUsbActiveMscHandler::StartDismountFat()
       
   328     {
       
   329     FLOG(_L("[USBWATCHER]\tCUsbActiveMscHandler::StartDismountFat"));
       
   330     iMountChanged = ETrue;
       
   331 
       
   332     if (!iDriveIndex)
       
   333         {
       
   334         //FAT dismounted from all the drives
       
   335         MountMassStorage();            
       
   336         //MSFS mounted to all the drives
       
   337         iMscState = EUsbMscStateFileTransfer;
       
   338         }
       
   339     else
       
   340         {
       
   341         --iDriveIndex;
       
   342         FTRACE( FPrint(
       
   343             _L( "[USBWATCHER]\tCUsbActiveMscHandler::StartDismountFat iDriveIndex = %d " ),
       
   344             iDriveIndex) );
       
   345         // see if FAT file system exists in drive and if it does, try to dismount it
       
   346         DismountFat(iDrives[iDriveIndex]);
       
   347         }
       
   348     }
       
   349 
       
   350 // ----------------------------------------------------------------------------
       
   351 // Add mass storage file system
       
   352 // ----------------------------------------------------------------------------
       
   353 // 
       
   354 TInt CUsbActiveMscHandler::AddMassStorageFileSystem()
       
   355     {
       
   356     FLOG(_L("[USBWATCHER]\tCUsbActiveMscHandler::AddMassStorageFileSystem"));
       
   357 
       
   358     TInt ret = KErrNone;
       
   359     
       
   360     // To be done only once during the lifetime of this object:
       
   361     // 5b. add Mass Storage File System to the file server.
       
   362     // (done like this to avoid Symbian crash)
       
   363     if (!iMsfsAdded)
       
   364         {
       
   365         FLOG( _L( "[USBWATCHER]\tCUsbActiveMscHandler::AddMassStorageFileSystem: Loading MSFS" ) );
       
   366         ret = iFs.AddFileSystem(KMsFs);
       
   367         if ((ret != KErrNone) && (ret != KErrAlreadyExists))
       
   368             {
       
   369             FTRACE( FPrint(
       
   370                 _L( "[USBWATCHER]\tCUsbActiveMscHandler::AddMassStorageFileSystem: ERROR: MSFS loading failed. Code: %d " ),
       
   371                 ret) );
       
   372             }
       
   373         else
       
   374             {
       
   375             iMsfsAdded = ETrue;
       
   376             if (ret == KErrAlreadyExists)
       
   377                 {
       
   378                 ret = KErrNone;
       
   379                 }
       
   380             }
       
   381         }
       
   382     FTRACE(FPrint(_L("[USBWATCHER]\tCUsbActiveMscHandler::AddMassStorageFileSystem: ret=%d"), ret));
       
   383     return ret;
       
   384     }
       
   385 
       
   386 // ----------------------------------------------------------------------------
       
   387 // Remove mass storage file system (MSFS) from file server
       
   388 // ----------------------------------------------------------------------------
       
   389 // 
       
   390 void CUsbActiveMscHandler::RemoveMassStorageFileSystem()
       
   391     {
       
   392     FLOG(_L("[USBWATCHER]\tCUsbActiveMscHandler::RemoveMassStorageFileSystem"));
       
   393     if (iMsfsAdded)
       
   394         {
       
   395         if (iFs.RemoveFileSystem(KMsFsyName) != KErrNone)
       
   396             {
       
   397             FLOG( _L( "[USBWATCHER]\tCUsbActiveMscHandler: RemoveMassStorageFileSystem: MSFS not removed from file server." ) );
       
   398             }
       
   399         else
       
   400             {
       
   401             iMsfsAdded = EFalse;
       
   402             }
       
   403         }    
       
   404     }
       
   405 
       
   406 // ----------------------------------------------------------------------------
       
   407 // Mount mass storage to all drives. Does not mount in charging state if device 
       
   408 // locked.
       
   409 // ----------------------------------------------------------------------------
       
   410 // 
       
   411 void CUsbActiveMscHandler::MountMassStorage()
       
   412     {
       
   413     FLOG(_L("[USBWATCHER]\tCUsbActiveMscHandler::MountMassStorage"));
       
   414     TInt ret = KErrNone;
       
   415     TBool locked = DeviceLocked();
       
   416     TUsbGlobalSystemState state = GlobalSystemState();
       
   417     FTRACE(FPrint(_L("[USBWATCHER]\tCUsbActiveMscHandler::MountMassStorage: locked=%d, state=%d"), locked, state));
       
   418 
       
   419     if ( (state == EUsbGSStateCategoryNormal) || 
       
   420        (!locked && (state==EUsbGSStateCharging)) )
       
   421         {
       
   422         for ( TInt driveIndex = iDrives.Count() - 1; driveIndex >= 0; driveIndex-- )
       
   423             {
       
   424             // Try mount Mass Storage File System into drive.
       
   425             ret = TryMountMassStorage(iDrives[driveIndex]);
       
   426             if (ret != KErrNone)
       
   427                 {
       
   428                 FTRACE(FPrint(_L("[USBWATCHER]\tCUsbActiveMscHandler::MountMassStorage: driveIndex=%d ret=%d"), 
       
   429                     driveIndex, ret));
       
   430                 }
       
   431             }
       
   432         }
       
   433     }
       
   434 
       
   435 // ----------------------------------------------------------------------------
       
   436 // Return removable drives in system.
       
   437 // ----------------------------------------------------------------------------
       
   438 //  
       
   439 TInt CUsbActiveMscHandler::GetDrives()
       
   440     {    
       
   441     TInt i;
       
   442     TInt ret = KErrNone;
       
   443     
       
   444     // go through drives A-Z except for C and Z
       
   445     TDriveInfo driveInfo;
       
   446     iDrives.Reset();
       
   447     
       
   448     for (i = EDriveA; i < EDriveZ; i++)
       
   449         {
       
   450         // skip drive C: and get drive info
       
   451         if ( EDriveC == i ) 
       
   452             {
       
   453             continue;
       
   454             }
       
   455         
       
   456     // unmounting FAT from the card when a decrypting/encrypting operation
       
   457     // is ongoing will corrupt the card, so it must be prevented.      
       
   458 	if( ( i == EDriveE ) || ( i == EDriveF ) )
       
   459 		{
       
   460         if(IsEncProtectionRequired(i))
       
   461 			{
       
   462 			FTRACE( FPrint( _L(" Skipping drive %d"), i));
       
   463 			continue;
       
   464 			}
       
   465 		}
       
   466         iFs.Drive(driveInfo, i);
       
   467         
       
   468         // if drive is not removable and local, it can be skipped
       
   469         if ((driveInfo.iDriveAtt & (KDriveAttRemovable | KDriveAttLocal))
       
   470             != (KDriveAttRemovable | KDriveAttLocal))
       
   471             {
       
   472             continue;
       
   473             }    
       
   474         
       
   475         FTRACE( FPrint(
       
   476             _L( "[USBWATCHER]\tCUsbActiveMscHandler::GetDrives, removable drive %d: MediaAtt: %d" ),
       
   477             i,driveInfo.iMediaAtt ) );
       
   478         FTRACE( FPrint(
       
   479             _L( "[USBWATCHER]\tCUsbActiveMscHandler::GetDrives, removable drive %d: Media info: %d" ),
       
   480             i, driveInfo.iType ) );
       
   481 
       
   482         // The memory card may be locked. No memory card password query is shown. 
       
   483         
       
   484         FTRACE(FPrint(
       
   485                _L("[USBWATCHER]\tCUsbActiveMscHandler::GetDrives: MMC inserted into drive %d"),
       
   486                i) );
       
   487         ret = iDrives.Append(i);
       
   488         }
       
   489         
       
   490     if (!iDrives.Count())
       
   491         {
       
   492         FLOG( _L( "[USBWATCHER]\tCUsbActiveMscHandler::GetDrives: No removable drives found." ) );
       
   493 
       
   494         iQueryParams().iQuery = EUSBStorageMediaFailure;
       
   495         iPersonalityParams.PersonalityNotifier().ShowQuery(KQueriesNotifier, iQueryParams, iDummy);
       
   496 
       
   497         return KErrNotFound;
       
   498         }
       
   499         
       
   500     return ret;
       
   501     }
       
   502 
       
   503 // ----------------------------------------------------------------------------
       
   504 // Dismounts FAT File System.
       
   505 // ----------------------------------------------------------------------------
       
   506 //   
       
   507 void CUsbActiveMscHandler::DismountFat( TInt aDrive )
       
   508     {
       
   509     FLOG( _L( "[USBWATCHER]\tCUsbActiveMscHandler::DismountFat" ) );
       
   510     
       
   511     //nothing to do if FAT file system not in aDrive
       
   512     if ( GetDriveFileSystem(aDrive) != EFsyFat )
       
   513         {
       
   514         FTRACE( FPrint(
       
   515             _L( "[USBWATCHER]\tCUsbActiveMscHandler::DismountFat: FAT FSY not found in drive %d" ),
       
   516             aDrive ) );
       
   517         //continue to the next drive
       
   518         TRequestStatus* status = &iStatus;
       
   519         User::RequestComplete(status, KErrNotFound);
       
   520         SetActive();
       
   521         }
       
   522     else
       
   523         {
       
   524         //FAT file system exists in aDrive -> dismount it
       
   525         iFs.NotifyDismount(iDrives[iDriveIndex], iStatus, EFsDismountNotifyClients);
       
   526         SetActive();
       
   527 
       
   528         //Give some time for applications before dismounting forcefully
       
   529         iDismountFatTimer->Start();
       
   530         }
       
   531 
       
   532     iMscState = EUsbMscStateMounting;
       
   533     }
       
   534 
       
   535 // ----------------------------------------------------------------------------
       
   536 // Mounts Mass Storage File System into drive.
       
   537 // ----------------------------------------------------------------------------
       
   538 //  
       
   539 TInt CUsbActiveMscHandler::TryMountMassStorage( TInt aDrive )
       
   540     {
       
   541     FLOG( _L( "[USBWATCHER]\tCUsbActiveMscHandler::TryMountMassStorage" ) );
       
   542     
       
   543     TInt ret(KErrNone);
       
   544     
       
   545     ret = iFs.MountFileSystem( KMsFsyName, aDrive );
       
   546     FTRACE( FPrint(
       
   547         _L( "[USBWATCHER]\tCUsbActiveMscHandler::TryMountMassStorage: fs mount ret %d" ),
       
   548         ret ) );
       
   549     
       
   550     if ( ret == KErrNotSupported )
       
   551         {
       
   552         // there is an error in environment and MSFS has been mounted into
       
   553         // drive but mounting has been unsuccessful -> remove it
       
   554         FTRACE( FPrint(
       
   555             _L( "[USBWATCHER]\tCUsbActiveMscHandler::TryMountMassStorage: MS FSY not supported in drive %d" ),
       
   556             aDrive ) );
       
   557         return ret;
       
   558         }
       
   559     else if ((ret != KErrNone) && (ret != KErrNotReady))
       
   560         {
       
   561         FTRACE( FPrint(
       
   562             _L( "[USBWATCHER]\tCUsbActiveMscHandler::TryMountMassStorage: ERROR %d in MS FSY mounting in drive %d" ),
       
   563             ret, aDrive ) );
       
   564         return ret;
       
   565         }
       
   566 
       
   567     FTRACE( FPrint(
       
   568         _L( "[USBWATCHER]\tCUsbActiveMscHandler::TryMountMassStorage: MS FSY mounted in drive %d" ),
       
   569         aDrive ) );
       
   570 
       
   571     return KErrNone;
       
   572     }
       
   573 
       
   574 // ----------------------------------------------------------------------------
       
   575 // Dismounts Mass Storage File System from the drive.
       
   576 // ----------------------------------------------------------------------------
       
   577 //  
       
   578 TInt CUsbActiveMscHandler::TryDismountMassStorage( TInt aDrive )
       
   579     {
       
   580     FTRACE( FPrint(
       
   581         _L( "[USBWATCHER]\tCUsbActiveMscHandler::TryDismountMassStorage: drive %d" ),
       
   582         aDrive ) );
       
   583 
       
   584     // initializations
       
   585     TInt ret = KErrNone;
       
   586     TInt numTry = KMscDismountRetryCount; // How many times to try to dismount the drive
       
   587     
       
   588     //only dismount if mass storage mounted
       
   589     if ( GetDriveFileSystem(aDrive) == EFsyMassStorage )
       
   590         {
       
   591         while ( numTry-- )
       
   592             {
       
   593             ret = iFs.DismountFileSystem( KMsFsyName, aDrive );
       
   594             if ( ret != KErrNone )
       
   595                 {
       
   596                 if ( ret == KErrInUse )
       
   597                     {
       
   598                     // It may be that USB mass storage transfer is still in use 
       
   599                     // when USB File transfer mode is tried to be distangled (this
       
   600                     // method is entered). Wait for a while and try again.
       
   601                     FTRACE( FPrint(
       
   602                          _L( "[USBWATCHER]\tCUsbActiveMscHandler: TryDismountMassStorage: Waiting MSFS dismounting for drive %d" ),
       
   603                          aDrive ) );
       
   604                     RTimer timer;
       
   605                     TRequestStatus timerStatus;
       
   606                     timer.CreateLocal(); // Create for this thread
       
   607                     timer.After(timerStatus, KWaitMscToComplete);
       
   608                     User::WaitForRequest( timerStatus );
       
   609                     if ( timerStatus != KErrNone )
       
   610                         {
       
   611                         FTRACE( FPrint(
       
   612                                 _L( "[USBWATCHER]\tCUsbActiveMscHandler::TryDismountMassStorage: ERROR: %d wait timer fails." ), 
       
   613                                 timerStatus.Int() ) );
       
   614                         }
       
   615                     timer.Close();
       
   616                     }
       
   617                 else
       
   618                     {
       
   619                     FTRACE( FPrint(
       
   620                         _L( "[USBWATCHER]\tCUsbActiveMscHandler: TryDismountMassStorage: ERROR %d in dismounting MSFS from drive %d" ),
       
   621                         ret, aDrive ) );
       
   622                     break;
       
   623                     }
       
   624                 }
       
   625             else
       
   626                 {
       
   627                 FTRACE( FPrint(
       
   628                     _L( "[USBWATCHER]\tCUsbActiveMscHandler: TryDismountMassStorage: MSFS dismounted from drive %d" ),
       
   629                     aDrive ) );
       
   630                 break;
       
   631                 }
       
   632             } //while
       
   633             
       
   634             if ( ret == KErrInUse )
       
   635                 {
       
   636                 FLOG(_L("[USBWATCHER]\tCUsbActiveMscHandler::TryDismountMassStorage using force"));
       
   637                 TRequestStatus dismountStatus;
       
   638                 iFs.NotifyDismount(aDrive, dismountStatus, EFsDismountForceDismount);
       
   639                 User::WaitForRequest(dismountStatus);
       
   640                 ret = dismountStatus.Int();
       
   641                 }
       
   642             
       
   643         }
       
   644         else 
       
   645             {
       
   646             FTRACE( FPrint(
       
   647                 _L( "[USBWATCHER]\tCUsbActiveMscHandler: TryDismountMassStorage: No MSFS on drive %d" ),
       
   648                 aDrive ) );
       
   649             }
       
   650 
       
   651     FTRACE( FPrint(
       
   652         _L( "[USBWATCHER]\tCUsbActiveMscHandler::TryDismountMassStorage: returning %d" ),
       
   653         ret ) );
       
   654     return ret;
       
   655     }
       
   656 
       
   657 // ----------------------------------------------------------------------------
       
   658 // Mounts FAT File System to the drive
       
   659 // ----------------------------------------------------------------------------
       
   660 //  
       
   661 TInt CUsbActiveMscHandler::TryMountFat( TInt aDrive )
       
   662     {
       
   663     FTRACE( FPrint(
       
   664         _L( "[USBWATCHER]\tCUsbActiveMscHandler::TryMountFat: drive %d" ),
       
   665         aDrive ) );
       
   666 
       
   667     // initializations
       
   668     TInt ret = KErrNone;
       
   669 
       
   670     // Mount back FAT only if there is no mounted file system
       
   671     if ( GetDriveFileSystem(aDrive) == EFsyNone )
       
   672         {
       
   673         ret = iFs.MountFileSystem( KFatFsyName, aDrive );
       
   674 
       
   675         FTRACE( FPrint(
       
   676             _L( "[USBWATCHER]\tCUsbActiveMscHandler: TryMountFat: return %d in mounting FAT into drive %d" ),
       
   677             ret, aDrive ) );
       
   678 
       
   679         if (ret != KErrNone)
       
   680             {
       
   681             FTRACE( FPrint(
       
   682                 _L( "[USBWATCHER]\tCUsbActiveMscHandler: TryMountFat: ERROR %d in mounting FAT into drive %d" ),
       
   683                 ret, aDrive ) );
       
   684             }
       
   685         else
       
   686             {
       
   687             FTRACE( FPrint(
       
   688                 _L( "[USBWATCHER]\tCUsbActiveMscHandler: TryMountFat: FAT mounted into drive %d" ),
       
   689                 aDrive ) );
       
   690             }
       
   691         }
       
   692 
       
   693     return ret;
       
   694     }
       
   695 
       
   696 // ----------------------------------------------------------------------------
       
   697 // Standard active object cancellation function.
       
   698 // ----------------------------------------------------------------------------
       
   699 //
       
   700 void CUsbActiveMscHandler::DoCancel()
       
   701     {
       
   702     FTRACE( FPrint(
       
   703         _L( "[USBWATCHER]\tCUsbActiveMscHandler: DoCancel iMscState=%d" ),
       
   704         iMscState ) );
       
   705 
       
   706     //Remove all notes. The state may have changed after the confirm unload
       
   707     //is started.
       
   708     if (iMscState != EUsbMscStateForciblyDismounting)
       
   709         {
       
   710         iPersonalityParams.PersonalityNotifier().CancelAll();
       
   711         }
       
   712 
       
   713     switch (iMscState)
       
   714         {
       
   715         case EUsbMscStateStarting:
       
   716         case EUsbMscStateStopping:
       
   717             break;
       
   718 
       
   719         case EUsbMscStateMounting:
       
   720         case EUsbMscStateForciblyDismounting:
       
   721             if (iDismountFatTimer)
       
   722                 {
       
   723                 iDismountFatTimer->Cancel();
       
   724                 }
       
   725             iFs.NotifyDismountCancel();
       
   726             break;
       
   727                         
       
   728         default:
       
   729             FLOG(_L("[USBWATCHER]\tCUsbActiveMscHandler::DoCancel: ERROR"));        
       
   730             break;
       
   731         }
       
   732 
       
   733     CompleteRequest(KErrCancel);
       
   734     }
       
   735     
       
   736 // ----------------------------------------------------------------------------
       
   737 // Complete request.
       
   738 // ----------------------------------------------------------------------------
       
   739 //
       
   740 void CUsbActiveMscHandler::CompleteRequest(TInt aError)
       
   741     {
       
   742     FLOG(_L("[USBWATCHER]\tCUsbActiveMscHandler::CompleteRequest"));
       
   743 
       
   744     if (iRequestStatus)
       
   745         {
       
   746         User::RequestComplete(iRequestStatus, aError);
       
   747         iRequestStatus = NULL;
       
   748         }
       
   749     }
       
   750     
       
   751 // ----------------------------------------------------------------------------
       
   752 // If client doesn't allow us to do dismount, let's force it.
       
   753 // ----------------------------------------------------------------------------
       
   754 //
       
   755 TInt CUsbActiveMscHandler::DismountFatCallBack(TAny* aPtr)
       
   756     {
       
   757     FLOG(_L("[USBWATCHER]\tCUsbActiveMscHandler::DismountFatCallBack"));        
       
   758     
       
   759     CUsbActiveMscHandler* handler = static_cast<CUsbActiveMscHandler *>(aPtr);
       
   760     
       
   761     handler->ForciblyDismountFat();
       
   762     
       
   763     return KErrNone;
       
   764     }
       
   765     
       
   766 // ----------------------------------------------------------------------------
       
   767 // Forced dismount is done over here.
       
   768 // ----------------------------------------------------------------------------
       
   769 //
       
   770 void CUsbActiveMscHandler::ForciblyDismountFat()
       
   771     {    
       
   772     FLOG(_L("[USBWATCHER]\tCUsbActiveMscHandler::ForciblyDismountFat"));
       
   773     
       
   774     //cancel won't complete, since there is no client request ongoing
       
   775     iMscState = EUsbMscStateForciblyDismounting; //do not close mode query
       
   776     Cancel();
       
   777     FTRACE( FPrint(
       
   778         _L( "[USBWATCHER]\tCUsbActiveMscHandler::ForciblyDismountFat DriveIndex= %d" ),
       
   779         iDriveIndex));
       
   780     FTRACE( FPrint(
       
   781         _L( "[USBWATCHER]\tCUsbActiveMscHandler::ForciblyDismountFat on Drive = %d" ),
       
   782         iDrives[iDriveIndex]) );
       
   783     iFs.NotifyDismount(iDrives[iDriveIndex], iStatus, EFsDismountForceDismount);
       
   784     SetActive();
       
   785     }
       
   786 
       
   787 
       
   788 TUsbGlobalSystemState CUsbActiveMscHandler::GlobalSystemState()
       
   789     {
       
   790     TInt state = EUsbGSStateUnknown;
       
   791     TUsbGlobalSystemState usbGlobalSysState = EUsbGSStateUnknown;
       
   792     TInt error = RProperty::Get( KPSUidStartup, KPSGlobalSystemState, state );
       
   793     FTRACE(FPrint(_L("[USBWATCHER]\tCUsbActiveMscHandler::GlobalSystemState: error %d, state %d"), error, state));
       
   794     if ( (state == ESwStateNormalRfOn) || 
       
   795          (state == ESwStateNormalRfOff) || 
       
   796          (state == ESwStateNormalBTSap) )
       
   797         {
       
   798         usbGlobalSysState = EUsbGSStateCategoryNormal;
       
   799         }
       
   800     else if ( state == ESwStateCharging )
       
   801         {
       
   802         usbGlobalSysState = EUsbGSStateCharging;
       
   803         }
       
   804         
       
   805     return usbGlobalSysState;;
       
   806     }
       
   807 
       
   808 
       
   809 // ----------------------------------------------------------------------------
       
   810 // Determine whether the device is locked or not
       
   811 // ----------------------------------------------------------------------------
       
   812 //
       
   813 TBool CUsbActiveMscHandler::DeviceLocked()
       
   814     {
       
   815     TBool locked(EFalse);
       
   816     if ( GlobalSystemState() == EUsbGSStateCharging )
       
   817         {
       
   818         if (AutoLockTime()>0 || AutoLockStatus()>0 )
       
   819             {
       
   820             locked=ETrue;
       
   821             }
       
   822         }
       
   823     
       
   824     return locked;
       
   825     }
       
   826 
       
   827 // ----------------------------------------------------------------------------
       
   828 // Read Lock time settings
       
   829 // Meant to be used only in Charging mode
       
   830 // ----------------------------------------------------------------------------
       
   831 //
       
   832 TInt CUsbActiveMscHandler::AutoLockTime()
       
   833     {
       
   834     TInt lockTime(0);
       
   835     
       
   836     TInt ret = iRepository->Get(KSettingsAutoLockTime, lockTime);
       
   837     FTRACE(FPrint(_L("[USBWATCHER]\tCUsbActiveMscHandler::LockeTime: ret=%d. LockTime=%d"), ret, lockTime));
       
   838 
       
   839     return lockTime;
       
   840     }
       
   841 
       
   842 // ----------------------------------------------------------------------------
       
   843 // Read Lock status settings
       
   844 // Meant to be used only in Charging mode
       
   845 // ----------------------------------------------------------------------------
       
   846 //
       
   847 TInt CUsbActiveMscHandler::AutoLockStatus()
       
   848     {    
       
   849     TInt lockStatus(0);
       
   850     
       
   851     TInt ret = iRepository->Get(KSettingsAutolockStatus, lockStatus);
       
   852     FTRACE(FPrint(_L("[USBWATCHER]\tCUsbActiveMscHandler::AutoLockStatus: ret=%d. lockStatus=%d"), ret, lockStatus));
       
   853     
       
   854     return lockStatus;    
       
   855     }
       
   856     
       
   857 // ----------------------------------------------------------------------------
       
   858 // Unmount mass storage dismounts mass storage and mounts FAT for all drives. 
       
   859 // ----------------------------------------------------------------------------
       
   860 // 
       
   861 void CUsbActiveMscHandler::UnmountMassStorage()
       
   862     {
       
   863     FLOG(_L("[USBWATCHER]\tCUsbActiveMscHandler::UnmountMassStorage"));        
       
   864     //Mounting may be ongoing. Note that the confirm unload query and the mode
       
   865     //query are closed. 
       
   866     Cancel();
       
   867 
       
   868     TInt index = iDrives.Count();
       
   869     while ( index > 0 )
       
   870         {
       
   871         --index;
       
   872         TInt drive = iDrives[index];
       
   873         // First dismount mounted Mass Storage File System
       
   874         TryDismountMassStorage(iDrives[index]);
       
   875         // Then mount back previously dismounted FAT File System
       
   876         TryMountFat(iDrives[index]);
       
   877         }
       
   878     iMountChanged = EFalse;
       
   879     }
       
   880 
       
   881 // ----------------------------------------------------------------------------
       
   882 // Return the mounted file system. 
       
   883 // ----------------------------------------------------------------------------
       
   884 // 
       
   885 TUsbFileSystem CUsbActiveMscHandler::GetDriveFileSystem( TInt aDrive )
       
   886     {
       
   887     FLOG(_L("[USBWATCHER]\tCUsbActiveMscHandler::GetDriveFileSystem"));
       
   888     TUsbFileSystem fileSystem = EFsyNone;
       
   889     TBuf<KMaxFullName> name;
       
   890     TInt err = iFs.FileSystemName( name, aDrive );
       
   891     if ( (err == KErrNone) && (name.Length() > 0) )
       
   892         {
       
   893 
       
   894         FTRACE( FPrint(_L( "[USBWATCHER]\tCUsbActiveMscHandler: GetDriveFileSystem: aDrive=%d name=%S" ),
       
   895                          aDrive, &name ) );
       
   896         
       
   897         if (name.CompareF( KMsFsyName ) == 0)
       
   898             {
       
   899             fileSystem = EFsyMassStorage;
       
   900             }
       
   901         else if (name.CompareF( KFatFsyName ) == 0)
       
   902             {
       
   903             fileSystem = EFsyFat;
       
   904             }        
       
   905         }
       
   906     FTRACE( FPrint(
       
   907         _L( "[USBWATCHER]\tCUsbActiveMscHandler: GetDriveFileSystem: filesystem %d on drive %d" ),
       
   908         fileSystem, aDrive ) );
       
   909                 
       
   910     return fileSystem;
       
   911     }
       
   912 
       
   913 // ----------------------------------------------------------------------------
       
   914 // Return whether device encryption is supported or not.
       
   915 // ----------------------------------------------------------------------------
       
   916 // 
       
   917 TBool CUsbActiveMscHandler::IsDeviceEncryptionSupportedL()
       
   918     {
       
   919     FLOG(_L("[USBWATCHER]\tCUsbActiveMscHandler::IsDeviceEncryptionSupportedL >>"));
       
   920     
       
   921     TBool ret(EFalse);
       
   922     
       
   923     FeatureManager::InitializeLibL();
       
   924     ret = FeatureManager::FeatureSupported(KFeatureIdFfDeviceEncryptionFeature);
       
   925     FeatureManager::UnInitializeLib();
       
   926  
       
   927     FTRACE(FPrint(_L("[USBWATCHER]\tCUsbActiveMscHandler::IsDeviceEncryptionSupportedL, ret = %d <<"), ret));
       
   928     return ret;
       
   929     }
       
   930 	
       
   931 // ----------------------------------------------------------------------------
       
   932 // Return device encryption info for further protection
       
   933 // ----------------------------------------------------------------------------
       
   934 // 
       
   935 TBool CUsbActiveMscHandler::IsEncProtectionRequired(const TInt& aDriveLetter)
       
   936     {
       
   937 	TBool ret = ETrue;
       
   938     FLOG(_L("[USBWATCHER]\tCUsbActiveMscHandler::GetEncProtectionInfo >>"));
       
   939 	
       
   940     // Is device encryption feature supported?
       
   941     TRAPD( r, ret = ( IsDeviceEncryptionSupportedL() ) );    
       
   942     if(r)
       
   943 		{
       
   944 		ret = EFalse;
       
   945 		}
       
   946 	
       
   947     // Get the enctrytion operation code
       
   948     if(ret)
       
   949         {
       
   950         FLOG(_L("Check drives for busy status"));
       
   951 	
       
   952         TInt encDriverStatus;
       
   953 		
       
   954         if(!iDevEncSession)
       
   955             {
       
   956             FTRACE(FPrint(_L("[USBWATCHER]\tCUsbActiveMscHandler::IsEncProtectionRequired: iDevEncSession is NULL")));
       
   957             User::Panic(_L("[USBWATCHER]DevEncCommonUtil Not Found"), KErrNotSupported);
       
   958             }    
       
   959         else
       
   960             {
       
   961             iDevEncSession->SetDrive( (TDriveNumber)aDriveLetter );
       
   962             TInt errCode = iDevEncSession->Connect();
       
   963             if( !errCode )
       
   964                 {
       
   965                 errCode = iDevEncSession->DiskStatus( encDriverStatus );
       
   966                 }
       
   967             iDevEncSession->Close();
       
   968             FTRACE(FPrint(_L("[USBWATCHER]\tCUsbActiveMscHandler::GetDrives: error %d, state %d"), errCode, encDriverStatus));
       
   969 
       
   970             ret = ( ( errCode == KErrNone ) && 
       
   971                               ( ( encDriverStatus == EEncrypting ) || ( encDriverStatus == EDecrypting ) ) );
       
   972             }
       
   973         }
       
   974 
       
   975     FTRACE(FPrint(_L("[USBWATCHER]\tCUsbActiveMscHandler::GetEncProtectionInfo, aDriveLetter= %d, ret= <<"), aDriveLetter,ret));
       
   976 	
       
   977 	return ret;
       
   978 	}
       
   979 void CUsbActiveMscHandler::LoadDevEncSessionL()
       
   980     {
       
   981     FLOG(_L("[USBWATCHER]\tCUsbActiveMscHandler::LoadDevEncSessionL >> "));
       
   982     
       
   983     if (!iDevEncSession)
       
   984         {
       
   985 	      TInt err = iLibrary.Load(KDevEncCommonUtils);	 
       
   986         if (err != KErrNone)
       
   987             {
       
   988             FTRACE(FPrint(_L("Error in finding the library... %d"), err));
       
   989             return;
       
   990             }
       
   991         TLibraryFunction entry = iLibrary.Lookup(1);
       
   992          
       
   993         if (!entry)
       
   994             {
       
   995             FLOG(_L("Error in loading the library..."));
       
   996             User::Leave(KErrBadLibraryEntryPoint);
       
   997             }
       
   998         iDevEncSession = (CDevEncSessionBase*) entry();
       
   999         }
       
  1000     FLOG(_L("[USBWATCHER]\tCUsbActiveMscHandler::LoadDevEncSessionL << "));
       
  1001     }
       
  1002 
       
  1003 void CUsbActiveMscHandler::UnloadDevEncSession()
       
  1004     {
       
  1005     FLOG(_L("[USBWATCHER]\tCUsbActiveMscHandler::UnloadDevEncSession >> "));
       
  1006     
       
  1007     if (iDevEncSession)
       
  1008         {
       
  1009         delete iDevEncSession;
       
  1010         iDevEncSession = NULL;
       
  1011         iLibrary.Close();
       
  1012         }
       
  1013     
       
  1014     FLOG(_L("[USBWATCHER]\tCUsbActiveMscHandler::UnloadDevEncSession << "));
       
  1015     }
       
  1016 // End of file