All Products
Search
Document Center

IoT Platform:Example of HTTPS dynamic registration

Last Updated:Jul 15, 2026

This topic uses the./demos/dynreg_basic_demo.c demo file in the C Link SDK as an example to show you how to call Link SDK APIs to send an HTTPS request to IoT Platform, dynamically register a device, and obtain the authentication information required for device activation.

Background information

For more information about HTTPS dynamic registration, see Overview of HTTPS dynamic registration.

Step 1: Initialization

  1. Add the header files.

    #include "aiot_state_api.h"
    #include "aiot_sysdep_api.h"
    #include "aiot_dynreg_api.h"
  2. Configure underlying dependencies and log output.

        aiot_sysdep_set_portfile(&g_aiot_sysdep_portfile);
        aiot_state_set_logcb(demo_state_logcb);
  3. Call aiot_dynreg_init to create a dynreg client instance and initialize the default parameters.

        dynreg_handle = aiot_dynreg_init();
        if (dynreg_handle == NULL) {
            printf("aiot_dynreg_init failed\n");
            return -1;
        }

Step 2: Configure features

You can call aiot_dynreg_init to configure the following features.

  1. Configure connection parameters.

  2. Configure message callbacks.

  3. For more information about configuration items, see HTTPS Dynamic Registration Configuration Items.

  4. Configure connection parameters.

    • Sample code:

          char       *url = "iot-auth.cn-shanghai.aliyuncs.com"; 
          ……
          char *product_key       = "a18wP******";
          char *product_secret    = "CpIlPVCXI7******";
          char *device_name       = "LightSwitch";
          ……
          ……
          /* Configure the server address. */
          aiot_dynreg_setopt(dynreg_handle, AIOT_DYNREGOPT_HOST, (void *)host);
          /* Configure the server port. */
          aiot_dynreg_setopt(dynreg_handle, AIOT_DYNREGOPT_PORT, (void *)&port);
          /* Configure the ProductKey of the device. */
          aiot_dynreg_setopt(dynreg_handle, AIOT_DYNREGOPT_PRODUCT_KEY, (void *)product_key);
          /* Configure the ProductSecret of the device. */
          aiot_dynreg_setopt(dynreg_handle, AIOT_DYNREGOPT_PRODUCT_SECRET, (void *)product_secret);
          /* Configure the DeviceName of the device. */
          aiot_dynreg_setopt(dynreg_handle, AIOT_DYNREGOPT_DEVICE_NAME, (void *)device_name);
          /* Configure the security credentials for the network connection. */
          aiot_dynreg_setopt(dynreg_handle, AIOT_DYNREGOPT_NETWORK_CRED, (void *)&cred);
          ……
          ……
    • Related parameters:

      Parameter

      Example

      Description

      url

      iot-auth.cn-shanghai.aliyuncs.com

      The server domain name for HTTPS dynamic registration.

      You can change cn-shanghai in the example to the ID of the region where your service is deployed to obtain the endpoint for your device.

      In the upper-left corner of the IoT Platform console, view the region where your service is deployed. For more information about region IDs, see Supported regions.

      This example uses a public instance in the China (Shanghai) region.

      Note

      The China (Beijing) and China (Shenzhen) regions are not supported.

      product_key

      a18wP******

      The ProductKey and ProductSecret that are obtained when you create a product in the IoT Platform console. For more information, see Create a product.

      product_secret

      CpIlPVCXI7******

      device_name

      LightSwitch

      The name of the device. The value of this parameter must be the same as the DeviceName that you specified when you added the device to the product in the IoT Platform.

      IoT Platform verifies the DeviceName during device activation. We recommend that you use an ID that can be directly read from the device, such as the MAC address, International Mobile Equipment Identity (IMEI), or serial number (SN), as the DeviceName.

      Important

      The name must be the same as the one specified when you create the device in IoT Platform.

  5. Configure the message callback.

    1. Configure the message callback function.

      • Sample code:

         int main(int argc, char *argv[])
        {
            …… 
            ……
            aiot_dynreg_setopt(dynreg_handle, AIOT_DYNREGOPT_RECV_HANDLER, (void *)demo_dynreg_recv_handler);
            aiot_dynreg_setopt(dynreg_handle, AIOT_DYNREGOPT_USERDATA, (void *)&demo_info);
            …… 
            ……
        }
      • Related parameters:

        Configuration item

        Example value

        Description

        AIOT_DYNREGOPT_RECV_HANDLER

        demo_dynreg_recv_handler

        Sets the message callback. When a message is received, the corresponding processing is performed based on the settings of this callback function.

        AIOT_DYNREGOPT_USERDATA

        &demo_info

        Set the context to be passed back when demo_dynregmq_recv_handler is called.

    2. Define the message callback function.

      void demo_dynreg_recv_handler(void *handle, const aiot_dynreg_recv_t *packet, void *userdata)
      {
          demo_info_t *demo_info = (demo_info_t *)userdata;
          switch (packet->type) {
              case AIOT_DYNREGRECV_STATUS_CODE: {
                  demo_info->code = packet->data.status_code.code;
              }
              break;
              /* TODO: In the callback, you must save the content of the space pointed to by the packet. After the callback returns, this space is released. */
              case AIOT_DYNREGRECV_DEVICE_INFO: {
                  demo_info->device_secret = malloc(strlen(packet->data.device_info.device_secret) + 1);
                  if (demo_info->device_secret != NULL) {
                      memset(demo_info->device_secret, 0, strlen(packet->data.device_info.device_secret) + 1);
                      memcpy(demo_info->device_secret, packet->data.device_info.device_secret,
                             strlen(packet->data.device_info.device_secret));
                  }
              }
              break;
              default: {
              }
              break;
          }
      }

Step 3: Send a request

Call aiot_dynreg_send_request, which uses the Configure connection parameters to send a dynamic registration request to the server.

/* Send a dynamic registration request. */
    res = aiot_dynreg_send_request(dynreg_handle);
    if (res < STATE_SUCCESS) {
        printf("aiot_dynreg_send_request failed: -0x%04X\n", -res);
        return -1;
    }

Step 4: Receive an acknowledgement

After a registration request message is sent, IoT Platform returns an acknowledgement. The device then calls aiot_dynreg_recv to receive the acknowledgement and execute the corresponding action based on the message callback function.

    res = aiot_dynreg_recv(dynreg_handle);
    if (res < STATE_SUCCESS) {
        printf("aiot_dynreg_recv failed: -0x%04X\n", -res);
        return -1;
    }  

The sample program only performs a print operation. However, in a production environment, you must also save the returned device identity authentication information to a local file. This information is used to connect to IoT Platform each time the device logs on.

    if (demo_info.device_secret != NULL) {
        printf("device secret: %s\n", demo_info.device_secret);
        free(demo_info.device_secret);
    }

Step 5: Exit the program

Call aiot_dynreg_deinit to destroy the dynreg client instance and release the resources.

    res = aiot_dynregmq_deinit(&dynregmq_handle);           

What to do next

  • After you configure the sample file, compile it to generate the executable file ./output/dynreg-basic-demo.

    For more information, see Compile and run.

  • For a detailed description of the operation results, see the operational log.