/*	HAE public function c file	*/
/*	Written by Liu Zhiguo V1.0	*/
/*	Time: 2003-07-31			*/
/*	--------------------------	*/

#include "./include/hae_include.h"

/*	+++++++++++++++++++++++++++++++++++++++++++++++++	*/
/*	transfer IMSI format from normal to anti sequence	*/
/*	0460022157127001 --> 64002251177200f1				*/
/*	+++++++++++++++++++++++++++++++++++++++++++++++++	*/
void hae_imsi_ntoa(u8 *anti_imsi,u8 *normal_imsi)
{
	u8		ii;
	u8		aa,bb=0;

	for (ii = 1;ii <= IMSI_LEN*2;ii++)
	{
		if ((ii % 2) == 1)
			bb = normal_imsi[ii/2] & 0x0f;
		else
		{
			if (ii == IMSI_LEN * 2)
				aa = 0x0f0;
			else
				aa = normal_imsi[ii/2] & 0x0f0;
			anti_imsi[ii/2-1] = aa | bb;
		}
	}
}

/*	+++++++++++++++++++++++++++++++++++++++++++++++++	*/
/*	transfer IMSI format from anti to normal sequence	*/
/*	64002251177200f1 --> 0460022157127001				*/
/*	+++++++++++++++++++++++++++++++++++++++++++++++++	*/
void hae_imsi_aton(u8 *normal_imsi,u8 *anti_imsi)
{	// the IMSI must 15 digits
	u8		ii;
	u8		aa,bb=0;

	normal_imsi[0] = 0;
	for (ii = 0;ii < IMSI_LEN*2;ii++)
	{
		if ((ii % 2) == 1)
		{
			aa = anti_imsi[ii/2] & 0x0f;
			normal_imsi[ii/2] |= aa;
		}
		else
		{
			if (ii/2 != IMSI_LEN -1)
			{
				bb = anti_imsi[ii/2] & 0x0f0;
				normal_imsi[ii/2+1] = bb;
			}
		}
	}
}

/*	+++++++++++++++++++++++++++++++++++++++++++++++++	*/
/*	transfer IMSI format from normal to ascii string	*/
/*	0460022157127001 --> "460022157127001"				*/
/*	+++++++++++++++++++++++++++++++++++++++++++++++++	*/
void hae_imsi_ntos(u8 *str_imsi,u8 *normal_imsi)
{
	u8		len;
	u8		ii,jj;
	
	str_imsi[0] = (normal_imsi[0] & 0x0f) + '0';
	len = 1;
	for (ii = 1;ii < IMSI_LEN;ii ++)
	{
		jj = (normal_imsi[ii] >> 4) & 0x0f;
		str_imsi[len++] = jj + '0';
		jj = normal_imsi[ii] & 0x0f;
		str_imsi[len++] = jj + '0';
	}
	str_imsi[len] = '\0';
}

/*	+++++++++++++++++++++++++++++++++++++++++++++++++	*/
/*	transfer IMSI format from ascii string to normal	*/
/*	"460022157127001" --> 0460022157127001				*/
/*	+++++++++++++++++++++++++++++++++++++++++++++++++	*/
void hae_imsi_ston(u8 *normal_imsi,u8 *str_imsi)
{
	if (strlen(str_imsi) != IMSI_LEN*2-1)
		return;
	normal_imsi[0] = str_imsi[0] - '0';
	AsciiToBcd(normal_imsi+1,str_imsi+1,IMSI_LEN*2-2);
}

/*	+++++++++++++++++++++++++++++++++++++++++++++++++	*/
/*	transfer ISDN format from normal to anti sequence	*/
/*	918675557127001EEE --> 08916857551700E1				*/
/*	+++++++++++++++++++++++++++++++++++++++++++++++++	*/
void hae_isdn_ntoa(u8 *anti_isdn,u8 *normal_isdn)
{
	u8		ii;
	u8		aa=0,bb=0;
	u8		len;

	anti_isdn[1] = normal_isdn[0];	// nature of address and numbering plan indicator
	len = 1;
	for (ii = 1;ii < ISDN_LEN;ii ++)
	{
		if ((normal_isdn[ii] == 0xee) || (normal_isdn[ii] == 0xff))
			break;
		else
		{
			aa = (normal_isdn[ii] & 0x0f0) >> 4;
			bb = normal_isdn[ii] & 0x0f;
			if (bb > 0x0c)
				bb = 0x0f;
			anti_isdn[ii+1] = (bb << 4) + aa;
			len ++;
		}
	}
	anti_isdn[0] = len;
}

