fprime-gds 3.6.1__py3-none-any.whl → 4.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. fprime_gds/common/communication/adapters/ip.py +14 -9
  2. fprime_gds/common/communication/adapters/uart.py +34 -25
  3. fprime_gds/common/communication/ccsds/__init__.py +0 -0
  4. fprime_gds/common/communication/ccsds/apid.py +19 -0
  5. fprime_gds/common/communication/ccsds/chain.py +106 -0
  6. fprime_gds/common/communication/ccsds/space_data_link.py +196 -0
  7. fprime_gds/common/communication/ccsds/space_packet.py +129 -0
  8. fprime_gds/common/communication/framing.py +27 -32
  9. fprime_gds/common/decoders/ch_decoder.py +1 -1
  10. fprime_gds/common/decoders/event_decoder.py +9 -2
  11. fprime_gds/common/decoders/pkt_decoder.py +1 -1
  12. fprime_gds/common/distributor/distributor.py +6 -3
  13. fprime_gds/common/encoders/ch_encoder.py +2 -2
  14. fprime_gds/common/encoders/cmd_encoder.py +2 -2
  15. fprime_gds/common/encoders/event_encoder.py +2 -2
  16. fprime_gds/common/encoders/pkt_encoder.py +2 -2
  17. fprime_gds/common/encoders/seq_writer.py +2 -2
  18. fprime_gds/common/fpy/README.md +56 -0
  19. fprime_gds/common/fpy/SPEC.md +69 -0
  20. fprime_gds/common/fpy/__init__.py +0 -0
  21. fprime_gds/common/fpy/bytecode/__init__.py +0 -0
  22. fprime_gds/common/fpy/bytecode/directives.py +490 -0
  23. fprime_gds/common/fpy/codegen.py +1687 -0
  24. fprime_gds/common/fpy/grammar.lark +88 -0
  25. fprime_gds/common/fpy/main.py +40 -0
  26. fprime_gds/common/fpy/parser.py +239 -0
  27. fprime_gds/common/gds_cli/base_commands.py +1 -1
  28. fprime_gds/common/handlers.py +39 -0
  29. fprime_gds/common/loaders/fw_type_json_loader.py +54 -0
  30. fprime_gds/common/loaders/json_loader.py +15 -0
  31. fprime_gds/common/loaders/pkt_json_loader.py +125 -0
  32. fprime_gds/common/loaders/prm_json_loader.py +85 -0
  33. fprime_gds/common/logger/__init__.py +2 -2
  34. fprime_gds/common/pipeline/dictionaries.py +60 -41
  35. fprime_gds/common/pipeline/encoding.py +19 -0
  36. fprime_gds/common/pipeline/histories.py +4 -0
  37. fprime_gds/common/pipeline/standard.py +16 -2
  38. fprime_gds/common/templates/cmd_template.py +8 -0
  39. fprime_gds/common/templates/prm_template.py +81 -0
  40. fprime_gds/common/testing_fw/api.py +148 -1
  41. fprime_gds/common/testing_fw/pytest_integration.py +37 -3
  42. fprime_gds/common/tools/README.md +34 -0
  43. fprime_gds/common/tools/params.py +246 -0
  44. fprime_gds/common/utils/config_manager.py +6 -6
  45. fprime_gds/common/utils/data_desc_type.py +6 -1
  46. fprime_gds/executables/apps.py +189 -11
  47. fprime_gds/executables/cli.py +468 -127
  48. fprime_gds/executables/comm.py +5 -2
  49. fprime_gds/executables/data_product_writer.py +164 -165
  50. fprime_gds/executables/fprime_cli.py +3 -3
  51. fprime_gds/executables/run_deployment.py +13 -5
  52. fprime_gds/flask/app.py +3 -0
  53. fprime_gds/flask/resource.py +5 -2
  54. fprime_gds/flask/static/addons/chart-display/addon.js +8 -3
  55. fprime_gds/flask/static/js/datastore.js +1 -0
  56. fprime_gds/flask/static/js/vue-support/channel.js +1 -1
  57. fprime_gds/flask/static/js/vue-support/event.js +1 -1
  58. fprime_gds/plugin/definitions.py +86 -8
  59. fprime_gds/plugin/system.py +172 -58
  60. {fprime_gds-3.6.1.dist-info → fprime_gds-4.0.0.dist-info}/METADATA +23 -21
  61. {fprime_gds-3.6.1.dist-info → fprime_gds-4.0.0.dist-info}/RECORD +66 -50
  62. {fprime_gds-3.6.1.dist-info → fprime_gds-4.0.0.dist-info}/WHEEL +1 -1
  63. {fprime_gds-3.6.1.dist-info → fprime_gds-4.0.0.dist-info}/entry_points.txt +2 -0
  64. fprime_gds/common/loaders/ch_py_loader.py +0 -79
  65. fprime_gds/common/loaders/cmd_py_loader.py +0 -66
  66. fprime_gds/common/loaders/event_py_loader.py +0 -75
  67. fprime_gds/common/loaders/python_loader.py +0 -132
  68. {fprime_gds-3.6.1.dist-info → fprime_gds-4.0.0.dist-info/licenses}/LICENSE.txt +0 -0
  69. {fprime_gds-3.6.1.dist-info → fprime_gds-4.0.0.dist-info/licenses}/NOTICE.txt +0 -0
  70. {fprime_gds-3.6.1.dist-info → fprime_gds-4.0.0.dist-info}/top_level.txt +0 -0
