Skip to content

Serial Protocol Specification

Hyacinthe-primus edited this page Nov 12, 2026 · 3 revisions

Serial Protocol Specification

The board and gatecore-cli exchange UTF-8, newline-delimited JSON at 2000000 baud. Each control message is one JSON object on one line. Bulk operations switch to raw bytes after a JSON header: import_bin, export_bin, sync_manifest and sync_apply.

The firmware side is in SerialProtocol.cpp and SystemController.cpp; the CLI side is in gatecore-cli/src/protocol.rs and gatecore-cli/src/serial.rs.

Firmware 1.2.0 reports protocol_version as hybrid-json-ctrl+bin-bulk/v1. Protocol changes are additive: the CLI ignores unknown response fields, and optional fields from older firmware have defaults.

Host to device

type Fields Meaning
add uid, name, registered, valid_days Add a user. Omit either date field for an admin badge.
batch_add users Add users in batches (JSON import path).
import_begin none Enter import mode and pause the reader.
import_bin bytes Announce a raw binary import, then send records.
import_end none Finish the import and save.
import_abort none Abandon an unfinished import and reload flash (1.2+).
remove uid Delete a user.
clear_all none Delete all users.
remove_all_except uids Delete every user not in the list.
rename uid, name Rename a user.
find uid Binary search by UID.
find_name query Case-insensitive substring search on names.
list none Stream the full user list as JSON.
export_bin none Stream the database as raw records.
sync_begin none Return the device db_crc32 and user count.
sync_manifest none Stream uid + record CRC32 entries.
sync_apply remove, add, replace counts Apply the raw sync stream.
enter_scan_mode continuous* Report the next card without checking access. continuous: true stays armed after a card.
exit_scan_mode none Leave scan mode.
status none Return database, flash and version information.
net_status none Return Wi-Fi state.
get_time none Return the device time.
ntp_sync none Force an NTP resync.
enter_renewal_mode valid_days Enter tag renewal mode.
exit_renewal_mode none Leave renewal mode.
configure_wifi ssid, password Store credentials and connect.
configure_timezone gmt_offset_sec, daylight_offset_sec* Store the timezone and apply it.

registered and valid_days must be omitted from add, not sent as null. The firmware checks whether the keys exist; either one missing makes the badge an admin badge.

import_bin must follow import_begin. Its bytes count must be a non-negative multiple of 74 and describes the record payload, not the 7-byte file header. sync_apply sets import mode itself and does not need import_begin.

Device to host

Common replies:

{"status":"ok"}
{"status":"error","message":"Duplicate UID"}
{"status":"ok","type":"uid_detected","uid":"04AABBCCDD"}
{"status":"ok","type":"remove_all_except","removed_count":3}

A status reply contains:

{
  "status": "ok",
  "type": "status",
  "firmware_version": "1.2.0",
  "protocol_version": "hybrid-json-ctrl+bin-bulk/v1",
  "db_path": "/users.bin",
  "fs_total_bytes": 12582912,
  "fs_used_bytes": 148000,
  "fs_free_bytes": 12434912,
  "user_count": 2000,
  "db_crc32": 3234567890
}

Other typed replies:

{"status":"ok","type":"wifi_status","connected":true,"message":"..."}
{"status":"ok","type":"timezone","applied":true,"gmt_offset_sec":3600,"daylight_offset_sec":0,"message":"..."}
{"status":"ok","type":"net_status","connected":true,"ssid":"...","ip":"...","rssi":-58,"time_synced":true}
{"status":"ok","type":"time","epoch":1783107800,"formatted":"2026-07-03 19:43:20"}
{"status":"ok","type":"ntp_sync","synced":true,"message":"2026-07-03 19:45:00"}
{"status":"ok","type":"batch_add_result","added":100,"errors":0,"failed":[]}
{"status":"ok","type":"batch_add_result","added":98,"errors":2,"failed":[{"uid":"...","message":"Duplicate UID"}]}
{"status":"ok","type":"find_result","uid":"04A1B2C3","name":"Alice","registered":"2026-01-15","valid_days":365,"search_us":42}
{"status":"error","type":"find_result","message":"UID not found","search_us":38}
{"status":"ok","type":"renewal_result","uid":"...","name":"...","registered":"...","valid_days":30}
{"status":"ok","type":"export_bin","bytes":370000,"count":5000}
{"status":"ok","type":"import_bin_result","added":5000,"errors":0}
{"status":"ok","type":"sync_begin","db_crc32":3234567890,"count":5000}
{"status":"ok","type":"sync_manifest","bytes":75000,"count":5000}
{"status":"ok","type":"sync_result","removed":2,"added":3,"replaced":1,"errors":0,"db_crc32":1234567890,"remove_phase_ms":0,"add_phase_ms":0,"replace_phase_ms":1,"save_ms":110}

