#ifndef DSTWR_H
#define DSTWR_H

typedef uint64_t	EUI64;

#define RTLS_MSG_TYPE_TWR_POLL						(0x51)	// Tag发出的轮询消息, Tag poll message
#define RTLS_MSG_TYPE_TWR_RESPONSE					(0x52)	// Anchor 应答 Tag 发出的消息, Anchor response to poll
#define RTLS_MSG_TYPE_TWR_FINAL						(0x53)	// Tag 发回给 Anchor 的最终消息, Tag final massage back to Anchor

// uint64 大小端转换
#define EndianSwap64(v)	\
						(((v & 0x00000000000000FF) << 56) |	/* 0xFF************FF */	\
						 ((v & 0x000000000000FF00) << 40) |	/* 0x00FF********FF00 */	\
						 ((v & 0x0000000000FF0000) << 24) |	/* 0x0000FF****FF0000 */	\
						 ((v & 0x00000000FF000000) <<  8) |	/* 0x000000FFFF000000 */	\
						 ((v & 0x000000FF00000000) >>  8) |	/* 0x000000FFFF000000 */	\
						 ((v & 0x0000FF0000000000) >> 24) |	/* 0x0000FF****FF0000 */	\
						 ((v & 0x00FF000000000000) >> 40) |	/* 0x00FF********FF00 */	\
						 ((v & 0xFF00000000000000) >> 56) )	/* 0xFF************FF */

// TWR POLL 消息
// 广播帧, Tag 发出的 twr poll 帧, 带有 PollTxTS (轮询包发送时间戳)
typedef struct  ieee154_broadcast_twr_poll_frame {
    uint8_t frameCtrl[2];			//  0, 2: frame control bytes 00-01: 0x01 (Frame Type 0x01=date), 0xC8 (0xC0=src extended address 64 bits, 0x08=dest address 16 bits)
    uint8_t seq8;					//  2, 1: sequence_number 02
    union {
        uint8_t panID[2];			// 3, 2, PAN ID 03-04
        uint16_t panId;				// 3, 2, PAN ID 03-04
    };
    union {
        uint8_t destAddr[2];		// 5, 2, 0xFFFF
        uint16_t destId16;
    };
    union {
        uint8_t source_addr[8];		// 7, 8, 64Bits EUI地址
        EUI64 sourceId;				// 7, 8, 64Bits EUI Id
    };
    uint8_t	messageType;			// 15, 1: Message Type = RTLS_MSG_TYPE_TWR_POLL
    uint8_t pollTxTS[5];			// 16, 5: poll tx 时间戳, 40 Bits
    uint8_t fcs[2];					// 21, 2:
} __attribute__((packed)) TWR_BROADCAST_POLL_MESSAGE;		// 以上合计 23 字节


// // TWR POLL 消息
// // 非广播帧, Tag 发出的 twr poll 帧, 带有 PollTxTS (轮询包发送时间戳)
// typedef struct  ieee154_unbroadcast_twr_poll_frame {
//     uint8_t frameCtrl[2];		//  0, 2 frame control bytes 00-01: 0x01 (Frame Type 0x01=date), 0xCC (0xC0=src extended address 64 bits, 0x0C=dest address 64 bits)
//     uint8_t seq8;				//  2, 1 sequence_number 02
//     union {
//         uint8_t panID[2];		// 3, 2, PAN ID 03-04
//         uint16_t panId;			// 3, 2, PAN ID 03-04
//     };
//     union {
//         uint8_t destAddr[8];	//  5, 8 64Bits EUI地址, 要配置的 Tag 的地址, 如果为 0x0000000000000000 或 0xFFFFFFFFFFFFFFFF 则为广播
//         EUI64 destId;
//     };
//     union {
//         uint8_t source_addr[8];	// 13, 8, 64Bits EUI地址
//         EUI64 sourceId;			// 13, 8, 64Bits EUI Id
//     };
//     uint8_t	messageType;		// 21, 1: Message Type = RTLS_MSG_TYPE_TWR_POLL
//     uint8_t pollTxTS[5];		// 22, 5: poll tx 时间戳, 40 Bits
//     uint8_t fcs[2];				// 27, 2:
// } __attribute__((packed)) TWR_UNBROADCAST_POLL_MESSAGE;		// 以上合计 29 字节

