#include "usr.h"
#include "app_baseinfo.h"
#include "hal_ble_client.h"
#include "hal_ble_host.h"
#include "bsp_time.h"
#include "hal_led.h"
#include "ble_gap.h"
#include "bsp_battery.h"
#include "drv_ms5611.h"

/*************************************************/
#define SHOES_NAME "SMART SHOE"
#define SHOES_NAME_LEN  64
#define HARDWARE_VERSION	0x0100
#define SOFTWARE_VERSION	0x0100
#define BASEINFOBUFF_LEN  80
static uint8_t baseinfoBuff[128];
static uint8_t basedata[16];

static uint8_t isGetClientInfo = 0;
void Host_GetClientInfo(void)
{
	static uint8_t buf[16];
	buf[0] = BLE_Host_T_UPDATE_BASEINFO;
	BLE_Host_Tx_Send(BLE_Host_T_UPDATE,buf,9,1,0,0,0);
}

void BASEINFO_HostGetClientData(void)
{
	static uint32_t tim=0;
	if(TIME_GetTicks()-tim>=10000){ tim = TIME_GetTicks();
		if(isGetClientInfo==0) Host_GetClientInfo();
	}
}

 //>> 0xA1: 查询
void cb_BLE_Host_R_UPDATE(void* handle)
{
	BLE_Host_Rx_t *target = handle;
	uint8_t _cmd = target->pDat[0];
	switch(_cmd){
		case BLE_Client_R_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
			BLE_Client_Tx_Send(BLE_Client_T_UPDATE,baseinfoBuff,BASEINFOBUFF_LEN,1,0,0,0);
			break;}
		case BLE_Client_R_UPDATE_DATA:{
			//>> 1(设备数据)
			//<< 1(子命令): 左鞋电量(1)+左鞋温度(1)+左鞋压力(4)+右鞋电量(1)+右鞋温度(1)+右鞋压力(4)
			//>> AA 06 F9 A1 01 4B
			//<< AA 12 ED A1 01 00 90 12 34 56 78 00 90 12 34 56 78 93

			break;}
		case BLE_Client_R_UPDATE_STEPNUM:{//>> 2: 查询步数 + 0 + byte[8]: 更新时间ms + byte[1]:距离上一个整点的分钟数 
								//	<< 2 + 0: 开始 + byte[8]: 开始时间ms + byte[1]: 包的数量
								//		<< 2 + byte[1]: 包序号 + byte[4]: 每小时步数（前面两个byte为步数，后面两个byte为距离） + byte[4]: 每小时步数（前面两个byte为步数，后面两个byte为距离） + byte[4]: 每小时步数（前面两个byte为步数，后面两个byte为距离） ..
								//			>> 2 + byte[1]: 包序号(表示收到反馈)
								//  	<< 2 + 2: byte[4]: 每小时步数 + byte[4]: 每小时步数 + byte[4]: 每小时步数 ..
								//			>> 2 + byte[1]: 包序号(表示收到反馈).. 包的数量 == 序号 结束流程
			
			break;}
		default:break;
	}
}

