TC Transfer Frames (CCSDS 232.0-B-4)
README.md

tc #

CCSDS TC (telecommand) Transfer Frames (CCSDS 232.0-B-4) in pure OCaml.

Every command a ground station sends to a spacecraft travels in a TC Transfer Frame. The frame has a 5-byte primary header that names the spacecraft and the virtual channel, then the command data, and usually a 2-byte CRC. On the spacecraft, the decoder reads bytes that an attacker may control, so it has to check every length before relying on it. This library decodes and encodes TC frames. It covers:

  • the primary header, with the bypass and control-command flags that tell a Type-A (sequence-controlled) frame from a Type-B (expedited) one;
  • the Frame Error Control Field (FECF), a CRC-16-CCITT with generator x^16 + x^12 + x^5 + 1 (0x1021) and its register preset to 0xFFFF. It is computed over every byte from the start of the primary header to the end of the data field and appended big-endian (CCSDS 232.0-B-4, 4.1.4.2);
  • spacecraft and virtual channel identifiers as types whose range is checked;
  • decoding that checks every bound and returns an error on truncated or inconsistent input.

Installation #

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam install tc

Usage #

(* Create a TC frame. *)
let scid = Tc.scid_exn 100
let vcid = Tc.vcid_exn 2
let data = String.make 50 '\x00'
let frame = Tc.v ~scid ~vcid ~seq_num:1 ~bypass:true data
let bytes = Tc.encode frame

let () =
  match Tc.decode bytes with
  | Ok frame ->
      Fmt.pr "SCID: %d, Bypass: %b@."
        (Tc.int_of_scid frame.header.scid)
        frame.header.bypass
  | Error e -> Fmt.epr "Error: %a@." Tc.pp_error e

Frame Structure #

+----------------+-------------+------+
| Primary Header | Data Field  | FECF |
|   (5 bytes)    | (variable)  | (2B) |
+----------------+-------------+------+

The primary header holds:

  • the version (2 bits), always 00 for TC;
  • the bypass flag, set on a Type-B frame, which skips the FARM sequence check. FARM (the Frame Acceptance and Reporting Mechanism) is the spacecraft side of COP-1. It accepts Type-A frames only in order of sequence number and reports the next number it expects in the CLCW, a status word sent down in telemetry, so that the ground can send again what was lost. A Type-B frame is accepted once without that check and is never sent again; it is used in emergencies or when the sequence state has been lost;
  • the control command flag, set when the frame carries control commands and not data;
  • the spacecraft ID (10 bits) and virtual channel ID (6 bits);
  • the frame length (10 bits), which is the total length minus one. A frame is therefore at most 1024 octets including the header and the FECF, leaving up to 1017 octets of data after the 5-octet header and the 2-octet FECF;
  • the frame sequence number (8 bits).

API #

  • Tc.scid and Tc.vcid are the spacecraft ID (0 to 1023) and the virtual channel ID (0 to 63). The functions with the same names check the range and return an option, while Tc.scid_exn and Tc.vcid_exn raise.
  • Tc.header is the primary header and Tc.t a complete frame; Tc.v builds a frame.
  • Tc.decode parses a frame and Tc.encode serialises one, with or without the FECF.
  • Tc.compute_fecf computes the CRC-16-CCITT of a string.

References #

Licence #

ISC License. See LICENSE.md for details.