Chapter 14 — Datagram Protocol
The Datagram protocol provides reliable 0-72 byte addressed transfers. It is the primary mechanism for configuration memory access (read/write) and is used by CDI, ACDI, and firmware upgrade features.
src/openlcb/protocol_datagram_handler.c,
src/openlcb/protocol_datagram_handler.h
14.1 Two-Phase ACK Protocol
Every datagram request follows a two-phase acknowledgement pattern. The Reply Pending bit (0x80) is always set in the Datagram Received OK reply, indicating that a response datagram will follow:
14.2 Address Space Routing
The datagram handler routes incoming datagrams to per-address-space callbacks based on the address space byte. The standard spaces are:
| Space | Name | Typical Use |
|---|---|---|
| 0xFF | CDI | XML device description (read-only) |
| 0xFE | Configuration Memory | User-writable settings |
| 0xFD | All | Full memory space |
| 0xFC | ACDI Manufacturer | Manufacturer info (read-only) |
| 0xFB | ACDI User | User name/description |
| 0xFA | Train FDI | Function Definition Info (train nodes) |
| 0xF9 | Train Function Config | Train function configuration |
14.3 Command Structure
The first byte of the datagram payload identifies the command type, and the second byte identifies the sub-command:
| Byte 0 (Command) | Meaning |
|---|---|
| 0x20 | Memory Configuration Protocol |
| Byte 1 (Sub-command) | Operation |
|---|---|
| 0x40 | Read |
| 0x50 | Read Reply OK |
| 0x58 | Read Reply Fail |
| 0x00 | Write |
| 0x10 | Write Reply OK |
| 0x18 | Write Reply Fail |
| 0x08 | Write Under Mask |
| 0x60 | Read (Stream) |
| 0x20 | Write (Stream) |
| 0x80 | Get Configuration Options |
| 0x82 | Configuration Options Reply |
| 0x84 | Get Address Space Info |
| 0x86 | Address Space Present Reply |
| 0x87 | Address Space Not Present Reply |
| 0x88 | Lock/Reserve |
| 0x8A | Lock/Reserve Reply |
| 0x8C | Get Unique ID |
| 0x8E | Get Unique ID Reply |
| 0xA0 | Unfreeze |
| 0xA1 | Freeze |
| 0xA8 | Update Complete |
| 0xA9 | Reset/Reboot |
| 0xAA | Factory Reset |
14.4 Read Handler Flow
When a Read command arrives, the handler:
- Sends Datagram Received OK (Reply Pending = 1).
- Extracts the address space byte from the datagram.
- Routes to the matching read callback (e.g.,
memory_read_space_configuration_memory). - If the callback is NULL, sends a Datagram Rejected with
SUBCOMMAND_UNKNOWN.
14.5 Write Handler Flow
Write commands follow the same pattern: acknowledge, extract address space, route to the write callback. The Write Under Mask variant allows bitwise modification of configuration memory (useful for flag bytes).
14.6 Retry Logic
When a Datagram Rejected reply is received:
- Temporary error (0x2xxx): The
resend_datagramflag is set on the node, and the stored datagram will be retried. - Permanent error (0x1xxx): The stored datagram is freed and the retry state is cleared.
A Datagram Received OK clears the resend flag and frees the stored datagram buffer.
The check_timeouts() function scans for timed-out pending datagrams
that have exceeded their maximum retry count.
14.7 Configuration Memory Operations
Beyond read/write, the datagram handler supports several configuration management commands:
| Operation | Purpose |
|---|---|
| Get Options | Query supported write sizes, available address spaces, and capabilities. |
| Get Address Space Info | Query size, flags, and description of a specific address space. |
| Lock/Reserve | Exclusive access to a node's configuration for multi-step writes. |
| Freeze / Unfreeze | Pause a node's normal operation while configuration is being updated. |
| Reset/Reboot | Request the node to restart (apply new configuration). |
| Factory Reset | Restore all configuration to factory defaults. |