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