bthci/hci2implementations/hctls/bcsp/src/hctlbcspreceiver.cpp
changeset 0 29b1cd4cb562
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     1 // Copyright (c) 2006-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 /**
       
    17  @file
       
    18  @internalComponent
       
    19 */
       
    20 
       
    21 #include "hctlbcspreceiver.h"
       
    22 
       
    23 #include "hctlbcsp.h"
       
    24 #include "hctlbcspconsts.h"
       
    25 #include "debug.h"
       
    26 
       
    27 
       
    28 /**
       
    29 Implementation of Class CHCTLBcspReceiver
       
    30 */
       
    31 CHCTLBcspReceiver::CHCTLBcspReceiver(CHCTLBcsp& aHCTLBcsp, RBusDevComm& aPort) :
       
    32 	CActive(EPriorityStandard), 
       
    33 	iHctlBcsp(aHCTLBcsp),
       
    34 	iReceiveBufPtr(NULL,0),
       
    35 	iPort(aPort)
       
    36     {
       
    37 	LOG_FUNC
       
    38 	CActiveScheduler::Add(this);
       
    39     }
       
    40 
       
    41 CHCTLBcspReceiver::~CHCTLBcspReceiver()
       
    42     {
       
    43 	LOG_FUNC
       
    44 
       
    45 	Cancel();
       
    46     iReceiveBuffer.Close();
       
    47     }
       
    48 
       
    49 CHCTLBcspReceiver* CHCTLBcspReceiver::NewL(CHCTLBcsp& aHCTLBcsp, RBusDevComm& aPort) 
       
    50     {
       
    51 	LOG_STATIC_FUNC
       
    52 
       
    53     CHCTLBcspReceiver* self=new(ELeave)CHCTLBcspReceiver(aHCTLBcsp, aPort);
       
    54     CleanupStack::PushL(self);
       
    55     self->ConstructL();
       
    56     CleanupStack::Pop(self);
       
    57     return self;
       
    58     }
       
    59 
       
    60 void CHCTLBcspReceiver::ProcessData()	
       
    61 /**
       
    62 	This method parses the byte stream for a pair of 0xC0 bytes denoting beginning and end of packets
       
    63 	Then calls the HandleRx
       
    64 	Then zeros the pointer to the receive buffer and queues another read for the next frame
       
    65 */
       
    66     {
       
    67 	LOG_FUNC
       
    68 
       
    69 #ifdef __DEBUG_FLOG_RAW_UART_	
       
    70 	LOG(_L8("HCTLBCSP: UART RX..."));
       
    71 	TPtrC8 ptr = iReceiveBufPtr.Ptr();
       
    72 	TInt len = iReceiveBufPtr.Length();
       
    73 	LOGHEXDESC(iReceiveBufPtr);
       
    74 #endif
       
    75 	
       
    76 	if(iReceiveBufPtr.Length() > 1) // Processing the remainder of the SLIP encoded Frame
       
    77 		{
       
    78 		if(iHctlBcsp.HandleRx(iReceiveBufPtr) != KErrNone)	// SLIP Decode Frame
       
    79 			{
       
    80 			LOG(_L8("HCTLBCSP: SLIP Decoding failed discard packet requeue read..."))
       
    81 			}
       
    82 		}
       
    83 
       
    84 	// Reset Buffers
       
    85 	iReceiveBufPtr.Zero();
       
    86 	QueueReadForNextFrame(); // Get more stuff until next slip byte
       
    87 	}
       
    88 
       
    89 void CHCTLBcspReceiver::QueueReadForNextFrame()
       
    90 /**
       
    91 	Method moved due to HCI refactoring to reduce code duplication
       
    92 	This method should suffice for all RBusDevComm reads
       
    93 
       
    94 	@param aBytesRequired
       
    95 */
       
    96 	{
       
    97 	LOG_FUNC
       
    98 
       
    99 	if (IsActive())
       
   100         {
       
   101 		LOG(_L8("Receiver already active - TRYING TO CANCEL AND REQUEUE"));
       
   102  		Cancel(); // consume signal then do re-queue
       
   103         }
       
   104  
       
   105     SetActive();
       
   106 
       
   107 	iPort.Read(iStatus, iReceiveBufPtr);
       
   108 	}
       
   109 
       
   110 void CHCTLBcspReceiver::RunL()
       
   111 /**
       
   112 	If no error occurred, call ProcessData(). Otherwise, throw any data away 
       
   113 	and requeue the Read. 	
       
   114 */
       
   115 	{
       
   116 	LOG_LINE
       
   117 	LOG_FUNC
       
   118 	LOG1(_L8("\tiStatus = %d"), iStatus.Int());
       
   119 
       
   120 	switch(iStatus.Int())
       
   121 		{
       
   122 		case KErrNone:
       
   123 			// Receiver has a framer state machine
       
   124 			ProcessData();
       
   125 			break;
       
   126 		
       
   127 		case KErrAbort:
       
   128 		case KErrBadPower:
       
   129 			iReceiveBufPtr.Zero();
       
   130 			// Pretend nothing happened and requeue the read.
       
   131 			// Indeed the driver gives us this notification, not on power-down
       
   132 			// but on the next power-up, so we can requeue.
       
   133 			QueueReadForNextFrame();
       
   134 			break;
       
   135 
       
   136 #ifdef __DEBUG_INIT_PROGRESS
       
   137 		case KErrCommsOverrun:
       
   138 		case KErrCommsFrame:
       
   139 		case KErrCommsParity:
       
   140 			break;
       
   141 
       
   142 		default:
       
   143 			PANIC(KHCTLBcspPanicCat, EHCIHCTLReceiverFramerBadState);
       
   144 			break;
       
   145 #else
       
   146 		default:
       
   147 			// Ignore error condition.
       
   148 			iReceiveBufPtr.Zero();
       
   149 			QueueReadForNextFrame();
       
   150 			break;
       
   151 #endif
       
   152 		};
       
   153 	}
       
   154 
       
   155 void CHCTLBcspReceiver::ConstructL()
       
   156     {
       
   157 	LOG_FUNC
       
   158 	
       
   159 	//Create a HBufC based RBuf Object. 
       
   160     HBufC8* buf = HBufC8::NewMaxL(KHCTLRecvBufSize);
       
   161     iReceiveBuffer.Assign(buf);
       
   162     iReceiveBufPtr.Set(buf->Des());
       
   163     }
       
   164 
       
   165 void CHCTLBcspReceiver::DoCancel()
       
   166 	{
       
   167 	LOG_FUNC
       
   168 	
       
   169 	iPort.ReadCancel();
       
   170 	}
       
   171 
       
   172 TInt CHCTLBcspReceiver::RunError(TInt aError)
       
   173 	{
       
   174 	LOG_LINE
       
   175 	LOG_FUNC
       
   176 	LOG1(_L8("\taError = %d"), aError);
       
   177 	static_cast<void>(aError); // prevent unused variable warning
       
   178 	return KErrNone;
       
   179 	}