Device distribution

Updated at:

IoT Platform uses device distribution to provision devices across regions, instances, or accounts. After a device receives and stores the connection information, it connects to the new endpoint. This eliminates the need to re-flash the device. This topic describes how to use the device distribution feature.

Prerequisites

Procedure

The following time series chart shows the device distribution flow. The device application demos/device_bootstrap_demo.c is used as an example.

image

Step 1: Initialize the device

  1. Add the header file.

    #include "aiot_device_api.h"
  2. Create a device handle.

    void *device_client = aiot_device_create(product_key, device_name);
    if (device_client == NULL) {
        printf("device_client failed\n");
        return -1;
    }

Step 2: Send a device distribution request

You can call the aiot_device_bootstrap_request interface to request device distribution.

Note
  • This is a synchronous request interface. Set an appropriate timeout period to prevent blocking.

  • connect_info contains the host and port of the server.

res = aiot_device_bootstrap_request(device_client, MQTT_PROTOCOL, timeout_ms, &connect_info);
if(res == STATE_SUCCESS){
 printf("update host %s,port %d\r\n", connect_info.host, connect_info.port);
}

(Optional) Step 3: Establish a device connection

After you obtain the host and port, establish an MQTT connection to IoT Platform.

/* Initialize the connection configuration parameters. */
aiot_linkconfig_t* config = aiot_linkconfig_init(protocol);
/* Set the host and port of the server. */
aiot_linkconfig_host(config, connect_info.host, connect_info.port);

/* Set the device connection parameters. */
aiot_device_set_linkconfig(device_client, config);
/* Establish a device connection. */
res = aiot_device_connect(device_client);

Step 4: Delete the device handle

    /* Destroy the device instance. This part of the code is generally not executed. */
    aiot_device_delete(&device_client);
    aiot_linkconfig_deinit(&config);