/*
* bmc_app application main code.
*/



#include <stdio.h>
#include <stdint.h>
#include <sys/prctl.h>
#include <semaphore.h>
#include <pthread.h>


#include "com_BmcType.h"
#include "com_IPMIDefs.h"
#include "com_Message.h"

#include "BmcType.h"
#include "main.h"
#include "Session.h"

#include "LANIfc.h"
#include "UDSIfc.h"
#include "IPMBIfc.h"
#include "MsgHndlrTask.h"
#include "ChassisTimerTask.h"
#include "TimerTask.h"


/* gloabl varible */
BMCInfo_t 	g_BMCInfo;
pthread_t 	gThreadIDs[256];
uint8_t		gThreadIndex = 0;


void main(void)
{
	PlatformInit();
	Init_IPMI_FRU_SDR_SEL();
	InitSdrConfig();
	InitSelConfig();
	Init_SessionTbl();
	InitTimerTaskTbl();

	printf("\tChassisID %#02x, SlotID %#02x\r\n", g_BMCInfo.ChassisID, g_BMCInfo.SlotID);
	if(g_BMCInfo.IpmiConfig.PrimaryIPMBSupport)
	{
		printf("\tPrimaryIPMBBus: %d, channel %d, Addr %#02x\n", g_BMCInfo.IpmiConfig.PrimaryIPMBBus, 
			PRIMARY_IPMB_CHANNEL, g_BMCInfo.IpmiConfig.PrimaryIPMBAddr);
	}
	if(g_BMCInfo.IpmiConfig.SecondaryIPMBSupport)
	{
		printf("\tSecondaryIPMBBus %d, Channel %d, Addr %#02x\n", g_BMCInfo.IpmiConfig.SecondaryIPMBBus,
			SECONDARY_IPMB_CHANNEL, g_BMCInfo.IpmiConfig.SecondaryIPMBAddr);
	}

	/* Create TimerTask */
	gThreadIndex = 0;
	if(0 != pthread_create(&gThreadIDs[gThreadIndex],NULL,TimerTask,NULL))
    {
        printf("%s: Create TimerTask thread failed!\n", __FUNCTION__);
    }

	/* Create MsgHndlr Task */
	gThreadIndex++;
	if(0 != pthread_create(&gThreadIDs[gThreadIndex],NULL,MsgHndlrTask,NULL))
    {
        printf("%s: Create MsgHndlrTask thread failed!\n", __FUNCTION__);
    }

    /* Create ChassisTimerTask */
	gThreadIndex++;
	if(0 != pthread_create(&gThreadIDs[gThreadIndex],NULL,ChassisTimerTask,NULL))
    {
        printf("%s: Create ChassisTimerTask thread failed!\n", __FUNCTION__);
    }

   /* Create SensorMonitorTask */
	gThreadIndex++;
	if(0 != pthread_create(&gThreadIDs[gThreadIndex],NULL,SensorMonitorTask,NULL))
   {
       printf("%s: Create SensorMonitorTask thread failed!\n", __FUNCTION__);
   }

	/* Create UDS interface */
	gThreadIndex++;
	if(0 != pthread_create(&gThreadIDs[gThreadIndex],NULL,UDSIfcTask,NULL))
    {
        printf("%s: Create UDSIfcTask thread failed!\n", __FUNCTION__);
    }

	/* Create LAN interface */
	gThreadIndex++;
	if(0 != pthread_create(&gThreadIDs[gThreadIndex],NULL,LANIfcTask,NULL))
    {
        printf("%s: Create LANIfcTask thread failed!\n", __FUNCTION__);
    }
//	/* Create IPMB interface */
//	uint8_t primaryIpmbSelect = 0;	//primary
//	gThreadIndex++;
//	if(0 != pthread_create(&gThreadIDs[gThreadIndex],NULL,IPMBIfcTask,&primaryIpmbSelect))
//    {
//        printf("%s: Create LANIfcTask thread failed!\n", __FUNCTION__);
//    }
//    uint8_t secondaryIpmbSelect = 1;	//secondary
//    gThreadIndex++;
//	if(0 != pthread_create(&gThreadIDs[gThreadIndex],NULL,IPMBIfcTask,&secondaryIpmbSelect))
//    {
//        printf("%s: Create LANIfcTask thread failed!\n", __FUNCTION__);
//    }

	/* Create Update FPGA thread */

	/* Create UDS interface */

	/* Create UDS interface */

	/* Create UDS interface */

	UpdateUserInfoTable();
	while(1)
	{
		sleep(100);
		//printf("Hello...\n");
	}
}
