/*++
*******************************************************************************
* Copyright (c) 1990-98 Digi International Inc., All Rights Reserved				*
* 																										*
* This software contains proprietary and confidential information of Digi		*
* International Inc.  By accepting transfer of this copy, Recipient agrees		*
* to retain this software in confidence, to prevent disclosure to others,		*
* and to make no use of this software other than that for which it was			*
* delivered.  This is an unpublished copyrighted work of Digi International	*
* Inc.  Except as permitted by federal law, 17 USC 117, copying is strictly	*
* prohibited.																						*
* 																										*
* Restricted Rights Legend																		*
*																										*
* Use, duplication, or disclosure by the Government is subject to					*
* restrictions set forth in sub-paragraph (c)(1)(ii) of The Rights in			*
* Technical Data and Computer Software clause at DFARS 252.227-7031 or			*
* subparagraphs (c)(1) and (2) of the Commercial Computer Software -				*
* Restricted Rights at 48 CFR 52.227-19, as applicable.								*
*																										*
* Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343				*
*																										*
*******************************************************************************

Module Name:

   dxbcredit.h

Abstract:

   This header defines all routines to handle credit reporting (or position reports).
   Uses the DXB_CREDIT_DESCRIPTOR structure.  All routines that use this structure 
	expect the Stream spinlock to be held when the routine is called, and released 
	sometime later.

Author:

   Michael Zarns

--*/
#ifndef DXBCREDIT_H
#define DXBCREDIT_H


__inline VOID
dxbCreditInit( PDXB_CREDIT_DESCRIPTOR pDesc, ULONG nCredits, ULONG nRefundable, ULONG Max, ULONG FCost )
{
	pDesc->Max = Max;
	pDesc->FCost = FCost;
	pDesc->nCredits = nCredits;
	pDesc->nRefundable = nRefundable;
}

__inline ULONG
dxbCreditAvailable( DXB_CREDIT_DESCRIPTOR const *pDesc )
{
	return pDesc->nCredits;
}

__inline BOOLEAN
dxbCreditApproved( DXB_CREDIT_DESCRIPTOR const *pDesc, ULONG Amount )
{
	return Amount <= dxbCreditAvailable( pDesc );
}

__inline ULONG
dxbCreditCap( DXB_CREDIT_DESCRIPTOR *pDesc )
{
	return pDesc->Max;
}

__inline VOID
dxbCreditCharge( DXB_CREDIT_DESCRIPTOR *pDesc, ULONG Amount )
{
	ASSERT( Amount <= pDesc->nCredits );
	pDesc->nCredits -= Amount;
}

__inline VOID
dxbCreditDeposit( DXB_CREDIT_DESCRIPTOR *pDesc, ULONG Amount )
{
	ASSERT( Amount < MAXULONG - pDesc->nCredits );
	pDesc->nCredits += Amount;

	if ( pDesc->nCredits > dxbCreditCap( pDesc ) )
		pDesc->nCredits = dxbCreditCap( pDesc );

#if DBG
	if ( pDesc->nCredits > pDesc->nCreditsMax )
		pDesc->nCreditsMax = pDesc->nCredits;
#endif
}

__inline VOID
dxbCreditMarkRefundable( DXB_CREDIT_DESCRIPTOR *pDesc, ULONG Amount )
{
	ASSERT( Amount < MAXULONG - pDesc->nRefundable );
	pDesc->nRefundable += Amount;
}

__inline ULONG
dxbCreditRefundable( DXB_CREDIT_DESCRIPTOR *pDesc )
{
	return pDesc->nRefundable;
}

__inline VOID
dxbCreditRefund( DXB_CREDIT_DESCRIPTOR *pDesc, ULONG Amount )
{
	ASSERT( Amount <= dxbCreditRefundable( pDesc ) );
	pDesc->nRefundable -= Amount;

	dxbCreditDeposit( pDesc, Amount );
}

__inline ULONG 
dxbCreditPutMsgCost( DXB_CREDIT_DESCRIPTOR const *pDesc, DS_PUTMSG const *pPutMsg )
{
	return pPutMsg->cbControl + pPutMsg->cbData + pDesc->FCost;
}

__inline ULONG
dxbCreditEventCost( DXB_CREDIT_DESCRIPTOR const *pDesc, ULONG cbCtrl, ULONG cbData )
{
	return cbCtrl + cbData + pDesc->FCost;
}

#endif
