#ifndef _LORA_H_
#define _LORA_H_


//#define lora_channel   0x02  //信道

#define ringbuffer_size     1024*60

#define LORA_POWER_PIN          (20)
#define LORA_TXD_PIN            (17)
#define LORA_RXD_PIN            (18)

#define LORA_BUSY_PIN            (5)
/*

BUSY_STA，忙状态指示。高电平表示设备当前工作状态忙，不可进行串口操
作，低电平表示外设可以进行串口操作

*/

#define LORA_DATA_MAX_LEN           1024


#define uart_fifo_size  120
#define buffer_size     1024*20
#define uart_timerout_times 30 //ms


typedef struct 
{
    char version;
    char wireless_ch;
    char wireless_freq_range;
    char wireless_tx_power;
    char wireless_bps;
    char serial_bps;
    char packet_fmt;
    char auto_ack;
    char outdir;
    char wireless_ch_interval_bandwidth;
}LORA_CFG_T;


typedef enum
{

    uart_idle = 0,
    uart_receving = 1,
    uart_receive_complete = 2,

}uart_status_t;



typedef struct {
    uint8_t data_len;
    uint8_t data_buf[LORA_DATA_MAX_LEN];
}LORA_DATA_T;

typedef LORA_DATA_T USE_DATA_T;

extern LORA_CFG_T lora_cfg_data;
extern uint8_t lora_rssi_data;
extern uint8_t lora_software_version;


void lora_send_queue_callback(uint8_t* data_buf,int len);

void lora_init(void);

void lora_uart_pin_init(void);
void lora_event_task(void *pvParameters);
void lora_set_power_level(int level);

int lora_set_by_serial(uint8_t cmd,uint8_t value);
int lora_reset_software(void);
int lora_set_factory(void);
int lora_set_wireless_channel_interval_bandwidth(uint8_t bandwidth);
int lora_set_data_outdir(uint8_t dir);
int lora_set_auto_ack(uint8_t ack);
int lora_set_packet_fmt(uint8_t fmt);
int lora_set_Wireless_Channel (uint8_t Channel);
int lora_set_tx_power(uint8_t power);
int lora_set_Wireless_bps(uint8_t bps);
int lora_set_serial_bps(uint8_t bps);
int lora_get_rssi(uint32_t rssi);
int lora_get_version(void);
int lora_get_cfg(void);
int lora_send_data(const char* lora_data,int lora_data_len);
int lora_set_cfg_mode(uint8_t mode);



int get_lora_busy_pin();
//int lora_analytic_data(const uint8_t* data_buf,int len);


#if 1



#define USER_QIXIN  1   //使用祁鑫的初始化



#define RX_MAX_LENGTH    1024

#include<stdbool.h>

#define LORA_CHANENL    0x00

#define HPD_LORA_ENABLE 0
#define VJ_LORA_ENABLE  1

#define LORA_UART  UART_NUM_1


// IO5	I		LORA_BUSY	LORA总线忙输入，参考LORA模块PDF文档	仅合普顿模块有效
// IO6	I		LORA_SET	LORA设置参数IO，参考LORA模块PDF文档	
// IO7	O		LORA_RST	LORA复位输出IO，参考LORA模块PDF文档	
// IO17	O		LORA_RXD	LORA串口数据RXD	
// IO18	I		LORA_TXD	LORA串口数据TXD	
// IO20	O		LORA_POWER_SD	LORA模块电源控制，默认高，低电平LORA模块上电。	


#define LORA_BUSY_PIN   5
#define LORA_SET_PIN    6
#define LORA_RST_PIN    7

#define LORA_RXD_PIN    18
#define LORA_TXD_PIN    17
#define LORA_POWER_PIN  20




#define LORA_CMD_MAX_LENGTH   256
#define LORA_CMD_NUM          16
#define MAX_RETRY_NUM         5



