|
| 1 | +# Packet Format |
| 2 | + |
| 3 | +This document describes the MeshCore packet format. |
| 4 | + |
| 5 | +- `0xYY` indicates `YY` in hex notation. |
| 6 | +- `0bYY` indicates `YY` in binary notation. |
| 7 | +- Bit 0 indicates the bit furthest to the right: `0000000X` |
| 8 | +- Bit 7 indicates the bit furthest to the left: `X0000000` |
| 9 | + |
| 10 | +## Version 1 Packet Format |
| 11 | + |
| 12 | +This is the protocol level packet structure used in MeshCore firmware v1.12.0 |
| 13 | + |
| 14 | +``` |
| 15 | +[header][transport_codes(optional)][path_length][path][payload] |
| 16 | +``` |
| 17 | + |
| 18 | +- [header](#header-format) - 1 byte |
| 19 | + - 8-bit Format: `0bVVPPPPRR` - `V=Version` - `P=PayloadType` - `R=RouteType` |
| 20 | + - Bits 0-1 - 2-bits - [Route Type](#route-types) |
| 21 | + - `0x00`/`0b00` - `ROUTE_TYPE_TRANSPORT_FLOOD` - Flood Routing + Transport Codes |
| 22 | + - `0x01`/`0b01` - `ROUTE_TYPE_FLOOD` - Flood Routing |
| 23 | + - `0x02`/`0b10` - `ROUTE_TYPE_DIRECT` - Direct Routing |
| 24 | + - `0x03`/`0b11` - `ROUTE_TYPE_TRANSPORT_DIRECT` - Direct Routing + Transport Codes |
| 25 | + - Bits 2-5 - 4-bits - [Payload Type](#payload-types) |
| 26 | + - `0x00`/`0b0000` - `PAYLOAD_TYPE_REQ` - Request (destination/source hashes + MAC) |
| 27 | + - `0x01`/`0b0001` - `PAYLOAD_TYPE_RESPONSE` - Response to `REQ` or `ANON_REQ` |
| 28 | + - `0x02`/`0b0010` - `PAYLOAD_TYPE_TXT_MSG` - Plain text message |
| 29 | + - `0x03`/`0b0011` - `PAYLOAD_TYPE_ACK` - Acknowledgment |
| 30 | + - `0x04`/`0b0100` - `PAYLOAD_TYPE_ADVERT` - Node advertisement |
| 31 | + - `0x05`/`0b0101` - `PAYLOAD_TYPE_GRP_TXT` - Group text message (unverified) |
| 32 | + - `0x06`/`0b0110` - `PAYLOAD_TYPE_GRP_DATA` - Group datagram (unverified) |
| 33 | + - `0x07`/`0b0111` - `PAYLOAD_TYPE_ANON_REQ` - Anonymous request |
| 34 | + - `0x08`/`0b1000` - `PAYLOAD_TYPE_PATH` - Returned path |
| 35 | + - `0x09`/`0b1001` - `PAYLOAD_TYPE_TRACE` - Trace a path, collecting SNR for each hop |
| 36 | + - `0x0A`/`0b1010` - `PAYLOAD_TYPE_MULTIPART` - Packet is part of a sequence of packets |
| 37 | + - `0x0B`/`0b1011` - `PAYLOAD_TYPE_CONTROL` - Control packet data (unencrypted) |
| 38 | + - `0x0C`/`0b1100` - reserved |
| 39 | + - `0x0D`/`0b1101` - reserved |
| 40 | + - `0x0E`/`0b1110` - reserved |
| 41 | + - `0x0F`/`0b1111` - `PAYLOAD_TYPE_RAW_CUSTOM` - Custom packet (raw bytes, custom encryption) |
| 42 | + - Bits 6-7 - 2-bits - [Payload Version](#payload-versions) |
| 43 | + - `0x00`/`0b00` - v1 - 1-byte src/dest hashes, 2-byte MAC |
| 44 | + - `0x01`/`0b01` - v2 - Future version (e.g., 2-byte hashes, 4-byte MAC) |
| 45 | + - `0x02`/`0b10` - v3 - Future version |
| 46 | + - `0x03`/`0b11` - v4 - Future version |
| 47 | +- `transport_codes` - 4 bytes (optional) |
| 48 | + - Only present for `ROUTE_TYPE_TRANSPORT_FLOOD` and `ROUTE_TYPE_TRANSPORT_DIRECT` |
| 49 | + - `transport_code_1` - 2 bytes - `uint16_t` - calculated from region scope |
| 50 | + - `transport_code_2` - 2 bytes - `uint16_t` - reserved |
| 51 | +- `path_length` - 1 byte - Length of the path field in bytes |
| 52 | +- `path` - size provided by `path_length` - Path to use for Direct Routing |
| 53 | + - Up to a maximum of 64 bytes, defined by `MAX_PATH_SIZE` |
| 54 | + - v1.12.0 firmware and older drops packets with `path_length` [larger than 64](https://github.com/meshcore-dev/MeshCore/blob/e812632235274ffd2382adf5354168aec765d416/src/Dispatcher.cpp#L144) |
| 55 | +- `payload` - variable length - Payload Data |
| 56 | + - Up to a maximum 184 bytes, defined by `MAX_PACKET_PAYLOAD` |
| 57 | + - Generally this is the remainder of the raw packet data |
| 58 | + - The firmware parses this data based on the provided Payload Type |
| 59 | + - v1.12.0 firmware and older drops packets with `payload` sizes [larger than 184](https://github.com/meshcore-dev/MeshCore/blob/e812632235274ffd2382adf5354168aec765d416/src/Dispatcher.cpp#L152) |
| 60 | + |
| 61 | +### Packet Format |
| 62 | + |
| 63 | +| Field | Size (bytes) | Description | |
| 64 | +|-----------------|----------------------------------|----------------------------------------------------------| |
| 65 | +| header | 1 | Contains routing type, payload type, and payload version | |
| 66 | +| transport_codes | 4 (optional) | 2x 16-bit transport codes (if ROUTE_TYPE_TRANSPORT_*) | |
| 67 | +| path_length | 1 | Length of the path field in bytes | |
| 68 | +| path | up to 64 (`MAX_PATH_SIZE`) | Stores the routing path if applicable | |
| 69 | +| payload | up to 184 (`MAX_PACKET_PAYLOAD`) | Data for the provided Payload Type | |
| 70 | + |
| 71 | +> NOTE: see the [Payloads](./payloads.md) documentation for more information about the content of specific payload types. |
| 72 | +
|
| 73 | +### Header Format |
| 74 | + |
| 75 | +Bit 0 means the lowest bit (1s place) |
| 76 | + |
| 77 | +| Bits | Mask | Field | Description | |
| 78 | +|------|--------|-----------------|----------------------------------| |
| 79 | +| 0-1 | `0x03` | Route Type | Flood, Direct, etc | |
| 80 | +| 2-5 | `0x3C` | Payload Type | Request, Response, ACK, etc | |
| 81 | +| 6-7 | `0xC0` | Payload Version | Versioning of the payload format | |
| 82 | + |
| 83 | +### Route Types |
| 84 | + |
| 85 | +| Value | Name | Description | |
| 86 | +|--------|-------------------------------|----------------------------------| |
| 87 | +| `0x00` | `ROUTE_TYPE_TRANSPORT_FLOOD` | Flood Routing + Transport Codes | |
| 88 | +| `0x01` | `ROUTE_TYPE_FLOOD` | Flood Routing | |
| 89 | +| `0x02` | `ROUTE_TYPE_DIRECT` | Direct Routing | |
| 90 | +| `0x03` | `ROUTE_TYPE_TRANSPORT_DIRECT` | Direct Routing + Transport Codes | |
| 91 | + |
| 92 | +### Payload Types |
| 93 | + |
| 94 | +| Value | Name | Description | |
| 95 | +|--------|---------------------------|----------------------------------------------| |
| 96 | +| `0x00` | `PAYLOAD_TYPE_REQ` | Request (destination/source hashes + MAC) | |
| 97 | +| `0x01` | `PAYLOAD_TYPE_RESPONSE` | Response to `REQ` or `ANON_REQ` | |
| 98 | +| `0x02` | `PAYLOAD_TYPE_TXT_MSG` | Plain text message | |
| 99 | +| `0x03` | `PAYLOAD_TYPE_ACK` | Acknowledgment | |
| 100 | +| `0x04` | `PAYLOAD_TYPE_ADVERT` | Node advertisement | |
| 101 | +| `0x05` | `PAYLOAD_TYPE_GRP_TXT` | Group text message (unverified) | |
| 102 | +| `0x06` | `PAYLOAD_TYPE_GRP_DATA` | Group datagram (unverified) | |
| 103 | +| `0x07` | `PAYLOAD_TYPE_ANON_REQ` | Anonymous request | |
| 104 | +| `0x08` | `PAYLOAD_TYPE_PATH` | Returned path | |
| 105 | +| `0x09` | `PAYLOAD_TYPE_TRACE` | Trace a path, collecting SNR for each hop | |
| 106 | +| `0x0A` | `PAYLOAD_TYPE_MULTIPART` | Packet is part of a sequence of packets | |
| 107 | +| `0x0B` | `PAYLOAD_TYPE_CONTROL` | Control packet data (unencrypted) | |
| 108 | +| `0x0C` | reserved | reserved | |
| 109 | +| `0x0D` | reserved | reserved | |
| 110 | +| `0x0E` | reserved | reserved | |
| 111 | +| `0x0F` | `PAYLOAD_TYPE_RAW_CUSTOM` | Custom packet (raw bytes, custom encryption) | |
| 112 | + |
| 113 | +### Payload Versions |
| 114 | + |
| 115 | +| Value | Version | Description | |
| 116 | +|--------|---------|--------------------------------------------------| |
| 117 | +| `0x00` | 1 | 1-byte src/dest hashes, 2-byte MAC | |
| 118 | +| `0x01` | 2 | Future version (e.g., 2-byte hashes, 4-byte MAC) | |
| 119 | +| `0x02` | 3 | Future version | |
| 120 | +| `0x03` | 4 | Future version | |
0 commit comments