All Products
Search
Document Center

IoT Platform:Example

Last Updated:Jun 16, 2026

Use the Link SDK for C API to implement the revert-RPC (RRPC) feature on a device. The sample code file demos/mqtt_rrpc_demo.c is used in this example.

Background information

  • For more information about the RRPC feature, see Overview.

  • RRPC is available only for devices connected to IoT Platform over MQTT. For more information about the MQTT connection code, see Overview.

Procedure

  1. Initialize a client.

    For more information, see Step 1: Initialization.

  2. Configure required features.

    For more information, see Step 2: Configure features.

  3. Establish a connection.

    For more information, see Step 3: Request a connection.

  4. Receive an RRPC message.

    1. After the device connects to IoT Platform, call the RRpc operation to send an RRPC command to the device. The following topics are available:

    2. The device calls the aiot_mqtt_recv function to receive the RRPC message. A callback function then processes the message.

      In this example, a callback function named demo_mqtt_default_recv_handler is used.

    3. Define a callback function to process the RRPC message.

      Important

      After the device receives the request, you must send a response within 8 seconds. Otherwise, IoT Platform determines that the call failed regardless of whether the device received the request.

      When you define the callback logic, note the following items:

      • Check whether the message is an RRPC message based on the topic format.

        • If you use an RRPC-specific topic, the device does not need to subscribe to the topic.

          Topic format: /sys/${YourProductKey}/${YourDeviceName}/rrpc/request/${messageId}. For more information, see Use RRPC-specific topics.

        • If you use a custom topic, the device must subscribe to it. For more information, see Step 6: Subscribe to a topic.

          When you use a custom topic to send a message, the message is delivered to the device through the corresponding RRPC request topic. RRPC request topic format: /ext/rrpc/${messageId}/${topic}. For more information, see Call a custom topic (Recommended).

      • Define the callback processing logic: after receiving the RRPC message, the device processes the command and sends a response to IoT Platform.

      • In this example, the RRPC message is printed and a response with the payload set to pong is returned to IoT Platform.

      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);
      
                  /* TODO: Define the logic for processing business messages that are sent by the server. */  */
                  /* The following sample code shows a sample RRPC response. */        
                  {
                      char *payload = "pong";
                      char resp_topic[256];
                      if(packet->data.pub.topic_len > 256) {
                          break;
                      }
      
                      memset(resp_topic, 0, sizeof(resp_topic));
                      memcpy(resp_topic, packet->data.pub.topic, packet->data.pub.topic_len);
      
                      aiot_mqtt_pub(handle, resp_topic, (uint8_t *)payload, (uint32_t)strlen(payload), 0);
                  } 
              }
              break;
      
              ……
              ……       
      
              default: {
      
              }
          }
      }
  5. Close the connection.

    For more information, see Step 8: Disconnect.

  6. Exit the program.

    For more information, see Step 9: Exit the program.

What to do next

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

    For more information, see Compile and run.

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