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 
57 // ======================================================================
58 // Dispatch Table Type Definitions
59 // ======================================================================
60 
70 using StateSendFunc = void (Transaction::*)();
71 
82 using StateRecvFunc = void (Transaction::*)(const Fw::Buffer& buffer);
83 
93 };
94 
105 };
106 
117 };
118 
127 };
128 
137 };
138 
147 };
148 
157 class Transaction {
158  friend class Engine;
159  friend class Channel;
160  friend class CfdpManagerTester;
161 
162  public:
163  // ----------------------------------------------------------------------
164  // Construction and Destruction
165  // ----------------------------------------------------------------------
166 
172  Transaction(Channel* channel, U8 channelId, Engine* engine, CfdpManager* manager);
173 
174  ~Transaction();
175 
182  void reset();
183 
194  void initTxFile(Class::T cfdp_class, Keep::T keep, U8 chan, U8 priority);
195 
205  static CListTraverseStatus findBySequenceNumberCallback(CListNode* node, void* context);
206 
216  static CListTraverseStatus prioritySearchCallback(CListNode* node, void* context);
217 
218  // ----------------------------------------------------------------------
219  // Accessors
220  // ----------------------------------------------------------------------
221 
226  History* getHistory() const { return m_history; }
227 
232  U8 getPriority() const { return m_priority; }
233 
238  U8 getChannelId() const { return m_chan_num; }
239 
244  Class::T getClass() const { return m_txn_class; }
245 
250  TxnState getState() const { return m_state; }
251 
252  // ----------------------------------------------------------------------
253  // TX State Machine - Implemented in TransactionTx.cpp
254  // ----------------------------------------------------------------------
255 
256  /************************************************************************/
261  void s1Recv(const Fw::Buffer& buffer);
262 
263  /************************************************************************/
268  void s2Recv(const Fw::Buffer& buffer);
269 
270  /************************************************************************/
273  void s1Tx();
274 
275  /************************************************************************/
278  void s2Tx();
279 
280  /************************************************************************/
286  void sAckTimerTick();
287 
288  /************************************************************************/
298  void sTick(I32* cont);
299 
300  /************************************************************************/
309  void sTickNak(I32* cont);
310 
311  /************************************************************************/
314  void sCancel();
315 
316  /************************************************************************/
319  void s1SubstateSendEof();
320 
321  /************************************************************************/
324  void s2SubstateSendEof();
325 
326  /************************************************************************/
334  void sSubstateSendFileData();
335 
336  /************************************************************************/
342  void s2SubstateSendFileData();
343 
344  /************************************************************************/
350  void sSubstateSendMetadata();
351 
352  /************************************************************************/
357  void s2EarlyFin(const Fw::Buffer& pdu);
358 
359  /************************************************************************/
364  void s2Fin(const Fw::Buffer& pdu);
365 
366  /************************************************************************/
375  void s2Nak(const Fw::Buffer& pdu);
376 
377  /************************************************************************/
382  void s2NakArm(const Fw::Buffer& pdu);
383 
384  /************************************************************************/
391  void s2EofAck(const Fw::Buffer& pdu);
392 
393  private:
394  /***********************************************************************
395  *
396  * Handler routines for send-file transactions
397  * These are not called from outside this module, but are declared here so they can be unit tested
398  *
399  ************************************************************************/
400 
401  /************************************************************************/
408  Status::T sSendEof();
409 
410  Status::T sSendFileData(FileSize foffs, FileSize bytes_to_read, U8 calc_crc, FileSize* bytes_processed);
411 
412  Status::T sCheckAndRespondNak(bool* nakProcessed);
413 
414  Status::T sSendFinAck();
415 
416  public:
417  // ----------------------------------------------------------------------
418  // RX State Machine - Implemented in TransactionRx.cpp
419  // ----------------------------------------------------------------------
420 
421  /************************************************************************/
426  void r1Recv(const Fw::Buffer& buffer);
427 
428  /************************************************************************/
433  void r2Recv(const Fw::Buffer& buffer);
434 
435  /************************************************************************/
441  void rAckTimerTick();
442 
443  /************************************************************************/
454  void rTick(I32* cont);
455 
456  /************************************************************************/
459  void rCancel();
460 
461  /************************************************************************/
464  void rInit();
465 
466  /************************************************************************/
471  void r2SetFinTxnStatus(TxnStatus txn_stat);
472 
473  /************************************************************************/
480  void r1Reset();
481 
482  /************************************************************************/
487  void r2Reset();
488 
489  /************************************************************************/
496  Status::T rCheckCrc(U32 expected_crc);
497 
498  /************************************************************************/
511  void r2Complete(I32 ok_to_send_nak);
512 
513  // ----------------------------------------------------------------------
514  // Dispatch Methods (ported from cf_cfdp_dispatch.c)
515  // ----------------------------------------------------------------------
516 
517  /************************************************************************/
528  void rDispatchRecv(const Fw::Buffer& buffer, const RSubstateDispatchTable* dispatch, StateRecvFunc fd_fn);
529 
530  /************************************************************************/
539  void sDispatchRecv(const Fw::Buffer& buffer, const SSubstateRecvDispatchTable* dispatch);
540 
541  /************************************************************************/
550  void sDispatchTransmit(const SSubstateSendDispatchTable* dispatch);
551 
552  /************************************************************************/
560  void txStateDispatch(const TxnSendDispatchTable* dispatch);
561 
562  private:
563  /************************************************************************/
570  Status::T rProcessFd(const Fw::Buffer& pdu);
571 
572  /************************************************************************/
583  Status::T rSubstateRecvEof(const Fw::Buffer& pdu);
584 
585  /************************************************************************/
592  void r1SubstateRecvEof(const Fw::Buffer& pdu);
593 
594  /************************************************************************/
602  void r2SubstateRecvEof(const Fw::Buffer& pdu);
603 
604  /************************************************************************/
611  void r1SubstateRecvFileData(const Fw::Buffer& pdu);
612 
613  /************************************************************************/
624  void r2SubstateRecvFileData(const Fw::Buffer& pdu);
625 
626  /************************************************************************/
635  void r2GapCompute(const Chunk* chunk, NakPdu& nak);
636 
642  static void r2GapComputeWrapper(const Chunk* chunk, void* opaque);
643 
644  /************************************************************************/
655  Status::T rSubstateSendNak();
656 
657  /************************************************************************/
673  Status::T r2CalcCrcChunk();
674 
675  /************************************************************************/
680  Status::T r2SubstateSendFin();
681 
682  /************************************************************************/
690  void r2RecvFinAck(const Fw::Buffer& pdu);
691 
692  /************************************************************************/
703  void r2RecvMd(const Fw::Buffer& pdu);
704 
705  /************************************************************************/
708  void rSendInactivityEvent();
709 
710  private:
711  // ----------------------------------------------------------------------
712  // Member Variables
713  // ----------------------------------------------------------------------
714 
720  TxnState m_state;
721 
727  Class::T m_txn_class;
728 
734  History* m_history;
735 
741  CfdpChunkWrapper* m_chunks;
742 
748  Timer m_inactivity_timer;
749 
755  Timer m_ack_timer;
756 
760  FileSize m_fsize;
761 
765  FileSize m_foffs;
766 
770  Os::File m_fd;
771 
775  CFDP::Checksum m_crc;
776 
780  Keep::T m_keep;
781 
787  U8 m_chan_num;
788 
792  U8 m_priority;
793 
800  TransactionInitType m_initType;
801 
807  CListNode m_cl_node;
808 
814  Playback* m_pb;
815 
819  CfdpStateData m_state_data;
820 
830  CfdpStateFlags m_flags;
831 
837  CfdpManager* m_cfdpManager;
838 
844  Channel* m_chan;
845 
851  Engine* m_engine;
852 };
853 
854 } // namespace Cfdp
855 } // namespace Ccsds
856 } // namespace Svc
857 
858 #endif // Svc_Ccsds_CfdpTransaction_HPP
U8 getPriority() const
Get transaction priority.
CFDP Protocol Engine.
Definition: Engine.hpp:89
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:92
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.
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.
void(Transaction::*)() StateSendFunc
A member function pointer for dispatching actions to a handler, without existing PDU data...
Definition: Transaction.hpp:70
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:91
T
The raw enum type.
Definition: ClassEnumAc.hpp:42
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:82
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