cbsref/telephonyrefplugins/atltsy/comms/src/commengine.cpp
changeset 44 8b72faa1200f
equal deleted inserted replaced
39:2473f5e227f9 44:8b72faa1200f
       
     1 // Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // @file commengine.cpp
       
    15 // This contains CCommEngine which manage access to serial port.
       
    16 // 
       
    17 
       
    18 // user include
       
    19 #include "globalphonemanager.h"
       
    20 #include "commengine.h"
       
    21 #include "mslogger.h"
       
    22 #include "commreader.h"
       
    23 #include "commwriter.h"
       
    24 #include "mcommobserver.h"
       
    25 #include "stringparser.h"
       
    26 
       
    27 // const define
       
    28 const TInt KLtsyOneSecondPause = 1000000;
       
    29 
       
    30 // ---------------------------------------------------------------------------
       
    31 // CCommEngine::NewL
       
    32 // other items were commented in a header
       
    33 // ---------------------------------------------------------------------------
       
    34 CCommEngine* CCommEngine::NewL(TInt aBufferSize, 
       
    35 					           TInt aReadPriority, 
       
    36 					           TInt aWritePriority, 
       
    37 					           TPortAccess& aPortAccess)
       
    38 	{
       
    39 	CCommEngine *self = CCommEngine::NewLC(aBufferSize, 
       
    40 										   aReadPriority, 
       
    41 										   aWritePriority, 
       
    42 										   aPortAccess);
       
    43 	CleanupStack::Pop(self);
       
    44 	
       
    45 	return self;
       
    46 	}
       
    47 
       
    48 // ---------------------------------------------------------------------------
       
    49 // CCommEngine::NewLC
       
    50 // other items were commented in a header
       
    51 // ---------------------------------------------------------------------------
       
    52 CCommEngine* CCommEngine::NewLC(TInt aBufferSize, 
       
    53 							    TInt aReadPriority, 
       
    54 							    TInt aWritePriority, 
       
    55 							    TPortAccess& aPortAccess)
       
    56 	{
       
    57 	CCommEngine* self = new (ELeave) CCommEngine(aPortAccess);
       
    58 	CleanupStack::PushL(self);
       
    59 	
       
    60 	self->ConstructL(aBufferSize, aReadPriority, aWritePriority);
       
    61 	
       
    62 	return self;
       
    63 	}
       
    64 
       
    65 // ---------------------------------------------------------------------------
       
    66 // CCommEngine::~CCommEngine
       
    67 // other items were commented in a header
       
    68 // ---------------------------------------------------------------------------
       
    69 CCommEngine::~CCommEngine()
       
    70 	{
       
    71     delete iCommReader;
       
    72 	delete iCommWriter;
       
    73 	delete iStringParse;
       
    74 	delete iCallbackTimer;
       
    75 	iCommPort.Close();
       
    76 	iCommServer.Close();
       
    77 	}
       
    78 
       
    79 // ---------------------------------------------------------------------------
       
    80 // CCommEngine::CCommEngine
       
    81 // other items were commented in a header
       
    82 // ---------------------------------------------------------------------------
       
    83 CCommEngine::CCommEngine(TPortAccess& aPortAccess) 
       
    84 						:iPortAccess(aPortAccess)
       
    85 	{
       
    86 	iCommReader = NULL;
       
    87 	iCommWriter = NULL;
       
    88 	iEngineObserver = NULL;
       
    89 	iStringParse = NULL;
       
    90 	iCommCancel = EFalse;
       
    91 	}
       
    92 
       
    93 // ---------------------------------------------------------------------------
       
    94 // CCommEngine::ConstructL
       
    95 // other items were commented in a header
       
    96 // ---------------------------------------------------------------------------
       
    97 void CCommEngine::ConstructL(TInt aBufferSize, TInt aReadPriority, TInt aWritePriority)
       
    98 	{
       
    99 	iCommReader = new (ELeave) CCommReader(this, aReadPriority);
       
   100 	iCommWriter = new (ELeave) CCommWriter(this, aWritePriority);	
       
   101 	iStringParse = CStringParser::NewL(aBufferSize);
       
   102 	iCallbackTimer = CCallbackTimer::NewL(*this);
       
   103 	}
       
   104 
       
   105 // ---------------------------------------------------------------------------
       
   106 // CCommEngine::ConfigurePort
       
   107 // other items were commented in a header
       
   108 // ---------------------------------------------------------------------------
       
   109 TInt CCommEngine::ConfigurePort(TCommConfig aConfiguration)
       
   110 	{
       
   111 	LOGTEXT(_L8("[Ltsy] Starting CCommEngine::ConfigurePort()"));
       
   112 	if (iPortAccess == EPortAccessAllowed)
       
   113 		{
       
   114 		TInt ret;
       
   115 		TCommConfig cbuf;
       
   116 		TCommConfigV01 &cfg = cbuf();
       
   117 		
       
   118 		// Get the Configuration from current serial com port
       
   119 		iCommPort.Config(cbuf);
       
   120 	    
       
   121 		// set the configuration according to passed config data
       
   122 		TCommConfigV01 &newCfg = aConfiguration();
       
   123 		cfg.iRate = newCfg.iRate;
       
   124 		cfg.iDataBits = newCfg.iDataBits;
       
   125 		cfg.iStopBits = newCfg.iStopBits;
       
   126 		cfg.iParity = newCfg.iParity;
       
   127 		cfg.iHandshake = newCfg.iHandshake;
       
   128 		
       
   129 		// Set the Configuration
       
   130 		ret = iCommPort.SetConfig(cbuf);
       
   131 		if(ret != KErrNone)
       
   132 			{
       
   133 			LOGTEXT2(_L8("[Ltsy] CCommEngine:\tError %d configuring port"),ret);
       
   134 			return ret;
       
   135 			}
       
   136 		}
       
   137 	return KErrNone;	
       
   138 	}
       
   139 
       
   140 // ---------------------------------------------------------------------------
       
   141 // CCommEngine::Disconnect
       
   142 // other items were commented in a header
       
   143 // ---------------------------------------------------------------------------
       
   144 void CCommEngine::Disconnect()
       
   145 	{
       
   146 	LOGTEXT(_L8("[Ltsy] Starting CCommEngine::Disconnect()"));
       
   147 	
       
   148 	if (iPortAccess == EPortAccessAllowed)
       
   149 		{
       
   150 		TCommConfig cbuf;
       
   151 		TCommConfigV01 &cfg = cbuf();
       
   152 		iCommPort.Config(cbuf);
       
   153 		cfg.iHandshake = KConfigFreeRTS	| KConfigFreeDTR;
       
   154 		TInt ret = iCommPort.SetConfig(cbuf);
       
   155 		if (ret == KErrNone)
       
   156 			{
       
   157 			iCommPort.SetSignalsToSpace(KSignalRTS | KSignalDTR);
       
   158 			}
       
   159 		}
       
   160 	
       
   161 	CommClose();
       
   162 	
       
   163 	iPortAccess = EPortAccessDenied;	
       
   164 	}
       
   165 
       
   166 // ---------------------------------------------------------------------------
       
   167 // CCommEngine::Read
       
   168 // other items were commented in a header
       
   169 // ---------------------------------------------------------------------------
       
   170 void CCommEngine::Read()
       
   171 	{
       
   172 	LOGTEXT(_L8("[Ltsy] Starting CCommEngine::Read()"));
       
   173 	iCommCancel = EFalse;
       
   174 	if (iPortAccess == EPortAccessAllowed)
       
   175 		{
       
   176 		CommReadOneOrMore();
       
   177 		}
       
   178 	else
       
   179 		{
       
   180 		LOGTEXT(_L8("[Ltsy] Port Not Allow Access"));
       
   181 		}	
       
   182 	}
       
   183 
       
   184 // ---------------------------------------------------------------------------
       
   185 // CCommEngine::DropDtr
       
   186 // other items were commented in a header
       
   187 // ---------------------------------------------------------------------------
       
   188 void CCommEngine::DropDtr()
       
   189 	{
       
   190 	LOGTEXT(_L8("[Ltsy] Starting CCommEngine::DropDtr()"));
       
   191 	
       
   192 	if (iPortAccess == EPortAccessAllowed)
       
   193 		{
       
   194 		LOGTEXT(_L8("[Ltsy] CCommEngine::DropDtr Dropping DTR"));
       
   195 		
       
   196 		iCommPort.SetSignals(0,KSignalDTR);
       
   197 		}
       
   198 	}
       
   199 
       
   200 // ---------------------------------------------------------------------------
       
   201 // CCommEngine::RaiseDTR
       
   202 // other items were commented in a header
       
   203 // ---------------------------------------------------------------------------
       
   204 void CCommEngine::RaiseDTR()
       
   205 	{
       
   206 	LOGTEXT(_L8("[Ltsy] Starting CCommEngine::RaiseDTR()"));
       
   207 	
       
   208 	if (iPortAccess == EPortAccessAllowed)
       
   209 		{
       
   210 		LOGTEXT(_L8("[Ltsy] CCommEngine::RaiseDTR Raising DTR"));
       
   211 		
       
   212 		iCommPort.SetSignals(KSignalDTR,0);
       
   213 		}
       
   214 	}
       
   215 
       
   216 // ---------------------------------------------------------------------------
       
   217 // CCommEngine::DropRTS
       
   218 // other items were commented in a header
       
   219 // ---------------------------------------------------------------------------
       
   220 void CCommEngine::DropRTS()
       
   221 	{
       
   222 	LOGTEXT(_L8("[Ltsy] Starting CCommEngine::DropRTS()"));
       
   223 	
       
   224 	if (iPortAccess == EPortAccessAllowed)
       
   225 		{
       
   226 		LOGTEXT(_L8("[Ltsy] Dropping RTS"));
       
   227 		
       
   228 		iCommPort.SetSignals(0,KSignalRTS);
       
   229 		}
       
   230 	}
       
   231 
       
   232 // ---------------------------------------------------------------------------
       
   233 // CCommEngine::RaiseRTS
       
   234 // other items were commented in a header
       
   235 // ---------------------------------------------------------------------------
       
   236 void CCommEngine::RaiseRTS()
       
   237 	{
       
   238 	LOGTEXT(_L8("[Ltsy] Starting CCommEngine::RaiseRTS()"));
       
   239 	
       
   240 	if (iPortAccess == EPortAccessAllowed)
       
   241 		{
       
   242 		LOGTEXT(_L8("[Ltsy] Raising RTS"));
       
   243 		
       
   244 		iCommPort.SetSignals(KSignalRTS,0);
       
   245 		}
       
   246 	}
       
   247 // ---------------------------------------------------------------------------
       
   248 // CCommEngine::ResetBuffers
       
   249 // other items were commented in a header
       
   250 // ---------------------------------------------------------------------------
       
   251 void CCommEngine::ResetBuffers()
       
   252 	{
       
   253 	iCommPort.ResetBuffers();
       
   254 	}
       
   255 // ---------------------------------------------------------------------------
       
   256 // CCommEngine::Signals
       
   257 // other items were commented in a header
       
   258 // ---------------------------------------------------------------------------
       
   259 TUint CCommEngine::Signals()
       
   260 	{
       
   261 	LOGTEXT(_L8("[Ltsy] Starting CCommEngine::Signals()"));
       
   262 	
       
   263 	return iCommPort.Signals();
       
   264 	}
       
   265 
       
   266 // ---------------------------------------------------------------------------
       
   267 // CCommEngine::ResetReadAndWriteBuffers
       
   268 // other items were commented in a header
       
   269 // ---------------------------------------------------------------------------
       
   270 void CCommEngine::ResetReadAndWriteBuffers()
       
   271 	{
       
   272 	LOGTEXT(_L8("[Ltsy] Starting CCommEngine::ResetReadAndWriteBuffers()"));
       
   273 	
       
   274 	iCommPort.ResetBuffers();
       
   275 	}
       
   276 
       
   277 // ---------------------------------------------------------------------------
       
   278 // CCommEngine::GetSizeOfRxBuffer
       
   279 // other items were commented in a header
       
   280 // ---------------------------------------------------------------------------
       
   281 TInt CCommEngine::GetSizeOfRxBuffer()
       
   282 	{
       
   283 	LOGTEXT(_L8("[Ltsy] Starting CCommEngine::GetSizeOfRxBuffer()"));
       
   284 	
       
   285 	return iCommPort.QueryReceiveBuffer();
       
   286 	}
       
   287 
       
   288 // ---------------------------------------------------------------------------
       
   289 // CCommEngine::GetPortShutdownTimeout
       
   290 // other items were commented in a header
       
   291 // ---------------------------------------------------------------------------
       
   292 TInt CCommEngine::GetPortShutdownTimeout()
       
   293 	{
       
   294 	if (iPortAccess == EPortAccessAllowed)
       
   295 		{
       
   296 		TCommConfig2 cbuf;
       
   297 		TCommConfigV02& cfg = cbuf();
       
   298 		// Get the Configuration
       
   299 		iCommPort.Config(cbuf);				
       
   300 
       
   301 		return (cfg.iTxShutdownTimeout);
       
   302 		}
       
   303 	else
       
   304 		{
       
   305 		return (0);
       
   306 		}
       
   307 	}
       
   308 
       
   309 // ---------------------------------------------------------------------------
       
   310 // CCommEngine::SetPortShutdownTimeout
       
   311 // other items were commented in a header
       
   312 // ---------------------------------------------------------------------------
       
   313 TInt CCommEngine::SetPortShutdownTimeout(TInt aTimeout)
       
   314 	{
       
   315 	if (iPortAccess == EPortAccessAllowed)
       
   316 		{
       
   317 		TCommConfig2 cbuf;
       
   318 		TCommConfigV02 &cfg = cbuf();
       
   319 		
       
   320 		//Get the Configuration
       
   321 		iCommPort.Config(cbuf);
       
   322 	
       
   323 		//Set the Configuration
       
   324 		cfg.iTxShutdownTimeout = aTimeout;
       
   325 		return (iCommPort.SetConfig(cbuf));
       
   326 		}
       
   327 	else
       
   328 		{
       
   329 		return KErrAccessDenied;
       
   330 		}
       
   331 	}
       
   332 
       
   333 // ---------------------------------------------------------------------------
       
   334 // CCommEngine::CommOpen
       
   335 // other items were commented in a header
       
   336 // ---------------------------------------------------------------------------
       
   337 TInt CCommEngine::CommOpen(const TDesC& aDll, const TDesC& aName, TCommAccess aMode)
       
   338 	{
       
   339 	TInt err;
       
   340 	if (err = iCommServer.Connect(), err!=KErrNone)
       
   341 		return err;
       
   342 		
       
   343 	// load serial comm module
       
   344 	if (aDll.Length()>0)
       
   345 		{
       
   346 		if (err = iCommServer.LoadCommModule(aDll), err!=KErrNone)
       
   347 			{
       
   348 			iCommServer.Close();
       
   349 			return err;
       
   350 			}
       
   351 		}
       
   352 	
       
   353 	// start to open the serial com with ECommShared model
       
   354 	if (aMode==ECommShared)
       
   355 		{
       
   356 		if (err = iCommPort.Open(iCommServer, aName, aMode), err!=KErrNone)
       
   357 			{
       
   358 			iCommServer.Close();
       
   359 			return err;
       
   360 			}
       
   361 		}
       
   362 		
       
   363 	return KErrNone;
       
   364 	}
       
   365 
       
   366 // ---------------------------------------------------------------------------
       
   367 // CCommEngine::CommClose
       
   368 // other items were commented in a header
       
   369 // ---------------------------------------------------------------------------
       
   370 void CCommEngine::CommClose()
       
   371 	{
       
   372 	iCommReader->Cancel();
       
   373 	iCommWriter->Cancel();
       
   374 	iCommPort.Close();
       
   375 	iCommServer.Close();
       
   376 	}
       
   377 
       
   378 // ---------------------------------------------------------------------------
       
   379 // CCommEngine::CommWrite
       
   380 // other items were commented in a header
       
   381 // ---------------------------------------------------------------------------
       
   382 void CCommEngine::CommWrite(const TDesC8& aDes)
       
   383 	{
       
   384     LOGTEXT(_L8("[Ltsy] Starting CCommEngine::CommWrite()"));
       
   385     LOGTEXTREL2(_L8("Tx:\t%S"),&aDes);
       
   386     
       
   387 	iCommPort.Write(iCommWriter->StatusRef(), aDes);
       
   388 	iCommWriter->Activate();
       
   389 	}
       
   390 
       
   391 // ---------------------------------------------------------------------------
       
   392 // CCommEngine::CommRead
       
   393 // other items were commented in a header
       
   394 // ---------------------------------------------------------------------------
       
   395 void CCommEngine::CommRead()
       
   396 	{
       
   397 	iCommPort.Read(iCommReader->StatusRef(), iRxBuf, iRxBuf.Length());
       
   398 	iCommReader->Activate();
       
   399 	}
       
   400 
       
   401 // ---------------------------------------------------------------------------
       
   402 // CCommEngine::CommReadOneOrMore
       
   403 // other items were commented in a header
       
   404 // ---------------------------------------------------------------------------
       
   405 void CCommEngine::CommReadOneOrMore()
       
   406 	{
       
   407 	if(!iCommCancel)
       
   408 		{
       
   409 		iCommPort.ReadOneOrMore(iCommReader->StatusRef(), iRxBuf);
       
   410 		iCommReader->Activate();
       
   411 		}
       
   412 	}
       
   413 
       
   414 // ---------------------------------------------------------------------------
       
   415 // CCommEngine::CommCancel
       
   416 // other items were commented in a header
       
   417 // ---------------------------------------------------------------------------
       
   418 void CCommEngine::CommCancel()
       
   419 	{
       
   420 	iCommCancel = ETrue;
       
   421 	if(iCallbackTimer->IsActive())
       
   422 	    {
       
   423 		iCallbackTimer->Cancel();
       
   424 		}
       
   425 		
       
   426 	if (NULL != iCommWriter)
       
   427 		{
       
   428 		iCommWriter->Cancel();
       
   429 		}
       
   430 	
       
   431 	if (NULL != iCommReader)
       
   432 		{
       
   433 		iCommReader->Cancel();
       
   434 		}
       
   435 	}
       
   436 
       
   437 // ---------------------------------------------------------------------------
       
   438 // CCommEngine::CommWriteCancel
       
   439 // other items were commented in a header
       
   440 // ---------------------------------------------------------------------------
       
   441 void CCommEngine::CommWriteCancel()
       
   442 	{
       
   443 	if(iCallbackTimer->IsActive())
       
   444 	    {
       
   445 		iCallbackTimer->Cancel();
       
   446 		}
       
   447 		
       
   448 	if (NULL != iCommWriter)
       
   449 		{
       
   450 		iCommWriter->Cancel();
       
   451 		}
       
   452 	}
       
   453 
       
   454 // ---------------------------------------------------------------------------
       
   455 // CCommEngine::CommReadCancel
       
   456 // other items were commented in a header
       
   457 // ---------------------------------------------------------------------------
       
   458 void CCommEngine::CommReadCancel()
       
   459 	{
       
   460 	if(iCallbackTimer->IsActive())
       
   461 	    {
       
   462 		iCallbackTimer->Cancel();
       
   463 		}
       
   464 		
       
   465 	if (NULL != iCommReader)
       
   466 		{
       
   467 		iCommReader->Cancel();
       
   468 		}
       
   469 	}
       
   470 
       
   471 // ---------------------------------------------------------------------------
       
   472 // CCommEngine::CommReadComplete
       
   473 // other items were commented in a header
       
   474 // ---------------------------------------------------------------------------
       
   475 void CCommEngine::CommReadComplete(TInt aStatus)
       
   476 	{
       
   477 	LOGTEXT(_L8("[Ltsy] Starting CCommEngine::CommReadComplete()"));
       
   478 	LOGTEXT2(_L8("[Ltsy] aStatus = %d"), aStatus);
       
   479 	LOGTEXT2(_L8("Received %d"),iRxBuf.Length());
       
   480 	LOGTEXTREL2(_L8("Rx:\t%S"),&iRxBuf);
       
   481 		
       
   482 	if (KErrNone == aStatus)
       
   483 		{
       
   484 		if (NULL != iStringParse)
       
   485 			{
       
   486 			iStringParse->ProcessReadString(iRxBuf);
       
   487 			}
       
   488 			
       
   489 		CommReadOneOrMore();	
       
   490 		}
       
   491 	else
       
   492 		{
       
   493 		if (NULL != iEngineObserver)
       
   494 			{
       
   495 			iEngineObserver->ReadOrWriteComplete(EReadCompletion, aStatus);
       
   496 			}
       
   497 		}
       
   498 	LOGTEXT(_L8("[Ltsy] End CCommEngine::CommReadComplete()"));
       
   499 	}
       
   500 
       
   501 // ---------------------------------------------------------------------------
       
   502 // CCommEngine::CommWriteComplete
       
   503 // other items were commented in a header
       
   504 // ---------------------------------------------------------------------------
       
   505 void CCommEngine::CommWriteComplete(TInt aStatus)
       
   506 	{
       
   507 	LOGTEXT(_L8("[Ltsy] Starting CCommEngine::CommWriteComplete()"));
       
   508 	LOGTEXT2(_L8("[Ltsy] aStatus = %d"), aStatus);
       
   509 	if(iCallbackTimer->IsActive())
       
   510 	    {
       
   511 		iCallbackTimer->Cancel();
       
   512 		}
       
   513 		
       
   514 	if (NULL != iEngineObserver)
       
   515 		{
       
   516 		iEngineObserver->ReadOrWriteComplete(EWriteCompletion, aStatus);
       
   517 		}
       
   518 	}
       
   519 
       
   520 // ---------------------------------------------------------------------------
       
   521 // CCommEngine::GetStringParse
       
   522 // other items were commented in a header
       
   523 // ---------------------------------------------------------------------------
       
   524 CStringParser* CCommEngine::GetStringParse() const
       
   525 	{
       
   526 	return iStringParse;
       
   527 	}
       
   528 
       
   529 // ---------------------------------------------------------------------------
       
   530 // CCommEngine::SetCommReadLineNotify
       
   531 // other items were commented in a header
       
   532 // ---------------------------------------------------------------------------
       
   533 void CCommEngine::SetCommReadLineNotify(MCommReadLineNotifier *aReadLineNotify)
       
   534 	{
       
   535 	iStringParse->SetReadLineNotify(aReadLineNotify);
       
   536 	}
       
   537 
       
   538 // ---------------------------------------------------------------------------
       
   539 // CCommEngine::SetCommEngineObserver
       
   540 // other items were commented in a header
       
   541 // ---------------------------------------------------------------------------
       
   542 void CCommEngine::SetCommEngineObserver(MCommEngineObserver *aEngineObserver)
       
   543 	{
       
   544 	iEngineObserver = aEngineObserver;
       
   545 	}
       
   546 
       
   547 // ---------------------------------------------------------------------------
       
   548 // CCommEngine::TimerRun
       
   549 // other items were commented in a header
       
   550 // ---------------------------------------------------------------------------
       
   551 void CCommEngine::TimerRun(TInt aError)
       
   552     {
       
   553 	if(aError == KErrNone)
       
   554 	    {
       
   555 		iEngineObserver->ReadOrWriteComplete(ETimeoutCompletion, KErrTimedOut);
       
   556 		}
       
   557 	}
       
   558 
       
   559 // ---------------------------------------------------------------------------
       
   560 // CCommEngine::StartTimer
       
   561 // other items were commented in a header
       
   562 // ---------------------------------------------------------------------------
       
   563 void CCommEngine::StartTimer(TInt aSeconds)
       
   564     {
       
   565     if (iCallbackTimer->IsActive())
       
   566     	{
       
   567     	iCallbackTimer->Cancel();
       
   568     	}
       
   569 	iCallbackTimer->After(aSeconds*KLtsyOneSecondPause);
       
   570 	}
       
   571 
       
   572 // ---------------------------------------------------------------------------
       
   573 // CCommEngine::StopTimer
       
   574 // other items were commented in a header
       
   575 // ---------------------------------------------------------------------------
       
   576 void CCommEngine::StopTimer()	
       
   577     {
       
   578 	iCallbackTimer->Cancel();
       
   579 	}
       
   580 
       
   581 // End of file