dbgagents/trkagent/engine/TrkXtiCommPort.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 "TrkXtiCommPort.h"
       
    24 #include "TrkFramingLayer.h"
       
    25 
       
    26 _LIT(KSendErrMsg, "Failed to write to the XTI channel");
       
    27 
       
    28 //
       
    29 // Default comm values
       
    30 //
       
    31 
       
    32 
       
    33 //
       
    34 // Static helper functions
       
    35 //
       
    36 
       
    37 //
       
    38 //
       
    39 // CTrkXtiCommPort implementation
       
    40 //
       
    41 //
       
    42 
       
    43 //
       
    44 // CTrkXtiCommPort constructor
       
    45 //
       
    46 CTrkXtiCommPort::CTrkXtiCommPort()
       
    47 	: CTrkCommPort(CActive::EPriorityStandard),
       
    48 	  iConnected(EFalse),
       
    49 	  iListening(EFalse),
       
    50 	  iLineNumber(0),
       
    51 	  iLddLoaded(EFalse)
       
    52 {
       
    53 }
       
    54 
       
    55 //
       
    56 // CTrkXtiCommPort::ConstructL
       
    57 //
       
    58 void CTrkXtiCommPort::ConstructL(TTrkConnData aTrkConnData, TDes& aErrorMessage)
       
    59 {
       
    60 
       
    61 	iLDD = aTrkConnData.iLDD;
       
    62 	// We don't really need this
       
    63 	//iUnitNumber = aTrkConnData.iPortNumber;
       
    64 
       
    65 	if (!iLDD.Size())
       
    66 	{
       
    67 		aErrorMessage.Format(_L("LDD not specified in init file\r\n"));		
       
    68 		User::Leave(KErrCorrupt);
       
    69 	}
       
    70 	
       
    71 }
       
    72 
       
    73 //
       
    74 // CTrkXtiCommPort destructor
       
    75 //
       
    76 CTrkXtiCommPort::~CTrkXtiCommPort()
       
    77 {
       
    78 	if (iLddLoaded)	
       
    79 	{				
       
    80 		//cancel the read request
       
    81 		iXtiDriver.ReadCancel();		
       
    82 		iXtiDriver.Close();
       
    83 
       
    84 		TInt err = User::FreeLogicalDevice(iLDD);
       
    85 		if (KErrNone != err)
       
    86 			User::Panic(_L("FreeLogicalDevice failed"), err);
       
    87 			
       
    88 		iLddLoaded = EFalse;
       
    89 	}
       
    90 	
       
    91 }
       
    92 
       
    93 //
       
    94 // CTrkXtiCommPort::NewL
       
    95 //
       
    96 CTrkXtiCommPort* CTrkXtiCommPort::NewL(TTrkConnData aTrkConnData, TDes& aErrorMessage)
       
    97 {
       
    98 	CTrkXtiCommPort* self = new(ELeave) CTrkXtiCommPort;
       
    99 	CleanupStack::PushL(self);
       
   100 	self->ConstructL(aTrkConnData, aErrorMessage);
       
   101 	CleanupStack::Pop(self);
       
   102 	return self;
       
   103 }
       
   104 
       
   105 //
       
   106 // CTrkXtiCommPort::OpenPortL
       
   107 //
       
   108 // Open the serial type communications port
       
   109 //
       
   110 void CTrkXtiCommPort::OpenPortL()
       
   111 {
       
   112 	TInt error = KErrNone;
       
   113 	
       
   114 	// make sure the LDD is not already loaded
       
   115 	User::FreeLogicalDevice(iLDD);
       
   116 
       
   117 	error = User::LoadLogicalDevice(iLDD);
       
   118 	if ((KErrAlreadyExists != error) && (KErrNone != error))
       
   119 		ReportAndLeaveIfErrorL(error, _L("Failed to load xti logical device."));	
       
   120 
       
   121 	error = iXtiDriver.Open();
       
   122 	if (KErrNone != error)
       
   123 		ReportAndLeaveIfErrorL(error, _L("Failed to open xti logical device."));
       
   124 		
       
   125 	iLddLoaded = ETrue;
       
   126 	
       
   127 	
       
   128 	_LIT(KXTIMSG, "Connection: XTI\r\nLDD: TrkXtiDriver\r\n");
       
   129 	
       
   130 	iConnectionMessage = KXTIMSG;
       
   131 }
       
   132 
       
   133 //
       
   134 // CTrkXtiCommPort::ClosePort
       
   135 //
       
   136 // Close the communications port
       
   137 //
       
   138 void CTrkXtiCommPort::ClosePort()
       
   139 {
       
   140 	Cancel();
       
   141 	
       
   142 	if (iLddLoaded)
       
   143 	{
       
   144 		//cancel the read request
       
   145 		iXtiDriver.ReadCancel();		
       
   146 		iXtiDriver.Close();
       
   147 		
       
   148 		TInt err = User::FreeLogicalDevice(iLDD);
       
   149 		if (KErrNone != err)
       
   150 			User::Panic(_L("FreeLogicalDevice failed"), err);
       
   151 
       
   152 		iLddLoaded = EFalse;			
       
   153 	}
       
   154 
       
   155 }
       
   156 
       
   157 //
       
   158 // CTrkXtiCommPort::SendDataL
       
   159 //
       
   160 // Write data to the serial type port
       
   161 //
       
   162 void CTrkXtiCommPort::SendDataL(const TDesC8& aBuffer)
       
   163 {
       
   164 	TInt err = iXtiDriver.Write(aBuffer);
       
   165 	
       
   166 	ReportAndLeaveIfErrorL(err, KSendErrMsg);
       
   167 }
       
   168 
       
   169 //
       
   170 // CTrkXtiCommPort::Listen
       
   171 //
       
   172 // Start listening for data coming into the serial type communications port
       
   173 //
       
   174 void CTrkXtiCommPort::Listen(CTrkFramingLayer *aFramingLayer)
       
   175 {
       
   176 	iFramingLayer = aFramingLayer;
       
   177 	CActiveScheduler::Add(this);
       
   178 	IssueReadRequest();
       
   179 	iListening = ETrue;
       
   180 }
       
   181 
       
   182 //
       
   183 // CTrkXtiCommPort::StopListening
       
   184 //
       
   185 // Stop listening for data coming into the serial type communications port
       
   186 //
       
   187 void CTrkXtiCommPort::StopListening()
       
   188 {
       
   189 	if (iListening)
       
   190 	{
       
   191 		Cancel();
       
   192 		Deque();
       
   193 	}
       
   194 	
       
   195 	iListening = EFalse;
       
   196 }
       
   197 
       
   198 //
       
   199 // CTrkXtiCommPort::ReportAndLeaveIfErrorL
       
   200 //
       
   201 // If an error occurred, print the error information to the screen and bail out
       
   202 //
       
   203 void CTrkXtiCommPort::ReportAndLeaveIfErrorL(TInt aError, const TDesC& aDesc)
       
   204 {
       
   205 	if (KErrNone != aError)
       
   206 	{
       
   207 		iErrorMessage.Format(_L("%S\r\nError Code: %d\r\n"), &aDesc, aError);
       
   208 		User::Leave(aError);
       
   209 	}
       
   210 }
       
   211 
       
   212 
       
   213 //
       
   214 // CTrkXtiCommPort::IssueReadRequest
       
   215 //
       
   216 // Wait for data to come into the communications port
       
   217 //
       
   218 void CTrkXtiCommPort::IssueReadRequest()
       
   219 {
       
   220 	iNextReadChar = 0;
       
   221 	iXtiDriver.ReadOneOrMore(iStatus, iReceivedChars);
       
   222 	SetActive();
       
   223 }
       
   224 
       
   225 //
       
   226 // CTrkXtiCommPort::DoCancel
       
   227 //
       
   228 // Cancel the request for data from the communications port
       
   229 //
       
   230 void CTrkXtiCommPort::DoCancel()
       
   231 {
       
   232 	iXtiDriver.ReadCancel();
       
   233 }
       
   234 
       
   235 //
       
   236 // CTrkXtiCommPort::RunL
       
   237 //
       
   238 // Called when data comes into the communications port
       
   239 //
       
   240 void CTrkXtiCommPort::RunL()
       
   241 {
       
   242 	// pass the data onto the framing layer
       
   243 	if (iStatus.Int() == KErrNone)
       
   244 	{
       
   245 		while (iNextReadChar < iReceivedChars.Length())
       
   246 			iFramingLayer->HandleByte(iReceivedChars[iNextReadChar++]);
       
   247 		//iXtiDriver.Write(iReceivedChars);
       
   248 	}
       
   249 	// continue waiting for data
       
   250 	IssueReadRequest();
       
   251 }
       
   252