get_time returns an error while NTP is unsynced. epoch is raw UTC; formatted is the device's local time.

import_result is sent by import_end. Its added field is the total user count after the save, not the number added by that import. Per-record rejections are reported by import_bin_result or batch_add_result.

configure_timezone resyncs NTP immediately when Wi-Fi is connected. Offline, the firmware stores and applies the offset and keeps it for the next sync.

Raw formats

Records

A .bin file contains a 7-byte header followed by fixed 74-byte records, little-endian. The raw import_bin and export_bin streams carry only the records; the header stays in the JSON control message for export_bin, and the CLI adds or strips it when reading and writing files.

header = "RUD1"  version:u8  record_size:u16
record = uid_len:u8  uid:[u8;10]  name_len:u8  name:[u8;48]
         reg_days:u16  valid_days:f64  crc32:u32
  • reg_days is days since 1970-01-01 UTC. 0xFFFF marks an admin badge.
  • valid_days is -1.0 for admin badges.
  • The per-record CRC32 (IEEE, zlib-compatible) covers the first 70 bytes.
  • db_crc32 chains the CRC over the first 70 bytes of every record in UID order. The trailing record CRC is excluded; including it would make the database checksum depend only on the record count.

For export_bin, the bytes and count values travel in the JSON header. The raw stream contains records only. The CLI prepends the file header when it writes a .bin file.

import_bin also sends records only. The CLI strips the file header and filters corrupt records and trailing partial bytes before sending. The firmware checks the payload length against bytes and reads it in chunks.

Sync entries

sync_manifest sends one entry per user:

uid_len:u8  uid:[u8;10]  record_crc32:u32     // 15 bytes

record_crc32 is the same CRC stored at the end of the 74-byte record.

The remove list in sync_apply uses:

uid_len:u8  uid:[u8;10]                        // 11 bytes

The raw payload is remove_count remove entries, then add_count 74-byte records, then replace_count 74-byte records. There is no framing between the phases; the counts in the preceding sync_apply message determine the lengths.

Session behaviour

  • Boot. After a reset the board prints non-JSON trace lines. On the first boot after a flash, formatting the 12 MB LittleFS can take 10 to 30 seconds. The CLI opens the port with DTR and RTS low and polls status for up to 45 seconds.
  • Noise. Lines that are not JSON objects are skipped. They do not consume a reply attempt or trigger a resend.
  • Events. uid_detected and renewal_result can arrive while the board is in scan or renewal mode. The CLI queues them instead of treating them as the next command's reply.
  • Retries. A request with no reply is attempted up to three times, with a 2-second idle timeout by default. Requests that are not safe to repeat are sent once: import_bin, sync_apply, import_end, configure_wifi, export_bin and sync_manifest. The JSON import path retries a timed-out batch once and reclaims a "Duplicate UID" result from the first attempt. A retried add that returns "Duplicate UID", or a retried remove that returns "UID not found", is treated as a lost reply. Replies have no request ID, so this is a mitigation, not a guarantee.
  • Raw transfers. There is no framing to resync on. The CLI treats 10 seconds without a byte as a stall and fails the transfer; reconnect and retry. The firmware's limit is 5 seconds per chunk. For import_bin and sync_apply, the firmware allocates its buffers before sending ok, then reads the payload.
  • Import mode. import_begin pauses the reader. Finish with import_end, or call import_abort on failure. The firmware also abandons an unfinished import after IMPORT_IDLE_TIMEOUT_MS (2 minutes) without serial traffic and reloads the flash copy.

Clone this wiki locally