LiteOS云端对接教程05-LiteOS基于MQTTS对接华为OC平台实战

1. LiteOS OC MQTT 抽象组件概述为了适应各种各样的使用mqtt接入华为OC的模式,特采用该层次接口,对上提供应用所需的接口,对下允许接入方式的灵活适配。OC MQTT AL的api接口声明在中,使用相关的接口需要包含该头文件。配置并连接对接服务器的所有信息保存在结构体oc_mqtt_config_t中,其定义在oc_mqtt_al.h中,如下:typedef struc...
摘要由CSDN通过智能技术生成

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,
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值