usbengines/usbotgwatcher/src/cusbotgwatcher.cpp
branchRCL_3
changeset 21 ff9df6630274
parent 20 a15c582fbf97
child 74 830546d5951a
child 79 25fce757be94
equal deleted inserted replaced
20:a15c582fbf97 21:ff9df6630274
     1 /*
     1 /*
     2  * Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
     2  * Copyright (c) 2008 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".
    62 #ifdef _DEBUG
    62 #ifdef _DEBUG
    63     SelfTestL();
    63     SelfTestL();
    64 #endif
    64 #endif
    65 
    65 
    66 #ifndef STIF
    66 #ifndef STIF
    67     User::LeaveIfError(RProperty::Define(KPSUidUsbWatcher,
    67     LEAVEIFERROR(RProperty::Define(KPSUidUsbWatcher,
    68             KUsbWatcherIsPeripheralConnected, RProperty::EInt,
    68                     KUsbWatcherIsPeripheralConnected, RProperty::EInt,
    69             KAlwaysPassPolicy, KLocalServicesPolicy));
    69                     KAlwaysPassPolicy, KLocalServicesPolicy));
    70 
    70 
    71     User::LeaveIfError(RProperty::Set(KPSUidUsbWatcher,
    71     LEAVEIFERROR(RProperty::Set(KPSUidUsbWatcher,
    72             KUsbWatcherIsPeripheralConnected,
    72                     KUsbWatcherIsPeripheralConnected,
    73             KUsbWatcherPeripheralIsNotConnected));
    73                     KUsbWatcherPeripheralIsNotConnected));
    74 #endif
    74 #endif
    75 
    75 
    76     iUsbServiceControl = CUsbServiceControl::NewL(*this, iUsb);
    76     iUsbServiceControl = CUsbServiceControl::NewL(*this, iUsb);
    77 
    77 
    78     User::LeaveIfError(iStates.Append(CUsbStateHostUndefined::NewL(*this)));
    78     LEAVEIFERROR(iStates.Append(CUsbStateHostUndefined::NewL(*this)));
    79     User::LeaveIfError(iStates.Append(CUsbStateHostAInitiate::NewL(*this)));
    79     LEAVEIFERROR(iStates.Append(CUsbStateHostAInitiate::NewL(*this)));
    80     User::LeaveIfError(iStates.Append(CUsbStateHostAHost::NewL(*this)));
    80     LEAVEIFERROR(iStates.Append(CUsbStateHostAHost::NewL(*this)));
    81     User::LeaveIfError(iStates.Append(CUsbStateHostAPeripheral::NewL(*this)));
    81     LEAVEIFERROR(iStates.Append(CUsbStateHostAPeripheral::NewL(*this)));
    82 
    82 
    83     User::LeaveIfError(iStates.Append(CUsbStateHostDelayAttachedHandle::NewL(
    83     LEAVEIFERROR(iStates.Append(CUsbStateHostDelayAttachedHandle::NewL(
    84             *this)));
    84                             *this)));
    85     User::LeaveIfError(iStates.Append(
    85     LEAVEIFERROR(iStates.Append(
    86             CUsbStateHostDelayNotAttachedHandle::NewL(*this)));
    86                     CUsbStateHostDelayNotAttachedHandle::NewL(*this)));
    87     User::LeaveIfError(iStates.Append(
    87     LEAVEIFERROR(iStates.Append(
    88             CUsbStateHostHandleDropping::NewL(*this)));
    88                     CUsbStateHostHandleDropping::NewL(*this)));
    89 
    89 
    90     iIdPinObserver = CUsbIdPinObserver::NewL();
    90     iIdPinObserver = CUsbIdPinObserver::NewL();
    91     iVBusObserver = CUsbVBusObserver::NewL();
    91     iVBusObserver = CUsbVBusObserver::NewL();
    92     iOtgStateObserver = CUsbOtgStateObserver::NewL();
    92     iOtgStateObserver = CUsbOtgStateObserver::NewL();
    93     iBusActivityObserver = CUsbBusActivityObserver::NewL();
    93     iBusActivityObserver = CUsbBusActivityObserver::NewL();
   120 // 
   120 // 
   121 // ---------------------------------------------------------------------------
   121 // ---------------------------------------------------------------------------
   122 //
   122 //
   123 EXPORT_C TBool CUsbOtgWatcher::IsDeviceA()
   123 EXPORT_C TBool CUsbOtgWatcher::IsDeviceA()
   124     {
   124     {
   125     __ASSERT_DEBUG(iIdPinObserver != NULL, Panic(EIdPinObserverNULLPointer));
   125     ASSERT_PANIC(iIdPinObserver != NULL, EIdPinObserverNULLPointer);
   126     return (iIdPinObserver->IdPin() == CUsbIdPinObserver::EIdPinOn
   126     return (iIdPinObserver->IdPin() == CUsbIdPinObserver::EIdPinOn
   127                                                                    ? ETrue
   127                                                                    ? ETrue
   128                                                                       : EFalse);
   128                                                                       : EFalse);
   129     }
   129     }
   130 
   130 
   354     LOG1( "iUsbServiceControl->Stop() err = %d", err );
   354     LOG1( "iUsbServiceControl->Stop() err = %d", err );
   355 
   355 
   356     if (KErrNone != err)
   356     if (KErrNone != err)
   357         {
   357         {
   358         LOG( "ErrorStoppingUsbServices" );
   358         LOG( "ErrorStoppingUsbServices" );
   359         Panic(ECanNotStopUsbServices);
   359         PANIC(ECanNotStopUsbServices);
   360         }
   360         }
   361     }
   361     }
   362 
   362 
   363 // ---------------------------------------------------------------------------
   363 // ---------------------------------------------------------------------------
   364 // 
   364 // 
   365 // ---------------------------------------------------------------------------
   365 // ---------------------------------------------------------------------------
   366 //
   366 //
   367 void CUsbOtgWatcher::IdPinErrorL(TInt aError)
   367 void CUsbOtgWatcher::IdPinErrorL(TInt aError)
   368     {
   368     {
   369     __ASSERT_DEBUG(iHostState != NULL, Panic(EBadHostState));
   369     ASSERT_PANIC(iHostState != NULL, EBadHostState);
   370     HandleHostProblemL(EUsbWatcherIdPinError, EUsbStateHostHandleDropping);
   370     HandleHostProblemL(EUsbWatcherIdPinError, EUsbStateHostHandleDropping);
   371 
   371 
   372     }
   372     }
   373 
   373 
   374 // From VBus observer
   374 // From VBus observer
   376 // 
   376 // 
   377 // ---------------------------------------------------------------------------
   377 // ---------------------------------------------------------------------------
   378 //
   378 //
   379 void CUsbOtgWatcher::VBusDownL()
   379 void CUsbOtgWatcher::VBusDownL()
   380     {
   380     {
   381     __ASSERT_DEBUG(iHostState != NULL, Panic(EBadHostState));
   381     ASSERT_PANIC(iHostState != NULL, EBadHostState);
   382     iHostState->VBusDownL();
   382     iHostState->VBusDownL();
   383     }
   383     }
   384 
   384 
   385 // ---------------------------------------------------------------------------
   385 // ---------------------------------------------------------------------------
   386 // 
   386 // 
   387 // ---------------------------------------------------------------------------
   387 // ---------------------------------------------------------------------------
   388 //
   388 //
   389 void CUsbOtgWatcher::VBusUpL()
   389 void CUsbOtgWatcher::VBusUpL()
   390     {
   390     {
   391     __ASSERT_DEBUG(iHostState != NULL, Panic(EBadHostState));
   391     ASSERT_PANIC(iHostState != NULL, EBadHostState);
   392     iHostState->VBusUpL();
   392     iHostState->VBusUpL();
   393     }
   393     }
   394 
   394 
   395 // ---------------------------------------------------------------------------
   395 // ---------------------------------------------------------------------------
   396 // 
   396 // 
   397 // ---------------------------------------------------------------------------
   397 // ---------------------------------------------------------------------------
   398 //
   398 //
   399 void CUsbOtgWatcher::VBusObserverErrorL(TInt aError)
   399 void CUsbOtgWatcher::VBusObserverErrorL(TInt aError)
   400     {
   400     {
   401     __ASSERT_DEBUG(iHostState != NULL, Panic(EBadHostState));
   401     ASSERT_PANIC(iHostState != NULL, EBadHostState);
   402     HandleHostProblemL(EUsbWatcherVBusObserverError,
   402     HandleHostProblemL(EUsbWatcherVBusObserverError,
   403             EUsbStateHostHandleDropping);
   403             EUsbStateHostHandleDropping);
   404     }
   404     }
   405 
   405 
   406 // From OTG state observer
   406 // From OTG state observer
   408 // 
   408 // 
   409 // ---------------------------------------------------------------------------
   409 // ---------------------------------------------------------------------------
   410 //
   410 //
   411 void CUsbOtgWatcher::AIdleL()
   411 void CUsbOtgWatcher::AIdleL()
   412     {
   412     {
   413     __ASSERT_DEBUG(iHostState != NULL, Panic(EBadHostState));
   413     ASSERT_PANIC(iHostState != NULL, EBadHostState);
   414     iHostState->AIdleL();
   414     iHostState->AIdleL();
   415     }
   415     }
   416 
   416 
   417 // ---------------------------------------------------------------------------
   417 // ---------------------------------------------------------------------------
   418 // 
   418 // 
   419 // ---------------------------------------------------------------------------
   419 // ---------------------------------------------------------------------------
   420 //
   420 //
   421 void CUsbOtgWatcher::AHostL()
   421 void CUsbOtgWatcher::AHostL()
   422     {
   422     {
   423     __ASSERT_DEBUG(iHostState != NULL, Panic(EBadHostState));
   423     ASSERT_PANIC(iHostState != NULL, EBadHostState);
   424     iHostState->AHostL();
   424     iHostState->AHostL();
   425     }
   425     }
   426 
   426 
   427 // ---------------------------------------------------------------------------
   427 // ---------------------------------------------------------------------------
   428 // 
   428 // 
   429 // ---------------------------------------------------------------------------
   429 // ---------------------------------------------------------------------------
   430 //
   430 //
   431 void CUsbOtgWatcher::APeripheralL()
   431 void CUsbOtgWatcher::APeripheralL()
   432     {
   432     {
   433     __ASSERT_DEBUG(iHostState != NULL, Panic(EBadHostState));
   433     ASSERT_PANIC(iHostState != NULL, EBadHostState);
   434     iHostState->APeripheralL();
   434     iHostState->APeripheralL();
   435     }
   435     }
   436 
   436 
   437 // ---------------------------------------------------------------------------
   437 // ---------------------------------------------------------------------------
   438 // 
   438 // 
   439 // ---------------------------------------------------------------------------
   439 // ---------------------------------------------------------------------------
   440 //
   440 //
   441 void CUsbOtgWatcher::AVBusErrorL()
   441 void CUsbOtgWatcher::AVBusErrorL()
   442     {
   442     {
   443     __ASSERT_DEBUG(iHostState != NULL, Panic(EBadHostState));
   443     ASSERT_PANIC(iHostState != NULL, EBadHostState);
   444     iHostState->AVBusErrorL();
   444     iHostState->AVBusErrorL();
   445     }
   445     }
   446 
   446 
   447 // ---------------------------------------------------------------------------
   447 // ---------------------------------------------------------------------------
   448 // 
   448 // 
   449 // ---------------------------------------------------------------------------
   449 // ---------------------------------------------------------------------------
   450 //
   450 //
   451 void CUsbOtgWatcher::BIdleL()
   451 void CUsbOtgWatcher::BIdleL()
   452     {
   452     {
   453     __ASSERT_DEBUG(iHostState != NULL, Panic(EBadHostState));
   453     ASSERT_PANIC(iHostState != NULL, EBadHostState);
   454     iHostState->BIdleL();
   454     iHostState->BIdleL();
   455     }
   455     }
   456 
   456 
   457 // ---------------------------------------------------------------------------
   457 // ---------------------------------------------------------------------------
   458 // 
   458 // 
   459 // ---------------------------------------------------------------------------
   459 // ---------------------------------------------------------------------------
   460 //
   460 //
   461 void CUsbOtgWatcher::BPeripheralL()
   461 void CUsbOtgWatcher::BPeripheralL()
   462     {
   462     {
   463     __ASSERT_DEBUG(iHostState != NULL, Panic(EBadHostState));
   463     ASSERT_PANIC(iHostState != NULL, EBadHostState);
   464     iHostState->BPeripheralL();
   464     iHostState->BPeripheralL();
   465     }
   465     }
   466 
   466 
   467 // ---------------------------------------------------------------------------
   467 // ---------------------------------------------------------------------------
   468 // 
   468 // 
   469 // ---------------------------------------------------------------------------
   469 // ---------------------------------------------------------------------------
   470 //
   470 //
   471 void CUsbOtgWatcher::BHostL()
   471 void CUsbOtgWatcher::BHostL()
   472     {
   472     {
   473     __ASSERT_DEBUG(iHostState != NULL, Panic(EBadHostState));
   473     ASSERT_PANIC(iHostState != NULL, EBadHostState);
   474     iHostState->BHostL();
   474     iHostState->BHostL();
   475     }
   475     }
   476 
   476 
   477 // ---------------------------------------------------------------------------
   477 // ---------------------------------------------------------------------------
   478 // 
   478 // 
   479 // ---------------------------------------------------------------------------
   479 // ---------------------------------------------------------------------------
   480 //
   480 //
   481 void CUsbOtgWatcher::OtgStateErrorL(TInt aError)
   481 void CUsbOtgWatcher::OtgStateErrorL(TInt aError)
   482     {
   482     {
   483     __ASSERT_DEBUG(iHostState != NULL, Panic(EBadHostState));
   483     ASSERT_PANIC(iHostState != NULL, EBadHostState);
   484     HandleHostProblemL(EUsbWatcherOtgStateError, EUsbStateHostHandleDropping);
   484     HandleHostProblemL(EUsbWatcherOtgStateError, EUsbStateHostHandleDropping);
   485     }
   485     }
   486 
   486 
   487 // From bus activity observer
   487 // From bus activity observer
   488 // ---------------------------------------------------------------------------
   488 // ---------------------------------------------------------------------------
   489 // 
   489 // 
   490 // ---------------------------------------------------------------------------
   490 // ---------------------------------------------------------------------------
   491 //
   491 //
   492 void CUsbOtgWatcher::BusIdleL()
   492 void CUsbOtgWatcher::BusIdleL()
   493     {
   493     {
   494     __ASSERT_DEBUG(iHostState != NULL, Panic(EBadHostState));
   494     ASSERT_PANIC(iHostState != NULL, EBadHostState);
   495     iHostState->BusIdleL();
   495     iHostState->BusIdleL();
   496     }
   496     }
   497 
   497 
   498 // ---------------------------------------------------------------------------
   498 // ---------------------------------------------------------------------------
   499 // 
   499 // 
   500 // ---------------------------------------------------------------------------
   500 // ---------------------------------------------------------------------------
   501 //
   501 //
   502 void CUsbOtgWatcher::BusActiveL()
   502 void CUsbOtgWatcher::BusActiveL()
   503     {
   503     {
   504     __ASSERT_DEBUG(iHostState != NULL, Panic(EBadHostState));
   504     ASSERT_PANIC(iHostState != NULL, EBadHostState);
   505     iHostState->BusActiveL();
   505     iHostState->BusActiveL();
   506     }
   506     }
   507 
   507 
   508 // ---------------------------------------------------------------------------
   508 // ---------------------------------------------------------------------------
   509 // 
   509 // 
   510 // ---------------------------------------------------------------------------
   510 // ---------------------------------------------------------------------------
   511 //
   511 //
   512 void CUsbOtgWatcher::BusActivityErrorL(TInt aError)
   512 void CUsbOtgWatcher::BusActivityErrorL(TInt aError)
   513     {
   513     {
   514     __ASSERT_DEBUG(iHostState != NULL, Panic(EBadHostState));
   514     ASSERT_PANIC(iHostState != NULL, EBadHostState);
   515     // no action, continue
   515     // no action, continue
   516     }
   516     }
   517 
   517 
   518 // From Host Event notification observer
   518 // From Host Event notification observer
   519 // ---------------------------------------------------------------------------
   519 // ---------------------------------------------------------------------------
   520 // 
   520 // 
   521 // ---------------------------------------------------------------------------
   521 // ---------------------------------------------------------------------------
   522 //
   522 //
   523 void CUsbOtgWatcher::DeviceAttachedL(TDeviceEventInformation aTdi)
   523 void CUsbOtgWatcher::DeviceAttachedL(TDeviceEventInformation aTdi)
   524     {
   524     {
   525     __ASSERT_DEBUG(iHostState != NULL, Panic(EBadHostState));
   525     ASSERT_PANIC(iHostState != NULL, EBadHostState);
   526     iHostState->DeviceAttachedL(aTdi);
   526     iHostState->DeviceAttachedL(aTdi);
   527     }
   527     }
   528 
   528 
   529 // ---------------------------------------------------------------------------
   529 // ---------------------------------------------------------------------------
   530 // 
   530 // 
   531 // ---------------------------------------------------------------------------
   531 // ---------------------------------------------------------------------------
   532 //
   532 //
   533 void CUsbOtgWatcher::DeviceDetachedL(TDeviceEventInformation aTdi)
   533 void CUsbOtgWatcher::DeviceDetachedL(TDeviceEventInformation aTdi)
   534     {
   534     {
   535     __ASSERT_DEBUG(iHostState != NULL, Panic(EBadHostState));
   535     ASSERT_PANIC(iHostState != NULL, EBadHostState);
   536     iHostState->DeviceDetachedL(aTdi);
   536     iHostState->DeviceDetachedL(aTdi);
   537     }
   537     }
   538 
   538 
   539 // ---------------------------------------------------------------------------
   539 // ---------------------------------------------------------------------------
   540 // 
   540 // 
   541 // ---------------------------------------------------------------------------
   541 // ---------------------------------------------------------------------------
   542 //
   542 //
   543 void CUsbOtgWatcher::DriverLoadSuccessL(TDeviceEventInformation aTdi)
   543 void CUsbOtgWatcher::DriverLoadSuccessL(TDeviceEventInformation aTdi)
   544     {
   544     {
   545     __ASSERT_DEBUG(iHostState != NULL, Panic(EBadHostState));
   545     ASSERT_PANIC(iHostState != NULL, EBadHostState);
   546     iHostState->DriverLoadSuccessL(aTdi);
   546     iHostState->DriverLoadSuccessL(aTdi);
   547     }
   547     }
   548 
   548 
   549 // ---------------------------------------------------------------------------
   549 // ---------------------------------------------------------------------------
   550 // 
   550 // 
   551 // ---------------------------------------------------------------------------
   551 // ---------------------------------------------------------------------------
   552 //
   552 //
   553 void CUsbOtgWatcher::DriverLoadPartialSuccessL(TDeviceEventInformation aTdi)
   553 void CUsbOtgWatcher::DriverLoadPartialSuccessL(TDeviceEventInformation aTdi)
   554     {
   554     {
   555     __ASSERT_DEBUG(iHostState != NULL, Panic(EBadHostState));
   555     ASSERT_PANIC(iHostState != NULL, EBadHostState);
   556     iHostState->DriverLoadPartialSuccessL(aTdi);
   556     iHostState->DriverLoadPartialSuccessL(aTdi);
   557     }
   557     }
   558 
   558 
   559 // ---------------------------------------------------------------------------
   559 // ---------------------------------------------------------------------------
   560 // 
   560 // 
   561 // ---------------------------------------------------------------------------
   561 // ---------------------------------------------------------------------------
   562 //
   562 //
   563 void CUsbOtgWatcher::DriverLoadFailureL(TDeviceEventInformation aTdi)
   563 void CUsbOtgWatcher::DriverLoadFailureL(TDeviceEventInformation aTdi)
   564     {
   564     {
   565     __ASSERT_DEBUG(iHostState != NULL, Panic(EBadHostState));
   565     ASSERT_PANIC(iHostState != NULL, EBadHostState);
   566     iHostState->DriverLoadFailureL(aTdi);
   566     iHostState->DriverLoadFailureL(aTdi);
   567     }
   567     }
   568 
   568 
   569 // ---------------------------------------------------------------------------
   569 // ---------------------------------------------------------------------------
   570 // 
   570 // 
   571 // ---------------------------------------------------------------------------
   571 // ---------------------------------------------------------------------------
   572 //
   572 //
   573 void CUsbOtgWatcher::HostEventNotificationErrorL(TInt aError)
   573 void CUsbOtgWatcher::HostEventNotificationErrorL(TInt aError)
   574     {
   574     {
   575     __ASSERT_DEBUG(iHostState != NULL, Panic(EBadHostState));
   575     ASSERT_PANIC(iHostState != NULL, EBadHostState);
   576     HandleHostProblemL(EUsbWatcherHostEventNotificationError,
   576     HandleHostProblemL(EUsbWatcherHostEventNotificationError,
   577             EUsbStateHostHandleDropping);
   577             EUsbStateHostHandleDropping);
   578     }
   578     }
   579 
   579 
   580 // From message notification observer
   580 // From message notification observer
   582 // 
   582 // 
   583 // ---------------------------------------------------------------------------
   583 // ---------------------------------------------------------------------------
   584 //
   584 //
   585 void CUsbOtgWatcher::MessageNotificationReceivedL(TInt aMessage)
   585 void CUsbOtgWatcher::MessageNotificationReceivedL(TInt aMessage)
   586     {
   586     {
   587     __ASSERT_DEBUG(iHostState != NULL, Panic(EBadHostState));
   587     ASSERT_PANIC(iHostState != NULL, EBadHostState);
   588     iHostState->MessageNotificationReceivedL(aMessage);
   588     iHostState->MessageNotificationReceivedL(aMessage);
   589     }
   589     }
   590 
   590 
   591 // ---------------------------------------------------------------------------
   591 // ---------------------------------------------------------------------------
   592 // 
   592 // 
   593 // ---------------------------------------------------------------------------
   593 // ---------------------------------------------------------------------------
   594 //
   594 //
   595 void CUsbOtgWatcher::BadHubPositionL()
   595 void CUsbOtgWatcher::BadHubPositionL()
   596     {
   596     {
   597     __ASSERT_DEBUG(iHostState != NULL, Panic(EBadHostState));
   597     ASSERT_PANIC(iHostState != NULL, EBadHostState);
   598     iHostState->BadHubPositionL();
   598     iHostState->BadHubPositionL();
   599     }
   599     }
   600 
   600 
   601 // ---------------------------------------------------------------------------
   601 // ---------------------------------------------------------------------------
   602 // 
   602 // 
   603 // ---------------------------------------------------------------------------
   603 // ---------------------------------------------------------------------------
   604 //
   604 //
   605 void CUsbOtgWatcher::VBusErrorL()
   605 void CUsbOtgWatcher::VBusErrorL()
   606     {
   606     {
   607     __ASSERT_DEBUG(iHostState != NULL, Panic(EBadHostState));
   607     ASSERT_PANIC(iHostState != NULL, EBadHostState);
   608     iHostState->VBusErrorL();
   608     iHostState->VBusErrorL();
   609     }
   609     }
   610 
   610 
   611 // ---------------------------------------------------------------------------
   611 // ---------------------------------------------------------------------------
   612 // 
   612 // 
   613 // ---------------------------------------------------------------------------
   613 // ---------------------------------------------------------------------------
   614 //
   614 //
   615 void CUsbOtgWatcher::SrpReceivedL()
   615 void CUsbOtgWatcher::SrpReceivedL()
   616     {
   616     {
   617     __ASSERT_DEBUG(iHostState != NULL, Panic(EBadHostState));
   617     ASSERT_PANIC(iHostState != NULL, EBadHostState);
   618     iHostState->SrpReceivedL();
   618     iHostState->SrpReceivedL();
   619     }
   619     }
   620 
   620 
   621 // ---------------------------------------------------------------------------
   621 // ---------------------------------------------------------------------------
   622 // 
   622 // 
   623 // ---------------------------------------------------------------------------
   623 // ---------------------------------------------------------------------------
   624 //
   624 //
   625 void CUsbOtgWatcher::SessionRequestedL()
   625 void CUsbOtgWatcher::SessionRequestedL()
   626     {
   626     {
   627     __ASSERT_DEBUG(iHostState != NULL, Panic(EBadHostState));
   627     ASSERT_PANIC(iHostState != NULL, EBadHostState);
   628     iHostState->SessionRequestedL();
   628     iHostState->SessionRequestedL();
   629     }
   629     }
   630 
   630 
   631 // ---------------------------------------------------------------------------
   631 // ---------------------------------------------------------------------------
   632 // 
   632 // 
   633 // ---------------------------------------------------------------------------
   633 // ---------------------------------------------------------------------------
   634 //
   634 //
   635 void CUsbOtgWatcher::MessageNotificationErrorL(TInt aError)
   635 void CUsbOtgWatcher::MessageNotificationErrorL(TInt aError)
   636     {
   636     {
   637     __ASSERT_DEBUG(iHostState != NULL, Panic(EBadHostState));
   637     ASSERT_PANIC(iHostState != NULL, EBadHostState);
   638     HandleHostProblemL(EUsbWatcherMessageNotificationError,
   638     HandleHostProblemL(EUsbWatcherMessageNotificationError,
   639             EUsbStateHostHandleDropping);
   639             EUsbStateHostHandleDropping);
   640     }
   640     }
   641 
   641 
   642 // ---------------------------------------------------------------------------
   642 // ---------------------------------------------------------------------------
   735 // 
   735 // 
   736 // ---------------------------------------------------------------------------
   736 // ---------------------------------------------------------------------------
   737 //
   737 //
   738 CUsbStateHostHandle* CUsbOtgWatcher::HostHandle(TUsbStateIds aInState) const
   738 CUsbStateHostHandle* CUsbOtgWatcher::HostHandle(TUsbStateIds aInState) const
   739     {
   739     {
   740     __ASSERT_DEBUG(iStates[aInState] != NULL, Panic(EBadState));
   740     ASSERT_PANIC(iStates[aInState] != NULL, EBadState);
   741 
   741 
   742     return (CUsbStateHostHandle*) iStates[aInState];
   742     return (CUsbStateHostHandle*) iStates[aInState];
   743     }
   743     }
   744 
   744 
   745 // ---------------------------------------------------------------------------
   745 // ---------------------------------------------------------------------------
   758         }
   758         }
   759 
   759 
   760     // sets new state	
   760     // sets new state	
   761 
   761 
   762     // be careful to add states into States in exactly the same order as they enumerated
   762     // be careful to add states into States in exactly the same order as they enumerated
   763     // this method will work right as long as states, which in the enumeration, is added to the list iStates in the same order as they enumerated
   763     // this method will work right as long as states, which in the enumeration, is added 
   764     // and no one state is added, if all previous states are not added. no need to add all states, but if added - previous all must be added.
   764     // to the list iStates in the same order as they enumerated and no one state is added,
       
   765     // if all previous states are not added. no need to add all states, but if added - 
       
   766     // previous all must be added.
   765     // For the general case, when some states added, some not, this method has to implement search by state ID. for this CUsbOtgWatcher::Id() is maintained.
   767     // For the general case, when some states added, some not, this method has to implement search by state ID. for this CUsbOtgWatcher::Id() is maintained.
   766 
   768 
   767     iState = iStates[aNewStateId];
   769     iState = iStates[aNewStateId];
   768 
   770 
   769     __ASSERT_DEBUG(iState != NULL, Panic(EBadState));
   771     ASSERT_PANIC(iState != NULL, EBadState);
   770 
   772 
   771     iState->JustAdvancedToThisStateL(); // do any initial activity, once advanced to the state
   773     iState->JustAdvancedToThisStateL(); // do any initial activity, once advanced to the state
   772 
   774 
   773     }
   775     }
   774 
   776 
   788         }
   790         }
   789 
   791 
   790     // set new state	
   792     // set new state	
   791 
   793 
   792     // be careful to add states into States in exactly the same order as they enumerated
   794     // be careful to add states into States in exactly the same order as they enumerated
   793     // this method will work right as long as states, which in the enumeration, is added to the list iStates in the same order as they enumerated
   795     // this method will work right as long as states, which in the enumeration, is added 
   794     // and no one state is added, if all previous states are not added. no need to add all states, but if added - previous all must be added.
   796     // to the list iStates in the same order as they enumerated and no one state is added,
   795     // For the general case, when some states added, some not, this method has to implement search by state Id. for this CUsbOtgWatcher::Id() is maintained.
   797     // if all previous states are not added. no need to add all states, but if added - 
       
   798     // previous all must be added.
       
   799     // For the general case, when some states added, some not, this method has to implement search by state ID. for this CUsbOtgWatcher::Id() is maintained.
   796 
   800 
   797     iHostState = iStates[aNewStateId];
   801     iHostState = iStates[aNewStateId];
   798 
   802 
   799     __ASSERT_DEBUG(iHostState != NULL, Panic(EBadState));
   803     ASSERT_PANIC(iHostState != NULL, EBadState)
   800 
   804 
   801     iHostState->JustAdvancedToThisStateL(); // checks if there are conditions for advancing to another state(s)
   805     iHostState->JustAdvancedToThisStateL(); // checks if there are conditions for advancing to another state(s)
   802 
   806 
   803     // notify state change to observers
   807     // notify state change to observers
   804     for (TInt i(0); i < iOtgStateObservers.Count(); ++i)
   808     for (TInt i(0); i < iOtgStateObservers.Count(); ++i)
   855     {
   859     {
   856     LOG_FUNC
   860     LOG_FUNC
   857 
   861 
   858     LOG1( "aError = %d" , aError);
   862     LOG1( "aError = %d" , aError);
   859 
   863 
       
   864     iUsbServiceRequest = CUsbServiceControl::ERequestUndefined;
       
   865 
   860     switch (aError)
   866     switch (aError)
   861         {
   867         {
   862         case KErrInUse:
   868         case KErrInUse:
   863             // usb services already started (this might happen if more than one idpin on event come)
   869             // usb services already started (this might happen if more than one idpin on event come)
   864             {
   870             {
   867 
   873 
   868         case KErrNone:
   874         case KErrNone:
   869             {
   875             {
   870             break; // do normal routine
   876             break; // do normal routine
   871             }
   877             }
   872 
   878         case KErrNoMemory:
   873         default: // handle the issue
   879             {
       
   880             HandleHostProblemL(EUsbWatcherNoMemory,
       
   881                     EUsbStateHostHandleDropping);
       
   882             return;
       
   883             }
       
   884 
       
   885         default:
       
   886             // handle the issue
   874             {
   887             {
   875             if (iUsbServiceRequest == CUsbServiceControl::EStartUsbService) // Handle only start issues
   888             if (iUsbServiceRequest == CUsbServiceControl::EStartUsbService) // Handle only start issues
   876                 {
   889                 {
   877                 HandleHostProblemL(EUsbWatcherCanNotStartUsbServices,
   890                 HandleHostProblemL(EUsbWatcherNoMemory,
   878                         EUsbStateHostHandleDropping);
   891                         EUsbStateHostHandleDropping);
   879                 }
   892                 }
   880 
   893 
   881             iUsbServiceRequest = CUsbServiceControl::ERequestUndefined;
   894             iUsbServiceRequest = CUsbServiceControl::ERequestUndefined;
   882             return;
   895             return;
   897         }
   910         }
   898 
   911 
   899     switch (serviceState)
   912     switch (serviceState)
   900         {
   913         {
   901         case EUsbServiceIdle: // just stopped usb service
   914         case EUsbServiceIdle: // just stopped usb service
       
   915 
   902             {
   916             {
   903             LOG("UsbServiceState == EUsbServiceIdle" );
   917             LOG("UsbServiceState == EUsbServiceIdle" );
   904             // do nothing
   918             // do nothing
   905             break;
   919             break;
   906             }
   920             }
   907 
   921 
   908         case EUsbServiceStarted: // just started usb service
   922         case EUsbServiceStarted: // just started usb service
       
   923 
   909             {
   924             {
   910             LOG( "UsbServiceState == EUsbServiceStarted" );
   925             LOG( "UsbServiceState == EUsbServiceStarted" );
   911 
   926 
   912             ChangeHostStateL(EUsbStateHostAInitiate);
   927             ChangeHostStateL(EUsbStateHostAInitiate);
   913 
   928 
   916         case EUsbServiceStarting:
   931         case EUsbServiceStarting:
   917             {
   932             {
   918             LOG("UsbServiceState == EUsbServiceStarting" );
   933             LOG("UsbServiceState == EUsbServiceStarting" );
   919             // should not receive that, due to call back is called when service stopped or started
   934             // should not receive that, due to call back is called when service stopped or started
   920             // therefore scream
   935             // therefore scream
   921             // no break statement here
   936             PANIC(EUnexpectedUsbServiceState);
       
   937             break;
   922             }
   938             }
   923         case EUsbServiceStopping:
   939         case EUsbServiceStopping:
   924             {
   940             {
   925             LOG("UsbServiceState == EUsbServiceStopping" );
   941             LOG("UsbServiceState == EUsbServiceStopping" );
   926             // should not receive that, due to call back is called when service stopped or started
   942             // should not receive that, due to call back is called when service stopped or started
   927             // therefore scream
   943             // therefore scream
   928             // no break statement here
   944             PANIC(EUnexpectedUsbServiceState);
       
   945             break;
   929             }
   946             }
   930         case EUsbServiceFatalError:
   947         case EUsbServiceFatalError:
   931             {
   948             {
   932             LOG( "UsbServiceState == EUsbServiceFatalError" );
   949             LOG( "UsbServiceState == EUsbServiceFatalError" );
   933             Panic(EUnexpectedUsbServiceState);
   950             PANIC(EUnexpectedUsbServiceState);
   934             break;
   951             break;
   935             }
   952             }
   936 
   953 
   937         default:
   954         default:
   938             {
   955             {
   939             Panic(EUnknownUsbServiceState);
   956             PANIC(EUnknownUsbServiceState);
   940             }
   957             }
   941         }
   958         }
   942     }
   959     }
   943 
   960 
   944 // ---------------------------------------------------------------------------
   961 // ---------------------------------------------------------------------------
   962 
   979 
   963     LOG( "Observers getters" );
   980     LOG( "Observers getters" );
   964 
   981 
   965     if (iIdPinObserver != IdPinObserver())
   982     if (iIdPinObserver != IdPinObserver())
   966         {
   983         {
   967         User::Leave(KErrGeneral);
   984         LEAVE(KErrGeneral);
   968         }
   985         }
   969 
   986 
   970     if (iOtgStateObserver != OtgStateObserver())
   987     if (iOtgStateObserver != OtgStateObserver())
   971         {
   988         {
   972         User::Leave(KErrGeneral);
   989         LEAVE(KErrGeneral);
   973         }
   990         }
   974 
   991 
   975     if (iBusActivityObserver != BusActivityObserver())
   992     if (iBusActivityObserver != BusActivityObserver())
   976         {
   993         {
   977         User::Leave(KErrGeneral);
   994         LEAVE(KErrGeneral);
   978         }
   995         }
   979 
   996 
   980     if (iHostEventNotificationObserver != HostEventNotificationObserver())
   997     if (iHostEventNotificationObserver != HostEventNotificationObserver())
   981         {
   998         {
   982         User::Leave(KErrGeneral);
   999         LEAVE(KErrGeneral);
   983         }
  1000         }
   984 
  1001 
   985     if (iMessageNotificationObserver != MessageNotificationObserver())
  1002     if (iMessageNotificationObserver != MessageNotificationObserver())
   986         {
  1003         {
   987         User::Leave(KErrGeneral);
  1004         LEAVE(KErrGeneral);
   988         }
  1005         }
   989 
  1006 
   990     LOG( "Observers destructors" );
  1007     LOG( "Observers destructors" );
   991 
  1008 
   992     // idpinobserver is deleted later        
  1009     // idpinobserver is deleted later        
  1001     delete iMessageNotificationObserver;
  1018     delete iMessageNotificationObserver;
  1002     iMessageNotificationObserver = 0;
  1019     iMessageNotificationObserver = 0;
  1003 
  1020 
  1004     LOG("Creating states");
  1021     LOG("Creating states");
  1005 
  1022 
  1006     User::LeaveIfError(iStates.Append(CUsbStateHostUndefined::NewL(*this)));
  1023     LEAVEIFERROR(iStates.Append(CUsbStateHostUndefined::NewL(*this)));
  1007     User::LeaveIfError(iStates.Append(CUsbStateHostAInitiate::NewL(*this)));
  1024     LEAVEIFERROR(iStates.Append(CUsbStateHostAInitiate::NewL(*this)));
  1008     User::LeaveIfError(iStates.Append(CUsbStateHostAHost::NewL(*this)));
  1025     LEAVEIFERROR(iStates.Append(CUsbStateHostAHost::NewL(*this)));
  1009     User::LeaveIfError(iStates.Append(CUsbStateHostAPeripheral::NewL(*this)));
  1026     LEAVEIFERROR(iStates.Append(CUsbStateHostAPeripheral::NewL(*this)));
  1010     User::LeaveIfError(iStates.Append(CUsbStateHostDelayAttachedHandle::NewL(
  1027     LEAVEIFERROR(iStates.Append(CUsbStateHostDelayAttachedHandle::NewL(
  1011             *this)));
  1028             *this)));
  1012     User::LeaveIfError(iStates.Append(
  1029     LEAVEIFERROR(iStates.Append(
  1013             CUsbStateHostDelayNotAttachedHandle::NewL(*this)));
  1030             CUsbStateHostDelayNotAttachedHandle::NewL(*this)));
  1014     User::LeaveIfError(iStates.Append(
  1031     LEAVEIFERROR(iStates.Append(
  1015             CUsbStateHostHandleDropping::NewL(*this)));
  1032             CUsbStateHostHandleDropping::NewL(*this)));
  1016 
  1033 
  1017     LOG("Check State()" );
  1034     LOG("Check State()" );
  1018 
  1035 
  1019     if (iStates[EUsbStateHostAInitiate] != State(EUsbStateHostAInitiate))
  1036     if (iStates[EUsbStateHostAInitiate] != State(EUsbStateHostAInitiate))
  1020         {
  1037         {
  1021         User::Leave(KErrGeneral);
  1038         LEAVE(KErrGeneral);
  1022         }
  1039         }
  1023 
  1040 
  1024     LOG("Check CurrentHostState()" );
  1041     LOG("Check CurrentHostState()" );
  1025 
  1042 
  1026     iHostState = iStates[EUsbStateHostAInitiate];
  1043     iHostState = iStates[EUsbStateHostAInitiate];
  1027 
  1044 
  1028     if (iStates[EUsbStateHostAInitiate] != CurrentHostState())
  1045     if (iStates[EUsbStateHostAInitiate] != CurrentHostState())
  1029         {
  1046         {
  1030         User::Leave(KErrGeneral);
  1047         LEAVE(KErrGeneral);
  1031         }
  1048         }
  1032 
  1049 
  1033     LOG("NotifManager and WarningNotifier." );
  1050     LOG("NotifManager and WarningNotifier." );
  1034 
  1051 
  1035     CUsbNotifManager* usbnotifmanager = CUsbNotifManager::NewL(*this);
  1052     CUsbNotifManager* usbnotifmanager = CUsbNotifManager::NewL(*this);
  1036     RNotifier rnotifier;
  1053     RNotifier rnotifier;
  1037     User::LeaveIfError(rnotifier.Connect());
  1054     LEAVEIFERROR(rnotifier.Connect());
  1038     CUsbWarningNotifier* usbnotifier = CUsbWarningNotifier::NewL(rnotifier,
  1055     CUsbWarningNotifier* usbnotifier = CUsbWarningNotifier::NewL(rnotifier,
  1039             *usbnotifmanager, EUsbOtgPartiallySupportedDevice);
  1056             *usbnotifmanager, EUsbOtgPartiallySupportedDevice);
  1040     usbnotifier->IsFeedbackNeeded();
  1057     usbnotifier->IsFeedbackNeeded();
  1041 
  1058 
  1042     LOG( "NotifManager and WarningNotifier destruction." );
  1059     LOG( "NotifManager and WarningNotifier destruction." );
  1080 
  1097 
  1081     // check if the same observer already exist in a list
  1098     // check if the same observer already exist in a list
  1082     if (KErrNotFound != iOtgStateObservers.Find(&aObserver))
  1099     if (KErrNotFound != iOtgStateObservers.Find(&aObserver))
  1083         {
  1100         {
  1084         LOG( "Observer already exists" );
  1101         LOG( "Observer already exists" );
  1085         Panic(EObserverAlreadyExists);
  1102         PANIC(EObserverAlreadyExists);
  1086         return;
  1103         return;
  1087         }
  1104         }
  1088     iOtgStateObservers.AppendL(&aObserver);
  1105     iOtgStateObservers.AppendL(&aObserver);
  1089 
  1106 
  1090     }
  1107     }
  1099 
  1116 
  1100     TInt i(iOtgStateObservers.Find(&aObserver));
  1117     TInt i(iOtgStateObservers.Find(&aObserver));
  1101     if (KErrNotFound == i)
  1118     if (KErrNotFound == i)
  1102         {
  1119         {
  1103         LOG( "Observer not found" );
  1120         LOG( "Observer not found" );
  1104         Panic(ECanNotFindUsbOtgWatcherStateObserver);
  1121         PANIC(ECanNotFindUsbOtgWatcherStateObserver);
  1105         return;
  1122         return;
  1106         }
  1123         }
  1107 
  1124 
  1108     iOtgStateObservers.Remove(i);
  1125     iOtgStateObservers.Remove(i);
  1109     }
  1126     }