#ifndef _NRF_DATA_H_
#define _NRF_DATA_H_

#include "stm32f10x.h"

typedef char    BYTE;
typedef uint8_t BOOL;   

#define MAC_LEN					5
#define PAYLOAD_LEN				32
#define PALYOAD_HEADER_LEN		sizeof(PayloadHeader)
#define PAYLOAD_DATA_LEN		(PAYLOAD_LEN-PALYOAD_HEADER_LEN)
#define DATA_BUF_COUNT			50

#define MAX_FAILED_COUNT		3     //发送失败次数
#define LINK_INTERVAL 			1000  //处理泛洪间隔,单位10ms(10秒)
#define HEART_LINK_INTERVAL		2200  //发送心跳间隔,单位10ms(22秒)

enum NODESTATUS{INVALID,VALID};
enum LINKSTATUS{DISCONNECT,WAIT_CONNECT,CONNECTED,WAIT_CHANGE};
enum PAYLOADSTATUS{IDLE,WAIT_USE,WAIT_SEND,WAIT_SEND_RETRY,SEND_FAILED,SEND_SUCCESS,WAIT_RECV,RECV_FAILED,WAIT_HANDLER};
enum PACKAGETYPE{START_LINK,BUILD_CONNECT,BUILD_CONNECT_ACK,CHECK_LINK,CHECK_LINK_ACK,ONE_DATA,ONE_DATA_ACK,MULTI_DATA};

#define PACKAGE_SUCCESS   0x01
#define PACKAGE_FAILED    0x00

typedef void (*HANDLECALLBACK)(u8 src_addr,u8 dst_addr,u8 pack_type,u16 num,u8 *buf,u8 status);

#pragma pack(1)
typedef struct 
{
	u8 src_addr; //源地址
	u8 dst_addr; //目的地址
	u8 pack_type;//payload类型
	u16 pwd;
	u16 ID;      //发送payload ID
}PayloadHeader,*LPayloadHeader;//payload头结构

typedef struct
{
	PayloadHeader header;    //payload 头
	u8 buf[PAYLOAD_DATA_LEN];//数据缓冲区
}Payload,*LPayload;//payload结构 
#pragma pack()

typedef struct
{
	u8 status;         	 //数据包发送状态
	u8 isBroadcast;		 //是否广播
	u8 send_failed_count;//发送失败次数
	u8 dst_channel;      //发送信道
	u8 dst_mac[MAC_LEN+1];//发送的目的MAC地址
	HANDLECALLBACK HandleCallBack;//回调
	Payload payload;     //数据包
}DATA_BUF,*LPDATA_BUF;//同步数据包

typedef struct
{
	u8 node_addr;		   //节点地址
	u8 local_mac[MAC_LEN]; //节点的mac
	u16 pwd;
}NRFSTOREDATA,*LPNRFSTOREDATA;

#pragma pack(1)
typedef struct
{
	u8 node_addr;           //当前地址
	u8 route_addr;
	u8 channel;             //当前信道
	u8 link_status;    		//连接状态
	u8 send_failed_count;
	u8 node_mac[MAC_LEN+1];
	u16 pwd;
	u32 shift_failed_count; //切换失败计数
	u32 shift_count;        //切换成功计数器
	u32 recv_count;         //接收数据包数
}NODESTATUS;//节点状态结构
#pragma pack()

typedef struct
{
	u8 src_addr;
	u8 dst_addr;
	u16 RID;
	u32 time;    
}Handl,*LHandl;

#endif
