usbmgmt/usbmgr/usbman/chargingplugin/src/CUsbBatteryChargingPlugin.cpp
changeset 0 c9bc50fca66e
child 15 f92a4f87e424
equal deleted inserted replaced
-1:000000000000 0:c9bc50fca66e
       
     1 /*
       
     2 * Copyright (c) 2005-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 /** @file
       
    19 @internalComponent
       
    20 */
       
    21 
       
    22 #include "CUsbBatteryChargingPlugin.h"
       
    23 #include "chargingstates.h"
       
    24 #include <musbmanextensionpluginobserver.h>
       
    25 #include "cusbbatterycharginglicenseehooks.h"
       
    26 #include "reenumerator.h"
       
    27 #include <usb/usblogger.h>
       
    28 #include <e32property.h>
       
    29 #include <centralrepository.h>
       
    30 #include <usbotgdefs.h>
       
    31 
       
    32 #ifdef SYMBIAN_ENABLE_USB_OTG_HOST_PRIV          // For host OTG enabled charging plug-in 
       
    33 #include "idpinwatcher.h"
       
    34 #include "otgstatewatcher.h"
       
    35 #endif
       
    36 
       
    37 #include "vbuswatcher.h"
       
    38 #include <e32debug.h> 
       
    39 #include <e32def.h>
       
    40 
       
    41 static const TInt KUsbBatteryChargingConfigurationDescriptorCurrentOffset = 8; // see bMaxPower in section 9.6.3 of USB Spec 2.0
       
    42 static const TInt KUsbBatteryChargingCurrentRequestTimeout = 3000000; // 3 seconds
       
    43 
       
    44 #ifdef __FLOG_ACTIVE
       
    45 _LIT8(KLogComponent, "USBCHARGE");
       
    46 #endif
       
    47 
       
    48 /**
       
    49 Factory function.
       
    50 @return Ownership of a new CUsbBatteryChargingPlugin.
       
    51 */
       
    52 CUsbBatteryChargingPlugin* CUsbBatteryChargingPlugin::NewL(MUsbmanExtensionPluginObserver& aObserver)
       
    53     {
       
    54     CUsbBatteryChargingPlugin* self = new(ELeave) CUsbBatteryChargingPlugin(aObserver);
       
    55     CleanupStack::PushL(self);
       
    56     self->ConstructL();
       
    57     CleanupStack::Pop(self);
       
    58     return self;
       
    59     }
       
    60 
       
    61 /**
       
    62 Destructor.
       
    63 */
       
    64 CUsbBatteryChargingPlugin::~CUsbBatteryChargingPlugin()
       
    65     {
       
    66     LOGTEXT(KNullDesC8);
       
    67     LOGTEXT2(_L8(">>CUsbBatteryChargingPlugin::~CUsbBatteryChargingPlugin this = [0x%08x]"), this);
       
    68     
       
    69     iCurrentValues.Close();
       
    70     delete iDeviceReEnumerator;
       
    71     delete iDeviceStateTimer;
       
    72     delete iRepositoryNotifier;
       
    73     delete iLicenseeHooks;
       
    74     
       
    75 // For host OTG enabled charging plug-in
       
    76 #ifdef SYMBIAN_ENABLE_USB_OTG_HOST_PRIV
       
    77     delete iIdPinWatcher;
       
    78     delete iOtgStateWatcher;
       
    79 #endif
       
    80     
       
    81     delete iVBusWatcher;
       
    82     for (TInt index = 0; index < EPluginStateCount; index ++)
       
    83         {
       
    84         delete iPluginStates[index];
       
    85         iPluginStates[index] = NULL;
       
    86         }
       
    87     }
       
    88 
       
    89 /**
       
    90 Constructor.
       
    91 */
       
    92 CUsbBatteryChargingPlugin::CUsbBatteryChargingPlugin(MUsbmanExtensionPluginObserver& aObserver)
       
    93 :    CUsbmanExtensionPlugin(aObserver) , iLdd(Observer().DevUsbcClient())
       
    94     {
       
    95     }
       
    96 
       
    97 /**
       
    98 2nd-phase construction.
       
    99 */
       
   100 void CUsbBatteryChargingPlugin::ConstructL()
       
   101     {
       
   102     LOGTEXT(_L8(">>CUsbBatteryChargingPlugin::ConstructL"));
       
   103    
       
   104     // Create state objects
       
   105     iPluginStates[EPluginStateIdle] = 
       
   106         new (ELeave) TUsbBatteryChargingPluginStateIdle(*this);
       
   107     iPluginStates[EPluginStateCurrentNegotiating] = 
       
   108         new (ELeave) TUsbBatteryChargingPluginStateCurrentNegotiating(*this);
       
   109     iPluginStates[EPluginStateCharging] = 
       
   110         new (ELeave) TUsbBatteryChargingPluginStateCharging(*this);
       
   111     iPluginStates[EPluginStateNoValidCurrent] = 
       
   112         new (ELeave) TUsbBatteryChargingPluginStateNoValidCurrent(*this);
       
   113     iPluginStates[EPluginStateIdleNegotiated] = 
       
   114         new (ELeave) TUsbBatteryChargingPluginStateIdleNegotiated(*this);
       
   115     iPluginStates[EPluginStateUserDisabled] = 
       
   116         new (ELeave) TUsbBatteryChargingPluginStateUserDisabled(*this);
       
   117     iPluginStates[EPluginStateBEndedCableNotPresent] = 
       
   118         new (ELeave) TUsbBatteryChargingPluginStateBEndedCableNotPresent(*this);
       
   119 
       
   120     // Set initial state to idle
       
   121     SetState(EPluginStateIdle);
       
   122     
       
   123     TInt err = RProperty::Define(KPropertyUidUsbBatteryChargingCategory,
       
   124                       KPropertyUidUsbBatteryChargingAvailableCurrent,
       
   125                       RProperty::EInt,
       
   126                       ECapabilityReadDeviceData,
       
   127                       ECapabilityCommDD);
       
   128 
       
   129     if(err == KErrNone || err == KErrAlreadyExists)
       
   130         {
       
   131 
       
   132         err = RProperty::Define(KPropertyUidUsbBatteryChargingCategory,
       
   133                           KPropertyUidUsbBatteryChargingChargingCurrent,
       
   134                           RProperty::EInt,
       
   135                           ECapabilityReadDeviceData,
       
   136                           ECapabilityCommDD);
       
   137         }
       
   138     else
       
   139         {
       
   140         LEAVEL(err);
       
   141         }
       
   142 
       
   143     if(err == KErrNone || err == KErrAlreadyExists)
       
   144         {
       
   145         err = RProperty::Set(KPropertyUidUsbBatteryChargingCategory,
       
   146                                         KPropertyUidUsbBatteryChargingAvailableCurrent,
       
   147                                         0);
       
   148         }
       
   149     else
       
   150         {
       
   151         static_cast<void> (RProperty::Delete (
       
   152                 KPropertyUidUsbBatteryChargingCategory,
       
   153                 KPropertyUidUsbBatteryChargingAvailableCurrent ));
       
   154         LEAVEL(err);
       
   155         }
       
   156     
       
   157     err = RProperty::Set(KPropertyUidUsbBatteryChargingCategory,
       
   158                                         KPropertyUidUsbBatteryChargingChargingCurrent,
       
   159                                         0);
       
   160 
       
   161     if(err != KErrNone)
       
   162         {
       
   163         static_cast<void> (RProperty::Delete (
       
   164                 KPropertyUidUsbBatteryChargingCategory,
       
   165                 KPropertyUidUsbBatteryChargingAvailableCurrent ));
       
   166         static_cast<void> (RProperty::Delete (
       
   167                 KPropertyUidUsbBatteryChargingCategory,
       
   168                 KPropertyUidUsbBatteryChargingChargingCurrent ));
       
   169         LEAVEL(err);
       
   170         }
       
   171         
       
   172     iRepositoryNotifier = CUsbChargingRepositoryNotifier::NewL (*this,
       
   173             KUsbBatteryChargingCentralRepositoryUid,
       
   174             KUsbBatteryChargingKeyEnabledUserSetting );
       
   175     iDeviceStateTimer = CUsbChargingDeviceStateTimer::NewL(*this);
       
   176 
       
   177     iDeviceReEnumerator = CUsbChargingReEnumerator::NewL(iLdd);
       
   178 
       
   179     iPluginState = EPluginStateIdle;
       
   180     iPluginStateToRecovery = EPluginStateIdle;
       
   181     ReadCurrentRequestValuesL();
       
   182     iVBusWatcher = CVBusWatcher::NewL(this);
       
   183     iVBusState = iVBusWatcher->VBusState();
       
   184     
       
   185 // For host OTG enabled charging plug-in
       
   186 #ifdef SYMBIAN_ENABLE_USB_OTG_HOST_PRIV
       
   187     iOtgStateWatcher = COtgStateWatcher::NewL(this);
       
   188     iOtgState = iOtgStateWatcher->OtgState();
       
   189     iIdPinWatcher = CIdPinWatcher::NewL(this);
       
   190     TInt value = iIdPinWatcher->IdPinValue();
       
   191     iIdPinState = iIdPinWatcher->IdPinValue();
       
   192     if (iIdPinState == EUsbBatteryChargingIdPinBRole)
       
   193 #else
       
   194     if (ETrue)
       
   195 #endif
       
   196         {
       
   197 #if !defined(__WINS__) && !defined(__CHARGING_PLUGIN_TEST_CODE__)
       
   198         SetInitialConfigurationL();
       
   199 #endif
       
   200         }
       
   201     else
       
   202         {
       
   203         iPluginState = EPluginStateBEndedCableNotPresent;
       
   204         LOGTEXT2(_L8("PluginState => EPluginStateADevice(%d)"), iPluginState);
       
   205         }
       
   206 
       
   207     Observer().RegisterStateObserverL(*this);
       
   208 
       
   209     iLicenseeHooks = CUsbBatteryChargingLicenseeHooks::NewL();
       
   210     LOGTEXT(_L8("Created licensee specific hooks"));
       
   211     
       
   212     // Set initial recovery state to idle
       
   213     PushRecoverState(EPluginStateIdle);
       
   214     
       
   215     LOGTEXT(_L8("<<CUsbBatteryChargingPlugin::ConstructL"));
       
   216     }
       
   217 
       
   218 // For host OTG enabled charging plug-in
       
   219 TBool CUsbBatteryChargingPlugin::IsUsbChargingPossible()
       
   220     {
       
   221 #ifdef SYMBIAN_ENABLE_USB_OTG_HOST_PRIV
       
   222     // VBus is off, so there is no way to do charging
       
   223     if (0 == iVBusState)
       
   224         {
       
   225         return EFalse;
       
   226         }
       
   227     
       
   228     // 'A' end cable connected. I'm the power supplier, with no way to charge myself :-) 
       
   229     if (EUsbBatteryChargingIdPinARole == iIdPinState)
       
   230         {
       
   231         return EFalse;
       
   232         }
       
   233 
       
   234     // BHost charging is disabled
       
   235     if (EUsbOtgStateBHost == iOtgState)
       
   236         {
       
   237         return EFalse;
       
   238         }
       
   239         
       
   240     return ETrue;
       
   241 #else
       
   242     return ETrue;
       
   243 #endif
       
   244     }
       
   245 /**
       
   246  Set first power to request
       
   247 */
       
   248 void CUsbBatteryChargingPlugin::SetInitialConfigurationL()
       
   249     {
       
   250     LOGTEXT(_L8("Setting Initial Configuration"));
       
   251     if (iCurrentValues.Count() > 0)
       
   252         {
       
   253         TInt configDescriptorSize = 0;
       
   254         LEAVEIFERRORL(iLdd.GetConfigurationDescriptorSize(configDescriptorSize));
       
   255         HBufC8* configDescriptor = HBufC8::NewLC(configDescriptorSize);
       
   256         TPtr8 ptr(configDescriptor->Des());
       
   257 
       
   258         LOGTEXT2(_L8("Getting Configuration Descriptor (size = %d)"),configDescriptorSize);
       
   259         LEAVEIFERRORL(iLdd.GetConfigurationDescriptor(ptr));
       
   260 
       
   261 // For host OTG enabled charging plug-in
       
   262 #ifdef SYMBIAN_ENABLE_USB_OTG_HOST_PRIV
       
   263         // Get first power to put in configurator
       
   264         LOGTEXT(_L8("Checking IdPin state:"));
       
   265         if(iIdPinState == EUsbBatteryChargingIdPinBRole)
       
   266 #else
       
   267         if (ETrue)
       
   268 #endif
       
   269         	{
       
   270             if (iCurrentValues.Count() > 0)
       
   271                 {
       
   272                 iCurrentIndexRequested = 0;
       
   273                 iRequestedCurrentValue = iCurrentValues[iCurrentIndexRequested];
       
   274                 LOGTEXT2(_L8("IdPin state is 0, current set to: %d"), iRequestedCurrentValue);
       
   275                 }
       
   276             else
       
   277                 {
       
   278                 LOGTEXT(_L8("No vailable current found !"));
       
   279                 }
       
   280             }
       
   281         else
       
   282             {
       
   283             iRequestedCurrentValue = 0;
       
   284             LOGTEXT(_L8("IdPin state is 1, current set to 0"));
       
   285             }
       
   286 
       
   287         TUint oldCurrentValue = ptr[KUsbBatteryChargingConfigurationDescriptorCurrentOffset] << 1;
       
   288         ptr[KUsbBatteryChargingConfigurationDescriptorCurrentOffset] = (iRequestedCurrentValue >> 1);
       
   289 
       
   290         LOGTEXT(_L8("Setting Updated Configuration Descriptor"));
       
   291         LEAVEIFERRORL(iLdd.SetConfigurationDescriptor(ptr));
       
   292 
       
   293         CleanupStack::PopAndDestroy(configDescriptor); 
       
   294         }
       
   295     }
       
   296 
       
   297 
       
   298 TAny* CUsbBatteryChargingPlugin::GetInterface(TUid aUid)
       
   299     {
       
   300     LOGTEXT(KNullDesC8);
       
   301     LOGTEXT3(_L8(">>CUsbBatteryChargingPlugin::GetInterface this = [0x%08x], aUid = 0x%08x"), this, aUid);
       
   302     (void)aUid;
       
   303 
       
   304     TAny* ret = NULL;
       
   305 
       
   306     LOGTEXT2(_L8("<<CUsbBatteryChargingPlugin::GetInterface ret = [0x%08x]"), ret);
       
   307     return ret;
       
   308     }
       
   309 
       
   310 void CUsbBatteryChargingPlugin::Panic(TUsbBatteryChargingPanic aPanic)
       
   311     {
       
   312     LOGTEXT2(_L8("*** CUsbBatteryChargingPlugin::Panic(%d) ***"),aPanic);
       
   313     _LIT(KUsbChargingPanic,"USB Charging");
       
   314     User::Panic(KUsbChargingPanic, aPanic);
       
   315     }
       
   316 
       
   317 void CUsbBatteryChargingPlugin::UsbServiceStateChange(TInt /*aLastError*/, TUsbServiceState /*aOldState*/, TUsbServiceState /*aNewState*/)
       
   318     {
       
   319     // not used
       
   320     }
       
   321 
       
   322 void CUsbBatteryChargingPlugin::PushRecoverState(TUsbChargingPluginState aRecoverState)
       
   323     {
       
   324     LOG_FUNC
       
   325 
       
   326     if((aRecoverState == EPluginStateIdle)||
       
   327        (aRecoverState == EPluginStateIdleNegotiated) ||
       
   328        (aRecoverState == EPluginStateBEndedCableNotPresent) ||
       
   329        (aRecoverState == EPluginStateNoValidCurrent))
       
   330         {
       
   331         iPluginStateToRecovery = aRecoverState;
       
   332         }
       
   333     }
       
   334 
       
   335 TUsbChargingPluginState CUsbBatteryChargingPlugin::PopRecoverState()
       
   336     {
       
   337     LOG_FUNC
       
   338     
       
   339     SetState(iPluginStateToRecovery);
       
   340     
       
   341     iPluginStateToRecovery = EPluginStateIdle;
       
   342     
       
   343     return iPluginStateToRecovery;
       
   344     }
       
   345 
       
   346 TUsbChargingPluginState CUsbBatteryChargingPlugin::SetState(TUsbChargingPluginState aState)
       
   347     {
       
   348     LOG_FUNC
       
   349 
       
   350     switch (aState)
       
   351         {
       
   352         case EPluginStateIdle:
       
   353             {
       
   354             ResetPlugin();
       
   355             iCurrentState = iPluginStates[aState];
       
   356             }
       
   357             break;
       
   358         case EPluginStateCurrentNegotiating:
       
   359         case EPluginStateCharging:
       
   360         case EPluginStateNoValidCurrent:
       
   361         case EPluginStateIdleNegotiated:
       
   362         case EPluginStateUserDisabled:
       
   363         case EPluginStateBEndedCableNotPresent:
       
   364             {
       
   365             iPluginState = aState;
       
   366             iCurrentState = iPluginStates[aState];
       
   367             }
       
   368             break;
       
   369         default:
       
   370             // Should never happen ...
       
   371             iPluginState = EPluginStateIdle;
       
   372             iCurrentState = iPluginStates[EPluginStateIdle];
       
   373     
       
   374             LOGTEXT2(_L8(">>CUsbBatteryChargingPlugin::SetState: Invalid new state: aState = %d"), aState);
       
   375             
       
   376             Panic(EUsbBatteryChargingPanicUnexpectedPluginState);
       
   377         }
       
   378     iPluginState = aState;
       
   379     
       
   380     LOGTEXT2(_L8(">>CUsbBatteryChargingPlugin::SetState, New state: aState = %d"), aState);
       
   381     
       
   382     return iPluginState;
       
   383     }
       
   384 
       
   385 void CUsbBatteryChargingPlugin::NegotiateChargingCurrent()
       
   386     {
       
   387     LOG_FUNC
       
   388 
       
   389     LOGTEXT2(_L8(">>CUsbBatteryChargingPlugin::StartNegotiation,  iDeviceState = %d"), iDeviceState);
       
   390     TRAPD(result, NegotiateNextCurrentValueL());
       
   391     if(result == KErrNone)
       
   392         {
       
   393         SetState(EPluginStateCurrentNegotiating);
       
   394         }
       
   395     else
       
   396         {
       
   397         LOGTEXT2(_L8("Negotiation call failed, iVBusState = 1: result = %d"), result);
       
   398         }
       
   399     }
       
   400 
       
   401 void CUsbBatteryChargingPlugin::UsbDeviceStateChange(TInt aLastError,
       
   402         TUsbDeviceState aOldState, TUsbDeviceState aNewState)
       
   403     {
       
   404     LOG_FUNC
       
   405     
       
   406     iCurrentState->UsbDeviceStateChange(aLastError, aOldState, aNewState);
       
   407     }
       
   408 
       
   409 void CUsbBatteryChargingPlugin::HandleRepositoryValueChangedL(const TUid& aRepository, TUint aId, TInt aVal)
       
   410     {
       
   411     LOG_FUNC    
       
   412 
       
   413     iCurrentState->HandleRepositoryValueChangedL(aRepository, aId, aVal);
       
   414     }
       
   415 
       
   416 void CUsbBatteryChargingPlugin::DeviceStateTimeout()
       
   417     {
       
   418     LOG_FUNC
       
   419         
       
   420     iCurrentState->DeviceStateTimeout();
       
   421     }
       
   422 
       
   423 void CUsbBatteryChargingPlugin::NegotiateNextCurrentValueL()
       
   424     {
       
   425     LOG_FUNC
       
   426 
       
   427     iDeviceStateTimer->Cancel();
       
   428     TUint newCurrent = 0;
       
   429 
       
   430     if ((iPluginState == EPluginStateIdle)  && iCurrentValues.Count() > 0)
       
   431         {
       
   432         // i.e. we haven't requested anything yet, and there are some current values to try
       
   433         iCurrentIndexRequested  = 0;
       
   434         newCurrent = iCurrentValues[iCurrentIndexRequested];        
       
   435         }
       
   436     else if (iPluginState == EPluginStateCurrentNegotiating && ( iCurrentIndexRequested + 1) < iCurrentValues.Count())
       
   437         {
       
   438         // there are more current values left to try
       
   439         iCurrentIndexRequested++;
       
   440         newCurrent = iCurrentValues[iCurrentIndexRequested];    
       
   441         }
       
   442     else if(iRequestedCurrentValue != 0)
       
   443         {
       
   444         // There isn't a 0ma round set from the Repository source -> 10208DD7.txt
       
   445         // Just add it to make sure the device can be accepted by host    
       
   446         newCurrent = 0;
       
   447         }
       
   448     else
       
   449         {
       
   450         // Warning 0001: If you go here, something wrong happend, check it.
       
   451         __ASSERT_DEBUG(0,Panic(EUsbBatteryChargingPanicBadCharingCurrentNegotiation));
       
   452         }
       
   453     
       
   454     RequestCurrentL(newCurrent);
       
   455     iRequestedCurrentValue = newCurrent;
       
   456     iPluginState = EPluginStateCurrentNegotiating;
       
   457     }
       
   458 
       
   459 void CUsbBatteryChargingPlugin::ResetPlugin()
       
   460     {
       
   461     LOG_FUNC
       
   462     
       
   463     if((iPluginState != EPluginStateIdle))
       
   464         {
       
   465         iDeviceStateTimer->Cancel(); // doesn't matter if not running
       
   466         iPluginState = EPluginStateIdle;
       
   467         iPluginStateToRecovery = EPluginStateIdle;
       
   468         LOGTEXT2(_L8("PluginState => EPluginStateIdle(%d)"),iPluginState);
       
   469 
       
   470         iRequestedCurrentValue = 0;
       
   471         iCurrentIndexRequested = 0;
       
   472         iAvailableMilliAmps = 0;
       
   473         SetNegotiatedCurrent(0);
       
   474         TRAP_IGNORE(SetInitialConfigurationL());
       
   475         }
       
   476     }
       
   477 
       
   478 void CUsbBatteryChargingPlugin::RequestCurrentL(TUint aMilliAmps)
       
   479     {
       
   480     LOG_FUNC
       
   481     
       
   482     LOGTEXT2(_L8(">>CUsbBatteryChargingPlugin::RequestCurrent aMilliAmps = %d"), aMilliAmps);
       
   483 
       
   484     if((EPluginStateCurrentNegotiating == iPluginState) && (iRequestedCurrentValue != aMilliAmps))
       
   485         {
       
   486         TInt configDescriptorSize = 0;
       
   487         LEAVEIFERRORL(iLdd.GetConfigurationDescriptorSize(configDescriptorSize));
       
   488         HBufC8* configDescriptor = HBufC8::NewLC(configDescriptorSize);
       
   489         TPtr8 ptr(configDescriptor->Des());
       
   490 
       
   491         LOGTEXT2(_L8("Getting Configuration Descriptor (size = %d)"),configDescriptorSize);
       
   492         LEAVEIFERRORL(iLdd.GetConfigurationDescriptor(ptr));
       
   493 
       
   494         // set bMaxPower field. One unit = 2mA, so need to halve aMilliAmps.
       
   495         LOGTEXT3(_L8("Setting bMaxPower to %d mA ( = %d x 2mA units)"),aMilliAmps, (aMilliAmps >> 1));
       
   496         TUint oldCurrentValue = ptr[KUsbBatteryChargingConfigurationDescriptorCurrentOffset] << 1;
       
   497         LOGTEXT2(_L8("(old value was %d mA)"), oldCurrentValue);
       
   498 
       
   499         //since the device will force reEnumeration if the value is odd
       
   500         aMilliAmps = aMilliAmps & 0xFFFE;    
       
   501     
       
   502         // to negotiate a new current value, ReEnumerate is needed
       
   503         LOGTEXT(_L8("Forcing ReEnumeration"));
       
   504         ptr[KUsbBatteryChargingConfigurationDescriptorCurrentOffset] = (aMilliAmps >> 1);
       
   505         LOGTEXT(_L8("Setting Updated Configuration Descriptor"));
       
   506         LEAVEIFERRORL(iLdd.SetConfigurationDescriptor(ptr));
       
   507         LOGTEXT(_L8("Triggering Re-enumeration"));
       
   508         iDeviceReEnumerator->ReEnumerate();
       
   509         
       
   510         CleanupStack::PopAndDestroy(configDescriptor); // configDescriptor
       
   511         }    
       
   512     
       
   513     // Always issue a timer as a watchdog to monitor the request progress
       
   514     LOGTEXT2(_L8("Starting timer: %d"), User::NTickCount());
       
   515     iDeviceStateTimer->Cancel();
       
   516     iDeviceStateTimer->Start(TTimeIntervalMicroSeconds32(KUsbBatteryChargingCurrentRequestTimeout));
       
   517     }
       
   518 
       
   519 void CUsbBatteryChargingPlugin::ReadCurrentRequestValuesL()
       
   520     {
       
   521     LOG_FUNC
       
   522     
       
   523     CRepository* repository = CRepository::NewLC(KUsbBatteryChargingCentralRepositoryUid);
       
   524 
       
   525     TInt numberOfCurrents = 0;
       
   526     repository->Get(KUsbBatteryChargingKeyNumberOfCurrentValues, numberOfCurrents);
       
   527 
       
   528     TInt i = 0;
       
   529     for (i=0; i<numberOfCurrents; i++)
       
   530         {
       
   531         TInt value;
       
   532         repository->Get(KUsbBatteryChargingCurrentValuesOffset + i, value);
       
   533         iCurrentValues.Append(static_cast<TUint>(value));
       
   534         LOGTEXT3(_L8("CurrentValue %d = %dmA"),i,value);
       
   535         }
       
   536 
       
   537     CleanupStack::PopAndDestroy(repository);
       
   538     }
       
   539 
       
   540 void CUsbBatteryChargingPlugin::StartCharging(TUint aMilliAmps)
       
   541     {
       
   542     LOG_FUNC
       
   543     
       
   544     LOGTEXT2(_L8(">>CUsbBatteryChargingPlugin::StartCharging aMilliAmps = %d"), aMilliAmps);
       
   545     
       
   546     // do licensee specific functionality (if any)
       
   547     iLicenseeHooks->StartCharging(aMilliAmps);
       
   548 
       
   549 #ifdef __FLOG_ACTIVE
       
   550     TInt err = RProperty::Set(KPropertyUidUsbBatteryChargingCategory,
       
   551                 KPropertyUidUsbBatteryChargingChargingCurrent,
       
   552                             aMilliAmps);
       
   553     LOGTEXT3(_L8("Set P&S current = %dmA - err = %d"),aMilliAmps,err);
       
   554 #else
       
   555     (void)RProperty::Set(KPropertyUidUsbBatteryChargingCategory,
       
   556                 KPropertyUidUsbBatteryChargingChargingCurrent,
       
   557                                 aMilliAmps);
       
   558 #endif
       
   559 
       
   560     SetState(EPluginStateCharging);
       
   561     }
       
   562 
       
   563 void CUsbBatteryChargingPlugin::StopCharging()
       
   564     {
       
   565     LOG_FUNC
       
   566     
       
   567     // do licensee specific functionality (if any)
       
   568     iLicenseeHooks->StopCharging();
       
   569 
       
   570 #ifdef __FLOG_ACTIVE
       
   571     TInt err = RProperty::Set(KPropertyUidUsbBatteryChargingCategory,
       
   572                                     KPropertyUidUsbBatteryChargingChargingCurrent,
       
   573                                     0);
       
   574     LOGTEXT2(_L8("Set P&S current = 0mA - err = %d"),err);
       
   575 #else
       
   576     (void)RProperty::Set(KPropertyUidUsbBatteryChargingCategory,
       
   577                                     KPropertyUidUsbBatteryChargingChargingCurrent,
       
   578                                     0);
       
   579 #endif
       
   580     }
       
   581 
       
   582 void CUsbBatteryChargingPlugin::SetNegotiatedCurrent(TUint aMilliAmps)
       
   583     {
       
   584     LOG_FUNC
       
   585     
       
   586     LOGTEXT2(_L8(">>CUsbBatteryChargingPlugin::SetNegotiatedCurrent aMilliAmps = %d"), aMilliAmps);
       
   587 
       
   588     // Ignore errors - not much we can do if it fails
       
   589 #ifdef __FLOG_ACTIVE
       
   590     TInt err = RProperty::Set(KPropertyUidUsbBatteryChargingCategory,
       
   591                                     KPropertyUidUsbBatteryChargingAvailableCurrent,
       
   592                                     aMilliAmps);
       
   593     LOGTEXT3(_L8("Set P&S current = %dmA - err = %d"),aMilliAmps,err);
       
   594 #else
       
   595     (void)RProperty::Set(KPropertyUidUsbBatteryChargingCategory,
       
   596                                     KPropertyUidUsbBatteryChargingAvailableCurrent,
       
   597                                     aMilliAmps);
       
   598 #endif
       
   599     }
       
   600 
       
   601 
       
   602 #ifndef __FLOG_ACTIVE
       
   603 void CUsbBatteryChargingPlugin::LogStateText(TUsbDeviceState /*aState*/)
       
   604     {
       
   605     LOG_FUNC
       
   606     }
       
   607 #else
       
   608 void CUsbBatteryChargingPlugin::LogStateText(TUsbDeviceState aState)
       
   609     {
       
   610     LOG_FUNC
       
   611     
       
   612     switch (aState)
       
   613         {
       
   614         case EUsbDeviceStateUndefined:
       
   615             LOGTEXT(_L8(" ***** UNDEFINED *****"));
       
   616             break;
       
   617         case EUsbDeviceStateDefault:
       
   618             LOGTEXT(_L8(" ***** DEFAULT *****"));
       
   619             break;
       
   620         case EUsbDeviceStateAttached:
       
   621             LOGTEXT(_L8(" ***** ATTACHED *****"));
       
   622             break;
       
   623         case EUsbDeviceStatePowered:
       
   624             LOGTEXT(_L8(" ***** POWERED *****"));
       
   625             break;
       
   626         case EUsbDeviceStateConfigured:
       
   627             LOGTEXT(_L8(" ***** CONFIGURED *****"));
       
   628             break;
       
   629         case EUsbDeviceStateAddress:
       
   630             LOGTEXT(_L8(" ***** ADDRESS *****"));
       
   631             break;
       
   632         case EUsbDeviceStateSuspended:
       
   633             LOGTEXT(_L8(" ***** SUSPENDED *****"));
       
   634             break;
       
   635         default:
       
   636             break;
       
   637         }
       
   638     }
       
   639 #endif
       
   640 
       
   641 void CUsbBatteryChargingPlugin::MpsoVBusStateChanged(TInt aNewState)
       
   642     {
       
   643     LOG_FUNC
       
   644     
       
   645     iCurrentState->MpsoVBusStateChanged(aNewState);
       
   646     }
       
   647 
       
   648 
       
   649 // For host OTG enabled charging plug-in
       
   650 #ifdef SYMBIAN_ENABLE_USB_OTG_HOST_PRIV
       
   651 void CUsbBatteryChargingPlugin::MpsoIdPinStateChanged(TInt aValue)
       
   652     {
       
   653     LOG_FUNC
       
   654     
       
   655     iCurrentState->MpsoIdPinStateChanged(aValue);
       
   656     }
       
   657 
       
   658 void CUsbBatteryChargingPlugin::MpsoOtgStateChangedL(TUsbOtgState aNewState)
       
   659     {
       
   660     LOG_FUNC
       
   661 
       
   662     iCurrentState->MpsoOtgStateChangedL(aNewState);
       
   663     }
       
   664 #endif