/*	map coding operation & maintenance c file	*/
/*	written by Liu Zhiguo 2003-03-04			*/
/*	Version 2.0									*/
/*	-----------------------------------------	*/

#include "map_includes.h"

/*	++++++++++++++++++++++++++++++++++++++++++++++	*/
/*	encoding/decoding activate trace mode argument	*/
/*  ++++++++++++++++++++++++++++++++++++++++++++++	*/
int assign_acttrcmodearg(struct MapATM_Arg *atm_ptr,u8 *buf)
{
	ASN_BUF		asn_buf;
	int			buf_len=0;

	asn_encode(buf,&asn_buf);
	if (atm_ptr->param_flag & 0x01)		// imsi
		buf_len = add_tlv("0",IMSI_LEN,atm_ptr->imsi,0x80,&asn_buf);
	if (atm_ptr->param_flag & 0x02)		// trace reference
		buf_len = add_tlv("1",2,atm_ptr->trace_ref,0x80,&asn_buf);
	else
		return RER;
	if (atm_ptr->param_flag & 0x04)		// trace type
		buf_len = add_int("2",atm_ptr->trace_type,0x80,&asn_buf);
	else
		return RER;
	if (atm_ptr->param_flag & 0x08)		// omc id
		buf_len = add_tlv("3",atm_ptr->omc_id[0],atm_ptr->omc_id+1,0x80,&asn_buf);
	return buf_len;
}

int extract_acttrcmodearg(struct MapATM_Arg *atm_ptr,u32 buf_len,u8 *buf)
{
	ASN_BUF		asn_buf;
	int 		len;
	u8			temp_buf[256];

	atm_ptr->param_flag = 0;
	if (buf_len == 0)
	{
		mapp_log_debug("MAPP--extract_activate trace mode buf_len=0");
		return RER;
	}
	if (asn_decode_v3(buf,buf_len,&decErr,&asn_buf) == -1)
		return RER;
	len = get_tlv("0",temp_buf,&asn_buf);
	if (len==IMSI_LEN && (temp_buf[IMSI_LEN-1]&0xf0) == 0xf0)	//get imsi
	{
		atm_ptr->param_flag |= 0x01;
		memcpy(atm_ptr->imsi,temp_buf,IMSI_LEN);
	}
	if (get_tlv("1",temp_buf,&asn_buf) == 2)
	{
		atm_ptr->param_flag |= 0x02;
		memcpy(atm_ptr->trace_ref,temp_buf,2);
	}
	else
		return RER;
	if (get_tlv("2",temp_buf,&asn_buf) == 1)
	{
		atm_ptr->param_flag |= 0x04;
		atm_ptr->trace_type = temp_buf[0];
	}
	else
		return RER;
	len = get_tlv("3",temp_buf,&asn_buf);
	if (len > 0 && len <= ISDN_LEN)
	{
		atm_ptr->param_flag |= 0x08;
		atm_ptr->omc_id[0] = len;
		memcpy(atm_ptr->omc_id+1,temp_buf,len);
	}
	return ROK;
}

/*	++++++++++++++++++++++++++++++++++++++++++++++	*/
/*	encoding/decoding activate trace mode response	*/
/*  ++++++++++++++++++++++++++++++++++++++++++++++	*/
int assign_acttrcmoderes(struct MapATM_Res *atm_ptr,u8 *buf)
{
	return 0;
}

int extract_acttrcmoderes(struct MapATM_Res *atm_ptr,u32 buf_len,u8 *buf)
{
	return ROK;
}

/*	++++++++++++++++++++++++++++++++++++++++++++++++	*/
/*	encoding/decoding deactivate trace mode argument	*/
/*  ++++++++++++++++++++++++++++++++++++++++++++++++	*/
int assign_deacttrcmodearg(struct MapDTM_Arg *dtm_ptr,u8 *buf)
{
	ASN_BUF		asn_buf;
	int			buf_len=0;

	asn_encode(buf,&asn_buf);
	if (dtm_ptr->param_flag & 0x01)		// imsi
		buf_len = add_tlv("0",IMSI_LEN,dtm_ptr->imsi,0x80,&asn_buf);
	if (dtm_ptr->param_flag & 0x02)		// trace reference
		buf_len = add_tlv("1",2,dtm_ptr->trace_ref,0x80,&asn_buf);
	else
		return RER;
	return buf_len;
}

