usbengines/usbotgwatcher/src/cusbbusactivityobserver.cpp
changeset 0 1e05558e2206
child 5 7068aba64af5
equal deleted inserted replaced
-1:000000000000 0:1e05558e2206
       
     1 /*
       
     2 * Copyright (c) 2008-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:  Implementation
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include <usbotgdefs.h>
       
    20 
       
    21 #include "cusbbusactivityobserver.h"
       
    22 
       
    23 #include "definitions.h"
       
    24 #include "debug.h"
       
    25 #include "panic.h"
       
    26 
       
    27 // ---------------------------------------------------------------------------
       
    28 // 
       
    29 // ---------------------------------------------------------------------------
       
    30 //
       
    31 CUsbBusActivityObserver::CUsbBusActivityObserver() :
       
    32     CActive(EPriorityStandard)
       
    33     {
       
    34     CActiveScheduler::Add(this);
       
    35     }
       
    36 
       
    37 // ---------------------------------------------------------------------------
       
    38 // 
       
    39 // ---------------------------------------------------------------------------
       
    40 //
       
    41 void CUsbBusActivityObserver::ConstructL()
       
    42     {
       
    43 
       
    44         FLOG( _L( "[USBOTGWATCHER]\tCUsbBusActivityObserver::ConstructL" ) );
       
    45 
       
    46     User::LeaveIfError(iBusActivity.Attach(KUidUsbManCategory,
       
    47             KUsbOtgConnectionIdleProperty));
       
    48 
       
    49     }
       
    50 
       
    51 // ---------------------------------------------------------------------------
       
    52 // 
       
    53 // ---------------------------------------------------------------------------
       
    54 //
       
    55 CUsbBusActivityObserver* CUsbBusActivityObserver::NewL()
       
    56     {
       
    57 
       
    58         FLOG( _L( "[USBOTGWATCHER]\tCUsbBusActivityObserver::NewL" ) );
       
    59 
       
    60     CUsbBusActivityObserver* self = new (ELeave) CUsbBusActivityObserver();
       
    61     CleanupStack::PushL(self);
       
    62     self->ConstructL();
       
    63     CleanupStack::Pop(self);
       
    64     return self;
       
    65     }
       
    66 
       
    67 // ---------------------------------------------------------------------------
       
    68 // 
       
    69 // ---------------------------------------------------------------------------
       
    70 //
       
    71 CUsbBusActivityObserver::~CUsbBusActivityObserver()
       
    72 
       
    73     {
       
    74 
       
    75         FLOG( _L( "[USBOTGWATCHER]\tCUsbBusActivityObserver::~CUsbBusActivityObserver" ) );
       
    76 
       
    77     Cancel();
       
    78 
       
    79     iBusActivity.Close();
       
    80 
       
    81     iObservers.Close();
       
    82 
       
    83     }
       
    84 
       
    85 // ---------------------------------------------------------------------------
       
    86 // 
       
    87 // ---------------------------------------------------------------------------
       
    88 //
       
    89 CUsbBusActivityObserver::TBusActivity CUsbBusActivityObserver::BusActivity()
       
    90     {
       
    91 
       
    92         FLOG( _L( "[USBOTGWATCHER]\tCUsbBusActivityObserver::BusActivity" ) );
       
    93 
       
    94     TInt val(0);
       
    95 
       
    96     TInt err = iBusActivity.Get(val);
       
    97 
       
    98     if (KErrNone != err)
       
    99         {
       
   100             FLOG( _L( "[USBOTGWATCHER]\tCUsbBusActivityObserver::BusActivity CanNotGetBusActivityProperty" ) );
       
   101         Panic(ECanNotGetBusActivityProperty);
       
   102         }
       
   103 
       
   104         FTRACE( FPrint(_L( "[USBOTGWATCHER]\tCUsbBusActivityObserver::State = %d" ), val ));
       
   105 
       
   106     return (0 == val ? EBusActive : EBusIdle);
       
   107 
       
   108     }
       
   109 
       
   110 // ---------------------------------------------------------------------------
       
   111 // 
       
   112 // ---------------------------------------------------------------------------
       
   113 //
       
   114 void CUsbBusActivityObserver::SubscribeL(MUsbBusActivityObserver* aObserver)
       
   115     {
       
   116         FLOG( _L( "[USBOTGWATCHER]\tCUsbBusActivityObserver::SubscribeL" ) );
       
   117 
       
   118     User::LeaveIfError(iObservers.Append(aObserver));
       
   119 
       
   120     if (KFirst == iObservers.Count()) // first item
       
   121         {
       
   122         iBusActivity.Subscribe(iStatus);
       
   123         SetActive();
       
   124 
       
   125         }
       
   126     }
       
   127 
       
   128 // ---------------------------------------------------------------------------
       
   129 // 
       
   130 // ---------------------------------------------------------------------------
       
   131 //
       
   132 void CUsbBusActivityObserver::UnsubscribeL(MUsbBusActivityObserver* aObserver)
       
   133     {
       
   134         FLOG( _L( "[USBOTGWATCHER]\tCUsbBusActivityObserver::UnsubscribeL" ) );
       
   135 
       
   136     if (0 == iObservers.Count()) // no items
       
   137         {
       
   138         FLOG( _L( "[USBOTGWATCHER]\tCUsbBusActivityObserver::UnsubscribeL No observers" ) );
       
   139         return;
       
   140         }
       
   141         
       
   142     TInt i(0);
       
   143     while (i < iObservers.Count() && aObserver != iObservers[i])
       
   144         ++i;
       
   145 
       
   146     if (aObserver == iObservers[i]) // found
       
   147         {
       
   148         iObservers.Remove(i);
       
   149         }
       
   150     else
       
   151         {
       
   152             FLOG( _L( "[USBOTGWATCHER]\tCUsbBusActivityObserver::UnsubscribeL CanNotFindBusActivityObserver " ) );
       
   153         Panic(ECanNotFindBusActivityObserver);
       
   154         }
       
   155 
       
   156     if (0 == iObservers.Count()) // no items
       
   157         {
       
   158         // cancel pending request
       
   159         Cancel();
       
   160         }
       
   161     }
       
   162 
       
   163 // ---------------------------------------------------------------------------
       
   164 
       
   165 // ---------------------------------------------------------------------------
       
   166 //
       
   167 void CUsbBusActivityObserver::RunL()
       
   168     {
       
   169         FTRACE( FPrint(_L( "[USBOTGWATCHER]\tCUsbBusActivityObserver::RunL iStatus = %d" ), iStatus.Int()));
       
   170 
       
   171     // if error occured, inform observers
       
   172     if(KErrNone != iStatus.Int())
       
   173         {
       
   174         for (TInt i(0); i < iObservers.Count(); ++i)
       
   175                     {
       
   176                     iObservers[i]->BusActivityErrorL(iStatus.Int());
       
   177                     }
       
   178         }
       
   179 
       
   180     // re-issue request first
       
   181     iBusActivity.Subscribe(iStatus);
       
   182     SetActive();
       
   183 
       
   184     // then process property change
       
   185     TBusActivity state(BusActivity());
       
   186 
       
   187     switch (state)
       
   188         {
       
   189         case EBusIdle:
       
   190             {
       
   191                 FLOG( _L( "[USBOTGWATCHER]\tCUsbBusActivityObserver::RunL BusIdle" ) );
       
   192 
       
   193             for (TInt i(0); i < iObservers.Count(); ++i)
       
   194                 {
       
   195                 iObservers[i]->BusIdleL();
       
   196                 }
       
   197             break;
       
   198             }
       
   199 
       
   200         case EBusActive:
       
   201             {
       
   202                 FLOG( _L( "[USBOTGWATCHER]\tCUsbBusActivityObserver::RunL BusActive" ) );
       
   203 
       
   204             for (TInt i(0); i < iObservers.Count(); ++i)
       
   205                 {
       
   206                 iObservers[i]->BusActiveL();
       
   207                 }
       
   208             break;
       
   209             }
       
   210 
       
   211         default:
       
   212             {
       
   213                 FLOG( _L( "[USBOTGWATCHER]\tCUsbBusActivityObserver::RunL BusActive WrongBusState" ) );
       
   214             Panic(EWrongBusState);
       
   215             }
       
   216         }
       
   217 
       
   218     }
       
   219 
       
   220 // ---------------------------------------------------------------------------
       
   221 // 
       
   222 // ---------------------------------------------------------------------------
       
   223 //
       
   224 void CUsbBusActivityObserver::DoCancel()
       
   225     {
       
   226     iBusActivity.Cancel();
       
   227     }
       
   228 
       
   229 // ---------------------------------------------------------------------------
       
   230 // 
       
   231 // ---------------------------------------------------------------------------
       
   232 //
       
   233 TInt CUsbBusActivityObserver::RunError(TInt aError)
       
   234     {
       
   235 
       
   236     FTRACE( FPrint(_L( "[USBOTGWATCHER]\tCUsbBusActivityObserver::RunError aError = %d" ), aError));
       
   237                   
       
   238     // try to continue	
       
   239     return KErrNone;
       
   240 
       
   241     }
       
   242