








void Pro_SendData(const u8 data,const u8 type,const u16 id,const u8 *buf,HANDLECALLBACK fun)//发送数据接口
{
	if(strlen((char *)buf)>PAYLOAD_DATA_LEN)
	{
		return ;
	}
	DataBuf.status=WAIT_SEND;
	DataBuf.channel=NodeStatus.channel;
	Pro_CopyMac(DataBuf.mac,NodeStatus.mac);
	DataBuf.CallBack=fun;
	DataBuf.payload.header.header.data=data;
	DataBuf.payload.header.header.pack_type;
	DataBuf.payload.header.header.ID=id; 
	Pro_MemCopy((BYTE *)DataBuf.payload.buf,(BYTE *)buf,PAYLOAD_DATA_LEN);
}




DataBuf DataBuf;//数据缓冲
NODE_STATUS NodeStatus;//节点状态
static CALLBACK RecvHandler=NULL;

Pro_MemSet((BYTE *)&DataBuf,0,sizeof(DataBuf));//初始化缓冲区为空闲状态
Pro_MemSet((BYTE *)&NodeStatus,0,sizeof(NODE_STATUS));

void Pro_SetDefaultRecvHandler(CALLBACK pFun)
{
	RecvHandler=pFun;
}

void NRF_SendData(const u8 data,const u8 type,const u16 id,const u8 *buf,const u8 channel,const u8 *mac)//发送数据接口
{
	if(WAIT_SEND==DataBuf.status||WAIT_SEND_RETRY==DataBuf.status)
	{
		if(!NRF_DisturbanceDetect())//检测是否有其它节点在发送数据
		{
			if(NRF_SendPayload(mac,channel,(u8 *)&payload))//发送
			{
				DataBuf.status=SEND_SUCCESS;
			}
			else
			{
				DataBuf.status=WAIT_SEND_RETRY;
			}
			NRF_RxMode((u8 *)init_rx_mac, channel);
		}
	}
}

void Pro_HandleBuf(void)//根据缓冲区状态处理缓冲区
{
	switch(Data_Buf.status)
	{
		case WAIT_SEND_RETRY:
			if(Data_Buf.send_failed_count>=MAX_FAILED_COUNT)
			{
				Data_Buf.status=SEND_FAILED;
			}
			break;
		case SEND_SUCCESS:
			if(Data_Buf.CallBack)
			{
				Data_Buf.CallBack(Data_Buf.payload.header.data,
									Data_Buf.payload.header.pack_type,
									Data_Buf.payload.header.ID,
									Data_Buf.payload.buf,PACKAGE_SUCCESS);
			}
			Pro_MemSet((BYTE *)&DataBuf,0,sizeof(DataBuf));//初始化缓冲区为空闲状态
			break;
		case SEND_FAILED:
			if(Data_Buf.CallBack)
			{
				Data_Buf.CallBack(Data_Buf.payload.header.data,
									Data_Buf.payload.header.pack_type,
									Data_Buf.payload.header.ID,
									Data_Buf.payload.buf,PACKAGE_FAILED);
			}
			Pro_MemSet((BYTE *)&DataBuf,0,sizeof(DataBuf));//初始化缓冲区为空闲状态
			break;
		default:break;
	}
}

void NRF_RecvData(void)
{
	Payload tmp;
	u8 id,status,pipe_num;
	NRF_CE = 0;
	Pro_MemSet((BYTE *)&tmp,0,sizeof(Payload));
	status=NRF_ReadReg(STATUS);//读取status寄存器的值
	NRF_ReadBuf(R_RX_PAYLOAD, (u8 *)&tmp, PAYLOAD_LEN);//保存接收到的数据
//#ifdef DEBUG
//	printf("***Pipe%d-RecvPayload***\r\n",pipe_num);
//	printf("src_addr=%d\r\ndst_addr=%d\r\n",tmp.header.src_addr,tmp.header.dst_addr);
//	printf("pwd=%d\r\nID=%d\r\n",tmp.header.pwd,tmp.header.ID);
//	printf("type=%d\r\nbuf=%s\r\n\r\n",tmp.header.pack_type,tmp.buf);
//#endif
	if(RecvHandler)
	{
		RecvHandler(tmp.header.data,pPayLoad->header.pack_type,
					tmp.header.id,tmp.buf,0);
	}
	NRF_RxMode((u8 *)init_rx_mac, channel);
}



static void RecvCallBack(u8 data,u8 pack_type,u16 id,u8 *buf,u8 state);

Pro_SetDefaultRecvHandler(RecvCallBack);//注册回调函数

void RecvCallBack(u8 data,u8 pack_type,u16 id,u8 *buf,u8 state)
{
	switch(tmp.header.pack_type)
	{
		case START_SCAN:
			NRF_SendData(node_addr,LOCAL_ADDR,0,(u8 *)"",channel,tx_mac)
			break;
		case LOCAL_ADDR:
			break;
		case CONFIG_CH:
			break;
		case ONE_DATA:
			if(GetFifoBytes()>1024)
			{
				NRF_SendData(0,DATA_PAUSE,0,(u8 *)"",channel,tx_mac)
			}
			else
			{
				NRF_SendData(0,ONE_DATA_ACK,0id,DataBuf,channel,tx_mac)
			}
			break;
		case ONE_DATA_ACK:
			break;
		case DATA_PAUSE:
			break;
		case DATA_LAST:
			break;
		case SUCCEED:
			break;
		default:break;
	}
}