usbengines/usbotgwatcher/src/cusbidpinobserver.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 "cusbidpinobserver.h"
    20 #include "cusbidpinobserver.h"
    22 
    21 
    38 // 
    37 // 
    39 // ---------------------------------------------------------------------------
    38 // ---------------------------------------------------------------------------
    40 //
    39 //
    41 void CUsbIdPinObserver::ConstructL()
    40 void CUsbIdPinObserver::ConstructL()
    42     {
    41     {
    43 
    42     LOG_FUNC
    44         FLOG( _L( "[USBOTGWATCHER]\tCUsbIdPinObserver::ConstructL" ) );
       
    45 
    43 
    46     User::LeaveIfError(iIdPin.Attach(KUidUsbManCategory,
    44     User::LeaveIfError(iIdPin.Attach(KUidUsbManCategory,
    47             KUsbOtgIdPinPresentProperty));
    45             KUsbOtgIdPinPresentProperty));
    48 
    46 
    49     }
    47     }
    52 // 
    50 // 
    53 // ---------------------------------------------------------------------------
    51 // ---------------------------------------------------------------------------
    54 //
    52 //
    55 CUsbIdPinObserver* CUsbIdPinObserver::NewL()
    53 CUsbIdPinObserver* CUsbIdPinObserver::NewL()
    56     {
    54     {
    57 
    55     LOG_FUNC
    58         FLOG( _L( "[USBOTGWATCHER]\tCUsbIdPinObserver::NewL" ) );
       
    59 
    56 
    60     CUsbIdPinObserver* self = new (ELeave) CUsbIdPinObserver();
    57     CUsbIdPinObserver* self = new (ELeave) CUsbIdPinObserver();
    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 CUsbIdPinObserver::~CUsbIdPinObserver()
    68 CUsbIdPinObserver::~CUsbIdPinObserver()
    72     {
    69     {
    73 
    70     LOG_FUNC
    74         FLOG( _L( "[USBOTGWATCHER]\tCUsbIdPinObserver::~CUsbIdPinObserver" ) );
       
    75 
    71 
    76     Cancel();
    72     Cancel();
    77 
    73 
    78     iIdPin.Close();
    74     iIdPin.Close();
    79 
    75 
    87 //
    83 //
    88 CUsbIdPinObserver::TState CUsbIdPinObserver::IdPin()
    84 CUsbIdPinObserver::TState CUsbIdPinObserver::IdPin()
    89 /* this getter is not const, because for some reason RProperty::Get is not const */
    85 /* this getter is not const, because for some reason RProperty::Get is not const */
    90     {
    86     {
    91 
    87 
    92         FLOG( _L( "[USBOTGWATCHER]\tCUsbIdPinObserver::IdPin" ) );
       
    93 
       
    94     TInt val(0);
    88     TInt val(0);
    95 
    89 
    96     TInt err = iIdPin.Get(val);
    90     TInt err = iIdPin.Get(val);
    97 
    91 
    98     if (KErrNone != err)
    92     if (KErrNone != err)
    99         {
    93         {
   100             FLOG( _L( "[USBOTGWATCHER]\tCUsbIdPinObserver::IdPin CanNotGetIdPinProperty" ) );
    94         LOG("CanNotGetIdPinProperty" );
   101         Panic(ECanNotGetIdPinProperty);
    95         Panic( ECanNotGetIdPinProperty);
   102         }
    96         }
   103 
    97 
   104         FTRACE( FPrint(_L( "[USBOTGWATCHER]\tCUsbIdPinObserver::IdPin = %d" ), val ));
    98     return (EFalse == val ? EIdPinOff : EIdPinOn);
   105 
       
   106     // not found in docs clear definition of this property. Verification is needed   
       
   107     return (0 == val ? EIdPinOff : EIdPinOn);
       
   108     }
    99     }
   109 
   100 
   110 // ---------------------------------------------------------------------------
   101 // ---------------------------------------------------------------------------
   111 // 
   102 // 
   112 // ---------------------------------------------------------------------------
   103 // ---------------------------------------------------------------------------
   113 //   
   104 //   
   114 void CUsbIdPinObserver::SubscribeL(MUsbIdPinObserver* aObserver)
   105 void CUsbIdPinObserver::SubscribeL(MUsbIdPinObserver& aObserver)
   115     {
   106     {
   116         FLOG( _L( "[USBOTGWATCHER]\tCUsbIdPinObserver::SubscribeL" ) );
   107     LOG_FUNC
   117 
   108 
   118     User::LeaveIfError(iObservers.Append(aObserver));
   109     // check if the same observer already exist in a list
       
   110     if (KErrNotFound != iObservers.Find(&aObserver))
       
   111         {
       
   112         LOG("Observer already exists" );
       
   113         Panic( EObserverAlreadyExists);
       
   114         return;
       
   115         }
       
   116     iObservers.AppendL(&aObserver);
   119 
   117 
   120     if (KFirst == iObservers.Count()) // first item
   118     if (KFirst == iObservers.Count()) // first item
   121         {
   119         {
   122         iIdPin.Subscribe(iStatus);
   120         iIdPin.Subscribe(iStatus);
   123         SetActive();
   121         SetActive();
   127 
   125 
   128 // ---------------------------------------------------------------------------
   126 // ---------------------------------------------------------------------------
   129 // 
   127 // 
   130 // ---------------------------------------------------------------------------
   128 // ---------------------------------------------------------------------------
   131 //
   129 //
   132 void CUsbIdPinObserver::UnsubscribeL(MUsbIdPinObserver* aObserver)
   130 void CUsbIdPinObserver::UnsubscribeL(MUsbIdPinObserver& aObserver)
   133     {
   131     {
   134         FLOG( _L( "[USBOTGWATCHER]\tCUsbIdPinObserver::UnsubscribeL" ) );
   132     LOG_FUNC
   135     if (0 == iObservers.Count()) // no items
   133 
   136         {
   134     TInt i(iObservers.Find(&aObserver));
   137         FLOG( _L( "[USBOTGWATCHER]\tCUsbIdPinObserver::UnsubscribeL No observers" ) );
   135     if (KErrNotFound == i)
       
   136         {
       
   137         LOG("Observer not found");
       
   138         Panic( ECanNotFindIdPinObserver);
   138         return;
   139         return;
   139         }
   140         }
   140     
   141 
   141     TInt i(0);
   142     iObservers.Remove(i);
   142     while (i < iObservers.Count() && aObserver != iObservers[i])
   143 
   143         ++i;
   144     if (0 == iObservers.Count()) // no observers anymore
   144 
       
   145     if (aObserver == iObservers[i]) // found
       
   146         {
       
   147         iObservers.Remove(i);
       
   148         }
       
   149     else
       
   150         {
       
   151             FLOG( _L( "[USBOTGWATCHER]\tCUsbIdPinObserver::UnsubscribeL CanNotGetIdPinObserver" ) );
       
   152         Panic(ECanNotFindIdPinObserver);
       
   153         }
       
   154 
       
   155     if (0 == iObservers.Count()) // no items
       
   156         {
   145         {
   157         // cancel pending request
   146         // cancel pending request
   158         Cancel();
   147         Cancel();
   159         }
   148         }
   160     }
   149     }
   163 //
   152 //
   164 // ---------------------------------------------------------------------------
   153 // ---------------------------------------------------------------------------
   165 //
   154 //
   166 void CUsbIdPinObserver::RunL()
   155 void CUsbIdPinObserver::RunL()
   167     {
   156     {
   168         FTRACE( FPrint(_L( "[USBOTGWATCHER]\tCUsbIdPinObserver::RunL iStatus = %d" ), iStatus.Int()));
   157     LOG_FUNC
   169 
   158 
   170         // if error occured, tell to Observers
   159     LOG1( "iStatus = %d" , iStatus.Int());
   171         if(KErrNone != iStatus.Int()) 
   160 
   172             {
   161     // if error occured, tell to Observers
   173             for (TInt i(0); i < iObservers.Count(); ++i)
   162     if (KErrNone != iStatus.Int())
   174                  {
   163         {
   175                  iObservers[i]->IdPinErrorL(iStatus.Int());
   164         for (TInt i(0); i < iObservers.Count(); ++i)
   176                  }
   165             {
   177             return;
   166             iObservers[i]->IdPinErrorL(iStatus.Int());
   178             }
   167             }
       
   168         return;
       
   169         }
   179 
   170 
   180     // re-issue request first
   171     // re-issue request first
   181     iIdPin.Subscribe(iStatus);
   172     iIdPin.Subscribe(iStatus);
   182     SetActive();
   173     SetActive();
   183 
   174 
   187     // report change   
   178     // report change   
   188     switch (state)
   179     switch (state)
   189         {
   180         {
   190         case EIdPinOn:
   181         case EIdPinOn:
   191             {
   182             {
   192                 FLOG(_L( "[USBOTGWATCHER]\tCUsbIdPinObserver::RunL IdPin ON"));
   183             LOG("IdPin ON");
   193 
   184 
   194             for (TInt i(0); i < iObservers.Count(); ++i)
   185             for (TInt i(0); i < iObservers.Count(); ++i)
   195                 {
   186                 {
   196                 iObservers[i]->IdPinOnL();
   187                 iObservers[i]->IdPinOnL();
   197                 }
   188                 }
   199             }
   190             }
   200             ;
   191             ;
   201 
   192 
   202         case EIdPinOff:
   193         case EIdPinOff:
   203             {
   194             {
   204                 FLOG(_L( "[USBOTGWATCHER]\tCUsbIdPinObserver::RunL IdPin OFF"));
   195             LOG("IdPin OFF");
   205 
   196 
   206             for (TInt i(0); i < iObservers.Count(); ++i)
   197             for (TInt i(0); i < iObservers.Count(); ++i)
   207                 {
   198                 {
   208                 iObservers[i]->IdPinOffL();
   199                 iObservers[i]->IdPinOffL();
   209                 }
   200                 }
   211             }
   202             }
   212             ;
   203             ;
   213 
   204 
   214         default:
   205         default:
   215             {
   206             {
   216                 FLOG(_L( "[USBOTGWATCHER]\tCUsbIdPinObserver::RunL WrongIdPinState"));
   207             LOG("WrongIdPinState");
   217             Panic(EWrongIdPinState);
   208             Panic( EWrongIdPinState);
   218             }
   209             }
   219         }
   210         }
   220 
   211 
   221     }
   212     }
   222 
   213 
   233 // 
   224 // 
   234 // ---------------------------------------------------------------------------
   225 // ---------------------------------------------------------------------------
   235 //
   226 //
   236 TInt CUsbIdPinObserver::RunError(TInt aError)
   227 TInt CUsbIdPinObserver::RunError(TInt aError)
   237     {
   228     {
   238 
   229     LOG_FUNC
   239         FTRACE( FPrint(_L( "[USBOTGWATCHER]\tCUsbIdPinObserver::RunError aError = %d" ), aError));
   230 
       
   231     LOG1( "aError = %d", aError);
   240 
   232 
   241     // try to continue	
   233     // try to continue	
   242     return KErrNone;
   234     return KErrNone;
   243 
   235 
   244     }
   236     }