/*Includes ----------------------------------------------*/
#include "system.h"
#include "ble_comm.h"
#include "bsp_time.h"
#include "hal_ble_host.h"
#include "hal_ble_client.h"
#include "hal_ble_common.h"
#include "hal_ser_imu_mode_manage.h"
#include "app_flash.h"
#include "app_game.h"
#include "app_data_transfer.h"
	
/*Local Functions ------------------------------------------------------------------------------------------------------------------------------------*/
static void app_data_transfer_Process(void)
{
	static uint32_t tim 	= 0;
	static uint32_t	tim2 	= 0;
	static uint8_t	flag 	= 0;
	
	if(hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_GAME) != -1 || \
		 hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_REALSTEP) != -1)
	{
		if(!flag){
			Process_SetHoldOn(app_data_transfer_Process,1);
			flag =1;
		}
		if(TIME_GetTicks()-tim>=500){tim = TIME_GetTicks();
			if( 0 == slave_isconnect()){
				SEGGER_RTT_printf(0,"app_data_transfer_Process advertising_start\r\n");
				advertising_stop();
				advertising_start();
			}
			if(mFlash.isHost && 0 == host_isconnect())scan_start();
		}

		if(TIME_GetTicks()-tim2>=3){tim2 = TIME_GetTicks();	
			IMU_Dtalige();
		}			
	}
	else{
		if(flag){
			Process_SetHoldOn(app_data_transfer_Process,0);
			flag =0;
		}
	}

}


/*API -------------------------------------------------------*/
/**
	@brief 		将旧版本的IMU_SetSlaveData(uint8_t* pdat,uint8_t len)写在这里
	@param 		pdat	略
	@param 		len		略
	@return 	无
*/
void app_data_transfer_set_SlaveData(uint8_t* pdat,uint8_t len)
{
	if(pdat[3]==BLE_TRACK){
		IMU_Rec_data(pdat,len);
		
	}
	app_game_SetClientGameMode();
}
/**
	@brief 		初始化数据传输应用
	@param 		无
	@return 	错误代码					- [out]	-1失败，0成功
*/
int	app_data_transfer_Init(void)
{
	//设置数据传输线程
	Process_Start(0,"app_data_transfer_Process",app_data_transfer_Process);
	
	BLE_Host_Rx_Regist_Game(app_data_transfer_set_SlaveData);
	
	return 0;
}

void send_protocol(uint8_t index,uint8_t cmd,uint8_t* p,uint8_t datLen)
{
	BLE_Client_Tx_Send(0,(BLE_CMD_n)cmd,p,datLen);
}	





