mmappcomponents/harvester/server/src/mpxusbeventhandler.cpp
changeset 0 a2952bb97e68
child 14 05b0d2323768
child 25 d881023c13eb
equal deleted inserted replaced
-1:000000000000 0:a2952bb97e68
       
     1 /*
       
     2 * Copyright (c) 2006 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 for USB events
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32base.h>
       
    20 #include <f32file.h>
       
    21 #include <e32property.h>
       
    22 #ifdef RD_MULTIPLE_DRIVE
       
    23 #include <driveinfo.h>
       
    24 #endif //RD_MULTIPLE_DRIVE
       
    25 #include <mpxpskeywatcher.h>
       
    26 #include <coreapplicationuisdomainpskeys.h>
       
    27 #include <UsbWatcherInternalPSKeys.h>
       
    28 #include <usbpersonalityids.h>
       
    29 #include <MtpPrivatePSKeys.h>
       
    30 #include <mpxlog.h>
       
    31 #include "mpxusbeventhandler.h"
       
    32 
       
    33 // CONSTANTS
       
    34 const TUint KUsbAllStates = 0xFFFFFFFF;
       
    35 
       
    36 // ---------------------------------------------------------------------------
       
    37 // Default Constructor
       
    38 // ---------------------------------------------------------------------------
       
    39 //
       
    40 CMPXUsbEventHandler::CMPXUsbEventHandler( MMPXSystemEventObserver& aObserver ) :
       
    41                                           CActive( EPriorityStandard ),
       
    42                                           iWasMtp  ( EFalse ),
       
    43                                           iObserver( aObserver ),
       
    44                                           iMTPActive ( EFalse ),
       
    45                                           iUsbManConnected( EFalse )
       
    46     {
       
    47     CActiveScheduler::Add( this );
       
    48     }
       
    49 
       
    50 
       
    51 // ---------------------------------------------------------------------------
       
    52 // 2nd Phase Constructor
       
    53 // ---------------------------------------------------------------------------
       
    54 //
       
    55 void CMPXUsbEventHandler::ConstructL()
       
    56     {
       
    57     MPX_DEBUG1(_L("CMPXUsbEventHandler::ConstructL <---"));
       
    58     iPSKeyWatcher = CMPXPSKeyWatcher::NewL( KPSUidUsbWatcher,
       
    59                                             KUsbWatcherSelectedPersonality,
       
    60                                             this );
       
    61 
       
    62     //for checking MTP status key whether a client has started
       
    63     iMTPKeyWatcher = CMPXPSKeyWatcher::NewL( KMtpPSUid, 
       
    64                                              KMtpPSStatus,
       
    65                                              this );
       
    66 
       
    67     // Connect to usbman
       
    68     ConnectUsbMan();
       
    69     MPX_DEBUG1(_L("CMPXUsbEventHandler::ConstructL --->"));
       
    70     }
       
    71 
       
    72 
       
    73 // ---------------------------------------------------------------------------
       
    74 // Two Phased Constructor
       
    75 // ---------------------------------------------------------------------------
       
    76 //
       
    77 CMPXUsbEventHandler* CMPXUsbEventHandler::NewL
       
    78                                         ( MMPXSystemEventObserver& aObserver )
       
    79     {
       
    80     CMPXUsbEventHandler* self = CMPXUsbEventHandler::NewLC( aObserver );
       
    81     CleanupStack::Pop( self );
       
    82     return self;
       
    83     }
       
    84 
       
    85 
       
    86 // ---------------------------------------------------------------------------
       
    87 // Two Phased Constructor
       
    88 // ---------------------------------------------------------------------------
       
    89 //
       
    90 CMPXUsbEventHandler* CMPXUsbEventHandler::NewLC
       
    91                                         ( MMPXSystemEventObserver& aObserver )
       
    92 
       
    93     {
       
    94     CMPXUsbEventHandler* self = new( ELeave ) CMPXUsbEventHandler( aObserver);
       
    95     CleanupStack::PushL( self );
       
    96     self->ConstructL();
       
    97     return self;
       
    98     }
       
    99 
       
   100 
       
   101 // ---------------------------------------------------------------------------
       
   102 // Destructor
       
   103 // ---------------------------------------------------------------------------
       
   104 //
       
   105 CMPXUsbEventHandler::~CMPXUsbEventHandler()
       
   106     {
       
   107     Cancel();
       
   108     if ( iUsbManConnected )
       
   109         {
       
   110         iUsbMan.Close();
       
   111         }
       
   112     delete iPSKeyWatcher;
       
   113     delete iMTPKeyWatcher;
       
   114     }
       
   115 
       
   116 // ---------------------------------------------------------------------------
       
   117 // Poll for any ongoing USB / MTP event
       
   118 // ---------------------------------------------------------------------------
       
   119 //
       
   120 void CMPXUsbEventHandler::PollStatus()
       
   121     {
       
   122     TInt value;
       
   123     iPSKeyWatcher->GetValue( value );
       
   124     if( (value==KUsbPersonalityIdMS) ||
       
   125         (value==KUsbPersonalityIdMTP) ||
       
   126         (value==KUsbPersonalityIdPCSuiteMTP) )
       
   127         {
       
   128         HandlePSEvent( TUid::Uid(0), 0 );
       
   129         }
       
   130     }
       
   131 
       
   132 // ---------------------------------------------------------------------------
       
   133 // Handle the PS key event
       
   134 // ---------------------------------------------------------------------------
       
   135 //
       
   136 void CMPXUsbEventHandler::HandlePSEvent( TUid aUid, TInt aKey )
       
   137     {
       
   138     TRAP_IGNORE( DoHandlePSEventL(aUid,aKey) );
       
   139     }
       
   140 
       
   141 // ---------------------------------------------------------------------------
       
   142 // Handle the PS key event
       
   143 // ---------------------------------------------------------------------------
       
   144 //
       
   145 void CMPXUsbEventHandler::DoHandlePSEventL( TUid /*aUid*/, TInt /*aKey*/ )
       
   146     {
       
   147     MPX_FUNC("CMPXUsbEventHandler::DoHandlePSEvent()");
       
   148     if ( !iUsbManConnected )
       
   149         {
       
   150         ConnectUsbMan();
       
   151         }
       
   152     
       
   153     // Use the default MMC drive
       
   154     TInt removableDrive( EDriveE );
       
   155 #ifdef RD_MULTIPLE_DRIVE
       
   156     User::LeaveIfError( DriveInfo::GetDefaultDrive(
       
   157         DriveInfo::EDefaultRemovableMassStorage,
       
   158         removableDrive ) );
       
   159 #endif // RD_MULTIPLE_DRIVE
       
   160 
       
   161     // Handle the Key event
       
   162     TInt value;
       
   163     iPSKeyWatcher->GetValue( value );
       
   164 
       
   165     // Determine if we are currently in MTP / Mass Storage mode
       
   166     //
       
   167     MPX_DEBUG2("CMPXUsbEventHandler::DoHandlePSEvent -- get usb value %i", value);
       
   168 	MPX_DEBUG2("CMPXUsbEventHandler::DoHandlePSEvent -iState=%d", iState);
       
   169     if( (value == KUsbPersonalityIdMS) && 
       
   170         ( iDeviceState == EUsbDeviceStateAddress ||
       
   171           iDeviceState == EUsbDeviceStateConfigured ) )
       
   172         {
       
   173         // Switch mode
       
   174         if( iState == KUsbPersonalityIdMTP )
       
   175             {
       
   176             MPX_DEBUG1("CMPXUsbEventHandler::DoHandlePSEvent - MTP End");
       
   177             iObserver.HandleSystemEventL( EUSBMTPEndEvent, removableDrive );
       
   178             iMTPActive = EFalse;
       
   179             }
       
   180         MPX_DEBUG1("CMPXUsbEventHandler::DoHandlePSEvent - USB Start");
       
   181 #ifdef RD_MULTIPLE_DRIVE
       
   182         removableDrive = -1;
       
   183 #endif // RD_MULTIPLE_DRIVE
       
   184 
       
   185         // Notify the state change (may happen more than once)
       
   186         iObserver.HandleSystemEventL( EUSBMassStorageStartEvent, removableDrive );
       
   187         iState = KUsbPersonalityIdMS;
       
   188         }
       
   189     else if( ( value == KUsbPersonalityIdMTP || value == KUsbPersonalityIdPCSuiteMTP ) &&
       
   190              ( iDeviceState == EUsbDeviceStateAddress || iDeviceState == EUsbDeviceStateConfigured ) )
       
   191         {
       
   192         if( iState == KUsbPersonalityIdMS )
       
   193             {
       
   194             MPX_DEBUG1("CMPXUsbEventHandler::DoHandlePSEvent - USB End");
       
   195             iObserver.HandleSystemEventL( EUSBMassStorageEndEvent, removableDrive );
       
   196             }
       
   197         iMTPKeyWatcher->GetValue(value);
       
   198         MPX_DEBUG2("CMPXUsbEventHandler::DoHandlePSEvent -- get MTP value %i", value);
       
   199 		
       
   200         if (value == EMtpPSStatusActive)
       
   201             {
       
   202             if( !iMTPActive )
       
   203             	{
       
   204                 MPX_DEBUG1("CMPXUsbEventHandler::DoHandlePSEvent - MTP Start");
       
   205                 iObserver.HandleSystemEventL( EUSBMTPStartEvent, removableDrive );
       
   206                 iState = KUsbPersonalityIdMTP;
       
   207                 iMTPActive = ETrue;
       
   208             	}
       
   209             }
       
   210         else if( value != EMtpPSStatusReadyToSync )
       
   211             {
       
   212             MPX_DEBUG1("CMPXUsbEventHandler::DoHandlePSEvent - MTP Not Active");
       
   213             iObserver.HandleSystemEventL( EUSBMTPNotActiveEvent, removableDrive );
       
   214             iState = KUsbPersonalityIdMTP;
       
   215             iMTPActive = EFalse;
       
   216             }
       
   217         }
       
   218     // Make sure MTP and MS flags are OFF
       
   219     //
       
   220     else if( value != KUsbPersonalityIdMTP &&
       
   221              value != KUsbPersonalityIdMS &&
       
   222              value != KUsbPersonalityIdPCSuiteMTP )
       
   223         {
       
   224         // Signal end of event
       
   225         if( iState == KUsbPersonalityIdMS )
       
   226             {
       
   227             MPX_DEBUG1("CMPXUsbEventHandler::DoHandlePSEvent - USB End");
       
   228             iObserver.HandleSystemEventL( EUSBMassStorageEndEvent, removableDrive );
       
   229             }
       
   230         else if( iState == KUsbPersonalityIdMTP )
       
   231             {
       
   232             MPX_DEBUG1("CMPXUsbEventHandler::DoHandlePSEvent - MTP End");
       
   233             iObserver.HandleSystemEventL( EUSBMTPEndEvent, removableDrive );
       
   234             iMTPActive = EFalse;
       
   235             }
       
   236         iState = KUsbWatcherSelectedPersonalityNone;
       
   237         }
       
   238     }
       
   239 
       
   240 // ---------------------------------------------------------------------------
       
   241 // CMPXUsbEventHandler::RunL
       
   242 // ---------------------------------------------------------------------------
       
   243 //
       
   244 void CMPXUsbEventHandler::RunL()
       
   245     {
       
   246     MPX_FUNC("CMPXUsbEventHandler::RunL()");
       
   247     TInt status( iStatus.Int() );
       
   248     
       
   249     MPX_DEBUG2("CMPXUsbEventHandler::RunL status=%d", status );
       
   250     if ( status != KErrCancel && status != KErrServerTerminated )
       
   251         {
       
   252         iUsbMan.DeviceStateNotification( KUsbAllStates, iDeviceState, iStatus );
       
   253         SetActive();
       
   254         }
       
   255     
       
   256     if ( status == KErrNone )
       
   257         {
       
   258         MPX_DEBUG2("CMPXUsbEventHandler::RunL - DeviceState = %d", iDeviceState);
       
   259         HandlePSEvent( TUid::Uid(0), 0 );
       
   260         }
       
   261     }
       
   262 
       
   263 // ---------------------------------------------------------------------------
       
   264 // CMPXUsbEventHandler::DoCancel
       
   265 // ---------------------------------------------------------------------------
       
   266 //
       
   267 void CMPXUsbEventHandler::DoCancel()
       
   268     {
       
   269     MPX_FUNC("CMPXUsbEventHandler::DoCancel()");
       
   270     if ( iUsbManConnected )
       
   271         {
       
   272         iUsbMan.DeviceStateNotificationCancel();
       
   273         }
       
   274     }
       
   275 
       
   276 // ---------------------------------------------------------------------------
       
   277 // CMPXUsbEventHandler::ConnectUsbMan
       
   278 // If error, default iDeviceState to EUsbDeviceStateConfigured so this would not
       
   279 // block usb event mode change.
       
   280 // ---------------------------------------------------------------------------
       
   281 void CMPXUsbEventHandler::ConnectUsbMan()
       
   282     {
       
   283     MPX_FUNC("CMPXUsbEventHandler::ConnectUsbMan()");
       
   284     if ( iUsbMan.Connect() == KErrNone )
       
   285         {
       
   286         iUsbManConnected = ETrue;
       
   287         // get device state
       
   288         TInt err = iUsbMan.GetDeviceState( iDeviceState );
       
   289         if ( err )
       
   290             {
       
   291             iDeviceState = EUsbDeviceStateUndefined;
       
   292             }
       
   293         
       
   294         // start active object
       
   295         if ( !IsActive() )
       
   296             {
       
   297             iUsbMan.DeviceStateNotification( KUsbAllStates, iDeviceState, iStatus );
       
   298             SetActive();
       
   299             }
       
   300         }
       
   301     else
       
   302         {
       
   303         iDeviceState = EUsbDeviceStateConfigured;
       
   304         }
       
   305     }
       
   306 // END OF FILE
       
   307