|
| 1 | +# Protocol Definition |
| 2 | + |
| 3 | +The TCP packet format used in the CSM-TCP-Router is defined as follows: |
| 4 | + |
| 5 | +``` |
| 6 | +| Data Length (4B) | Version (1B) | TYPE (1B) | FLAG1 (1B) | FLAG2 (1B) | Text Data | |
| 7 | +╰───────────────────────────────── Header ──────────────────────────────╯╰─── Data Length Range ──╯ |
| 8 | +``` |
| 9 | + |
| 10 | +## Header Fields |
| 11 | + |
| 12 | +### Data Length (4 Bytes) |
| 13 | + |
| 14 | +This field specifies the length of the data section and is represented using 4 bytes. |
| 15 | + |
| 16 | +### Version (1 Byte) |
| 17 | + |
| 18 | +This field indicates the version of the data packet. The current version is `0x01`. Different versions can be handled appropriately to ensure forward compatibility. |
| 19 | + |
| 20 | +### Packet Type (1 Byte) |
| 21 | + |
| 22 | +This field defines the type of the data packet and is an enumerated value. The supported packet types are: |
| 23 | + |
| 24 | +- Information Packet (`info`) - `0x00` |
| 25 | +- Error Packet (`error`) - `0x01` |
| 26 | +- Command Packet (`cmd`) - `0x02` |
| 27 | +- Synchronous Response Packet (`resp`) - `0x03` |
| 28 | +- Asynchronous Response Packet (`async-resp`) - `0x04` |
| 29 | +- Subscription Status Packet (`status`) - `0x05` |
| 30 | + |
| 31 | +### FLAG1 (1 Byte) |
| 32 | + |
| 33 | +This field is reserved for future use to describe additional attributes of the data packet. |
| 34 | + |
| 35 | +### FLAG2 (1 Byte) |
| 36 | + |
| 37 | +Similar to FLAG1, this field is reserved for future use to describe additional attributes of the data packet. |
| 38 | + |
| 39 | +## Data Content |
| 40 | + |
| 41 | +### Information Packet (`info`) |
| 42 | + |
| 43 | +The content of an information packet is plain text containing informational data. |
| 44 | + |
| 45 | +### Error Packet (`error`) |
| 46 | + |
| 47 | +The content of an error packet is plain text describing an error, formatted as per the CSM Error format. |
| 48 | + |
| 49 | +> [!NOTE] |
| 50 | +> The CSM Error format is: `[Error: Error Code] Error Message`. |
| 51 | +
|
| 52 | +### Command Packet (`cmd`) |
| 53 | + |
| 54 | +The content of a command packet is a command in the CSM local command format. It supports the following types of messages: |
| 55 | + |
| 56 | +- Synchronous (`-@`) |
| 57 | +- Asynchronous (`->`) |
| 58 | +- Asynchronous without return (`->|`) |
| 59 | +- Register (`register`) |
| 60 | +- Unregister (`unregister`) |
| 61 | + |
| 62 | +> [!NOTE] |
| 63 | +> Example: Suppose there is a CSM module named `DAQmx` in the local program with an interface `API: Start Sampling`. You can send the following messages to control data acquisition: |
| 64 | +> |
| 65 | +> - `API: Start Sampling -@ DAQmx` // Synchronous message |
| 66 | +> - `API: Start Sampling -> DAQmx` // Asynchronous message |
| 67 | +> - `API: Start Sampling ->| DAQmx` // Asynchronous message without return |
| 68 | +> |
| 69 | +> These messages can also be sent over a TCP connection to achieve remote control. |
| 70 | +
|
| 71 | +> [!NOTE] |
| 72 | +> Example: Suppose there is a CSM module `A` that continuously sends a monitoring status called `Status`. Another module `B` can subscribe to this status: |
| 73 | +> |
| 74 | +> - `status@a >> api@b -><register>` // Subscribe to status |
| 75 | +> - `status@a >> api@b -><unregister>` // Unsubscribe from status |
| 76 | +> |
| 77 | +> Similarly, these messages can be sent over a TCP connection to manage subscriptions remotely. |
| 78 | +> |
| 79 | +> If the subscriber (`api@b`) is omitted, it indicates that the client connected to the TCP router is subscribing to the status: |
| 80 | +> |
| 81 | +> - `status@a -><register>` // Client subscribes to module A's status |
| 82 | +> - `status@a >> api@b -><unregister>` // Client unsubscribes from module A's status |
| 83 | +> |
| 84 | +> When module `A` sends a `Status`, the client will automatically receive a `status` packet. |
| 85 | +
|
| 86 | +### Synchronous Response Packet (`resp`) |
| 87 | + |
| 88 | +After executing a synchronous command, the TCP router sends a response packet back to the client. |
| 89 | + |
| 90 | +### Asynchronous Response Packet (`async-resp`) |
| 91 | + |
| 92 | +After executing an asynchronous command, the TCP router sends a response packet back to the client. The format is: `Response Data <- Original Asynchronous Message`. |
| 93 | + |
| 94 | +### Subscription Status Packet (`status`) |
| 95 | + |
| 96 | +When a client subscribes to the status of a CSM module, it will automatically receive this packet whenever the status changes. |
| 97 | + |
| 98 | +The packet format is: `Status Name >> Status Data <- Sending Module`. |
0 commit comments