1. LiteOS MQTT组件
概述
MQTT AL用来解耦基于MQTT的业务和MQTT的具体实现,具体来说以后的MQTT业务层应该有且只能使用MQTT AL提供的相关功能(API 数据结构 流程等)。MQTT AL定义MQTT的标准,用来屏蔽各个MQTT协议实现的差异(如软件库 或者硬件),让上层业务无需关心MQTT的实现部分。
MQTT AL的api接口声明在 中,使用相关的接口需要包含该头文件,关于函数的详细参数请参考该头文件的声明。
配置并连接
对接服务器的所有信息保存在结构体mqtt_al_conpara_t
中,其定义在mqtt_al.h
中,如下:
/** @brief defines the paramter for the mqtt connect */
typedef struct
{
mqtt_al_string_t serveraddr; ///< mqtt server:support domain name and dot format
int serverport; ///< mqtt server port
mqtt_al_security_para_t *security; ///< if NULL,will use en_mqtt_security_none
en_mqtt_al_verison version; ///< mqtt version will be used
mqtt_al_string_t clientid; ///< mqtt connect client identifier
mqtt_al_string_t user; ///< mqtt connect user
mqtt_al_string_t passwd; ///< mqtt connect passwd
int cleansession; ///< 1 clean the session while 0 not
mqtt_al_willmsg_t *willmsg; ///< mqtt connect will message
unsigned short keepalivetime;///< keep alive time
char conret; ///< mqtt connect code, return by server
int timeout; ///< how much time will be blocked
}mqtt_al_conpara_t;
其中的一些参数值已经使用枚举给出:
- security:安全连接参数(使用此需要确保mbedtls组件开启)
枚举值如下:
/** @brief this enum all the transport encode we support now*/
typedef enum
{
en_mqtt_al_security_none = 0, ///< no encode
en_mqtt_al_security_psk, ///< use the psk mode in transport layer
en_mqtt_al_security_cas, ///< use the ca mode in transport layer,only check the server
en_mqtt_al_security_cacs, ///< use the ca mode in transport layer,both check the server and client
en_mqtt_al_security_end, ///< the end for the mqtt
}en_mqtt_al_security_t;
- version:使用的MQTT协议版本
枚举值如下:
/** @brief enum the mqtt version*/
typedef enum
{
en_mqtt_al_version_3_1_0 = 0,
en_mqtt_al_version_3_1_1,
}en_mqtt_al_verison;
另外,在复制的时候还需要注意,很多字符串参数都是使用mqttalstring_t类型,其定义如下:
/** brief defines for all the ascii or data used in the mqtt engine */
typedef struct
{
char *data; ///< buffer to storage the data
int len; ///< buffer data length
}mqtt_al_string_t; //used to represent any type string (maybe not ascii)
在配置结构体完成之后,调用配置函数进行配置并连接,API如下:
/**
*@brief: you could use this function to connect to the mqtt server
*
*@param[in] conparam the parameter we will use in connect, refer to the data mqtt_al_conpara_t
*@
*@return: first you should check the return value then the return code in conparam
*
*@retval NULL which means you could not get the connect to the server,maybe network reason
*@retval handle, which means you get the context,