All Products
Search
Document Center

IoT Platform:Example

Last Updated:Sep 12, 2023

This topic describes how to call the API operations of Link SDK for C to implement the revert-RPC (RRPC) feature for a device. In this example, a sample code file named demos/mqtt_rrpc_demo.c is used.

Background information

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

  • The RRPC 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.

Procedure

  1. Initialize a client.

    For more information, see Step 1: Initialize a client.

  2. Configure required features.

    For more information, see Step 2: Configure required features.

  3. Establish a connection.

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

  4. Receive an RRPC message.

    1. After the device is connected to IoT Platform, you can call the RRpc operation of IoT Platform 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. Then, a callback function is called to process the RRPC 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 specify the processing logic of the callback, take note of 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 the topic. For more information about how to subscribe to a topic, see Step 6: Subscribe to a topic.

          After you use a custom topic to send a message, the message is sent to the device by using the corresponding RRPC-request topic. Format of an RRPC request topic: /ext/rrpc/${messageId}/${topic}. For more information, see Use custom topics (recommended).

      • Specify the processing logic of the callback: After the device receives 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 whose payload parameter is 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.

  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 the file to generate an executable file. In this example, the ./output/mqtt-rrpc-demo executable file is generated.

    For more information, see Compilation and running.

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