int extract_deacttrcmodearg(struct MapDTM_Arg *dtm_ptr,u32 buf_len,u8 *buf)
{
	ASN_BUF		asn_buf;
	int 		len;
	u8			temp_buf[256];

	dtm_ptr->param_flag = 0;
	if (buf_len == 0)
	{
		mapp_log_debug("MAPP--extract_deactivate trace mode buf_len=0");
		return RER;
	}
	if (asn_decode_v3(buf,buf_len,&decErr,&asn_buf) == -1)
		return RER;
	len = get_tlv("0",temp_buf,&asn_buf);
	if (len==IMSI_LEN && (temp_buf[IMSI_LEN-1]&0xf0) == 0xf0)	//get imsi
	{
		dtm_ptr->param_flag |= 0x01;
		memcpy(dtm_ptr->imsi,temp_buf,IMSI_LEN);
	}
	if (get_tlv("1",temp_buf,&asn_buf) == 2)
	{
		dtm_ptr->param_flag |= 0x02;
		memcpy(dtm_ptr->trace_ref,temp_buf,2);
	}
	else
		return RER;
	return ROK;
}

/*	++++++++++++++++++++++++++++++++++++++++++++++++	*/
/*	encoding/decoding deactivate trace mode response	*/
/*  ++++++++++++++++++++++++++++++++++++++++++++++++	*/
int assign_deacttrcmoderes(struct MapDTM_Res *dtm_ptr,u8 *buf)
{
	return 0;
}

int extract_deacttrcmoderes(struct MapDTM_Res *dtm_ptr,u32 buf_len,u8 *buf)
{
	return ROK;
}

/*	++++++++++++++++++++++++++++++++++++	*/
/*	encoding/decoding send IMSI argument	*/
/*  ++++++++++++++++++++++++++++++++++++	*/
int assign_sndimsiarg(struct MapSIMSI_Arg *sim_ptr,u8 *buf)
{
	ASN_BUF		asn_buf;
	int			buf_len=0;

	asn_encode(buf,&asn_buf);
	if (sim_ptr->param_flag & 0x01)		// msisdn
		buf_len = add_tlv("4",sim_ptr->msisdn[0],sim_ptr->msisdn+1,0x00,&asn_buf);
	else
		return RER;
	return buf_len;
}

int extract_sndimsiarg(struct MapSIMSI_Arg *sim_ptr,u32 buf_len,u8 *buf)
{
	ASN_BUF		asn_buf;
	int 		len;
	u8			temp_buf[256];

	sim_ptr->param_flag = 0;
	if (buf_len == 0)
	{
		mapp_log_debug("MAPP--extract_send imsi buf_len=0");
		return RER;
	}
	if (asn_decode_v3(buf,buf_len,&decErr,&asn_buf) == -1)
		return RER;
	len = get_tlv("4",temp_buf,&asn_buf);
	if (len > 0 && len <= ISDN_LEN)
	{
		sim_ptr->param_flag |= 0x01;
		sim_ptr->msisdn[0] = len;
		memcpy(sim_ptr->msisdn+1,temp_buf,len);
	}
	else
		return RER;
	return ROK;
}

/*	++++++++++++++++++++++++++++++++++++	*/
/*	encoding/decoding send IMSI response	*/
/*  ++++++++++++++++++++++++++++++++++++	*/
int assign_sndimsires(struct MapSIMSI_Res *sim_ptr,u8 *buf)
{
	ASN_BUF		asn_buf;
	int			buf_len=0;

	asn_encode(buf,&asn_buf);
	if (sim_ptr->param_flag & 0x01)		// imsi
		buf_len = add_tlv("4",IMSI_LEN,sim_ptr->imsi,0x00,&asn_buf);
	else
		return RER;
	return buf_len;
}

int extract_sndimsires(struct MapSIMSI_Res *sim_ptr,u32 buf_len,u8 *buf)
{
	ASN_BUF		asn_buf;
	int 		len;
	u8			temp_buf[256];

	sim_ptr->param_flag = 0;
	if (buf_len == 0)
	{
		mapp_log_debug("MAPP--extract_send imsi buf_len=0");
		return RER;
	}
	if (asn_decode_v3(buf,buf_len,&decErr,&asn_buf) == -1)
		return RER;
	len = get_tlv("4",temp_buf,&asn_buf);
	if (len == IMSI_LEN && (temp_buf[IMSI_LEN-1]&0xf0) == 0xf0)
	{
		sim_ptr->param_flag |= 0x01;
		memcpy(sim_ptr->imsi,temp_buf,IMSI_LEN);
	}
	else
		return RER;
	return ROK;
}
