/**
  ******************************************************************************
  * @file    BLV_485_Dev_BLE_Music.c
  * @author  BLW RCU  development Team
  * @version 1.0
  * @date    2024年11月23日
  * @brief   MP2411音频播放驱动
  ******************************************************************************
  */
#include "includes.h"


/*函数声明*/
uint8_t BLV_Rs485_BLE_Music_CycleDis(uint32_t DevAdd);
uint8_t BLV_Rs485_BLE_Music_Check(uint32_t DevAdd ,uint32_t Data_addr, uint16_t DataLen);
uint8_t Dev_BLE_Music_InType_Get(uint32_t DevAddr, uint16_t DevInputLoop, uint16_t DevInputType);
void Dev_BLE_Music_Output_Ctrl(uint32_t CfgDevAddIn, uint16_t DevInputAddr, uint32_t DevAddr, uint16_t DevOutputLoop, uint16_t DevOutputType);
uint16_t Dev_BLE_Music_Loop_State(uint32_t devaddr, uint16_t DevOutputLoop);

void BLE_Query_Status(Device_Public_Information_G *BUS_Public,BLE_MUSIC_INFO *BleMusicInfo);
void BLE_Set_Name(Device_Public_Information_G *BUS_Public,BLE_MUSIC_INFO *BleMusicInfo);
void BLE_Set_PlayStatus(Device_Public_Information_G *BUS_Public,BLE_MUSIC_INFO *BleMusicInfo);
void BLE_Set_Volume(Device_Public_Information_G *BUS_Public,BLE_MUSIC_INFO *BleMusicInfo);
void BLE_Query_VerInf(Device_Public_Information_G *BUS_Public,BLE_MUSIC_INFO *BleMusicInfo);
void BLE_Event_Ack(Device_Public_Information_G *BUS_Public,BLE_MUSIC_INFO *BleMusicInfo);
void BLE_ReFresh_PassWord(Device_Public_Information_G *BUS_Public,BLE_MUSIC_INFO *BleMusicInfo);



void BLV_RS485_BLE_Music_For_Logic_Init(LOGICFILE_DEVICE_INFO *dev_info,uint8_t *data,uint16_t data_len)
{
	uint8_t roomnumber[50];
	
	Device_Public_Information_G BUS_Public;
	BLE_MUSIC_INFO BleMusicInfo;       
	
	memset(&BUS_Public,0,sizeof(Device_Public_Information_G));
	memset(&BleMusicInfo,0,sizeof(BLE_MUSIC_INFO));
	
	BUS_Public.addr = dev_info->addr;			        //设备地址
	BUS_Public.type = dev_info->type;			        //设备类型
	BUS_Public.baud = dev_info->baud;		            //设备发送波特率
	BUS_Public.retry_num = 0x03;//dev_info->retry;		        //设备数据重发次数
	BUS_Public.wait_time = 200;//dev_info->writ_time;		    //设备数据发送等待回复时间 
	
	BUS_Public.polling_cf = (uint32_t)&BLV_Rs485_BLE_Music_CycleDis;
	BUS_Public.processing_cf = (uint32_t)&BLV_Rs485_BLE_Music_Check;
	
	BUS_Public.Protocol = dev_info->version;
	
	BUS_Public.DevFunInfo.Dev_Input_Type_Get = Dev_BLE_Music_InType_Get;
	BUS_Public.DevFunInfo.Dev_Output_Ctrl = Dev_BLE_Music_Output_Ctrl;
	BUS_Public.DevFunInfo.Dev_Output_Loop_State_Get = Dev_BLE_Music_Loop_State;
	
	BleMusicInfo.InputValidNum = dev_info->input_num;
	BleMusicInfo.OutputValidNum = dev_info->output_num;		
	BleMusicInfo.DevOffline = BleMusicInfo.DevOfflineLast = DEV_IS_LINEUNINIT;   //在线状态初始化
	
	BleMusicInfo.SetName_Flag = 0x01;
	memcpy((uint8_t *)&BleMusicInfo.Ble_Name_Prefixes,dev_info->priproperty,10);
	
	Dbg_Print_Buff(DBG_BIT_DEVICE_STATUS_bit,"BLE_Music Prefixes:",BleMusicInfo.Ble_Name_Prefixes,10);
	
	SRAM_Read_Buff(roomnumber,16,SRAM_Register_Start_ADDRESS + Register_RoomNumNote_OFFSET);
	snprintf((char*)BleMusicInfo.Ble_Name,sizeof(BleMusicInfo.Ble_Name),"%s%c%s",BleMusicInfo.Ble_Name_Prefixes,'-',roomnumber);
	
	Dbg_Print_Buff(DBG_BIT_DEVICE_STATUS_bit,"BLE_Music Name:",BleMusicInfo.Ble_Name,32);
	
	BleMusicInfo.RState_Flag = 0x01;
	
	BleMusicInfo.HSPlayTime = 600;
	BleMusicInfo.HSPlayStartVol = 80;
	BleMusicInfo.HSPlayEndVol = 10;
	
	BleMusicInfo.CurePlayTime = 600;
	BleMusicInfo.CurePlayStartVol = 80;
	BleMusicInfo.CurePlayEndVol = 10;
	
	switch(dev_info->port)
	{
		case Active_Port:   //主动端口
			BUS_Public.port = Active_Port;		//设备发送端口
			Add_ACT_Device_To_List2(&BUS_Public,(uint8_t *)&BleMusicInfo,sizeof(BLE_MUSIC_INFO));
			Act485_Info.device_num += 1;
			break;  
		case Polling_Port:   //轮询端口
			BUS_Public.port = Polling_Port;		//设备发送端口
			Add_POLL_Device_To_List2(&BUS_Public,(uint8_t *)&BleMusicInfo,sizeof(BLE_MUSIC_INFO));   //将填充后的数据添加到链表
			Poll485_Info.device_num += 1;	
			break;
		case Bus_port:    //总线端口
			break;
	}
}



