Skip to content

Commit

Permalink
Handle upseide down transmissions, make all FEC maximum, and fix a cr…
Browse files Browse the repository at this point in the history
…ash.
  • Loading branch information
g4klx committed Aug 25, 2023
1 parent f6c6b51 commit 8a5054b
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 80 deletions.
27 changes: 8 additions & 19 deletions IL2PRXFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

#include "IL2PRXFrame.h"
#include "Debug.h"

#include <cstring>

Expand Down Expand Up @@ -90,11 +91,9 @@ bool CIL2PRXFrame::processHeader(const uint8_t* in, uint8_t* out)
if (m_payloadByteCount > 1023U)
return false;

bool maxFEC = (buffer[0U] & 0x80U) == 0x80U;

m_outOffset = m_headerByteCount;

calculatePayloadBlockSize(maxFEC);
calculatePayloadBlockSize();

return true;
}
Expand Down Expand Up @@ -147,7 +146,7 @@ uint16_t CIL2PRXFrame::getPayloadParityLength() const
return m_paritySymbolsPerBlock * (m_largeBlockCount + m_smallBlockCount);
}

void CIL2PRXFrame::calculatePayloadBlockSize(bool max)
void CIL2PRXFrame::calculatePayloadBlockSize()
{
if (m_payloadByteCount == 0U) {
m_payloadBlockCount = 0U;
Expand All @@ -156,10 +155,7 @@ void CIL2PRXFrame::calculatePayloadBlockSize(bool max)
m_largeBlockCount = 0U;
m_smallBlockCount = 0U;
m_paritySymbolsPerBlock = 0U;
return;
}

if (max) {
} else {
m_payloadBlockCount = m_payloadByteCount / 239U;
m_payloadBlockCount += (m_payloadByteCount % 239U) > 0U ? 1U : 0U;

Expand All @@ -170,22 +166,13 @@ void CIL2PRXFrame::calculatePayloadBlockSize(bool max)
m_smallBlockCount = m_payloadBlockCount - m_largeBlockCount;

m_paritySymbolsPerBlock = 16U;
} else {
m_payloadBlockCount = m_payloadByteCount / 247U;
m_payloadBlockCount += (m_payloadByteCount % 247U) > 0U ? 1U : 0U;

m_smallBlockSize = m_payloadByteCount / m_payloadBlockCount;
m_largeBlockSize = m_smallBlockSize + 1U;

m_largeBlockCount = m_payloadByteCount - (m_payloadBlockCount * m_smallBlockSize);
m_smallBlockCount = m_payloadBlockCount - m_largeBlockCount;

m_paritySymbolsPerBlock = (m_smallBlockSize / 32U) + 2U;
}
}

