Metadata-Version: 2.4
Name: fprime-gds
Version: 4.4.2a1
Summary: F Prime Flight Software Ground Data System layer
Author-email: Michael Starch <Michael.D.Starch@jpl.nasa.gov>, Thomas Boyer-Chammard <Thomas.Boyer.Chammard@jpl.nasa.gov>
License-Expression: Apache-2.0
Project-URL: Homepage, https://fprime.jpl.nasa.gov
Project-URL: Documentation, https://nasa.github.io/fprime/
Project-URL: Repository, https://github.com/fprime-community/fprime-gds
Keywords: fprime,embedded,nasa,flight,software
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: Operating System :: Unix
Classifier: Operating System :: POSIX
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Python :: Implementation :: PyPy
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE.txt
Requires-Dist: flask>=3.0.0
Requires-Dist: flask_compress>=1.11
Requires-Dist: pyzmq>=24.0.1
Requires-Dist: pexpect>=4.8.0
Requires-Dist: pytest>=6.2.4
Requires-Dist: flask_restful>=0.3.8
Requires-Dist: fprime-tools>=3.4.3
Requires-Dist: argcomplete!=3.7.1,>=1.12.3
Requires-Dist: Jinja2>=2.11.3
Requires-Dist: openpyxl>=3.0.10
Requires-Dist: pyserial>=3.5
Requires-Dist: pydantic>=2.6
Requires-Dist: PyYAML>=6.0.2
Requires-Dist: spacepackets>=0.30.0
Requires-Dist: crcmod==1.7
Requires-Dist: lark>=1.3.0
Dynamic: license-file

# F´ GDS

