usbengines/usbotgwatcher/src/cusbstatehostainitiate.cpp
changeset 35 9d8b04ca6939
child 63 ef2686f7597e
equal deleted inserted replaced
34:7858bc6ead78 35:9d8b04ca6939
       
     1 /*
       
     2  * Copyright (c) 2008 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:  Implementation
       
    15  *
       
    16  */
       
    17 #include <usbuinotif.h>
       
    18 #include <usbhosterrors.h>
       
    19 
       
    20 #include "cusbotgwatcher.h"
       
    21 #include "cusbstatehostainitiate.h"
       
    22 
       
    23 #ifndef STIF
       
    24 #include "cusbtimer.h"
       
    25 #include "cusbnotifmanager.h"
       
    26 #else
       
    27 #include "mockcusbnotifmanager.h"
       
    28 #include "mockcusbtimer.h"
       
    29 #endif
       
    30 
       
    31 #include "definitions.h"
       
    32 #include "errors.h"
       
    33 
       
    34 #include "debug.h"
       
    35 #include "panic.h"
       
    36 
       
    37 // ---------------------------------------------------------------------------
       
    38 // 
       
    39 // ---------------------------------------------------------------------------
       
    40 //
       
    41 CUsbStateHostAInitiate::CUsbStateHostAInitiate(CUsbOtgWatcher& aWatcher) :
       
    42     CUsbStateHostABase(aWatcher), iDeviceAttached(EFalse)
       
    43     {
       
    44     }
       
    45 
       
    46 // ---------------------------------------------------------------------------
       
    47 // 
       
    48 // ---------------------------------------------------------------------------
       
    49 //
       
    50 void CUsbStateHostAInitiate::ConstructL()
       
    51     {
       
    52     LOG_FUNC
       
    53 
       
    54     CUsbStateHostABase::ConstructL();
       
    55 
       
    56     iAttachmentTimer = CUsbTimer::NewL(*this, EDeviceAttachmentTimer);
       
    57 
       
    58     }
       
    59 
       
    60 // ---------------------------------------------------------------------------
       
    61 // 
       
    62 // ---------------------------------------------------------------------------
       
    63 //
       
    64 CUsbStateHostAInitiate* CUsbStateHostAInitiate::NewL(CUsbOtgWatcher& aWatcher)
       
    65     {
       
    66     LOG_FUNC
       
    67 
       
    68     CUsbStateHostAInitiate* self = new (ELeave) CUsbStateHostAInitiate(
       
    69             aWatcher);
       
    70     CleanupStack::PushL(self);
       
    71     self->ConstructL();
       
    72     CleanupStack::Pop(self);
       
    73     return self;
       
    74     }
       
    75 
       
    76 // ---------------------------------------------------------------------------
       
    77 // 
       
    78 // ---------------------------------------------------------------------------
       
    79 //
       
    80 CUsbStateHostAInitiate::~CUsbStateHostAInitiate()
       
    81     {
       
    82     LOG_FUNC
       
    83 
       
    84     delete iAttachmentTimer;
       
    85     }
       
    86 
       
    87 // ---------------------------------------------------------------------------
       
    88 // 
       
    89 // ---------------------------------------------------------------------------
       
    90 //
       
    91 TUsbStateIds CUsbStateHostAInitiate::Id()
       
    92     {
       
    93     return EUsbStateHostAInitiate;
       
    94     }
       
    95 
       
    96 // ---------------------------------------------------------------------------
       
    97 // 
       
    98 // ---------------------------------------------------------------------------
       
    99 //
       
   100 void CUsbStateHostAInitiate::JustBeforeLeavingThisStateL()
       
   101     {
       
   102     LOG_FUNC
       
   103 
       
   104     iAttachmentTimer->Cancel();
       
   105 
       
   106     // do general things 
       
   107     CUsbStateHostABase::JustBeforeLeavingThisStateL();
       
   108     }
       
   109 
       
   110 // ---------------------------------------------------------------------------
       
   111 // 
       
   112 // ---------------------------------------------------------------------------
       
   113 //
       
   114 void CUsbStateHostAInitiate::JustAdvancedToThisStateL()
       
   115     {
       
   116     LOG_FUNC
       
   117 
       
   118     // do general things 
       
   119     CUsbStateHostABase::JustAdvancedToThisStateL();
       
   120 
       
   121     // clear this attribute, and wait for attachment
       
   122     // one attchment has to correspond to only one detachment
       
   123     iDeviceAttached = EFalse; // to catch not corresponding Detachment, if any 
       
   124 
       
   125     TInt err = iWatcher.Usb().EnableFunctionDriverLoading();
       
   126 
       
   127     if (KErrNone != err)
       
   128         {
       
   129         LOG1( "EnableFunctionDriverLoading err = %d", err );
       
   130         iWatcher.HandleHostProblemL(EUsbWatcherCanNotEnableDriverLoading,
       
   131                 EUsbStateHostHandleDropping);
       
   132         return;
       
   133         }
       
   134 
       
   135     // do BusRequest, if down
       
   136 
       
   137     if (CUsbVBusObserver::EVBusUp != iWatcher.VBusObserver()->VBus())
       
   138         {
       
   139         const TUint maxTrial = 3;
       
   140         TInt busReqErr(KErrGeneral);
       
   141         TUint count(0);
       
   142 
       
   143         while (count < maxTrial && KErrNone != busReqErr)
       
   144             {
       
   145             busReqErr = iWatcher.Usb().BusRequest();
       
   146             LOG1( "BusRequest() busReqErr = %d" , busReqErr);
       
   147 
       
   148             if (KErrNone != busReqErr)
       
   149                 {
       
   150                 err = iWatcher.Usb().BusClearError();
       
   151                 if (KErrNone != err)
       
   152                     {
       
   153                     iWatcher.HandleHostProblemL(
       
   154                             EUsbWatcherCanNotClearBusError,
       
   155                             EUsbStateHostHandleDropping);
       
   156                     return;
       
   157                     }
       
   158                 }
       
   159             ++count;
       
   160             }
       
   161         if (KErrNone != busReqErr)
       
   162             {
       
   163             iWatcher.HandleHostProblemL(EUsbWatcherCanNotRaiseVBus,
       
   164                     EUsbStateHostHandleDropping);
       
   165             return;
       
   166             }
       
   167         }
       
   168 
       
   169     // start timer, waiting for device attachment
       
   170     iAttachmentTimer->After(KTimeToWaitForDeviceAttachment);
       
   171 
       
   172     }
       
   173 
       
   174 // From TimerObserver
       
   175 // ---------------------------------------------------------------------------
       
   176 // 
       
   177 // ---------------------------------------------------------------------------
       
   178 //
       
   179 void CUsbStateHostAInitiate::TimerElapsedL(TUsbTimerId aTimerId)
       
   180     {
       
   181     LOG_FUNC
       
   182 
       
   183     switch (aTimerId)
       
   184         {
       
   185         case EDeviceAttachmentTimer:
       
   186             {
       
   187             LOG("AttachmentTimer" );
       
   188             HandleL(EUsbWatcherErrDandlingCable, EUsbStateHostHandleDropping);
       
   189             break;
       
   190             }
       
   191         default:
       
   192             {
       
   193             LOG1( "Unknown timer id = %d", aTimerId );
       
   194             PANIC( EWrongTimerId);
       
   195             }
       
   196         }
       
   197     }
       
   198 
       
   199 // From VBus observer
       
   200 // ---------------------------------------------------------------------------
       
   201 // 
       
   202 // ---------------------------------------------------------------------------
       
   203 //
       
   204 void CUsbStateHostAInitiate::VBusUpL()
       
   205     {
       
   206     LOG_FUNC
       
   207     }
       
   208 
       
   209 // From Host Event notification observer
       
   210 // ---------------------------------------------------------------------------
       
   211 // 
       
   212 // ---------------------------------------------------------------------------
       
   213 //
       
   214 void CUsbStateHostAInitiate::DeviceAttachedL(TDeviceEventInformation aTdi)
       
   215     {
       
   216     LOG_FUNC
       
   217 
       
   218     iDeviceAttached = ETrue;
       
   219     iAttachmentTimer->Cancel();
       
   220 
       
   221     // check if an OTG device connected
       
   222     TOtgDescriptor otgDescriptor;
       
   223 
       
   224     // ignore all the errors, assume we connected to not otg
       
   225     TInt err = iWatcher.Usb().GetOtgDescriptor(aTdi.iDeviceId, otgDescriptor);
       
   226     LOG1("GetOtgDescriptor() err = %d", err );
       
   227 
       
   228     TBool hnpSupported(otgDescriptor.iAttributes & EUsbOtgHNPSupported);
       
   229     TBool srpSupported(otgDescriptor.iAttributes & EUsbOtgSRPSupported);
       
   230 
       
   231     // OTG device supports both hnp and srp
       
   232     if (hnpSupported && srpSupported)
       
   233         {
       
   234 
       
   235         HandleL(EUsbWatcherConnectedToOTG, EUsbStateHostHandleDropping);
       
   236         return;
       
   237         }
       
   238 
       
   239     switch (aTdi.iError)
       
   240         // error in attachement
       
   241         {
       
   242         case KErrNone:
       
   243             {
       
   244             break;
       
   245             }
       
   246         case KErrUsbDeviceDetachedDuringDriverLoading:
       
   247             {
       
   248             //do not show error, due to device is already detached
       
   249             // Device Detachment will not come after this (it was not attached yet)
       
   250             // therefore emulate detachment
       
   251             // content of aTdi is not important, due to detachment anyway
       
   252             DeviceDetachedL(aTdi);
       
   253             break;
       
   254             }
       
   255         case KErrBadPower:
       
   256             {
       
   257             LOG( "TooMuchPower" );
       
   258             HandleL(EUsbWatcherErrDeviceRequiresTooMuchPowerOnEnumeration,
       
   259                     EUsbStateHostDelayNotAttachedHandle);
       
   260             break;
       
   261             }
       
   262         default:
       
   263             {
       
   264             LOG1("AttachmentError aTdi.iError = %d" , aTdi.iError );
       
   265             HandleL(EUsbWatcherErrUnsupportedDevice,
       
   266                     EUsbStateHostHandleDropping);
       
   267             }
       
   268         }
       
   269     }
       
   270 
       
   271 // ---------------------------------------------------------------------------
       
   272 // 
       
   273 // ---------------------------------------------------------------------------
       
   274 //
       
   275 void CUsbStateHostAInitiate::DeviceDetachedL(TDeviceEventInformation aTdi)
       
   276     {
       
   277     LOG_FUNC
       
   278 
       
   279     // iDeviceAttached is used here to avoid one more state creation (init->attached->detached)
       
   280     // due to purpose of this state only to wait for drivers load
       
   281     if (!iDeviceAttached)
       
   282         {
       
   283         PANIC(EDeviceDetachedNotExpected)
       
   284         }
       
   285 
       
   286     // go out, and go in to the same state
       
   287     ChangeHostStateL( EUsbStateHostAInitiate);
       
   288     }
       
   289 
       
   290 // ---------------------------------------------------------------------------
       
   291 // 
       
   292 // ---------------------------------------------------------------------------
       
   293 //
       
   294 void CUsbStateHostAInitiate::DriverLoadSuccessL(TDeviceEventInformation)
       
   295     {
       
   296     LOG_FUNC
       
   297     ChangeHostStateL( EUsbStateHostAHost);
       
   298     }
       
   299 
       
   300 // ---------------------------------------------------------------------------
       
   301 // 
       
   302 // ---------------------------------------------------------------------------
       
   303 //
       
   304 void CUsbStateHostAInitiate::DriverLoadPartialSuccessL(
       
   305         TDeviceEventInformation aDei)
       
   306     {
       
   307     LOG_FUNC
       
   308 
       
   309     switch (aDei.iError)
       
   310         {
       
   311         case KErrUsbDeviceDetachedDuringDriverLoading:
       
   312             {
       
   313             //do not show warning, due to device is already detached
       
   314             break;
       
   315             }
       
   316         default:
       
   317             {
       
   318             iWatcher.NotifManager()->ShowNotifierL(KUsbUiNotifOtgWarning,
       
   319                     EUsbOtgPartiallySupportedDevice, NULL);
       
   320             }
       
   321         }
       
   322     ChangeHostStateL( EUsbStateHostAHost);
       
   323 
       
   324     }
       
   325 
       
   326 // ---------------------------------------------------------------------------
       
   327 // 
       
   328 // ---------------------------------------------------------------------------
       
   329 //
       
   330 void CUsbStateHostAInitiate::DriverLoadFailureL(TDeviceEventInformation aDei)
       
   331     {
       
   332     LOG_FUNC
       
   333 
       
   334     HandleL(EUsbWatcherErrDriversNotFound, EUsbStateHostDelayAttachedHandle);
       
   335     }