@@ -50,13 +50,11 @@ class IpAdapter(fprime_gds.common.communication.adapters.base.BaseAdapter):
50
50
  both. This data is concatenated and returned up the stack for processing.
51
51
  """
52
52
 
53
- # Interval to send a KEEPALIVE packet. None will turn off KEEPALIVE.
54
- KEEPALIVE_INTERVAL = 0.500
55
53
  # Data to send out as part of the KEEPALIVE packet. Should not be null nor empty.
56
54
  KEEPALIVE_DATA = b"sitting well"
57
55
  MAXIMUM_DATA_SIZE = 4096
58
56
 
59
- def __init__(self, address, port, server=True):
57
+ def __init__(self, address, port, server=True, keepalive_interval=0.5):
60
58
  """
61
59
  Initialize this adapter by creating a handler for UDP and TCP. A thread for the KEEPALIVE application packets
62
60
  will be created, if the interval is not none. Handlers are servers unless server=False.
@@ -64,7 +62,8 @@ class IpAdapter(fprime_gds.common.communication.adapters.base.BaseAdapter):
64
62
  self.address = address
65
63
  self.port = port
66
64
  self.stop = False
67
- self.keepalive = None
65
+ self.keepalive_thread = None
66
+ self.keepalive_interval = keepalive_interval
68
67
  self.tcp = TcpHandler(address, port, server=server)
69
68
  self.udp = UdpHandler(address, port, server=server)
70
69
  self.thtcp = None
@@ -91,11 +90,11 @@ class IpAdapter(fprime_gds.common.communication.adapters.base.BaseAdapter):
91
90
  self.thudp.daemon = True
92
91
  self.thudp.start()
93
92
  # Start up a keep-alive ping if desired. This will hit the TCP uplink, and die if the connection is down
94
- if IpAdapter.KEEPALIVE_INTERVAL is not None:
95
- self.keepalive = threading.Thread(
96
- target=self.th_alive, name="KeepCommAliveThread", args=[float(self.KEEPALIVE_INTERVAL)]
93
+ if self.keepalive_interval > 0.0:
94
+ self.keepalive_thread = threading.Thread(
95
+ target=self.th_alive, name="KeepCommAliveThread", args=[self.keepalive_interval]
97
96
  )
98
- self.keepalive.start()
97
+ self.keepalive_thread.start()
99
98
  except (ValueError, TypeError) as exc:
100
99
  LOGGER.error(
101
100
  f"Failed to start keep-alive thread. {type(exc).__name__}: {str(exc)}"
@@ -186,6 +185,12 @@ class IpAdapter(fprime_gds.common.communication.adapters.base.BaseAdapter):
186
185
  "default": True,
187
186
  "help": "Run the IP adapter as the client (connects to FSW running TcpServer)",
188
187
  },
188
+ ("--keepalive-interval",): {
189
+ "dest": "keepalive_interval",
190
+ "type": float,
191
+ "default": 0.5000,
192
+ "help": "Keep alive packet interval. 0.0 = off, default = 0.5",
193
+ },
189
194
  }
190
195
 
191
196
  @classmethod
@@ -195,7 +200,7 @@ class IpAdapter(fprime_gds.common.communication.adapters.base.BaseAdapter):
195
200
  return cls
