dbgagents/trkagent/xtidriver/TrkXtiDriver.cpp
changeset 0 c6b0df440bee
equal deleted inserted replaced
-1:000000000000 0:c6b0df440bee
       
     1 /*
       
     2 * Copyright (c) 2007 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 #ifdef __WINS__
       
    20 #error - this driver cannot be built for emulation
       
    21 #endif
       
    22 
       
    23 #include <e32def.h>
       
    24 #include <e32cmn.h>
       
    25 #include <kernel.h>
       
    26 #include <kern_priv.h>
       
    27 #include <arm.h>
       
    28 
       
    29 #include "TrkXtiComm.h"
       
    30 #include "TrkXtiDriver.h"
       
    31 
       
    32 #define KTrkAppSecurUid 0x200170BB
       
    33 #define KTrkExeSecurUid 0x200159E2
       
    34 
       
    35 //
       
    36 // Static function definitions
       
    37 //
       
    38 
       
    39 
       
    40 //
       
    41 //
       
    42 // DTrkXtiDriverFactory implementation
       
    43 //
       
    44 //
       
    45 
       
    46 //
       
    47 // DTrkXtiDriverFactory constructor
       
    48 //
       
    49 DTrkXtiDriverFactory::DTrkXtiDriverFactory()
       
    50 {
       
    51     iVersion = TVersion(KMajorXtiVersionNumber, KMinorXtiVersionNumber, KBuildXtiVersionNumber);    
       
    52 }
       
    53 
       
    54 //
       
    55 // DTrkXtiDriverFactory::Create
       
    56 //
       
    57 TInt DTrkXtiDriverFactory::Create(DLogicalChannelBase*& aChannel)
       
    58 {
       
    59 	if (iOpenChannels != 0)
       
    60 		return KErrInUse; // a channel is already open
       
    61 	
       
    62 	aChannel = new DTrkXtiChannel(this);
       
    63 	
       
    64 	return aChannel ? KErrNone : KErrNoMemory;
       
    65 }
       
    66 
       
    67 //
       
    68 // DTrkXtiDriverFactory::Install
       
    69 //
       
    70 TInt DTrkXtiDriverFactory::Install()
       
    71 {
       
    72     return(SetName(&KTrkXtiDriverName));
       
    73 }
       
    74 
       
    75 //
       
    76 // DTrkXtiDriverFactory::Install
       
    77 //
       
    78 void DTrkXtiDriverFactory::GetCaps(TDes8& aDes) const
       
    79 {
       
    80     TCapsTrkXtiDriver b;
       
    81     b.iVersion = TVersion(KMajorXtiVersionNumber, KMinorXtiVersionNumber, KBuildXtiVersionNumber);
       
    82     
       
    83     Kern::InfoCopy(aDes,(TUint8*)&b, sizeof(b));
       
    84 }
       
    85 
       
    86 
       
    87 //
       
    88 //
       
    89 // DTrkXtiChannel implementation
       
    90 //
       
    91 //
       
    92 
       
    93 //
       
    94 // DTrkXtiChannel constructor
       
    95 //
       
    96 DTrkXtiChannel::DTrkXtiChannel(DLogicalDevice* aLogicalDevice)
       
    97 					: iRxPendingMsg(EFalse),
       
    98 					  iInterruptId(76)
       
    99 {
       
   100 	
       
   101 	LOG_MSG("DTrkXtiChannel::DTrkXtiChannel()");
       
   102 
       
   103 	iDevice = aLogicalDevice;
       
   104 	
       
   105 	iClientThread = &Kern::CurrentThread();
       
   106 	TInt err = iClientThread->Open();	
       
   107 	
       
   108 	
       
   109 	LOG_MSG("DTrkXtiChannel::DTrkXtiChannel() End"); 
       
   110 }
       
   111 
       
   112 //
       
   113 // DTrkXtiChannel destructor
       
   114 //
       
   115 DTrkXtiChannel::~DTrkXtiChannel()
       
   116 {
       
   117 	LOG_MSG("DTrkXtiChannel::~DTrkXtiChannel()");
       
   118 	
       
   119 	Kern::SafeClose((DObject*&)iClientThread, NULL);
       
   120 	
       
   121 	if (iLock)
       
   122 		iLock->Close(NULL);	
       
   123 }
       
   124 
       
   125 //
       
   126 // DTrkXtiChannel::DoCreate
       
   127 //
       
   128 TInt DTrkXtiChannel::DoCreate(TInt /*aUnit*/, const TDesC* /*anInfo*/, const TVersion& aVer)
       
   129 {
       
   130 	LOG_MSG("DTrkXtiChannel::DoCreate()");
       
   131 
       
   132   	if (!Kern::QueryVersionSupported(TVersion(KMajorXtiVersionNumber, KMinorXtiVersionNumber, KBuildXtiVersionNumber), aVer))
       
   133 		return KErrNotSupported; 
       
   134 
       
   135 	//Setup the driver for receiving client messages
       
   136 	iDFCQue = NULL;
       
   137 	TBuf8<KMaxInfoName> xtiDFC = _L8("XTI DFC");;
       
   138 	
       
   139 	TInt err = Kern::DfcQCreate(iDFCQue, 27, &xtiDFC);
       
   140 	if (err == KErrNone)
       
   141 	{
       
   142 		SetDfcQ(iDFCQue);
       
   143 	}
       
   144 	else
       
   145 	{
       
   146 		SetDfcQ(Kern::DfcQue0());
       
   147 	}
       
   148 
       
   149 	iMsgQ.Receive();  	
       
   150 	
       
   151 	// create subscriber and subscribe to receive trk messages from TraceCore
       
   152 	iTrkXtiSubscriber = new DTrkXtiSubscriber;
       
   153  	if (!iTrkXtiSubscriber)
       
   154 		return KErrNoMemory;
       
   155  	
       
   156 	err = iTrkXtiSubscriber->Create(this, iClientThread);
       
   157 	if (err != KErrNone)
       
   158 		return err;
       
   159 	
       
   160 	err = Kern::SemaphoreCreate(iLock, _L("TrkXtiDriverWriteLock"), 1 /* Initial count */);
       
   161 	if (err != KErrNone)
       
   162 		return err;
       
   163 
       
   164 	return KErrNone;
       
   165 }
       
   166 
       
   167 //
       
   168 // DTrkXtiChannel::DoCancel
       
   169 //
       
   170 void DTrkXtiChannel::DoCancel(TInt aReqNo)
       
   171 {
       
   172 	LOG_MSG("DTrkXtiChannel::DoCancel()");
       
   173 	
       
   174 	switch(aReqNo)
       
   175 	{
       
   176 		case RTrkXtiDriver::ERequestReadCancel:
       
   177 		{
       
   178 			Kern::RequestComplete(iClientThread, iRxRequestStatus, KErrCancel);
       
   179 			iRxClientBuffer = NULL;
       
   180 			iRxRequestStatus = NULL;
       
   181 			iRxClientBufferLength = 0;
       
   182 		}
       
   183 		break;
       
   184 	}
       
   185 
       
   186 }
       
   187 
       
   188 //
       
   189 // DTrkXtiChannel::DoRequest
       
   190 //
       
   191 void DTrkXtiChannel::DoRequest(TInt aReqNo, TRequestStatus* aStatus, TAny* a1, TAny* a2)
       
   192 {
       
   193 	LOG_MSG("DTrkXtiChannel::DoRequest()");
       
   194 		
       
   195 	switch(aReqNo)
       
   196 	{
       
   197 		case RTrkXtiDriver::ERequestRead:
       
   198 		{
       
   199 			iRxRequestStatus = aStatus;
       
   200 			iRxClientBuffer = a1;
       
   201 			iRxClientBufferLength = (TUint32)a2;
       
   202 			
       
   203 			if (iRxPendingMsg)
       
   204 			{
       
   205 				TInt err = Kern::ThreadDesWrite(iClientThread, iRxClientBuffer, iRxPendingMsgBuffer, 0, KChunkShiftBy0, iClientThread);
       
   206 				Kern::RequestComplete(iClientThread, iRxRequestStatus, err);
       
   207 		
       
   208 				iRxRequestStatus = NULL;
       
   209 				iRxPendingMsg = EFalse;
       
   210 			}
       
   211 
       
   212 			break;
       
   213 		}		
       
   214 		default:
       
   215 			Kern::RequestComplete(iClientThread, aStatus, KErrNotSupported);
       
   216 	}
       
   217 }
       
   218 
       
   219 //
       
   220 // DTrkXtiChannel::DoControl
       
   221 //
       
   222 TInt DTrkXtiChannel::DoControl(TInt aFunction, TAny* a1, TAny* a2)
       
   223 {
       
   224 	LOG_MSG("DTrkXtiChannel::DoControl()");
       
   225 	
       
   226 	TInt err = KErrNone;
       
   227 	
       
   228 	switch(aFunction)
       
   229 	{
       
   230 		case RTrkXtiDriver::EControlWrite:
       
   231 		{
       
   232 			//lock the Semaphore so that write doesn't happen when a write is already in progress.
       
   233 			Kern::SemaphoreWait(*iLock);
       
   234 			err = DoWrite((TUint32)a1, a2);			
       
   235 			Kern::SemaphoreSignal(*iLock);			
       
   236 			break;
       
   237 		}
       
   238 		default:
       
   239 		{
       
   240 			return KErrGeneral;
       
   241 		}
       
   242 
       
   243 	}
       
   244 	if (KErrNone != err)
       
   245 	{
       
   246 		LOG_MSG2("Error %d from control function", err);
       
   247 	}
       
   248 	return err;
       
   249 }
       
   250 
       
   251 //
       
   252 // DTrkXtiChannel::HandleMsg
       
   253 //
       
   254 void DTrkXtiChannel::HandleMsg(TMessageBase* aMsg)
       
   255 {
       
   256 	LOG_MSG("DTrkXtiChannel::HandleMsg()");
       
   257 	
       
   258 	TThreadMessage& m = *(TThreadMessage*)aMsg;
       
   259 	TInt id = m.iValue;
       
   260 
       
   261 
       
   262 	if (id == (TInt)ECloseMsg)
       
   263 	{
       
   264         if (iTrkXtiSubscriber)
       
   265         {
       
   266             Kern::SemaphoreWait(*iLock);
       
   267             delete iTrkXtiSubscriber;
       
   268             iTrkXtiSubscriber = NULL;   
       
   269             Kern::SemaphoreSignal(*iLock);              
       
   270         }
       
   271 		m.Complete(KErrNone, EFalse);
       
   272 		return;
       
   273 	}
       
   274 
       
   275 	if (id == KMaxTInt)
       
   276 	{
       
   277 		// DoCancel
       
   278 		DoCancel(m.Int0());
       
   279 		m.Complete(KErrNone, ETrue); 
       
   280 		return;
       
   281 	}
       
   282 
       
   283 	if (id < 0)
       
   284 	{
       
   285 		// DoRequest
       
   286 		TRequestStatus* pStatus = (TRequestStatus*)m.Ptr0();
       
   287 		DoRequest(~id, pStatus, m.Ptr1(), m.Ptr2());
       
   288 		m.Complete(KErrNone, ETrue);
       
   289 	}
       
   290 	else
       
   291 	{
       
   292 		// DoControl
       
   293 		TInt err = DoControl(id, m.Ptr0(), m.Ptr1());
       
   294 		m.Complete(err, ETrue);
       
   295 	}
       
   296 }
       
   297 
       
   298 //
       
   299 // DTrkXtiChannel::DoWrite
       
   300 //
       
   301 TInt DTrkXtiChannel::DoWrite(TUint32 aLength, TAny* a2)
       
   302 {
       
   303 	LOG_MSG("DTrkXtiChannel::DoWrite()");
       
   304 
       
   305 	TInt err = KErrNone;
       
   306 
       
   307 	err = Kern::ThreadDesRead(iClientThread, a2, iTxBuffer, 0, KChunkShiftBy0);
       
   308 
       
   309 	if (err == KErrNone)
       
   310 	{	
       
   311 		err = WriteXtiChannel(iTxBuffer);
       
   312 	}
       
   313 	return err;
       
   314 }
       
   315 
       
   316 //
       
   317 // DTrkXtiChannel::WriteXtiChannel
       
   318 //
       
   319 // This function writes all the data in the descriptor in one message
       
   320 TInt DTrkXtiChannel::WriteXtiChannel(TDesC8& aDes)
       
   321 {
       
   322 	TInt err = KErrNone;
       
   323 		err = iTrkXtiSubscriber->Send(aDes);
       
   324 
       
   325 	return err;
       
   326 }
       
   327 
       
   328 
       
   329 
       
   330 
       
   331 
       
   332 //
       
   333 // DTrkXtiChannel::DoCompleteRx
       
   334 //
       
   335 void DTrkXtiChannel::DoCompleteRx(const TDesC8& aMessage)
       
   336 {
       
   337 	LOG_MSG("DTrkXtiChannel::DoCompleteRx()");
       
   338 
       
   339 	// If there is a pending read request, complete the request with this message.
       
   340 	// Otherwise store the message, ideally we should queue the messages, but having 
       
   341 	// a large buffer is failing to initialize at the startup.
       
   342 	// For now, we will just store only one message
       
   343 	// If for some reason, TRK is waiting on something and doesn't read for a while, 
       
   344 	// then its possible that we will loose messages. 
       
   345 	Kern::SemaphoreWait(*iLock);
       
   346 	if (iRxRequestStatus)
       
   347 	{
       
   348 		TInt err = Kern::ThreadDesWrite(iClientThread, iRxClientBuffer, aMessage, 0, KChunkShiftBy0, iClientThread);
       
   349 		Kern::RequestComplete(iClientThread, iRxRequestStatus, err);
       
   350 		
       
   351 		iRxRequestStatus = NULL;
       
   352 	}
       
   353 	else if (!iRxPendingMsg)
       
   354 	{
       
   355 		iRxPendingMsgBuffer.Copy(aMessage);
       
   356 		iRxPendingMsg = ETrue;
       
   357 	}
       
   358 	Kern::SemaphoreSignal(*iLock);
       
   359 }
       
   360 
       
   361 DECLARE_STANDARD_LDD()
       
   362 {
       
   363     return new DTrkXtiDriverFactory;
       
   364 }