ptlbase.cpp

来自「ABis无线接口全套资料」· C++ 代码 · 共 1,059 行 · 第 1/2 页

CPP
1,059
字号
/* ======================================================================== *\
 |
 |
 |  JOYIT Communication Technology
 |  Copyright (C)  2002-2003,  All Right Reserved.
 |
 |  System: Programmable Signaling Gateway
 |  Sub-system: OAM Server
 |  Filename: ptlbase.cpp
 |  Environment: Red Hat Linux 7.2 & GNU C/C++ Compiler 2.96
 |  Function description: Protocol base class
 |
\* ======================================================================== */

#ifndef _PTLBASE_HPP
#include "ptlbase.hpp"
#endif

CIeBase::CIeBase( )
{
	Init( );
}

CIeBase::~CIeBase( )
{
	Release( );
}

// ===========================================================================
//
// Description: Initialize all method data of CIeBase object.
// Input:  Null
// Output: Null
// Return: void
//
// ===========================================================================
void CIeBase::Init( )
{
	id = 0;		// Primary key
	len = 0;	// Length of IE(data) not include id and len
	memset(&ieStatus, 0, sizeof(SIEStatus));	// Status of this IE.

	ptr = NULL;	// Pointer point to IE's content when length of IE data longer than 4 bytes.
}

// ===========================================================================
//
// Description: Release memory that the IE seizured.
// Input:  Null
// Output: Null
// Return: void
//
// ===========================================================================
void CIeBase::Release( )
{
	if ((len > SIZE_OF_POINTER)
		&& (ptr != NULL))
	{
		delete ptr;
	}

	id = 0;		// Primary key
	len = 0;	// Length of IE(data) not include id and len
	memset(&ieStatus, 0, sizeof(SIEStatus));	// Status of this IE.

	ptr = NULL;
}

// ===========================================================================
//
// Description: Gain the IE's data.
// Input:  NULL
// Output:
//		l		Length of IE's data.
// Return: pointer point to IE's data.
//
// ===========================================================================
char  * CIeBase::GetIeData(UINT16& l)
{
	l = len;

	return ((len > SIZE_OF_POINTER) ? (ptr) : ((char *)&data));
}

// ===========================================================================
//
// Description: Gain the IE's data.
// Input:  NULL 
// Output: NULL
// Return: Value of IE's data(When length of IE's data not longer than 4-byte).
//
// ===========================================================================
UINT32 CIeBase::GetIeData( )
{
	return data;
}

// ===========================================================================
//
// Description: Assign operator overload funciton.
// Input:
//		s		Source CIeBase object.
// Output: Null
// Return: Null
//
// ===========================================================================
void CIeBase::operator = (CIeBase& s)
{
	id = s.id;
	len = s.len;
	data = s.data; // or ptr = s.ptr;

	memcpy((char *)&ieStatus, (char *)&s.ieStatus, sizeof(SIEStatus));

	if ((s.len > SIZE_OF_POINTER)
		&& (s.ptr != NULL))
	{
		ptr = new char[s.len];

		if (ptr != NULL)
		{
			memcpy(ptr, s.ptr, s.len);
		}
	}
}

// ===========================================================================
//
// Description: Copy function.
// Input:
//		s		Source CIeBase object.
// Output: Null
// Return: Null
//
// ===========================================================================

void CIeBase::Copy(CIeBase& s)
{
	id = s.id;
	len = s.len;
	data = s.data; // or ptr = s.ptr;

	memcpy((char *)&ieStatus, (char *)&s.ieStatus, sizeof(SIEStatus));
}

// ==========================================================================
CPduBase::CPduBase( )
{
	Init( );
}

CPduBase::CPduBase(UINT8 pt, UINT8 pv, UINT8 pcf, UINT16 mt)
{
	Init( );
	
	pType = pt;
	pVariant = pv;
	ss7Pcf = pcf;
	mType = mt;
}

CPduBase::CPduBase(CPduBase& s) // Add 2005-11-04, by Wujianjin. Copy construction function.
{
	pType = s.pType;		// Protocol type, ISUP/TUP/SCCP/M3UA/SIP.
	pVariant = s.pVariant;	// Protocol variant, such as CHINA/ETSI/ANSI/ITU, etc.
	ss7Pcf = s.ss7Pcf;		// SS7 point code format. 24-bit/14-bit
	primType = s.primType;	// Joyit API primitive type. Only available in Joyit API message. 2005-10-27, by Wujianjin.
	mType = s.mType;		// Joyit API (ISUP/TUP/DSS1) message type. 2005-10-27, by Wujianjin.
	IeCounter = s.IeCounter;			// Number of IE be included.
	// memcpy(Index2Ieid, s.Index2Ieid, MAX_IE);	// Store the sequence of all IEs.
	memcpy(Ieid2Index, s.Ieid2Index, MAX_IE);	// Store the IE's ID index.
	
	for (int i=0; i<IeCounter; ++i)
	{
		IE[i] = s.IE[i];	// Store all IE information.
	}

	memcpy(Header, s.Header, MAX_HEADER_LENGTH);
	LengthOfHeader = s.LengthOfHeader;
}

