All Products
Search
Document Center

IoT Platform:Usage example

Last Updated:Jun 18, 2026

Use the demo file ./demos/compress_basic_demo.c in the C Link SDK to call Link SDK APIs and enable data compression for a device.

Background information

  • For more information about data compression, see Overview.

  • Data compression uses MQTT connections. For more information about MQTT-related code, see Usage example.

Step 1: Initialization

  1. Add the header file.

    ……
    ……
    #include "aiot_compress_api.h"
    ……
  2. Configure the underlying dependency and log output.

    aiot_sysdep_set_portfile(&g_aiot_sysdep_portfile);
    aiot_state_set_logcb(demo_state_logcb);

  3. Call aiot_compress_init to create a data compression instance and initialize default parameters.

     /* Create a compress client 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 features

Call aiot_compress_setopt to configure the following settings.

  1. Associate the MQTT connection handle.

    /* Associate the MQTT handle. */
    aiot_compress_setopt(compress_handle, AIOT_COMPRESSOPT_MQTT_HANDLE, mqtt_handle);
  2. Set the compression level.

     /* Set the compression level. The level ranges from 1 to 9. A higher level provides better compression but consumes more memory and time. */
     uint8_t compress_level = 1;
     aiot_compress_setopt(compress_handle, AIOT_COMPRESSOPT_LEVEL, &compress_level);
  3. Add topics for mobile originated (MO) messages that require compression.

    Specify the full topic path. Wildcard characters are not supported.

    • Define the list.

      /* TODO: Replace this with the list of topics for mobile originated messages that you want to compress. */
      char *compr_list[] = {
          "/${YourProductKey}/${YourDeviceName}/user/update",
          "/sys/${YourProductKey}/${YourDeviceName}/thing/event/property/post",
      };
    • Configure the topic list.

      /* Add the topics for mobile originated messages that require compression. */
      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. Add topics for mobile terminated (MT) messages that require decompression.

    • Define the list.

      /* TODO: Replace this with the list of topics for mobile terminated messages that you want to compress. */
      char *decompr_list[] = {
          "/${YourProductKey}/${YourDeviceName}/user/update_reply",
          "/sys/${YourProductKey}/${YourDeviceName}/thing/event/property/post_reply",
      };
    • Configure the topic list.

       /* Add the topics for mobile terminated messages that require decompression. */
       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. Set the callback function for the topic list report.

     /* Report the list of topics for compression or decompression to IoT Platform. You only need to report the list once. The configuration remains valid after a restart. */
        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 to retrieve the 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: Report the compression topic list

Report the list of topics for compression or decompression from the device, and wait for the result.

Note
  • After the device reports the list, the configuration persists across restarts.

  • To update the topic list, report it again. IoT Platform always uses the latest reported list.

  • Report the compression topic list.

    aiot_compress_topiclist_update(compress_handle);
  • A return code of 200 indicates that the operation 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: Report messages in the compression list

When you publish a message to a topic in the compression list, the Link SDK compresses the message automatically.

Call aiot_mqtt_pub to publish the message in the same way as a regular message.

 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: Deliver messages in the compressed list

For mobile terminated messages that require compression, IoT Platform compresses them before delivery. The Link SDK automatically decompresses these messages, and the callback function receives the decompressed data.

Call aiot_mqtt_recv to process received messages. When a compressed message arrives, the Link SDK decompresses it and runs the message callback.

/* The default MQTT message processing callback. This is called when the SDK receives an MQTT message from the server and no corresponding user callback is available to process it. */
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 aiot_compress_deinit to destroy the compression instance and release resources.

  /* Destroy the COMPRESS instance. This part of the code is not typically 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 file, compile it to generate the executable file ./output/compress-basic-demo. For more information, see Compile and run.

  • For more information about operation results, see Operational logs.