mmappcomponents/harvester/server/src/mpxconnectioneventhandler.cpp
changeset 32 edd273b3192a
child 35 2ee890d2f7e7
equal deleted inserted replaced
27:cbb1bfb7ebfb 32:edd273b3192a
       
     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 "mpxconnectioneventhandler.h"
       
    32 
       
    33 // CONSTANTS
       
    34 const TUint KUsbAllStates = 0xFFFFFFFF;
       
    35 
       
    36 // ---------------------------------------------------------------------------
       
    37 // Default Constructor
       
    38 // ---------------------------------------------------------------------------
       
    39 //
       
    40 CMPXConnectionEventHandler::CMPXConnectionEventHandler( MMPXSystemEventObserver& aObserver ) :
       
    41                                           CActive( EPriorityStandard ),
       
    42                                           iObserver( aObserver ),
       
    43                                           iState ( EMPXConnectionNone ),
       
    44                                           iUsbManConnected( EFalse )
       
    45                                           
       
    46                                           
       
    47     {
       
    48     CActiveScheduler::Add( this );
       
    49     }
       
    50 
       
    51 
       
    52 // ---------------------------------------------------------------------------
       
    53 // 2nd Phase Constructor
       
    54 // ---------------------------------------------------------------------------
       
    55 //
       
    56 void CMPXConnectionEventHandler::ConstructL()
       
    57     {
       
    58     MPX_DEBUG1(_L("CMPXConnectionEventHandler::ConstructL <---"));
       
    59     iUSBKeyWatcher = CMPXPSKeyWatcher::NewL( KPSUidUsbWatcher,
       
    60                                             KUsbWatcherSelectedPersonality,
       
    61                                             this );
       
    62 
       
    63     //for checking MTP status key whether a client has started
       
    64     iMTPKeyWatcher = CMPXPSKeyWatcher::NewL( KMtpPSUid, 
       
    65                                              KMtpPSStatus,
       
    66                                              this );
       
    67 
       
    68     // Connect to usbman
       
    69     ConnectUsbMan();
       
    70     MPX_DEBUG1(_L("CMPXConnectionEventHandler::ConstructL --->"));
       
    71     }
       
    72 
       
    73 
       
    74 // ---------------------------------------------------------------------------
       
    75 // Two Phased Constructor
       
    76 // ---------------------------------------------------------------------------
       
    77 //
       
    78 CMPXConnectionEventHandler* CMPXConnectionEventHandler::NewL
       
    79                                         ( MMPXSystemEventObserver& aObserver )
       
    80     {
       
    81     CMPXConnectionEventHandler* self = CMPXConnectionEventHandler::NewLC( aObserver );
       
    82     CleanupStack::Pop( self );
       
    83     return self;
       
    84     }
       
    85 
       
    86 
       
    87 // ---------------------------------------------------------------------------
       
    88 // Two Phased Constructor
       
    89 // ---------------------------------------------------------------------------
       
    90 //
       
    91 CMPXConnectionEventHandler* CMPXConnectionEventHandler::NewLC
       
    92                                         ( MMPXSystemEventObserver& aObserver )
       
    93 
       
    94     {
       
    95     CMPXConnectionEventHandler* self = new( ELeave ) CMPXConnectionEventHandler( aObserver);
       
    96     CleanupStack::PushL( self );
       
    97     self->ConstructL();
       
    98     return self;
       
    99     }
       
   100 
       
   101 
       
   102 // ---------------------------------------------------------------------------
       
   103 // Destructor
       
   104 // ---------------------------------------------------------------------------
       
   105 //
       
   106 CMPXConnectionEventHandler::~CMPXConnectionEventHandler()
       
   107     {
       
   108     Cancel();
       
   109     if ( iUsbManConnected )
       
   110         {
       
   111         iUsbMan.Close();
       
   112         }
       
   113     delete iUSBKeyWatcher;
       
   114     delete iMTPKeyWatcher;
       
   115     }
       
   116 
       
   117 // ---------------------------------------------------------------------------
       
   118 // Poll for any ongoing USB / MTP event
       
   119 // ---------------------------------------------------------------------------
       
   120 //
       
   121 void CMPXConnectionEventHandler::PollStatus()
       
   122     {
       
   123     // only call back on connection state != EMPXConnectionNone
       
   124     if (iState == EMPXConnectionMassStorage)
       
   125         {
       
   126         TRAP_IGNORE( DoMSStartEventL() );
       
   127         }
       
   128     else if (iState == EMPXConnectionMTPActive)
       
   129         {
       
   130         TRAP_IGNORE( DoMTPStartEventL() );
       
   131         }
       
   132     else if (iState == EMPXConnectionMTPIdle)
       
   133         {
       
   134         TRAP_IGNORE( DoMTPNotActiveEventL() );
       
   135         }
       
   136     }
       
   137 
       
   138 // ---------------------------------------------------------------------------
       
   139 // Handle the PS key event
       
   140 // ---------------------------------------------------------------------------
       
   141 //
       
   142 void CMPXConnectionEventHandler::HandlePSEvent( TUid aUid, TInt aKey )
       
   143     {
       
   144     TRAP_IGNORE( DoHandlePSEventL(aUid,aKey) );
       
   145     }
       
   146 
       
   147 // ---------------------------------------------------------------------------
       
   148 // Handle the PS key event
       
   149 // ---------------------------------------------------------------------------
       
   150 //
       
   151 void CMPXConnectionEventHandler::DoHandlePSEventL( TUid /*aUid*/, TInt /*aKey*/ )
       
   152     {
       
   153     MPX_FUNC("CMPXConnectionEventHandler::DoHandlePSEvent()");
       
   154     if ( !iUsbManConnected )
       
   155         {
       
   156         ConnectUsbMan();
       
   157         }
       
   158     
       
   159     // Handle the Key event
       
   160     TInt usbStatus;
       
   161     iUSBKeyWatcher->GetValue( usbStatus );
       
   162     
       
   163     TInt mtpStatus;
       
   164     iMTPKeyWatcher->GetValue(mtpStatus);
       
   165     
       
   166     MPX_DEBUG3("CMPXConnectionEventHandler::DoHandlePSEventL, usbStatus = %d, mtpStatus = %d", usbStatus, mtpStatus);
       
   167     MPX_DEBUG2("CMPXConnectionEventHandler::DoHandlePSEventL, iState = %d", iState);
       
   168     MPX_DEBUG4("CMPXConnectionEventHandler::DoHandlePSEventL, iDeviceState = %d, EUsbDeviceStateAddress(%d), EUsbDeviceStateConfigured(%d)", iDeviceState, EUsbDeviceStateAddress, EUsbDeviceStateConfigured);
       
   169     
       
   170     RDebug::Print(_L("tpoon: CMPXConnectionEventHandler::DoHandlePSEventL, usbStatus = %d, mtpStatus = %d"), usbStatus, mtpStatus);
       
   171     RDebug::Print(_L("tpoon: CMPXConnectionEventHandler::DoHandlePSEventL, iState = %d"), iState);
       
   172     RDebug::Print(_L("tpoon: CMPXConnectionEventHandler::DoHandlePSEventL, iDeviceState = %d, EUsbDeviceStateAddress(%d), EUsbDeviceStateConfigured(%d)"), iDeviceState, EUsbDeviceStateAddress, EUsbDeviceStateConfigured);
       
   173     
       
   174     // events from lower level is not causing multiple callback on the same type
       
   175     // usb is really only connected if went through Address or Configured state
       
   176     if( (usbStatus == KUsbPersonalityIdMS) && (iDeviceState == EUsbDeviceStateAddress || iDeviceState == EUsbDeviceStateConfigured))
       
   177         {
       
   178         if ((iState == EMPXConnectionMTPIdle) || (iState == EMPXConnectionMTPActive))
       
   179             {
       
   180             MPX_DEBUG1("CMPXConnectionEventHandler::DoHandlePSEvent - MTP End");
       
   181             RDebug::Print(_L("tpoon: MTP End"));
       
   182             iObserver.HandleSystemEventL( EUSBMTPEndEvent, -1 );
       
   183             iState = EMPXConnectionNone;
       
   184             }
       
   185         
       
   186         if (iState != EMPXConnectionMassStorage)
       
   187             {
       
   188             DoMSStartEventL();
       
   189             }
       
   190         }
       
   191     else if (iState == EMPXConnectionMassStorage)
       
   192         {
       
   193         if (usbStatus != KUsbPersonalityIdMS)
       
   194             {
       
   195             MPX_DEBUG1("CMPXConnectionEventHandler::DoHandlePSEvent - USB MassStorage End");
       
   196             RDebug::Print(_L("tpoon: MS End"));
       
   197             iObserver.HandleSystemEventL( EUSBMassStorageEndEvent, -1 );
       
   198             iState = EMPXConnectionNone;
       
   199             }
       
   200         }
       
   201     
       
   202     // after MassStorage End, it is possible that MTP is still connected
       
   203     if (iState != EMPXConnectionMassStorage)
       
   204         {
       
   205         if ((mtpStatus == EMtpPSStatusUninitialized) && (iState != EMPXConnectionNone))
       
   206             {
       
   207             MPX_DEBUG1("CMPXConnectionEventHandler::DoHandlePSEvent - MTP End");
       
   208             RDebug::Print(_L("tpoon: MTP End"));
       
   209             iObserver.HandleSystemEventL( EUSBMTPEndEvent, -1 );
       
   210             iState = EMPXConnectionNone;
       
   211             }
       
   212         else if ((mtpStatus == EMtpPSStatusActive) && (iState != EMPXConnectionMTPActive)
       
   213                 && ((usbStatus == KUsbPersonalityIdMTP) || (usbStatus == KUsbPersonalityIdPCSuiteMTP))) // only trigger MusicPlayer fully block and RAM Drive if USB MTP/PCSuiteMTP is connected
       
   214             {
       
   215             DoMTPStartEventL();
       
   216             }
       
   217         else if ((mtpStatus == EMtpPSStatusReadyToSync) && (iState != EMPXConnectionMTPIdle) && (iState != EMPXConnectionMTPActive))
       
   218             {
       
   219             DoMTPNotActiveEventL();
       
   220             }
       
   221         }
       
   222     }
       
   223 
       
   224 void CMPXConnectionEventHandler::DoMSStartEventL()
       
   225     {
       
   226     MPX_DEBUG1("CMPXConnectionEventHandler::DoMSStartEventL - USB MassStorage Start");
       
   227     RDebug::Print(_L("tpoon: MS Start"));
       
   228     iObserver.HandleSystemEventL( EUSBMassStorageStartEvent, -1 );
       
   229     iState = EMPXConnectionMassStorage;
       
   230     }
       
   231 
       
   232 void CMPXConnectionEventHandler::DoMTPStartEventL()
       
   233     {
       
   234     MPX_DEBUG1("CMPXConnectionEventHandler::DoMTPStartEventL - MTP Start");
       
   235     RDebug::Print(_L("tpoon: MTP Start"));
       
   236     iObserver.HandleSystemEventL( EUSBMTPStartEvent, -1 );
       
   237     iState = EMPXConnectionMTPActive;
       
   238     }
       
   239 
       
   240 void CMPXConnectionEventHandler::DoMTPNotActiveEventL()
       
   241     {
       
   242     MPX_DEBUG1("CMPXConnectionEventHandler::DoMTPNotActiveEventL - MTP Not Active");
       
   243     RDebug::Print(_L("tpoon: MTP Not Active"));
       
   244     iObserver.HandleSystemEventL( EUSBMTPNotActiveEvent, -1 );
       
   245     iState = EMPXConnectionMTPIdle;
       
   246     }
       
   247 
       
   248 // ---------------------------------------------------------------------------
       
   249 // CMPXConnectionEventHandler::RunL
       
   250 // ---------------------------------------------------------------------------
       
   251 //
       
   252 void CMPXConnectionEventHandler::RunL()
       
   253     {
       
   254     MPX_FUNC("CMPXConnectionEventHandler::RunL()");
       
   255     TInt status( iStatus.Int() );
       
   256     
       
   257     MPX_DEBUG2("CMPXConnectionEventHandler::RunL status=%d", status );
       
   258     if ( status != KErrCancel && status != KErrServerTerminated )
       
   259         {
       
   260         iUsbMan.DeviceStateNotification( KUsbAllStates, iDeviceState, iStatus );
       
   261         SetActive();
       
   262         }
       
   263     
       
   264     if ( status == KErrNone )
       
   265         {
       
   266         MPX_DEBUG2("CMPXConnectionEventHandler::RunL - DeviceState = %d", iDeviceState);
       
   267         HandlePSEvent( TUid::Uid(0), 0 );
       
   268         }
       
   269     }
       
   270 
       
   271 // ---------------------------------------------------------------------------
       
   272 // CMPXConnectionEventHandler::DoCancel
       
   273 // ---------------------------------------------------------------------------
       
   274 //
       
   275 void CMPXConnectionEventHandler::DoCancel()
       
   276     {
       
   277     MPX_FUNC("CMPXConnectionEventHandler::DoCancel()");
       
   278     if ( iUsbManConnected )
       
   279         {
       
   280         iUsbMan.DeviceStateNotificationCancel();
       
   281         }
       
   282     }
       
   283 
       
   284 // ---------------------------------------------------------------------------
       
   285 // CMPXConnectionEventHandler::ConnectUsbMan
       
   286 // If error, default iDeviceState to EUsbDeviceStateConfigured so this would not
       
   287 // block usb event mode change.
       
   288 // ---------------------------------------------------------------------------
       
   289 void CMPXConnectionEventHandler::ConnectUsbMan()
       
   290     {
       
   291     MPX_FUNC("CMPXConnectionEventHandler::ConnectUsbMan()");
       
   292     if ( iUsbMan.Connect() == KErrNone )
       
   293         {
       
   294         iUsbManConnected = ETrue;
       
   295         // get device state
       
   296         TInt err = iUsbMan.GetDeviceState( iDeviceState );
       
   297         if ( err )
       
   298             {
       
   299             iDeviceState = EUsbDeviceStateUndefined;
       
   300             }
       
   301         
       
   302         // start active object
       
   303         if ( !IsActive() )
       
   304             {
       
   305             iUsbMan.DeviceStateNotification( KUsbAllStates, iDeviceState, iStatus );
       
   306             SetActive();
       
   307             }
       
   308         }
       
   309     else
       
   310         {
       
   311         iDeviceState = EUsbDeviceStateConfigured;
       
   312         }
       
   313     }
       
   314 // END OF FILE
       
   315