文档

子设备动态注册

更新时间:

网关向物联网平台上报子设备的ProductKey和DeviceName进行注册。物联网平台校验子设备ProductKey和DeviceName通过后,动态下发子设备的DeviceSecret。本文介绍子设备通过网关设备完成动态注册。

前提条件

使用流程

如下功能时序图,以设备的应用程序demos/gateway_subdev_dynamic_register_demo.c为例,介绍网关模块的动态注册功能。

image

步骤1:网关设备初始化

  1. 添加头文件。

    #include "aiot_gateway_api.h"
    
        /* 配置SDK的底层依赖 */
        aiot_sysdep_init(aiot_sysdep_get_portfile());
  2. 网关设备初始化函数实现。

    该函数内包含创建设备句柄、连接参数配置、网关设备建连。

    static void* demo_gateway_device_init(char *product_key, char *device_name, char *device_secret, char *host, uint16_t port)
    {
        int32_t res = STATE_SUCCESS;
        /* 创建设备 */
        void *device = aiot_device_create(product_key, device_name);
        if (device == NULL) {
            printf("device create failed\n");
            return NULL;
        }
        /* 设置设备密钥 */
        aiot_device_set_device_secret(device, device_secret);
    
        /* 连接配置参数初始化 */
        aiot_linkconfig_t* config = aiot_linkconfig_init(protocol);
        /* 设置服务器的host、port */
        aiot_linkconfig_host(config, host, port);
    
        /* 设置设备连接参数 */
        aiot_device_set_linkconfig(device, config);
        
        /* 设备建连 */
        res = aiot_device_connect(device);
        if (res < STATE_SUCCESS) {
            /* 尝试建立连接失败, 销毁MQTT实例, 回收资源 */
            aiot_linkconfig_deinit(&config);
            aiot_device_delete(&device);
            printf("aiot_device_connect failed: -0x%04X\n\r\n", -res);
            return NULL;
        }
    
        /* 建连成功返回设备对象 */
        aiot_linkconfig_deinit(&config);
        return device;
    }
  3. 网关设备初始化。

    网关设备正常建连后,会设置网关模块的回调函数。

     /* 网关设备完成初始化,启动建连,如果失败返回NULL */
        void *gateway_device = demo_gateway_device_init(gateway_product_key, gateway_device_name, gateway_device_secret, host, port);
        if(gateway_device == NULL) {
            return -1;
        }

(可选)步骤2:子设备动态获取密钥

  1. 子设备需要先与网关设备完成绑定。

    1. 登录物联网平台控制台

    2. 实例概览页面,找到对应的实例,单击实例进入实例详情页面。

    3. 在左侧导航栏,选择设备管理 > 设备,在列表中找到网关设备。

    4. 单击网关设备对应的子设备,进入子设备管理页面。

    5. 单击添加子设备,将创建网关与子设备步骤中的子设备添加到网关下。

  2. 获取动态密钥。

    说明

    如果返回值为STATE_SUCCESS,表示获取动态密钥成功,密钥保存在sub_device_meta.device_secret中。

     /* 子设备动态获取密钥,返回密钥直接写进sub_device_meta中 */
        res = aiot_gateway_dynamic_secret(gateway_device, sub_device_meta, subdev_num);
        if(res < STATE_SUCCESS) {
            printf("aiot_gateway_dynamic_secret error %d\r\n", res);
            return -1;
        }

(可选)步骤3:子设备动态注册

获取子设备动态密钥,但子设备与网关设备不需要提前绑定,仅使用子设备的产品密钥。

说明

如果返回值为STATE_SUCCESS,表示子设备动态注册成功,密钥保存在sub_device_meta.device_secret中。

    /* 子设备动态注册,返回密钥直接写进sub_device_meta中 */
    res = aiot_gateway_dynamic_register(gateway_device, sub_device_meta, subdev_num);
    if(res < STATE_SUCCESS) {
        printf("aiot_gateway_dynamic_register error %d\r\n", res);
        return -1;
    }

步骤4:网关设备反初始化

static void demo_gateway_device_deinit(void *device)
{
    int32_t res = STATE_SUCCESS;
    res = aiot_device_disconnect(device);
    if (res < STATE_SUCCESS) {
        printf("aiot_device_disconnect failed: -0x%04X\n", -res);
    }
    aiot_device_delete(&device);
}
  • 本页导读 (1)
文档反馈