Skip to content

Commit 718093a

Browse files
committed
handle serial buffer overflow condition (while the modem protocol *can* send frames >512 bytes the serial port handler can only handle frames up to 512 bytes); better handle transmitting double length (512 byte) P25 PDU frames;
1 parent 251cfd4 commit 718093a

File tree

4 files changed

+39
-11
lines changed

4 files changed

+39
-11
lines changed

SerialPort.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,20 @@ void SerialPort::process()
7878
if (m_dblFrame) {
7979
m_buffer[m_ptr] = c;
8080
m_len = ((c & 0xFFU) << 8);
81+
// DEBUG3("long frame, len msb", m_len, c);
8182
} else {
8283
m_len = m_buffer[m_ptr] = c;
84+
// DEBUG2("short frame, len", m_len);
8385
}
8486
m_ptr = 2U;
8587
}
8688
else if (m_ptr == 2U && m_dblFrame) {
8789
// Handle the frame length
8890
m_buffer[m_ptr] = c;
8991
m_len = (m_len + (c & 0xFFU));
92+
if (m_len > SERIAL_FB_LEN)
93+
m_len = SERIAL_FB_LEN; // don't allow length to be longer then the buffer
94+
// DEBUG3("long frame, len lsb", m_len, c);
9095
m_ptr = 3U;
9196
}
9297
else {
@@ -350,8 +355,12 @@ void SerialPort::process()
350355
/** Project 25 */
351356
case CMD_P25_DATA:
352357
if (m_p25Enable) {
353-
if (m_modemState == STATE_IDLE || m_modemState == STATE_P25)
354-
err = p25TX.writeData(m_buffer + 3U, m_len - 3U);
358+
if (m_modemState == STATE_IDLE || m_modemState == STATE_P25) {
359+
if (m_dblFrame)
360+
err = p25TX.writeData(m_buffer + 4U, m_len - 4U);
361+
else
362+
err = p25TX.writeData(m_buffer + 3U, m_len - 3U);
363+
}
355364
}
356365
if (err == RSN_OK) {
357366
if (m_modemState == STATE_IDLE)

SerialPort.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ enum CMD_REASON_CODE {
155155
const uint8_t DVM_SHORT_FRAME_START = 0xFEU;
156156
const uint8_t DVM_LONG_FRAME_START = 0xFDU;
157157

158+
#define SERIAL_FB_LEN 518U
158159
#define SERIAL_SPEED 115200
159160
/** @} */
160161

@@ -287,8 +288,8 @@ class DSP_FW_API SerialPort {
287288
void writeDump(const uint8_t* data, uint16_t length);
288289

289290
private:
290-
uint8_t m_buffer[518U];
291-
uint8_t m_ptr;
291+
uint8_t m_buffer[SERIAL_FB_LEN];
292+
uint16_t m_ptr;
292293
uint16_t m_len;
293294
bool m_dblFrame;
294295

p25/P25TX.cpp

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ void P25TX::process()
9696

9797
/* Write data to the local buffer. */
9898

99-
uint8_t P25TX::writeData(const uint8_t* data, uint8_t length)
99+
uint8_t P25TX::writeData(const uint8_t* data, uint16_t length)
100100
{
101101
if (length < (P25_TDU_FRAME_LENGTH_BYTES + 1U))
102102
return RSN_ILLEGAL_LENGTH;
@@ -108,8 +108,16 @@ uint8_t P25TX::writeData(const uint8_t* data, uint8_t length)
108108
return RSN_RINGBUFF_FULL;
109109
}
110110

111-
m_fifo.put(length - 1U);
112-
for (uint8_t i = 0U; i < (length - 1U); i++)
111+
if (length <= 255U) {
112+
m_fifo.put(DVM_SHORT_FRAME_START);
113+
m_fifo.put(length - 1U);
114+
} else {
115+
m_fifo.put(DVM_LONG_FRAME_START);
116+
m_fifo.put(((length - 1U) >> 8U) & 0xFFU);
117+
m_fifo.put((length - 1U) & 0xFFU);
118+
}
119+
120+
for (uint16_t i = 0U; i < (length - 1U); i++)
113121
m_fifo.put(data[i + 1U]);
114122

115123
return RSN_OK;
@@ -182,9 +190,19 @@ void P25TX::createData()
182190
m_poBuffer[m_poLen++] = P25_START_SYNC;
183191
}
184192
else {
185-
uint8_t length = m_fifo.get();
193+
uint8_t frameType = m_fifo.get();
194+
uint16_t length = 0U;
195+
switch (frameType) {
196+
case DVM_SHORT_FRAME_START:
197+
length = m_fifo.get();
198+
break;
199+
case DVM_LONG_FRAME_START:
200+
length = ((m_fifo.get() & 0xFFU) << 8) + (m_fifo.get());
201+
break;
202+
}
203+
186204
DEBUG3("P25TX::createData() dataLength/fifoSpace", length, m_fifo.getSpace());
187-
for (uint8_t i = 0U; i < length; i++) {
205+
for (uint16_t i = 0U; i < length; i++) {
188206
m_poBuffer[m_poLen++] = m_fifo.get();
189207
}
190208
}
@@ -198,7 +216,7 @@ void P25TX::createCal()
198216
{
199217
// 1.2 kHz sine wave generation
200218
if (m_modemState == STATE_P25_CAL) {
201-
for (unsigned int i = 0U; i < P25_LDU_FRAME_LENGTH_BYTES; i++) {
219+
for (uint8_t i = 0U; i < P25_LDU_FRAME_LENGTH_BYTES; i++) {
202220
m_poBuffer[i] = P25_START_SYNC;
203221
}
204222

p25/P25TX.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ namespace p25
6565
* @param length Length of buffer.
6666
* @returns uint8_t Reason code.
6767
*/
68-
uint8_t writeData(const uint8_t* data, uint8_t length);
68+
uint8_t writeData(const uint8_t* data, uint16_t length);
6969

7070
/**
7171
* @brief Clears the local buffer.

0 commit comments

Comments
 (0)