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>
10 
11 namespace Svc {
12 
13 namespace Ccsds {
14 
15 // ----------------------------------------------------------------------
16 // Component construction and destruction
17 // ----------------------------------------------------------------------
18 
19 CcsdsSdlsFramer ::CcsdsSdlsFramer(const char* const compName) : CcsdsSdlsFramerComponentBase(compName) {}
20 
22 
23 // ----------------------------------------------------------------------
24 // Handler implementations for typed input ports
25 // ----------------------------------------------------------------------
26 
27 void CcsdsSdlsFramer ::bufferReturnIn_handler(FwIndexType portNum,
28  Fw::Buffer& data,
29  const ComCfg::FrameContext& context) {
30  // The encryption helper has returned the original data buffer: send it back upstream
31  this->dataReturnOut_out(0, data, context);
32 }
33 
34 void CcsdsSdlsFramer ::comStatusIn_handler(FwIndexType portNum, Fw::Success& condition) {
35  if (this->isConnected_comStatusOut_OutputPort(portNum)) {
36  this->comStatusOut_out(portNum, condition);
37  }
38 }
39 
40 void CcsdsSdlsFramer ::dataIn_handler(FwIndexType portNum, Fw::Buffer& data, const ComCfg::FrameContext& context) {
41  // Determine the security association index: use the context's value when set, otherwise the SA_INDEX parameter
42  const U16 unsetSaIndex = static_cast<U16>(ComCfg::SaIndexUnset);
43  U16 saIndex = context.get_saIndex();
44  if (saIndex == unsetSaIndex) {
46  saIndex = this->paramGet_SA_INDEX(valid);
47  FW_ASSERT(FW_PARAM_OK(valid), static_cast<FwAssertArgType>(valid.e));
48  }
49 
50  // Copy context and record the security association index used for encryption
51  ComCfg::FrameContext newContext = context;
52  newContext.set_saIndex(saIndex);
53 
54  this->encryptOut_out(0, saIndex, data, newContext);
55 }
56 
57 void CcsdsSdlsFramer ::dataReturnIn_handler(FwIndexType portNum,
58  Fw::Buffer& data,
59  const ComCfg::FrameContext& context) {
60  // dataReturnIn is the allocated frame buffer coming back from the dataOut port
61  this->bufferDeallocate_out(0, data);
62 }
63 
64 void CcsdsSdlsFramer ::encryptIn_handler(FwIndexType portNum,
65  const Svc::Ccsds::SdlsStatus& status,
66  Fw::Buffer& data,
67  const ComCfg::FrameContext& context) {
68  if (status != Svc::Ccsds::SdlsStatus::SUCCESS) {
69  this->log_WARNING_HI_EncryptionFailed(status);
70  // Drop the frame: return ownership of the buffer to the encryption subsystem
71  this->encryptReturnOut_out(0, data, context);
72  this->sendComStatusOnDrop();
73  return;
74  }
75 
76  FW_ASSERT(data.getSize() <= std::numeric_limits<Fw::Buffer::SizeType>::max() - sizeof(U16),
77  static_cast<FwAssertArgType>(data.getSize()));
78  const Fw::Buffer::SizeType frameSize = static_cast<Fw::Buffer::SizeType>(data.getSize() + sizeof(U16));
79 
80  // Allocate the frame buffer used to prepend the security association index to the encrypted data
81  Fw::Buffer frameBuffer = this->bufferAllocate_out(0, frameSize);
82  if ((!frameBuffer.isValid()) || (frameBuffer.getSize() < frameSize)) {
83  this->log_WARNING_HI_BufferAllocationFailed(frameSize);
84  // Drop the frame: return the undersized allocation (when valid) and the encrypted data buffer
85  if (frameBuffer.isValid()) {
86  this->bufferDeallocate_out(0, frameBuffer);
87  }
88  this->encryptReturnOut_out(0, data, context);
89  this->sendComStatusOnDrop();
90  return;
91  }
92 
93  auto frameSerializer = frameBuffer.getSerializer();
94  Fw::SerializeStatus serializeStatus = frameSerializer.serializeFrom(context.get_saIndex());
95  FW_ASSERT(serializeStatus == Fw::FW_SERIALIZE_OK, serializeStatus);
96  serializeStatus = frameSerializer.serializeFrom(data.getData(), data.getSize(), Fw::Serialization::OMIT_LENGTH);
97  FW_ASSERT(serializeStatus == Fw::FW_SERIALIZE_OK, serializeStatus);
98 
99  // Trim to actual frame size in case the allocator returned a larger buffer
100  frameBuffer.setSize(frameSize);
101 
102  // Return ownership of the encrypted data buffer to the encryption helper, then send the frame
103  this->encryptReturnOut_out(0, data, context);
104  this->dataOut_out(0, frameBuffer, context);
105 }
106 
107 // ----------------------------------------------------------------------
108 // Private helper implementations
109 // ----------------------------------------------------------------------
110 
111 void CcsdsSdlsFramer ::sendComStatusOnDrop() {
112  // Report ready-for-more on a dropped frame so a ComQueue-driven downlink does not stall
113  if (this->isConnected_comStatusOut_OutputPort(0)) {
115  this->comStatusOut_out(0, status);
116  }
117 }
118 
119 } // namespace Ccsds
120 
121 } // 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.