#include "system.h"
#include "app_client.h"
#include "hal_ble_client.h"
#include "hal_ble_host.h"
#include "bsp_time.h"
#include "hal_led.h"
#include "ble_gap.h"
#include "app_host.h"
#include "app_flash.h"
#include "app_client_step.h"
#include "hal_battery.h"
#include "ble_comm.h"
#include "app_game.h"
#include "app_step.h"

void app_client_DataUpdate_Send(void)
{
	//>> 1(设备数据)
	//<< 1(子命令): 左鞋电量(1)+左鞋温度(1)+左鞋压力(4)+左鞋步数(4)+右鞋电量(1)+右鞋温度(1)+右鞋压力(4)+右鞋步数(4)
	uint8_t buf[32];
	uint8_t L=0;
	int32_t temperature;
 
	sd_temp_get(&temperature);
	temperature = temperature>>2; 

	//子命令
	buf[L++] = BLE_UPDATE_DATA;
	//左鞋
	buf[L++] = GetBatteryPersent();
	buf[L++] = (uint8_t)temperature;
  //压力数据
  buf[L++] = 0;
	buf[L++] = 0;
	buf[L++] = 0;
	buf[L++] = 0;
	
	buf[L++] = (uint8_t)(mFlash.mStep.stepCur[0]>>24);
	buf[L++] = (uint8_t)(mFlash.mStep.stepCur[0]>>16);
	buf[L++] = (uint8_t)(mFlash.mStep.stepCur[0]>>8);
	buf[L++] = (uint8_t)(mFlash.mStep.stepCur[0]>>0);
	//右鞋
	buf[L++] = app_host_GetVol_R();
	buf[L++] = app_host_GetTemp_R();
	buf[L++] = (uint8_t)(app_host_GetPress_R()>>24);
	buf[L++] = (uint8_t)(app_host_GetPress_R()>>16);
	buf[L++] = (uint8_t)(app_host_GetPress_R()>>8);
	buf[L++] = (uint8_t)(app_host_GetPress_R()>>0);
	buf[L++] = (uint8_t)(mFlash.mStep.stepCur[1]>>24);
	buf[L++] = (uint8_t)(mFlash.mStep.stepCur[1]>>16);
	buf[L++] = (uint8_t)(mFlash.mStep.stepCur[1]>>8);
	buf[L++] = (uint8_t)(mFlash.mStep.stepCur[1]>>0);
	
	int16_t left_adc = hal_GetBatttery_Adc();
	buf[L++] = (uint8_t)(left_adc>>8);
	buf[L++] = (uint8_t)(left_adc);
	
	buf[L++] = (uint8_t)(app_host_GetVolAdc_R()>>8);
	buf[L++] = (uint8_t)(app_host_GetVolAdc_R());
	
	BLE_Client_Tx_Send(0,BLE_UPDATE,buf,L);
}

void app_client_infomation_Send_defineName(uint8_t name[],uint8_t lenth)
{
	//<< 0(设备基本信息): 设备型号(64)+左鞋蓝牙地址(6)+硬件版本(2)+软件版本(2)+左鞋蓝牙地址(6)+硬件版本(6)+软件版本(2)
	
	uint8_t buf[128];
	uint8_t L = SHOES_NAME_LEN+1;
	
	memset(buf,0,sizeof(buf));
	//子命令
	buf[0] = BLE_UPDATE_BASEINFO;
	//设备名称
	for(int i=0;i<lenth;i++){
		buf[i+1] = name[i];
	}
	//MAC 地址
	Get_MACaddr(&buf[L]);
	//DEBUG_LOG("mac addr:");for(int i=0;i<6;i++){DEBUG_LOG("%02X ",buf[L+i]);}DEBUG_LOG("\n");
	L +=6;
	//硬件版本
	buf[L++] = (uint8_t)((uint32_t)HARDWARE_VERSION>>24);
	buf[L++] = (uint8_t)((uint32_t)HARDWARE_VERSION>>16);
	buf[L++] = (uint8_t)((uint32_t)HARDWARE_VERSION>>8);
	buf[L++] = (uint8_t)((uint32_t)HARDWARE_VERSION>>0);
	//软件版本
	buf[L++] = (uint8_t)(SOFTWARE_VERSION>>8);
	buf[L++] = (uint8_t)(SOFTWARE_VERSION>>0);
	
	//右鞋mac地址
	for(int i=0;i<6;i++) buf[L++] = mFlash.mClient.macAddr[i];
	
	//右鞋硬件版本
	buf[L++] = (uint8_t)((uint32_t)mFlash.mClient.hardVersion>>24);
	buf[L++] = (uint8_t)((uint32_t)mFlash.mClient.hardVersion>>16);
	buf[L++] = (uint8_t)((uint32_t)mFlash.mClient.hardVersion>>8);
	buf[L++] = (uint8_t)((uint32_t)mFlash.mClient.hardVersion>>0);
	//右鞋软件版本
	buf[L++] = (uint8_t)(mFlash.mClient.sotfVersion>>8);
	buf[L++] = (uint8_t)(mFlash.mClient.sotfVersion>>0);
	
	BLE_Client_Tx_Send(0,BLE_UPDATE,buf,L);
}

