usbengines/usbotgwatcher/src/cusbmessagenotificationobserver.cpp
branchRCL_3
changeset 5 7068aba64af5
parent 0 1e05558e2206
child 6 a15c582fbf97
equal deleted inserted replaced
4:7e15987c4500 5:7068aba64af5
    20 #include <d32usbdi_errors.h>
    20 #include <d32usbdi_errors.h>
    21 #include <d32otgdi_errors.h>
    21 #include <d32otgdi_errors.h>
    22 
    22 
    23 #include "cusbmessagenotificationobserver.h"
    23 #include "cusbmessagenotificationobserver.h"
    24 
    24 
    25 #include "definitions.h"
    25 #include "definitions.h" 
    26 #include "debug.h"
    26 #include "debug.h"
    27 #include "panic.h"
    27 #include "panic.h"
    28 
    28 
    29 // ---------------------------------------------------------------------------
    29 // ---------------------------------------------------------------------------
    30 // 
    30 // 
    83 // ---------------------------------------------------------------------------
    83 // ---------------------------------------------------------------------------
    84 // 
    84 // 
    85 // ---------------------------------------------------------------------------
    85 // ---------------------------------------------------------------------------
    86 //
    86 //
    87 void CUsbMessageNotificationObserver::SubscribeL(
    87 void CUsbMessageNotificationObserver::SubscribeL(
    88         MUsbMessageNotificationObserver* aObserver)
    88         MUsbMessageNotificationObserver& aObserver)
    89     {
    89     {
    90         FLOG( _L( "[USBOTGWATCHER]\tCUsbMessageNotificationObserver::SubscribeL" ) );
    90         FLOG( _L( "[USBOTGWATCHER]\tCUsbMessageNotificationObserver::SubscribeL" ) );
    91 
    91 
    92     User::LeaveIfError(iObservers.Append(aObserver));
    92         // check if the same observer already exist in a list
       
    93         if(KErrNotFound != iObservers.Find(&aObserver))
       
    94             {
       
    95             FLOG( _L( "[USBOTGWATCHER]\tCUsbMessageNotificationObserver::SubscribeL Observer already exists." ) );
       
    96             Panic(EObserverAlreadyExists);
       
    97             return;
       
    98             }
       
    99         iObservers.AppendL(&aObserver);
    93 
   100 
    94     if (KFirst == iObservers.Count()) // first item
   101     if (KFirst == iObservers.Count()) // first item
    95         {
   102         {
    96         iUsb->MessageNotification(iStatus, iMessage);
   103         iUsb->MessageNotification(iStatus, iMessage);
    97         SetActive();
   104         SetActive();
   101 // ---------------------------------------------------------------------------
   108 // ---------------------------------------------------------------------------
   102 // 
   109 // 
   103 // ---------------------------------------------------------------------------
   110 // ---------------------------------------------------------------------------
   104 //
   111 //
   105 void CUsbMessageNotificationObserver::UnsubscribeL(
   112 void CUsbMessageNotificationObserver::UnsubscribeL(
   106         MUsbMessageNotificationObserver* aObserver)
   113         MUsbMessageNotificationObserver& aObserver)
   107     {
   114     {
   108         FLOG( _L( "[USBOTGWATCHER]\tCUsbMessageNotificationObserver::UnsubscribeL" ) );
   115         FLOG( _L( "[USBOTGWATCHER]\tCUsbMessageNotificationObserver::UnsubscribeL" ) );
   109 
   116 
   110         if (0 == iObservers.Count()) // no items
   117         TInt i(iObservers.Find(&aObserver));
   111             {
   118         if(KErrNotFound == i)
   112             FLOG( _L( "[USBOTGWATCHER]\tCUsbMessageNotificationObserver::UnsubscribeL No observers" ) );
   119             {
       
   120             FLOG( _L( "[USBOTGWATCHER]\tCUsbIdPinObserver::UnsubscribeL Observer not found." ) );
       
   121             Panic(ECanNotFindMessageNotificationObserver);
   113             return;
   122             return;
   114             }
   123             }
   115         
   124         
   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);
   125         iObservers.Remove(i);
   123         }
       
   124     else
       
   125         {
       
   126             FLOG( _L( "[USBOTGWATCHER]\tCUsbMessageNotificationObserver::UnsubscribeL CanNotFindMessageNotificationObserver" ) );
       
   127         Panic(ECanNotFindMessageNotificationObserver);
       
   128         }
       
   129 
   126 
   130     if (0 == iObservers.Count()) // no items
   127     if (0 == iObservers.Count()) // no items
   131         {
   128         {
   132         // cancel pending request
   129         // cancel pending request
   133         Cancel();
   130         Cancel();