uint8_t BLV_Rs485_BLE_Music_CycleDis(uint32_t DevAdd)
{
	Device_Public_Information_G BUS_Public;
	BLE_MUSIC_INFO BleMusicInfo; 

	uint8_t Ret = RS485OCCUPYNOTIME;
	uint8_t keep_flag = 0x00;
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),DevAdd);			
	SRAM_DMA_Read_Buff((uint8_t *)&BleMusicInfo,sizeof(BLE_MUSIC_INFO),DevAdd+Dev_Privately);/*拷贝设备私有信息*/
	
	
	if(BleMusicInfo.RState_Flag != 0x00)                    //查询状态
	{
		BLE_Query_Status(&BUS_Public,&BleMusicInfo);
		Ret = RS485OCCUPYTIME;
	}
	else if(BleMusicInfo.SetName_Flag != 0x00)              //设置名称
	{
		BLE_Set_Name(&BUS_Public,&BleMusicInfo);
		Ret = RS485OCCUPYTIME;
	}
	else if(BleMusicInfo.PlayCtrl_Flag != 0x00)            //指定播放状态
	{
		BLE_Set_PlayStatus(&BUS_Public,&BleMusicInfo);
		Ret = RS485OCCUPYTIME;
	}
	else if(BleMusicInfo.SetVol_Flag != 0x00)              //设置音量
	{
		BLE_Set_Volume(&BUS_Public,&BleMusicInfo);
		Ret = RS485OCCUPYTIME;
	}
	else if(BleMusicInfo.ReadVer_Flag != 0x00)             //读取设备信息和软件版本号
	{
		BLE_Query_VerInf(&BUS_Public,&BleMusicInfo);
		Ret = RS485OCCUPYTIME;
	}
	else if(BleMusicInfo.PSRef_Flag == 0x01)               //更新配对密码
	{
		BLE_ReFresh_PassWord(&BUS_Public,&BleMusicInfo);
		Ret = RS485OCCUPYTIME;
	}
	
	
	if((Ret == RS485OCCUPYTIME) || (keep_flag == 0x01))
	{
		if(BleMusicInfo.DevSendCnt >= BUS_Public.retry_num)
		{
			if(BleMusicInfo.DevOffline != DEV_IS_OFFLINE)
			{
				Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"%s Device_Offline",__func__);
				LOG_Device_Online_Record(Dev_Rs458_RotaryCtrl, BUS_Public.addr,LogInfo_Device_Offline);	//记录设备离线
			}

			BleMusicInfo.DevOffline = DEV_IS_OFFLINE;   //判为离线
			if(BleMusicInfo.DevOffline != BleMusicInfo.DevOfflineLast)	//当前在线状态不等于上一次在线状态
			{
				BleMusicInfo.DevOfflineLast = BleMusicInfo.DevOffline;	//更新上一次在线状态
				Write_Device_Fault_State(BUS_Public.type,BUS_Public.addr,In_ErrFun_LineState,DEV_IS_OFFLINE);	//保存设备在线状态到SRAM
			}
			
			/*重发次数达到清发送标记开始*/
			if(BleMusicInfo.RState_Flag != 0x00)                    //查询状态
			{
				BleMusicInfo.RState_Flag = 0x00;
			}
			else if(BleMusicInfo.SetName_Flag != 0x00)              //设置音量
			{
				BleMusicInfo.SetName_Flag = 0x00;
			}
			else if(BleMusicInfo.PlayCtrl_Flag != 0x00)            //指定播放状态
			{
				BleMusicInfo.PlayCtrl_Flag = 0x00;
			}
			else if(BleMusicInfo.SetVol_Flag != 0x00)              //设置音量
			{
				BleMusicInfo.SetVol_Flag = 0x00;
			}
			else if(BleMusicInfo.ReadVer_Flag != 0x00)             //读取设备信息和软件版本号
			{
				BleMusicInfo.ReadVer_Flag = 0x00;
			}
			else if(BleMusicInfo.PSRef_Flag == 0x01)               //更新配对密码
			{
				BleMusicInfo.PSRef_Flag = 0x00;
			}
			/*重发次数达到清发送标记结束*/
			
			BleMusicInfo.DevSendCnt = 0x00;
		}
		else
		{
			BleMusicInfo.DevSendCnt++;   //发送次数累计
		}
		
		/*通讯统计记录*/
		BLV_Communication_Record(&BleMusicInfo.comm_record,0x01,0x00);
		
		BUS_Public.check = 0x00;
		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&BleMusicInfo, sizeof(BLE_MUSIC_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),DevAdd);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&BleMusicInfo,sizeof(BLE_MUSIC_INFO),DevAdd+Dev_Privately);
	}
	
	return Ret;
}


