messagingfw/msgtest/targetautomation/SerialLog/T_seriallog.cpp
changeset 0 8e480a14352b
equal deleted inserted replaced
-1:000000000000 0:8e480a14352b
       
     1 // Copyright (c) 1998-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 //
       
    15 
       
    16 #include <e32std.h>
       
    17 #include <bacline.h>
       
    18 #include <d32comm.h>
       
    19 #include <e32test.h>
       
    20 #include <f32file.h>
       
    21 
       
    22 RTest test(_L("T_SERIALLOG"));
       
    23 
       
    24 const TInt KSerialLogBufferSize=1024;
       
    25 const TInt KSerialLogDefaultSerialPort=0;
       
    26 const TInt KSerialLogTransmitTimeout=1000000;
       
    27 const TInt KSerialLogErrorDisplayWait=5000000;
       
    28 _LIT8(KSerialLogStartDelimiterFormat,"[=====> START %S]\n");
       
    29 _LIT8(KSerialLogStopDelimiterFormat, "[=====> STOP %S]\n");
       
    30 
       
    31 LOCAL_C void ReportError(TInt aError)
       
    32 	{
       
    33 	test.Console()->Printf(_L("\n\n"));
       
    34 	switch (aError)
       
    35 		{
       
    36 		case KErrArgument:
       
    37 			test.Console()->Printf(_L("Usage: t_seriallog logfilename [serial port]\n"));
       
    38 			break;
       
    39 		case KErrTimedOut:
       
    40 			test.Console()->Printf(_L("ERROR: Timeout during serial write\n"));
       
    41 			break;
       
    42 		case KErrNotFound:
       
    43 			test.Console()->Printf(_L("ERROR: File/Port not found\n"));
       
    44 			break;
       
    45 		case KErrNotSupported:
       
    46 			test.Console()->Printf(_L("ERROR: Port not found/supported\n"));
       
    47 			break;
       
    48 		default:
       
    49 			test.Console()->Printf(_L("ERROR: %d\n"),aError);
       
    50 			break;
       
    51 		}
       
    52 	if (aError)
       
    53 		User::After(KSerialLogErrorDisplayWait);
       
    54 	}
       
    55 
       
    56 LOCAL_C TInt SetSerialConfiguration(RBusDevComm& aSerial)
       
    57 	{
       
    58 	TCommConfigV01 theConfig;
       
    59 	TCommConfig config(theConfig);
       
    60 	aSerial.Config(config);
       
    61 	config().iRate=EBps115200;
       
    62 	config().iParity=EParityNone;
       
    63 	config().iDataBits=EData8;
       
    64 	config().iStopBits=EStop1;
       
    65 	return(aSerial.SetConfig(config));
       
    66 	}
       
    67 
       
    68 LOCAL_C TInt SendSerialData(RBusDevComm& aSerial,const TDesC8& aData)
       
    69 	{
       
    70 	TRequestStatus serStat;
       
    71 	TRequestStatus timStat;
       
    72 
       
    73 	RTimer timer;
       
    74 	TInt error=timer.CreateLocal();
       
    75 	if (!error)
       
    76 		{
       
    77 		timer.After(timStat,KSerialLogTransmitTimeout);
       
    78 		aSerial.Write(serStat,aData);
       
    79 		User::WaitForRequest(serStat,timStat);
       
    80 		if (timStat.Int()==KErrNone)
       
    81 			{
       
    82 			aSerial.WriteCancel();
       
    83 			error=KErrTimedOut;
       
    84 			}
       
    85 		else if (serStat.Int()!=KErrNone)
       
    86 			{
       
    87 			timer.Cancel();
       
    88 			error=serStat.Int();
       
    89 			}
       
    90 		else
       
    91 			timer.Cancel();
       
    92 		timer.Close();
       
    93 		}
       
    94 	return(error);
       
    95 	}
       
    96 
       
    97 LOCAL_C TInt SendDelimiterL(RBusDevComm& aSerial,const TDesC8& aFormatter,const TPtrC& aLogFile)
       
    98 	{
       
    99 	TBuf8<KSerialLogBufferSize> aLogFileDes8;
       
   100 	for (TInt i=0;i<aLogFile.Length();i++)
       
   101 		aLogFileDes8.Append((TChar)aLogFile[i]);
       
   102 	TBuf8<KSerialLogBufferSize> buffer;
       
   103 	buffer.Format(aFormatter,&aLogFileDes8);
       
   104 	return(SendSerialData(aSerial,buffer));
       
   105 	}
       
   106 
       
   107 LOCAL_C void doOpenSerialL(RBusDevComm& aSerial, TInt aPort)
       
   108 	{
       
   109 	TInt error;
       
   110 #if defined (__WINS__)
       
   111 	error=User::LoadPhysicalDevice(_L("ECDRV"));
       
   112 #else
       
   113 	error=User::LoadPhysicalDevice(_L("EUART1"));
       
   114 	if (error==KErrNone||error==KErrAlreadyExists)
       
   115 		error=User::LoadPhysicalDevice(_L("EUART2"));
       
   116 	if (error==KErrNone||error==KErrAlreadyExists)
       
   117 		error=User::LoadPhysicalDevice(_L("EUART3"));
       
   118 	if (error==KErrNone||error==KErrAlreadyExists)
       
   119 		error=User::LoadPhysicalDevice(_L("EUART4"));
       
   120 #endif
       
   121 	if (error==KErrNone||error==KErrAlreadyExists||error==KErrNotFound)
       
   122 		error=User::LoadLogicalDevice(_L("ECOMM"));
       
   123 	if (error==KErrAlreadyExists)
       
   124 		error=KErrNone;
       
   125 	User::LeaveIfError(error);
       
   126 
       
   127 	test.Printf(_L("Loaded serial device drivers.\n"));
       
   128 
       
   129 	// Open serial port.
       
   130 	User::LeaveIfError(aSerial.Open(aPort));
       
   131 	User::LeaveIfError(SetSerialConfiguration(aSerial));
       
   132 
       
   133 	test.Printf(_L("Opened serial port.\n"));
       
   134 	}
       
   135 
       
   136 LOCAL_C void doSendTerminateL(TInt aPort)
       
   137 	{
       
   138 	// Open serial port.
       
   139 	RBusDevComm serial;
       
   140 	doOpenSerialL(serial,aPort);
       
   141 	SendSerialData(serial, _L8("!TERMINATE_MSG_LOGGING!"));
       
   142 	// Close serial port.
       
   143 	serial.Close();
       
   144 	}
       
   145 
       
   146 LOCAL_C void doSendSerialL(const TPtrC& aLogFile, TInt aPort)
       
   147 	{
       
   148 	TInt error;
       
   149 	RFs aFs;
       
   150 	User::LeaveIfError(aFs.Connect());
       
   151 
       
   152 	// Open logfile.
       
   153 	RFile logFile;
       
   154 	test.Printf(_L("\nSending file: %S.\n"),&aLogFile);
       
   155 	User::LeaveIfError(logFile.Open(aFs,aLogFile,EFileRead));
       
   156 
       
   157 	// Open serial port.
       
   158 	RBusDevComm serial;
       
   159 	doOpenSerialL(serial, aPort);
       
   160 
       
   161 	// Send the start delimiter.
       
   162 	TBuf8<KSerialLogBufferSize> buffer;
       
   163 	error=SendDelimiterL(serial,KSerialLogStartDelimiterFormat,aLogFile);
       
   164 	
       
   165 	// Send the logfile.
       
   166 	while (!error)
       
   167 		{
       
   168 		error=logFile.Read(buffer);
       
   169 		if (!error && buffer.Length())
       
   170 			error=SendSerialData(serial,buffer);
       
   171 		if (!buffer.Length())
       
   172 			error=KErrEof;
       
   173 		}
       
   174 
       
   175 	// Any errors?
       
   176 	if (error==KErrEof)
       
   177 		error=KErrNone;
       
   178 
       
   179 	// Send the stop delimiter.
       
   180 	if (!error)
       
   181 		error=SendDelimiterL(serial,KSerialLogStopDelimiterFormat,aLogFile);
       
   182 	User::After(1000000);
       
   183 
       
   184 	// Close serial port.
       
   185 	serial.Close();
       
   186 	// Close logfile.
       
   187 	logFile.Close();
       
   188 
       
   189 	User::LeaveIfError(error);
       
   190 	}
       
   191 
       
   192 LOCAL_C void doMainL()
       
   193 	{
       
   194 	// Get command line.
       
   195 	CCommandLineArguments* parsed=CCommandLineArguments::NewLC();
       
   196 	// Check for numerical arguments.
       
   197 	TInt error=KErrNone;
       
   198 	if (parsed->Count()>=2)
       
   199 		{
       
   200 		//  Argument 1 = log file name [Argument 2 = serial port (defaults to 0)]
       
   201 		TInt serialPort=KSerialLogDefaultSerialPort;
       
   202 		if (parsed->Count()==3)
       
   203 			{
       
   204 			TLex convArg(parsed->Arg(2));
       
   205 			TInt parsedSerialPort;
       
   206 			if (convArg.Val(parsedSerialPort)==KErrNone)
       
   207 				serialPort=parsedSerialPort;
       
   208 			else
       
   209 				error=KErrArgument;
       
   210 			}
       
   211 		if (parsed->Count()>3)
       
   212 			error=KErrArgument;
       
   213 		if (!error)
       
   214 			{
       
   215 			if (parsed->Arg(1).Compare(_L("TERMINATE")) == 0)
       
   216 				doSendTerminateL(serialPort);
       
   217 			else
       
   218 				doSendSerialL(parsed->Arg(1),serialPort);
       
   219 			}
       
   220 		}
       
   221 	else
       
   222 		{
       
   223 		error=KErrArgument;
       
   224 		}
       
   225 
       
   226 	CleanupStack::PopAndDestroy(parsed);
       
   227 	User::LeaveIfError(error);
       
   228 	}
       
   229 
       
   230 GLDEF_C TInt E32Main()
       
   231 	{	
       
   232 	__UHEAP_MARK;
       
   233 //	HAL::SetAutoSwitchOffBehavior(ESwitchOffDisabled);	// Can't do this anymore :<
       
   234 	User::ResetInactivityTime();
       
   235 	test.Title();
       
   236 	CTrapCleanup* theCleanup=CTrapCleanup::New();
       
   237 	TRAPD(result,doMainL()); 
       
   238 	ReportError(result);
       
   239 	delete theCleanup;      
       
   240 	test.Close();
       
   241 	__UHEAP_MARKEND;
       
   242 	return(KErrNone);
       
   243 	}