void BASEINFO_SendUpdateData(void)
{
	//>> 1(设备数据)
	//<< 1(子命令): 左鞋电量(1)+左鞋温度(1)+左鞋压力(4)+右鞋电量(1)+右鞋温度(1)+右鞋压力(4)
	//>> AA 06 F9 A1 01 4B
	//<< AA 12 ED A1 01 00 90 12 34 56 78 00 90 12 34 56 78 93
	uint8_t L=0;
	int32_t temperature;
	uint32_t press;
	sd_temp_get(&temperature);
	temperature = temperature>>2; 
	press = MS5611_ReadPressure();
	SEGGER_RTT_printf(0,"temperature=%d\n",temperature);
	SEGGER_RTT_printf(0,"press=%d\n",press);
	temperature = 0x90;
	press = 0x12345678;
	
	//子命令
	basedata[L++] = BLE_Client_T_UPDATE_DATA;
	//左鞋
	basedata[L++] = ADC_GetBatteryPersent();
	basedata[L++] = (uint8_t)temperature;
	basedata[L++] = (uint8_t)(press>>24);
	basedata[L++] = (uint8_t)(press>>16);
	basedata[L++] = (uint8_t)(press>>8);
	basedata[L++] = (uint8_t)(press>>0);
	//右鞋
	basedata[L++] = ADC_GetBatteryPersent();
	basedata[L++] = (uint8_t)temperature;
	basedata[L++] = (uint8_t)(press>>24);
	basedata[L++] = (uint8_t)(press>>16);
	basedata[L++] = (uint8_t)(press>>8);
	basedata[L++] = (uint8_t)(press>>0);
	BLE_Client_Tx_Send(BLE_Client_T_UPDATE,basedata,L,1,0,0,0);
}

 //>> 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_Client_R_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
			BLE_Client_Tx_Send(BLE_Client_T_UPDATE,baseinfoBuff,BASEINFOBUFF_LEN,1,0,0,0);
			break;}
		case BLE_Client_R_UPDATE_DATA:{
			//>> 1(设备数据)
			//<< 1(子命令): 左鞋电量(1)+左鞋温度(1)+左鞋压力(4)+右鞋电量(1)+右鞋温度(1)+右鞋压力(4)
			//>> AA 06 F9 A1 01 4B
			//<< AA 12 ED A1 01 00 90 12 34 56 78 00 90 12 34 56 78 93
			BASEINFO_SendUpdateData();
			break;}
		case BLE_Client_R_UPDATE_STEPNUM:{//>> 2: 查询步数 + 0 + byte[8]: 更新时间ms + byte[1]:距离上一个整点的分钟数 
								//	<< 2 + 0: 开始 + byte[8]: 开始时间ms + byte[1]: 包的数量
								//		<< 2 + byte[1]: 包序号 + byte[4]: 每小时步数（前面两个byte为步数，后面两个byte为距离） + byte[4]: 每小时步数（前面两个byte为步数，后面两个byte为距离） + byte[4]: 每小时步数（前面两个byte为步数，后面两个byte为距离） ..
								//			>> 2 + byte[1]: 包序号(表示收到反馈)
								//  	<< 2 + 2: byte[4]: 每小时步数 + byte[4]: 每小时步数 + byte[4]: 每小时步数 ..
								//			>> 2 + byte[1]: 包序号(表示收到反馈).. 包的数量 == 序号 结束流程
			
			break;}
		default:break;
	}
}

void BASEINFO_Set(void)
{ //<< 0(设备基本信息): 设备型号(64)+左鞋蓝牙地址(6)+硬件版本(2)+软件版本(2)+左鞋蓝牙地址(6)+硬件版本(6)+软件版本(2)
	ble_gap_addr_t mAddr;
	uint32_t err_code;
	uint8_t L = SHOES_NAME_LEN+1;
	memset(baseinfoBuff,0,sizeof(baseinfoBuff));
	//子命令
	baseinfoBuff[0] = BLE_Client_T_UPDATE_BASEINFO;
	//设备名称
	for(int i=0;i<sizeof(SHOES_NAME);i++){
		baseinfoBuff[i+1] = SHOES_NAME[i];
	}
	//MAC 地址
	err_code = sd_ble_gap_addr_get(&mAddr); APP_ERROR_CHECK(err_code);
	SEGGER_RTT_printf(0,"mac addr:");for(int i=0;i<6;i++){SEGGER_RTT_printf(0,"%02X ",mAddr.addr[i]);}SEGGER_RTT_printf(0,"\n");
	for(int i=0;i<6;i++){
		baseinfoBuff[L++] = mAddr.addr[5-i];
	}
	//硬件版本
	baseinfoBuff[L++] = (uint8_t)((uint16_t)HARDWARE_VERSION>>8);
	baseinfoBuff[L++] = (uint8_t)((uint16_t)HARDWARE_VERSION>>0);
	//软件版本
	baseinfoBuff[L++] = (uint8_t)((uint16_t)SOFTWARE_VERSION>>8);
	baseinfoBuff[L++] = (uint8_t)((uint16_t)SOFTWARE_VERSION>>0);
}

void BASEINFO_Initialize(void)
{
	BASEINFO_Set();
	BLE_Client_Rx_Regist(BLE_Client_R_UPDATE,cb_BLE_Client_R_UPDATE);
	BLE_Host_Rx_Regist(BLE_Host_R_UPDATE,cb_BLE_Host_R_UPDATE);
	Process_Regist(BASEINFO_HostGetClientData);
	
}