uint8_t BLV_Rs485_BLE_Music_Check(uint32_t DevAdd ,uint32_t Data_addr, uint16_t DataLen) 
{
	uint8_t ret = 0x01;
	uint8_t data[55] = {0};
	
	Device_Public_Information_G BUS_Public;
	BLE_MUSIC_INFO BleMusicInfo; 
	
	if((DataLen < 0x08) || (DataLen > 52))
	{
		return ret;
	}
	
	SRAM_DMA_Read_Buff(data,DataLen,Data_addr);   //拷贝482数据
	
	
	if((data[0] != 0xA5) || (data[DataLen - 1] != 0x5A))
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"Head Err Not BLE Music Data");
		return ret;
	}
	
	if(data[3] != DataLen)
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"BLE Music Data Lens Err");
		return ret;
	}
	
	if(0x00 != CheckSum(data,DataLen))
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"BLE Music Data CheckSum Err");
		return ret;
	}
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),DevAdd);
	
	if(data[2] != BUS_Public.addr)
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"BLE Music Data Addr Err");
		return ret;
	}
	
	
	
	SRAM_DMA_Read_Buff((uint8_t *)&BleMusicInfo,sizeof(BLE_MUSIC_INFO),DevAdd+Dev_Privately);/*拷贝设备私有信息*/
	
	
	switch(data[5])
	{
		case BLE_CMD_QUERY:
		case BLE_CMD_SETNAME:
		case BLE_CMD_PLAY:
		case BLE_CMD_VOLUME:
		case BLE_CMD_VERINF:
		case BLE_CMD_PLAY_END:
		case BLE_CMD_UPDATE_PASSWORD:
			ret = 0x00;
			if(BleMusicInfo.DevOffline != DEV_IS_ONLINE)
			{
				Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"BLE Music DEV_IS_ONLINE");
				LOG_Device_Online_Record(Dev_485_BLE_Music,BUS_Public.addr,LogInfo_Device_Online);	//记录设备在线
			}
			BleMusicInfo.DevSendCnt = 0x00;
			BleMusicInfo.DevOffline = DEV_IS_ONLINE;    //设备在线
			if(BleMusicInfo.DevOffline != BleMusicInfo.DevOfflineLast)	//当前在线状态不等于上一次在线状态
			{
				BleMusicInfo.DevOfflineLast = BleMusicInfo.DevOffline;	//更新上一次在线状态
				Write_Device_Fault_State(BUS_Public.type,BUS_Public.addr,In_ErrFun_LineState,DEV_IS_ONLINE);	//保存设备在线状态到SRAM
			}
			BLV_Communication_Record(&BleMusicInfo.comm_record,0x02,0x01);		//记录通讯成功
			break;
	}
	
	switch(data[5])
	{
		case BLE_CMD_QUERY:
			BleMusicInfo.RState_Flag = 0x00;
			BleMusicInfo.BleCon_State = data[PKG_PARA];
		
			BleMusicInfo.Play_Now_State = data[PKG_PARA + 1];
			BleMusicInfo.Play_Now_Folder = data[PKG_PARA + 2];
			BleMusicInfo.Volume_Now = data[PKG_PARA + 3];
			if(BleMusicInfo.Volume_Globle != data[PKG_PARA + 4])
			{
				BleMusicInfo.Volume_Globle = data[PKG_PARA + 4];
				
			}
			break;
		case BLE_CMD_SETNAME:
			BleMusicInfo.SetName_Flag = 0x00;
			break;
	    case BLE_CMD_PLAY:
			BleMusicInfo.PlayCtrl_Flag = 0x00;
			break;
		case BLE_CMD_VOLUME:
			BleMusicInfo.SetVol_Flag = 0x00;
			break;
		case BLE_CMD_VERINF:
			BleMusicInfo.ReadVer_Flag = 0x00;
			if(memcmp(&BleMusicInfo.BLE_MAC[0],&data[1],12) != 0x00)
			{
				memcpy(&BleMusicInfo.BLE_MAC[0],&data[1],12);
			}
			
			if(memcmp(&BleMusicInfo.Ble_Name[0],&data[13],32) != 0x00)
			{
				memcpy(&BleMusicInfo.Ble_Name[0],&data[13],32);
			}
			break;
		case BLE_CMD_PLAY_END:
			BleMusicInfo.FolderPlayEnd &= 0xff00;
			BleMusicInfo.FolderPlayEnd = data[PKG_PARA];
			BLE_Event_Ack(&BUS_Public,&BleMusicInfo);
			break;
		case BLE_CMD_UPDATE_PASSWORD:
			BleMusicInfo.PSRef_Flag = 0x00;
			break;
	}
	
	BUS_Public.check = 0x00;
	BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&BleMusicInfo, sizeof(BLE_MUSIC_INFO));
	SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),DevAdd);/*将数据保存*/
	SRAM_DMA_Write_Buff((uint8_t *)&BleMusicInfo,sizeof(BLE_MUSIC_INFO),DevAdd+Dev_Privately);
	
	return ret;
}

