#ifndef _SR_NET_COAP_H_
#define _SR_NET_COAP_H_

#include "sr_net_adapter.h"

typedef struct SR_COAP_CLIENT  SR_COAP_CLIENT_T;


/**
 *  @brief CoAP消息类型枚举
 */
typedef enum
{
    MSG_CON = 0x0,    /**< Confirmable message */
    MSG_NON = 0x1,    /**< Non-confirmable message */
    MSG_ACK = 0x2,    /**< Acknowledgement message */
    MSG_RST = 0x3     /**< Reset message */
}SR_COAP_MSG_TYPE_E;


/**
 *  @brief 响应枚举：成功
 */
typedef enum
{
    RSP_CREATED = 1,                 /**< Created success response */
    RSP_DELETED = 2,                 /**< Deleted success response */
    RSP_VALID = 3,                   /**< Valid success response */
    RSP_CHANGED = 4,                 /**< Changed success response */
    RSP_CONTENT = 5,                 /**< Content success response */
    RSP_CONTINUE = 31                /**< Continue success response */
}
SR_COAP_RSP_SUCSS_T;

/**
 *  @brief 响应码枚举：客户端错误
 */
typedef enum
{
    RSP_BAD_REQ = 0,                  /**< Bad request client error */
    RSP_UNAUTHORIZED = 1,             /**< Unauthorized client error */
    RSP_BAD_OPTION = 2,               /**< Bad option client error */
    RSP_FORBIDDEN = 3,                /**< Forbidden client error */
    RSP_NOT_FOUND = 4,                /**< Not found client error */
    RSP_METHOD_NOT_ALLOWED = 5,       /**< Method not allowed client error */
    RSP_NOT_ACCEPTABLE = 6,           /**< Not acceptable client error */
    RSP_INCOMPLETE = 8,               /**< Request entity incomplete client error */
    RSP_PRECOND_FAILED = 12,          /**< Precondition failed client error */
    RSP_REQ_ENT_TOO_LARGE = 13,       /**< Request entity too large client error */
    RSP_UNSUP_CONT_FMT = 15           /**< Unsupported content-format client error */
}
SR_COAP_RSP_CLTERR_T;

/**
 *  @brief 响应码枚举：服务器错误
 */
typedef enum
{
    RSP_INT_SERVER_ERR = 0,           /**< Internal server error */
    RSP_NOT_IMPL = 1,                 /**< Not implemented server error */
    RSP_BAD_GATEWAY = 2,              /**< Bad gateway server error */
    RSP_SERV_UNAVAIL = 3,             /**< Service unavailable server error */
    RSP_GATEWAY_TIMEOUT = 4,          /**< Gateway timeout server error */
    RSP_PROXY_NOT_SUP = 5             /**< Proxying not supported server error */
}
SR_COAP_RSP_SRVERR_T;



/**  
* @brief COAP 请求方法类型枚举
*/
typedef enum SR_COAP_METHED
{
  COAP_MTD_GET =0,   /*!<GET*/
  COAP_MTD_POST,     /*!<POST*/
  COAP_MTD_PUT,      /*!<PUT*/
  COAP_MTD_DEL,      /*!<DELETE*/
  COAP_MTD_MAX
}SR_COAP_METHED_E;


/**  
* @brief COAP消息结构体
*/
typedef struct SR_COAP_MSG 
{
    SR_COAP_MSG_TYPE_E type; /*!<消息类型*/ 
    uint16_t  code;          /*!<请求方法或响应码*/
    uint8_t *data; 		     /*!<数据*/     
    uint16_t dataLen;        /*!<数据长度*/           		
}SR_COAP_MSG_T;





/**  
* @brief COAP事件类型
*/
typedef enum
{
  COAP_RECV_DLD_DATA=0,     /*!< 收到平台下行数据 */


}SR_COAP_EVT_E;


/**
* @brief COAP事件通知回调函类型
* 
* @param[in] coapc   COAP Client指针
* @param[in] evt     事件类型枚举
* @param[in] msg     COAP消息，里面携带数据
*
* @return 无
*/
typedef void (*CoapEvtCb)(SR_COAP_CLIENT_T *coapc,SR_COAP_EVT_E evt,SR_COAP_MSG_T *msg);


/**  
* @brief MQTT初始化参数结构体
*/
typedef struct SR_COAP_INIT
{
    char *srvIP;         /*!<服务器地址*/
    uint16_t srvPort;    /*!<服务器端口*/
    uint8_t  isBypass;   /*!<是否旁路，使用模组CoAP透传的方式*/

}SR_COAP_INIT_T;


/**  
* @brief CoAP Client 结构体
*/
typedef struct SR_COAP_CLIENT
{ 
    SR_NET_ADP_T *adp;               /*!< 网络适配器 */
    void *thdData;                   /*!< 第三方平台私有数据，通常为控制句柄*/
    unsigned int nextMsgId;          /*!< 报文ID */
    SR_COAP_INIT_T initParams;       /*!< 初始化参数 */
    CoapEvtCb evtCb;                 /*!< 事件通知回调 */ 

}SR_COAP_CLIENT_T;

/***********************************************************CoAP抽象接口************************************************/
/**
* @brief 初始化CoAP客户端
* 
* @param[in] adp        网络适配器指针
* @param[in] coapc      CoAP客户端对象指针
* @param[in] params     CoAP客户端初始化参数
* 
* @return 0:成功   负值错误码:失败
*/
int SR_NetCoapcInit(SR_NET_ADP_T *adp, SR_COAP_CLIENT_T *coapc,SR_COAP_INIT_T *params);


/**
* @brief 去初始化CoAP客户端
* 
* @param[in] coapc     CoAP客户端对象指针
* 
* @return 0:成功   负值错误码:失败
*/
int SR_NetCoapcDeinit(SR_COAP_CLIENT_T *coapc);


/**
* @brief 添加加请求消息option
* 
* @param[in] coapc     CoAP客户端对象指针
* 
* @return 0:成功   负值错误码:失败
*/
int coap_msg_add_op(coap_msg_t *msg, unsigned num, unsigned len, const char *val);



/**
* @brief CoAP客户端发送消息
* 
* @param[in] coapc      CoAP客户端对象指针
* @param[in] msg        CoAP 消息
* 
* @return 0:成功   负值错误码:失败
*/
int SR_NetCoapcSend(SR_COAP_CLIENT_T *coapc,SR_COAP_MSG_T *msg);


/**
* @brief CoAP客户端透传发送
* @note  仅bypass 模式下使用
*
* @param[in] coapc      CoAP客户端对象指针
* @param[in] data       CoAP 透传数据
* @param[in] dataLen    CoAP 透传数据长度
* 
* @return 0:成功   负值错误码:失败
*/
int SR_NetCoapcBypassSend( SR_COAP_CLIENT_T *coapc,uint8_t data,uint8_t dataLen);




#endif