/*	+++++++++++++++++++++++++++++++++++++++++++++++++	*/
/*	transfer ISDN format from anti to normal sequence	*/
/*	08916857551700E1 --> 918675557127001EEE				*/
/*	+++++++++++++++++++++++++++++++++++++++++++++++++	*/
void hae_isdn_aton(u8 *normal_isdn,u8 *anti_isdn)
{
	u8		ii;
	u8		aa=0,bb=0;
	u8		isdn_len;

	isdn_len = anti_isdn[0];
	if (isdn_len > ISDN_LEN)
		isdn_len = ISDN_LEN;
	normal_isdn[0] = anti_isdn[1];
	for (ii = 1;ii < isdn_len;ii ++)
	{
		if ((anti_isdn[ii+1] & 0x0f) >= 0x0e)
			break;
		aa = (anti_isdn[ii+1] & 0x0f0) >> 4;
		if (aa > 0x0c)
			aa = 0x0e;
		bb = anti_isdn[ii+1] & 0x0f;
		normal_isdn[ii] = (bb << 4) + aa;
	}
	for (;ii < ISDN_LEN;ii ++)
		normal_isdn[ii] = 0x0ee;
}

/*	+++++++++++++++++++++++++++++++++++++++++++++++++	*/
/*	transfer ISDN format from normal to ascii string	*/
/*	918675557127001EEE --> "8675557127001"				*/
/*	+++++++++++++++++++++++++++++++++++++++++++++++++	*/
void hae_isdn_ntos(u8 *str_isdn,u8 *normal_isdn)
{
	u8		len = 0;
	u8		ii,jj;

	for (ii = 1;ii < ISDN_LEN;ii ++)
	{
		jj = (normal_isdn[ii] >> 4) & 0x0f;
		if (jj > 0x0c)
			break;
		str_isdn[len++] = jj + '0';
		jj = normal_isdn[ii] & 0x0f;
		if (jj > 0x0c)
			break;
		str_isdn[len++] = jj + '0';
	}
	str_isdn[len] = '\0';
}

/*	+++++++++++++++++++++++++++++++++++++++++++++++++	*/
/*	transfer ISDN format from ascii string to normal	*/
/*	"8675557127001" --> 918675557127001EEE				*/
/*	return value: 0--string has error; 1--success		*/
/*	+++++++++++++++++++++++++++++++++++++++++++++++++	*/
u8 hae_isdn_ston(u8 *normal_isdn,u8 *str_isdn)
{
	u8		ii;
	u8		len;

	len = strlen(str_isdn);
	if (len > ISDN_LEN*2-2)
		return 0;
	if ((len % 2) == 1)		// odd number
	{
		AsciiToBcd(normal_isdn+1,str_isdn,len-1);
		ii = len/2 + 1;
		normal_isdn[ii] = (str_isdn[len-1] - '0') << 4;
		normal_isdn[ii] |= 0x0E;
	}
	else
	{
		AsciiToBcd(normal_isdn+1,str_isdn,len);
		ii = len/2;
	}
	memset(normal_isdn+ii+1,0xEE,ISDN_LEN-ii-1);
	normal_isdn[0] = 0x91;	// default value
	return 1;
}

/*	+++++++++++++++++++++++++++++++++++++++++++++++++	*/
/*	extract MAP primitive parameter						*/
/*	return value: 0--extract fail; 1--extract success	*/
/*	+++++++++++++++++++++++++++++++++++++++++++++++++	*/
u8	hae_extract_param(struct MapOprSrv_struct *srv_ptr,struct MapOprData_struct *data_ptr)
{
	srv_ptr->port_id = data_ptr->port_id;
	srv_ptr->dialogue_id = data_ptr->dialogue_id;
	srv_ptr->invoke_id = data_ptr->invoke_id;
	srv_ptr->message_type = data_ptr->message_type;
	srv_ptr->message_flag = data_ptr->message_flag;
	if (!(data_ptr->param_flag & 0x20))	// has not parameter
		return 0;
	return extract_mapparam(srv_ptr,data_ptr->message_type,data_ptr->message_flag,data_ptr->param_len,data_ptr->param);
}

/*	+++++++++++++++++++++++++++++++++++++++++++++	*/
/*	build MAP primitive parameter					*/
/*	return value: 0--build fail; 1--build success	*/
/*	+++++++++++++++++++++++++++++++++++++++++++++	*/
u8	hae_build_param(struct MapOprData_struct *data_ptr,struct MapOprSrv_struct *srv_ptr)
{
	u32		len;
	u8		buf[4096];

	data_ptr->port_id = srv_ptr->port_id;
	data_ptr->dialogue_id = srv_ptr->dialogue_id;
	data_ptr->invoke_id = srv_ptr->invoke_id;
	data_ptr->message_type = srv_ptr->message_type;
	data_ptr->message_flag = srv_ptr->message_flag;
	data_ptr->param_flag = 0x1f;
	len = build_mapparam(srv_ptr,buf);
	if (len == 0 || len > MAX_MAPPOPR_LEN)
		return 0;
	data_ptr->param_len = len;
	memcpy(data_ptr->param,buf,len);
	data_ptr->param_flag |= 0x20;
	return 1;
}

