testexecfw/tef/utils/src/serialwriter.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 #include <e32std.h>
       
    21 #include "SerialWriter.h"
       
    22 #include "TEFConst.h"
       
    23 #include <TEFExportConst.h>
       
    24 
       
    25 #if defined (__WINS__)
       
    26 _LIT(KPddDev, "ECDRV.PDD");
       
    27 #else
       
    28 _LIT(KPddDev, "EUART1");
       
    29 #endif
       
    30 
       
    31 #if defined (__WINS__)
       
    32 _LIT(KLddDev, "ECOMM.LDD");
       
    33 #else
       
    34 _LIT(KLddDev, "ECOMM");
       
    35 #endif
       
    36 
       
    37 
       
    38 /**
       
    39 Creates an instance of CSerialWriter object
       
    40 @return a pointer to the new created CSerialWriter Object
       
    41 @leave KErrNoMemory if no memory
       
    42 */
       
    43  CSerialWriter* CSerialWriter::NewL()
       
    44 	{
       
    45 	CSerialWriter *me = new (ELeave) CSerialWriter;
       
    46 	CleanupStack::PushL(me);
       
    47 	me->ConstructL();
       
    48 	CleanupStack::Pop();
       
    49 	return me;
       
    50 	}
       
    51 
       
    52 /**
       
    53 Public Destructor
       
    54 */
       
    55 CSerialWriter::~CSerialWriter()
       
    56 	{
       
    57     Disconnect();
       
    58 	}
       
    59 
       
    60 //default constructor
       
    61 CSerialWriter::CSerialWriter()
       
    62 	{
       
    63 	}
       
    64 
       
    65 void CSerialWriter::ConstructL()
       
    66 	{
       
    67 	iReady = EFalse;
       
    68 	
       
    69     TInt err = User::LoadPhysicalDevice(KPddDev);
       
    70     if(err!=KErrNone && err!=KErrAlreadyExists)
       
    71         User::Leave(err);
       
    72 
       
    73     err = User::LoadLogicalDevice(KLddDev);
       
    74     if(err!=KErrNone && err!=KErrAlreadyExists)
       
    75         User::Leave(err);
       
    76  	}
       
    77 
       
    78 TInt CSerialWriter::Connect()
       
    79     {
       
    80     if(!iReady)
       
    81         {
       
    82         TInt err = KErrNone;
       
    83         err = iComm.Open(iPortNum);
       
    84         if(err!=KErrNone)
       
    85         	{
       
    86        		iReady = EFalse;
       
    87             return err;
       
    88         	}
       
    89         	else
       
    90 		        iReady = ETrue;
       
    91         }
       
    92     return KErrNone;
       
    93     }
       
    94 
       
    95 TInt CSerialWriter::Config()
       
    96     {
       
    97     TInt err = KErrNone;
       
    98     if(iReady)
       
    99         {
       
   100         TCommConfig2 cfgBuf;
       
   101         TCommConfigV02& cfg = cfgBuf();
       
   102         iComm.Config(cfgBuf);
       
   103         cfg.iRate = EBps115200;// default EBps9600;
       
   104         cfg.iDataBits = EData8;// default EData8;
       
   105         cfg.iStopBits = EStop1;// default EStop1;
       
   106         cfg.iParity = EParityNone;// default EParityNone;
       
   107         cfg.iHandshake = 0;// default KConfigObeyCTS;
       
   108         cfg.iParityError = KConfigParityErrorFail;
       
   109         cfg.iFifo = EFifoEnable;// default EFifoEnable;
       
   110         cfg.iSpecialRate = 0;// default 0;
       
   111         cfg.iTerminatorCount = 0;// default 0;
       
   112         cfg.iXonChar = 0x11;// default 0x11; // XON
       
   113         cfg.iXoffChar = 0x13;// default 0x13; // XOFF
       
   114         cfg.iParityErrorChar = 0;// default 0;
       
   115         cfg.iSIREnable = ESIRDisable;// default ESIRDisable;
       
   116         cfg.iSIRSettings = 0;// no default
       
   117         
       
   118         err = iComm.SetConfig(cfgBuf);
       
   119         if(err!=KErrNone && err!=KErrInUse)
       
   120             {
       
   121             iReady = EFalse;
       
   122             return(err);
       
   123             }
       
   124         }
       
   125     
       
   126     return err;
       
   127     }
       
   128 
       
   129 TInt CSerialWriter::Disconnect()
       
   130     {
       
   131     if(iReady)
       
   132         {
       
   133 	    iReady = EFalse;
       
   134         iComm.Close();
       
   135         }
       
   136     return KErrNone;
       
   137     }
       
   138 
       
   139 TInt CSerialWriter::Write(const TDesC8& aData)
       
   140 	{
       
   141 	TInt err = KErrNone;
       
   142 	
       
   143 	if(iReady)
       
   144 		{
       
   145 		TRequestStatus writeStatus;
       
   146 	    iComm.Write(writeStatus, aData);
       
   147     	User::WaitForRequest(writeStatus);
       
   148     	err = writeStatus.Int();  	
       
   149 		}
       
   150 
       
   151    	return err;
       
   152 	}
       
   153 
       
   154 void CSerialWriter::AddTime(TDes8& aLogBuffer)
       
   155 	{
       
   156 	TTime now;
       
   157 	now.UniversalTime();
       
   158 	TDateTime dateTime = now.DateTime();
       
   159 	_LIT8(KFormat,"%02d:%02d:%02d:%03d ");
       
   160 	// add the current time 
       
   161 	aLogBuffer.Append(KTEFNewLine) ; 
       
   162 	aLogBuffer.AppendFormat(KFormat,dateTime.Hour(),dateTime.Minute(),dateTime.Second(),(dateTime.MicroSecond()/1000)); 
       
   163 	}
       
   164 
       
   165 void CSerialWriter::AddSeverity(TDes8& aLogBuffer, const TInt& aSeverity)
       
   166 	{
       
   167 	if (aSeverity < 1) 
       
   168 		{
       
   169 		return;
       
   170 		}
       
   171 
       
   172 	_LIT(KErr,"ERROR - ");
       
   173 	_LIT(KHigh,"HIGH - ");
       
   174 	_LIT(KWarn,"WARN - ");
       
   175 	_LIT(KMedium,"MEDIUM - ");
       
   176 	_LIT(KInfo,"INFO - ");
       
   177 	_LIT(KLow,"LOW - ");
       
   178 
       
   179 	if(aSeverity == ESevrErr)
       
   180 		aLogBuffer.Append(KErr);
       
   181 	else if(aSeverity == ESevrHigh)
       
   182 		aLogBuffer.Append(KHigh);
       
   183 	else if(aSeverity == ESevrWarn)
       
   184 		aLogBuffer.Append(KWarn);
       
   185 	else if(aSeverity == ESevrMedium)
       
   186 		aLogBuffer.Append(KMedium);
       
   187 	else if (aSeverity == ESevrInfo)
       
   188 		aLogBuffer.Append(KInfo);
       
   189 	else if(aSeverity == ESevrLow)
       
   190 		aLogBuffer.Append(KLow);
       
   191 	else //if(aSeverity == ESevrAll)
       
   192 		aLogBuffer.Append(KInfo);
       
   193 
       
   194 	}
       
   195 
       
   196 
       
   197 /**
       
   198 Decorates the logs with Current Time and Severity 
       
   199 @returns Error code if any during writing
       
   200 @leave KErrNoMemory if no memory
       
   201 */
       
   202 TInt CSerialWriter::WriteDecorated(const TDesC8& aText, TInt aSeverity)	
       
   203 {
       
   204 	TInt ret = KErrNone; 	
       
   205 	HBufC8* buffer = HBufC8::New(aText.Length()+30);
       
   206 	if(buffer)
       
   207 		{
       
   208 		TPtr8 ptr(buffer->Des());
       
   209 		AddTime(ptr);
       
   210 		AddSeverity(ptr,aSeverity) ; 
       
   211 		ptr.Append(aText);
       
   212 		// Ignore error for the time being. Could do an ASSERT
       
   213 		ret = Write(ptr) ; 
       
   214 		delete buffer;
       
   215 		}
       
   216 	return ret ; 	
       
   217 }
       
   218 
       
   219 /**
       
   220   Overloaded for TDesC
       
   221   @returns Error code if any during writing
       
   222   @leave KErrNoMemory if no memory
       
   223   */
       
   224   TInt CSerialWriter::WriteDecorated(const TDesC& aText, TInt aSeverity)
       
   225   {
       
   226   	TPtrC8 representation((TUint8*)(&aText)->Ptr(), (&aText)->Size());
       
   227   	return WriteDecorated(representation,  aSeverity )	;
       
   228   }
       
   229 
       
   230 
       
   231 TInt CSerialWriter::Settings(const TInt& aPortNumber)
       
   232 	{
       
   233 	  	iPortNum = aPortNumber ; 
       
   234 	  	Disconnect();
       
   235 	  	TInt err = Connect();
       
   236 	  	if(err != KErrNone)
       
   237 	  		return err;
       
   238 	  	err = Config();
       
   239 	  	return err;
       
   240 	}