**Note:** This README describes GDS internals. Refer to the [F´ Documentation](https://fprime.jpl.nasa.gov/latest/docs/user-manual/overview/gds-introduction/) for instructions on how to use the GDS.

Issues should be reported here: [File an issue](https://github.com/nasa/fprime/issues/new/choose)

## Overview
The GDS consists of a collection of classes and tools that provide an interface for fprime
deployments, allowing users to view telemetry and events and send commands.

The GDS HTML GUI is an almost completely rewritten version of the F´ GSE UI, our historical
ground system that has been deprecated due to its Python 2 requirement.

The GDS was designed to be adaptable, easily understandable, and easily expandable. To this end, it
is built using publisher/subscriber relationships.

The diagram below shows the basic layout of the GDS. Data from the F´ deployment first enters the
GDS pipeline through the transport client, a `data_transport` plugin (see below). Each packet is then passed directly to the distributor which is responsible
for parsing the packets in to data messages and sending on each message type (currently only events,
channels, and packetized telemetry are supported) to decoders registered for that type. The decoder
is responsible for turning that data message into a data object which it passes along to all
consumers registered to it. These consumers could be anything, but in the GDS they are GUI panels
that display the data. For outgoing data, the structure is similar. Currently, commands are the only
output data type included. Command data objects are created in the command panel and then sent to
the command encoder registered to that panel. Encoders take a data object and turn it into binary
data that can be sent to the F´ deployment. The binary data is then passed to the transport client
which is registered to the encoder. Finally, the transport client sends the data back to the ground
system holding the link to the F´ deployment.

The transport client is selected with `--data-transport-selection`: `zmq` (ZeroMQ to the GDS communication
process, the default), `threaded-tcp-server` (the threaded TCP socket server, formerly `--no-zmq`), or any
installed plugin reaching another ground system (e.g. `yamcs` from the fprime-yamcs package).

![The layout of the GDS](https://raw.githubusercontent.com/nasa/fprime/refs/heads/devel/docs/img/gds_layout.jpg)

All of these objects are created and registered to other objects when the GDS
is initialized. Thus, all of the structure of the GDS is created in one place,
and can be easily modified.

## GDS Tools
The GDS was designed to have flexible configurations of consumers for its various data decoders.
This has been used to support several additional tools.

### GDS Standard Pipeline
The standard pipeline can be thought of as a Python helper-layer to instantiate the GDS and connect
to an F´ deployment. The pipeline provides event, telemetry and command histories, sending
commands and registering consumers to the GDS decoders. The Standard Pipeline can be found
[here](src/fprime_gds/common/pipeline/standard.py).

### GDS Integration Test API
The Integration Test API is a tool that provides the ability to write integration-level tests for an F´ deployment using the GDS. The tool provides history searches/asserts, command sending, a detailed test log, sub-histories and convenient access to GDS data objects. The test API comes with its own [user guide](https://fprime.jpl.nasa.gov/latest/docs/user-manual/gds/gds-test-api-guide/) and is built on top of the Standard Pipeline.

### Data Product Tools
`fprime-dp` decodes and validates F´ data product files (`.fdp`) against a JSON dictionary. `fprime-dp decode`
writes the raw records of any product as JSON, `fprime-dp validate` checks header and data checksums, and
`fprime-dp comlog` decodes the products written by `Svc.ComLoggerDp` into the events and telemetry channels they
recorded.

### F Prime Communication Bridge
`fprime-comm-bridge` bridges bidirectional communication between an F´ endpoint, reached through a
GDS communication adapter plugin (`--communication-selection`: `tcp-fast-server` by default, or
`tcp-fast-client`, `uart`, `ip`, or any installed adapter plugin), and a ground system exchanging
packets as UDP datagrams such as YAMCS or OpenC3 COSMOS. The ground side is the `udp-fast`
communication adapter, configured through its own options: deframed packets are sent one per
datagram to `--udp-fast-address`:`--udp-fast-send-port` (default `127.0.0.1:50000`) and command
datagrams received on `--udp-fast-bind-address`:`--udp-fast-recv-port` (default `127.0.0.1:50001`)
are framed and written to the endpoint. Command datagrams are accepted only from the peer address,
loopback, and hosts supplied via `--udp-fast-allowed-source`. Because the plugin system instantiates one
communication selection per process and `udp-fast` is the bridge's ground-side adapter, `udp-fast` cannot
also be selected as the endpoint-side communication of the bridge.

A single GDS framing plugin stage (`--framing-selection`) sits between the two sides. The default
`tm-frame-aggregator` re-establishes CCSDS TM transfer frame boundaries in the endpoint byte stream
(frame size and spacecraft ID from `--dictionary`/`--deployment`, or `--frame-size`/`--scid`) and
passes uplink frames through unchanged, so the ground system performs TM/TC (de)framing itself.
`raw-space-data-link` exposes CCSDS Space Packets instead: TM frames are deframed on downlink and
the bridge emits one datagram per Space Packet (idle packets dropped), while each uplink datagram
is one Space Packet framed into a TC frame. `no-op` passes bytes through unchanged, and `fprime`
handles the legacy F´ start-word/length framing.

```bash
fprime-comm-bridge --deployment build-artifacts/Linux/Ref --udp-fast-send-port 50000 --udp-fast-recv-port 50001
fprime-comm-bridge --communication-selection uart --uart-device /dev/ttyUSB0 --uart-baud 115200 \
    --framing-selection raw-space-data-link --dictionary build-artifacts/Linux/Ref/dict/RefTopologyDictionary.json
```

Operational notes: a ground datagram the framer cannot frame (e.g. a Space Packet too large for a
TC frame) is dropped with a warning and the bridge keeps running, whereas a failure of either pump
(an adapter error) stops the bridge with a non-zero exit status so a supervisor can restart it.
Unframed downlink data that never resynchronises is discarded once it exceeds ~640 KB, with a
warning at the start of each such outage. With `no-op` framing over a stream adapter (TCP, UART)
packet boundaries are not preserved: each `read()` chunk becomes one datagram.

`udp-fast` is also an ordinary communication adapter plugin (`fprime-gds --communication-selection udp-fast`,
`fprime-cli`, ...) for datagram-oriented flight links. Unlike `udp`, it reads the socket directly in the
caller's thread (no receive thread or queue), returns exactly one datagram per read, binds its receive
port to `--udp-fast-bind-address` (`127.0.0.1` by default rather than all interfaces), and drops datagrams
from sources other than `--udp-fast-address`, loopback, and `--udp-fast-allowed-source` hosts.

The bridge's integration tests (`test/fprime_gds/common/communication/bridge/test_comm_bridge.py`) use
`socat` to emulate a UART endpoint; without it those tests are skipped.

## GDS GUI Usage

A guide for how to use the GDS is available in the [F Prime documentation](https://fprime.jpl.nasa.gov/latest/docs/user-manual/overview/gds-introduction)

## Classes
The GDS back end is composed of several different data processing units. For 
most of the units described below, a base class describes the interface and 
subclasses implement the interface for specific data types (such as events,
channels, etc).

To expand the GDS to accept more data types or have additional features, new classes 
can be written and registered into the existing structure.

### Transport Client
The transport client (`data_transport` plugin: ZeroMQ, the threaded TCP server, or an external ground
system) is simply a passthrough for data coming from the ground side of the link. The client handles
all the connection overhead and passes un-parsed data onto all objects registered with it.

### Distributor
The distributer is responsible for taking in raw binary data, parsing off the 
length and descriptor, and then passing the data to all decoders registered to 
that descriptor. Descriptor types include events, channels, packets, etc (a full
enumeration can be found in (config_manager.py). The binary data that 
the descriptor receives should be of the form:

```mermaid
---
title: "Binary format received by Distributors"
---
packet-beta
  0-31: "Length [4 bytes]"
  32-63: "Type Descriptor [4 bytes]"
  64-95: "Message Data [variable length]"
```

The distributor should then pass only the message data along to the decoders. 

### Templates
For each general data type (channel, event, etc) there is a template type. 
Instances of these classes hold information about specific channels or event 
types (ex. the NumPkts channel or the FirstPacketReceived event).

Template classes hold information such as the channel/event/packet's id, name, 
argument types, value type, format string, etc. This information is used by 
decoders when parsing data that they receive. 

### Data Types
For each general data type (channel, event, etc) there is a type class. 
Instances of these classes hold information about a specific channel reading or 
event. They contain the actual data parsed by the decoders. As such, they are 
the data type returned by encoders. All of these classes have a time field are 
derived from type SysData, which implements a compare function, allowing any list 
of SysData objects to be sorted by time. Each inherited type should also 
implement the __str__ method so the objects can be easily printed. 

Each instance of a type class also has a reference to the corresponding template
class for that channel or event type.

### Loaders
Loaders are used to construct dictionaries of channel and events. These 
dictionaries have template classes as values and ids or names as keys. 

Each dictionary type has their own loader, but subclassing is used to prevent
code duplication. For example, there are loaders for channel and event python
file dictionaries, but they both subclass the python loader class which provides
helper functions for reading python file dictionaries. 

### Decoders
Decoders are responsible for parsing the message data for a specific descriptor
type. 

Each decoder uses dictionaries produced by loaders to help with its parsing.
These are given to the decoder's constructor.

The knowledge for how to parse that descriptor type should stay within the 
decoder. Each decoder type takes in the binary message data, parses it, and 
sends the resulting data object to all consumers registered to it. 

### Encoders
Encoders are responsible for taking data objects from consumers (GUI panels), 
converting them to binary data, and passing them to the transport client to send to 
the F´ deployment. 

Like the decoders, encoders use dictionaries produced by loaders to help craft
the binary output.

### Consumers
Consumers do not have a specific base class, but instead simply implement a data
callback method that is called by decoders with parsed data objects as the 
argument. In the case of the Gds, the consumers are the GUI panels that display 
data. Consumers can also produce data that is sent to encoders and eventually on
to the F´ deployment. 

### Main Frame Factory
This class is responsible for setting up the pipeline of data between different 
components in the publisher/subscriber interface - that is, it is 
responsible for registering all of the various components that wish to share
data. This class also supports the creation of multiple Gds GUI windows which
all share the same subscriptions and therefore receive the same data. 

### ConfigManager
The `ConfigManager` class is a singleton config class responsible for storing 
configurations used by GDS classes, including internal GDS types and constants,
as well as configuration from the FSW JSON dictionary. It can be controlled via
the `set_*` methods, but it is recommended to use the `DictionaryParser` class
to automatically load FSW dictionary data into the config manager.

### Framing Plugins
Framing plugins (`--framing-selection`) frame uplink data and deframe downlink
bytes into packets in `fprime_gds.common.communication`. The CCSDS plugins in
`fprime_gds.common.communication.ccsds` are chains of single-purpose stages
(`ChainedFramerDeframer`); deframing runs the stages from the link inward:

| Plugin | Deframing stages (outermost first) | Deframed element |
|---|---|---|
| `fprime` | F Prime framing | F Prime packet |
| `raw-space-packet` | Space Packet splitter, Space Packet header strip | Space Packet payload |
| `raw-space-data-link` | TM Space Data Link, Space Packet splitter | whole Space Packet (header included) |
| `space-packet-space-data-link` | TM Space Data Link, Space Packet splitter, Space Packet header strip | Space Packet payload |
| `space-packet-splitter` | Space Packet splitter | whole Space Packet (header included) |
| `tm-frame-aggregator` | TM frame aggregation | whole TM frame |

The Space Packet splitter (`space_packet_splitter.py`) is the single home of
Space Packet chopping: it yields exactly one whole non-idle Space Packet per
deframed element, drops idle packets (APID 0x7FF), resynchronizes on bytes that
cannot start a valid header, and holds a partial packet until the rest of a byte
stream arrives. The Space Packet header strip (`space_packet.py`) then expects
exactly one whole packet, removes the primary header, and checks the sequence
count. Uplink framing runs the stages in the reverse order; the splitter passes
uplink data through unchanged.

## Modify GDS Structure
To setup the structure of the GDS, instances of the above classes are first
created. Then, they are registered to one another by calling the data producer's
`register` function with the data consumer as the argument. The data consumer 
is expected to implement a callback function to receive the data (`data_callback`
for most classes, but check the base class's documentation for details).

An example of how to instantiate and register classes into the correct structure 
can be found in the `MainFrameFactory` class.

## Setup
The GDS requires the packages specified in [pyproject.toml](pyproject.toml).

These can be installed along the Gds package using the following commands:

```
pip install --upgrade fprime-gds
```

For full installation instructions, including virtual environment creation and installation verification, see [INSTALL.md](https://github.com/nasa/fprime/blob/devel/docs/INSTALL.md).

## Running Tests
Python tests run with `pytest`. The GDS frontend JavaScript tests require Node.js (>= 18):

- Under `pytest`, they run automatically when a suitable `node` is on the PATH.
- Locally they are skipped without a suitable `node`; on CI a missing or too-old `node` fails the
  test (CI relies on the runner image's preinstalled Node).
- Run them directly with `node --test test/fprime_gds/flask/js/*.test.mjs` (or one file at a time).
- The `package.json` (`"type": "module"`) in `src/fprime_gds/flask/static/js/` exists only so Node
  imports the GDS frontend sources as ES modules when running these tests; browsers ignore it.

## Generate Documentation
You can generate a doxygen documentation page for the GDS source.
To do this, you will need to install `doxygen`, `doxypypy` and `graphviz`.

### Linux/Windows WSL
```
apt-get install doxygen graphviz
pip install doxypypy
```

### Mac
Install doxygen from the website (http://www.stack.nl/~dimitri/doxygen/download.html) or by using the following command if you have Homebrew
```
brew install doxygen
brew install graphviz
pip install doxypypy
```

Next, make `docs/py_filter` available in your system path however you see fit.
Now you can run `doxygen Doxyfile` in the root directory to generate documentation in `docs/doxy/index.html`
