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