u8	hae_build_param_v2(struct MapOprData_struct *data_ptr,struct MapOprSrv_struct *srv_ptr, u8 linked_id)
{
	u32		len;
	u8		buf[4096];

	data_ptr->port_id = srv_ptr->port_id;
	data_ptr->dialogue_id = srv_ptr->dialogue_id;
	data_ptr->invoke_id = srv_ptr->invoke_id;
	data_ptr->message_type = srv_ptr->message_type;
	data_ptr->message_flag = srv_ptr->message_flag;
	data_ptr->linked_id = linked_id;
	data_ptr->param_flag = 0x11f;
	len = build_mapparam(srv_ptr,buf);
	if (len == 0 || len > MAX_MAPPOPR_LEN)
		return 0;
	data_ptr->param_len = len;
	memcpy(data_ptr->param,buf,len);
	data_ptr->param_flag |= 0x20;
	return 1;
}

/*	++++++++++++++++++++++++++++++++++++++++++	*/
/*	check if can send MAP common primitive		*/
/*	return value: 0--can not send; 1--can send	*/
/*	++++++++++++++++++++++++++++++++++++++++++	*/
u8	hae_check_sendcom(u32 did)
{
	return map_check_sendcom(did);
}

/*	++++++++++++++++++++++++++++++++++++++++++	*/
/*	check if can send MAP operation primitive	*/
/*	return value: 0--can not send; 1--can send	*/
/*	++++++++++++++++++++++++++++++++++++++++++	*/
u8	hae_check_sendopr(u32 did)
{
	return map_check_sendopr(did);
}

/*	++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++	*/
/*	get open from MAP provider										*/
/*	return value: 0--get fail; 1--get success						*/
/*	data_flow stores the message flow received from MAP provider	*/
/*	++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++	*/
u8 hae_get_open(u8 *data_flow,struct MapComSrv_struct *com_ptr,u8 ssn)
{
	if (!map_get_open(data_flow,ssn))
		return 0;
	if (!map_com_ftos(com_ptr,data_flow))
		return 0;
	return 1;
}

/*	++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++	*/
/*	get common primitive from MAP provider							*/
/*	return value: 0--get fail; 1--get success						*/
/*	data_flow stores the message flow received from MAP provider	*/
/*	++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++	*/
u8	hae_get_comdata(u8 *data_flow,struct MapComSrv_struct *com_ptr,u32 did)
{
	if (!map_get_comdata(data_flow,did))
		return 0;
	if (!map_com_ftos(com_ptr,data_flow))
		return 0;
	return 1;
}

/*	++++++++++++++++++++++++++++++++++++++++++++++++++++++	*/
/*	send common primitive to MAP provider					*/
/*	return value: 0--send fail; 1--send success				*/
/*	data_flow stores the message flow send to MAP provider	*/
/*	++++++++++++++++++++++++++++++++++++++++++++++++++++++	*/
u8	hae_send_comdata(u8 *data_flow,struct MapComSrv_struct *com_ptr,u8 DelimiterFlag)
{
	map_com_stof(com_ptr,data_flow,DelimiterFlag);
	return map_send_comdata(data_flow);
}

/*	+++++++++++++++++++++++++++++++++++++++++++++++++++++++	*/
/*	get operation primitive from MAP provider				*/
/*	return value: 0--get fail; 1--get success				*/
/*	data_flow stores the message flow get from MAP provider	*/
/*	+++++++++++++++++++++++++++++++++++++++++++++++++++++++	*/
u8	hae_get_oprdata(u8 *data_flow,struct MapOprData_struct *opr_ptr,u32 did)
{
	if (!map_get_oprdata(data_flow,did))
		return 0;
	if (!map_opr_ftos(opr_ptr,data_flow))
		return 0;
	return 1;
}

/*	++++++++++++++++++++++++++++++++++++++++++++++++++++++	*/
/*	send operation primitive to MAP provider				*/
/*	return value: 0--send fail; 1--send success				*/
/*	data_flow stores the message flow send to MAP provider	*/
/*	++++++++++++++++++++++++++++++++++++++++++++++++++++++	*/
u8	hae_send_oprdata(u8 *data_flow,struct MapOprData_struct *opr_ptr,u8 DelimiterFlag)
{
	map_opr_stof(opr_ptr,data_flow,DelimiterFlag);
	return map_send_oprdata(data_flow);
}
