commonservices/PlatformEnv/disknotifyhandler/src/diskwatcher.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 /*
       
     2 * Copyright (c) 2007 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:  Class for notify disk watcher
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 //  INCLUDES
       
    21 #include <e32std.h>
       
    22 #include <f32file.h>
       
    23 #include <driveinfo.h>
       
    24 #include "diskwatcher.h"
       
    25 #include "disknotifyhandler.h"
       
    26 #include "disknotifyhandlerdebug.h"
       
    27 
       
    28 
       
    29 // ======== LOCAL FUNCTIONS ========
       
    30 
       
    31 // ---------------------------------------------------------------------------
       
    32 // SetEvent
       
    33 // ---------------------------------------------------------------------------
       
    34 //
       
    35 inline static void SetEvent(
       
    36         MDiskNotifyHandlerCallback::TDiskEvent& aEvent,
       
    37         MDiskNotifyHandlerCallback::TDiskEventType aType,
       
    38         TInt aDrive,
       
    39         const TDriveInfo& aInfo,
       
    40         const TDriveInfo& aPrevInfo )
       
    41     {
       
    42     aEvent.iType = aType;
       
    43     aEvent.iDrive = aDrive;
       
    44     aEvent.iInfo = aInfo;
       
    45     aEvent.iPrevInfo = aPrevInfo;
       
    46     }
       
    47 
       
    48 // ---------------------------------------------------------------------------
       
    49 // SetEmptyDriveInfo
       
    50 // ---------------------------------------------------------------------------
       
    51 //
       
    52 inline static void SetEmptyDriveInfo( TDriveInfo& aInfo )
       
    53     {
       
    54     aInfo.iType = EMediaNotPresent;
       
    55     aInfo.iBattery = EBatNotSupported;
       
    56     aInfo.iDriveAtt = 0;
       
    57     aInfo.iMediaAtt = 0;
       
    58     }
       
    59 
       
    60 
       
    61 // ======== MEMBER FUNCTIONS ========
       
    62 
       
    63 // ---------------------------------------------------------------------------
       
    64 // CDiskWatcher::CDiskWatcher
       
    65 // ---------------------------------------------------------------------------
       
    66 //
       
    67 CDiskWatcher::CDiskWatcher(
       
    68         MDiskNotifyHandlerCallback& aCallback,
       
    69         RFs& aFs ) :
       
    70     CDiskWatcherBase( aCallback, aFs )
       
    71     {
       
    72     FUNC_LOG
       
    73     }
       
    74 
       
    75 // ---------------------------------------------------------------------------
       
    76 // CDiskWatcher::ConstructL
       
    77 // ---------------------------------------------------------------------------
       
    78 //
       
    79 void CDiskWatcher::ConstructL()
       
    80     {
       
    81     FUNC_LOG
       
    82 
       
    83     Activate();
       
    84 
       
    85     RFs& fs( Fs() );
       
    86     TDriveList drvList;
       
    87     User::LeaveIfError( fs.DriveList( drvList, KDriveAttAll ) );
       
    88 
       
    89     TInt count( Min( drvList.Length(), iInfoList.Count() ) );
       
    90     for ( TInt i( 0 ); i < count; ++i )
       
    91         {
       
    92         if ( drvList[ i ] )
       
    93             {
       
    94             iInfoList[ i ].Read( fs, i );
       
    95             }
       
    96         }
       
    97     }
       
    98 
       
    99 // ---------------------------------------------------------------------------
       
   100 // CDiskWatcher::NewL
       
   101 // ---------------------------------------------------------------------------
       
   102 //
       
   103 CDiskWatcher* CDiskWatcher::NewL(
       
   104         MDiskNotifyHandlerCallback& aCallback,
       
   105         RFs& aFs )
       
   106     {
       
   107     FUNC_LOG
       
   108 
       
   109     CDiskWatcher* self = new ( ELeave ) CDiskWatcher( aCallback, aFs );
       
   110     CleanupStack::PushL( self );
       
   111     self->ConstructL();
       
   112     CleanupStack::Pop( self );
       
   113     return self;
       
   114     }
       
   115 
       
   116 // ---------------------------------------------------------------------------
       
   117 // CDiskWatcher::~CDiskWatcher
       
   118 // ---------------------------------------------------------------------------
       
   119 //
       
   120 CDiskWatcher::~CDiskWatcher()
       
   121     {
       
   122     FUNC_LOG
       
   123 
       
   124     Cancel();
       
   125     }
       
   126 
       
   127 // ---------------------------------------------------------------------------
       
   128 // CDiskWatcher::WatcherError
       
   129 // ---------------------------------------------------------------------------
       
   130 //
       
   131 void CDiskWatcher::WatcherError(
       
   132         TInt aError,
       
   133         const TBool& /*aKilled*/ )
       
   134     {
       
   135     FUNC_LOG
       
   136 
       
   137     LOG_IF_ERROR1( aError, "CDiskWatcher::WatcherError-aError=%d",
       
   138         aError )
       
   139 
       
   140     MDiskNotifyHandlerCallback::TDiskEvent event;
       
   141     TDriveInfo emptyDrvInfo;
       
   142     SetEmptyDriveInfo( emptyDrvInfo );
       
   143 
       
   144     SetEvent(
       
   145         event,
       
   146         MDiskNotifyHandlerCallback::EDiskError,
       
   147         KErrNotFound,
       
   148         emptyDrvInfo,
       
   149         emptyDrvInfo );
       
   150 
       
   151     Callback().HandleNotifyDisk( aError, event );
       
   152     }
       
   153 
       
   154 // ---------------------------------------------------------------------------
       
   155 // CDiskWatcher::ReactivateWatcher
       
   156 // ---------------------------------------------------------------------------
       
   157 //
       
   158 void CDiskWatcher::ReactivateWatcher()
       
   159     {
       
   160     FUNC_LOG
       
   161 
       
   162     Activate();
       
   163     }
       
   164 
       
   165 // ---------------------------------------------------------------------------
       
   166 // CDiskWatcher::RunWatcher
       
   167 // ---------------------------------------------------------------------------
       
   168 //
       
   169 void CDiskWatcher::RunWatcher( const TBool& aKilled )
       
   170     {
       
   171     FUNC_LOG
       
   172 
       
   173     RFs& fs( Fs() );
       
   174     MDiskNotifyHandlerCallback& callback( Callback() );
       
   175 
       
   176     TDriveList drvList;
       
   177     TInt err( fs.DriveList( drvList, KDriveAttAll ) );
       
   178     if ( err != KErrNone )
       
   179         {
       
   180         ERROR_LOG1( "CDiskWatcher::RunWatcher-err1=%d", err )
       
   181         return;
       
   182         }
       
   183 
       
   184     TInt count( Min( drvList.Length(), iInfoList.Count() ) );
       
   185     TDriveInfo emptyDrvInfo;
       
   186     SetEmptyDriveInfo( emptyDrvInfo );
       
   187     MDiskNotifyHandlerCallback::TDiskEvent event;
       
   188     TWatcherInfo newInfo;
       
   189 
       
   190     for( TInt i( 0 ); i < count; ++i )
       
   191         {
       
   192         TWatcherInfo& info( iInfoList[ i ] );
       
   193         if ( !drvList[ i ] && info.iDriveStatus )
       
   194             {
       
   195             INFO_LOG1( "CDiskWatcher::RunWatcher-Drive %d removed", i )
       
   196 
       
   197             // Inform drives removed from drive list
       
   198             SetEvent(
       
   199                 event,
       
   200                 MDiskNotifyHandlerCallback::EDiskRemoved,
       
   201                 i,
       
   202                 emptyDrvInfo,
       
   203                 info.iDriveInfo );
       
   204 
       
   205             callback.HandleNotifyDisk( KErrNone, event );
       
   206             if ( aKilled )
       
   207                 {
       
   208                 return; // Deleted from callback
       
   209                 }
       
   210             info.Reset(); // Set to empty
       
   211             }
       
   212         else if ( drvList[ i ] && !info.iDriveStatus )
       
   213             {
       
   214             INFO_LOG1( "CDiskWatcher::RunWatcher-Drive %d added", i )
       
   215 
       
   216             // Inform drives added to drive list
       
   217             info.Read( fs, i );
       
   218             if ( info.iDriveStatus )
       
   219                 {
       
   220                 SetEvent(
       
   221                     event,
       
   222                     MDiskNotifyHandlerCallback::EDiskAdded,
       
   223                     i,
       
   224                     info.iDriveInfo,
       
   225                     emptyDrvInfo );
       
   226     
       
   227                 callback.HandleNotifyDisk( KErrNone, event );
       
   228                 if ( aKilled )
       
   229                     {
       
   230                     return;  // Deleted from callback
       
   231                     }
       
   232                 }
       
   233             }
       
   234         else if ( drvList[ i ] && info.iDriveStatus )
       
   235             {
       
   236             // Inform drive status changes
       
   237             newInfo.Read( fs, i );
       
   238             if ( info.IsChanged( newInfo ) )
       
   239                 {
       
   240                 INFO_LOG1( "CDiskWatcher::RunWatcher-Drive %d changed", i )
       
   241 
       
   242                 SetEvent(
       
   243                     event,
       
   244                     MDiskNotifyHandlerCallback::EDiskStatusChanged,
       
   245                     i,
       
   246                     newInfo.iDriveInfo,
       
   247                     info.iDriveInfo );
       
   248 
       
   249                 callback.HandleNotifyDisk( KErrNone, event );
       
   250                 if ( aKilled )
       
   251                     {
       
   252                     return; // Deleted from callback
       
   253                     }
       
   254                 info = newInfo; // Save changed info
       
   255                 }
       
   256             }
       
   257         }
       
   258     }
       
   259 
       
   260 // ---------------------------------------------------------------------------
       
   261 // CDiskWatcher::CancelWatcher
       
   262 // ---------------------------------------------------------------------------
       
   263 //
       
   264 void CDiskWatcher::CancelWatcher()
       
   265     {
       
   266     FUNC_LOG
       
   267 
       
   268     Fs().NotifyChangeCancel( iStatus );
       
   269     }
       
   270 
       
   271 // ---------------------------------------------------------------------------
       
   272 // CDiskWatcher::Activate
       
   273 // ---------------------------------------------------------------------------
       
   274 //
       
   275 void CDiskWatcher::Activate()
       
   276     {
       
   277     FUNC_LOG
       
   278 
       
   279     if ( !IsActive() )
       
   280         {
       
   281         INFO_LOG( "CDiskWatcher::Activate" )
       
   282         Fs().NotifyChange( ENotifyDisk, iStatus );
       
   283         SetActive();
       
   284         }
       
   285     }
       
   286 
       
   287 // ---------------------------------------------------------------------------
       
   288 // CDiskWatcher::TWatcherInfo::Read
       
   289 // ---------------------------------------------------------------------------
       
   290 //
       
   291 void CDiskWatcher::TWatcherInfo::Read( RFs& aFs, TInt aDrive )
       
   292     {
       
   293     FUNC_LOG
       
   294 
       
   295     TInt err( aFs.Drive( iDriveInfo, aDrive ) );
       
   296     LOG_IF_ERROR2( err, "CDiskWatcher::TWatcherInfo::Read-aDrive=%d,err1=%d",
       
   297         aDrive, err )
       
   298     if ( err == KErrNone )
       
   299         {
       
   300         // Needed for detecting USB mass storage mode transition
       
   301         err = DriveInfo::GetDriveStatus( aFs, aDrive, iDriveStatus );
       
   302         LOG_IF_ERROR2( err, "CDiskWatcher::TWatcherInfo::Read-aDrive=%d,err2=%d",
       
   303             aDrive, err )
       
   304         }
       
   305     if ( err != KErrNone )
       
   306         {
       
   307         Reset();
       
   308         }
       
   309     INFO_LOG3(
       
   310         "CDiskWatcher::TWatcherInfo::Read-Drive %d,Status 0x%x,Type %d",
       
   311         aDrive, iDriveStatus, iDriveInfo.iType )
       
   312     INFO_LOG3(
       
   313         "CDiskWatcher::TWatcherInfo::Read-Drive %d,DriveAtt 0x%x,MediaAtt 0x%x",
       
   314         aDrive, iDriveInfo.iDriveAtt, iDriveInfo.iMediaAtt )
       
   315     }
       
   316 
       
   317 // ---------------------------------------------------------------------------
       
   318 // CDiskWatcher::TWatcherInfo::IsChanged
       
   319 // ---------------------------------------------------------------------------
       
   320 //
       
   321 TBool CDiskWatcher::TWatcherInfo::IsChanged(
       
   322         const TWatcherInfo& aNewInfo ) const
       
   323     {
       
   324     FUNC_LOG
       
   325 
       
   326     TBool ret( iDriveStatus != aNewInfo.iDriveStatus ||
       
   327                iDriveInfo.iType != aNewInfo.iDriveInfo.iType ||
       
   328                iDriveInfo.iDriveAtt != aNewInfo.iDriveInfo.iDriveAtt ||
       
   329                iDriveInfo.iMediaAtt != aNewInfo.iDriveInfo.iMediaAtt );
       
   330     INFO_LOG3(
       
   331         "CDiskWatcher::TWatcherInfo::IsChanged %d,Status 0x%x,NewStatus 0x%x",
       
   332         ret, iDriveStatus, aNewInfo.iDriveStatus )
       
   333     return ret;
       
   334     }
       
   335 
       
   336 // ---------------------------------------------------------------------------
       
   337 // CDiskWatcher::TWatcherInfo::Reset
       
   338 // ---------------------------------------------------------------------------
       
   339 //
       
   340 void CDiskWatcher::TWatcherInfo::Reset()
       
   341     {
       
   342     FUNC_LOG
       
   343 
       
   344     SetEmptyDriveInfo( iDriveInfo );
       
   345     iDriveStatus = 0;
       
   346     }
       
   347 
       
   348 // End of File