All Products
Search
Document Center

IoT Platform:Example

Last Updated:Jul 07, 2023

This topic describes how to call the API operations of Link SDK for C to send HTTPS requests to IoT Platform and dynamically register a device and obtain the verification information that is required to activate the device. In this example, a sample code file named ./demos/dynreg_basic_demo.c is used.

Background information

For more information about HTTPS-based dynamic registration, see Overview.

Step 1: Initialize a client instance

  1. Add header files.

    #include "aiot_state_api.h"
    #include "aiot_sysdep_api.h"
    #include "aiot_dynreg_api.h"
  2. Add the underlying dependency and configure the log output feature.

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

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

Step 2: Configure the required features

Call the aiot_dynreg_init function to configure the following items:

  1. Configure connection parameters.

  2. Configure a message callback.

  3. For more information, see HTTPS-based dynamic registration.

  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";
          ……
          ……
          /* Specify the endpoint of the server.  */
          aiot_dynreg_setopt(dynreg_handle, AIOT_DYNREGOPT_HOST, (void *)host);
          /* Specify the port of the server.  */
          aiot_dynreg_setopt(dynreg_handle, AIOT_DYNREGOPT_PORT, (void *)&port);
          /* Specify the ProductKey of the product to which the device belongs.  */
          aiot_dynreg_setopt(dynreg_handle, AIOT_DYNREGOPT_PRODUCT_KEY, (void *)product_key);
          /* Specify the ProductSecret of the product to which the device belongs.  */
          aiot_dynreg_setopt(dynreg_handle, AIOT_DYNREGOPT_PRODUCT_SECRET, (void *)product_secret);
          /* Specify the DeviceName of the device.  */
          aiot_dynreg_setopt(dynreg_handle, AIOT_DYNREGOPT_DEVICE_NAME, (void *)device_name);
          /* Specify the security credential of the connection.  */
          aiot_dynreg_setopt(dynreg_handle, AIOT_DYNREGOPT_NETWORK_CRED, (void *)&cred);
          ……
          ……
    • Parameters:

      Parameter

      Example

      Description

      url

      iot-auth.cn-shanghai.aliyuncs.com

      The endpoint of the HTTPS server.

      Replace cn-shanghai in the sample code with the ID of the region where IoT Platform is activated to obtain the endpoint to which you want to connect your device.

      You can view the region in the upper-left corner of the IoT Platform console. For more information about region IDs, see .Supported regions.

      In this example, a public instance that resides in the China (Shanghai) region is used.

      Note

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

      product_key

      a18wP******

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

      product_secret

      CpIlPVCXI7******

      device_name

      LightSwitch

      The DeviceName 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 checks the DeviceName when a device initiates an activation request. We recommend that you use an identifier that can be obtained from the device as the DeviceName. The identifier can be the MAC address, International Mobile Equipment Identity (IMEI) number, or serial number (SN) of the device.

      Important

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

  5. Configure a message callback.

    1. Specify a 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);
            …… 
            ……
        }
      • Parameters:

        Parameter

        Example

        Description

        AIOT_DYNREGOPT_RECV_HANDLER

        demo_dynreg_recv_handler

        The message callback function. When a message is received, the callback function is called to perform the required operations.

        AIOT_DYNREGOPT_USERDATA

        &demo_info

        The context. When the demo_dynregmq_recv_handler function is called, the value obtained from the function is returned.

    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: You must save the space to which the packet parameter in the callback points. After the callback is implemented, the 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 the aiot_dynregmq_send_request operation to send a dynamic registration request to the server. For information about how to configure the parameters, see Configure connection parameters.

/* 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 a response

After the registration request is sent, IoT Platform returns a response. The device calls the aiot_dynreg_recv operation to receive the response data and then calls the message callback function to process the data.

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

In this example, the response data is printed. You must specify the logic to save the returned device verification information on premises. The verification information is used when the device connects to IoT Platform.

    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 the aiot_dynreg_deinit function to delete the dynreg client instance and release the related resources.

    res = aiot_dynregmq_deinit(&dynregmq_handle);
            

What to do next

  • After you configure the sample code file, compile the file to generate an executable file. In this example, an executable file named ./output/mqtt-basic-demo is generated.

    For more information, see Prepare an environment.

  • For more information about running results, see View logs.