CPduBase::~CPduBase( )
{
	// Release the memory which allocated to IE.
	/*
	for (int i=0; i<IeCounter; ++i)
	{
		if (IE[i].len > SIZE_OF_POINTER)
		{
			delete IE[i].ptr;
			IE[i].ptr = NULL;
			IE[i].len = 0;
		}
	}
	*/
}

// ===========================================================================
//
// Description: Initialize all method data of CPduBase object.
// Input:  Null
// Output: Null
// Return: void
//
// ===========================================================================
void CPduBase::Init( ) // Initialize the PDU.
{
	// Set to zero.
	pType = 0;		// Protocol type, ISUP/TUP/SCCP/M3UA/SIP.
	pVariant = 0;	// Protocol variant, such as CHINA/ETSI/ANSI/ITU, etc.
	ss7Pcf = 0;		// SS7 point code format. 24-bit/14-bit
	primType = 0;
	mType = 0;		// Message type.
	IeCounter = 0;	// Number of IE be included.

	// Iniaitlize the two index array default value to 255.
	memset(Ieid2Index, IE_NOT_EXIST, sizeof(UINT8)*MAX_IE);

	// Set to zero.
	memset(Header, 0, sizeof(char)*MAX_HEADER_LENGTH);
	LengthOfHeader = 0;
}

// ===========================================================================
//
// Description: Put one IE into the PDU object.
// Input:
//		id		IE ID
//		len		Length of IE data
//		ptr		Pointer point to IE data
// Output: Null
// Return: int
//		>= 0	Success.
//		-1		IE's data invalid.
//		-2		Out of memory.
//
// ===========================================================================
int CPduBase::PutIE(UINT8 id, UINT8 len, char * ptr)	// Put IE into PDU.
{
	int r = 0;

	if ((ptr != NULL)  &&
		(len > 0))
	{
		if (len > SIZE_OF_POINTER)
		{
			IE[IeCounter].ptr = new char[len];

			if (IE[IeCounter].ptr != NULL)
			{
				memcpy(IE[IeCounter].ptr, ptr, len);
			}
			else
			{
				r = -2; // Out of memory.
			}
		}
		else
		{
			memcpy(&IE[IeCounter].data, ptr, len);
		}

		if (0 == r)
		{
			IE[IeCounter].id = id;
			IE[IeCounter].len = len;

			// Create cross index.
			// Index2Ieid[IeCounter] = id;

			if (IE_NOT_EXIST == Ieid2Index[id])
			{
				Ieid2Index[id] = IeCounter;
			}
			else 
			{
				 // This is a repeated IE. If it is a repeated IE, Ieid2Index[xx] always indicate to
				 // the first one.
				 IE[Ieid2Index[id]].ieStatus.repeated ++;
				 IE[IeCounter].ieStatus.repeated = IE[Ieid2Index[id]].ieStatus.repeated;
				 IE[IeCounter].ieStatus.firstone = 1; // Not the first one.
			}

			++IeCounter; // Increase IE counter.
		}
	}
	else
	{
		r = -1; // IE's data invalid.
	}

	return r;
}

// ===========================================================================
//
// Description: Put one IE into the PDU object.
// Input:
//		ie		CIeBase object
// Output: Null
// Return: int
//		>= 0	Success.
//		-1		IE's data invalid.
//		-2		Out of memory.
//
// ===========================================================================
int CPduBase::PutIE(CIeBase& ie)	// Put IE into PDU.
{
	// UINT16 l;

	// return PutIE(ie.id, ie.len, ie.GetIeData(l));
	return PutIE(ie.id, ie.len, ie.ptr);
}

