SSP21-CPP
ILowerLayer.h
Go to the documentation of this file.
1 #ifndef SSP21_ILOWERLAYER_H
2 #define SSP21_ILOWERLAYER_H
3 
4 /** @file
5  * @brief Interface @ref ssp21::ILowerLayer.
6  */
7 
8 #include "ssp21/util/SequenceTypes.h"
9 
10 namespace ssp21
11 {
12 
13 /**
14  * @brief Performs asynchronous RX/TX operations on behalf of an @ref IUpperLayer.
15  *
16  * This class is used by @ref IStack. User of the library must implement this interface
17  * to send and receive the bytes on the wire.
18  */
20 {
21 public:
22  /**
23  * @brief Check if the layer is ready to transmit data.
24  * @return @cpp true @ce if a call to @ref start_tx_from_upper() will succeed, @cpp false @ce otherwise.
25  */
26  virtual bool is_tx_ready() const = 0;
27 
28  /**
29  * @brief Start an asynchronous TX operation.
30  * @param data Bytes to be transmitted
31  * @return @cpp true @ce if the operation was successfully executed or queued, @cpp false @ce otherwise.
32  *
33  * Once the operation is successfully completed, the implementor must call @ref IUpperLayer::on_lower_tx_ready().
34  *
35  * The underlying buffer pointed to by `data` is loaned out to this layer
36  * and must not be mutated until @ref IUpperLayer::on_lower_tx_ready() is called.
37  */
38  virtual bool start_tx_from_upper(const seq32_t& data) = 0;
39 
40  /**
41  * @brief Called by the @ref IUpperLayer when it's ready to receive the next chunk of data.
42  * @return Slice of received data
43  *
44  * The returned slice must remain valid until the next call or until the @ref IUpperLayer
45  * is closed.
46  *
47  * Implementor of this class must implement @ref ILowerLayer::start_rx_from_upper_impl().
48  */
50  {
51  if (this->upper_is_processing_rx_data)
52  {
53  this->discard_rx_data();
54  this->upper_is_processing_rx_data = false;
55  }
56 
57  const auto ret = this->start_rx_from_upper_impl();
58 
59  if (ret.is_not_empty())
60  {
61  this->upper_is_processing_rx_data = true;
62  }
63 
64  return ret;
65  }
66 
67 protected:
68  /**
69  * @brief Called when a previous @ref ILowerLayer::start_rx_from_upper() operation completes.
70  *
71  * Notifies that the buffer containing the RX data can be flushed.
72  */
73  virtual void discard_rx_data() = 0;
74 
75  /**
76  * @brief Start reading data.
77  * @return Slice of data
78  *
79  * When bytes arrive, the implementor must call @ref IUpperLayer::on_lower_rx_ready().
80  *
81  * The returned slice must remain valid until the next call or until the @ref IUpperLayer
82  * is closed.
83  *
84  * See @ref ILowerLayer::start_rx_from_upper()
85  */
86  virtual seq32_t start_rx_from_upper_impl() = 0;
87 
88  /**
89  * @brief Reset the layer
90  *
91  * This method cancels all pending RX and TX operations.
92  */
93  inline void reset_this_lower_layer()
94  {
95  this->upper_is_processing_rx_data = false;
96  }
97 
98  /**
99  * @brief Check if the upper layer is waiting for RX data.
100  * @return @cpp true @ce if the upper layer is waiting for RX data, @cpp false @ce otherwise.
101  */
102  inline bool is_upper_processing_rx() const
103  {
104  return upper_is_processing_rx_data;
105  }
106 
107 private:
108  bool upper_is_processing_rx_data = false;
109 };
110 
111 }
112 
113 #endif
SSP21-cpp main namespace.
Definition: BufferTypes.h:12
virtual bool start_tx_from_upper(const seq32_t &data)=0
Start an asynchronous TX operation.
virtual seq32_t start_rx_from_upper_impl()=0
Start reading data.
seq32_t start_rx_from_upper()
Called by the IUpperLayer when it's ready to receive the next chunk of data.
Definition: ILowerLayer.h:49
virtual void discard_rx_data()=0
Called when a previous ILowerLayer::start_rx_from_upper() operation completes.
Performs asynchronous RX/TX operations on behalf of an IUpperLayer.
Definition: ILowerLayer.h:19
bool is_upper_processing_rx() const
Check if the upper layer is waiting for RX data.
Definition: ILowerLayer.h:102
virtual bool is_tx_ready() const =0
Check if the layer is ready to transmit data.
void reset_this_lower_layer()
Reset the layer.
Definition: ILowerLayer.h:93