usbuis/usbui/USBClassChangeUIPlugin/src/usbotghoststatewatcher.cpp
branchRCL_3
changeset 80 e02eb84a14d2
parent 0 1e05558e2206
equal deleted inserted replaced
79:25fce757be94 80:e02eb84a14d2
       
     1 /*
       
     2 * Copyright (c) 2007 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:  Device state watcher class.
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <e32base.h>
       
    21 #include <e32property.h>
       
    22 #include <UsbWatcherInternalPSKeys.h>
       
    23 
       
    24 #include "usbotghoststatewatcher.h"
       
    25 #include "USBClassChangeUIPluginDebug.h"
       
    26 
       
    27 // --------------------------------------------------------------------------
       
    28 // Two-phased constructor. Uses existing usb manager session.
       
    29 // --------------------------------------------------------------------------
       
    30 CUSBOtgHostStateWatcher* 
       
    31 CUSBOtgHostStateWatcher::NewL(MUSBOtgHostStateObserver& aObserver, RUsb& aUsbMan)
       
    32     {
       
    33     FLOG(_L("[CUSBClassChangeUIPlugin]\t CUSBOtgHostStateWatcher:NewL"));
       
    34     
       
    35     CUSBOtgHostStateWatcher* self = new(ELeave)CUSBOtgHostStateWatcher(aObserver, aUsbMan);
       
    36     CleanupStack::PushL(self);
       
    37     self->ConstructL();
       
    38     CleanupStack::Pop(self);
       
    39     return self;
       
    40     }
       
    41 
       
    42 // --------------------------------------------------------------------------
       
    43 // C++ destructor
       
    44 // --------------------------------------------------------------------------
       
    45 CUSBOtgHostStateWatcher::~CUSBOtgHostStateWatcher()
       
    46     {
       
    47     FLOG(_L("[CUSBClassChangeUIPlugin]\t CUSBOtgHostStateWatcher:~CUSBOtgHostStateWatcher"));
       
    48 
       
    49     delete iOtgIdPinWatcher;
       
    50     delete iHostEventWatcher;
       
    51     iPeripheral.Close();
       
    52     }
       
    53 
       
    54 // --------------------------------------------------------------------------
       
    55 // Get Id pin state
       
    56 // --------------------------------------------------------------------------
       
    57 TInt CUSBOtgHostStateWatcher::IsIdPinOn(TBool& aIsIdPinOn)
       
    58     {
       
    59     return iOtgIdPinWatcher->IsIdPinOn(aIsIdPinOn);
       
    60     }
       
    61 
       
    62 // --------------------------------------------------------------------------
       
    63 // Check wheather a pheripheral device is connected or not. 
       
    64 // --------------------------------------------------------------------------
       
    65 TInt CUSBOtgHostStateWatcher::IsPeripheralConnected(TBool &aIsConnected)
       
    66     {
       
    67     aIsConnected = EFalse;
       
    68 
       
    69     TInt val(0);
       
    70     TInt err = iPeripheral.Get(val);
       
    71 
       
    72     if (KErrNone != err)
       
    73         {
       
    74         FTRACE(FPrint(_L("[CUSBClassChangeUIPlugin]\t CUSBOtgHostStateWatcher::IsPeripheralConnected - err=%d"), err));
       
    75         return err;
       
    76         }
       
    77     
       
    78     FTRACE(FPrint(_L("[CUSBClassChangeUIPlugin]\t CUSBOtgHostStateWatcher::IsPeripheralConnected - val=%d"), val));
       
    79     aIsConnected = (TBool)val;
       
    80     
       
    81     return KErrNone;
       
    82     }
       
    83 
       
    84 // --------------------------------------------------------------------------
       
    85 // C++ constructor
       
    86 // --------------------------------------------------------------------------
       
    87 CUSBOtgHostStateWatcher::CUSBOtgHostStateWatcher(
       
    88         MUSBOtgHostStateObserver& aObserver, RUsb& aUsbMan) 
       
    89     : iUsbMan(aUsbMan), iObserver(aObserver)
       
    90     {
       
    91     }
       
    92 
       
    93 // --------------------------------------------------------------------------
       
    94 // Symbian 2nd phase constructor. 
       
    95 // --------------------------------------------------------------------------
       
    96 void CUSBOtgHostStateWatcher::ConstructL()
       
    97     {
       
    98     iOtgIdPinWatcher = CUSBOtgIdPinStateWatcher::NewL(*this);
       
    99     iHostEventWatcher = CUSBHostEventNotifWatcher::NewL(*this);
       
   100     TInt ret = iPeripheral.Attach(KPSUidUsbWatcher, KUsbWatcherIsPeripheralConnected);
       
   101     if (ret != KErrNone)
       
   102         {
       
   103         FTRACE(FPrint(_L("[CUSBClassChangeUIPlugin]\t CUSBOtgHostStateWatcher::ConstructL - iPeripheral.Attach err=%d"), ret));
       
   104         User::Leave(ret);
       
   105         }    
       
   106     }
       
   107 
       
   108 
       
   109 // --------------------------------------------------------------------------
       
   110 // Two-phased constructor.
       
   111 // --------------------------------------------------------------------------
       
   112 CUSBOtgHostStateWatcher::CUSBOtgIdPinStateWatcher* 
       
   113 CUSBOtgHostStateWatcher::CUSBOtgIdPinStateWatcher::NewL(CUSBOtgHostStateWatcher& aOtgHostStateWatcher)
       
   114     {
       
   115     FLOG(_L("[CUSBClassChangeUIPlugin]\t CUSBOtgIdPinStateWatcher::NewL"));
       
   116 
       
   117     CUSBOtgIdPinStateWatcher* self = new(ELeave)CUSBOtgIdPinStateWatcher(aOtgHostStateWatcher);
       
   118     CleanupStack::PushL(self);
       
   119     self->ConstructL();
       
   120     CleanupStack::Pop(self);
       
   121     return self;
       
   122     }
       
   123 
       
   124 // --------------------------------------------------------------------------
       
   125 // C++ destructor
       
   126 // --------------------------------------------------------------------------
       
   127 CUSBOtgHostStateWatcher::CUSBOtgIdPinStateWatcher::~CUSBOtgIdPinStateWatcher()
       
   128     {
       
   129     FLOG(_L("[CUSBClassChangeUIPlugin]\t CUSBOtgIdPinStateWatcher::~CUSBOtgIdPinStateWatcher"));
       
   130 
       
   131     Cancel();    
       
   132     iIdPin.Close();
       
   133     }
       
   134 
       
   135 // --------------------------------------------------------------------------
       
   136 // Get Id pin state
       
   137 // --------------------------------------------------------------------------
       
   138 TInt CUSBOtgHostStateWatcher::CUSBOtgIdPinStateWatcher::IsIdPinOn(TBool& aIsIdPinOn)
       
   139     {
       
   140     FLOG(_L("[CUSBClassChangeUIPlugin]\t CUSBOtgIdPinStateWatcher::IsIdPinOn" ));
       
   141 
       
   142     TInt val(0);
       
   143     TInt err = iIdPin.Get(val);
       
   144 
       
   145     if (KErrNone != err)
       
   146         {
       
   147         FTRACE(FPrint(_L("[CUSBClassChangeUIPlugin]\t CUSBOtgIdPinStateWatcher::IsIdPinOn - err=%d"), err));
       
   148         return err;
       
   149         }
       
   150 
       
   151     FTRACE(FPrint(_L( "[CUSBClassChangeUIPlugin]\t CUSBOtgIdPinStateWatcher::IsIdPinOn=%d" ), val ));
       
   152 
       
   153     // not found in docs clear definition of this property. Verification is needed   
       
   154     aIsIdPinOn = (0 == val ? EFalse : ETrue);
       
   155     
       
   156     return KErrNone;
       
   157     }
       
   158 
       
   159 // --------------------------------------------------------------------------
       
   160 // The Id pin state has changed.
       
   161 // --------------------------------------------------------------------------
       
   162 void CUSBOtgHostStateWatcher::CUSBOtgIdPinStateWatcher::RunL()
       
   163     {
       
   164     FLOG(_L("[CUSBClassChangeUIPlugin]\t CUSBOtgIdPinStateWatcher::RunL"));
       
   165 
       
   166     if (iStatus.Int() != KErrNone)
       
   167         {
       
   168         FTRACE(FPrint(_L("[CUSBClassChangeUIPlugin]\t CUSBOtgIdPinStateWatcher::RunL - iStatus=%d"), iStatus.Int()));
       
   169         User::Leave(iStatus.Int());
       
   170         }    
       
   171 
       
   172     // re-issue request first
       
   173     iIdPin.Subscribe(iStatus);
       
   174     SetActive();
       
   175 
       
   176     TBool isIdPinOn;
       
   177     TInt ret = IsIdPinOn(isIdPinOn);
       
   178     if (ret != KErrNone)
       
   179         {
       
   180         FTRACE(FPrint(_L("[CUSBClassChangeUIPlugin]\t CUSBOtgIdPinStateWatcher::RunL - IsIdPinOn=%d"), ret));
       
   181         User::Leave(ret);
       
   182         }    
       
   183 
       
   184     if (isIdPinOn)
       
   185         {
       
   186         FLOG(_L("[CUSBClassChangeUIPlugin]\t CUSBOtgIdPinStateWatcher::RunL IdPin ON"));
       
   187 
       
   188         iOtgHostStateWatcher.iObserver.OtgHostIdPinStateChanged(ETrue);
       
   189         }
       
   190     else
       
   191         {
       
   192         FLOG(_L("[CUSBClassChangeUIPlugin]\t CUSBOtgIdPinStateWatcher::RunL IdPin OFF"));
       
   193 
       
   194         iOtgHostStateWatcher.iObserver.OtgHostIdPinStateChanged(EFalse);
       
   195         }
       
   196     }
       
   197  
       
   198 // ----------------------------------------------------------------------------
       
   199 // Standard active object error function.
       
   200 // ----------------------------------------------------------------------------
       
   201 TInt CUSBOtgHostStateWatcher::CUSBOtgIdPinStateWatcher::RunError(TInt aError)
       
   202     {
       
   203     FTRACE(FPrint(_L("[CUSBClassChangeUIPlugin]\t CUSBOtgIdPinStateWatcher::RunL"), aError));
       
   204 
       
   205     // Currently no leaving functions called in RunL, thus nothing should cause
       
   206     // this to be called -> return.
       
   207     return KErrNone;
       
   208     }
       
   209 
       
   210 // ----------------------------------------------------------------------------
       
   211 // Standard active object cancellation function.
       
   212 // ----------------------------------------------------------------------------
       
   213 void CUSBOtgHostStateWatcher::CUSBOtgIdPinStateWatcher::DoCancel()
       
   214     {
       
   215     FLOG(_L("[CUSBClassChangeUIPlugin]\t CUSBOtgIdPinStateWatcher::DoCancel"));
       
   216     
       
   217     iIdPin.Cancel();
       
   218     }
       
   219 
       
   220 // ----------------------------------------------------------------------------
       
   221 // C++ constructor
       
   222 // ----------------------------------------------------------------------------
       
   223 CUSBOtgHostStateWatcher::CUSBOtgIdPinStateWatcher::CUSBOtgIdPinStateWatcher(
       
   224         CUSBOtgHostStateWatcher& aOtgHostStateWatcher)
       
   225     : CActive(EPriorityStandard), iOtgHostStateWatcher(aOtgHostStateWatcher)
       
   226     {
       
   227     CActiveScheduler::Add(this);
       
   228     }
       
   229 
       
   230 // --------------------------------------------------------------------------
       
   231 // Symbian 2nd phase constructor. 
       
   232 // --------------------------------------------------------------------------
       
   233 void CUSBOtgHostStateWatcher::CUSBOtgIdPinStateWatcher::ConstructL()
       
   234     {
       
   235     TInt ret = iIdPin.Attach(KUidUsbManCategory, KUsbOtgIdPinPresentProperty);
       
   236     if (ret != KErrNone)
       
   237         {
       
   238         FTRACE(FPrint(_L("[CUSBClassChangeUIPlugin]\t CUSBOtgIdPinStateWatcher::ConstructL - iIdPin.Attach error:%d"), ret));
       
   239         User::Leave(ret);
       
   240         }
       
   241     
       
   242     // Issue request first
       
   243     iIdPin.Subscribe(iStatus);
       
   244     SetActive();
       
   245     }
       
   246 
       
   247 // --------------------------------------------------------------------------
       
   248 // Two-phased constructor.
       
   249 // --------------------------------------------------------------------------
       
   250 CUSBOtgHostStateWatcher::CUSBHostEventNotifWatcher* 
       
   251 CUSBOtgHostStateWatcher::CUSBHostEventNotifWatcher::NewL(CUSBOtgHostStateWatcher& aOtgHostStateWatcher)
       
   252     {
       
   253     FLOG(_L("[CUSBClassChangeUIPlugin]\t CUSBHostEventNotifWatcher::NewL"));
       
   254 
       
   255     CUSBHostEventNotifWatcher* self = new(ELeave)CUSBHostEventNotifWatcher(aOtgHostStateWatcher);
       
   256     CleanupStack::PushL(self);
       
   257     self->ConstructL();
       
   258     CleanupStack::Pop(self);
       
   259     return self;
       
   260     }
       
   261 
       
   262 // --------------------------------------------------------------------------
       
   263 // C++ destructor
       
   264 // --------------------------------------------------------------------------
       
   265 CUSBOtgHostStateWatcher::CUSBHostEventNotifWatcher::~CUSBHostEventNotifWatcher()
       
   266     {
       
   267     FLOG(_L("[CUSBClassChangeUIPlugin]\t CUSBHostEventNotifWatcher::~CUSBHostEventNotifWatcher"));
       
   268 
       
   269     Cancel();    
       
   270     }
       
   271 
       
   272 // --------------------------------------------------------------------------
       
   273 // The Id pin state has changed.
       
   274 // --------------------------------------------------------------------------
       
   275 void CUSBOtgHostStateWatcher::CUSBHostEventNotifWatcher::RunL()
       
   276     {
       
   277     FLOG(_L("[CUSBClassChangeUIPlugin]\t CUSBHostEventNotifWatcher::RunL"));
       
   278 
       
   279     if (iStatus.Int() != KErrNone)
       
   280         {
       
   281         FTRACE(FPrint(_L("[CUSBClassChangeUIPlugin]\t CUSBHostEventNotifWatcher::RunL - iStatus=%d"), iStatus.Int()));
       
   282         User::Leave(iStatus.Int());
       
   283         }    
       
   284 
       
   285     // Save current event
       
   286     TDeviceEventInformation dei = iEventInfo;
       
   287 
       
   288     // Re-issue request first
       
   289     iOtgHostStateWatcher.iUsbMan.HostEventNotification(iStatus, iEventInfo);
       
   290     SetActive();
       
   291    
       
   292     iOtgHostStateWatcher.iObserver.HostEventNotify(dei);    
       
   293     }
       
   294  
       
   295 // ----------------------------------------------------------------------------
       
   296 // Standard active object error function.
       
   297 // ----------------------------------------------------------------------------
       
   298 TInt CUSBOtgHostStateWatcher::CUSBHostEventNotifWatcher::RunError(TInt aError)
       
   299     {
       
   300     FTRACE(FPrint(_L("[CUSBClassChangeUIPlugin]\t CUSBHostEventNotifWatcher::RunL"), aError));
       
   301 
       
   302     // Currently no leaving functions called in RunL, thus nothing should cause
       
   303     // this to be called -> return.
       
   304     return KErrNone;
       
   305     }
       
   306 
       
   307 // ----------------------------------------------------------------------------
       
   308 // Standard active object cancellation function.
       
   309 // ----------------------------------------------------------------------------
       
   310 void CUSBOtgHostStateWatcher::CUSBHostEventNotifWatcher::DoCancel()
       
   311     {
       
   312     FLOG(_L("[CUSBClassChangeUIPlugin]\t CUSBHostEventNotifWatcher::DoCancel"));
       
   313     
       
   314     iOtgHostStateWatcher.iUsbMan.HostEventNotificationCancel();
       
   315     }
       
   316 
       
   317 // ----------------------------------------------------------------------------
       
   318 // C++ constructor
       
   319 // ----------------------------------------------------------------------------
       
   320 CUSBOtgHostStateWatcher::CUSBHostEventNotifWatcher::CUSBHostEventNotifWatcher(
       
   321         CUSBOtgHostStateWatcher& aOtgHostStateWatcher)
       
   322     : CActive(EPriorityStandard), iOtgHostStateWatcher(aOtgHostStateWatcher)
       
   323     {
       
   324     CActiveScheduler::Add(this);
       
   325     }
       
   326 
       
   327 // --------------------------------------------------------------------------
       
   328 // Symbian 2nd phase constructor. 
       
   329 // --------------------------------------------------------------------------
       
   330 void CUSBOtgHostStateWatcher::CUSBHostEventNotifWatcher::ConstructL()
       
   331     {
       
   332     iOtgHostStateWatcher.iUsbMan.HostEventNotification(iStatus, iEventInfo);
       
   333     SetActive();
       
   334     }