dbgagents/trkagent/engine/TrkSerialCommPort.cpp
changeset 0 c6b0df440bee
equal deleted inserted replaced
-1:000000000000 0:c6b0df440bee
       
     1 /*
       
     2 * Copyright (c) 2004 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 #include <e32cons.h>
       
    20 #include <f32file.h>
       
    21 
       
    22 #include "TrkEngineModel.h"
       
    23 #include "TrkSerialCommPort.h"
       
    24 #include "TrkFramingLayer.h"
       
    25 
       
    26 
       
    27 //
       
    28 // Static helper functions
       
    29 //
       
    30 static void BuildPortName(const TDesC& aModuleName, TUint aUnit, TDes& aCompleteName)
       
    31 {
       
    32 	aCompleteName.Zero();
       
    33 	aCompleteName.Append(aModuleName);
       
    34 	aCompleteName.AppendFill(':', 2);
       
    35 	aCompleteName.AppendNum(aUnit);
       
    36 }
       
    37 
       
    38 static TUint RateCapacityBitMaskFromRate(TUint aRate)
       
    39 {
       
    40 	switch (aRate)
       
    41 	{
       
    42 		case 50:      return KCapsBps50;
       
    43 		case 75:      return KCapsBps75;
       
    44 		case 110:     return KCapsBps110;
       
    45 		case 134:     return KCapsBps134;
       
    46 		case 150:     return KCapsBps150;
       
    47 		case 300:     return KCapsBps300;
       
    48 		case 600:     return KCapsBps600;
       
    49 		case 1200:    return KCapsBps1200;
       
    50 		case 1800:    return KCapsBps1800;
       
    51 		case 2000:    return KCapsBps2000;
       
    52 		case 2400:    return KCapsBps2400;
       
    53 		case 3600:    return KCapsBps3600;
       
    54 		case 4800:    return KCapsBps4800;
       
    55 		case 7200:    return KCapsBps7200;
       
    56 		case 9600:    return KCapsBps9600;
       
    57 		case 19200:   return KCapsBps19200;
       
    58 		case 38400:   return KCapsBps38400;
       
    59 		case 57600:   return KCapsBps57600;
       
    60 		case 115200:  return KCapsBps115200;
       
    61 		case 230400:  return KCapsBps230400;
       
    62 		case 460800:  return KCapsBps460800;
       
    63 		case 576000:  return KCapsBps576000;
       
    64 		case 1152000: return KCapsBps1152000;
       
    65 		case 4000000: return KCapsBps4000000;
       
    66 		default:      return 0;
       
    67 	}
       
    68 }
       
    69 
       
    70 static TBps TBpsRateFromRate(TUint aRate)
       
    71 {
       
    72 	switch (aRate)
       
    73 	{
       
    74 		case 50:      return EBps50;
       
    75 		case 75:      return EBps75;
       
    76 		case 110:     return EBps110;
       
    77 		case 134:     return EBps134;
       
    78 		case 150:     return EBps150;
       
    79 		case 300:     return EBps300;
       
    80 		case 600:     return EBps600;
       
    81 		case 1200:    return EBps1200;
       
    82 		case 1800:    return EBps1800;
       
    83 		case 2000:    return EBps2000;
       
    84 		case 2400:    return EBps2400;
       
    85 		case 3600:    return EBps3600;
       
    86 		case 4800:    return EBps4800;
       
    87 		case 7200:    return EBps7200;
       
    88 		case 9600:    return EBps9600;
       
    89 		case 19200:   return EBps19200;
       
    90 		case 38400:   return EBps38400;
       
    91 		case 57600:   return EBps57600;
       
    92 		case 115200:  return EBps115200;
       
    93 		case 230400:  return EBps230400;
       
    94 		case 460800:  return EBps460800;
       
    95 		case 576000:  return EBps576000;
       
    96 		case 1152000: return EBps1152000;
       
    97 		case 4000000: return EBps4000000;
       
    98 		default:      return EBpsAutobaud;
       
    99 	}
       
   100 }
       
   101 
       
   102 
       
   103 //
       
   104 //
       
   105 // CTrkSerialCommPort implementation
       
   106 //
       
   107 //
       
   108 
       
   109 //
       
   110 // CTrkSerialCommPort constructor
       
   111 //
       
   112 CTrkSerialCommPort::CTrkSerialCommPort()
       
   113 	: CTrkCommPort(CActive::EPriorityStandard),
       
   114 	  iPDD(NULL),
       
   115 	  iPDD2(NULL),
       
   116 	  iLDD(NULL),
       
   117 	  iCSY(NULL),
       
   118 	  iServerStarted(EFalse),
       
   119 	  iConnected(EFalse),
       
   120 	  iListening(EFalse),
       
   121 	  iLineNumber(0)
       
   122 {
       
   123 }
       
   124 
       
   125 //
       
   126 // CTrkSerialCommPort::ConstructL
       
   127 //
       
   128 void CTrkSerialCommPort::ConstructL(TTrkConnData& aTrkConnData , TDes& aErrorMessage)
       
   129 {
       
   130 	iPDD = aTrkConnData.iPDD;
       
   131 	iLDD = aTrkConnData.iLDD;
       
   132 	iCSY = aTrkConnData.iCSY;
       
   133 	iUnitNumber = aTrkConnData.iPortNumber;
       
   134 	iRate = aTrkConnData.iRate;
       
   135 	
       
   136 	
       
   137 	TBuf<KMaxPath> initFile(KInitFilePath);
       
   138 	initFile.Append(KInitFileName);
       
   139 	
       
   140 	if (!iPDD.Size())
       
   141 	{
       
   142 		aErrorMessage.Format(_L("PDD not specified in %S.\r\n"), &initFile);
       
   143 			User::Leave(KErrCorrupt);
       
   144 		}
       
   145 
       
   146 	if (!iLDD.Size())
       
   147 	{
       
   148 		aErrorMessage.Format(_L("LDD not specified in %S.\r\n"), &initFile);		
       
   149 			User::Leave(KErrCorrupt);
       
   150 		}
       
   151 
       
   152 	if (!iCSY.Size())
       
   153 	{
       
   154 		aErrorMessage.Format(_L("CSY not specified in %S.\r\n"), &initFile);		
       
   155 			User::Leave(KErrCorrupt);
       
   156 		}
       
   157 
       
   158 		if (!iRate)
       
   159 		{
       
   160 		aErrorMessage.Format(_L("RATE not specified in %S.\r\n"), &initFile);		
       
   161 			User::Leave(KErrCorrupt);
       
   162 		}
       
   163 	
       
   164 	iPDD2.Format(iPDD, iUnitNumber+1);
       
   165 		
       
   166 	iConnectionMessage.Format(_L("PDD: %S\r\nLDD: %S\r\nCSY: %S\r\nPort number: %d\r\nBaud rate: %d\r\n"), &iPDD2, &iLDD, &iCSY, iUnitNumber, iRate);
       
   167 
       
   168 	if (aTrkConnData.iDefault)
       
   169 	{
       
   170 		iConnectionMessage.Insert(0, _L("No init file found, using default values.\r\n"));
       
   171 	}
       
   172 }
       
   173 
       
   174 //
       
   175 // CTrkSerialCommPort destructor
       
   176 //
       
   177 CTrkSerialCommPort::~CTrkSerialCommPort()
       
   178 {
       
   179 	// make sure we cancel the request for data before shutting down
       
   180 	Cancel();
       
   181 
       
   182 	iPort.Close();
       
   183 	iConnected = EFalse;
       
   184 	iServer.Close();
       
   185 	iServerStarted = EFalse;	
       
   186 }
       
   187 
       
   188 //
       
   189 // CTrkSerialCommPort::NewL
       
   190 //
       
   191 CTrkSerialCommPort* CTrkSerialCommPort::NewL(TTrkConnData& aTrkConnData, TDes& aErrorMessage)
       
   192 {
       
   193 	CTrkSerialCommPort* self = new(ELeave) CTrkSerialCommPort;
       
   194 	CleanupStack::PushL(self);
       
   195 	self->ConstructL(aTrkConnData, aErrorMessage);
       
   196 	CleanupStack::Pop(self);
       
   197 	return self;
       
   198 }
       
   199 
       
   200 //
       
   201 // CTrkSerialCommPort::OpenPortL
       
   202 //
       
   203 // Open the serial type communications port
       
   204 //
       
   205 void CTrkSerialCommPort::OpenPortL()
       
   206 {
       
   207 	TInt error = KErrNone;
       
   208 
       
   209 	if (!iServerStarted)
       
   210 	{
       
   211 		StartC32();
       
   212 		ReportAndLeaveIfErrorL(iServer.Connect(),
       
   213 							  _L("Failed to connect to RCommServ."));
       
   214 		iServerStarted = ETrue;
       
   215 	}
       
   216 
       
   217 	// load the csy module
       
   218 	ReportAndLeaveIfErrorL(iServer.LoadCommModule(iCSY), _L("Failed to load CSY module."));
       
   219 
       
   220 	TInt ports = 0;
       
   221 	ReportAndLeaveIfErrorL(iServer.NumPorts(ports),
       
   222 						   _L("Unable to obtain information about port."));
       
   223 
       
   224 	// make sure the unit number is in range
       
   225 	TSerialInfo serialInfo;
       
   226 
       
   227 	error = KErrNotFound;
       
   228 	
       
   229 	TBuf<32> csyModule;
       
   230 	csyModule = iCSY;
       
   231 	csyModule.LowerCase();
       
   232 	for (TInt i=0; i<ports && error; i++)
       
   233 	{
       
   234 		TBuf<32> module;
       
   235 		ReportAndLeaveIfErrorL(iServer.GetPortInfo(i, module, serialInfo),
       
   236 							   _L("Unable to obtain information about port."));		
       
   237 		module.LowerCase();
       
   238 		if (module == csyModule)
       
   239 			error = KErrNone;
       
   240 	}
       
   241 
       
   242 	ReportAndLeaveIfErrorL(error, _L("Unable to obtain port information for comm module."));
       
   243 	
       
   244 	//If pdd is "NONE", don't try to load it.
       
   245 	//This is true incase of USB, where the USB pdd is a kernel extension on most of the phones
       
   246 	//and so can't be loaded.
       
   247 	TBuf<10> defaultUsbPdd;
       
   248 	defaultUsbPdd.Copy(KDefaultUsbPDD);	
       
   249 	if (iPDD2.Compare(defaultUsbPdd))
       
   250 	{
       
   251 		error = User::LoadPhysicalDevice(iPDD2);
       
   252 		if (error != KErrNone && error != KErrAlreadyExists)
       
   253 			ReportAndLeaveIfErrorL(error, _L("Failed to load physical device."));
       
   254 	}
       
   255 
       
   256 	error = User::LoadLogicalDevice(iLDD);
       
   257 	if (error != KErrNone && error != KErrAlreadyExists)
       
   258 		ReportAndLeaveIfErrorL(error, _L("Failed to load logical device."));
       
   259 
       
   260 	// Open port (try first in DTE mode and then if unsuccessful in
       
   261 	// DCE mode - necessary for cogent).
       
   262 
       
   263 	TBuf<KMaxPortName+4> portName;
       
   264 	BuildPortName(serialInfo.iName, iUnitNumber, portName);
       
   265 	error = iPort.Open(iServer, portName, ECommExclusive, ECommRoleDTE);
       
   266 	if (error != KErrNone)
       
   267 	{
       
   268 		ReportAndLeaveIfErrorL(iPort.Open(iServer, portName, ECommExclusive, ECommRoleDCE),
       
   269 							   _L("Failed to open port."));
       
   270 	}
       
   271 	iConnected = ETrue;
       
   272 
       
   273 	// Check Speed
       
   274 	TCommCaps caps;
       
   275 	iPort.Caps(caps);
       
   276 
       
   277 	TUint rateBitMask = RateCapacityBitMaskFromRate(iRate);
       
   278 	if (rateBitMask == 0 || (rateBitMask & caps().iRate) == 0)
       
   279 		ReportAndLeaveIfErrorL(KErrGeneral, _L("Unsupported baud rate."));
       
   280 
       
   281 	// Configure physical and logical characteristics
       
   282 	TCommConfig config;
       
   283 	iPort.Config(config);
       
   284 	config().iRate = TBpsRateFromRate(iRate);
       
   285 	config().iParity = EParityNone;
       
   286 	config().iDataBits = EData8;
       
   287 	config().iStopBits = EStop1;
       
   288 	config().iFifo = EFifoEnable;
       
   289 
       
   290 	config().iHandshake = 0;	// no flow control at all
       
   291 
       
   292 	ReportAndLeaveIfErrorL(iPort.SetConfig(config), _L("Failed to set port configuration."));
       
   293 
       
   294 	iPort.SetReceiveBufferLength(MAXMESSAGESIZE);
       
   295 
       
   296 	//reset the rx and tx buffers just in case 
       
   297 	//if there is any stale data lying around from the previous debug sessions
       
   298 	iPort.ResetBuffers(); 
       
   299 }
       
   300 
       
   301 //
       
   302 // CTrkSerialCommPort::ClosePort
       
   303 //
       
   304 // Close the communications port
       
   305 //
       
   306 void CTrkSerialCommPort::ClosePort()
       
   307 {
       
   308 	Cancel();
       
   309 	
       
   310 	if (iConnected)
       
   311 	{
       
   312 		iPort.Close();
       
   313 		iConnected = EFalse;
       
   314 	}
       
   315 
       
   316 	if (iServerStarted)
       
   317 	{
       
   318 		iServer.Close();
       
   319 		iServerStarted = EFalse;
       
   320 	}
       
   321 }
       
   322 
       
   323 //
       
   324 // CTrkSerialCommPort::SendDataL
       
   325 //
       
   326 // Write data to the serial type port
       
   327 //
       
   328 void CTrkSerialCommPort::SendDataL(const TDesC8& aBuffer)
       
   329 {
       
   330 	//_LIT8(KPrefix, "TrkToCarbide: ");
       
   331 	//PrintMessage(KPrefix(), aBuffer);
       
   332 	
       
   333 	TRequestStatus status;
       
   334 	iPort.Write(status, aBuffer);
       
   335 	User::WaitForRequest(status);
       
   336 	
       
   337 	User::LeaveIfError(status.Int());
       
   338 }
       
   339 
       
   340 //
       
   341 // CTrkSerialCommPort::Listen
       
   342 //
       
   343 // Start listening for data coming into the serial type communications port
       
   344 //
       
   345 void CTrkSerialCommPort::Listen(CTrkFramingLayer *aFramingLayer)
       
   346 {
       
   347 	iFramingLayer = aFramingLayer;
       
   348 	CActiveScheduler::Add(this);
       
   349 	IssueReadRequest();
       
   350 	iListening = ETrue;
       
   351 }
       
   352 
       
   353 //
       
   354 // CTrkSerialCommPort::StopListening
       
   355 //
       
   356 // Stop listening for data coming into the serial type communications port
       
   357 //
       
   358 void CTrkSerialCommPort::StopListening()
       
   359 {
       
   360 	if (iListening)
       
   361 	{
       
   362 		Cancel();
       
   363 		Deque();
       
   364 	}
       
   365 	
       
   366 	iListening = EFalse;
       
   367 }
       
   368 
       
   369 //
       
   370 // CTrkSerialCommPort::ReportAndLeaveIfErrorL
       
   371 //
       
   372 // If an error occurred, print the error information to the screen and bail out
       
   373 //
       
   374 void CTrkSerialCommPort::ReportAndLeaveIfErrorL(TInt aError, const TDesC& aDesc)
       
   375 {
       
   376 	if (KErrNone != aError)
       
   377 	{
       
   378 		iErrorMessage.Format(_L("%S\r\nError Code: %d\r\n"), &aDesc, aError);
       
   379 		User::Leave(aError);
       
   380 	}
       
   381 }
       
   382 
       
   383 //
       
   384 // CTrkSerialCommPort::IssueReadRequest
       
   385 //
       
   386 // Wait for data to come into the communications port
       
   387 //
       
   388 void CTrkSerialCommPort::IssueReadRequest()
       
   389 {
       
   390 	iNextReadChar = 0;
       
   391 	iReceivedChars.Zero();
       
   392 	iPort.ReadOneOrMore(iStatus, iReceivedChars);
       
   393 
       
   394 	SetActive();
       
   395 }
       
   396 
       
   397 //
       
   398 // CTrkSerialCommPort::DoCancel
       
   399 //
       
   400 // Cancel the request for data from the communications port
       
   401 //
       
   402 void CTrkSerialCommPort::DoCancel()
       
   403 {
       
   404 	iPort.ReadCancel();
       
   405 }
       
   406 
       
   407 //
       
   408 // CTrkSerialCommPort::RunL
       
   409 //
       
   410 // Called when data comes into the communications port
       
   411 //
       
   412 void CTrkSerialCommPort::RunL()
       
   413 {
       
   414 	//_LIT8(KPrefix, "CarbideToTrk: ");
       
   415 	//PrintMessage(KPrefix(), iReceivedChars);
       
   416 
       
   417 	// pass the data onto the framing layer
       
   418 	if (iStatus.Int() == KErrNone)
       
   419 		while (iNextReadChar < iReceivedChars.Length())
       
   420 			iFramingLayer->HandleByte(iReceivedChars[iNextReadChar++]);
       
   421 
       
   422 	// continue waiting for data
       
   423 	IssueReadRequest();
       
   424 }
       
   425 
       
   426 void CTrkSerialCommPort::PrintMessage(const TDesC8& aPrefix, const TDesC8& aBinaryData) const
       
   427 	{
       
   428 	TInt msgLength = aBinaryData.Length();
       
   429 	//RDebug::Printf("%S - start", &aPrefix);
       
   430 	for(TInt i=0; i<msgLength; i+=8)
       
   431 		{
       
   432 		TBuf8<30> tempBuf;
       
   433 		tempBuf.SetLength(0);
       
   434 		for(TInt j=i; (j<i+8) && (j<msgLength); j++)
       
   435 			{
       
   436 			TUint byte = (TUint)aBinaryData[j];
       
   437 			tempBuf.AppendNumFixedWidth(byte, EHex, 2);
       
   438 			tempBuf.Append(' ');
       
   439 			}
       
   440 		//RDebug::Printf("%S: [%S]", &aPrefix, &tempBuf);
       
   441 		}
       
   442 	//RDebug::Printf("%S - end", &aPrefix);
       
   443 
       
   444 	}