#include <stdio.h>
#include <stdlib.h>

#include "../src/include/radius_if.h"

#include "/usr/local/include/CUnit/CUnit.h"

int test_radius_ind(WORD *usr_pid, WORD pid, RADIUS_MSG *msg_pdu)
{
	static int				count = 0;
	int						i;
	int						num;
	BYTE					str[64] = "";
	BYTE					tmp[4] = "";
	RADIUS_MSG				pdu;
	RADIUS_ACCESS_REQUEST	*access_request;
	RADIUS_ACCESS_ACCEPT	*access_accept;
	RADIUS_VENDOR_SPECIFIC	*vendor_specific;
	RADIUS_VENDOR_ATTRIBUTE_3GPP2_PPAC		*ppac;
	RADIUS_VENDOR_ATTRIBUTE_3GPP2_PPAQ		*ppaq;
	RADIUS_DISCONNECT_REQUEST				*disconnect_request;
	RADIUS_ADDR								addr;
	RADIUS_ATTRIBUTE_REPLY_MESSAGE			*reply_message;

	*usr_pid = 0;

	if (msg_pdu->msg_type != RADIUS_MSG_TYPE_ACCESS_REQUEST)
	{
		return -1;
	}

	access_request = &msg_pdu->msg.access_request;

	if (access_request->user_password.flag == 1)
	{
		for (i = 0; i < 16; i++)
		{
			sprintf(tmp, "%02x", access_request->user_password.user_password.value[i]);
			strcat(str, tmp);
		}

		printf("user password=%s\r\n", str);
	}

	memset(&pdu, 0, sizeof(RADIUS_MSG));

	pdu.msg_type = RADIUS_MSG_TYPE_ACCESS_ACCEPT;

	access_accept = &pdu.msg.access_accept;

	vendor_specific = &access_accept->vendor_specific;

	num = vendor_specific->num;

	vendor_specific->vendor_specific[num].flag = 1;

	vendor_specific->vendor_specific[num].vendor_type = RADIUS_VENDOR_TYPE_3GPP2_PPAC;

	ppac = &vendor_specific->vendor_specific[num].attribute_3gpp2.ppac;

	ppac->sfs.flag = 1;
	ppac->sfs.sfs = 2;//ppac->sfs.sfs = 1;//

	vendor_specific->num++;

	num = vendor_specific->num;

	vendor_specific->vendor_specific[num].vendor_type = RADIUS_VENDOR_TYPE_3GPP2_PPAQ;

	ppaq = &vendor_specific->vendor_specific[num].attribute_3gpp2.ppaq;

	ppaq->qid.flag = 1;
	ppaq->qid.qid.value = 9;

/*	ppaq->vq.flag = 1;
	ppaq->vq.vq.integer = (count + 1) * 102400;
	ppaq->vq.vq.exponent = 1;

	ppaq->vt.flag = 1;
	ppaq->vt.vt.integer = (count + 1) * 102400 - 10240;
	ppaq->vt.vt.exponent = 1;*/

	ppaq->dq.flag = 1;
	ppaq->dq.dq.value = (count + 1) * 25;

	ppaq->dt.flag = 1;
	ppaq->dt.dt.value = (count + 1) * 25 - 7;

	vendor_specific->num++;

	access_accept->message_authenticator.flag = 1;

	radius_send_access_accept(0, pid, 0, &pdu);

	count++;
	
	if (count == 3)
	{
		memset(&pdu, 0, sizeof(RADIUS_MSG));

		disconnect_request = &pdu.msg.disconnect_request;

		num = disconnect_request->reply_message.num;
		
		reply_message = &disconnect_request->reply_message.reply_message[num];

		reply_message->flag = 1;
		
		strcpy(reply_message->reply_message.value,"Radius disconnect");

		disconnect_request->reply_message.num++;

		addr.addr.ipV4 = inet_addr("155.226.202.45");

		radius_send_disconnect_request(0, 0, &pdu, &addr);
	}

	return 0;
}

int test_radius_cnf(WORD usr_pid, WORD pid, RADIUS_MSG *msg_pdu)
{
	return 0;
}

void test_radius_init()
{
	int							sap_index;
	RADIUS_SAP					sap;
	RADIUS_NAS_INFORMATION		nas;

	strcpy(sap.name, "test_radius");

	sap.radius_ind = test_radius_ind;
	sap.radius_cnf = test_radius_cnf;

	radius_init();

	sap_index = radius_bind(&sap);

	nas.nas_ip = inet_addr("155.226.202.45");
	strcpy(nas.share_key, "topsecret");
	strcpy(nas.password, "test");

	radius_set_nas_info(sap_index, 0, &nas);
}

void test_radius(void)
{
	for(;;)
	{
		radius_timer();

		usleep(1000);
	}
}

