Skip to content

Commit

Permalink
refactor status bit handling slightly;
Browse files Browse the repository at this point in the history
  • Loading branch information
gatekeep committed Dec 13, 2024
1 parent 1528894 commit 1b4ea4d
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 180 deletions.
55 changes: 45 additions & 10 deletions src/common/p25/P25Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,37 @@ void P25Utils::setStatusBits(uint8_t* data, uint32_t ssOffset, bool b1, bool b2)
WRITE_BIT(data, ssOffset + 1U, b2);
}

/* Helper to set the starting status bits on P25 frame data to 1,1 for idle. */

void P25Utils::setStatusBitsStartIdle(uint8_t* data)
{
assert(data != nullptr);

// set "1,1" (Start of Inbound Slot/Idle) status bits [TIA-102.BAAA]
P25Utils::setStatusBits(data, P25_SS0_START, true, true);
}

/* Helper to set all status bits on a P25 frame data to 1,1 for idle. */

void P25Utils::setStatusBitsAllIdle(uint8_t* data, uint32_t length)
{
assert(data != nullptr);

// set "1,1" (Idle) status bits [TIA-102.BAAA]
for (uint32_t ss0Pos = P25_SS0_START; ss0Pos < length; ss0Pos += P25_SS_INCREMENT) {
uint32_t ss1Pos = ss0Pos + 1U;
WRITE_BIT(data, ss0Pos, true); // 1
WRITE_BIT(data, ss1Pos, true); // 1
}
}

/* Helper to add the status bits on P25 frame data. */