void app_client_infomation_Send(void)
{
	//<< 0(设备基本信息): 设备型号(64)+左鞋蓝牙地址(6)+硬件版本(2)+软件版本(2)+左鞋蓝牙地址(6)+硬件版本(6)+软件版本(2)
	
	uint8_t buf[128];
	uint8_t L = SHOES_NAME_LEN+1;
	
	memset(buf,0,sizeof(buf));
	//子命令
	buf[0] = BLE_UPDATE_BASEINFO;
	//设备名称
	for(int i=0;i<sizeof(SHOES_NAME);i++){
		buf[i+1] = SHOES_NAME[i];
	}
	//MAC 地址
	Get_MACaddr(&buf[L]);
	//DEBUG_LOG("mac addr:");for(int i=0;i<6;i++){DEBUG_LOG("%02X ",buf[L+i]);}DEBUG_LOG("\n");
	L +=6;
	
	//硬件版本
	buf[L++] = (uint8_t)((uint32_t)HARDWARE_VERSION>>24);
	buf[L++] = (uint8_t)((uint32_t)HARDWARE_VERSION>>16);
	buf[L++] = (uint8_t)((uint32_t)HARDWARE_VERSION>>8);
	buf[L++] = (uint8_t)((uint32_t)HARDWARE_VERSION>>0);
	//软件版本
	buf[L++] = (uint8_t)(SOFTWARE_VERSION>>8);
	buf[L++] = (uint8_t)(SOFTWARE_VERSION>>0);
	
	//右鞋mac地址
	for(int i=0;i<6;i++) buf[L++] = mFlash.mClient.macAddr[i];
//	DEBUG_LOG("mac addr:");for(int i=0;i<6;i++){DEBUG_LOG("%02X ",mFlash.mClient.macAddr[i]);}DEBUG_LOG("\n");
	//右鞋硬件版本
	buf[L++] = (uint8_t)((uint32_t)mFlash.mClient.hardVersion>>24);
	buf[L++] = (uint8_t)((uint32_t)mFlash.mClient.hardVersion>>16);
	buf[L++] = (uint8_t)((uint32_t)mFlash.mClient.hardVersion>>8);
	buf[L++] = (uint8_t)((uint32_t)mFlash.mClient.hardVersion>>0);
	//右鞋软件版本
	//右鞋软件版本
	buf[L++] = (uint8_t)(mFlash.mClient.sotfVersion>>8);
	buf[L++] = (uint8_t)(mFlash.mClient.sotfVersion>>0);
	
	BLE_Client_Tx_Send(0,BLE_UPDATE,buf,L);
}

//>> 0xA1: 查询
void cb_BLE_Client_R_UPDATE(void* handle)
{
	BLE_Client_Rx_t *target = handle;
	uint8_t _cmd = target->pDat[0];
	switch(_cmd){
		case BLE_UPDATE_BASEINFO:{
			//>> 0(设备基本信息): 开始时间ms(8)
			//<< 0(子命令): 设备型号(64)+左鞋蓝牙地址(6)+硬件版本(2)+软件版本(2)+右鞋蓝牙地址(6)+硬件版本(6)+软件版本(2)
			//>> AA 06 F9 A1 00 4A
			//<< AA 55 AA A1 00 53 4D 41 52 54 20 53 48 4F 45 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 C1 EB D0 E3 87 47 01 00 01 00 00 00 00 00 00 4F
			if(1 == mFlash.isHost){
				 app_host_GetClientInfo();
			}
      else app_client_infomation_Send();
//			DEBUG_LOG("!!!!!!!!!!!!!!!=======>app_client_infomation_Send\n");
			break;}
		case BLE_UPDATE_DATA:{
			//>> 1(设备数据)
			//<< 1(子命令): 左鞋电量(1)+左鞋温度(1)+左鞋压力(4)+左鞋步数(4)+右鞋电量(1)+右鞋温度(1)+右鞋压力(4)+右鞋步数(4)
			
			if(1 == mFlash.isHost){
				 app_host_GetClientData(20);
			}
      else app_client_DataUpdate_Send();
			break;}
		case BLE_UPDATE_STEPNUM:{
			//>> 2(查询步数): 包序号[0全部](2)
			//<< 2(查询步数): +开始时间ms(8)+包的数量(2)+包序号(2)+每小时步数(4)+...
			if(!app_step_Real_Get())app_client_SendStep(target);
			break;}
		case BLE_UPDATE_STEPNUM_CLERA:{
			//>> 3(删除步数): 开始时间ms(8)+距离上一个整点的分钟数(1)
			//<< 3(删除步数): 开始时间ms(8)+距离上一个整点的分钟数(1)
			BLE_Client_Tx_Send(0,BLE_UPDATE,target->pDat,target->datLen);
			app_client_DeleteStep(target);
			break;}
		default:break;
	}
}

static void app_client_Process(void)
{
	static uint8_t cnt =0;
	if(mFlash.isHost)return;
	if(slave_isconnect()){//右鞋连接上左鞋
		if(app_game_GetGameMode()){
			 if(cnt++ >= 4){cnt =0;
				  app_client_DataUpdate_Send();
			 }
		}
		else app_client_DataUpdate_Send();
	}
}

void app_client_Initialize(void)
{
	BLE_Client_Rx_Regist(BLE_UPDATE,cb_BLE_Client_R_UPDATE);
	app_client_step_Initialize();
	Process_Start(2000,"app_client",app_client_Process);
}
