阿里云IOT-SDK源码历程分析

文件路径:src/mqtt/examples/mqtt_example.c
阿里云快速体验手册:
https://help.aliyun.com/document_detail/96624.html?spm=a2c4g.11186623.2.15.69a27165DXIxEK

我们使用“以MQTT Topic编程方式接入设备”

直接使用MQTT TOPIC与物联网平台通信的流程示意图如下:

在这里插入图片描述

1、创建产品和设备

参考博客:

https://blog.csdn.net/cainiaofu/article/details/114187186?spm=1001.2014.3001.5501

2、产品功能实现

1)、在文件wrappers/os/ubuntu/HAL_OS_linux.c中修改三元组信息。
2)、定制化mqtt参数

iotx_mqtt_param_t       mqtt_params;
memset(&mqtt_params, 0x0, sizeof(mqtt_params));
// MQTT消息发送的超时时间*
/*mqtt_params.request_timeout_ms = 2000; /
//选择是否使用MQTT协议的clean session特性
/* mqtt_params.clean_session = 0; */
// MQTT心跳超时时间
/* mqtt_params.keepalive_interval_ms = 60000; */
// MQTT消息发送buffer最大长度
/* mqtt_params.write_buf_size = 1024; */
// MQTT消息接收buffer最大长度
/* mqtt_params.read_buf_size = 1024; */
//用户回调函数, 用与接收MQTT模块的事件信息
mqtt_params.handle_event.h_fp = example_event_handle;

说明:注释的是mqtt的默认参数。如果要修改,取消注释即可
3)、尝试建立与服务器的MQTT连接。

pclient = IOT_MQTT_Construct(&mqtt_params);
if (NULL == pclient) {
    EXAMPLE_TRACE("MQTT construct failed");
    return -1;
}

说明:
a、函数IOT_MQTT_Construct的返回值:成功,即为易筋经建立连接的句柄;失败:NULL
b、函数IOT_MQTT_Construct的参数是iotx_mqtt_param_t类型的结构体。结构体的内容如下:

typedef struct {
    uint16_t                   port;
    const char                 *host;
    const char                 *client_id;
    const char                 *username;
    const char                 *password;
    const char                 *pub_key;
    const char                 *customize_info;
    uint8_t                    clean_session;
    uint32_t                   request_timeout_ms;
    uint32_t                   keepalive_interval_ms;
    uint32_t                   write_buf_size;
    uint32_t                   read_buf_size;
    iotx_mqtt_event_handle_t    handle_event;
} iotx_mqtt_param_t, *iotx_mqtt_param_pt;

成员的含义如下:

port: 云端服务器端口
host: 云端服务器地址
client_id: MQTT客户端ID
username: 登录MQTT服务器用户名
password: 登录MQTT服务器密码
pub_key: MQTT连接加密方式及密钥
clean_session: 选择是否使用MQTT协议的clean session特性
request_timeout_ms: MQTT消息发送的超时时间
keepalive_interval_ms: MQTT心跳超时时间
write_buf_size: MQTT消息发送buffer最大长度
read_buf_size: MQTT消息接收buffer最大长度
handle_event: 用户回调函数, 用与接收MQTT模块的事件信息
customize_info: 用户自定义上报信息,是以逗号为分隔符kv字符串,如用户的厂商信息,模组信息自定义字符串为"pid=123456,mid=abcd";

4)、订阅指定的topic,即表示可以接收运平台上,哪些Topic的报文
函数原型:

int IOT_MQTT_Subscribe(void *handle,
const char *topic_filter,iotx_mqtt_qos_t qos,
iotx_mqtt_event_handle_func_fpt topic_handle_func,
void *pcontext);

参数说明:

handle	                : MQTT句柄,可为NULL,可为函数IOT_MQTT_Construct返回值。
topic_filter	        :需要订阅的topic
qos	iotx_mqtt_qos_t	:采用的QoS策略
topic_handle_func	:用于接收MQTT消息的回调函数
pcontext				:用户Context, 会通过回调函数送回

返回值:
0 :成功
<0:失败
如果订阅失败,调用函数IOT_MQTT_Destroy,销毁指定MQTT连接并释放资源。

int IOT_MQTT_Destroy(void **phandle);

下面开始介入循环处理阶段:
5)、上报数据到云端,即向指定topic推送消息
核心函数:

IOT_MQTT_Publish_Simple或IOT_MQTT_Publish()

函数原型:

int IOT_MQTT_Publish_Simple(void *handle, const char *topic_name, int qos, void *data, int len)

参数说明:

handle      :MQTT句柄,可为NULL
topic_name :接收此推送消息的目标topic
qos			 :采用的QoS策略
data	     :需要发送的数据
len	         :数据长度

返回值:

> 0	:成功(消息是QoS1时, 返回值就是这个上报报文的MQTT消息ID, 对应协议里的messageId)
0	:成功(消息是QoS0时)
< 0	:失败

6)、调用函数IOT_MQTT_Yield,用于接收云端下发的消息, 并调用用户在 IOT_MQTT_Subscribe() 时指定的回调函数,用于对数据进行处理。内含了心跳的维持, 服务器下行报文的收取等。
函数原型:

int IOT_MQTT_Yield(void *handle, int timeout_ms);

参数说明:

handle		:MQTT句柄,可为NULL
timeout_ms	:尝试接收报文的超时时间

返回值:
0:成功
SDK提供的用户API接口都列在 src/mqtt/mqtt_api.h, 可能需要对接的HAL函数都列在 src/mqtt/mqtt_wrapper.h, 以代码为准

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

One Piece&

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值