/*******************************************************************************
** Document: xmgr.c
** Copyright (c) 2004 R&D Center, Tailyn Communication Company, Ltd.  
** Created by: Chien Chung Lo
** Date: DEC. 29, 2004
** Description: XDSL manager.
** Version: 0.1
*******************************************************************************/
#include <pthread.h>
#include <unistd.h>
#include <string.h>

#include "msgqueue.h"
#include "umap_struct.h"
#include "umap_enum.h"
#include "umap_enum_mask.h"
#include "umap.h"
#include "umap_cmd.h"
#include "umap_ipc_def.h"

#include "xmgr_proc.h"
#include "xmgr.h"
#include "xmgr_bcm_utils.h"
#include "debug_msg.h"

#include <FW_cardinfo/FW_cardinfo_api.h>

#include "bcm_utils.h"
#include "bcm_globalObject.h"

//----------------------------------------------------------------------
UMAP_S32 qid_XDSLmgr;
UMAP_S32 qid_dispatch;
UMAP_S32 qid_dispatch_ack;

//----------------------------------------------------------------------
#ifdef DEBUG 
void* thread_process_test(void* unused);
#endif  //endif DEBUG

//----------------------------------------------------------------------
void printf_all(UMAP_Msg *pmsg)
{
	MSG("-----------------------------\n");
	MSG("message type:0X%x\n", (UMAP_S32)pmsg->type);
	MSG("InvokeId:0X%x\n", pmsg->data.InvokeId);
	MSG("command id:0X%x\n", pmsg->data.CmdId);
	MSG("Source id:0X%x\n", pmsg->data.Stid);
	MSG("destination id:0X%x\n", pmsg->data.Dtid);
	MSG("result:%x\n", pmsg->data.Result);
	MSG("-----------------------------\n\n\n\n"); 
}

//----------------------------------------------------------------------
void* thread_process_get(void* unused)
{
	UMAP_S32 len;
	UMAP_Msg pmsg, ret_pmsg;

	while(1)
	{	
		len = recv_message(qid_XDSLmgr, MSG_TYPE_GET, &pmsg);
		
		if(len > 0)
		{
			MSG("get\n");
			ProcessMsg(&pmsg, &ret_pmsg);
		}	
		else
		{
			perror("recv_message");
			continue;
		}
		
		if(send_message(qid_dispatch_ack, &ret_pmsg) < 0)
			perror("send_message");	
	}		
	return (void *)0;
}

//----------------------------------------------------------------------
void* thread_process_set(void* unused)
{
	UMAP_S32 len;
	UMAP_Msg pmsg ,ret_pmsg;

	while(1)
	{	
		len = recv_message(qid_XDSLmgr, MSG_TYPE_SET, &pmsg);
		
		if(len > 0)
		{
			MSG("set\n");
			ProcessMsg(&pmsg, &ret_pmsg);
		}	
		else
		{
			perror("recv_message");
			continue;
		}
		
		if(send_message(qid_dispatch_ack, &ret_pmsg) < 0)
			perror("send_message");
	}		
	return (void*)0;
}

//----------------------------------------------------------------------
int main(int argc,char *argv[])
{
	pthread_t thread_id_process_set, thread_id_process_get;
	
	UMAP_S32 return_value;
	
	#ifdef DEBUG 
	pthread_t thread_id_process_test;
	#endif  //endif DEBUG
/*	
	if(argc == 3)
	{
		if(!strcmp(argv[1], "auto"))
		{
			printf("run\n");
			
			auto_run();
		}
	}
*/

	FW_SetAllBusEn(ALL_ENABLE);
	FW_SetBpHostBusControl0(LT_ENABLE);
	FW_SetBpHostBusControl1(LT_ENABLE);
	FW_SetBpHostBusControl2(LT_ENABLE);
	FW_SetBpHostBusControl3(LT_ENABLE);
	FW_SetBpHostBusControl4(LT_ENABLE);

	//--------------------------------
	//first initialise the drivers and the BCM6410 (boot)
	hmiDriverInit();
	initGlobalObject();
	
	auto_run(1);
	//--------------------------------
								
	//--------------------------------
	//creates a message queue for TASK_XDSLMGR.
	delete_msgqueue(create_msgqueue(TASK_XDSLMGR));
	qid_XDSLmgr = create_msgqueue(TASK_XDSLMGR);
	MSG("qid_XDSLmgr=%d\n", qid_XDSLmgr);
	if(qid_XDSLmgr < 0)
	{
		perror("create_msgqueue_cmgr");
		return -1;
	}
	//--------------------------------

	//--------------------------------
	//creates two thread with the get and set message.
	pthread_create(&thread_id_process_set, NULL, &thread_process_set, NULL);
	pthread_create(&thread_id_process_get, NULL, &thread_process_get, NULL);
	//--------------------------------
	
	//--------------------------------
	//creates a thread for testing.
	#ifdef DEBUG 
	pthread_create(&thread_id_process_test, NULL, &thread_process_test, NULL);
	#endif  //endif DEBUG
	
	//--------------------------------
	pthread_join(thread_id_process_set, (void*)&return_value);
	pthread_join(thread_id_process_get, (void*)&return_value);
	
	#ifdef DEBUG 
	pthread_join(thread_id_process_test, (void*)&return_value);
	#endif  //endif DEBUG
	//--------------------------------
	
	MSG("\nthread return value = %d\n", return_value);
	
	return 0;
}
#ifdef DEBUG 
//----------------------------------------------------------------------
void* thread_process_test(void* unused)
{
	UMAP_Msg pmsg, rec_msg;
	UMAP_S32 len;

	pmsg.type = 0;
	pmsg.data.InvokeId = 1;
	
	delete_msgqueue(create_msgqueue(TASK_DISPATCH_ACK));
	qid_dispatch_ack = create_msgqueue(TASK_DISPATCH_ACK);
	pmsg.data.Stid = TASK_DISPATCH_ACK;		
	pmsg.data.Dtid = TASK_XDSLMGR;
	
	if(qid_XDSLmgr < 0)
	{
		perror("create_msgqueue_cmgr");
		return (void*)0;
	}
	
	qid_XDSLmgr = open_msgqueue(pmsg.data.Dtid);
	if(qid_XDSLmgr < 0)
	{
		perror("create_msgqueue_xdsl");
	}
	
	pmsg.data.u.pktAccessCardReset.entPhysicalIndex = 102;

	while(1)
	{	
		sleep(1);

		pmsg.data.CmdId = CmdIdAccessCardResetSet;
		pmsg.type = MSG_TYPE_SET;
		++pmsg.data.InvokeId;
		
		if(send_message(qid_XDSLmgr,&pmsg) < 0)
			perror("test send_message");
		
		len = recv_message(qid_dispatch_ack, MSG_TYPE_ACK, &rec_msg);
		if(len > 0)
		{
			MSG("recevie ack mesage!!\n");
            printf_all(&rec_msg);
		}	
		else
		{
			perror("recv_message");
			continue;
		}	

	}		
	return (void *)0;
}
//----------------------------------------------------------------------
#endif  //endif DEBUG

