- OCaml 91%
- Python 5%
- C 4%
- Java <1%
- Shell <1%
cfdp #
An OCaml implementation of the CCSDS File Delivery Protocol (CFDP, CCSDS 727.0-B-5), the standard way to move files to and from a spacecraft.
A pass is one interval during which a satellite is above a ground station's horizon and the two can talk; in low Earth orbit it lasts about 5 to 15 minutes and recurs a few times a day, so a transfer that does not fit in one pass must resume in the next. The link also drops packets, and round trips to deep space take minutes, so FTP over TCP does not work. CFDP splits a file into segments sent as protocol data units (PDUs), lets the receiver report the gaps it found, and resends only those, across as many passes as it takes. It has two classes of service:
- Class 1 transfers are unacknowledged: the sender sends every segment once, for links where loss is acceptable or handled below.
- Class 2 transfers are acknowledged: the receiver sends negative acknowledgements (NAKs) for missing ranges and the sender retransmits them.
The code comes in two packages:
cfdphas no I/O. It provides the PDU types and codecs and the four transfer state machines, each a pure transition from an event to a new state and a list of actions.cfdp-eiodrives those state machines over TCP, UDP or any PDU carrier with Eio, and reads and writes the files.
Installation #
Install with opam:
$ opam install cfdp cfdp-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 cfdp cfdp-eio
Usage #
Send a file #
let send_firmware env =
Eio.Switch.run @@ fun sw ->
let conn =
let net = (env#net :> [ `Generic ] Eio.Net.ty Eio.Resource.t) in
Cfdp_eio.connect ~sw ~net ~host:"flatsat-1" ~port:1734 ()
in
match
Cfdp_eio.Sender.send_file conn
~src:Eio.Path.(env#fs / "firmware.elf")
~dst:"firmware.elf"
()
with
| Ok tid -> Format.printf "Done: %a@." Cfdp.pp_transaction_id tid
| Error msg -> Format.eprintf "Error: %s@." msg
Receive a file #
let receive_firmware env =
Eio.Switch.run @@ fun sw ->
let conn =
let net = (env#net :> [ `Generic ] Eio.Net.ty Eio.Resource.t) in
Cfdp_eio.connect ~sw ~net ~host:"flatsat-1" ~port:1734 ()
in
match
Cfdp_eio.Receiver.recv_file conn ~clock:env#clock
~dest_dir:Eio.Path.(env#fs / "incoming")
()
with
| Ok (path, tid) ->
Format.printf "Received %a (txn %a)@."
Eio.Path.pp path Cfdp.pp_transaction_id tid
| Error msg -> Format.eprintf "Error: %s@." msg
Both calls take an optional ~on_progress callback, which receives a
Cfdp_eio.progress record of bytes and segments sent out of the totals.
Cfdp_eio.of_datagram runs the same transfers over UDP, and
Cfdp_eio.of_transport over any function pair that sends and receives one
PDU, such as a CCSDS space link carrying each PDU in a space packet.
Features #
- PDU headers with variable-length entity IDs (1 to 8 bytes). An entity is one end point that runs the protocol and has a file store, such as a ground station or an on-board computer; a transaction is named by the source entity's ID and a sequence number.
- The file directive PDUs: Metadata, EOF, ACK, NAK, Finished, Prompt and Keep Alive. A Prompt, from the sender, asks the receiver to send a NAK (its list of missing ranges) or a Keep Alive now rather than when its own timer fires; a Keep Alive, from the receiver, reports how many bytes it has received contiguously, so the sender knows the transfer is progressing.
- Segmented file data with optional record boundaries and segment metadata.
- The 16-bit CCSDS Telecommand CRC on PDUs (CCSDS 727.0-B-5, Section 4.1.3).
- File checksums: modular (identifier 0), CRC-32C (2), CRC-32 (3) and null (15) of the SANA Checksum Identifiers registry. The modular checksum cuts the file into 4-byte words aligned on their offset from the start of the file (a short last word padded with zeros), reads each as a big-endian unsigned integer and sums them modulo 2^32, so segments can be added in whatever order they arrive (CCSDS 727.0-B-5, section 4.2.2).
- Both receivers compare the file's checksum with the EOF checksum (section 4.6.1.2.8); a Class 2 mismatch cancels the transaction with a File Checksum Failure. A Metadata PDU naming an identifier this library does not implement raises the Unsupported Checksum Type fault and completes under the null checksum with condition 'Unsupported checksum type' (section 4.2.2.8).
- Both receivers raise the File Size Error fault (table 5-5) for a File Data PDU reaching past the file size its Metadata declared, and discard it: the fault's handler is Ignore (section 4.8), so a forged segment cannot end the transaction it names. A declared size of 0, a file of unbounded size, bounds nothing.
- State machines for the four transfer roles,
Sender1,Sender2,Receiver1andReceiver2. - Timers for NAK, EOF, Finished, inactivity and Keep Alive.
Low-level API #
Driving a state machine #
A custom transport, or a simulation in one process, drives the state machines directly. This is the class 1 sender:
let sender = Cfdp.Sender1.v ~local:(Cfdp.Entity_id.of_int_exn 1)
let req =
Cfdp.put_request
~dest_entity:(Cfdp.Entity_id.of_int_exn 2)
~source_filename:"firmware.elf"
~dest_filename:"firmware.elf"
()
let file_size = 4096L
let sender, actions =
Cfdp.Sender1.incoming sender
(Cfdp.Sender1.Put_request (req, file_size))
Each event returns actions such as Send_metadata and Send_file_data, and
each action carries its complete PDU header. The caller offers each file
segment with Segment_read. The machine emits the File Data PDU, adds the
segment to the running file checksum, and after the last segment emits
Send_eof with the checksum of the whole file.
Encoding and decoding PDUs #
open Cfdp
let source = Entity_id.of_int_exn 1
let dest = Entity_id.of_int_exn 2
let config = default_config
let meta =
metadata
~file_size:1024L
~source_filename:"data.bin"
~dest_filename:"data.bin" ()
let hdr : header =
{ version = 1;
pdu_type = Directive;
direction = Receiver;
transmission_mode = Unacknowledged;
crc_present = false;
large_file = false;
segment_ctrl = false;
segment_metadata = false;
source_entity = source;
transaction_seq = 1L;
dest_entity = dest;
data_len = 0;
}
let bytes = encode config (Directive (hdr, Metadata meta))
let () =
match decode bytes with
| Ok (pdu, _consumed) -> Format.printf "%a@." pp_pdu pdu
| Error e -> Format.printf "Error: %a@." pp_error e
State machines #
The four machines cover the two classes and the two ends of a transfer:
| Module | Class | Role |
|---|---|---|
Cfdp.Sender1 |
1 (unacknowledged) | Sender |
Cfdp.Sender2 |
2 (acknowledged) | Sender with NAK retransmission |
Cfdp.Receiver1 |
1 (unacknowledged) | Receiver |
Cfdp.Receiver2 |
2 (acknowledged) | Receiver with NAK generation |
A machine asks its caller to run a timer with a Start_timer or
Cancel_timer action naming it by Cfdp.timer_id (Nak, Eof, Finished,
Inactivity and Keep_alive). The inactivity timer's expiry reaches a
receiver as its Inactivity_timeout event, and any other timer's reaches a
class 2 machine as a Timer_expired event. Cfdp_eio.Timers runs them on an
Eio clock.
Related work #
- ION, the DTN implementation in C from NASA JPL, includes CFDP.
References #
- CCSDS 727.0-B-5, the CFDP Blue Book
- CCSDS 720.1-G-4, the CFDP Green Book
Licence #
ISC. See LICENSE.md.