Secure tunnel communication protocol

更新时间:
复制 MD 格式

This topic describes the secure tunnel communication protocol defined by IoT Platform.

Background information

The following figure shows how data is encapsulated for transmission over the communication protocol.

Data encapsulation method

After a device and an access client connect to the secure tunnel, they use the tunnel's communication protocol for remote access. All communication, session management, and data transfer between the device and the access client rely on tunnel frames. A tunnel frame is the payload of a binary WebSocket data frame.

You must design and develop the application layer communication protocol for both the access client application and the device-side target application. This protocol enables communication between the access client and the device.

Communication Data Format

A tunnel frame consists of two parts:

  • Tunnel Header: Contains metadata for the secure tunnel session.
    FieldDescription
    Header LengthThe length of the Tunnel Header content as a byte array. The maximum length is 2048 bytes. This value must occupy 2 bytes, with the high-order byte first.

    For example, if the UTF-8 encoded byte array of the Tunnel Header content has a length of 87 (decimal), which is 57 (hexadecimal), the high-order byte is 0x00 and the low-order byte is 0x57.

    Header BytesThe Tunnel Header content, which is a UTF-8 encoded byte array of the JSON content. For more information about the JSON data, see the following table Tunnel Header JSON data.
    Table 1. Tunnel Header JSON data
    ParameterData typeDescription
    frame_typeIntegerThe type of the tunnel frame. Valid values:
    • 1: common_response. The response data.
    • 2: session_create. Creates a session.
    • 3: session_release. Closes a session.
    • 4: data_transport. Transmits data within a session.
    session_idStringThe session ID. This ID is unique within the secure tunnel.

    When the access client sends a request to create a session, this parameter is not required. IoT Platform assigns a session ID and sends it to the device. For all other tunnel frame types, both the access client and the device must include the session ID.

    frame_idLongThe frame ID set by the access client or device when sending data. The value can range from 0 to (263 - 1).

    Use an incrementing frame ID for both the device and the access client to distinguish data within each session_id.

    service_typeStringA custom service type for the session. The value can contain letters, underscores (_), hyphens (-), and periods (.). The first character must be a letter. The maximum length is 16 characters.
  • Tunnel Payload: Contains the session payload. The length cannot exceed 4 KB. The data format depends on the frame_type. For more information, see Session details.

Session details

The following flowchart shows the session communication process between a device and an access client.

Session communication flow
Ordinal NumberProcessDescription
1 and 2Establish a WebSocket connection.The device and the access client establish a secure WebSocket channel.
3Create a session.The access client sends a session creation request to the device.
Important If the device does not return a response within 10 seconds after the session creation tunnel frame is sent, the access client receives a timeout error.
4Creates response data.IoT Platform or the device returns a response to the access client.
Note Response data can be created for different types of tunnel frames.
5 and 6Session data transmission.The device and the access client can use the session for data communication.
Important IoT Platform does not parse the Tunnel Payload data. The platform forwards the data directly to the device or the access client.
7Close the session.Common scenarios:
  • Either the device or the access client can actively close the session.
  • IoT Platform closes the session during an upgrade. In this case, the access client must be able to reconnect to the device after the session is closed by IoT Platform.
  • If either the device or the access client disconnects, the session is closed on the other end.

The following table describes the required JSON data in the Tunnel Header and Tunnel Payload for different session operations.

FieldCreate sessionResponse dataIn-Session Data TransmissionClose session
frame_typeSet to 2.Set to 1.Set to 4.Set to 3.
session_idNot required.

When a session is created, IoT Platform generates this ID and sends it to the device.

Required.

Use the session_id that IoT Platform sent to the device when the session was created.

Required.

Use the session_id that IoT Platform generated when the session was created.

Required.

Use the session_id that IoT Platform generated when the session was created.

frame_idRequired.

Set this parameter as described in the parameter description section above.

Required.

Use the frame_id from the session creation request that the device received.

Required.

Set this parameter as described in the parameter description section above.

Required.

Set this parameter as described in the parameter description section above.

service_typeRequired.

Set this parameter as described in the parameter description section above. After the access client specifies a service type, the device can use this type to connect to the specified target application when it receives the session creation request.

Required.

Use the service_type from the session creation request that the device received.

Required.

Use the service_type that was specified when the session was created.

Not applicable.
payloadNot applicable.JSON data with the following content:
{
    "code": 0,
    "msg": ""
}

For more information about the parameters, see the payload parameters for a response table below.

A custom byte array. The length cannot exceed 4 KB.JSON data with the following content:
{
    "code": 0,
    "msg": ""
}

For more information about the parameters, see the payload parameters for closing a session table below.

Table 2. Payload parameters for a response
ParameterData typeDescription
codeIntegerThe response code. The value can range from 0 to 255. Codes 0 to 15 are reserved by the system. You can customize codes from 16 to 255.
  • 0: The session was created. All other values indicate failure.
  • 1: The maximum number of sessions (10) in a single secure tunnel has been reached. No more sessions can be created.
  • 2: The device rejected the session creation request.
msgStringThe error message returned if the creation fails.
Table 3. Payload parameters for closing a session
ParameterData typeDescription
codeIntegerThe reason for closing the session. Valid values:
  • 0: The access client actively closed the session.
  • 1: The device actively closed the session.
  • 2: IoT Platform closed the session because it detected that the access client was disconnected.
  • 3: IoT Platform closed the session because it detected that the device was disconnected.
  • 4: IoT Platform closed the session due to a system update. The device and the access client can wait 1 second and then reconnect.
msgStringInformation about closing the session.