bluetooth/btstack/l2cap/l2capSigPacketInformation.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 "l2capSigPacketInformation.h"
       
    19 #include "l2capSignalHandler.h"
       
    20 #include "l2constants.h"
       
    21 
       
    22 #ifdef __FLOG_ACTIVE
       
    23 _LIT8(KLogComponent, LOG_COMPONENT_L2CAP);
       
    24 #endif
       
    25 
       
    26 //					INFORMATION REQUEST COMMAND 
       
    27 
       
    28 /*static*/ HInformationRequest* HInformationRequest::New(TInfoType aInfoType,
       
    29 		  	               								 TUint8 aRTXTimerDuration,
       
    30 	                       								 TUint16 aERTXTimerDuration)
       
    31 	{
       
    32 	LOG_STATIC_FUNC
       
    33 	HInformationRequest* cmd = NULL;
       
    34 	RMBufChain buf;
       
    35 	TRAPD(rerr, buf.AllocL(KInformationRequestLength));
       
    36 	if(rerr == KErrNone)
       
    37 		{
       
    38 		cmd = new HInformationRequest(buf, aRTXTimerDuration, aERTXTimerDuration);
       
    39 		if(cmd)
       
    40 			{
       
    41 			// Setup message contents.
       
    42 			cmd->SetCode(EInformationRequest);
       
    43 			cmd->SetID(KUninitialisedID);
       
    44 			cmd->WriteDataLength();
       
    45 			
       
    46 			cmd->SetInfoType(aInfoType);
       
    47 			}
       
    48 		else
       
    49 			{
       
    50 			// Free the allocated buffer.
       
    51 			buf.Free();
       
    52 			}
       
    53 		}
       
    54 	return cmd;
       
    55 	}	
       
    56 	
       
    57 /**
       
    58 Verifies that the buffer contains a structurally correct command. 
       
    59 This does not ensure that the content of the command is semantically valid.
       
    60 
       
    61 @param aCommmand A new HL2CapCommand if the buffer contains a structurally valid command.
       
    62 @param aBuffer The buffer containing the command
       
    63 @return KErrNone if the command if created.
       
    64 		KErrNoMemory if the command structure is valid but cannot be created.
       
    65 		KErrCorrupt if the command structure is invalid.
       
    66 */
       
    67 TInt HInformationRequest::NewCommandIfValid(RMBufChain& aBuffer, HL2CapCommand*& aCommand)
       
    68 	{
       
    69 	LOG_STATIC_FUNC
       
    70 	// Firstly align the MBufChain.  The maximum size we can align the 
       
    71 	// MBufChain to is the maximum MBuf size
       
    72 	__ASSERT_COMPILE(KInformationRequestLength <= KMBufSmallSize);
       
    73 	
       
    74 	TInt length = aBuffer.Length();
       
    75 	if(length == KInformationRequestLength)
       
    76 		{
       
    77 		// Don't need to check result as we know that the MBufChain contains
       
    78 		// at least length bytes as we asked first.
       
    79 		(void)aBuffer.Align(length);
       
    80 		aCommand = new HInformationRequest(aBuffer);
       
    81 		if(aCommand)
       
    82 			{
       
    83 			return KErrNone;
       
    84 			}
       
    85 		else
       
    86 			{
       
    87 			return KErrNoMemory;
       
    88 			}
       
    89 		}
       
    90 	else
       
    91 		{
       
    92 		// Dodge length!
       
    93 		return KErrCorrupt;
       
    94 		}
       
    95 	}
       
    96 
       
    97 HInformationRequest::HInformationRequest(RMBufChain& aCommand, 
       
    98           								 TUint8 aRTXTimerDuration,
       
    99           								 TUint16 aERTXTimerDuration)
       
   100  : HL2CapCommand(aCommand, aRTXTimerDuration, aERTXTimerDuration)
       
   101 	{
       
   102 	LOG_FUNC
       
   103 	}
       
   104 
       
   105 HInformationRequest::~HInformationRequest()
       
   106 	{
       
   107 	LOG_FUNC
       
   108 	}
       
   109 		
       
   110 TBool HInformationRequest::ProcessCommand(CL2CapSignalHandler& aSignalHandler)
       
   111 	{
       
   112 	LOG_FUNC
       
   113 	if(aSignalHandler.HandleInformationRequest(this))
       
   114 		{
       
   115 		// The command has been handled.  Delete it.
       
   116 		delete this;
       
   117 		return ETrue;
       
   118 		}
       
   119 	else
       
   120 		{
       
   121 		return EFalse;
       
   122 		}
       
   123 	}
       
   124 
       
   125 
       
   126 
       
   127 
       
   128 //					INFORMATION RESPONSE COMMAND 
       
   129 //Constructor to deal command coming in from remote side
       
   130 //
       
   131 HInformationResponse::HInformationResponse(RMBufChain& aCommand) 
       
   132  : HL2CapCommand(aCommand)
       
   133 	{
       
   134 	LOG_FUNC
       
   135 	}
       
   136 
       
   137 /*static*/ HInformationResponse* HInformationResponse::New(TInfoType aInfoType, TInfoReqResult aResult, TUint8 aId, TUint32 aExtendedFeatures)
       
   138 	{
       
   139 	LOG_STATIC_FUNC
       
   140 	HInformationResponse* cmd = NULL;
       
   141 	RMBufChain buf;
       
   142 	TRAPD(rerr, buf.AllocL(KInformationResponseMinLength + KExtendedFeatureMaskLength));
       
   143 	if(rerr == KErrNone)
       
   144 		{
       
   145 		cmd = new HInformationResponse(buf);
       
   146 		if(cmd)
       
   147 			{
       
   148 			// Setup message contents.
       
   149 			cmd->SetCode(EInformationResponse);
       
   150 			cmd->SetID(aId);
       
   151 			cmd->WriteDataLength();
       
   152 			
       
   153 			cmd->SetInfoType(aInfoType);
       
   154 			cmd->SetResult(aResult);
       
   155 			
       
   156 			cmd->SetExtendedFeatureMask(aExtendedFeatures);
       
   157 			}
       
   158 		else
       
   159 			{
       
   160 			// Free the allocated buffer.
       
   161 			buf.Free();
       
   162 			}
       
   163 		}
       
   164 	return cmd;
       
   165 	}		
       
   166 
       
   167 /*static*/ HInformationResponse* HInformationResponse::New(TInfoType aInfoType, TInfoReqResult aResult, TUint8 aId)
       
   168 	{
       
   169 	LOG_STATIC_FUNC
       
   170 	HInformationResponse* cmd = NULL;
       
   171 	RMBufChain buf;
       
   172 	TRAPD(rerr, buf.AllocL(KInformationResponseMinLength));
       
   173 	if(rerr == KErrNone)
       
   174 		{
       
   175 		cmd = new HInformationResponse(buf);
       
   176 		if(cmd)
       
   177 			{
       
   178 			// Setup message contents.
       
   179 			cmd->SetCode(EInformationResponse);
       
   180 			cmd->SetID(aId);
       
   181 			cmd->WriteDataLength();
       
   182 			
       
   183 			cmd->SetInfoType(aInfoType);
       
   184 			cmd->SetResult(aResult);
       
   185 			}
       
   186 		else
       
   187 			{
       
   188 			// Free the allocated buffer.
       
   189 			buf.Free();
       
   190 			}
       
   191 		}
       
   192 	return cmd;
       
   193 	}		
       
   194 
       
   195 /**
       
   196 Verifies that the buffer contains a structurally correct command. 
       
   197 This does not ensure that the content of the command is semantically valid.
       
   198 
       
   199 @param aCommmand A new HL2CapCommand if the buffer contains a structurally valid command.
       
   200 @param aBuffer The buffer containing the command
       
   201 @return KErrNone if the command if created.
       
   202 		KErrNoMemory if the command structure is valid but cannot be created.
       
   203 		KErrCorrupt if the command structure is invalid.
       
   204 */
       
   205 TInt HInformationResponse::NewCommandIfValid(RMBufChain& aBuffer, HL2CapCommand*& aCommand)
       
   206 	{
       
   207 	LOG_STATIC_FUNC
       
   208 	// Firstly align the MBufChain.  The maximum size we can align the 
       
   209 	// MBufChain to is the maximum MBuf size
       
   210 	__ASSERT_COMPILE(KInformationResponseMaxLength <= KMBufSmallSize);
       
   211 	
       
   212 	TInt length = aBuffer.Length();
       
   213 	if((length >= KInformationResponseMinLength) && (length <= KInformationResponseMaxLength))
       
   214 		{
       
   215 		// Don't need to check result as we know that the MBufChain contains
       
   216 		// at least length bytes as we asked first.
       
   217 		(void)aBuffer.Align(length);
       
   218 		}
       
   219 	else
       
   220 		{
       
   221 		// Dodge length!
       
   222 		return KErrCorrupt;
       
   223 		}
       
   224 		
       
   225 	if(VerifyStructure(aBuffer))
       
   226 		{
       
   227 		// Top!  The structure's fine, go ahead and create the command.
       
   228 		aCommand = new HInformationResponse(aBuffer);
       
   229 		if(aCommand)
       
   230 			{
       
   231 			return KErrNone;
       
   232 			}
       
   233 		else
       
   234 			{
       
   235 			return KErrNoMemory;
       
   236 			}
       
   237 		}
       
   238 	else
       
   239 		{
       
   240 		// We'll not have any of this nonsense!
       
   241 		return KErrCorrupt;
       
   242 		}		
       
   243 	}
       
   244 
       
   245 /* static */ TBool HInformationResponse::VerifyStructure(const RMBufChain& aCommand)
       
   246 	{
       
   247 	LOG_STATIC_FUNC
       
   248 	TBool valid = ETrue;
       
   249 	TInfoType infoType = static_cast<TInfoType>(GetLittleEndian16(KInfoTypeOffset, aCommand));
       
   250 	TInfoReqResult result = static_cast<TInfoReqResult>(GetLittleEndian16(KResultOffset, aCommand));
       
   251 	
       
   252 	if(result == ESuccess)
       
   253 		{
       
   254 		switch(infoType)
       
   255 			{ 
       
   256 			case EConnectionlessMTU:
       
   257 				{
       
   258 				if(aCommand.Length() != KInformationResponseMinLength + KConnectionlessMTULength)
       
   259 					{
       
   260 					valid = EFalse;
       
   261 					}
       
   262 
       
   263 				break;
       
   264 				}
       
   265 			case EExtendedFeaturesSupported:
       
   266 				{
       
   267 				if(aCommand.Length() != KInformationResponseMinLength + KExtendedFeatureMaskLength)
       
   268 					{
       
   269 					valid = EFalse;
       
   270 					}
       
   271 				break;
       
   272 				}
       
   273 			default:
       
   274 				{
       
   275 				// Unknown info type
       
   276 				// Although this is dodgy as the info type should be copied from the request
       
   277 				// we accept this for the sake of interop with legacy devices that deal
       
   278 				// incorrectly with extended features.
       
   279 				break;
       
   280 				}
       
   281 			};
       
   282 		}
       
   283 	else if(result == ENotsupported)
       
   284 		{
       
   285 		if(aCommand.Length() != KInformationResponseMinLength)
       
   286 			{
       
   287 			valid = EFalse;
       
   288 			}
       
   289 		}
       
   290 	else
       
   291 		{
       
   292 		valid = EFalse;
       
   293 		}
       
   294 	
       
   295 	return valid;
       
   296 	}
       
   297 
       
   298 HInformationResponse::~HInformationResponse()
       
   299 	{
       
   300 	LOG_FUNC
       
   301 	}
       
   302 
       
   303 TBool HInformationResponse::ProcessCommand(CL2CapSignalHandler& aSignalHandler)	
       
   304 	{
       
   305 	LOG_FUNC
       
   306 	if(aSignalHandler.HandleInformationResponse(this))
       
   307 		{
       
   308 		// The command has been handled.  Delete it.
       
   309 		delete this;
       
   310 		return ETrue;
       
   311 		}
       
   312 	else
       
   313 		{
       
   314 		return EFalse;
       
   315 		}
       
   316 	}
       
   317 
       
   318 TL2CapEntityInfo HInformationResponse::RemoteExtendedFeatureMask() const
       
   319 	{
       
   320 	LOG_FUNC
       
   321 	TL2CapEntityInfo peerExtendedFeatures;
       
   322 	
       
   323 	// Check that the features are supported, and the message is a valid length.  If not return the default
       
   324 	// remote entity information.
       
   325 	if(InfoType() == EExtendedFeaturesSupported && Result() == ESuccess)
       
   326 		{
       
   327 		TUint32 featureMask = GetLittleEndian32(KDataOffset, iCommand);
       
   328 
       
   329 		if(featureMask & EFlowControlMode)
       
   330 			{
       
   331 			peerExtendedFeatures.SetSupportFlowControl();
       
   332 			}
       
   333 			
       
   334 		if(featureMask & ERetransmissionMode)
       
   335 			{
       
   336 			peerExtendedFeatures.SetSupportRetranmission();
       
   337 			}
       
   338 			
       
   339 		if(featureMask & EBiDirectionalQOS)
       
   340 			{
       
   341 			peerExtendedFeatures.SetSupportBiDirectionalQos();
       
   342 			}
       
   343 
       
   344 		if(featureMask & EEnhancedRetransmissionMode)
       
   345 			{
       
   346 			peerExtendedFeatures.SetSupportEnhancedRetransmissionMode();
       
   347 			}
       
   348 
       
   349 		if(featureMask & EStreamingMode)
       
   350 			{
       
   351 			peerExtendedFeatures.SetSupportStreamingMode();
       
   352 			}
       
   353 
       
   354 		if(featureMask & EFCSOption)
       
   355 			{
       
   356 			peerExtendedFeatures.SetSupportFCSOption();
       
   357 			}
       
   358 
       
   359 		peerExtendedFeatures.SetLinkInfoState(EL2CapEntityInfoDefined);
       
   360 		}
       
   361 		
       
   362 	return peerExtendedFeatures;
       
   363 	}
       
   364