// TWR RESPONSE 消息
// 指定目标的帧, 是 anchor 回应 tag 发出的 twr poll 消息, tag 收到本消息后回应 twr final 消息
// 带有 PollTxTS(轮询包发送时间戳), ReponseTxTS(应答包的发送时间戳), timeReply1(Anchor收到Poll到发送Response的延时)
typedef struct  ieee154_unbroadcast_twr_response_frame {
    uint8_t frameCtrl[2];		//  0, 2 frame control bytes 00-01: 0x01 (Frame Type 0x01=date), 0xCC (0xC0=src extended address 64 bits, 0x0C=dest address 64 bits)
    uint8_t seq8;				//  2, 1 sequence_number 02
    union {
        uint8_t panID[2];		// 3, 2, PAN ID 03-04
        uint16_t panId;			// 3, 2, PAN ID 03-04
    };
    union {
        uint8_t destAddr[8];	//  5, 8 64Bits EUI地址, 要配置的 Tag 的地址, 如果为 0x0000000000000000 或 0xFFFFFFFFFFFFFFFF 则为广播
        EUI64 destId;
    };
    union {
        uint8_t source_addr[8];	// 13, 8, 64Bits EUI地址
        EUI64 sourceId;			// 13, 8, 64Bits EUI Id
    };
    uint8_t	messageType;		// 21, 1 Message Type = RTLS_MSG_TYPE_TWR_RESPONSE

    uint8_t pollTxTS[5];		// 22, 5: poll tx 时间戳, 40 Bits
    uint8_t pollRxTS[5];		// 27, 5: poll rx 时间戳, 40 Bits
    uint8_t responseTxTS[5];	// 32, 5: response tx 时间戳, 40 Bits
    uint8_t fcs[2];				// 37, 2:
} __attribute__((packed)) TWR_UNBROADCAST_RESPONSE_MESSAGE;		// 以上合计 39 字节

// TWR FINAL 消息
// 指定目标的帧, 是 tag 回应 anchor 发出的 twr response 消息, anchor 收到本消息后就可以计算距离了
// 带有 ReponseTxTS(应答包的发送时间戳), timeRound1(Tag发送poll包到收到Response的延时), timeReply1(Anchor收到Poll到发送Response的延时),  timeReply2(Tag收到Response到发送Final的延时)
// Anchor 收到后, 可以计算出 timeRound2(Anchor发出Response到收到Final的延时)
typedef struct  ieee154_unbroadcast_twr_final_frame {
    uint8_t frameCtrl[2];		//  0, 2 frame control bytes 00-01: 0x01 (Frame Type 0x01=date), 0xCC (0xC0=src extended address 64 bits, 0x0C=dest address 64 bits)
    uint8_t seq8;				//  2, 1 sequence_number 02
    union {
        uint8_t panID[2];		// 3, 2, PAN ID 03-04
        uint16_t panId;			// 3, 2, PAN ID 03-04
    };
    union {
        uint8_t destAddr[8];	//  5, 8 64Bits EUI地址, 要配置的 Tag 的地址, 如果为 0x0000000000000000 或 0xFFFFFFFFFFFFFFFF 则为广播
        EUI64 destId;
    };
    union {
        uint8_t source_addr[8];	// 13, 8, 64Bits EUI地址
        EUI64 sourceId;			// 13, 8, 64Bits EUI Id
    };
    uint8_t	messageType;		// 21, 1 Message Type = RTLS_MSG_TYPE_TWR_FINAL

    uint8_t pollTxTS[5];		// 22, 5: poll tx 时间戳, 40 Bits
    uint8_t pollRxTS[5];		// 27, 5: poll rx 时间戳, 40 Bits
    uint8_t responseTxTS[5];	// 32, 5: response tx 时间戳, 40 Bits
    uint8_t responseRxTS[5];	// 37, 5: response rx 时间戳, 40 Bits
    uint8_t finalTxTS[5];		// 42, 5: final tx 时间戳, 40 Bits
    uint8_t fcs[2];				// 47, 2:
} __attribute__((packed)) TWR_UNBROADCAST_FINAL_MESSAGE;		// 以上合计 49 字节


#ifdef __cplusplus
extern "C" {
#endif




#ifdef __cplusplus
}
#endif
#endif
