usbengines/usbwatcher/src/cusbdevconstarter.cpp
changeset 0 1e05558e2206
equal deleted inserted replaced
-1:000000000000 0:1e05558e2206
       
     1 /*
       
     2 * Copyright (c) 2007-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:  Implement class CUsbDevConStarter
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "cusbdevconstarter.h"
       
    20 #include "debug.h"
       
    21 
       
    22 _LIT16( KUsbDevConPath, "z:\\sys\\bin\\usbdevcon.exe" );
       
    23 _LIT16( KUsbDevConParams, "" );
       
    24 
       
    25 
       
    26 // ======== MEMBER FUNCTIONS ========
       
    27 
       
    28 // ----------------------------------------------------------------------------
       
    29 // NewL
       
    30 // ----------------------------------------------------------------------------
       
    31 //
       
    32 CUsbDevConStarter* CUsbDevConStarter::NewL()
       
    33     {
       
    34     LOG_FUNC
       
    35 
       
    36     CUsbDevConStarter* self = new ( ELeave ) CUsbDevConStarter();
       
    37     CleanupStack::PushL( self );
       
    38     self->ConstructL();
       
    39     CleanupStack::Pop( self );
       
    40     return self;
       
    41     }
       
    42 
       
    43 // ----------------------------------------------------------------------------
       
    44 // Destructor
       
    45 // ----------------------------------------------------------------------------
       
    46 //
       
    47 CUsbDevConStarter::~CUsbDevConStarter()
       
    48     {
       
    49     LOG_FUNC
       
    50 
       
    51     Cancel();
       
    52     }
       
    53 
       
    54 // ----------------------------------------------------------------------------
       
    55 // Start usbdevcon process unless it is already running.
       
    56 // ----------------------------------------------------------------------------
       
    57 //
       
    58 void CUsbDevConStarter::Start()
       
    59     {
       
    60     LOG_FUNC
       
    61 
       
    62     iStarted = ETrue;
       
    63     if ( EIdle == iState )
       
    64         {
       
    65         TInt err = iProcess.Create( KUsbDevConPath, KUsbDevConParams );
       
    66         if ( KErrNone == err )
       
    67             {
       
    68             //start process and wait until it is constructed
       
    69             iProcess.Resume();
       
    70             iState = EWaitingRendezvous;
       
    71             iProcess.Rendezvous( iStatus );
       
    72             SetActive();
       
    73             }
       
    74         else
       
    75             {
       
    76             //error in starting, no restarting needed
       
    77             iStarted = EFalse;
       
    78             LOG1( "ERROR: RProcess::Create = %d" ,  err );
       
    79             }
       
    80         }
       
    81 
       
    82     }
       
    83 
       
    84 
       
    85 // ----------------------------------------------------------------------------
       
    86 // Logon to usbdevcon process for termination
       
    87 // ----------------------------------------------------------------------------
       
    88 //
       
    89 void CUsbDevConStarter::Logon()
       
    90     {
       
    91     LOG_FUNC
       
    92 
       
    93     iState = ERunning;
       
    94 
       
    95     //request termination notify
       
    96     iProcess.Logon( iStatus );
       
    97     SetActive();
       
    98     }
       
    99 
       
   100 
       
   101 // ----------------------------------------------------------------------------
       
   102 // StopRestarting prevents usbdevcon process restarting.
       
   103 // ----------------------------------------------------------------------------
       
   104 //
       
   105 void CUsbDevConStarter::StopRestarting()
       
   106     {
       
   107     LOG_FUNC
       
   108 
       
   109     iStarted = EFalse;
       
   110     }
       
   111 
       
   112 // ----------------------------------------------------------------------------
       
   113 // RunL handles the process termination.
       
   114 // The process is restarted, if Stop has not been requested and it terminated
       
   115 // without error.
       
   116 // ----------------------------------------------------------------------------
       
   117 //
       
   118 void CUsbDevConStarter::RunL()
       
   119     {
       
   120     LOG_FUNC
       
   121 
       
   122     LOG2( "iStatus = %d, iState = %d", iStatus.Int(), iState );
       
   123     switch( iState )
       
   124         {
       
   125         case EIdle:
       
   126             {
       
   127             break;
       
   128             }
       
   129         case EWaitingRendezvous:
       
   130             {
       
   131             Logon();
       
   132             break;
       
   133             }
       
   134         case ERunning:
       
   135             {
       
   136             iState = EIdle;
       
   137             iProcess.Close();
       
   138 
       
   139             //Do not restart, if terminated with error.
       
   140             if( iStarted && ( iStatus == KErrNone ) )
       
   141                 {
       
   142                 Start();
       
   143                 }
       
   144             break;
       
   145             }
       
   146         default:
       
   147             LOG( "Unhandled state" );
       
   148 			break;
       
   149         }
       
   150 
       
   151     }
       
   152 
       
   153 // ----------------------------------------------------------------------------
       
   154 // Log error, if RunL leaves. RunL should never leave.
       
   155 // ----------------------------------------------------------------------------
       
   156 //
       
   157 TInt CUsbDevConStarter::RunError( TInt aError )
       
   158     {
       
   159     LOG_FUNC
       
   160 
       
   161     LOG1( "aError = %d", aError );
       
   162     return KErrNone;
       
   163     }
       
   164 
       
   165 // ----------------------------------------------------------------------------
       
   166 // DoCancel
       
   167 // ----------------------------------------------------------------------------
       
   168 //
       
   169 void CUsbDevConStarter::DoCancel()
       
   170     {
       
   171     LOG_FUNC
       
   172 
       
   173     iProcess.LogonCancel( iStatus );
       
   174     iProcess.Close();
       
   175     }
       
   176 
       
   177 // ----------------------------------------------------------------------------
       
   178 // Constructor
       
   179 // ----------------------------------------------------------------------------
       
   180 //
       
   181 CUsbDevConStarter::CUsbDevConStarter()
       
   182     : CActive( EPriorityStandard )
       
   183     , iStarted( EFalse )
       
   184     , iState( EIdle )
       
   185     {
       
   186     LOG_FUNC
       
   187 
       
   188     CActiveScheduler::Add( this );
       
   189     }
       
   190 
       
   191 // ----------------------------------------------------------------------------
       
   192 // ConstructL
       
   193 // ----------------------------------------------------------------------------
       
   194 //
       
   195 void CUsbDevConStarter::ConstructL()
       
   196     {
       
   197     LOG_FUNC
       
   198     //empty
       
   199     }
       
   200 
       
   201 // End of file