/************************************************************************************
 * @Copyright (c)    :(C)2017, Qindao ieslab Co., Ltd
 * @FileName   	     :nb_driver_bc25.h
 * @Author           :QDIES PLATFORM TEAM
 * @Version          :V1.0
 * @Date             :2020-8-13
 * @Description     :
 ************************************************************************************/

#ifndef __NB_DRIVER_MN316_H
#define	__NB_DRIVER_MN316_H


#include "sys_config.h"
#ifdef NB_MODULE_MN316
#include "type.h"
#include <string.h>
  

/*-----------------端口配置定义-------------------*/


#if (HARDWARE_VERSION==0x14)//方壳表
#define RSTN_NB_PORT    (GpioPortF)
#define RSTN_NB_PIN     (GpioPin0)

#define BAT_NB_PORT     (GpioPortE)
#define BAT_NB_PIN      (GpioPin4)

#define PWR_NB_PORT     (GpioPortA)//(GpioPortF)
#define PWR_NB_PIN      (GpioPin15)//(GpioPin1)

#define NB_PSM_WAKE_PORT  (GpioPortA)
#define NB_PSM_WAKE_PIN   (GpioPin2)

#elif (HARDWARE_VERSION==0x18)//圆壳表
#define RSTN_NB_PORT    (GpioPortE)
#define RSTN_NB_PIN     (GpioPin4)

#define BAT_NB_PORT     (GpioPortE)
#define BAT_NB_PIN      (GpioPin5)

#define PWR_NB_PORT     (GpioPortE)
#define PWR_NB_PIN      (GpioPin2)

#define NB_PSM_WAKE_PORT  (GpioPortE)
#define NB_PSM_WAKE_PIN   (GpioPin3)
#endif

#define RSTN_NB_LOW()    Gpio_WriteOutputIO(RSTN_NB_PORT, RSTN_NB_PIN, GPIO_PIN_SET)
#define RSTN_NB_HIG()    Gpio_WriteOutputIO(RSTN_NB_PORT, RSTN_NB_PIN, GPIO_PIN_RESET) 

#define BAT_NB_DISABLE() Gpio_WriteOutputIO(BAT_NB_PORT, BAT_NB_PIN, GPIO_PIN_RESET)
#define BAT_NB_ENABLE()  Gpio_WriteOutputIO(BAT_NB_PORT, BAT_NB_PIN, GPIO_PIN_SET)

#define PWR_NB_LOW()     Gpio_WriteOutputIO(PWR_NB_PORT, PWR_NB_PIN, GPIO_PIN_SET)
#define PWR_NB_HIG()     Gpio_WriteOutputIO(PWR_NB_PORT, PWR_NB_PIN, GPIO_PIN_RESET)

#define NB_PSM_WAKE_LOW()    Gpio_WriteOutputIO(NB_PSM_WAKE_PORT , NB_PSM_WAKE_PIN, GPIO_PIN_SET)
#define NB_PSM_WAKE_HIG()    Gpio_WriteOutputIO(NB_PSM_WAKE_PORT , NB_PSM_WAKE_PIN, GPIO_PIN_RESET)

#define MODULE_POWER_DELAY_TIME       1000  //电源导通稳定时间
#define MODULE_POWERKEY_DELAY_TIME    5000  //开机: 将 PWR_KEY 引脚拉低 2s~5s
/*-----------------端口配置定义-------------------*/
//查询的状态信息
#define TASK_BUSY                               0                   //BUSY状态
#define TASK_IDLE                               1                   //IDLE状态

//数据发送相关
#define NBIOT_ING                                0xA2                //发送中
#define NBIOT_ONLINE                             0xA3                //NBIOT上线
#define NBIOT_SUCCESS                            0xA4                //发送成功
#define NBIOT_ERROR                              0xA5                //发送失败
  
//任务完成情况
typedef enum{
	TASK_FAIL=0, 	
	TASK_SUCCESS,
	TASK_INIT,   	
}TYP_TASK_RESULT;

