Link Layer Protocol Documentation
Overview
This document describes the link layer protocol, which provides a common framing mechanism for communication over various transport methods (e.g., TCP, RS232). The link layer ensures integrity and interoperability of messages.
Frame Structure
Field | Size (bytes) | Description |
---|---|---|
STX | 1 | Start of Text (0x02 ). |
MessageType | 1 | Type of the message (e.g., Request, Response, KeepAlive). |
BODY | Variable | Encapsulated payload, structure depends on MessageType . |
ETX | 1 | End of Text (0x03 ). |
Body Structures
1. Request (MessageType 0x01
)
Field | Size (bytes) | Description |
---|---|---|
API Key | 4 | 4-byte unique key for authentication (e.g., 0x12 0x34 0xAB 0xCD ). |
Length (N) | 4 | Big-endian length of the JSON-RPC payload. |
Payload | N | JSON-RPC payload as a UTF-8 encoded JSON string. |
CRC16 | 2 | CRC16 checksum (polynomial 0xA001 ) calculated over Length and Payload . |
The network order for Length and CRC16 are big endian
2. Response & Event (MessageType 0x02
/ 0x03
)
Field | Size (bytes) | Description |
---|---|---|
Length (N) | 4 | Big-endian length of the JSON-RPC payload. |
Payload | N | JSON-RPC payload as a UTF-8 encoded JSON string. |
CRC16 | 2 | CRC16 checksum (polynomial 0xA001 ) calculated over Length and Payload . |
3. KeepAlive & ACK (MessageType 0x04
/ 0x05
)
- No body.
4. Error (MessageType 0x06
)
Field | Size (bytes) | Description |
---|---|---|
Error Code | 1 | Single byte indicating the error (see Error Codes). |
Error Codes
Error Code | Value (Hex) | Description |
---|---|---|
INVALID_MESSAGE |
0x01 |
Message frame is malformed. |
INVALID_MSG_TYPE |
0x02 |
Message type is unsupported. |
CRC_ERROR |
0x03 |
CRC16 checksum mismatch. |
LEN_ERROR |
0x04 |
Payload length is invalid. |
NOT_AUTHENTICATED |
0x05 |
API key is missing or invalid. |
CRC16 Calculation
The CRC16 checksum is calculated using the polynomial 0xA001
over the concatenated Length and Payload fields of the body.
Example Pseudocode
Example
Consider the following request
Payload Breakdown
Payload
The UTF-8 encoded JSON payload is:
Example
For this example I trimmed all the white spaces, but they can stay in the request as the field is in ASCII
Frame Fields
Field | Value (Hex) | Description |
---|---|---|
STX | 02 |
Start of Text. |
MessageType | 01 |
Request message type. |
API Key | 12 34 AB CD |
Example 4-byte API key. |
Length | 00 00 00 4B |
Payload length (75 bytes, big-endian). |
Payload | 7B 22 6A ... 6C 7D |
UTF-8 encoded JSON payload. |
CRC16 | 40 A2 |
CRC16 checksum over Length and Payload . |
ETX | 03 |
End of Text. |
Notes
- CRC16 Calculation: The CRC16 checksum is calculated using the polynomial
0xA001
over the concatenatedLength
andPayload
fields. - The payload length in this example is 68 bytes (decimal), represented as
0x00000044
in big-endian format.