bluetooth/btstack/l2cap/l2capSigPacketEcho.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 "l2capSigPacketEcho.h"
       
    19 #include "l2capSignalHandler.h"
       
    20 
       
    21 #ifdef __FLOG_ACTIVE
       
    22 _LIT8(KLogComponent, LOG_COMPONENT_L2CAP);
       
    23 #endif
       
    24 
       
    25 //                   ECHO REQUEST COMMAND 
       
    26 //For outgoing echo requests driven by an L2Cap IOCTL
       
    27 
       
    28 HEchoRequest::HEchoRequest(RMBufChain& aCommand,
       
    29 		  	               TUint8 aRTXTimerDuration,
       
    30 	                       TUint16 aERTXTimerDuration)
       
    31  : HL2CapCommand(aCommand, aRTXTimerDuration, aERTXTimerDuration),
       
    32    iEchoResponseHandler(NULL)
       
    33 	{
       
    34 	LOG_FUNC
       
    35 	}
       
    36 
       
    37 /*static*/ HEchoRequest* HEchoRequest::New(RMBufChain& aData,
       
    38 		  	                               TUint8 aRTXTimerDuration,
       
    39 	                                       TUint16 aERTXTimerDuration)
       
    40 	{
       
    41 	LOG_STATIC_FUNC
       
    42 	HEchoRequest* cmd = NULL;
       
    43 	RMBufChain buf;
       
    44 	buf.Assign(aData);
       
    45 	TRAPD(rerr, buf.PrependL(KEchoRequestEmptyLength));
       
    46 	
       
    47 	if(rerr == KErrNone)
       
    48 		{
       
    49 		cmd = new HEchoRequest(buf, aRTXTimerDuration, aERTXTimerDuration);
       
    50 		if(cmd)
       
    51 			{
       
    52 			// Setup message contents.
       
    53 			cmd->SetCode(EEchoRequest);
       
    54 			cmd->SetID(KUninitialisedID);
       
    55 			cmd->WriteDataLength();
       
    56 			}
       
    57 		else
       
    58 			{
       
    59 			// Free the allocated buffer.
       
    60 			buf.Free();
       
    61 			}
       
    62 		}
       
    63 	return cmd;
       
    64 	}		
       
    65 
       
    66 /*static*/ HEchoRequest* HEchoRequest::New(TUint8 aRTXTimerDuration,
       
    67 	                                       TUint16 aERTXTimerDuration)
       
    68 	{
       
    69 	LOG_STATIC_FUNC
       
    70 	HEchoRequest* cmd = NULL;
       
    71 	RMBufChain buf;
       
    72 	TRAPD(rerr, buf.AllocL(KEchoRequestEmptyLength));
       
    73 	
       
    74 	if(rerr == KErrNone)
       
    75 		{
       
    76 		cmd = new HEchoRequest(buf, aRTXTimerDuration, aERTXTimerDuration);
       
    77 		if(cmd)
       
    78 			{
       
    79 			// Setup message contents.
       
    80 			cmd->SetCode(EEchoRequest);
       
    81 			cmd->SetID(KUninitialisedID);
       
    82 			cmd->WriteDataLength();
       
    83 			}
       
    84 		else
       
    85 			{
       
    86 			// Free the allocated buffer.
       
    87 			buf.Free();
       
    88 			}
       
    89 		}
       
    90 	return cmd;
       
    91 	}	
       
    92 	
       
    93 /**
       
    94 Verifies that the buffer contains a structurally correct command. 
       
    95 This does not ensure that the content of the command is semantically valid.
       
    96 
       
    97 @param aCommmand A new HL2CapCommand if the buffer contains a structurally valid command.
       
    98 @param aBuffer The buffer containing the command
       
    99 @return KErrNone if the command if created.
       
   100 		KErrNoMemory if the command structure is valid but cannot be created.
       
   101 		KErrCorrupt if the command structure is invalid.
       
   102 */
       
   103 TInt HEchoRequest::NewCommandIfValid(RMBufChain& aBuffer, HL2CapCommand*& aCommand)
       
   104 	{
       
   105 	LOG_STATIC_FUNC
       
   106 	// Firstly align the MBufChain.  The maximum size we can align the 
       
   107 	// MBufChain to is the maximum MBuf size
       
   108 	__ASSERT_COMPILE(KEchoRequestEmptyLength <= KMBufSmallSize);
       
   109 	
       
   110 	// We don't align the data as that way we don't need to worry about it being 
       
   111 	// larger than one MBuf in size.  It doesn't need to be aligned anyway as the
       
   112 	// only way the data is accessed is by being copied out.
       
   113 	TInt length = aBuffer.Length();
       
   114 	if(length >= KEchoRequestEmptyLength)
       
   115 		{
       
   116 		// Don't need to check result as we know that the MBufChain contains
       
   117 		// at least length bytes as we asked first.
       
   118 		(void)aBuffer.Align(KEchoRequestEmptyLength);
       
   119 		aCommand = new HEchoRequest(aBuffer);
       
   120 		if(aCommand)
       
   121 			{
       
   122 			return KErrNone;
       
   123 			}
       
   124 		else
       
   125 			{
       
   126 			return KErrNoMemory;
       
   127 			}
       
   128 		}
       
   129 	else
       
   130 		{
       
   131 		// Dodge length!
       
   132 		return KErrCorrupt;
       
   133 		}	
       
   134 	}
       
   135 	
       
   136 HEchoRequest::~HEchoRequest()
       
   137 	{
       
   138 	LOG_FUNC
       
   139 	}
       
   140 
       
   141 TBool HEchoRequest::ProcessCommand(CL2CapSignalHandler& aSignalHandler)
       
   142 	{
       
   143 	LOG_FUNC
       
   144 	if(aSignalHandler.HandleEchoRequest(this))
       
   145 		{
       
   146 		// The command has been handled.  Delete it.
       
   147 		delete this;
       
   148 		return ETrue;
       
   149 		}
       
   150 	else
       
   151 		{
       
   152 		return EFalse;
       
   153 		}
       
   154 	}
       
   155 
       
   156 TInt HEchoRequest::GetData(RMBufChain& aData) const
       
   157 	{
       
   158 	LOG_FUNC
       
   159 	TInt rerr = KErrNone;
       
   160 	if(DataLength() > KEchoRequestEmptyLength)
       
   161 		{
       
   162 		TRAP(rerr, iCommand.CopyL(aData, KEchoRequestEmptyLength));
       
   163 		}
       
   164 	else
       
   165 		{
       
   166 		rerr = KErrNotFound;
       
   167 		}
       
   168 	return rerr;
       
   169 	}
       
   170 
       
   171 
       
   172 //					ECHO RESPONSE COMMAND 
       
   173 //Constructor to deal command coming in from remote side
       
   174 HEchoResponse::HEchoResponse(RMBufChain& aCommand) 
       
   175  : HL2CapCommand(aCommand)
       
   176 	{
       
   177 	LOG_FUNC
       
   178 	}
       
   179 
       
   180 /*static*/ HEchoResponse* HEchoResponse::New(RMBufChain& aData, TUint8 aId)
       
   181 	{
       
   182 	LOG_STATIC_FUNC
       
   183 	HEchoResponse* cmd = NULL;
       
   184 	RMBufChain buf;
       
   185 	buf.Assign(aData);
       
   186 	TRAPD(rerr, buf.PrependL(KEchoResponseEmptyLength));
       
   187 	
       
   188 	if(rerr == KErrNone)
       
   189 		{
       
   190 		cmd = new HEchoResponse(buf);
       
   191 		if(cmd)
       
   192 			{
       
   193 			// Setup message contents.
       
   194 			cmd->SetCode(EEchoResponse);
       
   195 			cmd->SetID(aId);
       
   196 			cmd->WriteDataLength();
       
   197 			}
       
   198 		else
       
   199 			{
       
   200 			// Free the allocated buffer.
       
   201 			buf.Free();
       
   202 			}
       
   203 		}
       
   204 	return cmd;
       
   205 	}		
       
   206 
       
   207 /*static*/ HEchoResponse* HEchoResponse::New(TUint8 aId)
       
   208 	{
       
   209 	LOG_STATIC_FUNC
       
   210 	HEchoResponse* cmd = NULL;
       
   211 	RMBufChain buf;
       
   212 	TRAPD(rerr, buf.AllocL(KEchoResponseEmptyLength));
       
   213 	
       
   214 	if(rerr == KErrNone)
       
   215 		{
       
   216 		cmd = new HEchoResponse(buf);
       
   217 		if(cmd)
       
   218 			{
       
   219 			// Setup message contents.
       
   220 			cmd->SetCode(EEchoResponse);
       
   221 			cmd->SetID(aId);
       
   222 			cmd->WriteDataLength();
       
   223 			}
       
   224 		else
       
   225 			{
       
   226 			// Free the allocated buffer.
       
   227 			buf.Free();
       
   228 			}
       
   229 		}
       
   230 	return cmd;
       
   231 	}		
       
   232 
       
   233 /**
       
   234 Verifies that the buffer contains a structurally correct command. 
       
   235 This does not ensure that the content of the command is semantically valid.
       
   236 
       
   237 @param aCommmand A new HL2CapCommand if the buffer contains a structurally valid command.
       
   238 @param aBuffer The buffer containing the command
       
   239 @return KErrNone if the command if created.
       
   240 		KErrNoMemory if the command structure is valid but cannot be created.
       
   241 		KErrCorrupt if the command structure is invalid.
       
   242 */
       
   243 TInt HEchoResponse::NewCommandIfValid(RMBufChain& aBuffer, HL2CapCommand*& aCommand)
       
   244 	{
       
   245 	LOG_STATIC_FUNC
       
   246 	// Firstly align the MBufChain.  The maximum size we can align the 
       
   247 	// MBufChain to is the maximum MBuf size
       
   248 	__ASSERT_COMPILE(KEchoResponseEmptyLength <= KMBufSmallSize);
       
   249 	
       
   250 	// We don't align the data as that way we don't need to worry about it being 
       
   251 	// larger than one MBuf in size.  It doesn't need to be aligned anyway as the
       
   252 	// only way the data is accessed is by being copied out.
       
   253 	TInt length = aBuffer.Length();
       
   254 	if(length >= KEchoResponseEmptyLength)
       
   255 		{
       
   256 		// Don't need to check result as we know that the MBufChain contains
       
   257 		// at least length bytes as we asked first.
       
   258 		(void)aBuffer.Align(KEchoResponseEmptyLength);
       
   259 		aCommand = new HEchoResponse(aBuffer);
       
   260 		if(aCommand)
       
   261 			{
       
   262 			return KErrNone;
       
   263 			}
       
   264 		else
       
   265 			{
       
   266 			return KErrNoMemory;
       
   267 			}
       
   268 		}
       
   269 	else
       
   270 		{
       
   271 		// Dodge length!
       
   272 		return KErrCorrupt;
       
   273 		}	
       
   274 	}
       
   275 
       
   276 HEchoResponse::~HEchoResponse()
       
   277 	{
       
   278 	LOG_FUNC
       
   279 	}
       
   280 
       
   281 
       
   282 TBool HEchoResponse::ProcessCommand(CL2CapSignalHandler& aSignalHandler)	// Handle incoming echo response
       
   283 	{
       
   284 	LOG_FUNC
       
   285 	if(aSignalHandler.HandleEchoResponse(this))
       
   286 		{
       
   287 		// The command has been handled.  Delete it.
       
   288 		delete this;
       
   289 		return ETrue;
       
   290 		}
       
   291 	else
       
   292 		{
       
   293 		return EFalse;
       
   294 		}
       
   295 	}
       
   296 	
       
   297 TInt HEchoResponse::GetData(RMBufChain& aData) const
       
   298 	{
       
   299 	LOG_FUNC
       
   300 	TInt rerr = KErrNone;
       
   301 	if(DataLength() > KEchoResponseEmptyLength)
       
   302 		{
       
   303 		TRAP(rerr, iCommand.CopyL(aData, KEchoResponseEmptyLength));
       
   304 		}
       
   305 	else
       
   306 		{
       
   307 		rerr = KErrNotFound;
       
   308 		}
       
   309 	return rerr;
       
   310 	}
       
   311