All Products
Search
Document Center

IoT Platform:Example

Last Updated:Oct 07, 2023

This topic describes how to call the API operations of Link SDK for C to submit device tags to IoT Platform or delete device tags. In this example, a sample code file named ./demos/devinfo_posix_demo.c is used.

Background information

  • For more information about device tags, see Overview.

  • The device tag feature is available only for devices that are connected to IoT Platform over Message Queuing Telemetry Transport (MQTT). For more information about the MQTT connection-related code for configuring the feature, see Overview.

Step 1: Initialize a client

  1. Add header files.

    ……
    ……
    
    #include "aiot_devinfo_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_devinfo_init operation to create a client instance named Devinfo and initialize the default parameters.

        devinfo_handle = aiot_devinfo_init();
        if (devinfo_handle == NULL) {
            demo_mqtt_stop(&mqtt_handle);
            printf("aiot_devinfo_init failed\n");
            return -1;
        }

Step 2: Configure required features

Call the aiot_devinfo_setopt operation to configure the following features:

  1. Associate with an MQTT connection handle.

    Important

    Before you configure device tag-specific parameters, make sure that device verification information is specified. For more information, see Example.

    • Sample code:

          aiot_devinfo_setopt(devinfo_handle, AIOT_DEVINFOOPT_MQTT_HANDLE, mqtt_handle);
    • Parameters:

      Parameter

      Example

      Description

      AIOT_DEVINFOOPT_MQTT_HANDLE

      mqtt_handle

      The device tags.

  2. Configure a message callback function.

    • Sample code:

          aiot_devinfo_setopt(devinfo_handle, AIOT_DEVINFOOPT_RECV_HANDLER, (void *)demo_devinfo_recv_handler);
    • Parameters:

      Parameter

      Example

      Description

      AIOT_DEVINFOOPT_RECV_HANDLER

      demo_devinfo_recv_handler

      When a device tag-specific message is received, the callback function is called.

  3. Configure status monitoring.

    1. Define a callback function to monitor status.

      When you specify the processing logic of the callback function, take note of the following items:

      • If tags fail to be submitted, the following event types are returned:

        Event type

        Description

        AIOT_DEVINFOEVT_INVALID_DEVINFO

        The device information in the response is invalid. The ProductKey and DeviceName cannot be obtained.

        AIOT_DEVINFOEVT_INVALID_RESPONSE

        The field in the response is invalid.

        AIOT_DEVINFOEVT_INVALID_RESPONSE_FORMAT

        The format of the field in the response is invalid.

      • In this example, the response is printed. You can specify the processing logic based on your business requirements.

      void demo_devinfo_event_handler(void *handle, const aiot_devinfo_event_t *event, void *userdata)
      {
          switch (event->type) {
              case AIOT_DEVINFOEVT_INVALID_DEVINFO: {
                  printf("AIOT_DEVINFOEVT_INVALID_DEVINFO\n");
              }
              break;
              case AIOT_DEVINFOEVT_INVALID_RESPONSE: {
                  printf("AIOT_DEVINFOEVT_INVALID_RESPONSE\n");
              }
              break;
              case AIOT_DEVINFOEVT_INVALID_RESPONSE_FORMAT: {
                  printf("AIOT_DEVINFOEVT_INVALID_RESPONSE_FORMAT\n");
              }
              break;
              default: {
      
              }
          }
      }
    2. Specify the callback function to monitor status.

      • Sample code:

            aiot_devinfo_setopt(devinfo_handle, AIOT_DEVINFOOPT_EVENT_HANDLER, (void *)demo_devinfo_event_handler);
      • Parameters:

        Parameter

        Example

        Description

        AIOT_DEVINFOOPT_EVENT_HANDLER

        demo_devinfo_event_handler

        When the status of device connection changes, the callback is called to perform the required operations.

