bluetooth/btstack/l2cap/l2capSigPacketCommandReject.cpp
changeset 0 29b1cd4cb562
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     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 //
       
    15 
       
    16 #include <bluetooth/logger.h>
       
    17 
       
    18 #include "l2capSigPacketCommandReject.h"
       
    19 #include "l2capSignalHandler.h"
       
    20 
       
    21 #ifdef __FLOG_ACTIVE
       
    22 _LIT8(KLogComponent, LOG_COMPONENT_L2CAP);
       
    23 #endif
       
    24 
       
    25 
       
    26 //					COMMAND REJECT COMMAND 
       
    27 
       
    28 
       
    29 HCommandReject::HCommandReject(RMBufChain& aCommand) 
       
    30  : HL2CapCommand(aCommand, KDefaultRTXTimerDuration, KDefaultERTXTimerDuration)
       
    31 	{
       
    32 	LOG_FUNC
       
    33 	}
       
    34 
       
    35 /**
       
    36 Verifies that the buffer contains a structurally correct command. 
       
    37 This does not ensure that the content of the command is semantically valid.
       
    38 
       
    39 @param aCommmand A new HL2CapCommand if the buffer contains a structurally valid command.
       
    40 @param aBuffer The buffer containing the command
       
    41 @return KErrNone if the command if created.
       
    42 		KErrNoMemory if the command structure is valid but cannot be created.
       
    43 		KErrCorrupt if the command structure is invalid.
       
    44 */
       
    45 TInt HCommandReject::NewCommandIfValid(RMBufChain& aBuffer, HL2CapCommand*& aCommand)
       
    46 	{
       
    47 	LOG_STATIC_FUNC
       
    48 	// Firstly align the MBufChain.  The maximum size we can align the 
       
    49 	// MBufChain to is the maximum MBuf size
       
    50 	__ASSERT_COMPILE(KL2CapCommandRejectMaxLength <= KMBufSmallSize);
       
    51 	
       
    52 	TInt length = aBuffer.Length();
       
    53 	if((length >= KL2CapCommandRejectMinLength) && (length <= KL2CapCommandRejectMaxLength))
       
    54 		{
       
    55 		// Don't need to check result as we know that the MBufChain contains
       
    56 		// at least length bytes as we asked first.
       
    57 		(void)aBuffer.Align(length);
       
    58 		}
       
    59 	else
       
    60 		{
       
    61 		// Dodge length!
       
    62 		return KErrCorrupt;
       
    63 		}
       
    64 		
       
    65 	if(VerifyStructure(aBuffer))
       
    66 		{
       
    67 		// Top!  The structure's fine, go ahead and create the command.
       
    68 		aCommand = new HCommandReject(aBuffer);
       
    69 		if(aCommand)
       
    70 			{
       
    71 			return KErrNone;
       
    72 			}
       
    73 		else
       
    74 			{
       
    75 			return KErrNoMemory;
       
    76 			}
       
    77 		}
       
    78 	else
       
    79 		{
       
    80 		// We'll not have any of this nonsense!
       
    81 		return KErrCorrupt;
       
    82 		}	
       
    83 	}
       
    84 
       
    85 /*static*/ HCommandReject* HCommandReject::New(TL2CAPCommandRejectData aRejectData, TUint8 aId)
       
    86 	{
       
    87 	LOG_STATIC_FUNC
       
    88 	HCommandReject* cmd = NULL;
       
    89 	RMBufChain buf;
       
    90 	TInt rerr = KErrNone;
       
    91 	
       
    92 	switch(aRejectData.iReason)
       
    93 		{
       
    94 		case ECommandNotUnderstood:
       
    95 			TRAP(rerr, buf.AllocL(KL2CapCommandRejectMinLength));
       
    96 			break;
       
    97 			
       
    98 		case EMTUExceeded:
       
    99 			TRAP(rerr, buf.AllocL(KL2CapCommandRejectMinLength + KMTUExceededLength));
       
   100 			break;
       
   101 
       
   102 		case EInvalidCID:
       
   103 			TRAP(rerr, buf.AllocL(KL2CapCommandRejectMinLength + KInvalidRequestedCIDLength));
       
   104 			break;
       
   105 
       
   106 		default:
       
   107 			// Invalid reject reason.
       
   108 			rerr = KErrArgument;
       
   109 			break;
       
   110 		};
       
   111 
       
   112 	if(rerr == KErrNone)
       
   113 		{
       
   114 		// Create the command - the RMBufChain ownership will pass
       
   115 		// to the new object.
       
   116 		cmd = new HCommandReject(buf);
       
   117 		if(cmd)
       
   118 			{
       
   119 			// Setup message contents.
       
   120 			cmd->SetCode(ECommandReject);
       
   121 			cmd->SetID(aId);
       
   122 			cmd->WriteDataLength();
       
   123 			cmd->SetRejectData(aRejectData);	
       
   124 			}
       
   125 		else
       
   126 			{
       
   127 			// Free the allocated buffer.
       
   128 			buf.Free();
       
   129 			}
       
   130 		}
       
   131 	return cmd;
       
   132 	}
       
   133 
       
   134 void HCommandReject::SetRejectData(TL2CAPCommandRejectData aRejectData)
       
   135 	{
       
   136 	LOG_FUNC
       
   137 	switch(aRejectData.iReason)
       
   138 		{
       
   139 		case ECommandNotUnderstood:
       
   140 			PutLittleEndian16(ECommandNotUnderstood, KReasonOffset, iCommand);
       
   141 			break;
       
   142 			
       
   143 		case EMTUExceeded:
       
   144 			PutLittleEndian16(EMTUExceeded, KReasonOffset, iCommand);
       
   145 			PutLittleEndian16(aRejectData.iMTUExceeded, KMTUExceededOffset, iCommand);
       
   146 			break;
       
   147 
       
   148 		case EInvalidCID:
       
   149 			PutLittleEndian16(EInvalidCID, KReasonOffset, iCommand);
       
   150 			PutLittleEndian16(aRejectData.iLocalEndpoint, KLocalEndpointOffset, iCommand);
       
   151 			PutLittleEndian16(aRejectData.iRemoteEndpoint, KRemoteEndpointOffset, iCommand);
       
   152 			break;
       
   153 
       
   154 		default:
       
   155 			Panic(EL2CAPInvalidCommandRejectReason);
       
   156 			break;
       
   157 		};
       
   158 	}
       
   159 	
       
   160 
       
   161 
       
   162 HCommandReject::~HCommandReject()
       
   163 	{
       
   164 	LOG_FUNC
       
   165 	}
       
   166 
       
   167 		
       
   168 TBool HCommandReject::ProcessCommand(CL2CapSignalHandler& aSignalHandler)
       
   169 	{
       
   170 	LOG_FUNC
       
   171 	if(aSignalHandler.HandleCommandReject(this))
       
   172 		{
       
   173 		// The command has been handled.  Delete it.
       
   174 		delete this;
       
   175 		return ETrue;
       
   176 		}
       
   177 	else
       
   178 		{
       
   179 		return EFalse;
       
   180 		}
       
   181 	}
       
   182 
       
   183 TBool HCommandReject::VerifyStructure(const RMBufChain& aBuffer)
       
   184 	{
       
   185 	LOG_STATIC_FUNC
       
   186 	TInt valid = ETrue;
       
   187 	// We have a reject reason, check there is enough data for that reason
       
   188 	TL2CAPCommandRejectReason reason = TL2CAPCommandRejectReason(GetLittleEndian16(KReasonOffset, aBuffer));
       
   189 	switch(reason)
       
   190 		{
       
   191 		case ECommandNotUnderstood:
       
   192 			{
       
   193 			if(aBuffer.Length() != KL2CapCommandRejectMinLength)
       
   194 				{
       
   195 				valid = EFalse;
       
   196 				}
       
   197 			}
       
   198 			break;
       
   199 		case EMTUExceeded:
       
   200 			{
       
   201 			if(aBuffer.Length() != (KL2CapCommandRejectMinLength + KMTUExceededLength))
       
   202 				{
       
   203 				valid = EFalse;
       
   204 				}
       
   205 			break;
       
   206 			}
       
   207 		case EInvalidCID:
       
   208 			{
       
   209 			if(aBuffer.Length() != (KL2CapCommandRejectMinLength + KInvalidRequestedCIDLength))
       
   210 				{
       
   211 				valid = EFalse;
       
   212 				}
       
   213 			}
       
   214 			break;
       
   215 		default:
       
   216 			{
       
   217 			// Unknown reason code
       
   218 			valid = EFalse;
       
   219 			break;
       
   220 			}
       
   221 		};
       
   222 		
       
   223 	return valid;
       
   224 	}
       
   225 
       
   226 TInt HCommandReject::RejectData(TL2CAPCommandRejectData& aRejectData) const
       
   227 	{
       
   228 	LOG_FUNC
       
   229 	TInt rcode = KErrNone;
       
   230 	
       
   231 	aRejectData.iReason = TL2CAPCommandRejectReason(GetLittleEndian16(KReasonOffset, iCommand));
       
   232 	
       
   233 	switch(aRejectData.iReason)
       
   234 		{
       
   235 		case EMTUExceeded:
       
   236 			aRejectData.iMTUExceeded = GetLittleEndian16(KMTUExceededOffset, iCommand);	
       
   237 			break;
       
   238 		case EInvalidCID:
       
   239 			aRejectData.iLocalEndpoint = GetLittleEndian16(KLocalEndpointOffset, iCommand);
       
   240 			aRejectData.iRemoteEndpoint = GetLittleEndian16(KRemoteEndpointOffset, iCommand);			
       
   241 			break;
       
   242 		default:
       
   243 			break;
       
   244 		};
       
   245 	
       
   246 	return rcode;
       
   247 	}