/*
*********************************************************************************************************
*                                       IAR Development Kits
*                                              on the
*
*                                    Nano100
*
* Filename      : uart_RFGateway.h
* Version       : V1.00
* Programmer(s) : Qian Xianghong
*********************************************************************************************************
*/

#ifndef  USER_UART_RFGATEWAY_PRESENT
#define  USER_UART_RFGATEWAY_PRESENT

#include "type.h"

extern const uint8_t RF_APP;	

// 初始化
void RF_Open();

// 模块上电，设置硬件参数
void RF_PowerOn();
void RF_PowerOff();

// 任务主体
void RF_GatewayTask(void *p_arg);
void RF_TranTask(void *p_arg);
void RF_SendTask(void *p_arg);

#define RF_MIN_FRAME_LEN	(24)
#define RF_MAX_FRAME_LEN	(61)
#define RF_MIN_PAYLOAD_LEN	(0)
#define RF_MAX_PAYLOAD_LEN	(RF_MAX_FRAME_LEN - RF_MIN_FRAME_LEN)

#define RF_DIR_UP			(0)		// 上行
#define RF_DIR_DOWN			(1)		// 下行
#define RF_MAC_TYPE_DATA	(0)		// mac帧-数据
#define RF_MAC_TYPE_ACK		(1)		// mac帧-ACK
#define RF_NET_TYPE_DATA		(0)		// net帧-数据
#define RF_NET_TYPE_FIND_RELAY	(1)		// net帧-寻找中继器
#define RF_NET_TYPE_RESP_RELAY	(2)		// net帧-应答中继器

#define RF_FRAME_SOF		(0x1B)	// 标识一个帧的开始
#define RF_MARK_LS			(('S' << 8) | 'L')
#define RF_APP_AJH			('A')	// data.idatatank.com:8088
#define RF_APP_PRAX			('P')	// tts.praxair.com.cn:7005
#define RF_APP_LANSHI		('L')	// data.cd-ls.cn:8088
#define RF_PROTOCOL_VER_1	(1)

#define RF_SENSOR_STATUS_NORMAL		0	// 正常状态
#define RF_SENSOR_STATUS_OVERFLOW	1	// 上溢出
#define RF_SENSOR_STATUS_UNDERFLOW	2	// 下溢出
#define RF_SENSOR_STATUS_NOCONNECT	3	// 未连接

// 射频初始化状态
extern uint8_t RF_initStatus;
extern uint32_t RF_seconds;

// 允许匿名结构
#pragma anon_unions
#pragma pack(push, 1)

typedef struct	// size = RF_MAX_FRAME_LEN
{
	uint8_t		sof;				// RF_FRMAE_SOF
	uint8_t		len;				// bytes from sof to crc（RF_MIN_FRAME_LEN~RF_MAX_FRAME_LEN）
	uint16_t	vendor_id;			// RF_MARK_LS
	uint8_t		app_id;				// RF_APP_AJH or RF_APP_PRAX or RF_APP_LANSHI
	uint8_t		protocol_ver;		// RF_PROTOCOL
	unsigned	dir : 1;			// RF_DIR_UP or RF_DIR_DOWN
	unsigned	mac_type : 2;  		// RF_MAC_TYPE_DATA or RF_MAC_TYPE_ACK
	unsigned	mac_ack_req : 1;	// is ack requested?
	unsigned 	net_type : 2;		// RF_NET_TYPE_DATA or RF_NET_TYPE_FIND_RELAY or RF_NET_TYPE_RESP_RELAY
	unsigned	: 2;				// reserved
	uint8_t		mac_fn;				// mac frame number
	uint8_t		destPSN[6];
	uint8_t		srcPSN[6];
	uint8_t		app_pn;				// package number
	unsigned	app_idx : 7;		// frame number in package
	unsigned	app_tbc : 1;		// to be continue?
	uint8_t		app_payload[RF_MAX_PAYLOAD_LEN];
	uint16_t	crc;				// 占位置，实际crc可能更靠前
} rf_frame_t;

// mac层ack信息
typedef struct
{
	uint8_t srcPSN[6];
	uint8_t mac_fn;
} rf_ack_t;

// 应用层应答信息
typedef struct
{
	uint8_t srcPSN[6];
	uint8_t app_pn;
	uint8_t payload_len;
	uint8_t payload[RF_MAX_PAYLOAD_LEN];
} rf_resp_t;

// 发送队列
typedef struct
{
	uint8_t type;	// 0-ACK，1-中继应答，2-应用层应答
	uint8_t oriPSN[6];
	uint8_t fn_pn;	// ACK的fn或应用层的pn
	uint8_t payload_len;
	uint8_t payload[RF_MAX_PAYLOAD_LEN];
} rf_send_queue_t;

typedef struct
{
	unsigned staDiff :		2;
	unsigned staPress :		2;
	unsigned staETempr1 :	2;
	unsigned charging :	1;
	unsigned bat_low : 1;
	unsigned measure_type :	2;
	unsigned bottle_type :	2;
	unsigned L_source :		3;
	unsigned location_type : 1; // 0-GPS,1-基站
} rf_sensor_t;

typedef struct	// size = 37
{
	uint16_t diff;
	uint16_t press;
	int16_t tempr;
	rf_sensor_t Sensor;
	uint16_t density;
	uint32_t longitude;
	uint32_t latitude;
	uint16_t voltage;
	uint8_t batPct;
	int8_t batCurrent;
	uint32_t relative_time;
	uint8_t hardVer;
	uint8_t softVer;
	uint32_t L;
	uint32_t d;
	uint8_t chargePct;
} rf_app_data_t;

#pragma pack(pop)

// 多项式为x16+x15+x2+1，LSB顺序
// 同ibutton的crc算法，不同于modbus的crc算法
uint16_t rf_crc_16(uint8_t *message, int16_t len);
// 计算自启动以来经过的秒数
uint32_t rf_get_seconds();

#endif