196
201
 
197
202
  @classmethod
198
- def check_arguments(cls, address, port, server=True):
203
+ def check_arguments(cls, address, port, server=True, keepalive_interval=0.5):
199
204
  """
200
205
  Code that should check arguments of this adapter. If there is a problem with this code, then a "ValueError"
201
206
  should be raised describing the problem with these arguments.
@@ -59,7 +59,7 @@ class SerialAdapter(fprime_gds.common.communication.adapters.base.BaseAdapter):
59
59
  4000000,
60
60
  ]
61
61
 
62
- def __init__(self, device, baud):
62
+ def __init__(self, device, baud, skip_check):
63
63
  """
64
64
  Initialize the serial adapter using the default settings. This does not open the serial port, but sets up all
65
65
  the internal variables used when opening the device.
@@ -77,9 +77,14 @@ class SerialAdapter(fprime_gds.common.communication.adapters.base.BaseAdapter):
77
77
  Opens the serial port based on previously supplied settings. If the port is already open, then close it first.
78
78
  Then open the port up again.
79
79
  """
80
- self.close()
81
- self.serial = serial.Serial(self.device, self.baud)
82
- return self.serial is not None
80
+ try:
81
+ self.close()
82
+ self.serial = serial.Serial(self.device, self.baud)
83
+ return self.serial is not None
84
+ except serial.serialutil.SerialException as exc:
85
+ LOGGER.warning("Serial exception caught: %s. Reconnecting.", (str(exc)))
86
+ self.close()
87
+ return False
83
88
 
84
89
  def close(self):
85
90
  """
@@ -100,12 +105,11 @@ class SerialAdapter(fprime_gds.common.communication.adapters.base.BaseAdapter):
100
105
  :return: True, when data was sent through the UART. False otherwise.
101
106
  """
102
107
  try:
103
- if self.serial is None:
104
- self.open()
105
- written = self.serial.write(frame)
106
- # Not believed to be possible to not send everything without getting a timeout exception
107
- assert written == len(frame)
108
- return True
108
+ if self.serial is not None or self.open():
109
+ written = self.serial.write(frame)
110
+ # Not believed to be possible to not send everything without getting a timeout exception
111
+ assert written == len(frame)
112
+ return True
109
113
  except serial.serialutil.SerialException as exc:
110
114
  LOGGER.warning("Serial exception caught: %s. Reconnecting.", (str(exc)))
111
115
  self.close()
@@ -121,17 +125,16 @@ class SerialAdapter(fprime_gds.common.communication.adapters.base.BaseAdapter):
121
125
  """
122
126
  data = b""
123
127
  try:
124
- if self.serial is None:
125
- self.open()
126
- # Read as much data as possible, while ensuring to block if no data is available at this time. Note: as much
127
- # data is read as possible to avoid a long-return time to this call. Minimum data to read is one byte in
128
- # order to block this function while data is incoming.
129
- self.serial.timeout = timeout
130
- data = self.serial.read(1) # Force a block for at least 1 character
131
- while self.serial.in_waiting:
132
- data += self.serial.read(
133
- self.serial.in_waiting
134
- ) # Drain the incoming data queue
128
+ if self.serial is not None or self.open():
129
+ # Read as much data as possible, while ensuring to block if no data is available at this time. Note: as much
130
+ # data is read as possible to avoid a long-return time to this call. Minimum data to read is one byte in
131
+ # order to block this function while data is incoming.
132
+ self.serial.timeout = timeout
133
+ data = self.serial.read(1) # Force a block for at least 1 character
134
+ while self.serial.in_waiting:
135
+ data += self.serial.read(
136
+ self.serial.in_waiting
137
+ ) # Drain the incoming data queue
135
138
  except serial.serialutil.SerialException as exc:
136
139
  LOGGER.warning("Serial exception caught: %s. Reconnecting.", (str(exc)))
137
140
  self.close()
@@ -161,6 +164,12 @@ class SerialAdapter(fprime_gds.common.communication.adapters.base.BaseAdapter):
161
164
  "default": 9600,
162
165
  "help": "Baud rate of the serial device.",
163
166
  },
167
+ ("--uart-skip-port-check",): {
168
+ "dest": "skip_check",
169
+ "default": False,
170
+ "action": "store_true",
171
+ "help": "Skip checking that the port exists"
172
+ }
164
173
  }
