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