#include "app_config.h"
#include "nrf_gpio.h"
#include "usr_config.h"
#include "bsp_time.h"
#include "system.h"
#include "hal_mt.h"
#include "hal_battery.h"
#include "app_charge.h"
#include "hal_ble_client.h"
#include "hal_led.h"
#include "hal_flash.h"
#include "ble_gap.h"
#include "hal_ble_uart0.h"
#include "ble_comm.h"

static ble_gap_addr_t mAddr;
static uint8_t isConfig = 0;

void UART0_Config_Send(uint8_t cmd)
{
	uint8_t buf[32];
	uint8_t L = 0;
	//MAC 地址
	for(int i=0;i<6;i++){
		buf[L++] = mAddr.addr[5-i];
	}
	//硬件版本
	buf[L++] = (uint8_t)((uint16_t)HARDWARE_VERSION>>8);
	buf[L++] = (uint8_t)((uint16_t)HARDWARE_VERSION>>0);
	//软件版本
	buf[L++] = (uint8_t)((uint16_t)SOFTWARE_VERSION>>8);
	buf[L++] = (uint8_t)((uint16_t)SOFTWARE_VERSION>>0);
	UART0_Tx_Send(0,cmd,buf,L);
}

void app_config_Process(void)
{
	static uint8_t flag = 0;
	if(app_charge_Getstate()!=BLE_Client_T_CHARGE_PULLOUT){
		if(flag != 1)
		{
//			UART0_Initialize();
			flag = 1;
		}
		else if(isConfig<50){ isConfig++;
			if(mFlash.isHost){ //充电模式下主机主机申请配对
//				UART0_Config_Send(UART0_T_CONFIG_ASK);
			}
		}
	}else{
		if(flag != 2)
		{
//			UART0_unInit();
			flag = 2;
		}
		else if(isConfig>0) isConfig = 0;
	}
}

void app_config_LED(void)
{
	static uint8_t state = 0;
	switch(state){
		case 0:
			Process_SetHoldOn(app_config_LED,1);
			LED_SetColor(LED_CONFIG,COLOR_BLUE);
			LED_Start(LED_CONFIG);
			state = 1;
			Process_UpdatePeroid(app_config_LED,2000);
			break;
		case 1:
			Process_UpdatePeroid(app_config_LED,0);
			LED_Stop(LED_CONFIG);
			state = 0;
			Process_SetHoldOn(app_config_LED,0);
			Process_Stop(app_config_LED);
			break;
		default:state=0;Process_UpdatePeroid(app_config_LED,0);break;
	}
	
}

void cb_UART0_R_CONFIG_ASK(void* handle)
{	//从机接收到配对申请
	UART0_Rx_t* target = handle;
	uint8_t mac[6];
	char buf[16];
	uint8_t ret = 0;
	
	if(app_charge_Getstate()==BLE_Client_T_CHARGE_PULLOUT) return; //非充电模式不匹配
	if(mFlash.isHost) return; //主机自发自收
	
	for(int i=0;i<6;i++) mac[i] = target->pDat[i];
	uint16_t hv = ((uint16_t)target->pDat[6]<<8)| ((uint16_t)target->pDat[7]<<0);
	uint16_t sv = ((uint16_t)target->pDat[8]<<8)| ((uint16_t)target->pDat[9]<<0);

	if(slave_isconnect()) slave_disconnect();
	else advertising_stop();
	
	UART0_Config_Send(UART0_T_CONFIG_ACK);//应答主机申请
	
	mFlash.mClient.isConfig = 'C';
	if(mBackup.isConfig != mFlash.mClient.isConfig)ret = 1;
	for(int i=0;i<6;i++){
		mFlash.mClient.macAddr[i] = mAddr.addr[5-i];	//从机自身mac地址
		mFlash.macHost[i] = mac[i];						//主机发过来的mac地址
		if(mBackup.macAddr_L[i]!=mFlash.macHost[i]) ret = 1;
		if(mBackup.macAddr_R[i]!=mFlash.mClient.macAddr[i]) ret = 1;
	}
	mFlash.mClient.hardVersion = hv;
	mFlash.mClient.sotfVersion = sv;
	if(	mBackup.hardVersion != mFlash.mClient.hardVersion) ret = 1;
	if( mBackup.sotfVersion != mFlash.mClient.sotfVersion) ret = 1;
	DEBUG_LOG("mFlash.mClient.hardVersion:%X\n",mFlash.mClient.hardVersion);
	DEBUG_LOG("mFlash.mClient.sotfVersion:%X\n",mFlash.mClient.sotfVersion);
	DEBUG_LOG("mFlash.mClient.macAddr:%02X %02X %02X %02X %02X %02X\n",mFlash.mClient.macAddr[0],mFlash.mClient.macAddr[1],mFlash.mClient.macAddr[2],mFlash.mClient.macAddr[3],mFlash.mClient.macAddr[4],mFlash.mClient.macAddr[5]);
	DEBUG_LOG("mFlash.macHost:%02X %02X %02X %02X %02X %02X\n",mFlash.macHost[0],mFlash.macHost[1],mFlash.macHost[2],mFlash.macHost[3],mFlash.macHost[4],mFlash.macHost[5]);
	Process_Start(0,"config_LED",app_config_LED);
	
	memset(buf,0,16);
	sprintf(buf,"%02X%02X%02X%02X%02X%02X",mFlash.macHost[0],mFlash.macHost[1],mFlash.macHost[2],mFlash.mClient.macAddr[3],mFlash.mClient.macAddr[4],mFlash.mClient.macAddr[5]);
	DEBUG_LOG("advName(%d):%s\n",strlen(buf),buf);
	slave_set_adv_name(buf,strlen(buf));
	slave_adv_init();
	if(ret >=1){
		
		for(int i=0;i<6;i++){
				mBackup.macAddr_L[i] = mFlash.macHost[i];		     //主机地址
				mBackup.macAddr_R[i] = mFlash.mClient.macAddr[i];//从机地址	
		}
		mBackup.hardVersion = mFlash.mClient.hardVersion;
		mBackup.sotfVersion = mFlash.mClient.sotfVersion;
		mBackup.isConfig = mFlash.mClient.isConfig;
		//DEBUG_LOG("AAAAAbackup config:%d,hardVersion:%d,sotfVersion:%d\n",mBackup.isConfig,mBackup.hardVersion,mBackup.sotfVersion);
		Flash_SaveInfomation();
		Flash_SaveBackup();
	}
	
	while(slave_isconnect());
	advertising_start();
//	nrf_gpio_cfg_output(PIN_LED_RUN); 	
//	nrf_gpio_pin_write(PIN_LED_RUN,0);
	
}

