Remote Configuration

更新时间:
复制 MD 格式

The server-side can push remote configurations to a device. A device can also query the server-side for remote configurations. If the server-side publishes a configuration while a device is offline, the device can query for the updated configuration later.

Before connecting to Alibaba Cloud IoT Platform, register a handler for the ITE_COTA event. When the configuration is modified in the cloud, the software development kit (SDK) calls the registered handler. The handler then uses IOT_Linkkit_Query to request the Configuration Over-the-Air (COTA) data.

1. Define a COTA event callback and get configuration data

The following sample code shows a callback function that uses IOT_Linkkit_Query to request COTA data:




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;
}               

2. Register the COTA event callback

The following is sample code:




int main(int argc, char **argv)
{
    char buffer[128] = {0};
    int buffer_length = 128;
    ...

    IOT_RegisterCallback(ITE_COTA, user_cota_event_handler);

    ...

    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));

    ...

    res = IOT_Linkkit_Connect(user_example_ctx->master_devid);
    if (res < 0) {
        EXAMPLE_TRACE("IOT_Linkkit_Connect Failed\n");
        return -1;
    }

...
}

Actively query for remote configurations from a device

When a device actively requests COTA data, you must still register the callback function for COTA events. See the code samples above for details. After the device connects to Alibaba Cloud IoT Platform, call IOT_Linkkit_Query to request the cloud to push the COTA data.

The following is sample code:




int main(int argc, char **argv)
{
    ...

    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;
    }
    res = IOT_Linkkit_Connect(user_example_ctx->master_devid);
    if (res < 0) {
        EXAMPLE_TRACE("IOT_Linkkit_Connect Failed\n");
        return -1;
    }

    /*Request the cloud to push COTA data*/
    IOT_Linkkit_Query(user_example_ctx->master_devid, ITM_MSG_REQUEST_COTA, NULL, 0);

   ...
}

Remote configuration APIs

For more information, see the API reference.

Required HAL

None