dbgagents/trkagent/engine/TrkDbgTrcCommPort.cpp
changeset 0 c6b0df440bee
equal deleted inserted replaced
-1:000000000000 0:c6b0df440bee
       
     1 /*
       
     2 * Copyright (c) 2006 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 "TrkConnData.h"
       
    23 #include "TrkDbgTrcCommPort.h"
       
    24 #include "TrkFramingLayer.h"
       
    25 
       
    26 _LIT(KSendErrMsg, "Failed to write to the DbgTrc channel");
       
    27 
       
    28 //
       
    29 // Default comm values
       
    30 //
       
    31 
       
    32 
       
    33 //
       
    34 // Static helper functions
       
    35 //
       
    36 
       
    37 //
       
    38 //
       
    39 // CTrkDbgTrcCommPort implementation
       
    40 //
       
    41 //
       
    42 
       
    43 //
       
    44 // CTrkDbgTrcCommPort constructor
       
    45 //
       
    46 CTrkDbgTrcCommPort::CTrkDbgTrcCommPort()
       
    47 	: CTrkCommPort(CActive::EPriorityStandard),
       
    48 	  iConnected(EFalse),
       
    49 	  iListening(EFalse),
       
    50 	  iLineNumber(0)
       
    51 {
       
    52 }
       
    53 
       
    54 //
       
    55 // CTrkDbgTrcCommPort::ConstructL
       
    56 //
       
    57 void CTrkDbgTrcCommPort::ConstructL(TTrkConnData aTrkConnData, TDes& aErrorMessage)
       
    58 {
       
    59 	iUnitNumber = aTrkConnData.iPortNumber;
       
    60 }
       
    61 
       
    62 //
       
    63 // CTrkDbgTrcCommPort destructor
       
    64 //
       
    65 CTrkDbgTrcCommPort::~CTrkDbgTrcCommPort()
       
    66 {
       
    67 	
       
    68 }
       
    69 
       
    70 //
       
    71 // CTrkDbgTrcCommPort::NewL
       
    72 //
       
    73 CTrkDbgTrcCommPort* CTrkDbgTrcCommPort::NewL(TTrkConnData aTrkConnData, TDes& aErrorMessage)
       
    74 {
       
    75 	CTrkDbgTrcCommPort* self = new(ELeave) CTrkDbgTrcCommPort;
       
    76 	CleanupStack::PushL(self);
       
    77 	self->ConstructL(aTrkConnData, aErrorMessage);
       
    78 	CleanupStack::Pop(self);
       
    79 	return self;
       
    80 }
       
    81 
       
    82 //
       
    83 // CTrkDbgTrcCommPort::OpenPortL
       
    84 //
       
    85 // Open the serial type communications port
       
    86 //
       
    87 void CTrkDbgTrcCommPort::OpenPortL()
       
    88 {
       
    89 	TInt error = KErrNone;
       
    90 
       
    91 	error = iDbgTrcPort.Connect();
       
    92 	if ((KErrAlreadyExists != error) && (KErrNone != error))
       
    93 		ReportAndLeaveIfErrorL(error, _L("Failed to connect to the debug trace comms server."));
       
    94 
       
    95 	TAcmConfig config;	
       
    96 	error = iDbgTrcPort.GetAcmConfig(config);
       
    97 
       
    98 	if (KErrNone != error)
       
    99 		ReportAndLeaveIfErrorL(error, _L("Failed to get the port configuration."));
       
   100 		
       
   101 	TAcmConfigV01 config1(*(TAcmConfigV01*)config.Ptr());
       
   102 	config1.iPortNumber = iUnitNumber;
       
   103 	
       
   104 	error = iDbgTrcPort.SetAcmConfig(config);
       
   105 	if (KErrNone != error && KErrInUse != error)
       
   106 		ReportAndLeaveIfErrorL(error, _L("Failed to set the port configuration."));
       
   107 
       
   108 	error = iDbgTrcPort.Open();
       
   109 	
       
   110 	if (KErrNone != error)
       
   111 		ReportAndLeaveIfErrorL(error, _L("Failed to open debug trace port."));
       
   112 	
       
   113 	iConnected = ETrue;
       
   114 	
       
   115 	// register the protocol id here
       
   116 	error = iDbgTrcPort.RegisterProtocolID(EOstProtTrk, EFalse);
       
   117 	if (KErrNone != error)
       
   118 		ReportAndLeaveIfErrorL(error, _L("Failed to register the protocol id."));
       
   119 
       
   120 		
       
   121 	iConnectionMessage.Format(_L("Connection: USB \r\nPort: %d\r\n"), iUnitNumber);
       
   122 }
       
   123 
       
   124 //
       
   125 // CTrkDbgTrcCommPort::ClosePort
       
   126 //
       
   127 // Close the communications port
       
   128 //
       
   129 void CTrkDbgTrcCommPort::ClosePort()
       
   130 {
       
   131 	Cancel();	
       
   132 	
       
   133 	if (iConnected)
       
   134 	{
       
   135 		iDbgTrcPort.UnRegisterProtocolID(EOstProtTrk);		
       
   136 		//iDbgTrcPort.Disconnect();
       
   137 		iDbgTrcPort.Close();
       
   138 		iConnected = EFalse;			
       
   139 	}	
       
   140 }
       
   141 
       
   142 //
       
   143 // CTrkDbgTrcCommPort::SendDataL
       
   144 //
       
   145 // Write data to the serial type port
       
   146 //
       
   147 void CTrkDbgTrcCommPort::SendDataL(const TDesC8& aBuffer)
       
   148 {
       
   149 	TRequestStatus status;
       
   150 	iDbgTrcPort.WriteMessage(status, aBuffer, EFalse);
       
   151 	User::WaitForRequest(status);
       
   152 	
       
   153 	TInt err = status.Int();	
       
   154 	ReportAndLeaveIfErrorL(err, KSendErrMsg);
       
   155 }
       
   156 
       
   157 //
       
   158 // CTrkDbgTrcCommPort::Listen
       
   159 //
       
   160 // Start listening for data coming into the serial type communications port
       
   161 //
       
   162 void CTrkDbgTrcCommPort::Listen(CTrkFramingLayer *aFramingLayer)
       
   163 {	
       
   164 	iFramingLayer = aFramingLayer;
       
   165 	CActiveScheduler::Add(this);
       
   166 	IssueReadRequest();
       
   167 	iListening = ETrue;
       
   168 }
       
   169 
       
   170 //
       
   171 // CTrkDbgTrcCommPort::StopListening
       
   172 //
       
   173 // Stop listening for data coming into the serial type communications port
       
   174 //
       
   175 void CTrkDbgTrcCommPort::StopListening()
       
   176 {
       
   177 	if (iListening)
       
   178 	{
       
   179 		Cancel();
       
   180 		Deque();
       
   181 	}
       
   182 	
       
   183 	iListening = EFalse;
       
   184 }
       
   185 
       
   186 //
       
   187 // CTrkDbgTrcCommPort::ReportAndLeaveIfErrorL
       
   188 //
       
   189 // If an error occurred, print the error information to the screen and bail out
       
   190 //
       
   191 void CTrkDbgTrcCommPort::ReportAndLeaveIfErrorL(TInt aError, const TDesC& aDesc)
       
   192 {
       
   193 	if (KErrNone != aError)
       
   194 	{
       
   195 		iErrorMessage.Format(_L("%S\r\nError Code: %d\r\n"), &aDesc, aError);
       
   196 		User::Leave(aError);
       
   197 	}
       
   198 }
       
   199 
       
   200 
       
   201 //
       
   202 // CTrkDbgTrcCommPort::IssueReadRequest
       
   203 //
       
   204 // Wait for data to come into the communications port
       
   205 //
       
   206 void CTrkDbgTrcCommPort::IssueReadRequest()
       
   207 {
       
   208 	iNextReadChar = 0;
       
   209 	iDbgTrcPort.ReadMessage(iStatus, iReceivedChars);
       
   210 	SetActive();
       
   211 }
       
   212 
       
   213 //
       
   214 // CTrkDbgTrcCommPort::DoCancel
       
   215 //
       
   216 // Cancel the request for data from the communications port
       
   217 //
       
   218 void CTrkDbgTrcCommPort::DoCancel()
       
   219 {
       
   220 	iDbgTrcPort.ReadCancel();
       
   221 }
       
   222 
       
   223 //
       
   224 // CTrkDbgTrcCommPort::RunL
       
   225 //
       
   226 // Called when data comes into the communications port
       
   227 //
       
   228 void CTrkDbgTrcCommPort::RunL()
       
   229 {
       
   230 	// pass the data onto the framing layer
       
   231 	if (iStatus.Int() == KErrNone)
       
   232 	{
       
   233 		while (iNextReadChar < iReceivedChars.Length())
       
   234 			iFramingLayer->HandleByte(iReceivedChars[iNextReadChar++]);
       
   235 	}
       
   236 	// continue waiting for data
       
   237 	IssueReadRequest();
       
   238 }
       
   239