在前面的文章:
《阿里云IOT-C-SDK系列(1)概述:移植流程、程序框架、代码目录》
《阿里云IOT-C-SDK系列(2)快速体验:移植+示例C代码》
《阿里云IOT-C-SDK系列(2)快速体验:移植+示例C代码》
《阿里云IOT-C-SDK系列(5):进一步理解SDK的移植使用方式》
我们是从 移植、应用的角度分析了 阿里云iot c-sdk的移植和使用方式,但是想要深入的理解该SDK的工作原理,最直接有效的方式就是阅读源码。本文及后续的文章将会分析C-SDK的源码脉络,理解其工作原来。
linkkit SDK是阿里云物联网平台的开发 SDK,简单的说,使用该SDK,可以方便的实现 单设备、网关设备连接到阿里云物联网平台上,在前面的文章也分析过,单设备联网相对比较简单,本文及后续的文章主要还是从网关设备的角度来分析。
要理解工作原理,首先要了解一下使用该SDK进行开发应用程序的流程 步骤,下面就简单的用伪代码 罗列一下 网关设备应用程序 的 步骤:
/* 子设备四元组 信息*/
#define EXAMPLE_SUBDEV_ADD_NUM 3
#define EXAMPLE_SUBDEV_MAX_NUM 20
const iotx_linkkit_dev_meta_info_t subdevArr[EXAMPLE_SUBDEV_MAX_NUM] = {
{
"a13Npv1vjZ4",
"PKbZL7baK8pBso94",
"example_sub1",
"eglNFNJiRuR0yncB9RP05sSTY4FrUIoe"
},
{
"a1YRfb9bepk",
"PKbZL7baK8pBso94",
"test_02",
"jFsErM3uA7UfbS6J0hm0QaEXsQbmO6Pa"
},
{
"a1YRfb9bepk",
"PKbZL7baK8pBso94",
"test_03",
"MjWMvCLBcuZyqUswryBbgypN8uOgJGVD"
}
};
static user_example_ctx_t *user_example_get_ctx(void)
{
return &g_user_example_ctx;
}
/* 用户 连接云服务器成功 回调函数 */
static int user_connected_event_handler(void)
{
user_example_ctx_t *user_example_ctx = user_example_get_ctx();
EXAMPLE_TRACE("Cloud Connected");
user_example_ctx->cloud_connected = 1;
return 0;
}
/* 用户 云服务器断开连接 回调函数 */
static int user_disconnected_event_handler(void)
{
user_example_ctx_t *user_example_ctx = user_example_get_ctx();
EXAMPLE_TRACE("Cloud Disconnected");
user_example_ctx->cloud_connected = 0;
return 0;
}
/* 用户自定义 属性设定 回调函数 */
static int user_property_set_event_handler(const int devid, const char *request, const int request_len)
{
int res = 0;
user_example_ctx_t *user_example_ctx = user_example_get_ctx();
EXAMPLE_TRACE("Property Set Received, Devid: %d, Request: %s", devid, request);
res = IOT_Linkkit_Report(devid, ITM_MSG_POST_PROPERTY,
(unsigned char *)request,