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