usbengines/usbotgwatcher/src/cusbstatehostainitiate.cpp
branchRCL_3
changeset 65 a44cdf4b4bf0
parent 64 8ecef05bbada
child 21 ff9df6630274
equal deleted inserted replaced
64:8ecef05bbada 65:a44cdf4b4bf0
     1 /*
     1 /*
     2 * Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
     2  * Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3  * All rights reserved.
     4 * This component and the accompanying materials are made available
     4  * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     5  * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     6  * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     8  *
     9 * Initial Contributors:
     9  * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    10  * Nokia Corporation - initial contribution.
    11 *
    11  *
    12 * Contributors:
    12  * Contributors:
    13 *
    13  *
    14 * Description:  Implementation
    14  * Description:  Implementation
    15  *
    15  *
    16 */
    16  */
    17 
    17 #include <usbuinotif.h>
    18 
    18 
    19 #include "cusbstatehostainitiate.h"
    19 #include "cusbstatehostainitiate.h"
    20 #ifndef STIF
    20 #ifndef STIF
       
    21 #include "cusbtimer.h"
    21 #include "cusbnotifmanager.h"
    22 #include "cusbnotifmanager.h"
    22 #include "cusbtimer.h"
       
    23 #else
    23 #else
    24 #include "mockcusbnotifmanager.h"
    24 #include "mockcusbnotifmanager.h"
    25 #include "mockcusbtimer.h"
    25 #include "mockcusbtimer.h"
    26 #endif
    26 #endif
       
    27 
    27 #include "definitions.h"
    28 #include "definitions.h"
    28 
       
    29 #include "errors.h"
    29 #include "errors.h"
       
    30 
    30 #include "debug.h"
    31 #include "debug.h"
    31 #include "panic.h"
    32 #include "panic.h"
    32 
    33 
    33 // ---------------------------------------------------------------------------
    34 // ---------------------------------------------------------------------------
    34 // 
    35 // 
    35 // ---------------------------------------------------------------------------
    36 // ---------------------------------------------------------------------------
    36 //
    37 //
    37 CUsbStateHostAInitiate::CUsbStateHostAInitiate(CUsbOtgWatcher* aWatcher) :
    38 CUsbStateHostAInitiate::CUsbStateHostAInitiate(CUsbOtgWatcher& aWatcher) :
    38     CUsbStateHostAInitiateBase(aWatcher)
    39     CUsbStateHostABase(aWatcher)
    39     {
    40     {
    40     }
    41     }
    41 
    42 
    42 // ---------------------------------------------------------------------------
    43 // ---------------------------------------------------------------------------
    43 // 
    44 // 
    44 // ---------------------------------------------------------------------------
    45 // ---------------------------------------------------------------------------
    45 //
    46 //
    46 void CUsbStateHostAInitiate::ConstructL()
    47 void CUsbStateHostAInitiate::ConstructL()
    47     {
    48     {
    48         FLOG( _L( "[USBOTGWATCHER]\tCUsbStateHostAInitiate::ConstructL" ) );
    49     LOG_FUNC
    49 
    50 
    50     CUsbStateHostAInitiateBase::ConstructL();
    51     CUsbStateHostABase::ConstructL();
    51     }
    52 
    52 
    53     iAttachmentTimer = CUsbTimer::NewL(*this, EDeviceAttachmentTimer);
    53 // ---------------------------------------------------------------------------
    54 
    54 // 
    55     }
    55 // ---------------------------------------------------------------------------
    56 
    56 //
    57 // ---------------------------------------------------------------------------
    57 CUsbStateHostAInitiate* CUsbStateHostAInitiate::NewL(CUsbOtgWatcher* aWatcher)
    58 // 
    58     {
    59 // ---------------------------------------------------------------------------
    59         FLOG( _L( "[USBOTGWATCHER]\tCUsbStateHostAInitiate::NewL" ) );
    60 //
       
    61 CUsbStateHostAInitiate* CUsbStateHostAInitiate::NewL(CUsbOtgWatcher& aWatcher)
       
    62     {
       
    63     LOG_FUNC
    60 
    64 
    61     CUsbStateHostAInitiate* self = new (ELeave) CUsbStateHostAInitiate(
    65     CUsbStateHostAInitiate* self = new (ELeave) CUsbStateHostAInitiate(
    62             aWatcher);
    66             aWatcher);
    63     CleanupStack::PushL(self);
    67     CleanupStack::PushL(self);
    64     self->ConstructL();
    68     self->ConstructL();
    70 // 
    74 // 
    71 // ---------------------------------------------------------------------------
    75 // ---------------------------------------------------------------------------
    72 //
    76 //
    73 CUsbStateHostAInitiate::~CUsbStateHostAInitiate()
    77 CUsbStateHostAInitiate::~CUsbStateHostAInitiate()
    74     {
    78     {
    75         FLOG( _L( "[USBOTGWATCHER]\tCUsbStateHostAInitiate::~CUsbStateHostAInitiate" ) );
    79     LOG_FUNC
       
    80 
       
    81     delete iAttachmentTimer;
    76     }
    82     }
    77 
    83 
    78 // ---------------------------------------------------------------------------
    84 // ---------------------------------------------------------------------------
    79 // 
    85 // 
    80 // ---------------------------------------------------------------------------
    86 // ---------------------------------------------------------------------------
    81 //
    87 //
    82 TUsbStateIds CUsbStateHostAInitiate::Id()
    88 TUsbStateIds CUsbStateHostAInitiate::Id()
    83     {
    89     {
    84         FLOG( _L( "[USBOTGWATCHER]\tCUsbStateHostAInitiate::Id" ) );
       
    85 
       
    86     return EUsbStateHostAInitiate;
    90     return EUsbStateHostAInitiate;
    87     }
    91     }
    88 
    92 
    89 // ---------------------------------------------------------------------------
    93 // ---------------------------------------------------------------------------
    90 // 
    94 // 
    91 // ---------------------------------------------------------------------------
    95 // ---------------------------------------------------------------------------
    92 //
    96 //
       
    97 void CUsbStateHostAInitiate::JustBeforeLeavingThisStateL()
       
    98     {
       
    99     LOG_FUNC
       
   100 
       
   101     iAttachmentTimer->Cancel();
       
   102 
       
   103     // do general things 
       
   104     CUsbStateHostABase::JustBeforeLeavingThisStateL();
       
   105     }
       
   106 
       
   107 // ---------------------------------------------------------------------------
       
   108 // 
       
   109 // ---------------------------------------------------------------------------
       
   110 //
    93 void CUsbStateHostAInitiate::JustAdvancedToThisStateL()
   111 void CUsbStateHostAInitiate::JustAdvancedToThisStateL()
    94     {
   112     {
    95         FLOG( _L( "[USBOTGWATCHER]\tCUsbStateHostAInitiate::JustAdvancedToThisStateL" ) );
   113     LOG_FUNC
    96 
   114 
    97     iWatcher->PrintStateToLog();
   115     // do general things 
    98 
   116     CUsbStateHostABase::JustAdvancedToThisStateL();
    99     TInt err = iWatcher->Usb().EnableFunctionDriverLoading();
   117 
       
   118     TInt err = iWatcher.Usb().EnableFunctionDriverLoading();
   100 
   119 
   101     if (KErrNone != err)
   120     if (KErrNone != err)
   102         {
   121         {
   103             FLOG( _L( "[USBOTGWATCHER]\tCUsbStateHostAInitiate::JustAdvancedToThisStateL EnableFunctionDriverLoading error" ) );
   122         LOG1( "EnableFunctionDriverLoading err = %d", err );
   104         iWatcher->HandleHostProblemL(EUsbWatcherErrorInConnection, EUsbStateHostHandle);
   123         iWatcher.HandleHostProblemL(EUsbWatcherCanNotEnableDriverLoading,
       
   124                 EUsbStateHostHandleDropping);
   105         return;
   125         return;
   106         }
   126         }
   107 
   127 
   108         FTRACE( FPrint(_L( "[USBOTGWATCHER]\tCUsbStateHostAInitiate::JustAdvancedToThisStateL EnableFunctionDriverLoading(); = %d" ), err));
       
   109 
       
   110     // do BusRequest, if down
   128     // do BusRequest, if down
   111 
   129 
   112     if (CUsbVBusObserver::EVBusUp != iWatcher->VBusObserver()->VBus())
   130     if (CUsbVBusObserver::EVBusUp != iWatcher.VBusObserver()->VBus())
   113         {
   131         {
   114         const TUint maxTrial = 3;
   132         const TUint maxTrial = 3;
   115         TInt busReqErr(KErrGeneral);
   133         TInt busReqErr(KErrGeneral);
   116         TUint count(0);
   134         TUint count(0);
   117 
   135 
   118         while (count < maxTrial && KErrNone != busReqErr)
   136         while (count < maxTrial && KErrNone != busReqErr)
   119             {
   137             {
   120             FLOG( _L( "[USBOTGWATCHER]\tCUsbStateHostAInitiate Calling RUsb::BusRequest()..." ) );
   138             busReqErr = iWatcher.Usb().BusRequest();
   121             busReqErr = iWatcher->Usb().BusRequest();
   139             LOG1( "BusRequest() err = %d" , err);
   122                 FTRACE( FPrint(_L( "[USBOTGWATCHER]\tCUsbStateHostAInitiate::JustAdvancedToThisStateL BusRequest() = %d" ), err));
       
   123 
   140 
   124             if (KErrNone != busReqErr)
   141             if (KErrNone != busReqErr)
   125                 {
   142                 {
   126                 err = iWatcher->Usb().BusClearError();
   143                 err = iWatcher.Usb().BusClearError();
   127                 if (KErrNone != err)
   144                 if (KErrNone != err)
   128                     {
   145                     {
   129                     iWatcher->HandleHostProblemL(EUsbWatcherErrorInConnection, EUsbStateHostHandle);
   146                     iWatcher.HandleHostProblemL(
       
   147                             EUsbWatcherCanNotClearBusError,
       
   148                             EUsbStateHostHandleDropping);
   130                     return;
   149                     return;
   131                     }
   150                     }
   132                 }
   151                 }
   133             ++count;
   152             ++count;
   134             }
   153             }
   135         if (KErrNone != busReqErr)
   154         if (KErrNone != busReqErr)
   136             {
   155             {
   137             iWatcher->HandleHostProblemL(EUsbWatcherErrorInConnection, EUsbStateHostHandle);
   156             iWatcher.HandleHostProblemL(EUsbWatcherCanNotRaiseVBus,
       
   157                     EUsbStateHostHandleDropping);
   138             return;
   158             return;
   139             }
   159             }
   140         }
   160         }
   141 
   161 
   142     // start timer, waiting for device attachment
   162     // start timer, waiting for device attachment
   143     iAttachmentTimer->After(KTimeToWaitForDeviceAttachment);
   163     iAttachmentTimer->After(KTimeToWaitForDeviceAttachment);
   144 
   164 
   145     }
   165     }
   146 
   166 
   147 // ---------------------------------------------------------------------------
   167 // From TimerObserver
   148 // 
   168 // ---------------------------------------------------------------------------
   149 // ---------------------------------------------------------------------------
   169 // 
   150 //
   170 // ---------------------------------------------------------------------------
   151 void CUsbStateHostAInitiate::JustBeforeLeavingThisStateL()
   171 //
   152     {
   172 void CUsbStateHostAInitiate::TimerElapsedL(TUsbTimerId aTimerId)
   153         FLOG( _L( "[USBOTGWATCHER]\tCUsbStateHostAInitiate::JustBeforeLeavingThisStateL" ) );
   173     {
   154     
   174     LOG_FUNC
   155         CUsbStateHostAInitiateBase::JustBeforeLeavingThisStateL();
   175 
   156     }
   176     switch (aTimerId)
       
   177         {
       
   178         case EDeviceAttachmentTimer:
       
   179             {
       
   180             LOG("AttachmentTimer" );
       
   181             HandleL(EUsbWatcherErrDandlingCable, EUsbStateHostHandleDropping);
       
   182             break;
       
   183             }
       
   184         default:
       
   185             {
       
   186             LOG1( "Unknown timer id = %d", aTimerId );
       
   187             Panic( EWrongTimerId);
       
   188             }
       
   189         }
       
   190     }
       
   191 
       
   192 // From VBus observer
       
   193 // ---------------------------------------------------------------------------
       
   194 // 
       
   195 // ---------------------------------------------------------------------------
       
   196 //
       
   197 void CUsbStateHostAInitiate::VBusUpL()
       
   198     {
       
   199     LOG_FUNC
       
   200     }
       
   201 
       
   202 // From Host Event notification observer
       
   203 // ---------------------------------------------------------------------------
       
   204 // 
       
   205 // ---------------------------------------------------------------------------
       
   206 //
       
   207 void CUsbStateHostAInitiate::DeviceAttachedL(TDeviceEventInformation aTdi)
       
   208     {
       
   209     LOG_FUNC
       
   210 
       
   211     iAttachmentTimer->Cancel();
       
   212 
       
   213     // check if an OTG device connected
       
   214     TOtgDescriptor otgDescriptor;
       
   215 
       
   216     // ignore all the errors, assume we connected to not otg
       
   217     TInt err = iWatcher.Usb().GetOtgDescriptor(aTdi.iDeviceId, otgDescriptor);
       
   218     LOG1("GetOtgDescriptor() err = %d", err );
       
   219 
       
   220     TBool hnpSupported(otgDescriptor.iAttributes & EUsbOtgHNPSupported);
       
   221     TBool srpSupported(otgDescriptor.iAttributes & EUsbOtgSRPSupported);
       
   222 
       
   223     // OTG device supports both hnp and srp
       
   224     if (hnpSupported && srpSupported)
       
   225         {
       
   226 
       
   227         HandleL(EUsbWatcherConnectedToOTG, EUsbStateHostHandleDropping);
       
   228         return;
       
   229         }
       
   230 
       
   231     if (KErrNone != aTdi.iError)
       
   232         {
       
   233         switch (aTdi.iError)
       
   234             // error in attachement
       
   235             {
       
   236             case KErrBadPower:
       
   237                 {
       
   238                 LOG( "TooMuchPower" );
       
   239                 HandleL(
       
   240                         EUsbWatcherErrDeviceRequiresTooMuchPowerOnEnumeration,
       
   241                         EUsbStateHostDelayNotAttachedHandle);
       
   242                 break;
       
   243                 }
       
   244             default:
       
   245                 {
       
   246                 LOG1("AttachmentError aTdi.iError = %d" , aTdi.iError );
       
   247                 HandleL(EUsbWatcherErrUnsupportedDevice,
       
   248                         EUsbStateHostHandleDropping);
       
   249                 break;
       
   250                 }
       
   251             }
       
   252 
       
   253         return;
       
   254         }
       
   255     }
       
   256 
       
   257 // ---------------------------------------------------------------------------
       
   258 // 
       
   259 // ---------------------------------------------------------------------------
       
   260 //
       
   261 void CUsbStateHostAInitiate::DriverLoadSuccessL(TDeviceEventInformation)
       
   262     {
       
   263     LOG_FUNC
       
   264     ChangeHostStateL( EUsbStateHostAHost);
       
   265     }
       
   266 
       
   267 // ---------------------------------------------------------------------------
       
   268 // 
       
   269 // ---------------------------------------------------------------------------
       
   270 //
       
   271 void CUsbStateHostAInitiate::DriverLoadPartialSuccessL(
       
   272         TDeviceEventInformation)
       
   273     {
       
   274     LOG_FUNC
       
   275     iWatcher.NotifManager()->ShowNotifierL(KUsbUiNotifOtgWarning,
       
   276             EUsbOtgPartiallySupportedDevice, NULL);
       
   277     ChangeHostStateL( EUsbStateHostAHost);
       
   278 
       
   279     }
       
   280 
       
   281 // ---------------------------------------------------------------------------
       
   282 // 
       
   283 // ---------------------------------------------------------------------------
       
   284 //
       
   285 void CUsbStateHostAInitiate::DriverLoadFailureL(TDeviceEventInformation aDei)
       
   286     {
       
   287     LOG_FUNC
       
   288 
       
   289     HandleL(EUsbWatcherErrDriversNotFound, EUsbStateHostDelayAttachedHandle);
       
   290     }