Synchronization and Channel Coding (CCSDS 131.0-B, 231.0-B)
README.md

ocaml-scc #

CCSDS synchronisation and channel coding for space links, in pure OCaml.

A radio link to a spacecraft delivers a stream of bits with errors in it. The
synchronisation and channel coding sublayer of the CCSDS stack is what finds
the frame boundaries in that stream and corrects the errors: on the
telecommand uplink, CCSDS 231.0-B
wraps each frame in a CLTU of BCH-coded codeblocks, and on the telemetry
downlink, CCSDS 131.0-B prefixes
each frame with an Attached Sync Marker and protects it with Reed-Solomon,
convolutional, turbo or LDPC codes. scc implements both directions without
I/O, and scc-eio reads and writes the framed streams over Eio flows.

A CLTU (Communications Link Transmission Unit) is the start sequence 0xEB90,
then the frame cut into 7-octet pieces, each followed by one octet of
BCH(63,56) parity, then a tail sequence. The Attached Sync Marker is the
4-octet pattern 0x1ACFFC1D placed before every telemetry frame, so the
receiver can find frame boundaries in the bit stream. Reed-Solomon (255,223)
corrects up to 16 wrong octets per 255-octet codeword; at interleaving depth I
the transmitter interleaves I codewords octet by octet, so a burst of up to
16 × I consecutive corrupted octets spreads over the I codewords and is
corrected.

The sublayer sits between the physical layer and the data link protocols,
which the tc, tm and aos packages implement:

   Space Packet / TC Frame / TM Frame
              |
   +----------v-----------+
   |  Data Link Protocol   |  tc, tm, aos, ...
   +-----------+-----------+
              |
   +----------v-----------+
   |  Synchronization &    |  scc
   |  Channel Coding (SCC) |
   +-----------+-----------+
              |
   Physical Layer (RF)

Installation #

Install with opam:

opam install scc scc-eio

If opam cannot find the packages, they may not yet be released in the public
opam-repository. Add the overlay repository, then install them:

opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
opam update
opam install scc scc-eio

Usage #

The uplink wraps a TC frame in a CLTU. The downlink randomises a frame, adds
Reed-Solomon parity at interleaving depth 5 and convolutionally encodes the
result. On the receiving side, Sync.Cltu_sync.read pulls every CLTU out of
a bytesrw reader and leaves the bytes after each one on the reader for the
next call:

(* TC uplink: frame -> CLTU. *)
let uplink tc_frame = Scc.Sync.Cltu.encode tc_frame

(* TM downlink: randomize + RS encode + convolutional encode. *)
let downlink frame =
  let r = Scc.Coding.Randomizer.v () in
  Scc.Coding.Randomizer.apply r frame 0 (Bytes.length frame);
  let codeword = Scc.Coding.Reed_solomon.encode ~interleave:I5 frame in
  Scc.Coding.Convolutional.encode codeword

(* Stream parsing: pull every CLTU out of a bytesrw reader. Sync.read drives
   the parser across slices and leaves the bytes after each frame on the reader
   for the next call. *)
let cltus_of_reader r =
  let rec loop state acc =
    match Scc.Sync.Cltu_sync.read state r with
    | state, Some frame -> loop state (frame :: acc)
    | _, None -> List.rev acc
  in
  loop (Scc.Sync.Cltu_sync.init ()) []

scc-eio gives the same framing over Eio flows. A receiver keeps the bytes
after each frame, so back-to-back CLTUs in one read are all recovered:

let serve ~source ~sink ~process_tc tm_frame =
  (* Receive CLTU-framed TC from a raw byte stream. The receiver holds the
     bytes after each frame, so back-to-back CLTUs are all recovered. *)
  let rx = Scc_eio.Cltu.of_source source in
  (match Scc_eio.Cltu.recv rx with
  | Ok frame -> process_tc frame
  | Error `Closed -> ()
  | Error (`Transport _) -> Fmt.epr "transport error@."
  | Error (`Error msg) -> Fmt.epr "decode error: %s@." msg);
  (* Send ASM-framed TM. *)
  match Scc_eio.Asm.send sink tm_frame with
  | Ok () -> ()
  | Error (`Error msg) -> Fmt.epr "send error: %s@." msg

API #

Synchronisation lives in Scc.Sync:

Module Standard Description
Scc.Sync.Cltu 231.0-B CLTU encoding and decoding with BCH(63,56)
Scc.Sync.Asm 131.0-B ASM (0x1ACFFC1D) insertion and detection
Scc.Sync.Cltu_sync 231.0-B CLTU extraction from a byte stream
Scc.Sync.Asm_sync 131.0-B ASM-framed data extraction from a byte stream

Channel coding lives in Scc.Coding:

Module Standard Description
Scc.Coding.Randomizer 131.0-B section 9 LFSR pseudo-random sequence
Scc.Coding.Reed_solomon 131.0-B section 4 RS(255,223), interleaving depth 1 to 5 or 8
Scc.Coding.Convolutional 131.0-B section 3 rate 1/2, K=7 (G1=0x79, G2=0x5B)
Scc.Coding.Turbo 131.0-B section 6 parallel concatenated, BCJR/MAP decoding
Scc.Coding.Ldpc 131.0-B section 7 belief-propagation decoding

The generic algorithms live in the reed-solomon, viterbi, turbo and
ldpc packages; this package supplies the CCSDS parameters.

The CLTU parser drops a candidate longer than 2048 bytes without reporting an
error and resumes searching for a start sequence.

Licence #

ISC. See LICENSE.md.