//NBIOT网络状态
#define NB_ON_LINE                           0x01                //在线
#define NB_OFF_LINE                          0x00                //不在线

#define CONNECT_ON_LINE                      0x01     //连接建立       
#define CONNECT_OFF_LINE                     0x00     //连接断开

//检查网络注册状态次数限制
#define NET_CGATT_MIN_TIMES  10
#define NET_CGATT_MAX_TIMES  150
/************************AT指令集配置*************************************/
//AT指令状态机执行结果
typedef enum{
	ResultOk=0, 	
	ResultBusy,
	ResultFail,   	
}TYP_RESULT_CODE;

//AT指令等待应答时间
typedef enum{
	WT_2S=2, 	
	WT_5S=5,
	WT_20S=20,
	WT_35S=35,
	WT_40S=40,
	WT_50S=50
}TYP_AT_WAIT_TIME;

//AT指令重试次数
typedef enum{
	RETRY_0=0, 
	RETRY_1, 
	RETRY_2, 
	RETRY_3, 
	RETRY_4, 
	RETRY_5,
	RETRY_15 = 15,
	RETRY_30 = 30
}TYP_AT_RETRY_COUNT;

//模组AT指令编号
typedef enum{
	AT_CODE_AT=0,  
	AT_CODE_VERCTRL,
	AT_CODE_VERCTRL1,
	AT_CODE_CMEE,   	
	AT_CODE_CGSN,   
	AT_CODE_CPIN,
	AT_CODE_CIMI,  
	AT_CODE_ICCID,   
	AT_CODE_CFUN_CK,  
	AT_CODE_CFUN0,  
	AT_CODE_CFUN1, 
	AT_CODE_CSQ, 
	AT_CODE_TUEINFO,
	AT_CODE_CCED,	
	AT_CODE_CGDCONT,
	AT_CODE_CGACT,
    AT_CODE_CEREG,	
	AT_CODE_CGACT_CK,
	AT_CODE_CCLK,
	AT_CODE_MIPMODE1,
	AT_CODE_MIPOPEN,
	AT_CODE_MCWCREATE,
	AT_CODE_MCWOPEN,
	AT_CODE_TCPSEND,
	AT_CODE_MIPCLOSE,
	AT_CODE_MIPSENDCOUNT,
	AT_CODE_MIPOPEN_UDP1,
	AT_CODE_MIPSENDCOUNT_UDP1,
	AT_CODE_UDPSEND1,
	AT_CODE_MIPCLOSE_UDP1,
	AT_CODE_MIPOPEN_UDP2,
	AT_CODE_MIPSENDCOUNT_UDP2,
	AT_CODE_UDPSEND2,
	AT_CODE_MIPCLOSE_UDP2,
	
	AT_CODE_CTEC_CK,
	AT_CODE_CCED_CK,
	
	AT_CODE_COPS_CK,
	AT_CODE_CGCONTRDP,
	AT_CODE_CGATT_CK,
	AT_CODE_NSOCR,
	AT_CODE_NSOCO,
	AT_CODE_READ_DATA,
	AT_CODE_CGDCONT_CK,
	AT_CODE_NUESTATS,
	AT_CODE_CGDCONT1,
	AT_CODE_CGDCONT3,
	AT_CODE_NSOCL,
	
	AT_CODE_MCWSEND0,
	AT_CODE_MCWSEND1,
	AT_CODE_NSOCFG,
	
	AT_CODE_NVGET,
	AT_CODE_NVSET,
	AT_CODE_NVSET0,
	AT_CODE_NRB,
	
	//onenet
	AT_CODE_MIPLCREATEEX,
	AT_CODE_MIPLADDOBJ,
	AT_CODE_MIPLDISCOVERRSP,
	AT_CODE_MIPLOPEN,
	AT_CODE_MIPLUPDATE,
	cst_AT_No_AT_MIPLOBSERVE,
	AT_CODE_MIPLNOTIFY,
	
	//下线流程
	AT_CODE_MIPLCLOSE,
	AT_CODE_MIPLDELOBJ,
	AT_CODE_MIPLDELETE,
	AT_CODE_CPOF,
	AT_CODE_NCSEARFCN,
	AT_CODE_MCWCLOSE,
	AT_CODE_MCWDELETE,
	
	AT_CODE_ALL
}TYP_AT_CMDCODE;

	
	
