usbmgmt/usbmgr/usbman/chargingplugin/src/chargingstates.cpp
changeset 0 c9bc50fca66e
child 29 59aa7d6e3e0f
equal deleted inserted replaced
-1:000000000000 0:c9bc50fca66e
       
     1 /*
       
     2 * Copyright (c) 2009 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:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 /** @file
       
    20 @internalComponent
       
    21 */
       
    22 
       
    23 #include "chargingstates.h"
       
    24 #include <usb/usblogger.h>
       
    25 #include "reenumerator.h"
       
    26 
       
    27 #ifdef __FLOG_ACTIVE
       
    28 _LIT8(KLogComponent, "USBCHARGEStates");
       
    29 #endif
       
    30 
       
    31 // Charging plugin base state
       
    32 
       
    33 // Empty virtual function implement to give a base of each state class. 
       
    34 // A concrete state class can overlap them according to actual demand.
       
    35 void TUsbBatteryChargingPluginStateBase::UsbServiceStateChange(TInt aLastError,
       
    36     TUsbServiceState aOldState, TUsbServiceState aNewState)
       
    37     {
       
    38     LOG_FUNC
       
    39     
       
    40     (void)aLastError;
       
    41     (void)aOldState;
       
    42     (void)aNewState;
       
    43     
       
    44     // Not use
       
    45     }
       
    46 
       
    47 void TUsbBatteryChargingPluginStateBase::UsbDeviceStateChange(TInt aLastError,
       
    48     TUsbDeviceState aOldState, TUsbDeviceState aNewState)
       
    49     {
       
    50     LOG_FUNC
       
    51     
       
    52     (void)aLastError;
       
    53     (void)aOldState;
       
    54     (void)aNewState;
       
    55     }
       
    56 
       
    57 void TUsbBatteryChargingPluginStateBase::HandleRepositoryValueChangedL(
       
    58     const TUid& aRepository, TUint aId, TInt aVal)
       
    59     {
       
    60     LOG_FUNC
       
    61     
       
    62     LOGTEXT4(_L8(">>TUsbBatteryChargingPluginStateBase::HandleRepositoryValueChangedL aRepository = 0x%08x, aId = %d, aVal = %d"), aRepository, aId, aVal);
       
    63     LOGTEXT3(_L8("Plugin State = %d, Device State = %d"), iParent.iPluginState, iParent.iDeviceState);
       
    64     
       
    65     if ((aRepository == KUsbBatteryChargingCentralRepositoryUid) &&
       
    66             (aId == KUsbBatteryChargingKeyEnabledUserSetting))
       
    67         {
       
    68         iParent.iUserSetting = (TUsbBatteryChargingUserSetting)aVal;
       
    69 
       
    70         if (iParent.iUserSetting == EUsbBatteryChargingUserSettingDisabled)
       
    71             {
       
    72             if(iParent.iPluginState == EPluginStateCharging)
       
    73                 {
       
    74                 iParent.StopCharging();
       
    75                 
       
    76                 // Push EPluginStateIdleNegotiated state to recover state
       
    77                 iParent.PushRecoverState(EPluginStateIdleNegotiated);
       
    78                 }
       
    79             else
       
    80                 {
       
    81                 // Push current state to recover state
       
    82                 iParent.PushRecoverState(iParent.iPluginState);
       
    83                 }
       
    84             
       
    85             iParent.SetState(EPluginStateUserDisabled);
       
    86             }
       
    87         }
       
    88     }
       
    89     
       
    90 void TUsbBatteryChargingPluginStateBase::DeviceStateTimeout()
       
    91     {
       
    92     LOG_FUNC
       
    93     LOGTEXT4(_L8("Time: %d Plugin State = %d, Device State = %d"), User::NTickCount(), iParent.iPluginState, iParent.iDeviceState);
       
    94     
       
    95     iParent.iDeviceReEnumerator->Cancel(); // cancel re-enumeration AO
       
    96     
       
    97     if(iParent.iUserSetting) // User allow charging already and not in negotiating process...
       
    98         {
       
    99         // Should not happen !!! Otherwise, something wrong!!!
       
   100         iParent.SetState(EPluginStateIdle);
       
   101         }
       
   102     }
       
   103 
       
   104 // For host OTG enabled charging plug-in
       
   105 #ifdef SYMBIAN_ENABLE_USB_OTG_HOST_PRIV
       
   106 void TUsbBatteryChargingPluginStateBase::MpsoIdPinStateChanged(TInt aValue)
       
   107     {
       
   108     LOG_FUNC
       
   109     
       
   110     LOGTEXT2(_L8("IdPinState changed => %d"), aValue);
       
   111     
       
   112     // Disable charging here when IdPin is present
       
   113     // When IdPin disappears (i.e. the phone becomes B-Device), all necessary step are performed 
       
   114     // in UsbDeviceStateChange() method
       
   115 
       
   116     iParent.iIdPinState = aValue;
       
   117     
       
   118     // For all other states besides EPluginStateUserDisabled
       
   119     switch(aValue)
       
   120         {
       
   121         case EUsbBatteryChargingIdPinARole:
       
   122             if (iParent.iPluginState == EPluginStateCharging)
       
   123                 {
       
   124                 iParent.StopCharging();
       
   125                 }
       
   126 
       
   127             TRAP_IGNORE(iParent.SetInitialConfigurationL());
       
   128             iParent.SetState(EPluginStateBEndedCableNotPresent);
       
   129             
       
   130             return;
       
   131 
       
   132         case EUsbBatteryChargingIdPinBRole:
       
   133             iParent.SetState(EPluginStateIdle);
       
   134             break;
       
   135 
       
   136         default:
       
   137             if (iParent.iPluginState == EPluginStateCharging)
       
   138                 {
       
   139                 iParent.StopCharging();
       
   140                 }     
       
   141             iParent.SetState(EPluginStateIdle);
       
   142             break;
       
   143         }
       
   144     }
       
   145 
       
   146 void TUsbBatteryChargingPluginStateBase::MpsoOtgStateChangedL(TUsbOtgState aNewState)
       
   147     {
       
   148     LOG_FUNC
       
   149     
       
   150     iParent.iOtgState = aNewState;
       
   151     
       
   152     // Not use currently
       
   153     }
       
   154 #endif
       
   155 
       
   156 void TUsbBatteryChargingPluginStateBase::MpsoVBusStateChanged(TInt aNewState)
       
   157     {
       
   158     LOG_FUNC
       
   159     
       
   160     if (aNewState == iParent.iVBusState)
       
   161         {
       
   162         LOGTEXT2(_L8("Receive VBus State Change notification without any state change: aNewState = %d"), aNewState);
       
   163         return;//should not happen??
       
   164         }
       
   165 
       
   166     LOGTEXT3(_L8("VBusState changed from %d to %d"), iParent.iVBusState, aNewState);
       
   167     
       
   168     iParent.iVBusState = aNewState;
       
   169     if (aNewState == 0) // VBus drop down - we have disconnected from host
       
   170         {
       
   171         if (iParent.iPluginState == EPluginStateCharging)
       
   172             {
       
   173             iParent.StopCharging();
       
   174             }
       
   175         iParent.SetState(EPluginStateIdle);
       
   176         }
       
   177     
       
   178     // The handling of VBus on will be down in DeviceStateChanged implicitly
       
   179     }
       
   180 
       
   181 
       
   182 TUsbBatteryChargingPluginStateBase::TUsbBatteryChargingPluginStateBase (
       
   183         CUsbBatteryChargingPlugin& aParentStateMachine ): 
       
   184         iParent(aParentStateMachine)
       
   185     {
       
   186     LOG_FUNC
       
   187     }
       
   188 
       
   189         
       
   190 // Charging plugin idle state
       
   191 
       
   192 TUsbBatteryChargingPluginStateIdle::TUsbBatteryChargingPluginStateIdle (
       
   193         CUsbBatteryChargingPlugin& aParentStateMachine ) :
       
   194     TUsbBatteryChargingPluginStateBase(aParentStateMachine)
       
   195     {
       
   196     LOG_FUNC
       
   197     };
       
   198 
       
   199 void TUsbBatteryChargingPluginStateIdle::UsbDeviceStateChange(
       
   200         TInt aLastError, TUsbDeviceState aOldState, TUsbDeviceState aNewState)
       
   201     {
       
   202     LOG_FUNC
       
   203 
       
   204     LOGTEXT4(_L8(">>TUsbBatteryChargingPluginStateIdle::UsbDeviceStateChange LastError = %d, aOldState = %d, aNewState = %d"), aLastError, aOldState, aNewState);
       
   205     (void)aLastError;
       
   206     (void)aOldState;
       
   207     iParent.iDeviceState = aNewState;
       
   208     iParent.LogStateText(aNewState);
       
   209     
       
   210     switch (iParent.iDeviceState)
       
   211         {
       
   212         case EUsbDeviceStateAddress:
       
   213             {
       
   214             if (iParent.iUserSetting)
       
   215                 {
       
   216                 if (iParent.IsUsbChargingPossible())
       
   217                     {
       
   218                     iParent.NegotiateChargingCurrent();
       
   219                     }
       
   220                 }
       
   221             else
       
   222                 {
       
   223                 iParent.SetState(EPluginStateUserDisabled);
       
   224                 }
       
   225             }
       
   226             break;
       
   227         }
       
   228     }
       
   229     
       
   230     
       
   231 // Charging plugin current negotiating state
       
   232 
       
   233 TUsbBatteryChargingPluginStateCurrentNegotiating::TUsbBatteryChargingPluginStateCurrentNegotiating (
       
   234         CUsbBatteryChargingPlugin& aParentStateMachine ) :
       
   235     TUsbBatteryChargingPluginStateBase(aParentStateMachine)
       
   236     {
       
   237     LOG_FUNC
       
   238     };
       
   239     
       
   240 void TUsbBatteryChargingPluginStateCurrentNegotiating::UsbDeviceStateChange(
       
   241         TInt aLastError, TUsbDeviceState aOldState, TUsbDeviceState aNewState)
       
   242     {
       
   243     LOG_FUNC
       
   244 
       
   245     LOGTEXT4(_L8(">>TUsbBatteryChargingPluginStateCurrentNegotiating::UsbDeviceStateChange LastError = %d, aOldState = %d, aNewState = %d"), aLastError, aOldState, aNewState);
       
   246     (void)aLastError;
       
   247     (void)aOldState;
       
   248     iParent.iDeviceState = aNewState;
       
   249     iParent.LogStateText(aNewState);
       
   250     
       
   251     switch(iParent.iDeviceState)
       
   252         {
       
   253         case EUsbDeviceStateConfigured:
       
   254             if (iParent.IsUsbChargingPossible())
       
   255                 {
       
   256                 iParent.iDeviceStateTimer->Cancel();
       
   257                 
       
   258                 LOGTEXT2(_L8("iParent.iAvailableMilliAmps = %d"),iParent.iAvailableMilliAmps);
       
   259                 iParent.iAvailableMilliAmps = iParent.iRequestedCurrentValue;
       
   260                 
       
   261                 if(0 != iParent.iRequestedCurrentValue)
       
   262                     {
       
   263                     // A non-zero value was accepted by host, charging 
       
   264                     // can be performed now.
       
   265                     iParent.StartCharging(iParent.iAvailableMilliAmps);                     
       
   266                     LOGTEXT2(_L8("PluginState => EPluginStateCharging(%d)"),iParent.iPluginState);
       
   267                     iParent.SetNegotiatedCurrent(iParent.iAvailableMilliAmps);
       
   268                     }
       
   269                 else
       
   270                     {
       
   271                     // Host can only accept 0 charging current
       
   272                     // No way to do charging
       
   273                     iParent.SetState(EPluginStateNoValidCurrent);
       
   274                     LOGTEXT2(_L8("No more current value to try, iPluginState turned to %d"), iParent.iPluginState);
       
   275                     }
       
   276                 }
       
   277             
       
   278             break;
       
   279         
       
   280         // If no configured received, there must be a timeout
       
   281         // caught by the iDeviceStateTimer, and it will try next value or send state to  
       
   282         // EPluginStateNoValidCurrent, so don't worry that we omit something important :-)
       
   283             
       
   284             
       
   285         default:
       
   286             break;
       
   287         }
       
   288     }
       
   289 
       
   290 void TUsbBatteryChargingPluginStateCurrentNegotiating::DeviceStateTimeout()
       
   291     {
       
   292     LOG_FUNC
       
   293     LOGTEXT4(_L8("Time: %d Plugin State = %d, Device State = %d"), User::NTickCount(), iParent.iPluginState, iParent.iDeviceState);
       
   294     
       
   295     iParent.iDeviceReEnumerator->Cancel(); // cancel re-enumeration AO
       
   296     
       
   297     if(iParent.iRequestedCurrentValue != 0)
       
   298         {
       
   299         // If there are more value to try ...
       
   300         iParent.NegotiateChargingCurrent();
       
   301         }
       
   302     else
       
   303         {
       
   304         // The Host doesn't accept 0ma power request?
       
   305         // Assume it will never happens.
       
   306         iParent.SetState(EPluginStateNoValidCurrent);
       
   307         }
       
   308     }
       
   309 
       
   310 
       
   311 // Charging plugin charing state
       
   312 
       
   313     
       
   314 TUsbBatteryChargingPluginStateCharging::TUsbBatteryChargingPluginStateCharging (
       
   315         CUsbBatteryChargingPlugin& aParentStateMachine ) :
       
   316     TUsbBatteryChargingPluginStateBase(aParentStateMachine)
       
   317     {
       
   318     LOG_FUNC
       
   319     }
       
   320 
       
   321 void TUsbBatteryChargingPluginStateCharging::UsbDeviceStateChange(
       
   322         TInt aLastError, TUsbDeviceState aOldState, TUsbDeviceState aNewState)
       
   323     {
       
   324     LOG_FUNC
       
   325 
       
   326     LOGTEXT4(_L8(">>TUsbBatteryChargingPluginStateCharging::UsbDeviceStateChange LastError = %d, aOldState = %d, aNewState = %d"), aLastError, aOldState, aNewState);
       
   327     (void)aLastError;
       
   328     (void)aOldState;
       
   329     iParent.iDeviceState = aNewState;
       
   330     iParent.LogStateText(aNewState);
       
   331 
       
   332     switch(iParent.iDeviceState)
       
   333         {
       
   334         case EUsbDeviceStateConfigured:
       
   335             break; // I think this can not happen at all but in case ...
       
   336             
       
   337         case EUsbDeviceStateAttached:
       
   338         case EUsbDeviceStatePowered:
       
   339         case EUsbDeviceStateDefault:
       
   340         case EUsbDeviceStateAddress:
       
   341         case EUsbDeviceStateSuspended:
       
   342             {
       
   343             // wait until configured
       
   344             iParent.StopCharging();
       
   345             iParent.SetState(EPluginStateIdleNegotiated);
       
   346             }
       
   347             break;
       
   348                             
       
   349         default:
       
   350             break;
       
   351         }
       
   352     }
       
   353 
       
   354 // Charging plugin negotiated fail state
       
   355     
       
   356     
       
   357 TUsbBatteryChargingPluginStateNoValidCurrent::TUsbBatteryChargingPluginStateNoValidCurrent (
       
   358         CUsbBatteryChargingPlugin& aParentStateMachine ) :
       
   359     TUsbBatteryChargingPluginStateBase(aParentStateMachine)
       
   360     {
       
   361     LOG_FUNC
       
   362     };
       
   363 
       
   364     
       
   365 // Charging plugin idle negotiated state
       
   366 
       
   367 TUsbBatteryChargingPluginStateIdleNegotiated::TUsbBatteryChargingPluginStateIdleNegotiated (
       
   368         CUsbBatteryChargingPlugin& aParentStateMachine ) :
       
   369     TUsbBatteryChargingPluginStateBase(aParentStateMachine)
       
   370     {
       
   371     LOG_FUNC
       
   372     };
       
   373 
       
   374 void TUsbBatteryChargingPluginStateIdleNegotiated::UsbDeviceStateChange(
       
   375         TInt aLastError, TUsbDeviceState aOldState, TUsbDeviceState aNewState)
       
   376     {
       
   377     LOG_FUNC
       
   378 
       
   379     LOGTEXT4(_L8(">>TUsbBatteryChargingPluginStateIdleNegotiated::UsbDeviceStateChange LastError = %d, aOldState = %d, aNewState = %d"), aLastError, aOldState, aNewState);
       
   380     (void)aLastError;
       
   381     (void)aOldState;
       
   382     iParent.iDeviceState = aNewState;
       
   383     iParent.LogStateText(aNewState);
       
   384 
       
   385     switch(iParent.iDeviceState)
       
   386         {
       
   387         case EUsbDeviceStateConfigured:
       
   388             {
       
   389             // wait until configured
       
   390             if (iParent.IsUsbChargingPossible())
       
   391             	{
       
   392                 iParent.StartCharging(iParent.iAvailableMilliAmps);
       
   393                 }
       
   394             }
       
   395             break;
       
   396 
       
   397         default:
       
   398             break;
       
   399         }
       
   400     }
       
   401  
       
   402 // Charging plugin user disabled state
       
   403     
       
   404 TUsbBatteryChargingPluginStateUserDisabled::TUsbBatteryChargingPluginStateUserDisabled (
       
   405         CUsbBatteryChargingPlugin& aParentStateMachine ) :
       
   406     TUsbBatteryChargingPluginStateBase(aParentStateMachine)
       
   407     {
       
   408     LOG_FUNC
       
   409     };
       
   410 
       
   411 
       
   412 void TUsbBatteryChargingPluginStateUserDisabled::UsbDeviceStateChange(
       
   413         TInt aLastError, TUsbDeviceState aOldState, TUsbDeviceState aNewState)
       
   414     {
       
   415     LOG_FUNC
       
   416 
       
   417     LOGTEXT4(_L8(">>TUsbBatteryChargingPluginStateUserDisabled::UsbDeviceStateChange LastError = %d, aOldState = %d, aNewState = %d"), aLastError, aOldState, aNewState);
       
   418     (void)aLastError;
       
   419     (void)aOldState;
       
   420     iParent.iDeviceState = aNewState;
       
   421     iParent.LogStateText(aNewState);
       
   422     }
       
   423 
       
   424 void TUsbBatteryChargingPluginStateUserDisabled::HandleRepositoryValueChangedL(
       
   425     const TUid& aRepository, TUint aId, TInt aVal)
       
   426     {
       
   427     LOG_FUNC
       
   428     
       
   429     LOGTEXT4(_L8(">>TUsbBatteryChargingPluginStateUserDisabled::HandleRepositoryValueChangedL aRepository = 0x%08x, aId = %d, aVal = %d"), aRepository, aId, aVal);
       
   430     LOGTEXT3(_L8("Plugin State = %d, Device State = %d"), iParent.iPluginState, iParent.iDeviceState);
       
   431     
       
   432     if ((aRepository == KUsbBatteryChargingCentralRepositoryUid) &&
       
   433             (aId == KUsbBatteryChargingKeyEnabledUserSetting))
       
   434         {
       
   435         iParent.iUserSetting = (TUsbBatteryChargingUserSetting)aVal;
       
   436 
       
   437         if (iParent.iUserSetting == EUsbBatteryChargingUserSettingEnabled)
       
   438             {
       
   439             // EPluginStateUserDisabled must be the current state
       
   440             iParent.PopRecoverState();
       
   441             if ((iParent.iPluginState == EPluginStateIdleNegotiated)
       
   442                     && (iParent.iDeviceState == EUsbDeviceStateConfigured))
       
   443                 {
       
   444                 iParent.StartCharging(iParent.iAvailableMilliAmps); // Go to charing state implicitly
       
   445                 }
       
   446             LOGTEXT2(_L8("PluginState => %d"), iParent.iPluginState);
       
   447             }
       
   448         }
       
   449     }
       
   450 
       
   451 // For host OTG enabled charging plug-in
       
   452 #ifdef SYMBIAN_ENABLE_USB_OTG_HOST_PRIV
       
   453 void TUsbBatteryChargingPluginStateUserDisabled::MpsoIdPinStateChanged(TInt aValue)
       
   454     {
       
   455     LOG_FUNC
       
   456     
       
   457     LOGTEXT2(_L8("IdPinState changed => %d"), aValue);
       
   458     
       
   459     // Disable charging here when IdPin is present
       
   460     // When IdPin disappears (i.e. the phone becomes B-Device), all necessary step are performed 
       
   461     // in UsbDeviceStateChange() method
       
   462 
       
   463     iParent.iIdPinState = aValue;
       
   464     
       
   465     switch(aValue)
       
   466         {
       
   467         case EUsbBatteryChargingIdPinARole:
       
   468             TRAP_IGNORE(iParent.SetInitialConfigurationL());
       
   469             iParent.PushRecoverState(EPluginStateBEndedCableNotPresent);
       
   470             
       
   471             return;
       
   472 
       
   473         case EUsbBatteryChargingIdPinBRole:
       
   474             iParent.PushRecoverState(EPluginStateIdle);
       
   475             break;
       
   476 
       
   477         default:     
       
   478             iParent.SetState(EPluginStateIdle);
       
   479             break;
       
   480         }
       
   481     }
       
   482 
       
   483 #endif     
       
   484  
       
   485 void TUsbBatteryChargingPluginStateUserDisabled::MpsoVBusStateChanged(TInt aNewState)
       
   486     {
       
   487     LOG_FUNC
       
   488     
       
   489     if (aNewState == iParent.iVBusState)
       
   490         {
       
   491         LOGTEXT2(_L8("Receive VBus State Change notification without any state change: aNewState = %d"), aNewState);
       
   492         return;
       
   493         }
       
   494 
       
   495     LOGTEXT3(_L8("VBusState changed from %d to %d"), iParent.iVBusState, aNewState);
       
   496     
       
   497     iParent.iVBusState = aNewState;
       
   498     if (aNewState == 0) // VBus drop down - we have disconnected from host
       
   499         {
       
   500         iParent.iRequestedCurrentValue = 0;
       
   501         iParent.iCurrentIndexRequested = 0;
       
   502         iParent.iAvailableMilliAmps = 0;
       
   503         
       
   504         iParent.iPluginStateToRecovery = EPluginStateIdle;
       
   505         }
       
   506     
       
   507     // The handling of VBus on will be down in DeviceStateChanged implicitly
       
   508     }
       
   509 
       
   510 
       
   511 // Charging plugin A-role state
       
   512     
       
   513 TUsbBatteryChargingPluginStateBEndedCableNotPresent::TUsbBatteryChargingPluginStateBEndedCableNotPresent (
       
   514         CUsbBatteryChargingPlugin& aParentStateMachine ) :
       
   515     TUsbBatteryChargingPluginStateBase(aParentStateMachine)
       
   516     {
       
   517     LOG_FUNC
       
   518     };
       
   519     
       
   520     
       
   521