telephonyprotocols/rawipnif/version1/src/BcaController.cpp
changeset 0 3553901f7fa8
child 24 6638e7f4bd8f
equal deleted inserted replaced
-1:000000000000 0:3553901f7fa8
       
     1 // Copyright (c) 2004-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 // Implements the interface to Bca & flow control.
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file
       
    20 */
       
    21 
       
    22 #include <e32uid.h>
       
    23 #include <nifmbuf.h>
       
    24 #include <es_ini.h>
       
    25 #include "BcaController.h"
       
    26 
       
    27 _LIT(KRawIpIniFile, "rawip.ini");
       
    28 _LIT(KLinkLit, "link");
       
    29 _LIT(KHighmarkLit, "highmark");
       
    30 //In order not to flow off SPUD everytime we set the default to 1
       
    31 const TUint KDefaultBufferSize=1;
       
    32 
       
    33 CBcaController::CBcaController(MControllerObserver& aObserver,
       
    34 	CBttLogger* aTheLogger)
       
    35 /**
       
    36  * Constructor. 
       
    37  */
       
    38 	: iObserver(aObserver),  
       
    39 	  iTheLogger(aTheLogger),
       
    40 	  iTxFlowControl(EFlowControlOff), 
       
    41 	  iTxContextActive(ETrue), 
       
    42 	  iSendState(EIdle),
       
    43 	  iMaxSendQueueLen(0),
       
    44 	  iNumPacketsInSendQueue(0)
       
    45 	{
       
    46 	iSendQueue.Init();
       
    47 	}
       
    48 
       
    49 
       
    50 CBcaController::~CBcaController()
       
    51 /**
       
    52  * Destructor.
       
    53  */
       
    54 	{
       
    55 	iSendQueue.Free();
       
    56 	iNumPacketsInSendQueue = 0;
       
    57 
       
    58 #ifdef RAWIP_HEADER_APPENDED_TO_PACKETS
       
    59 	delete iIPTagHeader;
       
    60 #endif // RAWIP_HEADER_APPENDED_TO_PACKETS
       
    61 	}
       
    62 
       
    63 void CBcaController::BaseConstructL()
       
    64 	{
       
    65 	_LOG_L1C1(_L8("CBcaController::BaseConstructL"));
       
    66 
       
    67 #ifdef RAWIP_HEADER_APPENDED_TO_PACKETS
       
    68 	iIPTagHeader = new (ELeave) CIPTagHeader(iTheLogger);
       
    69 #endif // RAWIP_HEADER_APPENDED_TO_PACKETS
       
    70 	CESockIniData* iniData = NULL;
       
    71 	TRAPD(res, iniData = CESockIniData::NewL(KRawIpIniFile));
       
    72 	CleanupStack::PushL(iniData);
       
    73 	
       
    74 	if(res!=KErrNone)
       
    75 		{
       
    76 		_LOG_L1C2(_L8("RawIp ini file %S not found. Default values will be used."), &KRawIpIniFile);
       
    77 		CleanupStack::PopAndDestroy();
       
    78 		return;
       
    79 		}
       
    80 	
       
    81 	//here process the file
       
    82 	if(!iniData->FindVar(KLinkLit(), KHighmarkLit(), iMaxSendQueueLen))
       
    83 		{
       
    84 		iMaxSendQueueLen = KDefaultBufferSize;
       
    85 		}
       
    86 	
       
    87 	CleanupStack::PopAndDestroy();
       
    88 	}
       
    89 
       
    90 void CBcaController::UpdateInternalFlowFlag(TFlowControl aValue)
       
    91 /**
       
    92  *  Updates Internal Flow flag and resumes the data flow if
       
    93  *  necessary.
       
    94  *
       
    95  * @param aValue the new state of iInternalFlow
       
    96  */
       
    97 	{
       
    98 	_LOG_L1C3(_L8("CBcaController::UpdateInternalFlowFlag[NewValue=%d, iSendState=%d]"),
       
    99 		aValue, iSendState);
       
   100 
       
   101 	if(iTxFlowControl == aValue)
       
   102 		{
       
   103 		// C32 Sent the same indication signal twice. Nif will ignore it.
       
   104 		_LOG_L2C1(_L8("WARNING CBcaController: Received same indication twice"));
       
   105 		return;
       
   106 		}
       
   107 	
       
   108 	// Update the flag value.
       
   109 	iTxFlowControl = aValue;
       
   110 
       
   111 	if(iTxFlowControl == EFlowControlOff)
       
   112 		{
       
   113 		// If the indication received turned flow control off...
       
   114 		if (IsTxPossible() && (iSendState == EIdle))
       
   115 			{
       
   116 			// ... if the NIF is in the EWaiting state
       
   117 			// then the data flow can be resumed. 
       
   118 			ResumeSending();
       
   119 			}
       
   120 		}		
       
   121 	else
       
   122 		{
       
   123 		// if the Flow Control is on we can remove all queued write requests
       
   124 		EmptySendQueue();
       
   125 		}
       
   126 	}
       
   127 
       
   128 void CBcaController::UpdateContextStateFlag(TBool aValue)
       
   129 /**
       
   130  *  Updates Context State flag and resumes the data flow if
       
   131  *  necessary.
       
   132  *
       
   133  * @param aValue the new state of iTxContextState
       
   134  */
       
   135 	{
       
   136 	_LOG_L1C3(_L8("CBcaController::UpdateContextStateFlag[NewValue=%d, OldValue=%d]"),
       
   137 		aValue, iTxContextActive);
       
   138 	
       
   139 	if(iTxContextActive == aValue)
       
   140 		{
       
   141 		return;
       
   142 		}
       
   143 
       
   144 	// Update the flag value.
       
   145 	iTxContextActive = aValue;
       
   146 
       
   147 	if(iTxContextActive)
       
   148 		{
       
   149 		// If the PDP context is active and...
       
   150 		if (IsTxPossible() && (iSendState == EIdle))
       
   151 			{
       
   152 			// ... if the NIF is in the EWaiting state
       
   153 			// then the data flow can be resumed. 
       
   154 			ResumeSending();
       
   155 			}
       
   156 		}
       
   157 	else
       
   158 		{
       
   159 		// if the PDP context is suspended we can remove all queued write requests
       
   160 		EmptySendQueue();
       
   161 		}
       
   162 	}
       
   163 
       
   164 TInt CBcaController::Send(RMBufChain& aPdu)
       
   165 /**
       
   166  *  This method is called by CRawIPNifMain in order to send a packet down
       
   167  * to the BCA. 
       
   168  *
       
   169  *	@param aPdu a data packet
       
   170  */
       
   171 	{
       
   172 	_LOG_L1C1(_L8(">>CBcaController::Send"));
       
   173 
       
   174 	// Check if NIF is shutting down
       
   175 	if (iSendState == EShuttingDown)
       
   176 		{
       
   177 		_LOG_L2C1(_L8("    ERROR: Nif is shutting down"));
       
   178 		
       
   179 		aPdu.Free();
       
   180 
       
   181 		return KErrDisconnected;
       
   182 		}
       
   183 	
       
   184 	// check that this packet isnt too big - If it is, we dont want to send it or
       
   185 	// add it to our queue
       
   186 	if ((aPdu.Length() - aPdu.First()->Length()) > BcaSendBufferLength())
       
   187 		{
       
   188 		_LOG_L2C1(_L8("Packet is too large - discarding"));
       
   189 		_LOG_L1C1(_L8("<<CSender::Send -> Error"));
       
   190 
       
   191 		aPdu.Free();
       
   192 		return KErrArgument;
       
   193 		}
       
   194 	
       
   195 	if (iSendState == ESending)
       
   196 		// If this happens, it means that TCP/IP has sent us an IP packet
       
   197 		// while we're still sending the previous one. 
       
   198 		{
       
   199 		// check that the queue isnt full NB. this check should not be needed as when the 
       
   200 		// queue becomes full the IP layer shouldnt send any more packets until it is told to
       
   201 		if (!IsSendQueueFull())
       
   202 			{
       
   203 			_LOG_L1C1(_L8("    Sender busy, appending packet to queue"));
       
   204 			//We know that flow control is off and context isnt suspended so can add to queue
       
   205 			AppendToSendQueue(aPdu);
       
   206 			
       
   207 			return IsSendQueueFull() ? KStopSending : KContinueSending;
       
   208 			}
       
   209 			
       
   210 		_LOG_L1C1(_L8("    Queue is full, upper layer is still sending packets, potential memory problems."));
       
   211 		AppendToSendQueue(aPdu);
       
   212 		return KStopSending;
       
   213 		}
       
   214 
       
   215 	// If we have got here then a write isnt currently happening
       
   216 	// We dont need to check flow control is off and context isnt suspended as the BCA always
       
   217 	// has room for one packet, so send the packet
       
   218 
       
   219 	if(!IsSendQueueEmpty())
       
   220 		{
       
   221 		//make sure that we don't change the order of packets!
       
   222 		//first send what has already been lined up
       
   223 		RMBufChain tmpPdu;
       
   224 		_LOG_L1C1(_L8("    Packet removed from queue to send"));
       
   225 		RemoveFromSendQueue(tmpPdu);
       
   226 		AppendToSendQueue(aPdu);
       
   227 		
       
   228 		// Update module state
       
   229 		_LOG_L2C1(_L8("     set State to ESending"));
       
   230 		iSendState = ESending;
       
   231 		
       
   232 		BcaSend(tmpPdu);
       
   233 		}
       
   234 	else
       
   235 		{
       
   236 		// Update module state
       
   237 		_LOG_L2C1(_L8("     set State to ESending"));
       
   238 		iSendState = ESending;
       
   239 		 
       
   240 		BcaSend(aPdu);
       
   241 		}
       
   242 		
       
   243 	_LOG_L2C1(_L8("<<CBcaController::Send - return StopSending/ContinueSending"));
       
   244 	return IsSendQueueFull() ? KStopSending : KContinueSending;
       
   245 	}
       
   246 
       
   247 void CBcaController::SendComplete()	
       
   248 /**
       
   249  *  This method is called after a packet was sent to the board. 
       
   250  *  If allowed by flow contol flags the NIF can signal the TCP/IP
       
   251  *  protocol indicating that is available to send more packets.
       
   252  */
       
   253 	{
       
   254 	_LOG_L1C1(_L8("CBcaController::SendComplete"));
       
   255 	_LOG_L2C1(_L8("     set State to EIdle"));
       
   256 	
       
   257 	iSendState = EIdle;
       
   258 
       
   259 	if (IsTxPossible())
       
   260 		ResumeSending();
       
   261 	}
       
   262 
       
   263 TBool CBcaController::IsTxPossible()
       
   264 /**
       
   265  *  This method returns ETrue if both TX flags are set to ETrue
       
   266  *
       
   267  * @return The Flow control state
       
   268  */
       
   269 	{
       
   270 	_LOG_L1C3(_L8("CBcaController::IsTxPossible (contextActive %d, flowcontrol %d)"), 
       
   271 		iTxContextActive, iTxFlowControl);
       
   272 
       
   273 	if(iTxContextActive && (iTxFlowControl == EFlowControlOff))
       
   274 		return ETrue;
       
   275 	else
       
   276 		return EFalse;
       
   277 	}
       
   278 
       
   279 void CBcaController::Process(TDesC8& aPdu)
       
   280 /**
       
   281  *  This method will pass on the received data to CBttNifMain. 
       
   282  *
       
   283  * @param aPdu a data packet
       
   284  */
       
   285 	{
       
   286 	_LOG_L1C1(_L8(">>CBcaController::Process"));
       
   287 
       
   288 	TInt ret;
       
   289 
       
   290 	// Create a packet object.
       
   291 	RMBufPacket packet;
       
   292 	TRAP(ret, packet.CreateL(aPdu));
       
   293 	if (ret != KErrNone)
       
   294 		{
       
   295 		// Couldn't create package. Packet will be ignored...
       
   296 		_LOG_L1C2(_L8("<<CBcaController::Process couldn't create MBuf [ret=%d]"), ret);
       
   297 		return;
       
   298 		}
       
   299 	else
       
   300 		// Package created...
       
   301 		{
       
   302 #ifdef RAWIP_HEADER_APPENDED_TO_PACKETS
       
   303 		TUint16 protocolCode = iIPTagHeader->RemoveHeader(packet);
       
   304 #else
       
   305 		TUint16 protocolCode = 0;
       
   306 #endif // RAWIP_HEADER_APPENDED_TO_PACKETS
       
   307 
       
   308 		packet.Pack();
       
   309 		// Process the packet
       
   310 		GetObserver().Process(packet, protocolCode);
       
   311 		}
       
   312 
       
   313 	_LOG_L1C1(_L8("<<CBcaController::Process"));
       
   314 	}
       
   315 
       
   316 void CBcaController::ResumeSending()
       
   317 /**
       
   318  *  Used to indicate to the TCP/IP protocol layer that the NIF is ready to 
       
   319  *	process more packets.
       
   320  */
       
   321 	{
       
   322 	_LOG_L1C1(_L8("CBcaIoController::ResumeSending"));
       
   323 
       
   324 	// If there are still some packets in the queue to be sent, then carry
       
   325 	// on sending them.
       
   326 	// NB. we only want to send more packets from the queue if we are currently EIdle
       
   327 	if (iSendState == EIdle) 
       
   328 		{
       
   329 		if(!IsSendQueueEmpty())
       
   330 			{
       
   331 			RMBufChain tmpPdu;
       
   332 			_LOG_L1C1(_L8("    Packet removed from queue to send"));
       
   333 			RemoveFromSendQueue(tmpPdu);
       
   334 			
       
   335 			// Update module state
       
   336 			_LOG_L2C1(_L8("     set State to ESending"));
       
   337 			iSendState = ESending;
       
   338 			
       
   339 			BcaSend(tmpPdu);
       
   340 			}
       
   341 		if(IsSendQueueEmpty())
       
   342 			{
       
   343 			iObserver.ResumeSending();
       
   344 			}
       
   345 		}
       
   346 	// if iSendState = ESending - do nothing
       
   347 	}
       
   348 	
       
   349 #ifdef RAWIP_HEADER_APPENDED_TO_PACKETS
       
   350 void CBcaController::SetType(TUint16 aType)
       
   351 {
       
   352 /**
       
   353  *  Used to specify the type of the IP header.
       
   354  */
       
   355 	_LOG_L1C1(_L8("CBcaController::SetType"));
       
   356 	
       
   357 	iIPTagHeader->SetType(aType);	
       
   358 }
       
   359 
       
   360 void CBcaController::AddHeader(TDes8& aDes)
       
   361 /**
       
   362  *  Used to add the IP header to the packet before sending to the BCA.
       
   363  */
       
   364 	{
       
   365 	_LOG_L1C1(_L8("CBcaController::AddHeader"));
       
   366 
       
   367 	iIPTagHeader->AddHeader(aDes);
       
   368 	}
       
   369 
       
   370 TUint16 CBcaController::RemoveHeader(RMBufChain& aPdu)
       
   371 /**
       
   372  *  Used to remove the IP header from the received the packet before sending to the 
       
   373  *  TCP/IP layer.  
       
   374  * @return The IP header that has been removed from the packet
       
   375  */
       
   376 	{
       
   377 	_LOG_L1C1(_L8("CBcaController::RemoveHeader"));
       
   378 
       
   379 	return (iIPTagHeader->RemoveHeader(aPdu));
       
   380 	}	
       
   381 #endif // RAWIP_HEADER_APPENDED_TO_PACKETS
       
   382 
       
   383 TBool CBcaController::IsSendQueueEmpty()
       
   384 /**
       
   385  *  Indicator of whether the BufferQueue is empty
       
   386  * @return TBool.  ETrue if bufferQueue is emtpy, EFalse if queue is not empty
       
   387  */
       
   388 	{
       
   389 	return iSendQueue.IsEmpty();
       
   390 	}
       
   391 	
       
   392 TBool CBcaController::IsSendQueueFull()
       
   393 /**
       
   394  *  Indicator of whether the BufferQueue is full
       
   395  * @return TBool.  ETrue if bufferQueue is full, EFalse if queue is not full
       
   396  */
       
   397 	{
       
   398 	return iNumPacketsInSendQueue >= iMaxSendQueueLen;
       
   399 	}	
       
   400 
       
   401 void CBcaController::AppendToSendQueue(RMBufChain& aPdu)
       
   402 /**
       
   403  * Appends the packet aPdu to the queue. 
       
   404  * Increments the packet count. Doesn't do error checking.
       
   405  * @param aChain buffer chain to be added
       
   406  */
       
   407 	{
       
   408 	iSendQueue.Append(aPdu);
       
   409 	iNumPacketsInSendQueue++;
       
   410 	}
       
   411 	
       
   412 TBool CBcaController::RemoveFromSendQueue(RMBufChain& aPdu)
       
   413 /**
       
   414  * Removes the packet aPdu from the queue. 
       
   415  * Decrements the packet count.
       
   416  * @param aChain buffer chain to be added
       
   417  * @return False if chain is empty
       
   418  */
       
   419 	{
       
   420 	TBool ret = iSendQueue.Remove(aPdu);
       
   421 	if(ret)
       
   422 		{
       
   423 		iNumPacketsInSendQueue--;
       
   424 		}
       
   425 	return ret;
       
   426 	}
       
   427 	
       
   428 void CBcaController::EmptySendQueue()
       
   429 /**
       
   430  * Removes all the packets from the send queue. Initializes the
       
   431  * send queue and sets the packet count to 0.
       
   432  */
       
   433 	{
       
   434 	iSendQueue.Free();
       
   435 	iSendQueue.Init();
       
   436 	iNumPacketsInSendQueue = 0;
       
   437 	}