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