uint8_t Dev_BLE_Music_InType_Get(uint32_t DevAddr, uint16_t DevInputLoop, uint16_t DevInputType)
{
	uint8_t ret = CtrlInvalid;         //控制无效
	
	return ret;
	
}

void Dev_BLE_Music_Output_Ctrl(uint32_t CfgDevAddIn, uint16_t DevInputAddr, uint32_t DevAddr, uint16_t DevOutputLoop, uint16_t DevOutputType)
{
	Device_Public_Information_G BUS_Public;
	BLE_MUSIC_INFO BleMusicInfo; 
	
//	uint8_t keepflag = 0x00;
	
	uint8_t ctrlmode = 0x00;
	uint8_t ctrlstate = 0x00;
	
	if(DevAddr == 0x00)	return;
	
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),DevAddr);	//拷贝所有共有数据	
	SRAM_DMA_Read_Buff((uint8_t *)&BleMusicInfo,sizeof(BLE_MUSIC_INFO),DevAddr+Dev_Privately);/*拷贝设备私有信息*/
	
	ctrlmode = DevOutputType & 0xff;
	ctrlstate = (DevOutputType >> 8) & 0xff;
	
	Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"BLE Music Ctrl Mode:%02X  State:%02X",ctrlmode,ctrlstate);
	
	switch(ctrlmode & 0x0f)
	{
		case CTRL_PLAY_MODE:                       //控制指定播放状态
			//Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"指定播放:%02X  State:%d",ctrlmode & 0x0f,ctrlstate);
			BleMusicInfo.PlayCtrl_Flag |= 0x01; 
			BleMusicInfo.PlayState = ctrlstate;
			break;
		case CTRL_CYCLE_STATE:                     //控制播放循环模式
			//Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"循环播放:%02X  State:%d",ctrlmode & 0x0f,ctrlstate);
			BleMusicInfo.PlayCtrl_Flag |= 0x02;
			BleMusicInfo.PlayMode = ctrlstate;
			break;
		case CTRL_PLAY_SPECIFY:                    //控制指定播放文件
			//Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"指定播放文件:%02X Folder:%d State:%d",ctrlmode & 0x0f,(ctrlmode >> 4) & 0x0f,ctrlstate);
			BleMusicInfo.PlayCtrl_Flag |= 0x08;
			BleMusicInfo.PlayFolder = (ctrlmode >> 4) & 0x0f;
			BleMusicInfo.PlayFileIndex = ctrlstate;
			BleMusicInfo.FolderPlayEnd = 0x00;
			BleMusicInfo.FolderPlayEnd = (BleMusicInfo.PlayFolder << 8); 
			break;
		case CTRL_VOLUME_VALUE:                    //控制音量大小
			//Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"控制音量:%02X 选项:%d State:%d",ctrlmode & 0x0f,(ctrlmode >> 4) & 0x0f,ctrlstate);
			switch((ctrlmode >> 4) & 0x0f)
			{
				case 0x01:        //设置音量
					BleMusicInfo.SetVol_Flag |= 0x01; 
					BleMusicInfo.PlayVolume = ctrlstate * 10;
					break;
				case 0x02:       //设置全局音量
					BleMusicInfo.SetVol_Flag |= 0x02;
					BleMusicInfo.Volume_Globle = ctrlstate * 10;
					break;
				case 0x03:       //设置本地音量上限
					BleMusicInfo.SetVol_Flag |= 0x10;
					BleMusicInfo.LocalVolUp = ctrlstate * 10;
					break;
				case 0x04:       //设置本地音量下限
					BleMusicInfo.SetVol_Flag |= 0x10;
					BleMusicInfo.LocalVolLow = ctrlstate * 10;
					break;
				case 0x05:       //助眠起始音量
					BleMusicInfo.HSPlayStartVol = ctrlstate * 10;
					break;
				case 0x06:       //助眠结束音量
					BleMusicInfo.HSPlayEndVol = ctrlstate * 10;
					break;
				case 0x07:       //疗愈起始音量
					BleMusicInfo.CurePlayStartVol = ctrlstate * 10;
					break;
				case 0x08:       //疗愈结束音量
					BleMusicInfo.CurePlayEndVol = ctrlstate * 10;
					break;
			}
			break;
		case CTRL_VOLUME_ADVANCED:                 //音量高级设置
			//Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"音量高级设置:%02X 选项:%d State:%d",ctrlmode & 0x0f,(ctrlmode >> 4) & 0x0f,ctrlstate);
			switch((ctrlmode >> 4) & 0x0f)
			{
				case 0x01:     //静音
					BleMusicInfo.SetVol_Flag |= 0x04;
					BleMusicInfo.SilentState = ctrlstate;
					break;
				case 0x02:     //相对音量
					BleMusicInfo.SetVol_Flag |= 0x08;
					BleMusicInfo.RelativeVol = ctrlstate;
					break;
			}
			break;
		case CTRL_HSPLAY_TIME_SET:                   //助眠放时间设置
			//Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"助眠播放时间设置:%02X 单位:%d 时间:%d",ctrlmode & 0x0f,(ctrlmode >> 4) & 0x0f,ctrlstate);
			switch((ctrlmode >> 4) & 0x0f)
			{
				case 0x01:    //秒
					BleMusicInfo.HSPlayTime = ctrlstate;
					break;
				case 0x02:    //分
					BleMusicInfo.HSPlayTime = ctrlstate * 60;
					break;
				case 0x03:    //时
					BleMusicInfo.HSPlayTime = ctrlstate * 60 * 60;
					break;
			}
			break;
		case CTRL_CUREPLAY_TIME_SET:                   //疗愈放时间设置
			//Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"疗愈播放时间设置:%02X 单位:%d 时间:%d",ctrlmode & 0x0f,(ctrlmode >> 4) & 0x0f,ctrlstate);
			switch((ctrlmode >> 4) & 0x0f)
			{
				case 0x01:    //秒
					BleMusicInfo.CurePlayTime = ctrlstate;
					break;
				case 0x02:    //分
					BleMusicInfo.CurePlayTime = ctrlstate * 60;
					break;
				case 0x03:    //时
					BleMusicInfo.CurePlayTime = ctrlstate * 60 * 60;
					break;
			}
			break;
		case CTRL_PASSWORD_REFRESH:                   //更新配对密码
			BleMusicInfo.PSRef_Flag = 0x01;
			break;
	}
	
	
	
