bluetooth/btstack/l2cap/l2capCommand.h
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 // The base class for L2Cap Commands
       
    15 // 
       
    16 //
       
    17 
       
    18 #ifndef L2CAPCOMMAND_H
       
    19 #define L2CAPCOMMAND_H
       
    20 
       
    21 #include <es_mbuf.h>
       
    22 #include <bt_sock.h>
       
    23 
       
    24 #include "l2util.h"
       
    25 #include "l2constants.h"
       
    26 #include "L2types.h"
       
    27 
       
    28 
       
    29 class CL2CapSignalHandler;
       
    30 class CL2CAPMux;
       
    31 
       
    32 NONSHARABLE_CLASS(HL2CapCommand)
       
    33 	{
       
    34 public:
       
    35 	// Message format lengths and offsets.
       
    36 	const static TUint8 KCodeOffset					= 0;
       
    37 	const static TUint8 KIdOffset					= 1;
       
    38 	const static TUint8 KLengthOffset 				= 2;
       
    39 	const static TUint8 KL2CapCommandHeaderLength 	= 4;		
       
    40 
       
    41 	// RTX / ERTX Timer values.
       
    42 	const static TUint8 KDefaultRTXTimerDuration	= 5;	// Second(s)
       
    43 	const static TUint8 KDefaultERTXTimerDuration	= 120;	// Second(s)
       
    44 	const static TUint8 KMinRTXTimerDuration		= 1;
       
    45 	const static TUint8 KMinERTXTimerDuration		= 60;
       
    46 	const static TUint8 KMaxRTXTimerDuration		= 60;
       
    47 	const static TUint16 KMaxERTXTimerDuration		= 300;
       
    48 	const static TUint8 KNumberOfRetransmissions	= 1;
       
    49 
       
    50 	// Used to specify that the ID has not been set yet.
       
    51 	const static TUint8 KUninitialisedID			= 0;
       
    52 
       
    53 	enum TRTXTimerType
       
    54 		{
       
    55 		ERTXTimer,
       
    56 		EERTXTimer,
       
    57 		};
       
    58 			
       
    59 	static TInt DecodeCommand(RMBufChain& aCommandString, HL2CapCommand*& aCommand);
       
    60 
       
    61 	virtual ~HL2CapCommand();
       
    62 	HL2CapCommand(RMBufChain& aCommand, 
       
    63 	              TUint8 aRTXTimerDuration = KDefaultRTXTimerDuration,
       
    64 	              TUint16 aERTXTimerDuration = KDefaultERTXTimerDuration);
       
    65 
       
    66 	// Pure virtual that is used to ensure that derived class have a mechanism to process themselves in
       
    67 	// signal handler
       
    68 	virtual	TBool ProcessCommand(CL2CapSignalHandler& aSignalHandler) = 0;
       
    69 
       
    70 	// This is called to pull an outgoing L2CAP command.  This method will initialise
       
    71 	// the CID if one has not yet been allocated. 
       
    72 	TInt GetCommand(CL2CAPMux& aMuxer, RMBufChain& aBuffer);
       
    73 	
       
    74 	inline TBool IsRequest() const;
       
    75 
       
    76 	inline TSignallingCommand Code() const;
       
    77 	inline void SetCode(TSignallingCommand aCode);
       
    78 	
       
    79 	inline TUint8 ID() const;
       
    80 	inline void SetID(TUint8 aID);
       
    81 
       
    82 	inline TInt DataLength() const;
       
    83 	inline void WriteDataLength();
       
    84 
       
    85 	inline TInt CommandLength() const;
       
    86 
       
    87 	void StartRTXTimer(TRTXTimerType aType, CL2CapSignalHandler& aSignalHandler);
       
    88 	static TInt ResponseTimerExpired(TAny* aL2CapCommand);
       
    89 	
       
    90 	static void PutLittleEndian32(TUint32 aValue, TInt aOffset, RMBufChain& aCommand);
       
    91 	static void PutLittleEndian16(TUint16 aValue,TInt aOffset, RMBufChain& aCommand);
       
    92 	static void PutByte(TUint8 aByte, TInt aOffset, RMBufChain& aCommand);
       
    93 
       
    94 	static TUint32 GetLittleEndian32(TInt aOffset, const RMBufChain& aCommand);
       
    95 	static TUint16 GetLittleEndian16(TInt aOffset, const RMBufChain& aCommand);
       
    96 	static TUint8 GetByte(TInt aOffset, const RMBufChain& aCommand);
       
    97 
       
    98 	TBool StatefulResponse();
       
    99 protected:
       
   100 	// RTx / ERTx Timer methods.
       
   101 	void CancelResponseTimer();
       
   102 	void HandleResponseTimerExpiry();
       
   103 	
       
   104 //members	
       
   105 public:
       
   106 	//Link member so that CCFramePDU can hold a linked list of these commands
       
   107 	TDblQueLink iLink;
       
   108 
       
   109 protected:
       
   110 	RMBufChain iCommand;
       
   111 	TBool iResponseTimerRunning;
       
   112 	TDeltaTimerEntry iResponseTimerEntry;
       
   113 	CL2CapSignalHandler* iResponseSignalHandler;
       
   114 	TUint8 iRetransmissionCounter;
       
   115 	
       
   116 	TUint8 iRTXTimerDuration;
       
   117 	TUint16 iERTXTimerDuration;
       
   118 	};
       
   119 
       
   120 
       
   121 NONSHARABLE_CLASS(HInvalidCommand) : public HL2CapCommand
       
   122 	{
       
   123 public:
       
   124 	HInvalidCommand(RMBufChain& aCommand);
       
   125 	~HInvalidCommand();
       
   126 
       
   127 	TBool ProcessCommand(CL2CapSignalHandler& aSignalHandler);
       
   128 	};
       
   129 
       
   130 
       
   131 inline TBool HL2CapCommand::IsRequest() const
       
   132 	{
       
   133 	// Every request command code has the LSB clear.
       
   134 	return (!(Code() & 0x01));
       
   135 	}
       
   136 
       
   137 inline TSignallingCommand HL2CapCommand::Code() const
       
   138 	{
       
   139 	return TSignallingCommand(GetByte(KCodeOffset, iCommand));
       
   140 	}
       
   141 	
       
   142 inline void HL2CapCommand::SetCode(TSignallingCommand aCode)
       
   143 	{
       
   144 	PutByte(static_cast<TUint8>(aCode), KCodeOffset, iCommand);
       
   145 	}
       
   146 
       
   147 inline TUint8 HL2CapCommand::ID() const
       
   148 	{
       
   149 	return GetByte(KIdOffset, iCommand);
       
   150 	}
       
   151 	
       
   152 inline void HL2CapCommand::SetID(TUint8 aID)
       
   153 	{
       
   154 	PutByte(aID, KIdOffset, iCommand);
       
   155 	}
       
   156 	
       
   157 inline TInt HL2CapCommand::DataLength() const
       
   158 	{
       
   159 	return GetLittleEndian16(KLengthOffset, iCommand);
       
   160 	}
       
   161 
       
   162 inline void HL2CapCommand::WriteDataLength()
       
   163 	{
       
   164 	PutLittleEndian16(static_cast<TUint16>(iCommand.Length() - KL2CapCommandHeaderLength), KLengthOffset, iCommand);
       
   165 	}
       
   166 
       
   167 inline TInt HL2CapCommand::CommandLength() const
       
   168 	{
       
   169 	return iCommand.Length();
       
   170 	}
       
   171 	
       
   172 
       
   173 #endif
       
   174