#define LORA_CMD_AH_CMD          "AH"
#define LORA_CMD_AH_CMD_RSP      "OK"


//设置波特率
#define LORA_CMD_AH_BAUDRATE         "AH+B"
#define LORA_CMD_AH_BAUDRATE_RSP     "OK+B"


#define AT_MAX_RETRY_TIMES   10


#if VJ_LORA_ENABLE   

#define LORA_SUCCESS   0x55  //沃进成功返回命令码
#define LORA_FAIL	   0xEE  //沃进失败返回命令码

#endif

typedef enum
{
	Lora_Cmd_Mode  = 0,  //AT命令模式
	Lora_UART_Mode = 1,  //串口透传模式
    // Lora_read_data = 2,
}Lora_Mode_t;


typedef enum
{
	lora_cmd_system_type  = 0,  //系统上电配置的AT初始化命令模式
	lora_cmd_user_type    = 1,  //用户自定义可以随时的发送命令

}Lora_cmd_type_t;



//命令解析函数
typedef struct 
{
   uint8_t cmd[LORA_CMD_MAX_LENGTH]; //命令字符串
   uint8_t cmd_len;    //命令长度
   uint32_t cmd_mode;   //当前命令
   void (*lora_send_cmd_handler)(uint32_t cmd_mode);

   uint8_t cmd_rsp[LORA_CMD_MAX_LENGTH]; //命令回复字符串
   uint8_t cmd_rsp_len;    //回复命令长度
   void(*success_hanlder)(unsigned char *cmd,unsigned char *cmd_rsp,int len);       //回调函数
   void(*timeout_hanlder)(unsigned char *cmd,unsigned char *cmd_rsp,int len);       //超时未响应回调函数
}lora_cmd_t;


typedef struct
{
	uint8_t   lora_mode;  	 //Lora_Mode_t
	lora_cmd_t  *lora_cmd;
	uint8_t cmd_index;      //当前执行命令索引值
	bool    timerout;
	uint8_t retry_times;    //最大重新传输次数
	uint8_t cmd_type;
}Lora_t;

extern uint8_t rssi;

void lora_init();

int lora_sendData(unsigned char* data,int len);

void lora_cmd_success_Hander(unsigned char *cmd,unsigned char *cmd_rsp,int len);

void lora_cmd_timerout_Hander(unsigned char *cmd,unsigned char *cmd_rsp,int len);


void VJ_Lora_set_cfg_mode(uint8_t mode);



void lora_set_config_mode(uint8_t mode);

void lora_set_channel(uint8_t channel);

void lora_set_bps(uint8_t bps);

void lora_set_mode(uint8_t mode);

void lora_send_ack(uint8_t cmd,uint8_t *mac,int id_num,uint8_t *cmd_index,uint8_t status);


void dymatic_change_chanel(uint8_t chanel);

void lora_set_device_id(uint32_t id);

void lora_set_dst_device_id(uint32_t id);




void dymatic_change_device_id(uint32_t id);
void dymatic_change_dst_device_id(uint32_t id);


void uart_sleep_in_config();
void uart_sleep_out_config();
void timer_wake_uart_sleep_out_config();


void lora_factory_config();
void reset_lora(void);
uint8_t set_lora(uint8_t new_channel,uint8_t eflag);
uint8_t crc8( uint8_t *pData, 
                    uint16_t dataLen,
                    uint8_t initialValue, 
                    uint8_t polynomial );

#if 0
bool lora_uart_is_normal(int Baud);

bool lora_send_cmd(String cmd,String cmd_rsp);

bool lora_set_Baud(int baud);

bool lora_set_default();


bool lora_set_work_freq(String freq);

bool lora_set_send_power(int baud);


bool lora_set_bw(int baud);

bool lora_set_sf(int baud);

bool lora_set_net_id(String id);

bool lora_set_rssi(int rssi);
#endif






















#endif
















#endif/*_LORA_H_*/