//	if(0x01 == keepflag)
	{
		
		BUS_Public.check = 0x00;
		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&BleMusicInfo, sizeof(BLE_MUSIC_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),DevAddr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&BleMusicInfo,sizeof(BLE_MUSIC_INFO),DevAddr+Dev_Privately);		
	}
	
}


uint16_t Dev_BLE_Music_Loop_State(uint32_t devaddr, uint16_t DevOutputLoop)
{
	BLE_MUSIC_INFO BleMusicInfo; 
	
	if(devaddr == 0x00)
		return 0x00;
	
	SRAM_DMA_Read_Buff((uint8_t *)&BleMusicInfo,sizeof(BLE_MUSIC_INFO),devaddr+Dev_Privately);	
	
	return BleMusicInfo.FolderPlayEnd;
}



/*
 * Fun name:    BLE_Query_Status
 * Description: 查询状态
 * Input:       设备属性
 * Return:      NULL
 */
void BLE_Query_Status(Device_Public_Information_G *BUS_Public,BLE_MUSIC_INFO *BleMusicInfo)
{
	uint8_t lens = 0x08;
	uint8_t data[lens];
	memset(data,0,sizeof(data));
	
	data[PKG_HEAD] = 0xA5;
	
	if(BleMusicInfo->DevSendCnt == 0x00)
	{
		BleMusicInfo->DevSendSN++;
		if(BleMusicInfo->DevSendSN > 0x0F)	BleMusicInfo->DevSendSN = 0x00;
		data[PKG_SN] |= (BleMusicInfo->DevSendSN & 0x0F);	//设置发送序号
	}else{	
		//本包数据为重发数据
		data[PKG_SN] |= (BleMusicInfo->DevSendSN & 0x0F);
		data[PKG_SN] |= 0x80;
	}
	
	data[PKG_ADDR] = BUS_Public->addr;
	data[PKG_LENS] = lens;
	data[PKG_CMD] = BLE_CMD_QUERY;
	data[PKG_PARA] = 0x00;
	data[lens - 1] = 0x5A;
	
	data[PKG_CHSUM] = CheckSum(data,lens);
	
	MCU485_SendString(BUS_Public->port,data, lens);
	
	Dbg_Print_Buff(DBG_BIT_DEVICE_STATUS_bit,"BLE_Query_Status:",data,lens);
	
	/*日志记录*/
	LOG_Device_COMM_Send_Control_Record(BUS_Public->port,BUS_Public->baud,data,lens);
}