void CIL2PRXFrame::processType0Header(const uint8_t* in, uint8_t* out)
{
DEBUG1("IL2PRX: type 0 header");

m_headerByteCount = 0U;
m_payloadByteCount = 0U;

Expand All @@ -203,6 +190,8 @@ void CIL2PRXFrame::processType0Header(const uint8_t* in, uint8_t* out)

void CIL2PRXFrame::processType1Header(const uint8_t* in, uint8_t* out)
{
DEBUG1("IL2PRX: type 1 header");

m_payloadByteCount = 0U;

::memset(out, 0x00U, 15U);
Expand Down
2 changes: 1 addition & 1 deletion IL2PRXFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class CIL2PRXFrame {
uint8_t m_paritySymbolsPerBlock;
uint16_t m_outOffset;

void calculatePayloadBlockSize(bool max);
void calculatePayloadBlockSize();

void processType0Header(const uint8_t* in, uint8_t* out);
void processType1Header(const uint8_t* in, uint8_t* out);
Expand Down
33 changes: 9 additions & 24 deletions IL2PTXFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

#include "IL2PTXFrame.h"
#include "Debug.h"

#include <cstring>

Expand Down Expand Up @@ -48,8 +49,7 @@ static const struct IL2P_PID {
{0xCEU, 0x0DU} // FlexNet
};

CIL2PTXFrame::CIL2PTXFrame(bool maxFEC) :
m_maxFEC(maxFEC),
CIL2PTXFrame::CIL2PTXFrame() :
m_rs2(2U),
m_rs4(4U),
m_rs6(6U),
Expand Down Expand Up @@ -78,7 +78,7 @@ uint16_t CIL2PTXFrame::process(const uint8_t* in, uint16_t inLength, uint8_t* ou
else
processType0Header(in, inLength, out);

calculatePayloadBlockSize(m_maxFEC);
calculatePayloadBlockSize();

// Scramble and RS encode the IL2P header
scramble(out, IL2P_HDR_LENGTH);
Expand Down Expand Up @@ -150,9 +150,9 @@ bool CIL2PTXFrame::isIL2PType1(const uint8_t* frame, uint16_t length) const

void CIL2PTXFrame::processType0Header(const uint8_t* in, uint16_t length, uint8_t* out)
{
::memset(out, 0x00U, IL2P_HDR_LENGTH);
DEBUG1("IL2PTX: type 0 header");

out[0U] = m_maxFEC ? 0x80U : 0x00U;
::memset(out, 0x00U, IL2P_HDR_LENGTH);

out[2U] = (length & 0x0200U) == 0x0200U ? 0x80U : 0x00U;
out[3U] = (length & 0x0100U) == 0x0100U ? 0x80U : 0x00U;
Expand All @@ -171,9 +171,9 @@ void CIL2PTXFrame::processType0Header(const uint8_t* in, uint16_t length, uint8_

void CIL2PTXFrame::processType1Header(const uint8_t* in, uint16_t length, uint8_t* out)
{
::memset(out, 0x00U, IL2P_HDR_LENGTH);
DEBUG1("IL2PTX: type 1 header");

out[0U] = m_maxFEC ? 0x80U : 0x00U;
::memset(out, 0x00U, IL2P_HDR_LENGTH);

m_payloadByteCount = 0U;
m_payloadOffset = 0U;
Expand Down Expand Up @@ -206,7 +206,6 @@ void CIL2PTXFrame::processType1Header(const uint8_t* in, uint16_t length, uint8_
} else if ((in[14U] & 0x03U) == 0x01U) {
// S frame
// Also a PID of 0x00 but that's default anyway
out[5U] |= 0x40U;
out[6U] |= (in[14U] & 0x80U) == 0x80U ? 0x40U : 0x00U;
out[7U] |= (in[14U] & 0x40U) == 0x40U ? 0x40U : 0x00U;
out[8U] |= (in[14U] & 0x20U) == 0x20U ? 0x40U : 0x00U;
Expand Down Expand Up @@ -312,7 +311,7 @@ void CIL2PTXFrame::processType1Header(const uint8_t* in, uint16_t length, uint8_
}
}

void CIL2PTXFrame::calculatePayloadBlockSize(bool max)
void CIL2PTXFrame::calculatePayloadBlockSize()
{
if (m_payloadByteCount == 0U) {
m_payloadBlockCount = 0U;
Expand All @@ -321,10 +320,7 @@ void CIL2PTXFrame::calculatePayloadBlockSize(bool max)
m_largeBlockCount = 0U;
m_smallBlockCount = 0U;
m_paritySymbolsPerBlock = 0U;
return;
}

if (max) {
} else {
m_payloadBlockCount = m_payloadByteCount / 239U;
m_payloadBlockCount += (m_payloadByteCount % 239U) > 0U ? 1U : 0U;

Expand All @@ -335,17 +331,6 @@ void CIL2PTXFrame::calculatePayloadBlockSize(bool max)
m_smallBlockCount = m_payloadBlockCount - m_largeBlockCount;

m_paritySymbolsPerBlock = 16U;
} else {
m_payloadBlockCount = m_payloadByteCount / 247U;
m_payloadBlockCount += (m_payloadByteCount % 247U) > 0U ? 1U : 0U;

m_smallBlockSize = m_payloadByteCount / m_payloadBlockCount;
m_largeBlockSize = m_smallBlockSize + 1U;

m_largeBlockCount = m_payloadByteCount - (m_payloadBlockCount * m_smallBlockSize);
m_smallBlockCount = m_payloadBlockCount - m_largeBlockCount;

m_paritySymbolsPerBlock = (m_smallBlockSize / 32U) + 2U;
}
}

Expand Down
5 changes: 2 additions & 3 deletions IL2PTXFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@

class CIL2PTXFrame {
public:
CIL2PTXFrame(bool maxFEC);
CIL2PTXFrame();
~CIL2PTXFrame();

uint16_t process(const uint8_t* in, uint16_t inLength, uint8_t* out);

private:
bool m_maxFEC;
CIL2PRS m_rs2;
CIL2PRS m_rs4;
CIL2PRS m_rs6;
Expand All @@ -50,7 +49,7 @@ class CIL2PTXFrame {
void processType0Header(const uint8_t* in, uint16_t length, uint8_t* out);
void processType1Header(const uint8_t* in, uint16_t length, uint8_t* out);

void calculatePayloadBlockSize(bool max);
void calculatePayloadBlockSize();

void scramble(uint8_t* buffer, uint16_t length) const;

Expand Down
2 changes: 2 additions & 0 deletions Mode2Defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ const uint8_t MODE2_HEADER_PARITY_BYTES = 2U;
const uint8_t MODE2_HEADER_PARITY_SYMBOLS = MODE2_HEADER_PARITY_BYTES * MODE2_SYMBOLS_PER_BYTE;
const uint8_t MODE2_HEADER_PARITY_SAMPLES = MODE2_HEADER_PARITY_SYMBOLS * MODE2_RADIO_SYMBOL_LENGTH;

const uint8_t MODE2_PAYLOAD_PARITY_BYTES = 16U;

const uint8_t MODE2_SYNC_LENGTH_BYTES = 6U;
const uint8_t MODE2_SYNC_LENGTH_SYMBOLS = MODE2_SYNC_LENGTH_BYTES * MODE2_SYMBOLS_PER_BYTE;
const uint16_t MODE2_SYNC_LENGTH_SAMPLES = MODE2_SYNC_LENGTH_SYMBOLS * MODE2_RADIO_SYMBOL_LENGTH;
Expand Down
39 changes: 8 additions & 31 deletions Mode2RX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ void CMode2RX::processHeader(q15_t sample)
if (m_dataPtr == m_endPtr) {
calculateLevels(m_startPtr, MODE2_HEADER_LENGTH_SYMBOLS + MODE2_HEADER_PARITY_SYMBOLS + 1U);

uint8_t frame[MODE2_HEADER_LENGTH_BYTES + MODE2_HEADER_PARITY_BYTES];
uint8_t frame[MODE2_HEADER_LENGTH_BYTES + MODE2_HEADER_PARITY_BYTES + 1U];
samplesToBits(m_startPtr, MODE2_HEADER_LENGTH_SYMBOLS + MODE2_HEADER_PARITY_SYMBOLS + 1U, frame);

bool ok = m_frame.processHeader(frame, m_packet);
Expand All @@ -186,8 +186,6 @@ void CMode2RX::processHeader(q15_t sample)

// The payload starts right after the header
m_startPtr = m_endPtr;
if (m_startPtr >= MODE2_MAX_LENGTH_SAMPLES)
m_startPtr -= MODE2_MAX_LENGTH_SAMPLES;

m_endPtr = m_startPtr + (length * MODE2_SYMBOLS_PER_BYTE * MODE2_RADIO_SYMBOL_LENGTH);
if (m_endPtr >= MODE2_MAX_LENGTH_SAMPLES)
Expand All @@ -201,23 +199,15 @@ void CMode2RX::processHeader(q15_t sample)
io.setDecode(false);
io.setADCDetection(false);

m_state = MODE2RXS_NONE;
m_endPtr = NOENDPTR;
m_averagePtr = NOAVEPTR;
m_countdown = 0U;
m_maxCorr = 0;
reset();
}
} else {
DEBUG1("Mode2RX: header is invalid");

io.setDecode(false);
io.setADCDetection(false);

m_state = MODE2RXS_NONE;
m_endPtr = NOENDPTR;
m_averagePtr = NOAVEPTR;
m_countdown = 0U;
m_maxCorr = 0;
reset();
}
}
}
Expand All @@ -230,7 +220,7 @@ void CMode2RX::processPayload(q15_t sample)

calculateLevels(m_startPtr, overallLength * MODE2_SYMBOLS_PER_BYTE + 1U);

uint8_t frame[1023U + (5U * 16U)];
uint8_t frame[1023U + (5U * MODE2_PAYLOAD_PARITY_BYTES)];
samplesToBits(m_startPtr, overallLength * MODE2_SYMBOLS_PER_BYTE + 1U, frame);

bool ok = m_frame.processPayload(frame, m_packet);
Expand All @@ -239,27 +229,14 @@ void CMode2RX::processPayload(q15_t sample)

uint16_t length = m_frame.getHeaderLength() + payloadLength;
serial.writeKISSData(KISS_TYPE_DATA, m_packet, length);

io.setDecode(false);
io.setADCDetection(false);

m_state = MODE2RXS_NONE;
m_endPtr = NOENDPTR;
m_averagePtr = NOAVEPTR;
m_countdown = 0U;
m_maxCorr = 0;
} else {
DEBUG1("Mode2RX: payload is invalid");
}

io.setDecode(false);
io.setADCDetection(false);
io.setDecode(false);
io.setADCDetection(false);

m_state = MODE2RXS_NONE;
m_endPtr = NOENDPTR;
m_averagePtr = NOAVEPTR;
m_countdown = 0U;
m_maxCorr = 0;
}
reset();
}
}

Expand Down
2 changes: 1 addition & 1 deletion Mode2RX.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ enum MODE2RX_STATE {
MODE2RXS_PAYLOAD
};

const uint16_t MODE2_MAX_LENGTH_SAMPLES = (1023U + MODE2_HEADER_LENGTH_BYTES + MODE2_HEADER_PARITY_BYTES + 5U * 16U) * MODE2_SYMBOLS_PER_BYTE;
const uint16_t MODE2_MAX_LENGTH_SAMPLES = (1023U + MODE2_HEADER_LENGTH_BYTES + MODE2_HEADER_PARITY_BYTES + 5U * MODE2_PAYLOAD_PARITY_BYTES) * MODE2_SYMBOLS_PER_BYTE;

class CMode2RX {
public:
Expand Down
2 changes: 1 addition & 1 deletion Mode2TX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ CMode2TX::CMode2TX() :
m_fifo(3000U),
m_modFilter(),
m_modState(),
m_frame(false),
m_frame(),
m_level(MODE2_TX_LEVEL * 128),
m_txDelay((TX_DELAY / 10U) * 12U),
m_tokens()
Expand Down

0 comments on commit 8a5054b

Please sign in to comment.