diff --git a/DMRIPSC.cpp b/DMRIPSC.cpp index 00e1a6e1c..8187168a9 100644 --- a/DMRIPSC.cpp +++ b/DMRIPSC.cpp @@ -47,7 +47,7 @@ m_pingTimer(1000U, 5U), m_buffer(NULL), m_salt(NULL), m_streamId(NULL), -m_rxData(1000U), +m_rxData(1000U, "DMR IPSC"), m_callsign(), m_rxFrequency(0U), m_txFrequency(0U), diff --git a/DMRSlot.cpp b/DMRSlot.cpp index 9d8a28202..8e3d88924 100644 --- a/DMRSlot.cpp +++ b/DMRSlot.cpp @@ -44,7 +44,7 @@ bool CDMRSlot::m_voice2 = true; CDMRSlot::CDMRSlot(unsigned int slotNo, unsigned int timeout) : m_slotNo(slotNo), -m_queue(1000U), +m_queue(1000U, "DMR Slot"), m_state(RS_LISTENING), m_embeddedLC(), m_lc(NULL), diff --git a/DStarControl.cpp b/DStarControl.cpp index 1ef4a530d..e371d2c70 100644 --- a/DStarControl.cpp +++ b/DStarControl.cpp @@ -28,7 +28,7 @@ m_gateway(NULL), m_network(network), m_display(display), m_duplex(duplex), -m_queue(1000U), +m_queue(1000U, "D-Star Control"), m_header(), m_state(RS_LISTENING), m_net(false), diff --git a/DStarNetwork.cpp b/DStarNetwork.cpp index 57f2bba69..db0f291d8 100644 --- a/DStarNetwork.cpp +++ b/DStarNetwork.cpp @@ -39,7 +39,7 @@ m_enabled(false), m_outId(0U), m_outSeq(0U), m_inId(0U), -m_buffer(1000U), +m_buffer(1000U, "D-Star Network"), m_pollTimer(1000U, 60U), m_linkStatus(LS_NONE), m_linkReflector(NULL) diff --git a/Modem.cpp b/Modem.cpp index 66b5905f0..a7030fef4 100644 --- a/Modem.cpp +++ b/Modem.cpp @@ -87,14 +87,14 @@ m_dmrEnabled(false), m_ysfEnabled(false), m_serial(port, SERIAL_115200, true), m_buffer(NULL), -m_rxDStarData(1000U), -m_txDStarData(1000U), -m_rxDMRData1(1000U), -m_rxDMRData2(1000U), -m_txDMRData1(1000U), -m_txDMRData2(1000U), -m_rxYSFData(1000U), -m_txYSFData(1000U), +m_rxDStarData(1000U, "Modem RX D-Star"), +m_txDStarData(1000U, "Modem TX D-Star"), +m_rxDMRData1(1000U, "Modem RX DMR1"), +m_rxDMRData2(1000U, "Modem RX DMR2"), +m_txDMRData1(1000U, "Modem TX DMR1"), +m_txDMRData2(1000U, "Modem TX DMR2"), +m_rxYSFData(1000U, "Modem RX YSF"), +m_txYSFData(1000U, "Modem TX YSF"), m_statusTimer(1000U, 0U, 100U), m_dstarSpace(0U), m_dmrSpace1(0U), diff --git a/RingBuffer.h b/RingBuffer.h index 07d4798e0..2e45caaec 100644 --- a/RingBuffer.h +++ b/RingBuffer.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2006-2009,2012,2013,2015 by Jonathan Naylor G4KLX + * Copyright (C) 2006-2009,2012,2013,2015,2016 by Jonathan Naylor G4KLX * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -19,18 +19,24 @@ #ifndef RingBuffer_H #define RingBuffer_H +#include "Log.h" + +#include #include #include template class CRingBuffer { public: - CRingBuffer(unsigned int length) : + CRingBuffer(unsigned int length, const char* name) : m_length(length), + m_name(name), m_buffer(NULL), m_iPtr(0U), - m_oPtr(0U) + m_oPtr(0U), + m_full(false) { assert(length > 0U); + assert(name != NULL); m_buffer = new T[length]; @@ -42,19 +48,24 @@ template class CRingBuffer { delete[] m_buffer; } - unsigned int addData(const T* buffer, unsigned int nSamples) + bool addData(const T* buffer, unsigned int nSamples) { - if (nSamples > freeSpace()) - return 0U; - for (unsigned int i = 0U; i < nSamples; i++) { + if (m_full) { + LogError("**** Overflow in %s ring buffer", m_name); + return false; + } + m_buffer[m_iPtr++] = buffer[i]; if (m_iPtr == m_length) m_iPtr = 0U; + + if (m_iPtr == m_oPtr) + m_full = true; } - return nSamples; + return true; } unsigned int getData(T* buffer, unsigned int nSamples) @@ -65,6 +76,8 @@ template class CRingBuffer { nSamples = data; for (unsigned int i = 0U; i < nSamples; i++) { + m_full = false; + buffer[i] = m_buffer[m_oPtr++]; if (m_oPtr == m_length) @@ -94,21 +107,30 @@ template class CRingBuffer { void clear() { - m_iPtr = 0U; - m_oPtr = 0U; + m_iPtr = 0U; + m_oPtr = 0U; + m_full = false; ::memset(m_buffer, 0x00, m_length * sizeof(T)); } unsigned int freeSpace() const { - if (m_oPtr == m_iPtr) - return m_length - 1U; + if (m_oPtr == m_iPtr && !m_full) + return m_length; + + if (m_oPtr == m_iPtr && m_full) + return 0U; + + if (m_iPtr > m_oPtr) + return m_iPtr - m_oPtr; - if (m_oPtr > m_iPtr) - return m_oPtr - m_iPtr - 1U; + return (m_length + m_iPtr) - m_oPtr; + } - return m_length - (m_iPtr - m_oPtr) - 1U; + unsigned int dataSize() const + { + return m_length - freeSpace(); } bool hasSpace(unsigned int length) const @@ -118,27 +140,21 @@ template class CRingBuffer { bool hasData() const { - return m_oPtr != m_iPtr; + return m_oPtr != m_iPtr || (m_oPtr == m_iPtr && m_full); } bool isEmpty() const { - return m_oPtr == m_iPtr; + return m_oPtr == m_iPtr && !m_full; } private: unsigned int m_length; + const char* m_name; T* m_buffer; volatile unsigned int m_iPtr; volatile unsigned int m_oPtr; - - unsigned int dataSize() const - { - if (m_iPtr >= m_oPtr) - return m_iPtr - m_oPtr; - - return m_length - (m_oPtr - m_iPtr); - } + volatile bool m_full; }; #endif diff --git a/YSFEcho.cpp b/YSFEcho.cpp index 5bcf54fc7..858d69961 100644 --- a/YSFEcho.cpp +++ b/YSFEcho.cpp @@ -21,7 +21,7 @@ #include "YSFDefines.h" CYSFEcho::CYSFEcho(unsigned int delay, unsigned int space) : -m_buffer(space), +m_buffer(space, "YSF Echo"), m_timer(1000U, delay) { }