#define AT_CMD_COUNT  AT_CODE_ALL

typedef struct 
{
	const TYP_AT_CMDCODE AtCode;
	const char* pAtCmdStr;
	const unsigned char WaitTime; 
	unsigned char ErrorLimit;//出错次数
	const unsigned char CaseAtNum;
} STRUCT_AT_CMD; 
/********************************************************************/
//AT指令通用状态
typedef enum{
	AT_REQUEST_IDLE=0,  
	AT_REQUEST_SEND,   	
	AT_REQUEST_CHECK,
	AT_REQUEST_WT
}TYP_STATE_AT_REQUEST;

//主状态机中的状态号
typedef enum{
	S_NBIOT_MAIN_IDLE=0,  
	S_NBIOT_MAIN_POWER_INIT,   	
	S_NBIOT_MAIN_NET_OPEN,   
	S_NBIOT_MAIN_DATA_SEND,  
	S_NBIOT_MAIN_COAP_OPEN,   
	S_NBIOT_MAIN_COAP_SEND,  
	S_NBIOT_MAIN_TCP_OPEN,   
	S_NBIOT_MAIN_TCP_SEND,  
    S_NBIOT_MAIN_TCP_READ,	
	S_NBIOT_MAIN_UDP_OPEN,   
	S_NBIOT_MAIN_UDP_SEND, 
	S_NBIOT_MAIN_READ_DATA,	
	S_NBIOT_MAIN_POWER_OFF, 
//	S_NBIOT_MAIN_SET_APN
}TYP_STATE_MAIN_CONNECT;

//模组开机状态号
typedef enum{
	MODULE_IDLE=0,  
	MODULE_POWERON,   	
	MODULE_POWERON_WT, 	
	MODULE_POWER_KEY_ON_WT,
}TYP_STEP_MODULE_POWERON;
	
//模组关机状态号
typedef enum{
	S_PWOFF_IDLE=0,  
	S_PWOFF_START,
	S_PWOFF_MIPLCLOSE,   	  
	S_PWOFF_MIPLDELOBJ,   
	S_PWOFF_MIPLDELETE,   
	S_PWOFF_CPOF,  
	S_PWOFF_UDPCLOSE,  
	S_PWOFF_PW_DOWN,  
    S_PWOFF_PW_DOWN_WAIT,	
	S_PWOFF_MCWCLOSE,
	S_PWOFF_MCWDELETE,
	S_PWOFF_CFUN0,
	S_PWOFF_NCSEARFCN,
}TYP_STATE_POWER_OFF;

//模组联网初始化状态号
typedef enum{
	S_MODULE_INIT_IDLE=0,
	S_MODULE_INIT_POWER_ON,   
	S_MODULE_INIT_AT,  
	S_MODULE_INIT_VERCTRL, 
	S_MODULE_INIT_VERCTRL1,
	S_MODULE_INIT_CMEE,   	
	S_MODULE_INIT_CGSN,   
	S_MODULE_INIT_CPIN,
	S_MODULE_INIT_CIMI,  
	S_MODULE_INIT_ICCID,   
	S_MODULE_INIT_CFUN_CK,  
	S_MODULE_INIT_CFUN0,  
	S_MODULE_INIT_CFUN1, 
	S_MODULE_INIT_CSQ,  
	S_MODULE_INIT_TUEINFO, 
	S_MODULE_INIT_CCED, 
	S_MODULE_INIT_CGDCONT_PRE,
	S_MODULE_INIT_CGDCONT,
	S_MODULE_INIT_CGACT,	
    S_MODULE_INIT_CEREG_CK,	
	S_MODULE_INIT_CGACT_CK,
	S_MODULE_INIT_CCLK,
	
	S_MODULE_INIT_CTEC_CK,
	S_MODULE_INIT_CCED_CK,
	S_MODULE_INIT_CGATT_CK,
	S_MODULE_INIT_COPS_CK,
	S_MODULE_INIT_CGCONTRDP,
	S_MODULE_INIT_GDCONT_CK,
	S_MODULE_INIT_CGDCONT1,
	S_MODULE_INIT_CGDCONT3,
	S_MODULE_INIT_NUESTATS_CK,
	
	S_MODULE_INIT_NVGET,
	S_MODULE_INIT_NVSET,
	S_MODULE_INIT_NRB,
}TYP_MODULE_INIT;

