SatCat5
stream_buffer.cc
1 // Copyright 2024 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
5 
6 #include <hal_freertos/stream_buffer.h>
7 
8 #ifndef SATCAT5_FREERTOS_STRM_BUF_SEMPHR_TIMEOUT_MS
9 #define SATCAT5_FREERTOS_STRM_BUF_SEMPHR_TIMEOUT_MS 1
10 #endif
11 
12 #ifndef SATCAT5_FREERTOS_STRM_BUFF_SEND_TIMEOUT_MS
13 #define SATCAT5_FREERTOS_STRM_BUFF_SEND_TIMEOUT_MS 1
14 #endif
15 
17 
18 StreamBuffer::StreamBuffer(
19  u8* txbuff,
20  unsigned txbytes,
21  StreamBufferHandle_t* stream_buff_handle,
22  SemaphoreHandle_t* stream_buff_semphr)
23  : satcat5::io::WriteableRedirect(&m_tx)
24  , m_tx(txbuff, txbytes, 0)
25  , m_handle(stream_buff_handle)
26  , m_semphr(stream_buff_semphr)
27  , m_status(0)
28 {
29  // Set Callback
30  m_tx.set_callback(this);
31 }
32 
34 {
35  // Check TX Ready Bytes & Transfer
36  while (u32 txbytes = m_tx.get_peek_ready())
37  {
38  // Take Semaphore
39  m_status = xSemaphoreTake(
40  *m_semphr,
41  pdMS_TO_TICKS(SATCAT5_FREERTOS_STRM_BUF_SEMPHR_TIMEOUT_MS));
42 
43  // Error Guard
44  if (!m_status) break;
45 
46  // Send to Message Buffer
47  m_status = xStreamBufferSend(
48  *m_handle,
49  m_tx.peek(txbytes),
50  txbytes,
51  pdMS_TO_TICKS(SATCAT5_FREERTOS_STRM_BUFF_SEND_TIMEOUT_MS));
52 
53  // Release Semaphore
54  xSemaphoreGive(*m_semphr);
55 
56  // Error Guard
57  if (!m_status) break;
58 
59  // Consume Bytes
60  m_tx.read_consume(txbytes);
61  }
62 }
63 
PacketBuffer to StreamBuffer adapter.
Definition: stream_buffer.h:28
void data_rcvd(satcat5::io::Readable *src)
The data_rcvd() callback is polled whenever data is available.
const u8 * peek(unsigned nbytes) const
Peek nbytes into the circular buffer.
Definition: pkt_buffer.cc:248
unsigned get_peek_ready() const
Find the longest available contiguous segment that can be requested by peek().
Definition: pkt_buffer.cc:242
bool read_consume(unsigned nbytes) override
Read and discard 0 or more bytes.
Definition: pkt_buffer.cc:256
Abstract API for reading byte-streams and packets.
Definition: io_readable.h:68
virtual void set_callback(satcat5::io::EventListener *callback)
Update registered callback for data_rcvd() events.
Definition: io_readable.cc:39