bluetooth/btstack/l2cap/l2capSigPacketDisconnection.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 "l2capSigPacketDisconnection.h"
       
    19 #include "l2capSignalHandler.h"
       
    20 #include "l2constants.h"
       
    21 
       
    22 #ifdef __FLOG_ACTIVE
       
    23 _LIT8(KLogComponent, LOG_COMPONENT_L2CAP);
       
    24 #endif
       
    25 
       
    26 //					DISCONNECT REQUEST COMMAND 
       
    27 
       
    28 /*static*/ HDisconnectRequest* HDisconnectRequest::New(TL2CAPPort aLocalPort, TL2CAPPort aRemotePort,
       
    29 		  	                                           TUint8 aRTXTimerDuration,
       
    30 	                                                   TUint16 aERTXTimerDuration)
       
    31 	{
       
    32 	LOG_STATIC_FUNC
       
    33 	HDisconnectRequest* cmd = NULL;
       
    34 	RMBufChain buf;
       
    35 	TRAPD(rerr, buf.AllocL(KDisconnectRequestLength));
       
    36 	if(rerr == KErrNone)
       
    37 		{
       
    38 		cmd = new HDisconnectRequest(buf, aRTXTimerDuration, aERTXTimerDuration);
       
    39 		if(cmd)
       
    40 			{
       
    41 			// Setup message contents.
       
    42 			cmd->SetCode(EDisconnectionRequest);
       
    43 			cmd->SetID(KUninitialisedID);
       
    44 			cmd->WriteDataLength();
       
    45 			
       
    46 			cmd->SetDestinationCID(aRemotePort);
       
    47 			cmd->SetSourceCID(aLocalPort);
       
    48 			}
       
    49 		else
       
    50 			{
       
    51 			// Free the allocated buffer.
       
    52 			buf.Free();
       
    53 			}
       
    54 		}
       
    55 	return cmd;
       
    56 	}		
       
    57 
       
    58 /**
       
    59 Verifies that the buffer contains a structurally correct command. 
       
    60 This does not ensure that the content of the command is semantically valid.
       
    61 
       
    62 @param aCommmand A new HL2CapCommand if the buffer contains a structurally valid command.
       
    63 @param aBuffer The buffer containing the command
       
    64 @return KErrNone if the command if created.
       
    65 		KErrNoMemory if the command structure is valid but cannot be created.
       
    66 		KErrCorrupt if the command structure is invalid.
       
    67 */
       
    68 TInt HDisconnectRequest::NewCommandIfValid(RMBufChain& aBuffer, HL2CapCommand*& aCommand)
       
    69 	{
       
    70 	LOG_STATIC_FUNC
       
    71 	// Firstly align the MBufChain.  The maximum size we can align the 
       
    72 	// MBufChain to is the maximum MBuf size
       
    73 	__ASSERT_COMPILE(KDisconnectRequestLength <= KMBufSmallSize);
       
    74 	
       
    75 	TInt length = aBuffer.Length();
       
    76 	if(length == KDisconnectRequestLength)
       
    77 		{
       
    78 		// Don't need to check result as we know that the MBufChain contains
       
    79 		// at least length bytes as we asked first.
       
    80 		(void)aBuffer.Align(length);
       
    81 		aCommand = new HDisconnectRequest(aBuffer);
       
    82 		if(aCommand)
       
    83 			{
       
    84 			return KErrNone;
       
    85 			}
       
    86 		else
       
    87 			{
       
    88 			return KErrNoMemory;
       
    89 			}
       
    90 		}
       
    91 	else
       
    92 		{
       
    93 		// Dodge length!
       
    94 		return KErrCorrupt;
       
    95 		}
       
    96 	}
       
    97 
       
    98 HDisconnectRequest::HDisconnectRequest(RMBufChain& aCommand,
       
    99                                        TUint8 aRTXTimerDuration,
       
   100                                        TUint16 aERTXTimerDuration)
       
   101  : HL2CapCommand(aCommand, aRTXTimerDuration, aERTXTimerDuration)
       
   102 	{
       
   103 	LOG_FUNC
       
   104 	}
       
   105 
       
   106 HDisconnectRequest::~HDisconnectRequest()
       
   107 	{
       
   108 	LOG_FUNC
       
   109 	}
       
   110 
       
   111 TBool HDisconnectRequest::ProcessCommand(CL2CapSignalHandler& aSignalHandler)
       
   112 	{
       
   113 	LOG_FUNC
       
   114 	if(aSignalHandler.HandleDisconnectRequest(this))
       
   115 		{
       
   116 		// The command has been handled.  Delete it.
       
   117 		delete this;
       
   118 		return ETrue;
       
   119 		}
       
   120 	else
       
   121 		{
       
   122 		return EFalse;
       
   123 		}
       
   124 	}
       
   125 
       
   126 
       
   127 
       
   128 //					DISCONNECT RESPONSE COMMAND 
       
   129 /*static*/ HDisconnectResponse* HDisconnectResponse::New(TUint8 aId, TL2CAPPort aLocalPort, TL2CAPPort aRemotePort)
       
   130 	{
       
   131 	LOG_STATIC_FUNC
       
   132 	HDisconnectResponse* cmd = NULL;
       
   133 	RMBufChain buf;
       
   134 	TRAPD(rerr, buf.AllocL(KDisconnectResponseLength));
       
   135 	if(rerr == KErrNone)
       
   136 		{
       
   137 		cmd = new HDisconnectResponse(buf);
       
   138 		if(cmd)
       
   139 			{
       
   140 			// Setup message contents.
       
   141 			cmd->SetCode(EDisconnectionResponse);
       
   142 			cmd->SetID(aId);
       
   143 			cmd->WriteDataLength();
       
   144 			
       
   145 			cmd->SetDestinationCID(aLocalPort);
       
   146 			cmd->SetSourceCID(aRemotePort);
       
   147 			}
       
   148 		else
       
   149 			{
       
   150 			// Free the allocated buffer.
       
   151 			buf.Free();
       
   152 			}
       
   153 		}
       
   154 	return cmd;
       
   155 	}	
       
   156 	
       
   157 /**
       
   158 Verifies that the buffer contains a structurally correct command. 
       
   159 This does not ensure that the content of the command is semantically valid.
       
   160 
       
   161 @param aCommmand A new HL2CapCommand if the buffer contains a structurally valid command.
       
   162 @param aBuffer The buffer containing the command
       
   163 @return KErrNone if the command if created.
       
   164 		KErrNoMemory if the command structure is valid but cannot be created.
       
   165 		KErrCorrupt if the command structure is invalid.
       
   166 */
       
   167 TInt HDisconnectResponse::NewCommandIfValid(RMBufChain& aBuffer, HL2CapCommand*& aCommand)
       
   168 	{
       
   169 	LOG_STATIC_FUNC
       
   170 	// Firstly align the MBufChain.  The maximum size we can align the 
       
   171 	// MBufChain to is the maximum MBuf size
       
   172 	__ASSERT_COMPILE(KDisconnectResponseLength <= KMBufSmallSize);
       
   173 	
       
   174 	TInt length = aBuffer.Length();
       
   175 	if(length == KDisconnectResponseLength)
       
   176 		{
       
   177 		// Don't need to check result as we know that the MBufChain contains
       
   178 		// at least length bytes as we asked first.
       
   179 		(void)aBuffer.Align(length);
       
   180 		aCommand = new HDisconnectResponse(aBuffer);
       
   181 		if(aCommand)
       
   182 			{
       
   183 			return KErrNone;
       
   184 			}
       
   185 		else
       
   186 			{
       
   187 			return KErrNoMemory;
       
   188 			}
       
   189 		}
       
   190 	else
       
   191 		{
       
   192 		// Dodge length!
       
   193 		return KErrCorrupt;
       
   194 		}
       
   195 	}
       
   196 
       
   197 HDisconnectResponse::HDisconnectResponse(RMBufChain& aCommand) 
       
   198  : HL2CapCommand(aCommand)
       
   199 	{
       
   200 	LOG_FUNC
       
   201 	}
       
   202 
       
   203 HDisconnectResponse::~HDisconnectResponse()
       
   204 	{
       
   205 	LOG_FUNC
       
   206 	}
       
   207 
       
   208 TBool HDisconnectResponse::ProcessCommand(CL2CapSignalHandler& aSignalHandler)	
       
   209 	{
       
   210 	LOG_FUNC
       
   211 	if(aSignalHandler.HandleDisconnectResponse(this))
       
   212 		{
       
   213 		// The command has been handled.  Delete it.
       
   214 		delete this;
       
   215 		return ETrue;
       
   216 		}
       
   217 	else
       
   218 		{
       
   219 		return EFalse;
       
   220 		}
       
   221 	}