void cb_UART0_R_CONFIG_ACK(void* handle)
{	//主机接收到配对应答
	UART0_Rx_t* target = handle;
	uint8_t mac[6];
	char buf[16];
	uint8_t ret = 0;
	
	if(app_charge_Getstate()==BLE_Client_T_CHARGE_PULLOUT) return; //非充电模式不匹配
	if(!mFlash.isHost) return; //从机自发自收
	
	for(int i=0;i<6;i++) mac[i] = target->pDat[i];
	uint16_t hv = ((uint16_t)target->pDat[6]<<8)| ((uint16_t)target->pDat[7]<<0);
	uint16_t sv = ((uint16_t)target->pDat[8]<<8)| ((uint16_t)target->pDat[9]<<0);
	
	isConfig = 100;	//已配对,停止配对发送
	
	mFlash.mClient.isConfig = 'C';
	if(mBackup.isConfig != mFlash.mClient.isConfig)ret = 1;
	for(int i=0;i<6;i++){
		mFlash.mClient.macAddr[i] = mac[i]; 	//收到的从机mac地址
		mFlash.macHost[i] = mAddr.addr[5-i];	//主机本身mac地址
		if(mBackup.macAddr_L[i]!=mFlash.macHost[i]) ret = 1;
		if(mBackup.macAddr_R[i]!=mFlash.mClient.macAddr[i]) ret = 1;
	}
	mFlash.mClient.hardVersion = hv;
	mFlash.mClient.sotfVersion = sv;
	if(	mBackup.hardVersion != mFlash.mClient.hardVersion) ret = 1;
	if( mBackup.sotfVersion != mFlash.mClient.sotfVersion) ret = 1;
	DEBUG_LOG("mFlash.mClient.hardVersion:%X\n",mFlash.mClient.hardVersion);
	DEBUG_LOG("mFlash.mClient.sotfVersion:%X\n",mFlash.mClient.sotfVersion);
	DEBUG_LOG("mFlash.mClient.macAddr:%02X %02X %02X %02X %02X %02X\n",mFlash.mClient.macAddr[0],mFlash.mClient.macAddr[1],mFlash.mClient.macAddr[2],mFlash.mClient.macAddr[3],mFlash.mClient.macAddr[4],mFlash.mClient.macAddr[5]);
	DEBUG_LOG("mFlash.macHost:%02X %02X %02X %02X %02X %02X\n",mFlash.macHost[0],mFlash.macHost[1],mFlash.macHost[2],mFlash.macHost[3],mFlash.macHost[4],mFlash.macHost[5]);
	Process_Start(0,"config_LED",app_config_LED);
	
	memset(buf,0,16);
	sprintf(buf,"%02X%02X%02X%02X%02X%02X",mFlash.macHost[0],mFlash.macHost[1],mFlash.macHost[2],mFlash.mClient.macAddr[3],mFlash.mClient.macAddr[4],mFlash.mClient.macAddr[5]);
	DEBUG_LOG("scanName(%d):%s\n",strlen(buf),buf);
	host_set_scan_name(buf,strlen(buf));
	
	if(ret >=1){
		for(int i=0;i<6;i++){
			mBackup.macAddr_L[i] = mFlash.macHost[i];		//主机地址
			mBackup.macAddr_R[i] = mFlash.mClient.macAddr[i];//从机地址
		}
		mBackup.hardVersion = mFlash.mClient.hardVersion;
		mBackup.sotfVersion = mFlash.mClient.sotfVersion;
		mBackup.isConfig = mFlash.mClient.isConfig;
		//DEBUG_LOG("AAAAAbackup config:%d,hardVersion:%d,sotfVersion:%d\n",mBackup.isConfig,mBackup.hardVersion,mBackup.sotfVersion);
		Flash_SaveInfomation();
		Flash_SaveBackup();
	}
	//nrf_gpio_cfg_output(PIN_LED_RUN); 	nrf_gpio_pin_write(PIN_LED_RUN,0);
}

void app_config_Init(void)
{
	uint32_t err_code = sd_ble_gap_addr_get(&mAddr); APP_ERROR_CHECK(err_code);
	Process_Start(100,"app_config",app_config_Process);
	UART0_Rx_Regist(UART0_R_CONFIG_ASK,cb_UART0_R_CONFIG_ASK);
	UART0_Rx_Regist(UART0_R_CONFIG_ACK,cb_UART0_R_CONFIG_ACK);
}


