You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
dvmhost/src/host/p25/packet/Data.h

307 lines
13 KiB

// SPDX-License-Identifier: GPL-2.0-only
/*
* Digital Voice Modem - Modem Host Software
* GPLv2 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright (C) 2016,2017 Jonathan Naylor, G4KLX
* Copyright (C) 2017-2025 Bryan Biedenkapp, N2PLL
* Copyright (C) 2026 David Andrzejewski
*
*/
/**
* @file Data.h
* @ingroup host_p25
* @file Data.cpp
* @ingroup host_p25
*/
#if !defined(__P25_PACKET_DATA_H__)
#define __P25_PACKET_DATA_H__
#include "Defines.h"
#include "common/p25/data/DataBlock.h"
#include "common/p25/data/DataHeader.h"
#include "common/p25/data/LowSpeedData.h"
#include "common/p25/data/Assembler.h"
#include "common/p25/lc/LC.h"
#include "common/Timer.h"
#include "p25/Control.h"
#include <cstdio>
#include <deque>
#include <mutex>
#include <string>
#include <unordered_map>
#include <vector>
// ---------------------------------------------------------------------------
// Class Prototypes
// ---------------------------------------------------------------------------
#if defined(CATCH2_TEST_COMPILATION)
class HostTestHooks;
#endif
namespace p25
{
// ---------------------------------------------------------------------------
// Class Prototypes
// ---------------------------------------------------------------------------
class HOST_SW_API Control;
namespace packet { class HOST_SW_API Trunk; }
namespace packet
{
// ---------------------------------------------------------------------------
// Class Declaration
// ---------------------------------------------------------------------------
/**
* @brief This class implements handling logic for P25 data packets.
* @ingroup host_p25
*/
class HOST_SW_API Data {
public:
/**
* @brief Resets the data states for the RF interface.
*/
void resetRF();
/** @name Frame Processing */
/**
* @brief Process a data frame from the RF interface.
* @param data Buffer containing data frame.
* @param len Length of data frame.
* @returns bool True, if data frame is processed, otherwise false.
*/
bool process(uint8_t* data, uint32_t len);
/**
* @brief Process a data frame from the network.
* @param data Buffer containing data frame.
* @param len Length of data frame.
* @param currentBlock
* @param blockLength
* @param totalBlocks
* @returns bool True, if data frame is processed, otherwise false.
*/
bool processNetwork(uint8_t* data, uint32_t len, uint8_t currentBlock, uint32_t blockLength,
uint16_t totalBlocks);
/** @} */
/**
* @brief Helper to write user data as a P25 PDU packet.
* @param dataHeader Instance of a PDU data header.
* @param extendedAddress Flag indicating whether or not extended addressing is in use.
* @param auxiliaryES Flag indicating whether or not an auxiliary ES is included.
* @param pduUserData Buffer containing user data to transmit.
* @param imm Flag indicating the PDU should be written to the immediate queue.
*/
bool writeRF_PDU_User(data::DataHeader& dataHeader, bool extendedAddress, bool auxiliaryES, uint8_t* pduUserData, bool imm = false);
/**
* @brief Helper to write user data as a P25 PDU packet.
* @param dataHeader Instance of a PDU data header.
* @param extendedAddress Flag indicating whether or not to extended addressing is in use.
* @param auxiliaryES Flag indicating whether or not an auxiliary ES is included.
* @param pduUserData Buffer containing user data to transmit.
*/
void writeNet_PDU_User(data::DataHeader& dataHeader, bool extendedAddress, bool auxiliaryES, uint8_t* pduUserData);
/**
* @brief Updates the processor by the passed number of milliseconds.
* @param ms Number of milliseconds.
*/
void clock(uint32_t ms);
/** @name SNDCP Helper Routines */
/**
* @brief Helper to initialize the SNDCP state for a logical link ID.
* @param llId Logical Link ID.
*/
void sndcpInitialize(uint32_t llId);
/**
* @brief Helper to determine if the logical link ID has been SNDCP initialized.
* @param llId Logical Link ID.
*/
bool isSNDCPInitialized(uint32_t llId) const;
/**
* @brief Helper to reset the SNDCP state for a logical link ID.
* @param llId Logical Link ID.
* @param callTerm Flag indicating call termination should be transmitted.
*/
void sndcpReset(uint32_t llId, bool callTerm = false);
/** @} */
private:
friend class p25::Control;
#if defined(CATCH2_TEST_COMPILATION)
friend class ::HostTestHooks;
#endif
Control* m_p25;
RPT_RF_STATE m_prevRfState;
data::Assembler* m_rfAssembler;
uint8_t* m_rfPDU;
uint32_t m_rfPDUCount;
uint32_t m_rfPDUBits;
data::Assembler* m_netAssembler;
std::unordered_map<uint16_t, uint8_t*> m_netReceivedBlocks;
uint16_t m_netDataBlockCnt;
uint16_t m_netTotalBlocks;
/**
* @brief Structure to maintain the retry state of a PDU.
*/
struct RetryPDUState {
/**
* @brief Buffer containing the PDU data.
*/
std::vector<uint8_t> data;
/**
* @brief Length of the PDU in bits.
*/
uint32_t bitLength = 0U;
/**
* @brief Number of times the PDU has been retried.
*/
uint8_t retryCount = 0U;
};
std::unordered_map<uint32_t, RetryPDUState> m_retryPDUState;
struct PendingPDU {
std::vector<uint8_t> data;
uint32_t bitLength = 0U;
bool imm = false;
bool ackRetry = false;
uint32_t retryLlId = 0U;
bool trackRetry = false;
};
std::deque<PendingPDU> m_pendingPDUs;
uint32_t m_pduPacingRemainingMs;
std::mutex m_pduPacingMutex;
uint8_t* m_rfPduUserData;
uint32_t m_rfPduUserDataLength;
uint8_t* m_netPduUserData;
uint32_t m_netPduUserDataLength;
std::unordered_map<uint32_t, defines::SNDCPState::E> m_sndcpStateTable;
std::unordered_map<uint32_t, Timer> m_sndcpReadyTimers;
std::unordered_map<uint32_t, Timer> m_sndcpStandbyTimers;
bool m_inbound;
bool m_dumpPDUData;
bool m_repeatPDU;
bool m_verbose;
bool m_debug;
/**
* @brief Initializes a new instance of the Data class.
* @param p25 Instance of the Control class.
* @param dumpPDUData Flag indicating whether PDU data is dumped to the log.
* @param repeatPDU Flag indicating whether incoming PDUs will be repeated automatically.
* @param debug Flag indicating whether P25 debug is enabled.
* @param verbose Flag indicating whether P25 verbose logging is enabled.
*/
Data(Control* p25, bool dumpPDUData, bool repeatPDU, bool debug, bool verbose);
/**
* @brief Finalizes a instance of the Data class.
*/
~Data();
/**
* @brief Helper used to process conventional data registration from PDU data.
* @param pduUserData Buffer containing user data to transmit.
* @returns bool True, if conventional data registration data was processed, otherwise false.
*/
bool processConvDataReg(const uint8_t* pduUserData, uint32_t length);
/**
* @brief Helper used to process SNDCP control data from PDU data.
* @param pduUserData Buffer containing user data to transmit.
* @param length Length of the SNDCP control data in bytes.
* @returns bool True, if SNDCP control data was processed, otherwise false.
*/
bool processSNDCPControl(const uint8_t* pduUserData, uint32_t length);
/**
* @brief Helper to copy the encoded data block that follows a response header out of a
* received RF PDU (e.g. the selective retry flags of an ACK RETRY response).
* @param[in] rfPDU Received RF PDU bits, starting with the preamble.
* @param rfPDUBits Number of valid bits held in rfPDU.
* @param[out] block Buffer of at least P25_PDU_FEC_LENGTH_BYTES receiving the encoded block.
* @returns bool True if the data block was received and copied; otherwise, false.
*/
static bool getResponseDataBlock(const uint8_t* rfPDU, uint32_t rfPDUBits, uint8_t* block);
/**
* @brief Write data processed from RF to the network.
* @param currentBlock Current Block ID.
* @param data Buffer containing block data.
* @param len Length of buffer.
* @param lastBlock Flag indicating whether or not this is the last block.
*/
void writeNetwork(const uint8_t currentBlock, const uint8_t* data, uint32_t len, bool lastBlock);
/**
* @brief Helper to reset received network blocks.
*/
void resetReceivedBlocks();
/**
* @brief Helper to determine whether an RF PDU is held by, or still spaced by, the native-modem pacing guard.
* @returns bool True, if an RF PDU is held or the pacing guard is running, otherwise false.
*/
bool isRFPDUPaced();
/**
* @brief Helper to queue a complete P25 PDU packet.
* @param[in] pdu Constructed PDU to transmit.
* @param bitlength Length of PDU in bits.
* @param imm Flag indicating the PDU should be written to the immediate queue.
* @param ackRetry Flag indicating the PDU is being sent as an acknowledged retry.
* @param retryLlId Logical Link ID for retrying the PDU.
* @param trackRetry Flag indicating whether or not to track the retry.
*/
bool queueRF_PDU(const uint8_t* pdu, uint32_t bitLength, bool imm = false, bool ackRetry = false,
uint32_t retryLlId = 0U, bool trackRetry = false);
/**
* @brief Helper to write a complete P25 PDU transaction without applying the native-modem guard.
* @param pdu Constructed PDU to transmit.
* @param bitLength Length of PDU in bits.
* @param imm Flag indicating the PDU should be written to the immediate queue.
* @param ackRetry Flag indicating the PDU is being sent as an acknowledged retry.
* @param retryLlId Logical Link ID for retrying the PDU.
* @param trackRetry Flag indicating whether or not to track the retry.
* @returns bool True, if the PDU was successfully written, otherwise false.
*/
bool writeRF_PDU(const uint8_t* pdu, uint32_t bitLength, bool imm, bool ackRetry,
uint32_t retryLlId, bool trackRetry);
/**
* @brief Helper to write a network P25 PDU packet.
* This will take buffered network PDU data and repeat it over the air.
*/
bool writeNet_PDU_Buffered();
/**
* @brief Helper to re-write a received P25 PDU packet.
* This will take buffered received PDU data and repeat it over the air.
*/
void writeRF_PDU_Buffered();
/**
* @brief Helper to write a PDU acknowledge response.
* @param ackClass Acknowledgement Class.
* @param ackType Acknowledgement Type.
* @param ackStatus
* @param llId Logical Link ID.
* @param extendedAddress Flag indicating whether or not to extended addressing is in use.
* @param srcLlId Source Logical Link ID.
*/
void writeRF_PDU_Ack_Response(uint8_t ackClass, uint8_t ackType, uint8_t ackStatus, uint32_t llId, bool extendedAddress,
uint32_t srcLlId = 0U);
};
} // namespace packet
} // namespace p25
#endif // __P25_PACKET_DATA_H__

Powered by TurnKey Linux.