usbengines/usbotgwatcher/src/cusbmessagenotificationobserver.cpp
changeset 0 1e05558e2206
child 13 7068aba64af5
child 25 8c311f9acc5e
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 <usbman.h>
       
    20 #include <d32usbdi_errors.h>
       
    21 #include <d32otgdi_errors.h>
       
    22 
       
    23 #include "cusbmessagenotificationobserver.h"
       
    24 
       
    25 #include "definitions.h"
       
    26 #include "debug.h"
       
    27 #include "panic.h"
       
    28 
       
    29 // ---------------------------------------------------------------------------
       
    30 // 
       
    31 // ---------------------------------------------------------------------------
       
    32 //
       
    33 CUsbMessageNotificationObserver::CUsbMessageNotificationObserver(RUsb* aUsb) :
       
    34     CActive(EPriorityStandard), iUsb(aUsb)
       
    35     {
       
    36     CActiveScheduler::Add(this);
       
    37     }
       
    38 
       
    39 // ---------------------------------------------------------------------------
       
    40 // 
       
    41 // ---------------------------------------------------------------------------
       
    42 //
       
    43 void CUsbMessageNotificationObserver::ConstructL()
       
    44     {
       
    45 
       
    46         FLOG( _L( "[USBOTGWATCHER]\tCUsbMessageNotificationObserver::ConstructL" ) );
       
    47 
       
    48     }
       
    49 
       
    50 // ---------------------------------------------------------------------------
       
    51 // 
       
    52 // ---------------------------------------------------------------------------
       
    53 //
       
    54 CUsbMessageNotificationObserver* CUsbMessageNotificationObserver::NewL(
       
    55         RUsb* aUsb)
       
    56     {
       
    57 
       
    58         FLOG( _L( "[USBOTGWATCHER]\tCUsbMessageNotificationObserver::NewL" ) );
       
    59 
       
    60     CUsbMessageNotificationObserver* self =
       
    61             new (ELeave) CUsbMessageNotificationObserver(aUsb);
       
    62     CleanupStack::PushL(self);
       
    63     self->ConstructL();
       
    64     CleanupStack::Pop(self);
       
    65     return self;
       
    66     }
       
    67 
       
    68 // ---------------------------------------------------------------------------
       
    69 // 
       
    70 // ---------------------------------------------------------------------------
       
    71 //
       
    72 CUsbMessageNotificationObserver::~CUsbMessageNotificationObserver()
       
    73     {
       
    74 
       
    75         FLOG( _L( "[USBOTGWATCHER]\tCUsbMessageNotificationObserver::~CUsbMessageNotificationObserver" ) );
       
    76 
       
    77     Cancel();
       
    78 
       
    79     iObservers.Close();
       
    80 
       
    81     }
       
    82 
       
    83 // ---------------------------------------------------------------------------
       
    84 // 
       
    85 // ---------------------------------------------------------------------------
       
    86 //
       
    87 void CUsbMessageNotificationObserver::SubscribeL(
       
    88         MUsbMessageNotificationObserver* aObserver)
       
    89     {
       
    90         FLOG( _L( "[USBOTGWATCHER]\tCUsbMessageNotificationObserver::SubscribeL" ) );
       
    91 
       
    92     User::LeaveIfError(iObservers.Append(aObserver));
       
    93 
       
    94     if (KFirst == iObservers.Count()) // first item
       
    95         {
       
    96         iUsb->MessageNotification(iStatus, iMessage);
       
    97         SetActive();
       
    98         }
       
    99     }
       
   100 
       
   101 // ---------------------------------------------------------------------------
       
   102 // 
       
   103 // ---------------------------------------------------------------------------
       
   104 //
       
   105 void CUsbMessageNotificationObserver::UnsubscribeL(
       
   106         MUsbMessageNotificationObserver* aObserver)
       
   107     {
       
   108         FLOG( _L( "[USBOTGWATCHER]\tCUsbMessageNotificationObserver::UnsubscribeL" ) );
       
   109 
       
   110         if (0 == iObservers.Count()) // no items
       
   111             {
       
   112             FLOG( _L( "[USBOTGWATCHER]\tCUsbMessageNotificationObserver::UnsubscribeL No observers" ) );
       
   113             return;
       
   114             }
       
   115         
       
   116     TInt i(0);
       
   117     while (i < iObservers.Count() && aObserver != iObservers[i])
       
   118         ++i;
       
   119 
       
   120     if (aObserver == iObservers[i]) // found
       
   121         {
       
   122         iObservers.Remove(i);
       
   123         }
       
   124     else
       
   125         {
       
   126             FLOG( _L( "[USBOTGWATCHER]\tCUsbMessageNotificationObserver::UnsubscribeL CanNotFindMessageNotificationObserver" ) );
       
   127         Panic(ECanNotFindMessageNotificationObserver);
       
   128         }
       
   129 
       
   130     if (0 == iObservers.Count()) // no items
       
   131         {
       
   132         // cancel pending request
       
   133         Cancel();
       
   134         }
       
   135     }
       
   136 
       
   137 // ---------------------------------------------------------------------------
       
   138 //
       
   139 // ---------------------------------------------------------------------------
       
   140 //
       
   141 void CUsbMessageNotificationObserver::RunL()
       
   142     {
       
   143     // if error occured, tell to Observers
       
   144     if(KErrNone != iStatus.Int()) 
       
   145         {
       
   146         for (TInt i(0); i < iObservers.Count(); ++i)
       
   147              {
       
   148              iObservers[i]->MessageNotificationErrorL(iStatus.Int());
       
   149              }
       
   150         return;
       
   151         }
       
   152 
       
   153     TInt message(iMessage);
       
   154 
       
   155     // re-issue request first
       
   156     iUsb->MessageNotification(iStatus, iMessage);
       
   157     SetActive();
       
   158 
       
   159         // Log the event
       
   160         FTRACE( FPrint(_L( "[USBOTGWATCHER]\tCUsbMessageNotificationObserver::RunL iMessage = %d" ), message));
       
   161 
       
   162     // then process property change
       
   163     switch (message)
       
   164         {
       
   165         case KErrUsbBadHubPosition:
       
   166             {
       
   167                 FLOG( _L( "[USBOTGWATCHER]\tCUsbMessageNotificationObserver::RunL HubBadPosition" ) );
       
   168 
       
   169             for (TInt i(0); i < iObservers.Count(); ++i)
       
   170                 {
       
   171                 iObservers[i]->BadHubPositionL();
       
   172                 }
       
   173             break;
       
   174             }
       
   175         case KErrUsbOtgVbusError:
       
   176             {
       
   177                 FLOG( _L( "[USBOTGWATCHER]\tCUsbMessageNotificationObserver::RunL VBusError" ) );
       
   178 
       
   179             for (TInt i(0); i < iObservers.Count(); ++i)
       
   180                 {
       
   181                 iObservers[i]->VBusErrorL();
       
   182                 }
       
   183             break;
       
   184             }
       
   185         case KUsbMessageSrpReceived:
       
   186             {
       
   187                 FLOG( _L( "[USBOTGWATCHER]\tCUsbMessageNotificationObserver::RunL SRP received" ) );
       
   188 
       
   189             for (TInt i(0); i < iObservers.Count(); ++i)
       
   190                 {
       
   191                 iObservers[i]->SrpReceivedL();
       
   192                 }
       
   193             break;
       
   194             }
       
   195         case KUsbMessageRequestSession:
       
   196             {
       
   197                 FLOG( _L( "[USBOTGWATCHER]\tCUsbMessageNotificationObserver::RunL Session Requested" ) );
       
   198 
       
   199             for (TInt i(0); i < iObservers.Count(); ++i)
       
   200                 {
       
   201                 iObservers[i]->SessionRequestedL();
       
   202                 }
       
   203             break;
       
   204             }
       
   205             // notify states with other messages  
       
   206         default:
       
   207             {
       
   208             for (TInt i(0); i < iObservers.Count(); ++i)
       
   209                 {
       
   210                 iObservers[i]->MessageNotificationReceivedL(message);
       
   211                 }
       
   212             break;
       
   213             }
       
   214 
       
   215         }
       
   216 
       
   217     }
       
   218 
       
   219 // ---------------------------------------------------------------------------
       
   220 // 
       
   221 // ---------------------------------------------------------------------------
       
   222 //
       
   223 void CUsbMessageNotificationObserver::DoCancel()
       
   224     {
       
   225     iUsb->MessageNotificationCancel();
       
   226     }
       
   227 
       
   228 // ---------------------------------------------------------------------------
       
   229 // 
       
   230 // ---------------------------------------------------------------------------
       
   231 //
       
   232 TInt CUsbMessageNotificationObserver::RunError(TInt aError)
       
   233     {
       
   234 
       
   235         FTRACE( FPrint(_L( "[USBOTGWATCHER]\tCUsbMessageNotificationObserver::RunError aError = %d" ), aError));
       
   236 
       
   237     // try to recover and continue	
       
   238     return KErrNone;
       
   239 
       
   240     }
       
   241