bluetooth/btstack/avdtp/avdtpAllocators.h
changeset 0 29b1cd4cb562
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     1 // Copyright (c) 2003-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 // Defines the avdtp allocators
       
    15 // These classes help track values that are selected from a pool
       
    16 // The values are released as R-classes to reflect the "resource ownership"
       
    17 // that is conferred upon the owner.
       
    18 // 
       
    19 //
       
    20 
       
    21 /**
       
    22  @file
       
    23  @internalComponent
       
    24 */
       
    25 
       
    26 
       
    27 #ifndef _AVDTPALLOCATORS_H
       
    28 #define _AVDTPALLOCATORS_H
       
    29 
       
    30 #include <bluetoothav.h>
       
    31 #include "gavdpinterface.h"
       
    32 
       
    33 NONSHARABLE_CLASS(TBitFieldAllocator)
       
    34 /*
       
    35 abstract base for types that need to use a bit field to handle their values
       
    36 */
       
    37 	{
       
    38 friend class RTrackedValue;
       
    39 protected:
       
    40 	inline TBitFieldAllocator() : iBitField(0) {};
       
    41 	TInt Get(TInt& aResult, TInt aMax, TInt aMin=0);
       
    42 	void Free(TInt aVal);
       
    43 protected:
       
    44 	TUint64 iBitField;
       
    45 	TInt iMin;
       
    46 	};
       
    47 
       
    48 
       
    49 NONSHARABLE_CLASS(RTrackedValue)
       
    50 	{
       
    51 public:
       
    52 	inline void Close();
       
    53 	inline void Acquire(RTrackedValue& aTrackedValue); // this takes ownership
       
    54 protected:
       
    55 	inline RTrackedValue();
       
    56 	inline void Set(TBitFieldAllocator& aAllocator, TUint32 aValue);
       
    57 protected:
       
    58 	TBitFieldAllocator* iAllocator; //non-owned
       
    59 	TUint32 iValue;
       
    60 private:
       
    61 	RTrackedValue(const RTrackedValue&); // forbid copying
       
    62 	RTrackedValue& operator=(const RTrackedValue&);
       
    63 	};
       
    64 	
       
    65 inline RTrackedValue::RTrackedValue()
       
    66 : iAllocator(NULL), iValue(0)
       
    67 	{
       
    68 	}
       
    69 	
       
    70 inline void RTrackedValue::Set(TBitFieldAllocator& aAllocator, TUint32 aValue)
       
    71 	{
       
    72 	iAllocator=&aAllocator;
       
    73 	iValue=aValue;
       
    74 	}
       
    75 	
       
    76 inline void RTrackedValue::Close()
       
    77 	{
       
    78 	if (iAllocator)
       
    79 		{
       
    80 		iAllocator->Free(iValue);
       
    81 		}
       
    82 	}
       
    83 	
       
    84 inline void RTrackedValue::Acquire(RTrackedValue& aValue)
       
    85 	{
       
    86 	Set(*aValue.iAllocator, aValue.iValue);
       
    87 	// to transfer ownership:
       
    88 	new (&aValue) RTrackedValue;
       
    89 	}
       
    90 	
       
    91 NONSHARABLE_CLASS(RAvdtpTransactionLabel) : public RTrackedValue
       
    92 	{
       
    93 friend class TTransactionLabelManager;
       
    94 public:	
       
    95 	inline TAvdtpTransactionLabel TransactionLabel() const;
       
    96 	};
       
    97 	
       
    98 NONSHARABLE_CLASS(RTSID) : public RTrackedValue
       
    99 	{
       
   100 friend class TTSIDManager;
       
   101 public:	
       
   102 	inline TTSID TSID() const;
       
   103 	};	
       
   104 
       
   105 NONSHARABLE_CLASS(RTCID) : public RTrackedValue
       
   106 	{
       
   107 friend class TTCIDManager;
       
   108 public:	
       
   109 	inline TTCID TCID() const;
       
   110 	};	
       
   111 	
       
   112 NONSHARABLE_CLASS(RSEID) : public RTrackedValue
       
   113 	{
       
   114 friend class TSEIDManager;
       
   115 public:	
       
   116 	inline TSEID SEID() const;
       
   117 	};
       
   118 
       
   119 inline TAvdtpTransactionLabel RAvdtpTransactionLabel::TransactionLabel() const
       
   120 	{
       
   121 	return static_cast<TAvdtpTransactionLabel>(iValue);
       
   122 	}
       
   123 
       
   124 inline TTCID RTCID::TCID() const
       
   125 	{
       
   126 	return static_cast<TTCID>(iValue);
       
   127 	}
       
   128 
       
   129 inline TTSID RTSID::TSID() const
       
   130 	{
       
   131 	return static_cast<TTSID>(iValue);
       
   132 	}
       
   133 
       
   134 inline TSEID RSEID::SEID() const
       
   135 	{
       
   136 	return static_cast<TSEID>(iValue);
       
   137 	}
       
   138 
       
   139 NONSHARABLE_CLASS(TTransactionLabelManager) : public TBitFieldAllocator
       
   140 	{
       
   141 public:
       
   142 	inline TInt	GetLabel(RAvdtpTransactionLabel& aLabel);
       
   143 	};
       
   144 	
       
   145 	
       
   146 NONSHARABLE_CLASS(TTCIDManager) : public TBitFieldAllocator
       
   147 	{
       
   148 public:
       
   149 	inline TInt GetTCID(RTCID& aTCID);
       
   150 	inline TInt GrabTCID(RTCID& aTCID, TTCID aAssignedTCID);
       
   151 	};
       
   152 	
       
   153 NONSHARABLE_CLASS(TTSIDManager) : public TBitFieldAllocator
       
   154 	{
       
   155 public:
       
   156 	inline TInt GetTSID(RTSID& aTSID);
       
   157 	};
       
   158 	
       
   159 NONSHARABLE_CLASS(TSEIDManager) : public TBitFieldAllocator
       
   160 	{
       
   161 public:
       
   162 	inline TInt GetSEID(RSEID& aSEID);
       
   163 	};
       
   164 	
       
   165 inline TInt TTransactionLabelManager::GetLabel(RAvdtpTransactionLabel& aLabel)
       
   166 	{
       
   167 	TInt val, res;
       
   168 	res = Get(val, KMaxTUint5);
       
   169 	if (res==KErrNone)
       
   170 		{
       
   171 		aLabel.Set(*this, val);
       
   172 		}
       
   173 	return res;
       
   174 	}
       
   175 
       
   176 inline TInt TTCIDManager::GetTCID(RTCID& aTCID)
       
   177 	{
       
   178 	TInt val, res;
       
   179 	res = Get(val, KMaxTUint5-1, KMinimumRealTCID); 	// cannot use top bit as RFD in spec
       
   180 	if (res==KErrNone)
       
   181 		{
       
   182 		aTCID.Set(*this, val);
       
   183 		}
       
   184 	return res;
       
   185 	}
       
   186 
       
   187 inline TInt TTCIDManager::GrabTCID(RTCID& aTCID, TTCID aAssignedTCID)
       
   188 	{
       
   189 	// during muxing the remote can "assign" a TCID from our pool
       
   190 	// if things are going ok with remote it should be a free TCID
       
   191 	// but the return allows for an error just in case
       
   192 	// constrain the range to reuse case-class code :o)
       
   193 	TInt val, res;
       
   194 	res = Get(val, aAssignedTCID, aAssignedTCID);
       
   195 	if (res==KErrNone)
       
   196 		{
       
   197 		aTCID.Set(*this, val);
       
   198 		}
       
   199 	return res;
       
   200 	}
       
   201 	
       
   202 inline TInt TTSIDManager::GetTSID(RTSID& aTSID)
       
   203 	{
       
   204 	TInt val, res;
       
   205 	res = Get(val, KMaxTUint5-1, KMinimumRealTSID); 	// cannot use top bit as RFD in spec
       
   206 	if (res==KErrNone)
       
   207 		{
       
   208 		aTSID.Set(*this, val);
       
   209 		}
       
   210 	return res;
       
   211 	}
       
   212 
       
   213 inline TInt TSEIDManager::GetSEID(RSEID& aSEID)
       
   214 	{
       
   215 	TInt val, res;
       
   216 	// to make agnostic to implementation of local/remote seids
       
   217 	// remembering this only allocates semantically local seids...
       
   218 	TSEID min(KMinimumRealSEID, ETrue);
       
   219 	TSEID max(KMaxTUint6-1, ETrue); // 0x3f is RFD, see 8.18
       
   220 	__ASSERT_COMPILE(KMinimumRealSEID<KMaxTUint6);
       
   221 	res = Get(val, max.Value(), min.Value()); 
       
   222 	if (res==KErrNone)
       
   223 		{
       
   224 		aSEID.Set(*this, val);
       
   225 		}
       
   226 	return res;
       
   227 	}
       
   228 
       
   229 #endif //_AVDTPALLOCATORS_H