/*	map coding auc service file			*/
/*	written by Liu Zhiguo 2002-06-03	*/
/*	Version 2.0							*/
/*	--------------------------------	*/

#include "map_includes.h"

/*	+++++++++++++++++++++++++++++++++++++++++++++	*/
/*	encoding/decoding security triplets argument	*/
/*	+++++++++++++++++++++++++++++++++++++++++++++	*/
int assign_securityTriplets_arg(struct MapST_Arg *ptr,u8 *buf)
{
	ASN_BUF		asn_buf;
	int			buf_len=0;
	u32			flag;
	
	flag = ptr->param_flag;
	asn_encode(buf,&asn_buf);
	if(flag & 0x01)		//imsi
		buf_len = add_tlv("4-1",IMSI_LEN,ptr->imsi,0x00,&asn_buf);
	else
		return RER;
	if(flag & 0x02)		//hlr_id
		buf_len = add_tlv("4-2",HLRID_LEN,ptr->hlr_id,0x00,&asn_buf);			
	return buf_len;	
}

int extract_securityTriplets_arg(struct MapST_Arg *ptr,u32 buf_len,u8 *buf)
{
	ASN_BUF	asn_buf;
	u8		temp_buf[256];
	int 	len;
		
	if (buf_len == 0)
	{
		mapp_log_debug("MAPP--extract_securityTriplets_arg buf_len=0");
		return RER;
	}
	if(asn_decode_v3(buf,buf_len,&decErr,&asn_buf) == -1)
		return RER;
	ptr->param_flag = 0;
	len = get_tlv("4-1",temp_buf,&asn_buf);
	if(len == IMSI_LEN && (temp_buf[IMSI_LEN-1]&0xf0) == 0xf0 )	//imsi
	{
		ptr->param_flag |= 0x01;
		memcpy(ptr->imsi,temp_buf,len);
	}
	if((len=get_tlv("4-2",temp_buf,&asn_buf)) == HLRID_LEN )	//hlr_id
	{
		ptr->param_flag |= 0x02;
		memcpy(ptr->hlr_id,temp_buf,len);
	}			
	return ROK;		
}

/*	+++++++++++++++++++++++++++++++++++++++++++++	*/
/*	encoding/decoding security triplets response	*/
/*	+++++++++++++++++++++++++++++++++++++++++++++	*/
int assign_securityTriplets_res(struct MapST_Res *ptr,u8 *buf)
{
	ASN_BUF		asn_buf;
	int			buf_len=0;
	u32			flag;
	int			ii;
	char		tlv1[32];
	int			temp_len;
	u8			temp_buf[256];
	
	flag = ptr->param_flag;
	asn_encode(buf,&asn_buf);
	if ((flag & 0x01) == 0)
	{
		mapp_log_debug("MAPP--assign_securityTriplets_res flag=0");
		return RER;
	}
	if (ptr->authlist.auth_len > AUTHLIST_LEN)
	{
		mapp_log_debug("MAPP--assign_securityTriplets_res auth_len>AUTHLIST_LEN");
		return RER;
	}
	for (ii = 0;ii < ptr->authlist.auth_len;ii ++)
	{
		temp_len = assign_authset(&ptr->authlist.auth_set[ii],temp_buf);
		if (temp_len == RER)
			break;
		sprintf(tlv1,"16-%d",ii+1);
		buf_len = add_tlv(tlv1,temp_len,temp_buf,0x20,&asn_buf);
	}
	return buf_len;
}

int extract_securityTriplets_res(struct MapST_Res *ptr,u32 buf_len,u8 *buf)
{
	u8			temp_buf[256];
	char		tlv1[32];
	int			ii;
	int			len;
	ASN_BUF		asn_buf;
				
	if (buf_len == 0)
	{
		mapp_log_debug("MAPP--extract_securityTriplets_res buf_len=0");
		return ROK;
	}
	if(asn_decode_v3(buf,buf_len,&decErr,&asn_buf) == -1)
		return RER;	
	ptr->param_flag = 0;
	for(ii = 0;ii < AUTHLIST_LEN;ii ++)
	{
		sprintf(tlv1,"16-%d",ii+1);
		len = get_tlv(tlv1,temp_buf,&asn_buf);
		if (len == -1)
			break;
		if (!extract_authset(&ptr->authlist.auth_set[ii],len,temp_buf))
			break;
		ptr->param_flag |= 0x01;
		ptr->authlist.auth_len ++;
	}
	return ROK;
}

