All Products
Search
Document Center

IoT Platform:Remote Configuration

Last Updated:Sep 03, 2023

The IoT platform can send the remote configuration information to a device, and a device can also query the remote configuration information from the platform. If the remote configuration information was published by IoT platform while the device is offline, the device can obtain the information by querying.

For more information about remote configuration, refer to Remote Configuration

The function of the server actively distributing the remote configuration information to the device is now explained step by step with reference to the examples/linkkit/linkkit_example_cota.c process
  1. Define and register the callback function when receiving the remote configuration information

/* Define the callback function */
static int user_cota_event_handler(int type, const char *config_id, int config_size, const char *get_type,
                                   const char *sign, const char *sign_method, const char *url)
{
    char buffer[128] = {0};
    int buffer_length = 128;
    user_example_ctx_t *user_example_ctx = user_example_get_ctx();

    if (type == 0) {
        EXAMPLE_TRACE("New Config ID: %s", config_id);
        EXAMPLE_TRACE("New Config Size: %d", config_size);
        EXAMPLE_TRACE("New Config Type: %s", get_type);
        EXAMPLE_TRACE("New Config Sign: %s", sign);
        EXAMPLE_TRACE("New Config Sign Method: %s", sign_method);
        EXAMPLE_TRACE("New Config URL: %s", url);

        IOT_Linkkit_Query(user_example_ctx->master_devid, ITM_MSG_QUERY_COTA_DATA, (unsigned char *)buffer, buffer_length);
    }

    return 0;
}

/* Register the callback function */
int linkkit_main(void *paras)
{
...
IOT_RegisterCallback(ITE_COTA, user_cota_event_handler);
...
}
  1. Set up trituple

    memset(&master_meta_info, 0, sizeof(iotx_linkkit_dev_meta_info_t));
    memcpy(master_meta_info.product_key, PRODUCT_KEY, strlen(PRODUCT_KEY));
    memcpy(master_meta_info.product_secret, PRODUCT_SECRET, strlen(PRODUCT_SECRET));
    memcpy(master_meta_info.device_name, DEVICE_NAME, strlen(DEVICE_NAME));
    memcpy(master_meta_info.device_secret, DEVICE_SECRET, strlen(DEVICE_SECRET));

3. To establish configuration with the cloud, call iot_ioctl () for the related configuration. See the corresponding API descriptions for details.

/* Choose Login Server, domain should be configured before IOT_Linkkit_Open() */
#if USE_CUSTOME_DOMAIN
    IOT_Ioctl(IOTX_IOCTL_SET_MQTT_DOMAIN, (void *)CUSTOME_DOMAIN_MQTT);
    IOT_Ioctl(IOTX_IOCTL_SET_HTTP_DOMAIN, (void *)CUSTOME_DOMAIN_HTTP);
#else
    int domain_type = IOTX_CLOUD_REGION_SHANGHAI;
    IOT_Ioctl(IOTX_IOCTL_SET_DOMAIN, (void *)&domain_type);
#endif

    /* Choose Login Method */
    int dynamic_register = 0;
    IOT_Ioctl(IOTX_IOCTL_SET_DYNAMIC_REGISTER, (void *)&dynamic_register);
  1. The primary device establishes a connection

/* Create Master Device Resources */
    user_example_ctx->master_devid = IOT_Linkkit_Open(IOTX_LINKKIT_DEV_TYPE_MASTER, &master_meta_info);
    if (user_example_ctx->master_devid < 0) {
        EXAMPLE_TRACE("IOT_Linkkit_Open Failed\n");
        return -1;
    }
        /* Start Connecting Aliyun Server */
    res = IOT_Linkkit_Connect(user_example_ctx->master_devid);
    if (res < 0) {
        EXAMPLE_TRACE("IOT_Linkkit_Connect Failed\n");
        return -1;
    }

IOT_Linkkit_Connect will subscribe to the remote configuration related topic, therefore the device can receive the remote configuration information when it is released by the IoT platform.

Process explanation 2: The device queries the platform for remote configuration information

With a few changes to the above example after step 4, a device can actively obtain remote configuration information from the server.

IOT_Linkkit_Query(user_example_ctx->master_devid, ITM_MSG_REQUEST_COTA, NULL, 0);

When IOT_Linkkit_Query queries the IoT platform for remote configuration information, if the information exists on the server, it is sent to the device and the user_cota_event_handler will be called.

HAL that needs to be implemented

None