testexecfw/statsrv/device/source/statapi/src/stat_controller.cpp
changeset 0 3e07fef1e154
equal deleted inserted replaced
-1:000000000000 0:3e07fef1e154
       
     1 /*
       
     2 * Copyright (c) 2005-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: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20  /************************************************************************* 
       
    21  *
       
    22  * System Includes
       
    23  *
       
    24  *************************************************************************/
       
    25 
       
    26 /*************************************************************************
       
    27  *
       
    28  * Local Includes
       
    29  *
       
    30  *************************************************************************/
       
    31 #include "assert.h"
       
    32 #include "stat.h"
       
    33 #include "stat_interfaces.h"
       
    34 #include "stat_controller.h"
       
    35 #include "stat_engine.h"
       
    36 
       
    37 
       
    38 /*************************************************************************
       
    39  *
       
    40  * Definitions
       
    41  *
       
    42  *************************************************************************/
       
    43 
       
    44 /*************************************************************************
       
    45  *
       
    46  * CStatController - Construction
       
    47  *
       
    48  *************************************************************************/
       
    49 CStatController *CStatController::NewL()
       
    50 {
       
    51     CStatController *self = new (ELeave) CStatController();
       
    52     CleanupStack::PushL(self);
       
    53 	self->ConstructL();
       
    54 	CleanupStack::Pop();
       
    55     return self;
       
    56 }
       
    57 
       
    58 CStatController::CStatController() : CActive(EPriorityHigh), iFs( NULL ), iMsg( NULL )
       
    59 {
       
    60 }
       
    61 
       
    62 void CStatController::ConstructL( void ) 
       
    63 {
       
    64 	// add me to the active scheduler
       
    65 	CActiveScheduler::Add(this); 
       
    66 
       
    67 	// params
       
    68 	iEngine = NULL;
       
    69 	iSessionStatus = EIdle;
       
    70 	iTransport = NULL;
       
    71 	iUI = NULL;
       
    72 	iConnectedSuccessfully = EFalse;
       
    73 	iUserInitiatedExitRequest = EFalse;
       
    74 	iFs =	NULL;
       
    75 	iMsg = NULL;
       
    76 
       
    77 	// wait for notifications
       
    78 	iStatus = KRequestPending;
       
    79 	SetActive();
       
    80 }
       
    81 
       
    82 CStatController::~CStatController()
       
    83 {
       
    84 	// clear notification -- deque (and CActive destructor) will fail otherwise
       
    85 	Notify( KErrCancel );
       
    86 	Deque();
       
    87 
       
    88 	// make sure the session is closed
       
    89 	asserte( iSessionStatus == EIdle );
       
    90 	asserte( iEngine == NULL );
       
    91 	asserte( iTransport == NULL );
       
    92 }
       
    93 
       
    94 /*************************************************************************
       
    95  *
       
    96  * CStatController - public interface
       
    97  *
       
    98  *************************************************************************/
       
    99 TInt CStatController::StartSession( TStatConnectType aConnectType, TDesC *aConnectParams, MNotifyUI *aUI, RFs *const aSession, MNotifyLogMessage *const aMsg )
       
   100 {
       
   101 	TInt exp =	KErrNone;
       
   102 
       
   103 	iFs =	aSession;
       
   104 	iMsg =	aMsg;
       
   105 
       
   106     if(!aConnectParams) {
       
   107         return -1;
       
   108     }
       
   109 
       
   110 	// this is the notification sink for this session
       
   111 	iUI = aUI;
       
   112 	iConnectType = aConnectType;
       
   113 	asserte( aConnectParams->Length() <= KAddressTextLimit );
       
   114     iConnectParams.Copy( *aConnectParams );
       
   115 		
       
   116 	// create the engine with a callback interface to the controller, create the 
       
   117 	// appropriate transport, and start the engine
       
   118 	iConnectedSuccessfully = EFalse;
       
   119 	TRAP( exp, (iEngine = CStatEngine::NewL(this, iFs, iMsg)) );
       
   120 	if( exp != KErrNone ) {
       
   121 		return -1;
       
   122 	}
       
   123 
       
   124 	// if the start transport leaves then its because it failed to create an object. This
       
   125 	// is either because the network or transport creation failed left. If it was the
       
   126 	// transport then the network may still have memory allocated.
       
   127 	TRAP( exp, (iTransport = StartTransportL(iConnectType)) );
       
   128 	if( exp != KErrNone ) {
       
   129 		if( iSerialNetwork != NULL ) {
       
   130 			delete iSerialNetwork;
       
   131 		}
       
   132 		return -1;
       
   133 	}
       
   134 
       
   135 	iEngine->StartEngine( iTransport, iConnectType, &iConnectParams );
       
   136 
       
   137 	// this controller only supports one session which always has ID = 1
       
   138 	return 1;
       
   139 }
       
   140 
       
   141 TInt CStatController::StopSession( TInt aSessionId )
       
   142 {
       
   143 	// make sure this is a valid, active, session
       
   144 	if( (aSessionId != 1) || (iSessionStatus == EIdle) )
       
   145 	{
       
   146 		// If the session is valid and idle then it is not
       
   147 		// initialised and connected correctly.
       
   148 		// Call Notify to cancel the outstanding asynchronous events
       
   149 		// we are registered for.
       
   150 		// This fixes a problem that appears when the engine fails to
       
   151 		// start correctly (due to COM port failures for example) and
       
   152 		// caused the CActiveScheduler never to stop as this object
       
   153 		// never received a callback when it needs one to remove
       
   154 		// itself from the active state.
       
   155 		// If the CActiveScheduler does not shut down then the
       
   156 		// program never exits and blocks.
       
   157 		Notify( KErrNone );
       
   158 		//
       
   159 		return KErrBadHandle;
       
   160 	}
       
   161 
       
   162 	iMsg->Msg( _L("CONTROLLER: Stopping session.") );
       
   163 
       
   164 	// tell the engine to stop and then wait for an asynchronous response
       
   165 	iUserInitiatedExitRequest = ETrue;
       
   166 	iEngine->StopEngine();
       
   167 	return KErrNone;
       
   168 }
       
   169 
       
   170 TInt CStatController::SessionStatus( TInt aSessionId )
       
   171 {
       
   172 	if( aSessionId != 1 )
       
   173 		return KErrBadHandle;
       
   174 	return iSessionStatus;
       
   175 }
       
   176 
       
   177 void CStatController::SetStatus( TCommStatus aNewStatus )
       
   178 {
       
   179 	iSessionStatus = aNewStatus;
       
   180 	if( iUI != NULL )
       
   181 		iUI->HandleStatusChange( 1, iSessionStatus );
       
   182 	if( aNewStatus == EConnected ) 
       
   183 		iConnectedSuccessfully = ETrue;
       
   184 }
       
   185 
       
   186 /*************************************************************************
       
   187  *
       
   188  * CStatController - MNotifyStatController
       
   189  *
       
   190  *************************************************************************/
       
   191 void CStatController::HandleStatusChange( TCommStatus aNewStatus )
       
   192 {
       
   193 	// If the engine has gone idle then everything is done and we can kill
       
   194 	// everything off. But, this function was called by the engine, so we
       
   195 	// can't kill it here -- send self an event to clean things up.
       
   196 	if( aNewStatus == EIdle ) {
       
   197 		Notify( KErrNone );
       
   198 		return;
       
   199 	}
       
   200 	
       
   201 	// update the status
       
   202 	SetStatus( aNewStatus );
       
   203 }
       
   204 
       
   205 // The engine deals with the transports in the face of errors itself, and the 
       
   206 // interaction with the engine is really handled via HandleStatusChange. This
       
   207 // function is mainly here to be able to pass information up from the transport
       
   208 // and the engine to the UI / user
       
   209 void CStatController::HandleError( TInt aError, void *aErrorData )
       
   210 {
       
   211 	if( iUI != NULL ) 
       
   212 		iUI->HandleError( aError, aErrorData );
       
   213 }
       
   214 
       
   215 // Info is for end-user consumption only and is sent directly to the UI.
       
   216 void CStatController::HandleInfo( const TDesC *aInfo )
       
   217 {
       
   218 	if( iUI != NULL )
       
   219 		iUI->HandleInfo( aInfo );
       
   220 }
       
   221 
       
   222 /*************************************************************************
       
   223  *
       
   224  * CStatController - CActive
       
   225  *
       
   226  *************************************************************************/
       
   227 void CStatController::RunL()
       
   228 {
       
   229 	// delete the engine and the transport
       
   230 	delete iEngine;
       
   231 	iEngine = NULL;
       
   232 	KillTransport();
       
   233 
       
   234 	// now change the status to idle
       
   235 	SetStatus( EIdle );
       
   236 
       
   237 	// wait again
       
   238 	iStatus = KRequestPending;
       
   239 	SetActive();
       
   240 
       
   241 	// But if this wasn't a user requested exit -- and the last connection
       
   242 	// did connect successfully, then restart. Otherwise clear the flags.
       
   243 	if( (iConnectedSuccessfully) && (!iUserInitiatedExitRequest) ) {
       
   244 		iConnectedSuccessfully = EFalse;
       
   245 		iEngine = CStatEngine::NewL( this, iFs, iMsg );
       
   246 		iTransport = StartTransportL( iConnectType );
       
   247 		iEngine->StartEngine( iTransport, iConnectType, &iConnectParams );
       
   248 	} else {
       
   249 		iUserInitiatedExitRequest = EFalse;
       
   250 		iConnectedSuccessfully = EFalse;
       
   251 	}
       
   252 }		
       
   253 
       
   254 void CStatController::DoCancel()
       
   255 {
       
   256 }
       
   257 
       
   258 void CStatController::Notify( TInt aErrorStatus )
       
   259 {
       
   260 	TRequestStatus *pstatus;
       
   261 	pstatus = &iStatus;
       
   262 	TRequestStatus *&rstatus = pstatus;
       
   263 	User::RequestComplete( rstatus, aErrorStatus );
       
   264 }
       
   265 
       
   266 /*************************************************************************
       
   267  *
       
   268  * CStatController - Private
       
   269  *
       
   270  *************************************************************************/
       
   271 MStatApiTransport *CStatController::StartTransportL( TStatConnectType aConnectType )
       
   272 {
       
   273 	switch( aConnectType ) {
       
   274 
       
   275 #ifndef LIGHT_MODE
       
   276 	case ETCPIP:
       
   277 		iTcpipTransport = CStatTransportTCPIP::NewL( );
       
   278 		iPacketisationTransport = CStatTransportPacketisation::NewL( iEngine, iTcpipTransport, iTcpipTransport->GetPacketSize() );
       
   279 		return iPacketisationTransport;
       
   280 
       
   281 	case EInfrared:
       
   282 		iSerialNetwork = CStatApiSerial::NewL( _L("IrCOMM") );
       
   283 		iPacketisationTransport = CStatTransportPacketisation::NewL( iEngine, iSerialNetwork, iSerialNetwork->GetPacketSize() );
       
   284 		return iPacketisationTransport;
       
   285 
       
   286 	case EBluetooth:
       
   287 		iBluetoothTransport = CStatTransportBT::NewL();
       
   288 		iPacketisationTransport = CStatTransportPacketisation::NewL( iEngine, iBluetoothTransport, iBluetoothTransport->GetPacketSize() );
       
   289 		return iPacketisationTransport;
       
   290 	
       
   291 	case EUsb:
       
   292 		iUsbNetwork = CStatApiUsb::NewL();
       
   293 		iPacketisationTransport = CStatTransportPacketisation::NewL( iEngine, iUsbNetwork, iUsbNetwork->GetPacketSize() );
       
   294 		return iPacketisationTransport;
       
   295 
       
   296 		
       
   297 		
       
   298 #endif // ifndef LIGHT_MODE
       
   299 
       
   300 	case ESerial:
       
   301 		iSerialNetwork = CStatApiSerial::NewL( _L("ECUART") );
       
   302 		iPacketisationTransport = CStatTransportPacketisation::NewL( iEngine, iSerialNetwork, iSerialNetwork->GetPacketSize() );
       
   303 		return iPacketisationTransport;
       
   304 
       
   305 	default:
       
   306 		;
       
   307 	}
       
   308 	return NULL;
       
   309 }
       
   310 
       
   311 void CStatController::KillTransport()
       
   312 {
       
   313 	switch( iConnectType ) {
       
   314 
       
   315 #ifndef LIGHT_MODE
       
   316 	case ETCPIP:
       
   317 		delete iTcpipTransport;
       
   318 		delete iPacketisationTransport;
       
   319 		iPacketisationTransport = NULL;
       
   320 		iTcpipTransport = NULL;
       
   321 		break;
       
   322 
       
   323 	case EBluetooth:
       
   324 		delete iBluetoothTransport;
       
   325 		delete iPacketisationTransport;
       
   326 		iBluetoothTransport = NULL;
       
   327 		iPacketisationTransport = NULL;
       
   328 		break;
       
   329 
       
   330 	case EInfrared:
       
   331 		delete iSerialNetwork;
       
   332 		delete iPacketisationTransport;
       
   333 		iSerialNetwork = NULL;
       
   334 		iPacketisationTransport = NULL;
       
   335 		break;
       
   336 #endif // ifndef LIGHT_MODE
       
   337 
       
   338 	case ESerial:
       
   339 		delete iSerialNetwork;
       
   340 		delete iPacketisationTransport;
       
   341 		iSerialNetwork = NULL;
       
   342 		iPacketisationTransport = NULL;
       
   343 		break;
       
   344 
       
   345 	default:
       
   346 		;
       
   347 	}
       
   348 	iTransport = NULL;	
       
   349 }