F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
Transaction.hpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title Transaction.hpp
3 // \brief CFDP Transaction state machine class for TX and RX operations
4 //
5 // This file is a port of transaction state machine definitions from the following files
6 // from the NASA Core Flight System (cFS) CFDP (CF) Application, version 3.0.0,
7 // adapted for use within the F-Prime (F') framework:
8 // - cf_cfdp_r.h (receive transaction state machine definitions)
9 // - cf_cfdp_s.h (send transaction state machine definitions)
10 // - cf_cfdp_dispatch.h (transaction dispatch definitions)
11 //
12 // This file contains the unified interface for CFDP transaction state
13 // machines, encompassing both TX (send) and RX (receive) operations.
14 // The implementation is split across TransactionTx.cpp and
15 // TransactionRx.cpp for maintainability.
16 //
17 // ======================================================================
18 //
19 // NASA Docket No. GSC-18,447-1
20 //
21 // Copyright (c) 2019 United States Government as represented by the
22 // Administrator of the National Aeronautics and Space Administration.
23 // All Rights Reserved.
24 //
25 // Licensed under the Apache License, Version 2.0 (the "License"); you may
26 // not use this file except in compliance with the License. You may obtain
27 // a copy of the License at
28 //
29 // http://www.apache.org/licenses/LICENSE-2.0
30 //
31 // Unless required by applicable law or agreed to in writing, software
32 // distributed under the License is distributed on an "AS IS" BASIS,
33 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34 // See the License for the specific language governing permissions and
35 // limitations under the License.
36 //
37 // ======================================================================
38 
39 #ifndef Svc_Ccsds_CfdpTransaction_HPP
40 #define Svc_Ccsds_CfdpTransaction_HPP
41 
42 #include <Fw/Types/BasicTypes.hpp>
43 
46 
47 namespace Svc {
48 namespace Ccsds {
49 namespace Cfdp {
50 
51 // Forward declarations
52 class CfdpManager;
53 class Engine;
54 class Channel;
55 class Transaction;
56 class FinPdu;
57 
58 // ======================================================================
59 // Dispatch Table Type Definitions
60 // ======================================================================
61 
71 using StateSendFunc = void (Transaction::*)();
72 
83 using StateRecvFunc = void (Transaction::*)(const Fw::Buffer& buffer);
84 
94 };
95 
106 };
107 
118 };
119 
128 };
129 
138 };
139 
148 };
149 
158 class Transaction {
159  friend class Engine;
160  friend class Channel;
161  friend class CfdpManagerTester;
162 
163  public:
164  // ----------------------------------------------------------------------
165  // Construction and Destruction
166  // ----------------------------------------------------------------------
167 
173  Transaction(Channel* channel, U8 channelId, Engine* engine, CfdpManager* manager);
174 
175  ~Transaction();
176 
183  void reset();
184 
195  void initTxFile(Class::T cfdp_class, Keep::T keep, U8 chan, U8 priority);
196 
206  static CListTraverseStatus findBySequenceNumberCallback(CListNode* node, void* context);
207 
217  static CListTraverseStatus prioritySearchCallback(CListNode* node, void* context);
218 
219  // ----------------------------------------------------------------------
220  // Accessors
221  // ----------------------------------------------------------------------
222 
227  History* getHistory() const { return m_history; }
228 
233  U8 getPriority() const { return m_priority; }
234 
239  U8 getChannelId() const { return m_chan_num; }
240 
245  Class::T getClass() const { return m_txn_class; }
246 
251  TxnState getState() const { return m_state; }
252 
253  // ----------------------------------------------------------------------
254  // TX State Machine - Implemented in TransactionTx.cpp
255  // ----------------------------------------------------------------------
256 
257  /************************************************************************/
262  void s1Recv(const Fw::Buffer& buffer);
263 
264  /************************************************************************/
269  void s2Recv(const Fw::Buffer& buffer);
270 
271  /************************************************************************/
274  void s1Tx();
275 
276  /************************************************************************/
279  void s2Tx();
280 
281  /************************************************************************/
287  void sAckTimerTick();
288 
289  /************************************************************************/
299  void sTick(I32* cont);
300 
301  /************************************************************************/
310  void sTickNak(I32* cont);
311 
312  /************************************************************************/
315  void sCancel();
316 
317  /************************************************************************/
320  void s1SubstateSendEof();
321 
322  /************************************************************************/
325  void s2SubstateSendEof();
326 
327  /************************************************************************/
335  void sSubstateSendFileData();
336 
337  /************************************************************************/
343  void s2SubstateSendFileData();
344 
345  /************************************************************************/
351  void sSubstateSendMetadata();
352 
353  /************************************************************************/
358  void s2EarlyFin(const Fw::Buffer& pdu);
359 
360  /************************************************************************/
365  void s2Fin(const Fw::Buffer& pdu);
366 
367  /************************************************************************/
376  bool s2DeserializeFin(const Fw::Buffer& pdu, FinPdu& fin);
377 
378  /************************************************************************/
383  void s2HandleFin(const FinPdu& fin);
384 
385  /************************************************************************/
394  void s2Nak(const Fw::Buffer& pdu);
395 
396  /************************************************************************/
401  void s2NakArm(const Fw::Buffer& pdu);
402 
403  /************************************************************************/
410  void s2EofAck(const Fw::Buffer& pdu);
411 
412  private:
413  /***********************************************************************
414  *
415  * Handler routines for send-file transactions
416  * These are not called from outside this module, but are declared here so they can be unit tested
417  *
418  ************************************************************************/
419 
420  /************************************************************************/
427  Status::T sSendEof();
428 
429  Status::T sSendFileData(FileSize foffs, FileSize bytes_to_read, U8 calc_crc, FileSize* bytes_processed);
430 
431  Status::T sCheckAndRespondNak(bool* nakProcessed);
432 
433  Status::T sSendFinAck();
434 
435  public:
436  // ----------------------------------------------------------------------
437  // RX State Machine - Implemented in TransactionRx.cpp
438  // ----------------------------------------------------------------------
439 
440  /************************************************************************/
445  void r1Recv(const Fw::Buffer& buffer);
446 
447  /************************************************************************/
452  void r2Recv(const Fw::Buffer& buffer);
453 
454  /************************************************************************/
460  void rAckTimerTick();
461 
462  /************************************************************************/
473  void rTick(I32* cont);
474 
475  /************************************************************************/
478  void rCancel();
479 
480  /************************************************************************/
483  void rInit();
484 
485  /************************************************************************/
490  void r2SetFinTxnStatus(TxnStatus txn_stat);
491 
492  /************************************************************************/
499  void r1Reset();
500 
501  /************************************************************************/
506  void r2Reset();
507 
508  /************************************************************************/
515  Status::T rCheckCrc(U32 expected_crc);
516 
517  /************************************************************************/
530  void r2Complete(I32 ok_to_send_nak);
531 
532  // ----------------------------------------------------------------------
533  // Dispatch Methods (ported from cf_cfdp_dispatch.c)
534  // ----------------------------------------------------------------------
535 
536  /************************************************************************/
547  void rDispatchRecv(const Fw::Buffer& buffer, const RSubstateDispatchTable* dispatch, StateRecvFunc fd_fn);
548 
549  /************************************************************************/
558  void sDispatchRecv(const Fw::Buffer& buffer, const SSubstateRecvDispatchTable* dispatch);
559 
560  /************************************************************************/
569  void sDispatchTransmit(const SSubstateSendDispatchTable* dispatch);
570 
571  /************************************************************************/
579  void txStateDispatch(const TxnSendDispatchTable* dispatch);
580 
581  private:
582  /************************************************************************/
589  Status::T rProcessFd(const Fw::Buffer& pdu);
590 
591  /************************************************************************/
602  Status::T rSubstateRecvEof(const Fw::Buffer& pdu);
603 
604  /************************************************************************/
611  void r1SubstateRecvEof(const Fw::Buffer& pdu);
612 
613  /************************************************************************/
621  void r2SubstateRecvEof(const Fw::Buffer& pdu);
622 
623  /************************************************************************/
630  void r1SubstateRecvFileData(const Fw::Buffer& pdu);
631 
632  /************************************************************************/
643  void r2SubstateRecvFileData(const Fw::Buffer& pdu);
644 
645  /************************************************************************/
654  void r2GapCompute(const Chunk* chunk, NakPdu& nak);
655 
661  static void r2GapComputeWrapper(const Chunk* chunk, void* opaque);
662 
663  /************************************************************************/
674  Status::T rSubstateSendNak();
675 
676  /************************************************************************/
694  Status::T r2CalcCrcChunk();
695 
696  /************************************************************************/
701  Status::T r2SubstateSendFin();
702 
703  /************************************************************************/
711  void r2RecvFinAck(const Fw::Buffer& pdu);
712 
713  /************************************************************************/
724  void r2RecvMd(const Fw::Buffer& pdu);
725 
726  /************************************************************************/
729  void rSendInactivityEvent();
730 
731  private:
732  // ----------------------------------------------------------------------
733  // Member Variables
734  // ----------------------------------------------------------------------
735 
741  TxnState m_state;
742 
748  Class::T m_txn_class;
749 
755  History* m_history;
756 
762  CfdpChunkWrapper* m_chunks;
763 
769  Timer m_inactivity_timer;
770 
776  Timer m_ack_timer;
777 
781  FileSize m_fsize;
782 
786  FileSize m_foffs;
787 
791  Os::File m_fd;
792 
796  CFDP::Checksum m_crc;
797 
801  Keep::T m_keep;
802 
808  U8 m_chan_num;
809 
813  U8 m_priority;
814 
821  TransactionInitType m_initType;
822 
828  CListNode m_cl_node;
829 
835  Playback* m_pb;
836 
840  CfdpStateData m_state_data;
841 
851  CfdpStateFlags m_flags;
852 
858  CfdpManager* m_cfdpManager;
859 
865  Channel* m_chan;
866 
872  Engine* m_engine;
873 };
874 
875 } // namespace Cfdp
876 } // namespace Ccsds
877 } // namespace Svc
878 
879 #endif // Svc_Ccsds_CfdpTransaction_HPP
U8 getPriority() const
Get transaction priority.
CFDP Protocol Engine.
Definition: Engine.hpp:90
const FileDirectiveDispatchTable * substate[static_cast< U32 >(TxSubState::TX_SUB_STATE_NUM_STATES)]
void s1Recv(const Fw::Buffer &buffer)
S1 receive PDU processing.
void s2EofAck(const Fw::Buffer &pdu)
S2 received ACK PDU.
TransactionInitType
Transaction initiation method.
Definition: Types.hpp:165
void sAckTimerTick()
Perform acknowledgement timer tick (time-based) processing for S transactions.
CFDP Channel class.
Definition: Channel.hpp:56
TxnState
High-level state of a transaction.
Definition: Types.hpp:116
void s2Fin(const Fw::Buffer &pdu)
S2 received FIN, so set flag to send FIN-ACK.
CFDP Transaction state machine class.
CListTraverseStatus
Traverse status for circular list operations.
Definition: Clist.hpp:45
void sDispatchTransmit(const SSubstateSendDispatchTable *dispatch)
Dispatch function to send/generate PDUs on send-file transactions.
StateSendFunc tx[static_cast< U32 >(TxnState::TXN_STATE_INVALID)]
Transmit handler function.
Definition: Transaction.hpp:93
void rCancel()
Cancel an R transaction.
void sSubstateSendFileData()
Standard state function to send the next file data PDU for active transaction.
Summary of all possible transaction state information (tx and rx)
Definition: Types.hpp:410
void r2Reset()
CFDP R2 transaction reset function.
void r2Recv(const Fw::Buffer &buffer)
R2 receive PDU processing.
StateRecvFunc rx[static_cast< U32 >(TxnState::TXN_STATE_INVALID)]
a separate recv handler for each possible file directive PDU in this state
void r2Complete(I32 ok_to_send_nak)
Checks R2 transaction state for transaction completion status.
void s2Tx()
S2 dispatch function.
void sTickNak(I32 *cont)
Perform NAK response for TX transactions.
const FileDirectiveDispatchTable * state[static_cast< U32 >(RxSubState::RX_SUB_STATE_NUM_STATES)]
Wrapper around a CfdpChunkList object.
Definition: Types.hpp:260
T
The raw enum type.
StateRecvFunc fdirective[static_cast< U32 >(FileDirective::FILE_DIRECTIVE_INVALID_MAX)]
a separate recv handler for each possible file directive PDU in this state
void s2NakArm(const Fw::Buffer &pdu)
S2 NAK handling but with arming the NAK timer.
static CListTraverseStatus prioritySearchCallback(CListNode *node, void *context)
Static callback for priority search.
Definition: Utils.cpp:89
Transaction(Channel *channel, U8 channelId, Engine *engine, CfdpManager *manager)
A table of receive handler functions based on transaction state.
void rTick(I32 *cont)
Perform tick (time-based) processing for R transactions.
TxnStatus
Values for Transaction Status code.
Definition: Types.hpp:193
void s2Recv(const Fw::Buffer &buffer)
S2 receive PDU processing.
A dispatch table for send file transactions, receive side.
CFDP Playback entry.
Definition: Types.hpp:278
void s2EarlyFin(const Fw::Buffer &pdu)
A FIN was received before file complete, so abandon the transaction.
Summary of all possible transaction flags (tx and rx)
Definition: Types.hpp:401
A dispatch table for send file transactions, transmit side.
U8 getChannelId() const
Get channel ID.
Class representing a 32-bit checksum as mandated by the CCSDS File Delivery Protocol.
Definition: Checksum.hpp:53
T
The raw enum type.
Definition: KeepEnumAc.hpp:36
void s1SubstateSendEof()
Sends an EOF for S1.
void r1Reset()
CFDP R1 transaction reset function.
void reset()
Reset transaction to default state.
void sDispatchRecv(const Fw::Buffer &buffer, const SSubstateRecvDispatchTable *dispatch)
Dispatch function for received PDUs on send-file transactions.
StateSendFunc substate[static_cast< U32 >(TxSubState::TX_SUB_STATE_NUM_STATES)]
void s2SubstateSendEof()
Triggers tick processing to send an EOF and wait for EOF-ACK for S2.
History * getHistory() const
Get transaction history.
void s1Tx()
S1 dispatch function.
The type of a Finished PDU.
Definition: FinPdu.hpp:19
TxnState getState() const
Get transaction state.
static CListTraverseStatus findBySequenceNumberCallback(CListNode *node, void *context)
Static callback for finding transaction by sequence number.
Definition: Utils.cpp:74
void s2SubstateSendFileData()
Send filedata handling for S2.
bool s2DeserializeFin(const Fw::Buffer &pdu, FinPdu &fin)
Deserialize a received FIN PDU.
void(Transaction::*)() StateSendFunc
A member function pointer for dispatching actions to a handler, without existing PDU data...
Definition: Transaction.hpp:71
U32 FileSize
File size and offset type.
void rAckTimerTick()
Perform acknowledgement timer tick (time-based) processing for R transactions.
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:54
void sTick(I32 *cont)
Perform tick (time-based) processing for S transactions.
void sSubstateSendMetadata()
Send metadata PDU.
Class::T getClass() const
Get transaction class (CLASS_1 or CLASS_2)
A dispatch table for receive file transactions, receive side.
void initTxFile(Class::T cfdp_class, Keep::T keep, U8 chan, U8 priority)
Initialize transaction for outgoing file transfer.
void rInit()
Initialize a transaction structure for R.
void sCancel()
Cancel an S transaction.
C++ header for working with basic fprime types.
Circular linked list node structure.
Definition: Clist.hpp:66
Marker value for the highest possible state number.
RateGroupDivider component implementation.
void rDispatchRecv(const Fw::Buffer &buffer, const RSubstateDispatchTable *dispatch, StateRecvFunc fd_fn)
Dispatch function for received PDUs on receive-file transactions.
void r2SetFinTxnStatus(TxnStatus txn_stat)
Helper function to store transaction status code and set send_fin flag.
A table of receive handler functions based on file directive code.
void txStateDispatch(const TxnSendDispatchTable *dispatch)
Top-level Dispatch function to send a PDU based on current state.
Status::T rCheckCrc(U32 expected_crc)
Checks that the transaction file&#39;s CRC matches expected.
A table of transmit handler functions based on transaction state.
Definition: Transaction.hpp:92
T
The raw enum type.
Definition: ClassEnumAc.hpp:42
void s2HandleFin(const FinPdu &fin)
S2 handling of a successfully deserialized FIN.
The type of a NAK PDU.
Definition: NakPdu.hpp:24
void(Transaction::*)(const Fw::Buffer &buffer) StateRecvFunc
A member function pointer for dispatching actions to a handler, with existing PDU data...
Definition: Transaction.hpp:83
void s2Nak(const Fw::Buffer &pdu)
S2 NAK PDU received handling.
void r1Recv(const Fw::Buffer &buffer)
R1 receive PDU processing.
Pairs an offset with a size to identify a specific piece of a file.
Definition: Chunk.hpp:49
CFDP History entry.
Definition: Types.hpp:244