void P25Utils::addStatusBits(uint8_t* data, uint32_t length, bool inbound, bool control)
void P25Utils::addStatusBits(uint8_t* data, uint32_t length, bool busy, bool unknown)
{
assert(data != nullptr);

// insert the "10" (Unknown, use for inbound or outbound) status bits
// set "1,0" (Unknown) status bits [TIA-102.BAAA]
for (uint32_t ss0Pos = P25_SS0_START; ss0Pos < length; ss0Pos += P25_SS_INCREMENT) {
uint32_t ss1Pos = ss0Pos + 1U;
WRITE_BIT(data, ss0Pos, true); // 1
Expand All @@ -47,42 +71,53 @@ void P25Utils::addStatusBits(uint8_t* data, uint32_t length, bool inbound, bool
// interleave the requested status bits (every other)
for (uint32_t ss0Pos = P25_SS0_START; ss0Pos < length; ss0Pos += (P25_SS_INCREMENT * 2U)) {
uint32_t ss1Pos = ss0Pos + 1U;
if (inbound) {
if (busy) {
// set "0,1" (Busy) status bits [TIA-102.BAAA]
WRITE_BIT(data, ss0Pos, false); // 0
WRITE_BIT(data, ss1Pos, true); // 1
} else {
if (control) {
if (unknown) {
// set "1,0" (Unknown) status bits [TIA-102.BAAA]
WRITE_BIT(data, ss0Pos, true); // 1
WRITE_BIT(data, ss1Pos, false); // 0
} else {
// set "1,1" (Start of Inbound Slot/Idle) status bits [TIA-102.BAAA]
WRITE_BIT(data, ss0Pos, true); // 1
WRITE_BIT(data, ss1Pos, true); // 1
}
}
}
}

/* Helper to add the idle status bits on P25 frame data. */
/* Helper to add the unknown (1,0) status bits on P25 frame data. */

void P25Utils::addIdleStatusBits(uint8_t* data, uint32_t length)
void P25Utils::addUnknownStatusBits(uint8_t* data, uint32_t length, uint8_t interval)
{
assert(data != nullptr);

for (uint32_t ss0Pos = P25_SS0_START; ss0Pos < length; ss0Pos += (P25_SS_INCREMENT * 5U)) {
if (interval == 0U)
interval = 1U;

for (uint32_t ss0Pos = P25_SS0_START; ss0Pos < length; ss0Pos += (P25_SS_INCREMENT * interval)) {
uint32_t ss1Pos = ss0Pos + 1U;
// set "1,0" (Unknown) status bits [TIA-102.BAAA]
WRITE_BIT(data, ss0Pos, true); // 1
WRITE_BIT(data, ss1Pos, false); // 0
}
}

/* Helper to add the trunk start slot status bits on P25 frame data. */
/* Helper to add the idle (1,1) status bits on P25 frame data. */

void P25Utils::addTrunkSlotStatusBits(uint8_t* data, uint32_t length)
void P25Utils::addIdleStatusBits(uint8_t* data, uint32_t length, uint8_t interval)
{
assert(data != nullptr);

for (uint32_t ss0Pos = P25_SS0_START; ss0Pos < length; ss0Pos += (P25_SS_INCREMENT * 5U)) {
if (interval == 0U)
interval = 1U;

for (uint32_t ss0Pos = P25_SS0_START; ss0Pos < length; ss0Pos += (P25_SS_INCREMENT * interval)) {
uint32_t ss1Pos = ss0Pos + 1U;
// set "1,1" (Start of Inbound Slot/Idle) status bits [TIA-102.BAAA]
WRITE_BIT(data, ss0Pos, true); // 1
WRITE_BIT(data, ss1Pos, true); // 1
}
Expand Down
41 changes: 27 additions & 14 deletions src/common/p25/P25Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,35 +120,48 @@ namespace p25
/**
* @brief Helper to set the status bits on P25 frame data.
* @param data P25 frame data buffer.
* @param ssOffset
* @param ssOffset Status symbol offset (bit offset).
* @param b1 Status Bit 1
* @param b2 Status Bit 2
*/
static void setStatusBits(uint8_t* data, uint32_t ssOffset, bool b1, bool b2);
/**
* @brief Helper to set the starting status bits on P25 frame data to 1,1 for idle.
* @param data P25 frame data buffer.
*/
static void setStatusBitsStartIdle(uint8_t* data);
/**
* @brief Helper to set all status bits on a P25 frame data to 1,1 for idle.
* @param data P25 frame data buffer.
* @param length Lenght of P25 frame in bits.
*/
static void setStatusBitsAllIdle(uint8_t* data, uint32_t length);
/**
* @brief Helper to add the status bits on P25 frame data.
* This appropriately sets the status bits for the P25 frame, starting with 1,0 and then
* properly setting 0,1 for inbound traffic, or 1,1 for idle (or 1,0 for control channels).
* properly setting 0,1 for inbound traffic, or 1,1 for idle (or 1,0 for unknown).
* @param data P25 frame data buffer.
* @param length
* @param inbound Flag indicating inbound channel is busy.
* @param control Flag indicating the channel is a control channel.
* @param length Lenght of P25 frame in bits.
* @param busy Flag indicating inbound channel is busy.
* @param unknown Flag indicating unknown slot state.
*/
static void addStatusBits(uint8_t *data, uint32_t length, bool inbound, bool control = false);
static void addStatusBits(uint8_t *data, uint32_t length, bool busy, bool unknown);
/**
* @brief Helper to add the idle status bits on P25 frame data.
* This sets the status bits to 1,0 interleaved every 5th status bit pair.
* @brief Helper to add the unknown (1,0) status bits on P25 frame data.
* This sets the status bits to 1,0 interleaved every variable status bit pair.
* @param data P25 frame data buffer.
* @param length
* @param length Lenght of P25 frame in bits.
* @param interval Status bit pair interval.
*/
static void addIdleStatusBits(uint8_t* data, uint32_t length);
static void addUnknownStatusBits(uint8_t* data, uint32_t length, uint8_t interval = 5U);
/**
* @brief Helper to add the trunk start slot status bits on P25 frame data.
* This sets the status bits to 1,1 interleaved every 5th status bit pair.
* @brief Helper to add the idle (1,1) status bits on P25 frame data.
* This sets the status bits to 1,1 interleaved every variable status bit pair.
* @param data P25 frame data buffer.
* @param length
* @param length Lenght of P25 frame in bits.
* @param interval Status bit pair interval.
*/
static void addTrunkSlotStatusBits(uint8_t* data, uint32_t length);
static void addIdleStatusBits(uint8_t* data, uint32_t length, uint8_t interval = 5U);

/**
* @brief Decode bit interleaving.
Expand Down
12 changes: 5 additions & 7 deletions src/fne/network/callhandler/TagP25Data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1247,20 +1247,18 @@ void TagP25Data::write_TSDU(uint32_t peerId, lc::TSBK* tsbk)
uint8_t data[P25_TSDU_FRAME_LENGTH_BYTES];
::memset(data, 0x00U, P25_TSDU_FRAME_LENGTH_BYTES);

// Generate Sync
// generate Sync
Sync::addP25Sync(data);

// network bursts have no NID

// Generate TSBK block
// generate TSBK block
tsbk->setLastBlock(true); // always set last block -- this a Single Block TSDU
tsbk->encode(data);

// Add busy bits
P25Utils::addStatusBits(data, P25_TSDU_FRAME_LENGTH_BYTES, false);

// Set first busy bits to 1,1
P25Utils::setStatusBits(data, P25_SS0_START, true, true);
// add status bits
P25Utils::addStatusBits(data, P25_TSDU_FRAME_LENGTH_BYTES, false, true);
P25Utils::setStatusBitsStartIdle(data);

if (m_debug) {
LogDebug(LOG_RF, P25_TSDU_STR ", lco = $%02X, mfId = $%02X, lastBlock = %u, AIV = %u, EX = %u, srcId = %u, dstId = %u, sysId = $%03X, netId = $%05X",
Expand Down
54 changes: 25 additions & 29 deletions src/host/modem/ModemV24.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,14 +542,14 @@ void ModemV24::create_TDU(uint8_t* buffer)
uint8_t data[P25_TDU_FRAME_LENGTH_BYTES + 2U];
::memset(data + 2U, 0x00U, P25_TDU_FRAME_LENGTH_BYTES);

// Generate Sync
// generate Sync
Sync::addP25Sync(data + 2U);

// Generate NID
// generate NID
m_nid->encode(data + 2U, DUID::TDU);

// Add busy bits
P25Utils::addStatusBits(data + 2U, P25_TDU_FRAME_LENGTH_BITS, false);
// add status bits
P25Utils::addStatusBits(data + 2U, P25_TDU_FRAME_LENGTH_BITS, false, false);

buffer[0U] = modem::TAG_EOT;
buffer[1U] = 0x01U;
Expand Down Expand Up @@ -672,17 +672,17 @@ void ModemV24::convertToAir(const uint8_t *data, uint32_t length)
lc.setKId(m_rxCall->kId);
lc.setMI(m_rxCall->MI);

// Generate Sync
// generate Sync
Sync::addP25Sync(buffer + 2U);

// Generate NID
// generate NID
m_nid->encode(buffer + 2U, DUID::HDU);

// Generate HDU
// generate HDU
lc.encodeHDU(buffer + 2U);

// Add busy bits
P25Utils::addStatusBits(buffer + 2U, P25_HDU_FRAME_LENGTH_BITS, true);
// add status bits
P25Utils::addStatusBits(buffer + 2U, P25_HDU_FRAME_LENGTH_BITS, true, false);

buffer[0U] = modem::TAG_DATA;
buffer[1U] = 0x01U;
Expand Down Expand Up @@ -753,24 +753,22 @@ void ModemV24::convertToAir(const uint8_t *data, uint32_t length)
uint8_t buffer[P25_PDU_FRAME_LENGTH_BYTES + 2U];
::memset(buffer, 0x00U, P25_PDU_FRAME_LENGTH_BYTES + 2U);

// Add the data
// add the data
uint32_t newBitLength = P25Utils::encode(data, buffer + 2U, bitLength);
uint32_t newByteLength = newBitLength / 8U;
if ((newBitLength % 8U) > 0U)
newByteLength++;

// Regenerate Sync
// regenerate Sync
Sync::addP25Sync(buffer + 2U);

// Regenerate NID
// regenerate NID
m_nid->encode(buffer + 2U, DUID::PDU);

// Add status bits
P25Utils::addStatusBits(buffer + 2U, newBitLength, false);
// add status bits
P25Utils::addStatusBits(buffer + 2U, newBitLength, false, false);
P25Utils::addIdleStatusBits(buffer + 2U, newBitLength);

// Set first busy bits to 1,1
P25Utils::setStatusBits(buffer + 2U, P25_SS0_START, true, true);
P25Utils::setStatusBitsStartIdle(buffer + 2U);

storeConvertedRx(buffer, P25_PDU_FRAME_LENGTH_BYTES + 2U);
}
Expand All @@ -790,22 +788,20 @@ void ModemV24::convertToAir(const uint8_t *data, uint32_t length)
buffer[0U] = modem::TAG_DATA;
buffer[1U] = 0x00U;

// Generate Sync
// generate Sync
Sync::addP25Sync(buffer + 2U);

// Generate NID
// generate NID
m_nid->encode(buffer + 2U, DUID::TSDU);

// Regenerate TSDU Data
// regenerate TSDU Data
tsbk.setLastBlock(true); // always set last block -- this a Single Block TSDU
tsbk.encode(buffer + 2U);

// Add busy bits
// add status bits
P25Utils::addStatusBits(buffer + 2U, P25_TSDU_FRAME_LENGTH_BYTES, false, true);
P25Utils::addTrunkSlotStatusBits(buffer + 2U, P25_TSDU_FRAME_LENGTH_BYTES);

// Set first busy bits to 1,1
P25Utils::setStatusBits(buffer + 2U, P25_SS0_START, true, true);
P25Utils::addIdleStatusBits(buffer + 2U, P25_TSDU_FRAME_LENGTH_BYTES);
P25Utils::setStatusBitsStartIdle(buffer + 2U);

storeConvertedRx(buffer, P25_TSDU_FRAME_LENGTH_BYTES + 2U);
}
Expand Down Expand Up @@ -1028,8 +1024,8 @@ void ModemV24::convertToAir(const uint8_t *data, uint32_t length)
m_audio.encode(buffer + 2U, m_rxCall->netLDU1 + 180U, 7U);
m_audio.encode(buffer + 2U, m_rxCall->netLDU1 + 204U, 8U);

// add busy bits
P25Utils::addStatusBits(buffer + 2U, P25_LDU_FRAME_LENGTH_BITS, true);
// add status bits
P25Utils::addStatusBits(buffer + 2U, P25_LDU_FRAME_LENGTH_BITS, true, false);

buffer[0U] = modem::TAG_DATA;
buffer[1U] = 0x01U;
Expand Down Expand Up @@ -1070,8 +1066,8 @@ void ModemV24::convertToAir(const uint8_t *data, uint32_t length)
m_audio.encode(buffer + 2U, m_rxCall->netLDU2 + 180U, 7U);
m_audio.encode(buffer + 2U, m_rxCall->netLDU2 + 204U, 8U);

// add busy bits
P25Utils::addStatusBits(buffer + 2U, P25_LDU_FRAME_LENGTH_BITS, true);
// add status bits
P25Utils::addStatusBits(buffer + 2U, P25_LDU_FRAME_LENGTH_BITS, true, false);

buffer[0U] = modem::TAG_DATA;
buffer[1U] = 0x01U;
Expand Down
8 changes: 4 additions & 4 deletions src/host/p25/Control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1725,14 +1725,14 @@ void Control::writeRF_TDU(bool noNetwork, bool imm)
uint8_t data[P25_TDU_FRAME_LENGTH_BYTES + 2U];
::memset(data + 2U, 0x00U, P25_TDU_FRAME_LENGTH_BYTES);

// Generate Sync
// generate Sync
Sync::addP25Sync(data + 2U);

// Generate NID
// generate NID
m_nid.encode(data + 2U, DUID::TDU);

// Add busy bits
P25Utils::addStatusBits(data + 2U, P25_TDU_FRAME_LENGTH_BITS, false);
// add status bits
P25Utils::setStatusBitsAllIdle(data + 2U, P25_TDU_FRAME_LENGTH_BITS);

if (!noNetwork)
m_voice->writeNetwork(data + 2U, DUID::TDU);
Expand Down
Loading

0 comments on commit 1b4ea4d

Please sign in to comment.