//TCP连接状态号
typedef enum{
	S_TCP_CONNECT_IDLE=0,
	S_TCP_CONNECT_START,
	S_TCP_CONNECT_MIPOPEN,
	S_TCP_CONNECT_NSOCR_R
}TYP_TCP_CONNECT;

//TCP发送数据状态号
typedef enum{
	S_TCP_SEND_IDLE=0,  
	S_TCP_SEND_START,   
	S_TCP_SEND_DATALEN,	
	S_TCP_SEND_DATA
}TYP_TCP_SEND;

//UDP连接状态号
typedef enum{
	S_UDP_CONNECT_IDLE=0,
	S_UDP_CONNECT_START,
	S_UDP_CONNECT_MIPOPEN,
	S_UDP_CONNECT_NSOCFG
}TYP_UDP_CONNECT;

//UDP发送数据状态号
typedef enum{
	S_UDP_SEND_IDLE=0,  
	S_UDP_SEND_START,   
	S_UDP_SEND_DATALEN,	
	S_UDP_SEND_DATA
}TYP_UDP_SEND;

//COAP连接状态号
typedef enum{
	S_COAP_CONNECT_IDLE=0,
	S_COAP_CONNECT_START,
	S_CTWING_MCWCREATE,
	S_CTWING_MCWCREATE_R,
	S_CTWING_MCWOPEN,
	S_CTWING_MCWOPEN_R
}TYP_COAP_CONNECT;

//COAP发送数据状态号
typedef enum{
	S_COAP_SEND_IDLE=0,  
	S_COAP_SEND_START,   
	S_COAP_SEND_DATA0,	
	S_COAP_SEND_DATA1
}TYP_COAP_SEND;

//ONNET连接状态号
typedef enum{
	S_ONENET_CONNECT_IDLE=0,
	S_ONENET_CONNECT_START,
	S_ONENET_MIPLCREATEEX,
	S_ONENET_MIPLADDOBJ,
	S_ONENET_MIPLDISCOVERRSP,
	S_ONENET_MIPLOPEN,
	S_ONENET_MIPLUPDATE
}TYP_ONENET_CONNECT;

//ONENET发送数据状态号
typedef enum{
	S_ONENET_SEND_IDLE=0,  
	S_ONENET_SEND_START,   
	S_ONENET_SEND_DATA0,	
	S_ONENET_SEND_DATA1
}TYP_ONENET_SEND;

//数据读取状态号
typedef enum{
	S_READ_DATA_IDLE=0,  
	S_READ_DATA_START,   
}TYP_READ_DATA;

//通信模式选择
#define SIM_CARD_TELECOM                        0x00 //电信卡
#define SIM_CARD_MOBILE                         0X01 //移动卡

//连接通信协议
#define NBIOT_CONNECT_COAP  0
#define NBIOT_CONNECT_UDP1  1
#define NBIOT_CONNECT_UDP2  2

#define SEND_TYPE_COAP                0
#define SEND_TYPE_UDP1                1
#define SEND_TYPE_UDP2                2
#define SEND_TYPE_TCP1                3
//******************************* 结构体定义*************************************
//参数空间
#ifndef  APN_SIZE
#define APN_SIZE                                32
#endif 
typedef struct
{
    u8 *work_mode; //工作模式，一直在线模式还是时段在线模式
    u8 *ip; //IP
    u8 *spare_ip; //备用IP
    u8 *port; //端口号
    u8 *spare_port; //备用端口号
    u8 *apn; //APN
} CFIG_PARA;

