usbengines/usbotgwatcher/src/cusbotgstateobserver.cpp
changeset 35 9d8b04ca6939
child 63 ef2686f7597e
equal deleted inserted replaced
34:7858bc6ead78 35:9d8b04ca6939
       
     1 /*
       
     2  * Copyright (c) 2008 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 #include "cusbotgstateobserver.h"
       
    19 
       
    20 #include "definitions.h"
       
    21 #include "debug.h"
       
    22 #include "panic.h"
       
    23 
       
    24 // ---------------------------------------------------------------------------
       
    25 // 
       
    26 // ---------------------------------------------------------------------------
       
    27 //
       
    28 CUsbOtgStateObserver::CUsbOtgStateObserver() :
       
    29     CActive(EPriorityStandard)
       
    30     {
       
    31     CActiveScheduler::Add(this);
       
    32     }
       
    33 
       
    34 // ---------------------------------------------------------------------------
       
    35 // 
       
    36 // ---------------------------------------------------------------------------
       
    37 //
       
    38 void CUsbOtgStateObserver::ConstructL()
       
    39     {
       
    40     LOG_FUNC
       
    41 
       
    42     LEAVEIFERROR(iOtgState.Attach(KUidUsbManCategory,
       
    43             KUsbOtgStateProperty));
       
    44 
       
    45     }
       
    46 
       
    47 // ---------------------------------------------------------------------------
       
    48 // 
       
    49 // ---------------------------------------------------------------------------
       
    50 //
       
    51 CUsbOtgStateObserver* CUsbOtgStateObserver::NewL()
       
    52     {
       
    53     LOG_FUNC
       
    54 
       
    55     CUsbOtgStateObserver* self = new (ELeave) CUsbOtgStateObserver();
       
    56     CleanupStack::PushL(self);
       
    57     self->ConstructL();
       
    58     CleanupStack::Pop(self);
       
    59     return self;
       
    60     }
       
    61 
       
    62 // ---------------------------------------------------------------------------
       
    63 // 
       
    64 // ---------------------------------------------------------------------------
       
    65 //
       
    66 CUsbOtgStateObserver::~CUsbOtgStateObserver()
       
    67     {
       
    68     LOG_FUNC
       
    69 
       
    70     Cancel();
       
    71 
       
    72     iOtgState.Close();
       
    73 
       
    74     iObservers.Close();
       
    75 
       
    76     }
       
    77 
       
    78 // ---------------------------------------------------------------------------
       
    79 // 
       
    80 // ---------------------------------------------------------------------------
       
    81 //
       
    82 TUsbOtgState CUsbOtgStateObserver::OtgState()
       
    83     {
       
    84     TInt val(0);
       
    85 
       
    86     TInt err = iOtgState.Get(val);
       
    87 
       
    88     if (KErrNone != err)
       
    89         {
       
    90         LOG("CanNotGetOtgStateProperty" );
       
    91         PANIC( ECanNotGetOtgStateProperty);
       
    92         }
       
    93 
       
    94     return (TUsbOtgState) val;
       
    95 
       
    96     }
       
    97 
       
    98 // ---------------------------------------------------------------------------
       
    99 // 
       
   100 // ---------------------------------------------------------------------------
       
   101 //
       
   102 void CUsbOtgStateObserver::SubscribeL(MUsbOtgStateObserver& aObserver)
       
   103     {
       
   104     LOG_FUNC
       
   105 
       
   106     // check if the same observer already exist in a list
       
   107     if (KErrNotFound != iObservers.Find(&aObserver))
       
   108         {
       
   109         LOG( "Observer already exists"  );
       
   110         PANIC( EObserverAlreadyExists);
       
   111         return;
       
   112         }
       
   113     iObservers.AppendL(&aObserver);
       
   114 
       
   115     if (KFirst == iObservers.Count()) // first item
       
   116         {
       
   117         iOtgState.Subscribe(iStatus);
       
   118         SetActive();
       
   119 
       
   120         }
       
   121     }
       
   122 
       
   123 // ---------------------------------------------------------------------------
       
   124 // 
       
   125 // ---------------------------------------------------------------------------
       
   126 //
       
   127 void CUsbOtgStateObserver::UnsubscribeL(MUsbOtgStateObserver& aObserver)
       
   128     {
       
   129     LOG_FUNC
       
   130 
       
   131     TInt i(iObservers.Find(&aObserver));
       
   132     if (KErrNotFound == i)
       
   133         {
       
   134         LOG("Observer not found");
       
   135         PANIC( ECanNotFindOtgStateObserver);
       
   136         return;
       
   137         }
       
   138 
       
   139     iObservers.Remove(i);
       
   140 
       
   141     if (0 == iObservers.Count()) // no observers anymore
       
   142         {
       
   143         // cancel pending request
       
   144         Cancel();
       
   145         }
       
   146     }
       
   147 
       
   148 // ---------------------------------------------------------------------------
       
   149 //
       
   150 // ---------------------------------------------------------------------------
       
   151 //
       
   152 void CUsbOtgStateObserver::RunL()
       
   153     {
       
   154     LOG_FUNC
       
   155 
       
   156     LOG1( "iStatus = %d" , iStatus.Int());
       
   157 
       
   158     // if error occured, tell to Observers
       
   159     if (KErrNone != iStatus.Int())
       
   160         {
       
   161         for (TInt i(0); i < iObservers.Count(); ++i)
       
   162             {
       
   163             iObservers[i]->OtgStateErrorL(iStatus.Int());
       
   164             }
       
   165         return;
       
   166         }
       
   167 
       
   168     // re-issue request first
       
   169     iOtgState.Subscribe(iStatus);
       
   170     SetActive();
       
   171 
       
   172     // then process property change
       
   173     TUsbOtgState state(OtgState());
       
   174 
       
   175     switch (state)
       
   176         {
       
   177         case EUsbOtgStateReset:
       
   178             {
       
   179             LOG("OTGState == RESET" );
       
   180             break;
       
   181             }
       
   182         case EUsbOtgStateAIdle:
       
   183             {
       
   184             LOG("OTGState == AIdle" );
       
   185             for (TInt i(0); i < iObservers.Count(); ++i)
       
   186                 {
       
   187                 iObservers[i]->AIdleL();
       
   188                 }
       
   189             break;
       
   190             }
       
   191         case EUsbOtgStateAHost:
       
   192             {
       
   193             LOG( "OTGState == AHost");
       
   194             for (TInt i(0); i < iObservers.Count(); ++i)
       
   195                 {
       
   196                 iObservers[i]->AHostL();
       
   197                 }
       
   198             break;
       
   199             }
       
   200         case EUsbOtgStateAPeripheral:
       
   201             {
       
   202             LOG("OTGState == APeripheral" );
       
   203             for (TInt i(0); i < iObservers.Count(); ++i)
       
   204                 {
       
   205                 iObservers[i]->APeripheralL();
       
   206                 }
       
   207             break;
       
   208             }
       
   209         case EUsbOtgStateAVbusError:
       
   210             {
       
   211             LOG("OTGState == AVBusError" );
       
   212             for (TInt i(0); i < iObservers.Count(); ++i)
       
   213                 {
       
   214                 iObservers[i]->AVBusErrorL();
       
   215                 }
       
   216             break;
       
   217             }
       
   218         case EUsbOtgStateBIdle:
       
   219             {
       
   220             LOG( "OTGState == BIdle" );
       
   221             for (TInt i(0); i < iObservers.Count(); ++i)
       
   222                 {
       
   223                 iObservers[i]->BIdleL();
       
   224                 }
       
   225             break;
       
   226             }
       
   227         case EUsbOtgStateBPeripheral:
       
   228             {
       
   229             LOG("OTGState == BPeripheral" );
       
   230             for (TInt i(0); i < iObservers.Count(); ++i)
       
   231                 {
       
   232                 iObservers[i]->BPeripheralL();
       
   233                 }
       
   234             break;
       
   235             }
       
   236         case EUsbOtgStateBHost:
       
   237             {
       
   238             LOG("OTGState == BHost" );
       
   239             for (TInt i(0); i < iObservers.Count(); ++i)
       
   240                 {
       
   241                 iObservers[i]->BHostL();
       
   242                 }
       
   243             break;
       
   244             }
       
   245         default:
       
   246             {
       
   247             LOG("WrongOtgState" );
       
   248             PANIC( EWrongOtgState);
       
   249             }
       
   250         }
       
   251 
       
   252     }
       
   253 
       
   254 // ---------------------------------------------------------------------------
       
   255 // 
       
   256 // ---------------------------------------------------------------------------
       
   257 //
       
   258 void CUsbOtgStateObserver::DoCancel()
       
   259     {
       
   260     iOtgState.Cancel();
       
   261     }
       
   262 
       
   263 // ---------------------------------------------------------------------------
       
   264 // 
       
   265 // ---------------------------------------------------------------------------
       
   266 //
       
   267 TInt CUsbOtgStateObserver::RunError(TInt aError)
       
   268     {
       
   269     LOG_FUNC
       
   270 
       
   271     LOG1( "aError = %d" , aError);
       
   272 
       
   273     // try to continue	
       
   274     return KErrNone;
       
   275 
       
   276     }
       
   277