harvester/monitorplugins/mmcplugin/src/mmcusbao.cpp
changeset 0 c53acadfccc6
child 20 f23c07ec56e2
equal deleted inserted replaced
-1:000000000000 0:c53acadfccc6
       
     1 /*
       
     2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Monitors Usb insertions and removals.*
       
    15 */
       
    16 
       
    17 
       
    18 #include <usbmsshared.h>
       
    19 
       
    20 #include "harvesterlog.h"
       
    21 #include "harvestercommon.h"
       
    22 #include "mmcusbao.h"
       
    23 #include "mmcmonitorplugin.h"
       
    24 
       
    25 CMMCUsbAO* CMMCUsbAO::NewL()
       
    26     {
       
    27     WRITELOG( "CMMCUsbAO::NewL" );
       
    28     
       
    29     CMMCUsbAO* self = new (ELeave) CMMCUsbAO();
       
    30     CleanupStack::PushL( self );
       
    31     self->ConstructL();
       
    32     CleanupStack::Pop( self );
       
    33     return self;
       
    34     }
       
    35 
       
    36 CMMCUsbAO::CMMCUsbAO() : CActive( KHarvesterPriorityMonitorPlugin ), iObserver( NULL )
       
    37     {
       
    38     iDrive = 0;
       
    39     iMassStorageMode = EFalse;
       
    40     }
       
    41 
       
    42 void CMMCUsbAO::ConstructL()
       
    43     {
       
    44     WRITELOG( "CMMCUsbAO::ConstructL" );
       
    45     
       
    46     CActiveScheduler::Add( this );
       
    47     
       
    48     User::LeaveIfError( iFs.Connect() );
       
    49 
       
    50     User::LeaveIfError( iDriveState.Attach( 
       
    51     		KUsbMsDriveState_Category, EUsbMsDriveState_DriveStatus ) );
       
    52     }
       
    53 
       
    54 CMMCUsbAO::~CMMCUsbAO()
       
    55     {
       
    56     WRITELOG( "CMMCUsbAO::~CMMCUsbAO" );
       
    57     StopMonitoring();
       
    58     }
       
    59 
       
    60 TBool CMMCUsbAO::StartMonitoring( MMMCMonitorObserver& aObserver )
       
    61     {
       
    62     WRITELOG( "CMMCUsbAO::StartMonitoring" );
       
    63     
       
    64     iObserver = &aObserver;
       
    65     
       
    66     return Resume();
       
    67     }
       
    68 
       
    69 void CMMCUsbAO::StartNotify()
       
    70     {
       
    71     WRITELOG( "CMMCUsbAO::StartNotify" );
       
    72     iDriveState.Subscribe( iStatus );
       
    73     SetActive();
       
    74     }
       
    75 
       
    76 TBool CMMCUsbAO::StopMonitoring()
       
    77     {
       
    78     WRITELOG( "CMMCUsbAO::StopNotify" );
       
    79     
       
    80     return Pause();
       
    81     }
       
    82 
       
    83 TBool CMMCUsbAO::Resume()
       
    84     {
       
    85     WRITELOG( "CMMCUsbAO::Resume" );
       
    86         
       
    87     StartNotify();
       
    88     
       
    89     return ETrue;
       
    90     }
       
    91 
       
    92 TBool CMMCUsbAO::Pause()
       
    93     {
       
    94     WRITELOG( "CMMCUsbAO::Pause" );
       
    95     
       
    96     Cancel();
       
    97     iDriveState.Close();
       
    98     iFs.Close();
       
    99     
       
   100     return ETrue;
       
   101     }
       
   102 
       
   103 void CMMCUsbAO::RunL()
       
   104     {
       
   105     WRITELOG1( "CMMCUsbAO::RunL iStatus: %d", iStatus.Int() );
       
   106     
       
   107     // check if change to pc-suite mode
       
   108     if ( iStatus.Int() == KErrNotFound )
       
   109         {
       
   110         TChar chr;
       
   111         RFs::DriveToChar( iDrive, chr );
       
   112         iObserver->MountEvent( chr, MediaID(iDrive), EMounted );
       
   113         iMassStorageMode = EFalse;
       
   114         StartNotify();
       
   115         return;
       
   116         }
       
   117     else
       
   118         {
       
   119         User::LeaveIfError( iStatus.Int() );
       
   120         }
       
   121         
       
   122     TUsbMsDrivesStatus allDrivesStatus;
       
   123     const TInt err = iDriveState.Get( allDrivesStatus );
       
   124     
       
   125     if ( err == KErrNone )
       
   126         {
       
   127         WRITELOG1( "CMMCUsbAO::RunL - allDrivesStatus.Length(): %d", allDrivesStatus.Length() );
       
   128         const TInt count = allDrivesStatus.Length()/2;
       
   129         for ( TInt i = 0; i < count; i++ )
       
   130             {
       
   131             TInt driveNumber = allDrivesStatus[2*i];
       
   132             TInt driveStatus = allDrivesStatus[2*i+1];
       
   133             TChar driveChar;
       
   134             RFs::DriveToChar( driveNumber, driveChar );
       
   135 #ifdef _DEBUG
       
   136             TFileName* fn = new (ELeave) TFileName( driveChar );
       
   137             _LIT( KIndicator, ":" );
       
   138             fn->Append( KIndicator );
       
   139             WRITELOG1( "CMMCUsbAO::RunL - drive letter: %S", fn );
       
   140             PrintDriveStatus( driveStatus );
       
   141             delete fn;
       
   142             fn = NULL;
       
   143 #endif
       
   144             switch( driveStatus )
       
   145                 {
       
   146                 case EUsbMsDriveState_Connected:
       
   147                     {
       
   148                     if ( iMassStorageMode )
       
   149                         {
       
   150                         break;
       
   151                         }
       
   152                     iObserver->MountEvent( driveChar, MediaID(driveNumber), EDismounted );
       
   153                     iMassStorageMode = ETrue;
       
   154                     iDrive = driveNumber;
       
   155                     }
       
   156                 break;
       
   157                 
       
   158                 case EUsbMsDriveState_Removed:
       
   159                     {
       
   160                     iObserver->MountEvent( driveChar, MediaID(driveNumber), EMounted );
       
   161                     iMassStorageMode = EFalse;
       
   162                     }
       
   163                 break;
       
   164                 
       
   165                 default:
       
   166                 	break;
       
   167                 }
       
   168             }
       
   169         }
       
   170     else
       
   171         {
       
   172         WRITELOG1( "CMMCUsbAO::RunL - drive state error %d", err );
       
   173         }
       
   174     
       
   175     StartNotify();
       
   176     }
       
   177 
       
   178 #ifdef _DEBUG
       
   179 TInt CMMCUsbAO::RunError( TInt aError )
       
   180 #else
       
   181 TInt CMMCUsbAO::RunError( TInt )
       
   182 #endif
       
   183     {
       
   184     WRITELOG1( "CMMCUsbAO::RunError with error code: %d", aError );
       
   185     
       
   186     StartNotify();
       
   187     return KErrNone;
       
   188     }
       
   189 
       
   190 void CMMCUsbAO::DoCancel()
       
   191     {
       
   192     WRITELOG( "CMMCUsbAO::DoCancel" );
       
   193     
       
   194     iDriveState.Cancel();
       
   195     }
       
   196 
       
   197 TUint32 CMMCUsbAO::MediaID( TInt aDrive )
       
   198     {
       
   199     TUint32 uid = 0;
       
   200     TVolumeInfo* vi = NULL;
       
   201     vi = new TVolumeInfo;
       
   202     if ( !vi )
       
   203         {
       
   204         return uid;
       
   205         }
       
   206     
       
   207     const TInt err = iFs.Volume( *vi, aDrive );
       
   208     if ( err == KErrNone )
       
   209         {
       
   210         uid = vi->iUniqueID;
       
   211         }
       
   212 
       
   213     delete vi;
       
   214     vi = NULL;
       
   215     
       
   216     return uid;
       
   217     }
       
   218 
       
   219 #ifdef _DEBUG
       
   220 void CMMCUsbAO::PrintDriveStatus( TInt aStatus )
       
   221     {
       
   222     switch( aStatus )
       
   223         {
       
   224         case EUsbMsDriveState_Disconnected:
       
   225         WRITELOG( "CMMCUsbAO::RunL - drive status: EUsbMsDriveState_Disconnected" );
       
   226         break;
       
   227         
       
   228         case EUsbMsDriveState_Connecting:
       
   229         WRITELOG( "CMMCUsbAO::RunL - drive status: EUsbMsDriveState_Connecting" );
       
   230         break;
       
   231         
       
   232         case EUsbMsDriveState_Connected:
       
   233         WRITELOG( "CMMCUsbAO::RunL - drive status: EUsbMsDriveState_Connected" );
       
   234         break;
       
   235         
       
   236         case EUsbMsDriveState_Disconnecting:
       
   237         WRITELOG( "CMMCUsbAO::RunL - drive status: EUsbMsDriveState_Disconnecting" );
       
   238         break;
       
   239         
       
   240         case EUsbMsDriveState_Active:
       
   241         WRITELOG( "CMMCUsbAO::RunL - drive status: EUsbMsDriveState_Active" );
       
   242         break;
       
   243         
       
   244         case EUsbMsDriveState_Locked:
       
   245         WRITELOG( "CMMCUsbAO::RunL - drive status: EUsbMsDriveState_Locked" );
       
   246         break;
       
   247         
       
   248         case EUsbMsDriveState_MediaNotPresent:
       
   249         WRITELOG( "CMMCUsbAO::RunL - drive status: EUsbMsDriveState_MediaNotPresent" );
       
   250         break;
       
   251         
       
   252         case EUsbMsDriveState_Removed:
       
   253         WRITELOG( "CMMCUsbAO::RunL - drive status: EUsbMsDriveState_Removed" );
       
   254         break;
       
   255         
       
   256         case EUsbMsDriveState_Error:
       
   257         WRITELOG( "CMMCUsbAO::RunL - drive status: EUsbMsDriveState_Error" );
       
   258         break;
       
   259         
       
   260         default:
       
   261         WRITELOG( "CMMCUsbAO::RunL - drive status: Unknown" );
       
   262         break;
       
   263         }
       
   264     }
       
   265 
       
   266 #endif