/*
 * Fun name:    BLE_Set_Name
 * Description: 设置蓝牙名称
 * Input:       设备属性
 * Return:      NULL
 */
void BLE_Set_Name(Device_Public_Information_G *BUS_Public,BLE_MUSIC_INFO *BleMusicInfo)
{
	uint8_t lens = 39;
	uint8_t data[lens];
	memset(data,0,sizeof(data));
	
	data[PKG_HEAD] = 0xA5;
	
	if(BleMusicInfo->DevSendCnt == 0x00)
	{
		BleMusicInfo->DevSendSN++;
		if(BleMusicInfo->DevSendSN > 0x0F)	BleMusicInfo->DevSendSN = 0x00;
		data[PKG_SN] |= (BleMusicInfo->DevSendSN & 0x0F);	//设置发送序号
	}else{	
		//本包数据为重发数据
		data[PKG_SN] |= (BleMusicInfo->DevSendSN & 0x0F);
		data[PKG_SN] |= 0x80;
	}
	
	data[PKG_ADDR] = BUS_Public->addr;
	data[PKG_LENS] = lens;
	data[PKG_CMD] = BLE_CMD_SETNAME;
	memcpy(&data[PKG_PARA],BleMusicInfo->Ble_Name,32);
	
	data[lens - 1] = 0x5A;
	
	data[PKG_CHSUM] = CheckSum(data,lens);
	
	MCU485_SendString(BUS_Public->port,data, lens);
	
	Dbg_Print_Buff(DBG_BIT_DEVICE_STATUS_bit,"BLE_Set_Name:",data,lens);
	
	/*日志记录*/
	LOG_Device_COMM_Send_Control_Record(BUS_Public->port,BUS_Public->baud,data,lens);
	
}

/*
 * Fun name:    BLE_Set_PlayStatus
 * Description: 指定播放状态
 * Input:       设备属性
 * Return:      NULL
 */
void BLE_Set_PlayStatus(Device_Public_Information_G *BUS_Public,BLE_MUSIC_INFO *BleMusicInfo)
{
	uint8_t lens = 17;
	uint8_t data[lens];
	
	memset(data,0,sizeof(data));
	
	data[PKG_HEAD] = 0xA5;
	
	if(BleMusicInfo->DevSendCnt == 0x00)
	{
		BleMusicInfo->DevSendSN++;
		if(BleMusicInfo->DevSendSN > 0x0F)	BleMusicInfo->DevSendSN = 0x00;
		data[PKG_SN] |= (BleMusicInfo->DevSendSN & 0x0F);	//设置发送序号
	}else{	
		//本包数据为重发数据
		data[PKG_SN] |= (BleMusicInfo->DevSendSN & 0x0F);
		data[PKG_SN] |= 0x80;
	}
	
	data[PKG_ADDR] = BUS_Public->addr;
	data[PKG_LENS] = lens;
	data[PKG_CMD] = BLE_CMD_PLAY;
	
	data[PKG_PARA] = BleMusicInfo->PlayCtrl_Flag;
	
	if(BleMusicInfo->PlayCtrl_Flag & 0x01)                //指定播放状态
	{
		data[PKG_PARA + 1] = BleMusicInfo->PlayState;     
	}
	
	if(BleMusicInfo->PlayCtrl_Flag & 0x02)                //播放循环模式
	{
		data[PKG_PARA + 2] = BleMusicInfo->PlayMode;    
	}
	
	if(BleMusicInfo->PlayCtrl_Flag & 0x04)                //指定播放音量
	{
		data[PKG_PARA + 3] = BleMusicInfo->PlayVolume;    
	}
	
	if(BleMusicInfo->PlayCtrl_Flag & 0x08)                //指定播放文件
	{
		data[PKG_PARA + 4] = BleMusicInfo->PlayFolder;
		data[PKG_PARA + 5] = BleMusicInfo->PlayFileIndex;
		
		if(BleMusicInfo->PlayFolder == 0x01)        //助眠文件夹
		{
			data[PKG_PARA + 6] = BleMusicInfo->HSPlayTime & 0xff;
			data[PKG_PARA + 7] = (BleMusicInfo->HSPlayTime >> 8) & 0xff;
			data[PKG_PARA + 8] = BleMusicInfo->HSPlayStartVol;
			data[PKG_PARA + 9] = BleMusicInfo->HSPlayEndVol;
		}
		else if(BleMusicInfo->PlayFolder == 0x02)   //疗愈文件夹
		{
			data[PKG_PARA + 6] = BleMusicInfo->CurePlayTime & 0xff;
			data[PKG_PARA + 7] = (BleMusicInfo->CurePlayTime >> 8) & 0xff;
			data[PKG_PARA + 8] = BleMusicInfo->CurePlayStartVol;
			data[PKG_PARA + 9] = BleMusicInfo->CurePlayEndVol;
		}
	}
	
	data[lens - 1] = 0x5A;
	
	data[PKG_CHSUM] = CheckSum(data,lens);
	
	MCU485_SendString(BUS_Public->port,data, lens);
	
	Dbg_Print_Buff(DBG_BIT_DEVICE_STATUS_bit,"BLE_Set_PlayStatus:",data,lens);
	
	/*日志记录*/
	LOG_Device_COMM_Send_Control_Record(BUS_Public->port,BUS_Public->baud,data,lens);

}


