usbengines/usbotgwatcher/src/cusbvbusobserver.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 <usbotgdefs.h>
       
    19 
       
    20 #include "cusbvbusobserver.h"
       
    21 
       
    22 #include "definitions.h"
       
    23 #include "debug.h"
       
    24 #include "panic.h"
       
    25 
       
    26 // ---------------------------------------------------------------------------
       
    27 // 
       
    28 // ---------------------------------------------------------------------------
       
    29 //
       
    30 CUsbVBusObserver::CUsbVBusObserver() :
       
    31     CActive(EPriorityStandard)
       
    32     {
       
    33     CActiveScheduler::Add(this);
       
    34     }
       
    35 
       
    36 // ---------------------------------------------------------------------------
       
    37 // 
       
    38 // ---------------------------------------------------------------------------
       
    39 //
       
    40 void CUsbVBusObserver::ConstructL()
       
    41     {
       
    42     LOG_FUNC
       
    43 
       
    44     LEAVEIFERROR(iVBus.Attach(KUidUsbManCategory,
       
    45             KUsbOtgVBusPoweredProperty));
       
    46 
       
    47     }
       
    48 
       
    49 // ---------------------------------------------------------------------------
       
    50 // 
       
    51 // ---------------------------------------------------------------------------
       
    52 //
       
    53 CUsbVBusObserver* CUsbVBusObserver::NewL()
       
    54     {
       
    55     LOG_FUNC
       
    56 
       
    57     CUsbVBusObserver* self = new (ELeave) CUsbVBusObserver();
       
    58     CleanupStack::PushL(self);
       
    59     self->ConstructL();
       
    60     CleanupStack::Pop(self);
       
    61     return self;
       
    62     }
       
    63 
       
    64 // ---------------------------------------------------------------------------
       
    65 // 
       
    66 // ---------------------------------------------------------------------------
       
    67 //
       
    68 CUsbVBusObserver::~CUsbVBusObserver()
       
    69     {
       
    70     LOG_FUNC
       
    71 
       
    72     Cancel();
       
    73 
       
    74     iVBus.Close();
       
    75 
       
    76     iObservers.Close();
       
    77 
       
    78     }
       
    79 
       
    80 // ---------------------------------------------------------------------------
       
    81 // 
       
    82 // ---------------------------------------------------------------------------
       
    83 //
       
    84 CUsbVBusObserver::TState CUsbVBusObserver::VBus() /* not const, because for some reason RProperty::Get is not const */
       
    85     {
       
    86 
       
    87     TInt val(0);
       
    88 
       
    89     TInt err = iVBus.Get(val);
       
    90 
       
    91     if (KErrNone != err)
       
    92         {
       
    93         LOG("CanNotGetVBusProperty" )
       
    94         PANIC( ECanNotGetVBusProperty);
       
    95         }
       
    96 
       
    97     return (0 == val ? EVBusDown : EVBusUp);
       
    98     }
       
    99 
       
   100 // ---------------------------------------------------------------------------
       
   101 // 
       
   102 // ---------------------------------------------------------------------------
       
   103 //
       
   104 void CUsbVBusObserver::SubscribeL(MUsbVBusObserver& aObserver)
       
   105     {
       
   106     LOG_FUNC
       
   107 
       
   108     // check if the same observer already exist in a list
       
   109     if (KErrNotFound != iObservers.Find(&aObserver))
       
   110         {
       
   111         LOG( "Observer already exists" );
       
   112         PANIC( EObserverAlreadyExists);
       
   113         return;
       
   114         }
       
   115 
       
   116     iObservers.AppendL(&aObserver);
       
   117 
       
   118     if (KFirst == iObservers.Count()) // first item
       
   119         {
       
   120         iVBus.Subscribe(iStatus);
       
   121         SetActive();
       
   122         }
       
   123     }
       
   124 
       
   125 // ---------------------------------------------------------------------------
       
   126 // 
       
   127 // ---------------------------------------------------------------------------
       
   128 //
       
   129 void CUsbVBusObserver::UnsubscribeL(MUsbVBusObserver& aObserver)
       
   130     {
       
   131     LOG_FUNC
       
   132 
       
   133     TInt i(iObservers.Find(&aObserver));
       
   134     if (KErrNotFound == i)
       
   135         {
       
   136         LOG("Observer not found" );
       
   137         PANIC( ECanNotFindVBusObserver);
       
   138         return;
       
   139         }
       
   140 
       
   141     iObservers.Remove(i);
       
   142 
       
   143     if (0 == iObservers.Count()) // no observers anymore
       
   144         {
       
   145         // cancel pending request, if any
       
   146         Cancel();
       
   147         }
       
   148     }
       
   149 
       
   150 // ---------------------------------------------------------------------------
       
   151 //
       
   152 // ---------------------------------------------------------------------------
       
   153 //
       
   154 void CUsbVBusObserver::RunL()
       
   155     {
       
   156     LOG_FUNC
       
   157 
       
   158     LOG1( "iStatus = %d" , iStatus.Int());
       
   159 
       
   160     // if error occured, tell to Observers
       
   161     if (KErrNone != iStatus.Int())
       
   162         {
       
   163         for (TInt i(0); i < iObservers.Count(); ++i)
       
   164             {
       
   165             iObservers[i]->VBusObserverErrorL(iStatus.Int());
       
   166             }
       
   167         return;
       
   168         }
       
   169 
       
   170     // re-issue request first
       
   171     iVBus.Subscribe(iStatus);
       
   172     SetActive();
       
   173 
       
   174     // then process property change
       
   175     TState state(VBus());
       
   176 
       
   177     // report change   
       
   178     switch (state)
       
   179         {
       
   180         case EVBusUp:
       
   181             {
       
   182             LOG("VBus UP");
       
   183 
       
   184             for (TInt i(0); i < iObservers.Count(); ++i)
       
   185                 {
       
   186                 iObservers[i]->VBusUpL();
       
   187                 }
       
   188             break;
       
   189             }
       
   190             ;
       
   191 
       
   192         case EVBusDown:
       
   193             {
       
   194             LOG("VBus DOWN");
       
   195 
       
   196             for (TInt i(0); i < iObservers.Count(); ++i)
       
   197                 {
       
   198                 iObservers[i]->VBusDownL();
       
   199                 }
       
   200             break;
       
   201             }
       
   202             ;
       
   203 
       
   204         default:
       
   205             {
       
   206             LOG("WrongVBusState");
       
   207             PANIC( EWrongVBusState);
       
   208             }
       
   209         }
       
   210     }
       
   211 
       
   212 // ---------------------------------------------------------------------------
       
   213 // 
       
   214 // ---------------------------------------------------------------------------
       
   215 //
       
   216 void CUsbVBusObserver::DoCancel()
       
   217     {
       
   218     iVBus.Cancel();
       
   219     }
       
   220 
       
   221 // ---------------------------------------------------------------------------
       
   222 // 
       
   223 // ---------------------------------------------------------------------------
       
   224 //
       
   225 TInt CUsbVBusObserver::RunError(TInt aError)
       
   226     {
       
   227     LOG_FUNC
       
   228 
       
   229     LOG1( "aError = %d" , aError);
       
   230 
       
   231     // try to continue	
       
   232     return KErrNone;
       
   233     }