165
174
 
166
175
  @classmethod
@@ -175,20 +184,20 @@ class SerialAdapter(fprime_gds.common.communication.adapters.base.BaseAdapter):
175
184
  return cls
176
185
 
177
186
  @classmethod
178
- def check_arguments(cls, device, baud):
187
+ def check_arguments(cls, device, baud, skip_check):
179
188
  """
180
189
  Code that should check arguments of this adapter. If there is a problem with this code, then a "ValueError"
181
190
  should be raised describing the problem with these arguments.
182
191
 
183
192
  :param args: arguments as dictionary
184
193
  """
185
- ports = map(lambda info: info.device, list_ports.comports(include_links=True))
186
- if device not in ports:
194
+ ports = list(map(lambda info: info.device, list_ports.comports(include_links=True)))
195
+ if not skip_check and device not in ports:
187
196
  msg = f"Serial port '{device}' not valid. Available ports: {ports}"
188
197
  raise ValueError(
189
198
  msg
190
199
  )
191
- # Note: baud rate may not *always* work. These are a superset
200
+ # Note: baud rate may not *always* work. These are a superset.
192
201
  try:
193
202
  baud = int(baud)
194
203
  except ValueError:
File without changes
@@ -0,0 +1,19 @@
1
+ """ ccsds.apid: APID mapping functions for F´ data """
2
+ from fprime_gds.common.utils.data_desc_type import DataDescType
3
+ from fprime.common.models.serialize.numerical_types import U32Type
4
+
5
+ class APID(object):
6
+ """ APID implementations """
7
+ #TODO: use the DataDescType configured by loading the dictionary
8
+
9
+ @classmethod
10
+ def from_type(cls, data_type: DataDescType):
11
+ """ Map from data description type to APID """
12
+ return data_type.value
13
+
14
+ @classmethod
15
+ def from_data(cls, data):
16
+ """ Map from data bytes to APID """
17
+ u32_type = U32Type()
18
+ u32_type.deserialize(data, offset=0)
19
+ return cls.from_type(DataDescType(u32_type.val))
@@ -0,0 +1,106 @@
1
+ """ fprime_encryption.framing.chain: implementation of a chained framer/deframer """
2
+ from abc import ABC, abstractmethod
3
+ from functools import reduce
4
+ from typing import Any, Dict, List, Type
5
+ from fprime_gds.common.communication.framing import FramerDeframer
6
+ from fprime_gds.common.communication.ccsds.space_data_link import SpaceDataLinkFramerDeframer
7
+ from fprime_gds.common.communication.ccsds.space_packet import SpacePacketFramerDeframer
8
+ from fprime_gds.plugin.definitions import gds_plugin
9
+
10
+
11
+
12
+ class ChainedFramerDeframer(FramerDeframer, ABC):
13
+ """ Framer/deframer that is a composite of chained framer/deframers
14
+
15
+ This Framer/Deframer will wrap a set of framer/deframers where the result of the frame and deframe options will pass
16
+ from one to the other subsequently. The order is specified via the framing path and deframing will use the reverse
17
+ order from specified.
18
+ """
19
+ def __init__(self, **kwargs):
20
+ """ Initialize the chained framer/deframer from a framing-ordered set of children """
21
+ frame_order_framer_deframers = [
22
+ composite(**self.get_argument_subset(composite, kwargs))
23
+ for composite in self.get_composites()
24
+ ]
25
+ self.framers = frame_order_framer_deframers[::1]
26
+ self.deframers = frame_order_framer_deframers[::-1]
27
+
28
+ @classmethod
29
+ @abstractmethod
30
+ def get_composites(cls) -> List[Type[FramerDeframer]]:
31
+ """ Return a list of composites
32
+ Innermost FramerDeframer should be first in the list. """
33
+ raise NotImplementedError(f"Subclasses of {cls.__name__} must implement get_composites")
34
+
35
+ @staticmethod
36
+ def get_argument_subset(composite: Type[FramerDeframer], argument_dictionary: Dict[str, Any]) -> Dict[str, Any]:
37
+ """ Get an argument subset that is needed by composite
38
+
39
+ For the composite, find the set of arguments that is needed by this composite and pull those out of the complete
40
+ argument dictionary.
41
+
42
+ Args:
43
+ composite: class of a subtype of FramerDeframer
44
+ argument_dictionary: dictionary of all input arguments
45
+ """
46
+ if not hasattr(composite, "get_arguments"):
47
+ return {}
48
+ needed_arguments = composite.get_arguments()
49
+ needed_argument_destinations = [
50
+ description["destination"] if "destination" in description else
51
+ [dash_dash for dash_dash in flag if dash_dash.startswith("--")][0].lstrip("-").replace("-", "_")
52
+ for flag, description in needed_arguments.items()
53
+ ]
54
+ return {name: argument_dictionary[name] for name in needed_argument_destinations}
55
+
56
+ @classmethod
57
+ def get_arguments(cls):
58
+ """ Arguments to request from the CLI """
59
+ all_arguments = {}
60
+ for composite in cls.get_composites():
61
+ all_arguments.update(composite.get_arguments() if hasattr(composite, "get_arguments") else {})
62
+ return all_arguments
63
+
64
+ @classmethod
65
+ def check_arguments(cls, **kwargs):
66
+ """ Check arguments from the CLI """
67
+ for composite in cls.get_composites():
68
+ subset_arguments = cls.get_argument_subset(composite, kwargs)
69
+ if hasattr(composite, "check_arguments"):
70
+ composite.check_arguments(**subset_arguments)
71
+
72
+ def deframe(self, data, no_copy=False):
73
+ """ Deframe via a chain of children deframers """
74
+ packet = data[:] if not no_copy else data
75
+ remaining = None
76
+ discarded = b""
77
+
78
+ for deframer in self.deframers:
79
+ new_packet, new_remaining, new_discarded = deframer.deframe(packet, True)
80
+ discarded += new_discarded
81
+ remaining = new_remaining if remaining is None else remaining
82
+ packet = new_packet
83
+ return packet, remaining, discarded
84
+
85
+ def frame(self, data):
86
+ """ Frame via a chain of children framers """
87
+ return reduce(lambda framed_data, framer: framer.frame(framed_data), self.framers, data)
88
+
89
+
90
+ @gds_plugin(FramerDeframer)
91
+ class SpacePacketSpaceDataLinkFramerDeframer(ChainedFramerDeframer):
92
+ """ Space Data Link Protocol framing and deframing that has a data unit of Space Packets as the central """
93
+
94
+ @classmethod
95
+ def get_composites(cls) -> List[Type[FramerDeframer]]:
96
+ """ Return the composite list of this chain
97
+ Innermost FramerDeframer should be first in the list. """
98
+ return [
99
+ SpacePacketFramerDeframer,
100
+ SpaceDataLinkFramerDeframer
101
+ ]
102
+
103
+ @classmethod
104
+ def get_name(cls):
105
+ """ Name of this implementation provided to CLI """
106
+ return "space-packet-space-data-link"
@@ -0,0 +1,196 @@
1
+ """F Prime Framer/Deframer Implementation of the CCSDS Space Data Link (TC/TM) Protocols"""
2
+ import sys
3
+ import struct
4
+ import copy
5
+
6
+ from fprime_gds.common.communication.framing import FramerDeframer
7
+ from fprime_gds.plugin.definitions import gds_plugin_implementation
8
+
9
+ import crc
10
+
11
+
12
+ class SpaceDataLinkFramerDeframer(FramerDeframer):
13
+ """CCSDS Framer/Deframer Implementation for the TC (uplink / framing) and TM (downlink / deframing)
14
+ protocols. This FramerDeframer is used for framing TC data for uplink and deframing TM data for downlink.
15
+ """
16
+
17
+ SEQUENCE_NUMBER_MAXIMUM = 256
18
+ TC_HEADER_SIZE = 5
19
+ TM_HEADER_SIZE = 6
20
+ TM_FIXED_FRAME_SIZE = 1024
21
+ TM_TRAILER_SIZE = 2
22
+ TC_TRAILER_SIZE = 2
23
+
24
+ # As per CCSDS standard, use CRC-16 CCITT config with init value
25
+ # all 1s and final XOR value of 0x0000
26
+ CRC_CCITT_CONFIG = crc.Configuration(
27
+ width=16,
28
+ polynomial=0x1021,
29
+ init_value=0xFFFF,
30
+ final_xor_value=0x0000,
31
+ )
32
+ CRC_CALCULATOR = crc.Calculator(CRC_CCITT_CONFIG)
33
+
34
+ def __init__(self, scid, vcid):
35
+ """ """
36
+ self.scid = scid
37
+ self.vcid = vcid
38
+ self.sequence_number = 0
39
+
40
+ def frame(self, data):
41
+ """Frame the supplied data in a TC frame"""
42
+ space_packet_bytes = data
43
+ # CCSDS TC protocol defines the length token as number of bytes in full frame, minus 1
44
+ # so we add to packet size the size of the header and trailer and subtract 1
45
+ length = (
46
+ len(space_packet_bytes) + self.TC_HEADER_SIZE + self.TC_TRAILER_SIZE - 1
47
+ )
48
+ assert length < (pow(2, 10) - 1), "Length too-large for CCSDS format"
49
+
50
+ # CCSDS TC Header:
51
+ # 2b - 00 - TF version number
52
+ # 1b - 0/1 - 0 enable FARM checks, 1 bypass FARM
53
+ # 1b - 0/1 - 0 = data (Type-D), 1 = control information (Type-C)
54
+ # 2b - 00 - Reserved
55
+ # 10b - XX - Spacecraft id
56
+ # 6b - XX - Virtual Channel ID
57
+ # 10b - XX - Frame length
58
+ # 8b - XX - Frame sequence number
59
+
60
+ # First 16 bits:
61
+ header_val1_u16 = (
62
+ (0 << 14) | # TF version number (2 bits)
63
+ (1 << 13) | # Bypass FARM (1 bit)
64
+ (0 << 12) | # Type-D (1 bit)
65
+ (0 << 10) | # Reserved (2 bits)
66
+ ((self.scid & 0x3FF)) # SCID (10 bits)
67
+ )
68
+ # Second 16 bits:
69
+ header_val2_u16 = (
70
+ ((self.vcid & 0x3F) << 10) | # VCID (6 bits)
71
+ (length & 0x3FF) # Frame length (10 bits)
72
+ )
73
+ # 8 bit sequence number - always 0 in bypass FARM mode
74
+ header_val3_u8 = 0
75
+ header_bytes = struct.pack(">HHB", header_val1_u16, header_val2_u16, header_val3_u8)
76
+ full_bytes_no_crc = header_bytes + space_packet_bytes
77
+ assert (
78
+ len(header_bytes) == self.TC_HEADER_SIZE
79
+ ), "CCSDS primary header must be 5 octets long"
80
+ assert len(full_bytes_no_crc) == self.TC_HEADER_SIZE + len(
81
+ data
82
+ ), "Malformed packet generated"
83
+
84
+ full_bytes = full_bytes_no_crc + struct.pack(
85
+ ">H", self.CRC_CALCULATOR.checksum(full_bytes_no_crc)
86
+ )
87
+ return full_bytes
88
+
89
+ def get_sequence_number(self):
90
+ """Get the sequence number and increment - used for TM deframing
91
+
92
+ This function will return the current sequence number and then increment the sequence number for the next round.
93
+
94
+ Return:
95
+ current sequence number
96
+ """
97
+ sequence = self.sequence_number
98
+ self.sequence_number = (self.sequence_number + 1) % self.SEQUENCE_NUMBER_MAXIMUM
99
+ return sequence
100
+
101
+ def deframe(self, data, no_copy=False):
102
+ """Deframe TM frames"""
103
+ discarded = b""
104
+ if not no_copy:
105
+ data = copy.copy(data)
106
+ # Continue until there is not enough data for the header, or until a packet is found (return)
107
+ while len(data) >= self.TM_FIXED_FRAME_SIZE:
108
+ # Read header information
109
+ sc_and_channel_ids = struct.unpack_from(">H", data)
110
+ spacecraft_id = (sc_and_channel_ids[0] & 0x3FF0) >> 4
111
+ virtual_channel_id = (sc_and_channel_ids[0] & 0x000E) >> 1
112
+ # Check if the header is correct with regards to expected spacecraft and VC IDs
113
+ if spacecraft_id != self.scid or virtual_channel_id != self.vcid:
114
+ # If the header is invalid, rotate away a Byte and keep processing
115
+ discarded += data[0:1]
116
+ data = data[1:]
117
+ continue
118
+ # Spacecraft ID and Virtual Channel ID match, so we look at end of frame for CRC
119
+ crc_offset = self.TM_FIXED_FRAME_SIZE - self.TM_TRAILER_SIZE
120
+ transmitted_crc = struct.unpack_from(">H", data, crc_offset)[0]
121
+ if transmitted_crc == self.CRC_CALCULATOR.checksum(data[:crc_offset]):
122
+ # CRC is valid, so we return the deframed data
123
+ deframed_data_len = (
124
+ self.TM_FIXED_FRAME_SIZE
125
+ - self.TM_TRAILER_SIZE
126
+ - self.TM_HEADER_SIZE
127
+ )
128
+ deframed = struct.unpack_from(
129
+ f">{deframed_data_len}s", data, self.TM_HEADER_SIZE
130
+ )[0]
131
+ # Consume the fixed size frame
132
+ data = data[self.TM_FIXED_FRAME_SIZE :]
133
+ return deframed, data, discarded
134
+
135
+ print(
136
+ "[WARNING] Checksum validation failed.",
137
+ file=sys.stderr,
138
+ )
139
+ # Bad checksum, rotate 1 and keep looking for non-garbage
140
+ discarded += data[0:1]
141
+ data = data[1:]
142
+ continue
143
+ return None, data, discarded
144
+
145
+ @classmethod
146
+ def get_arguments(cls):
147
+ """Arguments to request from the CLI"""
148
+ return {
149
+ ("--scid",): {
150
+ "type": lambda input_arg: int(input_arg, 0),
151
+ "help": "Spacecraft ID",
152
+ "default": 0x44,
153
+ "required": False,
154
+ },
155
+ ("--vcid",): {
156
+ "type": lambda input_arg: int(input_arg, 0),
157
+ "help": "Virtual channel ID",
158
+ "default": 1,
159
+ "required": False,
160
+ },
161
+ }
162
+
163
+ @classmethod
164
+ def check_arguments(cls, scid, vcid):
165
+ """Check arguments from the CLI
166
+
167
+ Confirms that the input arguments are valid for this framer/deframer.
168
+
169
+ Args:
170
+ scid: spacecraft id
171
+ vcid: virtual channel id
172
+ """
173
+ if scid is None:
174
+ raise TypeError(f"Spacecraft ID not specified")
175
+ if scid < 0:
176
+ raise TypeError(f"Spacecraft ID {scid} is negative")
177
+ if scid > 0x3FF:
178
+ raise TypeError(f"Spacecraft ID {scid} is larger than {0x3FF}")
179
+
180
+ if vcid is None:
181
+ raise TypeError(f"Virtual Channel ID not specified")
182
+ if vcid < 0:
183
+ raise TypeError(f"Virtual Channel ID {vcid} is negative")
184
+ if vcid > 0x3F:
185
+ raise TypeError(f"Virtual Channel ID {vcid} is larger than {0x3FF}")
186
+
187
+ @classmethod
188
+ def get_name(cls):
189
+ """Name of this implementation provided to CLI"""
190
+ return "raw-space-data-link"
191
+
192
+ @classmethod
193
+ @gds_plugin_implementation
194
+ def register_framing_plugin(cls):
195
+ """Register the MyPlugin plugin"""
196
+ return cls
@@ -0,0 +1,129 @@
1
+ """F Prime Framer/Deframer Implementation of the CCSDS Space Packet Protocol"""
2
+
3
+ from __future__ import annotations
4
+
5
+ import struct
6
+ import copy
7
+
8
+ from spacepackets.ccsds.spacepacket import SpacePacketHeader, PacketType, SpacePacket
9
+
10
+ from fprime_gds.common.communication.framing import FramerDeframer
11
+ from fprime_gds.plugin.definitions import gds_plugin_implementation, gds_plugin
12
+ from fprime_gds.common.utils.data_desc_type import DataDescType
13
+
14
+ from .apid import APID
15
+ import logging
16
+
17
+ LOGGER = logging.getLogger("framing")
18
+
19
+
20
+ @gds_plugin(FramerDeframer)
21
+ class SpacePacketFramerDeframer(FramerDeframer):
22
+ """Concrete implementation of FramerDeframer supporting SpacePacket protocol
23
+
24
+ This implementation is registered as a "framing" plugin to support encryption within the GDS layer.
25
+ """
26
+
27
+ SEQUENCE_COUNT_MAXIMUM = 16384 # 2^14
28
+ HEADER_SIZE = 6
29
+ IDLE_APID = 0x7FF # max 11 bit value per protocol specification
30
+
31
+ def __init__(self):
32
+ # self.sequence_number = 0
33
+ # Map APID to sequence counts
34
+ self.apid_to_sequence_count_map = dict()
35
+ for key in DataDescType:
36
+ self.apid_to_sequence_count_map[key.value] = 0
37
+
38
+ def frame(self, data):
39
+ """Frame the supplied data in Space Packet"""
40
+ # The protocol defines length token to be number of bytes minus 1
41
+ data_length_token = len(data) - 1
42
+ apid = APID.from_data(data)
43
+ space_header = SpacePacketHeader(
44
+ packet_type=PacketType.TC,
45
+ apid=apid,
46
+ seq_count=self.get_sequence_count(apid),
47
+ data_len=data_length_token,
48
+ )
49
+ space_packet = SpacePacket(space_header, sec_header=None, user_data=data)
50
+ return space_packet.pack()
51
+
52
+ def deframe(self, data, no_copy=False):
53
+ """Deframe the supplied data according to Space Packet protocol"""
54
+ discarded = b""
55
+ if data is None:
56
+ return None, None, discarded
57
+ if not no_copy:
58
+ data = copy.copy(data)
59
+ # Deframe all packets until there is not enough data for a header
60
+ while len(data) >= self.HEADER_SIZE:
61
+ # Read header information including start token and size and check if we have enough for the total size
62
+ try:
63
+ sp_header = SpacePacketHeader.unpack(data)
64
+ except ValueError:
65
+ # If the header is invalid, rotate away a byte and keep processing
66
+ discarded += data[0:1]
67
+ data = data[1:]
68
+ continue
69
+ if sp_header.ccsds_version != 0 or sp_header.packet_type != PacketType.TM:
70
+ # Space Packet version is specified as 0 per protocol
71
+ discarded += data[0:1]
72
+ data = data[1:]
73
+ continue
74
+ # Skip Idle Packets as they are not meaningful
75
+ if sp_header.apid == self.IDLE_APID:
76
+ data = data[sp_header.packet_len :]
77
+ continue
78
+ # Check sequence count and warn if not expected value (don't drop the packet)
79
+ if sp_header.seq_count != self.get_sequence_count(sp_header.apid):
80
+ LOGGER.warning(
81
+ f"APID {sp_header.apid} received sequence count: {sp_header.seq_count}"
82
+ f" (expected: {self.get_sequence_count(sp_header.apid)})"
83
+ )
84
+ # Set the sequence count to the next expected value (consider missing packets have been lost)
85
+ self.apid_to_sequence_count_map[sp_header.apid] = (
86
+ sp_header.seq_count + 1
87
+ )
88
+ # If the pool is large enough to read the whole packet, then read it
89
+ if len(data) >= sp_header.packet_len:
90
+ deframed = struct.unpack_from(
91
+ # data_len is number of bytes minus 1 per SpacePacket spec
92
+ f">{sp_header.data_len + 1}s",
93
+ data,
94
+ self.HEADER_SIZE,
95
+ )[0]
96
+ data = data[sp_header.packet_len :]
97
+ LOGGER.debug(f"Deframed packet: {sp_header}")
98
+ return deframed, data, discarded
99
+ else:
100
+ # If we don't have enough data, then break out of the loop
101
+ break
102
+ return None, data, discarded
103
+
104
+ def get_sequence_count(self, apid: int):
105
+ """Get the sequence number and increment
106
+
107
+ This function will return the current sequence number and then increment the sequence number for the next round.
108
+ Should an APID not be registered already, it will be initialized to 0.
109
+
110
+ Return:
111
+ current sequence number
112
+ """
113
+ # If APID is not registered, initialize it to 0
114
+ sequence = self.apid_to_sequence_count_map.get(apid, 0)
115
+ self.apid_to_sequence_count_map[apid] = (
116
+ sequence + 1
117
+ ) % self.SEQUENCE_COUNT_MAXIMUM
118
+ return sequence
119
+
120
+ @classmethod
121
+ def get_name(cls):
122
+ """Name of this implementation provided to CLI"""
123
+ return "raw-space-packet"
124
+
125
+ @classmethod
126
+ @gds_plugin_implementation
127
+ def register_framing_plugin(cls):
128
+ """Register the MyPlugin plugin"""
129
+ return cls