/*
 * Fun name:    BLE_Set_Volume
 * Description: 设置音量
 * Input:       设备属性
 * Return:      NULL
 */
void BLE_Set_Volume(Device_Public_Information_G *BUS_Public,BLE_MUSIC_INFO *BleMusicInfo)
{
	uint8_t lens = 14;
	uint8_t data[lens];
	
	memset(data,0,sizeof(data));
	
	data[PKG_HEAD] = 0xA5;
	
	if(BleMusicInfo->DevSendCnt == 0x00)
	{
		BleMusicInfo->DevSendSN++;
		if(BleMusicInfo->DevSendSN > 0x0F)	BleMusicInfo->DevSendSN = 0x00;
		data[PKG_SN] |= (BleMusicInfo->DevSendSN & 0x0F);	//设置发送序号
	}else{	
		//本包数据为重发数据
		data[PKG_SN] |= (BleMusicInfo->DevSendSN & 0x0F);
		data[PKG_SN] |= 0x80;
	}
	
	data[PKG_ADDR] = BUS_Public->addr;
	data[PKG_LENS] = lens;
	data[PKG_CMD] = BLE_CMD_VOLUME;
	
	data[PKG_PARA] = BleMusicInfo->SetVol_Flag;
	
	if(BleMusicInfo->SetVol_Flag & 0x01)          //设置音量
	{
		data[PKG_PARA + 1] = BleMusicInfo->PlayVolume;
	}
	
	if(BleMusicInfo->SetVol_Flag & 0x02)          //设置全局音量
	{
		data[PKG_PARA + 2] = BleMusicInfo->Volume_Globle;
	}
	
	if(BleMusicInfo->SetVol_Flag & 0x04)          //设置静音状态
	{
		data[PKG_PARA + 3] = BleMusicInfo->SilentState;
	}
	
	if(BleMusicInfo->SetVol_Flag & 0x08)          //相对音量
	{
		data[PKG_PARA + 4] = BleMusicInfo->RelativeVol;
	}

	if(BleMusicInfo->SetVol_Flag & 0x10)          //设置本地音量
	{
		data[PKG_PARA + 5] = BleMusicInfo->LocalVolUp;
		data[PKG_PARA + 6] = BleMusicInfo->LocalVolLow;
	}
	
	data[lens - 1] = 0x5A;
	
	data[PKG_CHSUM] = CheckSum(data,lens);
	
	MCU485_SendString(BUS_Public->port,data, lens);
	
	Dbg_Print_Buff(DBG_BIT_DEVICE_STATUS_bit,"BLE_Set_Volume:",data,lens);
	
	/*日志记录*/
	LOG_Device_COMM_Send_Control_Record(BUS_Public->port,BUS_Public->baud,data,lens);
}

/*
 * Fun name:    BLE_Query_VerInf
 * Description: 查询设备信息及软件版本号
 * Input:       设备属性
 * Return:      NULL
 */
