tencent cloud

Feedback

RRPC Communication

Last updated: 2021-10-27 14:56:33

    Feature Overview

    Because of the async communication mode of the MQTT protocol based on the publish/subscribe pattern, after the server controls a device, it cannot synchronously get the result returned by the device. To solve this problem, IoT Hub uses the Revert RPC (RRPC) technology to implement a sync communication mechanism.

    How Communication Works

    Communication topics

    • The subscription message topic $rrpc/rxd/${productID}/${deviceName}/+ is used to subscribe to RRPC request messages sent by the cloud (downstream).
    • The request message topic $rrpc/rxd/${productID}/${deviceName}/${processID} is used for the cloud to publish (downstream) RRPC request messages.
    • The response message topic $rrpc/txd/${productID}/${deviceName}/${processID} is used to publish (upstream) RRPC response messages.
    Note:

    • ${productID}: product ID
    • ${deviceName}: device name
    • ${processID}: unique message ID generated by the server to identify different RRPC messages. The corresponding RRPC request message can be found through the processID carried in the RRPC response message.

    Communication process

    1. The device subscribes to the RRPC subscription message topic.
    2. The server publishes an RRPC request message by calling the PublishRRPCMessage API.
    3. After receiving the message, the device extracts the processID distributed by the cloud in the request message topic, sets it as the processID of the response message topic, and publishes a return message of the device to the response message topic.
    4. After IoT Hub receives the return message from the device, it matches the message according to the processID and sends the return message to the server.
    Note:

    RRPC requests time out in 4s, that is, if the device doesn't respond within 4s, the request will be considered to have timed out.

    The flowchart is as follows:
    image.png

    RRPC communication sample

    The sample completes device connection through the device-side C-SDK on Linux and calls APIs together with Tencent Cloud API Explorer. The specific steps are as follows:

    Creating device in console

    Creating product and device

    Create an air conditioner product and an airConditioner1 device as instructed in Device Interconnection.

    Compiling and running demo (with key-authenticated device as example)

    1. Compile the SDK

    Modify CMakeLists.txt and make sure that the following options exist:

    set(BUILD_TYPE                   "release")
    set(COMPILE_TOOLS                "gcc") 
    set(PLATFORM                      "linux")
    set(FEATURE_MQTT_COMM_ENABLED ON)
    set(FEATURE_RRPC_ENABLED ON)
    set(FEATURE_AUTH_MODE "KEY")
    set(FEATURE_AUTH_WITH_NOTLS OFF)
    set(FEATURE_DEBUG_DEV_INFO_USED  OFF)
    

    Run the following script for compilation:

    ./cmake_build.sh 
    

    The demo output rrpc_sample is in the output/release/bin folder.

    2. Enter the device information

    Enter the information of the airConditioner1 device created above in the JSON file aircond_device_info1.json.

    {
      "auth_mode":"KEY",    
      "productId":"KL4J2****8",
      "deviceName":"airConditioner1",    
      "key_deviceinfo":{    
          "deviceSecret":"zOZXUaycuwleP****78dBA=="
      }
    }
    

    3. Run the rrpc_sample demo

    You can see that the airConditioner1 device has subscribed to the RRPC message and then entered the waiting status.

    ./rrpc_sample -c ./aircond_device_info1.json -l 1000
    INF|2020-08-03 23:57:55|qcloud_iot_device.c|iot_device_info_set(50): SDK_Ver: 3.2.0, Product_ID: KL4J2****8, Device_Name: airConditioner1
    DBG|2020-08-03 23:57:55|HAL_TLS_mbedtls.c|HAL_TLS_Connect(200): Setting up the SSL/TLS structure...
    DBG|2020-08-03 23:57:55|HAL_TLS_mbedtls.c|HAL_TLS_Connect(242): Performing the SSL/TLS handshake...
    DBG|2020-08-03 23:57:55|HAL_TLS_mbedtls.c|HAL_TLS_Connect(243): Connecting to /KL4J2****8.iotcloud.tencentdevices.com/8883...
    INF|2020-08-03 23:57:55|HAL_TLS_mbedtls.c|HAL_TLS_Connect(265): connected with /KL4J2****8.iotcloud.tencentdevices.com/8883...
    INF|2020-08-03 23:57:56|mqtt_client.c|IOT_MQTT_Construct(113): mqtt connect with id: 2**** success
    INF|2020-08-03 23:57:56|rrpc_sample.c|main(206): Cloud Device Construct Success
    DBG|2020-08-03 23:57:56|mqtt_client_subscribe.c|qcloud_iot_mqtt_subscribe(142): topicName=$rrpc/rxd/KL4J2****8/airConditioner1/+|packet_id=****
    INF|2020-08-03 23:57:56|rrpc_sample.c|_mqtt_event_handler(49): subscribe success, packet-id=*****
    DBG|2020-08-03 23:57:56|rrpc_client.c|_rrpc_event_callback(104): rrpc topic subscribe success
    

    4. Call TencentCloud API PublishRRPCMessage to send an RRPC request message

    Go to API Explorer, enter the personal key and device parameter information, select Online Call, and send the request.

    5. Observe the RRPC request message

    Observe the printout of the airConditioner1 device, and you can see that the RRPC request message has been received, and the processID is ***.

    DBG|2020-08-04 00:07:36|rrpc_client.c|_rrpc_message_cb(85): topic=$rrpc/rxd/KL4J2****8/airConditioner1/***
    INF|2020-08-04 00:07:36|rrpc_client.c|_rrpc_message_cb(86): len=6, topic_msg=closed
    INF|2020-08-04 00:07:36|rrpc_client.c|_rrpc_get_process_id(76): len=3, process id=***
    INF|2020-08-04 00:07:36|rrpc_sample.c|_rrpc_message_handler(137): rrpc message=closed
    

    6. Observe the RRPC response message

    Observe the printout of the airConditioner1 device, and you can see that the RRPC request message has been processed, the RRPC response message has been replied, and the processID is ***.

    DBG|2020-08-04 00:07:36|mqtt_client_publish.c|qcloud_iot_mqtt_publish(340): publish packetID=0|topicName=$rrpc/txd/KL4J2****8/airConditioner1/***|payload=ok
    

    7. Observe the server response result

    Observe the response result of the server, and you can see that the RRPC response message has been received. MessageId is ***, and Payload is **** after being Base64-encoded, which is the same as the actual response message of the client after being Base64-encoded. At this point, it can be confirmed that the response message has been received.

    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support