Step 3: Update tags

  1. Call the aiot_devinfo_send function to send a request to IoT Platform. For more information about the parameters in the request, see the previous step.

    When you send the request, take note of the following items:

    • aiot_devinfo_msg_t is an input parameter of the aiot_devinfo_send() function. This parameter specifies the data format.

    • AIOT_DEVINFO_MSG_UPDATE specifies the message type.

    • Sample message:

      Request message

      Alink format

      Description

      [
          {
              "attrKey": "testKey",
              "attrValue": "testValue"
          }
      ]
      {
        "id": "123",
        "version": "1.0",
        "sys":{
            "ack":0
        },
        "params": [
          {
              "attrKey": "testKey",
              "attrValue": "testValue"
          }
        ],
        "method": "thing.deviceinfo.update"
      }

      The message content is in the JSON format. The content is the value of the params parameter in the Alink data. For more information, see Submit tags.

      In this example, the specified tag is testKey: testValue.

        {
            aiot_devinfo_msg_t devinfo_update;
            char *update = "[{\"attrKey\":\"testKey\",\"attrValue\":\"testValue\"}]";
    
            memset(&devinfo_update, 0, sizeof(aiot_devinfo_msg_t));
            devinfo_update.product_key = product_key;
            devinfo_update.device_name = device_name;
            devinfo_update.type = AIOT_DEVINFO_MSG_UPDATE;
            devinfo_update.data.update.params = update;
    
            res = aiot_devinfo_send(devinfo_handle, &devinfo_update);
            if (res < STATE_SUCCESS) {
                aiot_devinfo_deinit(&devinfo_handle);
                demo_mqtt_stop(&mqtt_handle);
                return -1;
            }
            printf("aiot_devinfo_send update msg id: %d\n", res);
        }
  2. After IoT Platform receives the request message, IoT Platform updates the device tag and returns a response message.

  3. After the device receives the response message, the demo_devinfo_recv_handler callback function is called to perform the required operations.

    When you specify the processing logic of the callback function, take note of the following items:

    • aiot_devinfo_recv_t is an input parameter of the callback function. This parameter specifies the data format.

    • AIOT_DEVINFORECV_GENERIC_REPLY specifies the message type.

    • In this example, the message is printed.

    void demo_devinfo_recv_handler(void *handle, const aiot_devinfo_recv_t *packet, void *userdata)
    {
        switch (packet->type) {
            /* The response message from IoT Platform. */
            case AIOT_DEVINFORECV_GENERIC_REPLY: {
                printf("pk: %s, dn: %s, code: %d, msg id: %d, data: %.*s, message: %.*s\n", packet->product_key, packet->device_name,
                       packet->data.generic_reply.code, packet->data.generic_reply.msg_id, packet->data.generic_reply.data_len,
                       packet->data.generic_reply.data, packet->data.generic_reply.message_len, packet->data.generic_reply.message);
            }
            break;
            default: {
    
            }
        }
    }

(Optional) Step 4: Delete tags

  1. After device tags are submitted to IoT Platform, you can call the aiot_devinfo_send operation to delete the tags.

    When you send the request, take note of the following items:

    • aiot_devinfo_msg_t is an input parameter of the aiot_devinfo_send() function. This parameter specifies the data format.

    • AIOT_DEVINFO_MSG_DELETE specifies the message type.

    • Sample message:

      Request message

      Alink format

      Description

          {
              "attrKey": "testKey"
          }
      {
        "id": "123",
        "version": "1.0",
        "sys":{
            "ack":0
        },
        "params": [
          {
              "attrKey": "testKey",
          }
        ],
        "method": "thing.deviceinfo.update"
      }

      The message content is in the JSON format. The content is the value of the params parameter in the Alink data. For more information, see Delete tags.

      In this example, the specified tag is testKey.

           {
              aiot_devinfo_msg_t devinfo_delete;
              char *delete = "[{\"attrKey\":\"testKey\"}]";
      
              memset(&devinfo_delete, 0, sizeof(aiot_devinfo_msg_t));
              devinfo_delete.product_key = product_key;
              devinfo_delete.device_name = device_name;
              devinfo_delete.type = AIOT_DEVINFO_MSG_DELETE;
              devinfo_delete.data.delete.params = delete;
      
              res = aiot_devinfo_send(devinfo_handle, &devinfo_delete);
              if (res < STATE_SUCCESS) {
                  aiot_devinfo_deinit(&devinfo_handle);
                  demo_mqtt_stop(&mqtt_handle);
                  return -1;
              }
              printf("aiot_devinfo_send delete msg id: %d\n", res);
          } 
  2. After IoT Platform receives the request message, IoT Platform deletes the tag and returns a response message.

  3. After the device receives the response message, the demo_devinfo_recv_handler callback function is called to perform the required operations.

    For more information, see Process response messages.

Step 5: Exit the program

Call the aiot_devinfo_deinit function to delete the Devinfo client instance and release the corresponding resources.

    res = aiot_devinfo_deinit(&devinfo_handle);
    if (res < STATE_SUCCESS) {
        demo_mqtt_stop(&mqtt_handle);
        printf("aiot_devinfo_deinit failed: -0x%04X\n", -res);
        return -1;
    }

What to do next

  • After you configure the sample code file, compile the file to generate an executable file. In this example, the ./demos/devinfo-posix-demo executable file is generated.

    For more information, see Compilation and running.

  • For more information about the execution result, see View logs.