//NBIOT消息

typedef union
{
    struct
    {
        u8 para_change : 1; //参数改变
        u8 station_close : 1; //主站下线
        u8 open_spare_station : 1; //开启备站
        u8 close_spare_station : 1; //关闭备站
        u8 apn_open : 1; //APN开启
        u8 rev_data : 1; //收到数据
    } s;
    u8 info;
} NBIOT_MSG;

//NBIOT相关参数

typedef struct
{
    CFIG_PARA cfig_para; //可配置参数
    u8 csq; //信号值
    u8 net_state; //网络状态
    u8 offline_reason; //掉线原因
    u8 Telecom; //运营商
    u8 spare_station; //备站
	u8 coap_state;
	u8 udp1_state;
	u8 udp2_state;
} NBIOT_PARA;

//数据发送相关

typedef struct
{
    u8 *state; //发送申请指针msg
    u8 *buf; //发送数据指针
    u16 len; //发送长度
} NBIOT_SEND_DATA;

//唤醒PSM状态机
typedef enum{
	PSM_WKUPSTA_IDLE=0,
	PSM_WKUPSTA_START,
	PSM_WKUPSTA_EINT_LOW,
	PSM_WKUPSTA_EINT_HIGH,
	PSM_WKUPSTA_QLWEVTIND6,
	PSM_WKUPSTA_QSCLK0,
	PSM_WKUPSTA_QSCLK0_R,
	PSM_WKUPSTA_NCDP_CK,
	PSM_WKUPSTA_NCDP_CK_R,
	PSM_WKUPSTA_NPING,
	PSM_WKUPSTA_NPING_R,
	PSM_WKUPSTA_NUESTATS,
	PSM_WKUPSTA_NUESTATS_R,
	PSM_WKUPSTA_WAKE_SUCCESS,
	PSM_WKUPSTA_WAKE_FAIL,
}TYP_PSM_WAKEUP_STATE;
//PSM进入休眠状态
typedef enum{
	PSM_SLPSTA_IDLE=0, 	
	PSM_SLPSTA_START,
	PSM_SLPSTA_CPSMS1_R,
	PSM_SLPSTA_WAIT,
	PSM_CHECK_START,
	PSM_CHECK_WAIT,
}TYP_PSM_SLEEP_STATE;
//PSM状态相关
typedef enum{
	PSM_STATE_OUT=0, 	
	PSM_STATE_WAKE,
	PSM_STATE_SLEEP,   	
}TYP_PSM_STATE;

//******************************* 外部变量***************************************

//******************************* 外部接口函数***********************************
void NbiotOnlineTask(u8 *state);
void NbiotSendTask(u8 *addr, u16 len, u8 send_type,u8 *state);
void NbiotUdpOpenTask(void);
void NbiotReadECLSNRTask(u8 *state);

u8 NbiotIfIdle(void);
u8 NbiotIfOnline(void);
u8 NbiotIfSleep(void);
void NbiotPreSleep(void);
void NbiotWakeSleep(void);
u8 ConncetIfOnline(u8 connect_type);
void NB_NetConnectStart(unsigned char* temp_ip, u16 temp_port, u8 connect_type);

void NbiotGetCcidTask(u8 **ccid, u8 *state);
void NbiotMainMachine(void);

u8 If_NB_Poweron(void);
void StartPowerOffTask(void);
void set_ip_port(void);
void NbForcePowerOff(void);
u8 GetNbiotSendType(void);
u8 UdpIfOnline(void);

u8 TcpIfOnline(void);
u32 sys_get_net_state(void);
void sys_set_net_state(u8 state, u8 data);

TYP_PSM_STATE getPsmState(void);
void NbiotPsmWakeupTask(void);
void NbiotPsmSleepTask(void);
void NbiotPsmCheckTask(void);
#endif	

#endif	



