usbengines/usbotgwatcher/src/cusbbusactivityobserver.cpp
changeset 25 8c311f9acc5e
parent 0 1e05558e2206
child 26 0ae0f29418cd
equal deleted inserted replaced
8:7e15987c4500 25:8c311f9acc5e
     1 /*
     1 /*
     2 * Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
     2  * Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3  * All rights reserved.
     4 * This component and the accompanying materials are made available
     4  * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     5  * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     6  * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     8  *
     9 * Initial Contributors:
     9  * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    10  * Nokia Corporation - initial contribution.
    11 *
    11  *
    12 * Contributors:
    12  * Contributors:
    13 *
    13  *
    14 * Description:  Implementation
    14  * Description:  Implementation
    15  *
    15  *
    16 */
    16  */
    17 
       
    18 
    17 
    19 #include <usbotgdefs.h>
    18 #include <usbotgdefs.h>
    20 
    19 
    21 #include "cusbbusactivityobserver.h"
    20 #include "cusbbusactivityobserver.h"
    22 
    21 
    38 // 
    37 // 
    39 // ---------------------------------------------------------------------------
    38 // ---------------------------------------------------------------------------
    40 //
    39 //
    41 void CUsbBusActivityObserver::ConstructL()
    40 void CUsbBusActivityObserver::ConstructL()
    42     {
    41     {
    43 
    42     LOG_FUNC
    44         FLOG( _L( "[USBOTGWATCHER]\tCUsbBusActivityObserver::ConstructL" ) );
       
    45 
    43 
    46     User::LeaveIfError(iBusActivity.Attach(KUidUsbManCategory,
    44     User::LeaveIfError(iBusActivity.Attach(KUidUsbManCategory,
    47             KUsbOtgConnectionIdleProperty));
    45             KUsbOtgConnectionIdleProperty));
    48 
    46 
    49     }
    47     }
    52 // 
    50 // 
    53 // ---------------------------------------------------------------------------
    51 // ---------------------------------------------------------------------------
    54 //
    52 //
    55 CUsbBusActivityObserver* CUsbBusActivityObserver::NewL()
    53 CUsbBusActivityObserver* CUsbBusActivityObserver::NewL()
    56     {
    54     {
    57 
    55     LOG_FUNC
    58         FLOG( _L( "[USBOTGWATCHER]\tCUsbBusActivityObserver::NewL" ) );
       
    59 
    56 
    60     CUsbBusActivityObserver* self = new (ELeave) CUsbBusActivityObserver();
    57     CUsbBusActivityObserver* self = new (ELeave) CUsbBusActivityObserver();
    61     CleanupStack::PushL(self);
    58     CleanupStack::PushL(self);
    62     self->ConstructL();
    59     self->ConstructL();
    63     CleanupStack::Pop(self);
    60     CleanupStack::Pop(self);
    69 // ---------------------------------------------------------------------------
    66 // ---------------------------------------------------------------------------
    70 //
    67 //
    71 CUsbBusActivityObserver::~CUsbBusActivityObserver()
    68 CUsbBusActivityObserver::~CUsbBusActivityObserver()
    72 
    69 
    73     {
    70     {
    74 
    71     LOG_FUNC
    75         FLOG( _L( "[USBOTGWATCHER]\tCUsbBusActivityObserver::~CUsbBusActivityObserver" ) );
       
    76 
    72 
    77     Cancel();
    73     Cancel();
    78 
    74 
    79     iBusActivity.Close();
    75     iBusActivity.Close();
    80 
    76 
    87 // ---------------------------------------------------------------------------
    83 // ---------------------------------------------------------------------------
    88 //
    84 //
    89 CUsbBusActivityObserver::TBusActivity CUsbBusActivityObserver::BusActivity()
    85 CUsbBusActivityObserver::TBusActivity CUsbBusActivityObserver::BusActivity()
    90     {
    86     {
    91 
    87 
    92         FLOG( _L( "[USBOTGWATCHER]\tCUsbBusActivityObserver::BusActivity" ) );
       
    93 
       
    94     TInt val(0);
    88     TInt val(0);
    95 
    89 
    96     TInt err = iBusActivity.Get(val);
    90     TInt err = iBusActivity.Get(val);
    97 
    91 
    98     if (KErrNone != err)
    92     if (KErrNone != err)
    99         {
    93         {
   100             FLOG( _L( "[USBOTGWATCHER]\tCUsbBusActivityObserver::BusActivity CanNotGetBusActivityProperty" ) );
    94         LOG("ECanNotGetBusActivityProperty" );
   101         Panic(ECanNotGetBusActivityProperty);
    95         Panic( ECanNotGetBusActivityProperty);
   102         }
    96         }
   103 
       
   104         FTRACE( FPrint(_L( "[USBOTGWATCHER]\tCUsbBusActivityObserver::State = %d" ), val ));
       
   105 
    97 
   106     return (0 == val ? EBusActive : EBusIdle);
    98     return (0 == val ? EBusActive : EBusIdle);
   107 
    99 
   108     }
   100     }
   109 
   101 
   110 // ---------------------------------------------------------------------------
   102 // ---------------------------------------------------------------------------
   111 // 
   103 // 
   112 // ---------------------------------------------------------------------------
   104 // ---------------------------------------------------------------------------
   113 //
   105 //
   114 void CUsbBusActivityObserver::SubscribeL(MUsbBusActivityObserver* aObserver)
   106 void CUsbBusActivityObserver::SubscribeL(MUsbBusActivityObserver& aObserver)
   115     {
   107     {
   116         FLOG( _L( "[USBOTGWATCHER]\tCUsbBusActivityObserver::SubscribeL" ) );
   108     LOG_FUNC
   117 
   109 
   118     User::LeaveIfError(iObservers.Append(aObserver));
   110     // check if the same observer already exist in a list
       
   111     if (KErrNotFound != iObservers.Find(&aObserver))
       
   112         {
       
   113         LOG( "Observer already exists" );
       
   114         Panic( EObserverAlreadyExists);
       
   115         return;
       
   116         }
       
   117     iObservers.AppendL(&aObserver);
   119 
   118 
   120     if (KFirst == iObservers.Count()) // first item
   119     if (KFirst == iObservers.Count()) // first item
   121         {
   120         {
   122         iBusActivity.Subscribe(iStatus);
   121         iBusActivity.Subscribe(iStatus);
   123         SetActive();
   122         SetActive();
   127 
   126 
   128 // ---------------------------------------------------------------------------
   127 // ---------------------------------------------------------------------------
   129 // 
   128 // 
   130 // ---------------------------------------------------------------------------
   129 // ---------------------------------------------------------------------------
   131 //
   130 //
   132 void CUsbBusActivityObserver::UnsubscribeL(MUsbBusActivityObserver* aObserver)
   131 void CUsbBusActivityObserver::UnsubscribeL(MUsbBusActivityObserver& aObserver)
   133     {
   132     {
   134         FLOG( _L( "[USBOTGWATCHER]\tCUsbBusActivityObserver::UnsubscribeL" ) );
   133     LOG_FUNC
   135 
   134 
   136     if (0 == iObservers.Count()) // no items
   135     TInt i(iObservers.Find(&aObserver));
   137         {
   136     if (KErrNotFound == i)
   138         FLOG( _L( "[USBOTGWATCHER]\tCUsbBusActivityObserver::UnsubscribeL No observers" ) );
   137         {
       
   138         LOG( "Observer not found" );
       
   139         Panic( ECanNotFindBusActivityObserver);
   139         return;
   140         return;
   140         }
   141         }
   141         
   142 
   142     TInt i(0);
   143     iObservers.Remove(i);
   143     while (i < iObservers.Count() && aObserver != iObservers[i])
   144 
   144         ++i;
   145     if (0 == iObservers.Count()) // no observers anymore
   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         {
   146         {
   158         // cancel pending request
   147         // cancel pending request
   159         Cancel();
   148         Cancel();
   160         }
   149         }
   161     }
   150     }
   164 
   153 
   165 // ---------------------------------------------------------------------------
   154 // ---------------------------------------------------------------------------
   166 //
   155 //
   167 void CUsbBusActivityObserver::RunL()
   156 void CUsbBusActivityObserver::RunL()
   168     {
   157     {
   169         FTRACE( FPrint(_L( "[USBOTGWATCHER]\tCUsbBusActivityObserver::RunL iStatus = %d" ), iStatus.Int()));
   158     LOG_FUNC
       
   159 
       
   160     LOG1( "iStatus = %d" , iStatus.Int());
   170 
   161 
   171     // if error occured, inform observers
   162     // if error occured, inform observers
   172     if(KErrNone != iStatus.Int())
   163     if (KErrNone != iStatus.Int())
   173         {
   164         {
   174         for (TInt i(0); i < iObservers.Count(); ++i)
   165         for (TInt i(0); i < iObservers.Count(); ++i)
   175                     {
   166             {
   176                     iObservers[i]->BusActivityErrorL(iStatus.Int());
   167             iObservers[i]->BusActivityErrorL(iStatus.Int());
   177                     }
   168             }
   178         }
   169         }
   179 
   170 
   180     // re-issue request first
   171     // re-issue request first
   181     iBusActivity.Subscribe(iStatus);
   172     iBusActivity.Subscribe(iStatus);
   182     SetActive();
   173     SetActive();
   186 
   177 
   187     switch (state)
   178     switch (state)
   188         {
   179         {
   189         case EBusIdle:
   180         case EBusIdle:
   190             {
   181             {
   191                 FLOG( _L( "[USBOTGWATCHER]\tCUsbBusActivityObserver::RunL BusIdle" ) );
   182             LOG("BusIdle");
   192 
   183 
   193             for (TInt i(0); i < iObservers.Count(); ++i)
   184             for (TInt i(0); i < iObservers.Count(); ++i)
   194                 {
   185                 {
   195                 iObservers[i]->BusIdleL();
   186                 iObservers[i]->BusIdleL();
   196                 }
   187                 }
   197             break;
   188             break;
   198             }
   189             }
   199 
   190 
   200         case EBusActive:
   191         case EBusActive:
   201             {
   192             {
   202                 FLOG( _L( "[USBOTGWATCHER]\tCUsbBusActivityObserver::RunL BusActive" ) );
   193             LOG("BusActive");
   203 
   194 
   204             for (TInt i(0); i < iObservers.Count(); ++i)
   195             for (TInt i(0); i < iObservers.Count(); ++i)
   205                 {
   196                 {
   206                 iObservers[i]->BusActiveL();
   197                 iObservers[i]->BusActiveL();
   207                 }
   198                 }
   208             break;
   199             break;
   209             }
   200             }
   210 
   201 
   211         default:
   202         default:
   212             {
   203             {
   213                 FLOG( _L( "[USBOTGWATCHER]\tCUsbBusActivityObserver::RunL BusActive WrongBusState" ) );
   204             LOG("WrongBusState" );
   214             Panic(EWrongBusState);
   205             Panic( EWrongBusState);
   215             }
   206             }
   216         }
   207         }
   217 
   208 
   218     }
   209     }
   219 
   210 
   230 // 
   221 // 
   231 // ---------------------------------------------------------------------------
   222 // ---------------------------------------------------------------------------
   232 //
   223 //
   233 TInt CUsbBusActivityObserver::RunError(TInt aError)
   224 TInt CUsbBusActivityObserver::RunError(TInt aError)
   234     {
   225     {
   235 
   226     LOG_FUNC
   236     FTRACE( FPrint(_L( "[USBOTGWATCHER]\tCUsbBusActivityObserver::RunError aError = %d" ), aError));
   227 
   237                   
   228     LOG1( "aError = %d", aError );
       
   229 
   238     // try to continue	
   230     // try to continue	
   239     return KErrNone;
   231     return KErrNone;
   240 
   232 
   241     }
   233     }
   242 
   234