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 implement the data compression feature for a device. In this example, a sample code file named ./demos/compress_basic_demo.c is used.

Background information

  • For more information about the data compression feature, see Overview.

  • The data compression feature is available only for devices that are connected to IoT Platform over Message Queuing Telemetry Transport (MQTT). For more information about MQTT connection-related code when you configure the feature, see Example.

Step 1: Initialize the SDK

  1. Add header files.

    ……
    ……
    #include "aiot_compress_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_compress_init function to create a data compression module instance and initialize the default parameters.

    /* Create a data compression module instance and initialize the default parameters. */
        compress_handle = aiot_compress_init();
        if (compress_handle == NULL) {
            demo_mqtt_stop(&mqtt_handle);
            printf("aiot_compress_init failed\n");
            return -1;
        }

Step 2: Configure required features

Call the aiot_compress_setopt function to perform the following steps:

  1. Associate the data compression module instance with an MQTT connection handle.

    /* Specify the MQTT connection handle */
    aiot_compress_setopt(compress_handle, AIOT_COMPRESSOPT_MQTT_HANDLE, mqtt_handle);
  2. Specify a compression level.

     /* Specify a compression level. The compression level ranges from 1 to 9. A higher level indicates higher compression quality and higher consumption of memory and time.*/
     uint8_t compress_level = 1;
     aiot_compress_setopt(compress_handle, AIOT_COMPRESSOPT_LEVEL, &compress_level);
  3. Specify topics that receive upstream messages and whose data needs to be compressed.

    The names of the topics must be complete and cannot contain wildcard characters.

    • Define topics.

      /* TODO:Replace the following topics with the topics that receive upstream message and whose data needs to be compressed. */
      char *compr_list[] = {
          "/${YourProductKey}/${YourDeviceName}/user/update",
          "/sys/${YourProductKey}/${YourDeviceName}/thing/event/property/post",
      };
    • Specify the topics.

      /* Specify the topics. */
      for(int i = 0; i < sizeof(compr_list) / sizeof(char *); i++) {
          aiot_compress_setopt(compress_handle, AIOT_COMPRESSOPT_APPEND_COMPR_TOPIC, compr_list[i]);
      }
  4. Specify topics that receive downstream messages and whose data needs to be compressed.

    • Define topics.

      /* TODO: Replace the following topics with the topics that receive downstream message and whose data needs to be compressed. */
      char *decompr_list[] = {
          "/${YourProductKey}/${YourDeviceName}/user/update_reply",
          "/sys/${YourProductKey}/${YourDeviceName}/thing/event/property/post_reply",
      };
    • Specify the topics.

       /* Specify the topics. */
       for(int i = 0; i < sizeof(decompr_list) / sizeof(char *); i++) {
           aiot_compress_setopt(compress_handle, AIOT_COMPRESSOPT_APPEND_DECOMPR_TOPIC, decompr_list[i]);
       }
  5. Configure a callback function to submit topics.

     /* Submit topics whose data needs to be compressed or decompressed only once. The topics take effect after you restart the device. */
        uint32_t code = 0;
        aiot_compress_setopt(compress_handle, AIOT_COMPRESSOPT_RECV_HANDLER, demo_update_reply);
        aiot_compress_setopt(compress_handle, AIOT_COMPRESSOPT_USERDATA, &code);

    Implement the callback function and obtain the returned result.

    static void demo_update_reply(void *handle, const aiot_compress_recv_t *packet, void *userdata)
    {
        uint32_t *code = (uint32_t *)userdata;
        *code = packet->data.update_reply.code;
        printf("compress update reply code %d, message %.*s\r\n", packet->data.update_reply.code, packet->data.update_reply.message_len, packet->data.update_reply.message);
    }

(Optional) Step 3: Submit topics whose data needs to be compressed

The device needs to submit the topics whose data needs to be compressed or decompressed and wait for the returned result.

Note
  • After the device submits the topics, the device does not need to re-submit the topics after you restart the device.

  • If the device needs to update the topics, the device can submit new topics. IoT Platform uses the latest batch of topics.

  • Submit topics whose data needs to be compressed.

    aiot_compress_topiclist_update(compress_handle);
  • Return the result. The code 200 indicates that the call is successful.

     if(code != 200) {
            demo_mqtt_stop(&mqtt_handle);
            aiot_compress_deinit(&compress_handle);
            printf("aiot_compress_topiclist_update failed\n");
            return -1;
        }

Step 4: Submit messages that you want to compress to the topics

After you submit the topics, you can submit messages to the topics. Link SDK for C automatically compresses the messages.

Call the aiot_mqtt_pub function to submit the messages the same way you submit common messages

 char *pub_topic = "/sys/${YourProductKey}/${YourDeviceName}/thing/event/property/post";
 char *pub_payload = "{\"id\":\"1\",\"version\":\"1.0\",\"params\":{\"LightSwitch\":0, \"message\":\"this is a test message, this is a test message, this is a test message\"}}";
 aiot_mqtt_pub(mqtt_handle, pub_topic, (uint8_t *)pub_payload, (uint32_t)strlen(pub_payload), 0);

Step 5: Send messages to the topics

If downstream messages need to be compressed, IoT Platform can compress the messages. Link SDK for C decompresses the messages and the callback function returns the decompressed messages.

Call the aiot_mqtt_recv function to receive messages. When decompressed messages arrive, Link SDK for C decompresses the messages and calls the callback function to return the decompressed messages.

/* The default callback function that can be called to receive MQTT messages. This function is called when the SDK receives an MQTT message from the MQTT broker and no custom callback is available. */
void demo_mqtt_default_recv_handler(void *handle, const aiot_mqtt_recv_t *packet, void *userdata)
{
    switch (packet->type) {

	....
    case AIOT_MQTTRECV_PUB: {
        printf("pub, qos: %d, topic: %.*s\n", packet->data.pub.qos, packet->data.pub.topic_len, packet->data.pub.topic);
        printf("pub, payload: %.*s\n", packet->data.pub.payload_len, packet->data.pub.payload);
    }
    break;

	....
    }
}

Step 6: Exit the program

Call the aiot_compress_deinit function to delete the instance and release the resources.

  /* Delete the data compression module instance. In most cases, the following code is not run. */
    res = aiot_compress_deinit(&compress_handle);
    if (res < STATE_SUCCESS) {
        demo_mqtt_stop(&mqtt_handle);
        printf("aiot_compress_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, an executable file named ./output/compress-basic-demo is generated. For more information, see Prepare a development environment.

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