mmappcomponents/harvester/server/src/mpxconnectioneventhandler.cpp
branchGCC_SURGE
changeset 47 f3d2589f22d6
parent 43 7b58e65d61ee
equal deleted inserted replaced
36:3b7506f63335 47:f3d2589f22d6
       
     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     TInt err = 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     // events from lower level is not causing multiple callback on the same type
       
   171     // usb is really only connected if went through Address or Configured state
       
   172     if( (usbStatus == KUsbPersonalityIdMS) && (iDeviceState == EUsbDeviceStateAddress || iDeviceState == EUsbDeviceStateConfigured))
       
   173         {
       
   174         if ((iState == EMPXConnectionMTPIdle) || (iState == EMPXConnectionMTPActive))
       
   175             {
       
   176             MPX_DEBUG1("CMPXConnectionEventHandler::DoHandlePSEvent - MTP End");
       
   177             iObserver.HandleSystemEventL( EUSBMTPEndEvent, -1 );
       
   178             iState = EMPXConnectionNone;
       
   179             }
       
   180         
       
   181         if (iState != EMPXConnectionMassStorage)
       
   182             {
       
   183             DoMSStartEventL();
       
   184             }
       
   185         }
       
   186     else if (iState == EMPXConnectionMassStorage)
       
   187         {
       
   188         if (usbStatus != KUsbPersonalityIdMS)
       
   189             {
       
   190             MPX_DEBUG1("CMPXConnectionEventHandler::DoHandlePSEvent - USB MassStorage End");
       
   191             iObserver.HandleSystemEventL( EUSBMassStorageEndEvent, -1 );
       
   192             iState = EMPXConnectionNone;
       
   193             }
       
   194         }
       
   195     
       
   196     // after MassStorage End, it is possible that MTP is still connected
       
   197     if (iState != EMPXConnectionMassStorage)
       
   198         {
       
   199 		if (err == KErrNone)
       
   200 		    {
       
   201             if ((mtpStatus == EMtpPSStatusUninitialized) && (iState != EMPXConnectionNone))
       
   202                 {
       
   203                 MPX_DEBUG1("CMPXConnectionEventHandler::DoHandlePSEvent - MTP End");
       
   204                 iObserver.HandleSystemEventL( EUSBMTPEndEvent, -1 );
       
   205                 iState = EMPXConnectionNone;
       
   206                 }
       
   207             else if ((mtpStatus == EMtpPSStatusActive) && (iState != EMPXConnectionMTPActive)
       
   208                     && ((usbStatus == KUsbPersonalityIdMTP) || (usbStatus == KUsbPersonalityIdPCSuiteMTP))) // only trigger MusicPlayer fully block and RAM Drive if USB MTP/PCSuiteMTP is connected
       
   209                 {
       
   210                 DoMTPStartEventL();
       
   211                 }
       
   212             else if ((mtpStatus == EMtpPSStatusReadyToSync) && (iState != EMPXConnectionMTPIdle) && (iState != EMPXConnectionMTPActive))
       
   213                 {
       
   214                 DoMTPNotActiveEventL();
       
   215                 }
       
   216             }
       
   217         else
       
   218             {
       
   219             // mtpwatcher can err out because the mtp ps key would only be defined and default after first use,
       
   220             // and this logic here might hit before that
       
   221             MPX_DEBUG2("CMPXConnectionEventHandler::DoHandlePSEvent - MTPWatcher err = %d", err);
       
   222             }
       
   223         }
       
   224     }
       
   225 
       
   226 void CMPXConnectionEventHandler::DoMSStartEventL()
       
   227     {
       
   228     MPX_DEBUG1("CMPXConnectionEventHandler::DoMSStartEventL - USB MassStorage Start");
       
   229     iObserver.HandleSystemEventL( EUSBMassStorageStartEvent, -1 );
       
   230     iState = EMPXConnectionMassStorage;
       
   231     }
       
   232 
       
   233 void CMPXConnectionEventHandler::DoMTPStartEventL()
       
   234     {
       
   235     MPX_DEBUG1("CMPXConnectionEventHandler::DoMTPStartEventL - 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     iObserver.HandleSystemEventL( EUSBMTPNotActiveEvent, -1 );
       
   244     iState = EMPXConnectionMTPIdle;
       
   245     }
       
   246 
       
   247 // ---------------------------------------------------------------------------
       
   248 // CMPXConnectionEventHandler::RunL
       
   249 // ---------------------------------------------------------------------------
       
   250 //
       
   251 void CMPXConnectionEventHandler::RunL()
       
   252     {
       
   253     MPX_FUNC("CMPXConnectionEventHandler::RunL()");
       
   254     TInt status( iStatus.Int() );
       
   255     
       
   256     MPX_DEBUG2("CMPXConnectionEventHandler::RunL status=%d", status );
       
   257     if ( status != KErrCancel && status != KErrServerTerminated )
       
   258         {
       
   259         iUsbMan.DeviceStateNotification( KUsbAllStates, iDeviceState, iStatus );
       
   260         SetActive();
       
   261         }
       
   262     
       
   263     if ( status == KErrNone )
       
   264         {
       
   265         MPX_DEBUG2("CMPXConnectionEventHandler::RunL - DeviceState = %d", iDeviceState);
       
   266         HandlePSEvent( TUid::Uid(0), 0 );
       
   267         }
       
   268     }
       
   269 
       
   270 // ---------------------------------------------------------------------------
       
   271 // CMPXConnectionEventHandler::DoCancel
       
   272 // ---------------------------------------------------------------------------
       
   273 //
       
   274 void CMPXConnectionEventHandler::DoCancel()
       
   275     {
       
   276     MPX_FUNC("CMPXConnectionEventHandler::DoCancel()");
       
   277     if ( iUsbManConnected )
       
   278         {
       
   279         iUsbMan.DeviceStateNotificationCancel();
       
   280         }
       
   281     }
       
   282 
       
   283 // ---------------------------------------------------------------------------
       
   284 // CMPXConnectionEventHandler::ConnectUsbMan
       
   285 // If error, default iDeviceState to EUsbDeviceStateConfigured so this would not
       
   286 // block usb event mode change.
       
   287 // ---------------------------------------------------------------------------
       
   288 void CMPXConnectionEventHandler::ConnectUsbMan()
       
   289     {
       
   290     MPX_FUNC("CMPXConnectionEventHandler::ConnectUsbMan()");
       
   291     if ( iUsbMan.Connect() == KErrNone )
       
   292         {
       
   293         iUsbManConnected = ETrue;
       
   294         // get device state
       
   295         TInt err = iUsbMan.GetDeviceState( iDeviceState );
       
   296         if ( err )
       
   297             {
       
   298             iDeviceState = EUsbDeviceStateUndefined;
       
   299             }
       
   300         
       
   301         // start active object
       
   302         if ( !IsActive() )
       
   303             {
       
   304             iUsbMan.DeviceStateNotification( KUsbAllStates, iDeviceState, iStatus );
       
   305             SetActive();
       
   306             }
       
   307         }
       
   308     else
       
   309         {
       
   310         iDeviceState = EUsbDeviceStateConfigured;
       
   311         }
       
   312     }
       
   313 // END OF FILE
       
   314