// ===========================================================================
//
// Description: Get the specific IE from PDU object.
// Input:
//		ie		CIeBase object
//		seq		The sequence of (may be) repeated IE, default value equal to zero.
// Output: Null
// Return: int
//		>= 0	Success.
//		-1		IE not exist.
//
// ===========================================================================
int CPduBase::GetIE(CIeBase& ie, UINT8 seq)	// Get IE content from PDU.
{
	int r = 0;
	CIeBase  * pIe;

	pIe = GetIE(ie.id, seq);

	if ((NULL == pIe)
		|| (IE_NOT_EXIST == pIe->id))
	{
		r = -1; // IE not exist.
	}
	else
	{
		ie = *pIe;
	}

	return r;
}

// ===========================================================================
//
// Description: Get the specific IE from PDU object.
// Input:
//		id		IE ID, which IE to be gained.
//		seq		The sequence of (may be) repeated IE, default value equal to zero.
// Output: Null
// Return: CIeBase *
//		NULL							IE not exist.
//		CIeBase->id = IE_NOT_EXIST		IE not exist.
//		Others							Success.
//
// ===========================================================================
CIeBase  * CPduBase::GetIE(UINT8 id, UINT8 seq)	// Get IE content from PDU.
{
	CIeBase  * pIe = NULL;

	UINT8 idx = Ieid2Index[id];

	if (idx < IeCounter)
	{
		if (IE[idx].ieStatus.repeated != 0)
		{
			while ((seq != 0)  &&
				(idx < (IeCounter - 1)))
			{
				++ idx;

				if (IE[idx].id == id)
				{
					-- seq;
				}
			}

			if ((0 == seq)  &&
				(idx < IeCounter))
			{
				pIe = &IE[idx];
			}
		}
		else
		{
			pIe = &IE[idx];
		}
	}
	
	return pIe;
}

UINT8 CPduBase::GetIEindex(UINT8 id, UINT8 seq)
{
	UINT8 idx = Ieid2Index[id];

	if (idx < IeCounter)
	{
		if (IE[idx].ieStatus.repeated != 0)
		{
			while ((seq != 0)  &&
				(idx < (IeCounter - 1)))
			{
				++ idx;

				if (IE[idx].id == id)
				{
					-- seq;
				}
			}

			if ((0 == seq)  &&
				(idx < IeCounter))
			{
				// pIe = &IE[idx];
			}
			else
			{
				idx = IE_NOT_EXIST;
			}
		}
		else
		{
			// pIe = &IE[idx];
		}
	}

	return idx;
}

// ===========================================================================
//
// Description: Delete IE from IE[MAX_IE] by array index.
// Input:
//		idx		Array index.
// Output: Null
// Return: void
//
// ===========================================================================
void CPduBase::DeleteIEbyIndex(UINT8 idx)
{
	// IE[idx].~CIeBase( );
	IE[idx].Release( );

	int i;
	// Move the IE array.
	for (i=idx; i<(IeCounter-1); ++i)
	{
		// IE[i] = IE[i+1];
		IE[i].Copy(IE[i+1]);
	}

	// Clear the last IE.
	IE[i].Init( );
	
	-- IeCounter; // Decrease number of IE.

	// Adjust the Ieid2Index array.
	for (i=idx; i<IeCounter; ++i)
	{
		if (0 == IE[i].ieStatus.firstone)
		{
			-- Ieid2Index[IE[i].id];
		}
	}
}

// ===========================================================================
//
// Description: Delete IE by ID.
// Input:
//		id		IE ID.
//		seq		The sequence of (may be) repeated IE, default value equal to zero.
// Output: Null
// Return: int
//		>= 0	Success.
//		-1		IE not exist.
//		-2		The repeated IE not exist.
//
// ===========================================================================
int CPduBase::DelIE(UINT8 id, UINT8 seq)
{
	int r = 0;
	UINT8 idx = Ieid2Index[id];

	if (idx < IeCounter)
	{
		if (IE[idx].ieStatus.repeated > 0)
		{
			while ((seq != 0)  &&
				(idx < (IeCounter - 1)))
			{
				++ idx;

				if (IE[idx].id == id)
				{
					-- seq;
				}
			}

			if ((0 == seq)  &&
				(idx < IeCounter))
			{
				DeleteIEbyIndex(idx);

				// Adjust the repeated IE's repeated times.
				char first = 0;
				for (int i=0; i<IeCounter; ++i)
				{
					if (IE[i].id == id)
					{
						if (IE[i].ieStatus.repeated > seq)
						{
							-- IE[i].ieStatus.repeated;
						}

						if (0 == first)
						{
							if (IE[i].ieStatus.firstone != 0)
							{
								IE[i].ieStatus.firstone = 0;
								Ieid2Index[id] = i;
							}
						
							first = 1;
						}
					}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?