1. LiteOS OC MQTT 抽象组件
概述
为了适应各种各样的使用mqtt接入华为OC的模式,特采用该层次接口,对上提供应用所需的接口,对下允许接入方式的灵活适配。
OC MQTT AL的api接口声明在
中,使用相关的接口需要包含该头文件。
配置并连接
对接服务器的所有信息保存在结构体oc_mqtt_config_t
中,其定义在oc_mqtt_al.h
中,如下:
typedef struct
{
en_oc_mqtt_mode boot_mode; //对接模式:直连模式和bs模式
uint8_t lifetime; //保持连接时长
char *server_addr; //mqtt服务器地址,ip或者域名
char *server_port; //mqtt服务器端口
en_mqtt_al_security_t sec_type; //当前仅支持crt模式
char *id; //设备对接ID,默认使用NOTEID(设备标识码)对接
char *pwd; //设备秘钥
//int device_mode; //未使用
fn_oc_mqtt_msg_deal msg_deal;//接收到数据的回调函数
void *msg_deal_arg; //回调函数参数
}oc_mqtt_config_t;
其中boot_mode
是对接模式,对应华为平台的两种模式:
typedef enum
{
en_oc_mqtt_mode_bs_static_nodeid_hmacsha256_notimecheck_json =0,
en_oc_mqtt_mode_nobs_static_nodeid_hmacsha256_notimecheck_json,
en_oc_mqtt_mode_last,
}en_oc_mqtt_mode;
本实验中使用的是直连模式,选择第二种。
在配置结构体完成之后,调用配置函数进行配置并连接,API如下:
/**
* @brief the application use this function to configure the mqtt agent
*
* @param[in] param, refer to oc_mqtt_config_t
*
* @return code: define by en_oc_mqtt_err_code while 0 means success
*/
int oc_mqtt_config(oc_mqtt_config_t *param);
函数参数很清楚,将存放对接信息的结构体指针传入即可,API的返回值是由en_oc_mqtt_err_code
定义的枚举类型,方便定位问题:
typedef enum
{
en_oc_mqtt_err_ok = 0, ///< this means the status ok
en_oc_mqtt_err_parafmt, ///< this means the parameter err format
en_oc_mqtt_err_network, ///< this means the network wrong status
en_oc_mqtt_err_conversion, ///< this means the mqtt version err
en_oc_mqtt_err_conclientid, ///< this means the client id is err
en_oc_mqtt_err_conserver, ///< this means the server refused the service for some reason(likely the id and pwd)
en_oc_mqtt_err_conuserpwd, ///< bad user name or passwd
en_oc_mqtt_err_conclient, ///< the client id /user/pwd is right, but does not allowed
en_oc_mqtt_err_subscribe, ///< this means subscribe the topic failed
en_oc_mqtt_err_publish, ///< this means subscribe the topic failed
en_oc_mqtt_err_configured, ///< this means we has configured, please deconfigured it and then do configure again
en_oc_mqtt_err_noconfigured, ///< this means we have not configure it yet,so could not connect
en_oc_mqtt_err_noconected, ///< this means the connection has not been built, so you could not send data
en_oc_mqtt_err_gethubaddrtimeout, ///< this means get the hub address timeout
en_oc_mqtt_err_sysmem, ///< this means the system memory is not enough
en_oc_mqtt_err_system, ///< this means that the system porting may have some problem,maybe not install yet
en_oc_mqtt_err_last,