/*	++++++++++++++++++++++++++++++++++++++++++++++++++++	*/
/*	encoding/decoding interrogation subscriber argument		*/
/*	++++++++++++++++++++++++++++++++++++++++++++++++++++	*/
int assign_interrogateSubscriber_arg(struct MapIS_Arg *ptr,u8 *buf)
{
	ASN_BUF		asn_buf;
	int			buf_len=0;
	u32			flag;

	flag = ptr->param_flag;
	asn_encode(buf,&asn_buf);
	if(flag & 0x01)		//imsi
		buf_len = add_tlv("4-1",IMSI_LEN,ptr->imsi,0x00,&asn_buf);
	else
		return RER;
	if(flag & 0x02)		//hlr_id
		buf_len = add_tlv("4-2",HLRID_LEN,ptr->hlr_id,0x00,&asn_buf);
	return buf_len;	
}

int extract_interrogateSubscriber_arg(struct MapIS_Arg *ptr,u32 buf_len,u8 *buf)
{
	ASN_BUF	asn_buf;
	u8		temp_buf[256];
	int 	len;

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

/*	++++++++++++++++++++++++++++++++++++++++++++	*/
/*	encoding/decoding create subscriber argument	*/
/*	++++++++++++++++++++++++++++++++++++++++++++	*/
int assign_createSubscriber_arg(struct MapCS_Arg *ptr,u8 *buf)
{
	ASN_BUF		asn_buf;
	int			buf_len=0;
	u32			flag;

	flag = ptr->param_flag;
	asn_encode(buf,&asn_buf);
	if(flag & 0x01)		//imsi
		buf_len = add_tlv("4-1",IMSI_LEN,ptr->imsi,0x00,&asn_buf);
	else
		return RER;
	if(flag & 0x02)		//hlr_id
		buf_len = add_tlv("4-2",HLRID_LEN,ptr->hlr_id,0x00,&asn_buf);
	return buf_len;
}

int extract_createSubscriber_arg(struct MapCS_Arg *ptr,u32 buf_len,u8 *buf)
{
	ASN_BUF	asn_buf;
	u8		temp_buf[256];
	int 	len;

	if (buf_len == 0)
	{
		mapp_log_debug("MAPP--extract_createSubscriber_arg buf_len=0");
		return RER;
	}
	if(asn_decode_v3(buf,buf_len,&decErr,&asn_buf) == -1)
		return RER;
	ptr->param_flag = 0;
	len = get_tlv("4-1",temp_buf,&asn_buf);
	if(len == IMSI_LEN && (temp_buf[7]&0xf0) == 0xf0)	//imsi
	{
		ptr->param_flag |= 0x01;
		memcpy(ptr->imsi,temp_buf,len);
	}
	if((get_tlv("4-2",temp_buf,&asn_buf))==HLRID_LEN)	//hlr_id
	{
		ptr->param_flag |= 0x02;
		memcpy(ptr->hlr_id,temp_buf,HLRID_LEN);
	}
	return ROK;
}

/*	++++++++++++++++++++++++++++++++++++++++++++	*/
/*	encoding/decoding delete subscriber argument	*/
/*	++++++++++++++++++++++++++++++++++++++++++++	*/
int assign_deleteSubscriber_arg(struct MapDS_Arg *ptr,u8 *buf)
{
	ASN_BUF		asn_buf;
	int			buf_len=0;
	u32			flag;
	
	flag = ptr->param_flag;
	asn_encode(buf,&asn_buf);
	if(flag & 0x01)		//imsi
		buf_len = add_tlv("4-1",IMSI_LEN,ptr->imsi,0x00,&asn_buf);
	else
		return RER;
	if(flag & 0x02)		//hlr_id
		buf_len = add_tlv("4-2",HLRID_LEN,ptr->hlr_id,0x00,&asn_buf);
	return buf_len;
}

int extract_deleteSubscriber_arg(struct MapDS_Arg *ptr,u32 buf_len,u8 *buf)
{
	ASN_BUF	asn_buf;
	u8		temp_buf[256];
	int 	len;

	if (buf_len == 0)
	{
		mapp_log_debug("MAPP--extract_createSubscriber_arg buf_len=0");
		return RER;
	}
	if(asn_decode_v3(buf,buf_len,&decErr,&asn_buf) == -1)
		return RER;
	ptr->param_flag = 0;
	len = get_tlv("4-1",temp_buf,&asn_buf);
	if(len == IMSI_LEN && (temp_buf[7]&0xf0) == 0xf0)	//imsi
	{
		ptr->param_flag |= 0x01;
		memcpy(ptr->imsi,temp_buf,len);
	}
	if((get_tlv("4-2",temp_buf,&asn_buf))==HLRID_LEN)	//hlr_id
	{
		ptr->param_flag |= 0x02;
		memcpy(ptr->hlr_id,temp_buf,HLRID_LEN);
	}
	return ROK;
}
