F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
CcsdsSdlsFramer.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title CcsdsSdlsFramer.cpp
3 // \author devin
4 // \brief cpp file for CcsdsSdlsFramer component implementation class
5 // ======================================================================
6 
8 
9 #include <Fw/Prm/ParamValid.hpp>
11 
12 namespace Svc {
13 
14 namespace Ccsds {
15 
16 static_assert(sizeof(decltype(ComCfg::FrameContext().get_saIndex())) == SdlsSaIndexSize,
17  "Svc.Ccsds.SdlsSaIndexSize must match the prepended SA index");
18 
19 // ----------------------------------------------------------------------
20 // Component construction and destruction
21 // ----------------------------------------------------------------------
22 
23 CcsdsSdlsFramer ::CcsdsSdlsFramer(const char* const compName) : CcsdsSdlsFramerComponentBase(compName) {}
24 
26 
27 // ----------------------------------------------------------------------
28 // Handler implementations for typed input ports
29 // ----------------------------------------------------------------------
30 
31 void CcsdsSdlsFramer ::bufferReturnIn_handler(FwIndexType portNum,
32  Fw::Buffer& data,
33  const ComCfg::FrameContext& context) {
34  // The encryption helper has returned the original data buffer: send it back upstream
35  this->dataReturnOut_out(0, data, context);
36 }
37 
38 void CcsdsSdlsFramer ::comStatusIn_handler(FwIndexType portNum, Fw::Success& condition) {
39  if (this->isConnected_comStatusOut_OutputPort(portNum)) {
40  this->comStatusOut_out(portNum, condition);
41  }
42 }
43 
44 void CcsdsSdlsFramer ::dataIn_handler(FwIndexType portNum, Fw::Buffer& data, const ComCfg::FrameContext& context) {
45  // Determine the security association index: use the context's value when set, otherwise the SA_INDEX parameter
46  const U16 unsetSaIndex = static_cast<U16>(ComCfg::SaIndexUnset);
47  U16 saIndex = context.get_saIndex();
48  if (saIndex == unsetSaIndex) {
50  saIndex = this->paramGet_SA_INDEX(valid);
51  FW_ASSERT(FW_PARAM_OK(valid), static_cast<FwAssertArgType>(valid.e));
52  }
53 
54  // Copy context and record the security association index used for encryption
55  ComCfg::FrameContext newContext = context;
56  newContext.set_saIndex(saIndex);
57 
58  this->encryptOut_out(0, saIndex, data, newContext);
59 }
60 
61 void CcsdsSdlsFramer ::dataReturnIn_handler(FwIndexType portNum,
62  Fw::Buffer& data,
63  const ComCfg::FrameContext& context) {
64  // dataReturnIn is the allocated frame buffer coming back from the dataOut port
65  this->bufferDeallocate_out(0, data);
66 }
67 
68 void CcsdsSdlsFramer ::encryptIn_handler(FwIndexType portNum,
69  const Svc::Ccsds::SdlsStatus& status,
70  Fw::Buffer& data,
71  const ComCfg::FrameContext& context) {
72  if (status != Svc::Ccsds::SdlsStatus::SUCCESS) {
73  this->log_WARNING_HI_EncryptionFailed(status);
74  // Drop the frame: return ownership of the buffer to the encryption subsystem
75  this->encryptReturnOut_out(0, data, context);
76  this->sendComStatusOnDrop();
77  return;
78  }
79 
80  FW_ASSERT(data.getSize() <= std::numeric_limits<Fw::Buffer::SizeType>::max() - sizeof(U16),
81  static_cast<FwAssertArgType>(data.getSize()));
82  const Fw::Buffer::SizeType frameSize = static_cast<Fw::Buffer::SizeType>(data.getSize() + sizeof(U16));
83 
84  // Allocate the frame buffer used to prepend the security association index to the encrypted data
85  Fw::Buffer frameBuffer = this->bufferAllocate_out(0, frameSize);
86  if ((!frameBuffer.isValid()) || (frameBuffer.getSize() < frameSize)) {
87  this->log_WARNING_HI_BufferAllocationFailed(frameSize);
88  // Drop the frame: return the undersized allocation (when valid) and the encrypted data buffer
89  if (frameBuffer.isValid()) {
90  this->bufferDeallocate_out(0, frameBuffer);
91  }
92  this->encryptReturnOut_out(0, data, context);
93  this->sendComStatusOnDrop();
94  return;
95  }
96 
97  auto frameSerializer = frameBuffer.getSerializer();
98  Fw::SerializeStatus serializeStatus = frameSerializer.serializeFrom(context.get_saIndex());
99  FW_ASSERT(serializeStatus == Fw::FW_SERIALIZE_OK, serializeStatus);
100  serializeStatus = frameSerializer.serializeFrom(data.getData(), data.getSize(), Fw::Serialization::OMIT_LENGTH);
101  FW_ASSERT(serializeStatus == Fw::FW_SERIALIZE_OK, serializeStatus);
102 
103  // Trim to actual frame size in case the allocator returned a larger buffer
104  frameBuffer.setSize(frameSize);
105 
106  // Return ownership of the encrypted data buffer to the encryption helper, then send the frame
107  this->encryptReturnOut_out(0, data, context);
108  this->dataOut_out(0, frameBuffer, context);
109 }
110 
111 // ----------------------------------------------------------------------
112 // Private helper implementations
113 // ----------------------------------------------------------------------
114 
115 void CcsdsSdlsFramer ::sendComStatusOnDrop() {
116  // Report ready-for-more on a dropped frame so a ComQueue-driven downlink does not stall
117  if (this->isConnected_comStatusOut_OutputPort(0)) {
119  this->comStatusOut_out(0, status);
120  }
121 }
122 
123 } // namespace Ccsds
124 
125 } // namespace Svc
Serialization/Deserialization operation was successful.
#define FW_PARAM_OK(paramValid)
Definition: ParamValid.hpp:22
Representing success.
CcsdsSdlsFramer(const char *const compName)
Construct CcsdsSdlsFramer object.
void setSize(FwSizeType size)
Definition: Buffer.cpp:131
U8 * getData() const
Definition: Buffer.cpp:82
SerializeStatus serializeFrom(U8 val, Endianness mode=Endianness::BIG) override
Serialize an 8-bit unsigned integer value.
Status of an SDLS (Space Data Link Security) encryption/decryption request.
SerializeStatus
forward declaration for string
Omit length from serialization.
bool isValid() const
Definition: Buffer.cpp:78
enum T e
The raw enum value.
FwSizeType getSize() const
Definition: Buffer.cpp:90
U16 get_saIndex() const
Get member saIndex.
void set_saIndex(U16 saIndex)
Set member saIndex.
PlatformIndexType FwIndexType
FwSizeType SizeType
The size type for a buffer - for backwards compatibility.
Definition: Buffer.hpp:67
Type used to pass context info between components during framing/deframing.
RateGroupDivider component implementation.
Enum representing parameter validity.
Request completed successfully.
ExternalSerializeBufferWithMemberCopy getSerializer()
Definition: Buffer.cpp:155
#define FW_ASSERT(...)
Definition: Assert.hpp:14
Success/Failure.
~CcsdsSdlsFramer()
Destroy CcsdsSdlsFramer object.