space-packet #
The CCSDS Space Packet Protocol (CCSDS 133.0-B-2) in pure OCaml.
The ground segment is everything on Earth that operates a spacecraft: the
antennas, the ground stations and the mission control centre. Telemetry is
what the spacecraft sends down, its measurements and status, and
telecommands are the orders sent up to it. Most of that traffic travels as
CCSDS space packets. A packet has a 6-octet primary header, which names the
application process that produced it (the APID) and holds a sequence count
and a length, followed by 1 to 65536 octets of data. The length field stores
the number of data octets minus one, so its 16 bits cover 1 to 65536 and an
empty data field cannot be encoded. On the space link, transfer frames carry
these packets, and one packet can span several frames.
This library builds, encodes and decodes space packets as defined by
CCSDS 133.0-B-2. It reads and
writes them on bytesrw streams,
reads header fields without copying the data field (View), and rebuilds
the packets carried by a stream of TM or AOS transfer frames (Vc, following
CCSDS 132.0-B-3 and 732.0-B-4).
TM and AOS frames are the fixed-length frames a spacecraft sends down, each
with a header, a data zone (the packet zone) and a trailer. Packets follow
one another across the data zones of a virtual channel, so a packet can
start in one frame and end in the next. Each frame header has a first-header
pointer, which gives the offset in the data zone of the first packet that
starts in that frame, or all ones when no packet starts there; with it the
reassembler finds the packet boundaries again after losing a frame. The
secondary header is specific to each mission and is left to the caller. The
space-packet-eio library wraps the reassembler as a blocking stream of
packets over a source of frames.
Installation #
$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam install space-packet space-packet-eio
Usage #
(* Create a telemetry packet. *)
let pkt =
Space_packet.v_exn ~packet_type:Telemetry ~apid:100
~sequence_flags:Unsegmented ~sequence_count:42 "sensor data payload"
let bytes = Space_packet.encode pkt
let () =
match Space_packet.decode bytes with
| Ok decoded -> assert (Space_packet.apid decoded = 100)
| Error `Truncated -> failwith "truncated"
| Error (`Invalid_version v) -> Fmt.failwith "invalid version: %d" v
Streaming I/O #
let send writer = Space_packet.write writer pkt
let recv reader =
match Space_packet.read reader with
| Ok pkt -> Fmt.pr "APID: %d@." (Space_packet.apid pkt)
| Error _ -> Fmt.epr "decode failed@."
Packet structure #
+------------------+------------------+
| Primary Header | Packet Data |
| (6 octets) | (1-65536 octets) |
+------------------+------------------+
Primary Header:
+---------+------+-----+------+---------+---------+-------------+
| Version | Type | SHF | APID | SeqFlag | SeqCnt | Data Length |
| 3 bits | 1b | 1b | 11b | 2 bits | 14 bits | 16 bits |
+---------+------+-----+------+---------+---------+-------------+
SHF is the secondary-header flag. It is 1 when the data field starts with a
secondary header defined by the mission. The two sequence-flag bits say
whether the packet is the first (01), a middle (00) or the last (10)
segment of a larger unit of user data, or is unsegmented (11). The 14-bit
sequence count goes up by one for each packet with the same APID and wraps
at 16384.
API #
Space_packet.vbuilds a packet, or returns an error that names the first
argument out of range;Space_packet.v_exnraises instead.Space_packet.encodeandSpace_packet.decodeconvert between packets and
strings;decodetakes~offand~lento read a packet inside a larger
buffer.Space_packet.writeandSpace_packet.readdo the same on bytesrw writers
and readers.Space_packet.apid,packet_type,sequence_flags,sequence_countand
dataread the fields, andSpace_packet.is_idleis true for the idle
APID (2047).Space_packet.View.decodeparses the header and returns the data field as
a slice of the input buffer, without a copy.Space_packet.Vc.pushgives the reassembler the first-header pointer and
the packet zone of one transfer frame, and returns the packets that frame
completes.
Related work #
- ocaml-sle implements the CCSDS
Space Link Extension protocols, which carry space packets to the ground
station. - ocaml-tcf implements the CCSDS
time code formats that secondary headers often carry. - libcsp implements the CubeSat Space
Protocol in C, a different protocol with a similar role on small satellites. - SatCat5 is a
satellite communication library in C++ and VHDL.
Licence #
ISC License. See LICENSE.md for details.