void BLE_Query_VerInf(Device_Public_Information_G *BUS_Public,BLE_MUSIC_INFO *BleMusicInfo)
{
	uint8_t lens = 0x08;
	uint8_t data[lens];
	memset(data,0,sizeof(data));
	
	data[PKG_HEAD] = 0xA5;
	
	if(BleMusicInfo->DevSendCnt == 0x00)
	{
		BleMusicInfo->DevSendSN++;
		if(BleMusicInfo->DevSendSN > 0x0F)	BleMusicInfo->DevSendSN = 0x00;
		data[PKG_SN] |= (BleMusicInfo->DevSendSN & 0x0F);	//设置发送序号
	}else{	
		//本包数据为重发数据
		data[PKG_SN] |= (BleMusicInfo->DevSendSN & 0x0F);
		data[PKG_SN] |= 0x80;
	}
	
	data[PKG_ADDR] = BUS_Public->addr;
	data[PKG_LENS] = lens;
	data[PKG_CMD] = BLE_CMD_VERINF;
	data[PKG_PARA] = 0x00;
	data[lens - 1] = 0x5A;
	
	data[PKG_CHSUM] = CheckSum(data,lens);
	
	MCU485_SendString(BUS_Public->port,data, lens);
	
	Dbg_Print_Buff(DBG_BIT_DEVICE_STATUS_bit,"BLE_Query_VerInf:",data,lens);
	
	/*日志记录*/
	LOG_Device_COMM_Send_Control_Record(BUS_Public->port,BUS_Public->baud,data,lens);
}



/*
 * Fun name:    BLE_ReFresh_PassWord
 * Description: 更新配对密码
 * Input:       设备属性
 * Return:      NULL
 */
void BLE_ReFresh_PassWord(Device_Public_Information_G *BUS_Public,BLE_MUSIC_INFO *BleMusicInfo)
{
	uint8_t lens = 0x08;
	uint8_t data[lens];
	memset(data,0,sizeof(data));
	
	data[PKG_HEAD] = 0xA5;
	
	if(BleMusicInfo->DevSendCnt == 0x00)
	{
		BleMusicInfo->DevSendSN++;
		if(BleMusicInfo->DevSendSN > 0x0F)	BleMusicInfo->DevSendSN = 0x00;
		data[PKG_SN] |= (BleMusicInfo->DevSendSN & 0x0F);	//设置发送序号
	}else{	
		//本包数据为重发数据
		data[PKG_SN] |= (BleMusicInfo->DevSendSN & 0x0F);
		data[PKG_SN] |= 0x80;
	}
	
	data[PKG_ADDR] = BUS_Public->addr;
	data[PKG_LENS] = lens;
	data[PKG_CMD] = BLE_CMD_UPDATE_PASSWORD;
	data[PKG_PARA] = 0x01;
	data[lens - 1] = 0x5A;
	
	data[PKG_CHSUM] = CheckSum(data,lens);
	
	MCU485_SendString(BUS_Public->port,data, lens);
	
	Dbg_Print_Buff(DBG_BIT_DEVICE_STATUS_bit,"BLE_ReFresh_PassWord:",data,lens);
	
	/*日志记录*/
	LOG_Device_COMM_Send_Control_Record(BUS_Public->port,BUS_Public->baud,data,lens);
}


/*
 * Fun name:    BLE_Event_Ack
 * Description: 助眠/疗愈播放结束上报回复
 * Input:       设备属性
 * Return:      NULL
 */
void BLE_Event_Ack(Device_Public_Information_G *BUS_Public,BLE_MUSIC_INFO *BleMusicInfo)
{
	uint8_t lens = 0x08;
	uint8_t data[lens];
	memset(data,0,sizeof(data));
	
	data[PKG_HEAD] = 0xA5;
	
	if(BleMusicInfo->DevSendCnt == 0x00)
	{
		BleMusicInfo->DevSendSN++;
		if(BleMusicInfo->DevSendSN > 0x0F)	BleMusicInfo->DevSendSN = 0x00;
		data[PKG_SN] |= (BleMusicInfo->DevSendSN & 0x0F);	//设置发送序号
	}else{	
		//本包数据为重发数据
		data[PKG_SN] |= (BleMusicInfo->DevSendSN & 0x0F);
		data[PKG_SN] |= 0x80;
	}
	
	data[PKG_ADDR] = BUS_Public->addr;
	data[PKG_LENS] = lens;
	data[PKG_CMD] = BLE_CMD_PLAY_END;
	data[PKG_PARA] = 0x00;
	data[lens - 1] = 0x5A;
	
	data[PKG_CHSUM] = CheckSum(data,lens);
	
	MCU485_SendString(BUS_Public->port,data, lens);
	
	Dbg_Print_Buff(DBG_BIT_DEVICE_STATUS_bit,"BLE_Event_Ack:",data,lens);
	
	/*日志记录*/
	LOG_Device_COMM_Send_Control_Record(BUS_Public->port,BUS_Public->baud,data,lens);
}
