add tsbkdecode and callsim tools;

r05a06_dev
Bryan Biedenkapp 1 week ago
parent 9001b569f6
commit 9d774c0d9b

@ -260,6 +260,7 @@ add_definitions(-D__GIT_VER_HASH__="${GIT_VER_HASH}")
project(dvm)
include(src/CMakeLists.txt)
include(tools/CMakeLists.txt)
if (ENABLE_TESTS)
include(CTest)

@ -0,0 +1,25 @@
# SPDX-License-Identifier: GPL-2.0-only
#/*
# * Digital Voice Modem - CMake Build System
# * GPLv2 Open Source. Use is subject to license terms.
# * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
# *
# * Copyright (C) 2026 Bryan Biedenkapp, N2PLL
# *
# */
#
## callsim
#
include(tools/callsim/CMakeLists.txt)
add_executable(callsim ${common_INCLUDE} ${callSim_SRC})
target_link_libraries(callsim PRIVATE common ${OPENSSL_LIBRARIES} ${LIBDW_LIBRARY} asio::asio Threads::Threads)
target_include_directories(callsim PRIVATE ${OPENSSL_INCLUDE_DIR} ${LIBDW_INCLUDE_DIR} src src/host tools/callsim src)
#
# TSBK/CSBK decoder
#
include(tools/tsbkdecode/CMakeLists.txt)
add_executable(tsbkdecode ${common_INCLUDE} ${tsbkDecode_SRC})
target_link_libraries(tsbkdecode PRIVATE common ${OPENSSL_LIBRARIES} ${LIBDW_LIBRARY} asio::asio Threads::Threads)
target_include_directories(tsbkdecode PRIVATE ${OPENSSL_INCLUDE_DIR} ${LIBDW_INCLUDE_DIR} tools/tsbkdecode src)

@ -0,0 +1,13 @@
# SPDX-License-Identifier: GPL-2.0-only
#/*
# * Digital Voice Modem - CMake Build System
# * GPLv2 Open Source. Use is subject to license terms.
# * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
# *
# * Copyright (C) 2026 Bryan Biedenkapp, N2PLL
# *
# */
file(GLOB callSim_SRC CONFIGURE_DEPENDS
"${CMAKE_CURRENT_LIST_DIR}/*.h"
"${CMAKE_CURRENT_LIST_DIR}/*.cpp"
)

@ -0,0 +1,830 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
* Digital Voice Modem - FNE Call Simulator
* GPLv2 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright (C) 2026 Bryan Biedenkapp, N2PLL
*
*/
/**
* @defgroup callsim FNE Call Simulator
* @brief Command-line generation of deterministic DMR, P25, and NXDN calls.
*
* @file CallSim.cpp
* @brief Implements a command-line FNE call simulator for DMR, P25 Phase 1, and NXDN.
* @ingroup callsim
*
* The simulator authenticates as a normal FNE peer and emits group calls at
* each protocol's native network-frame cadence. Calls are sequential by
* default; simultaneous mode multiplexes one independent RTP stream per
* configured talkgroup over the same authenticated connection.
*/
#include "Defines.h"
#include "CallSimNetwork.h"
#include "common/Log.h"
#include "common/Thread.h"
#include "common/dmr/DMRDefines.h"
#include "common/dmr/data/NetData.h"
#include "common/network/Network.h"
#include "common/nxdn/NXDNDefines.h"
#include "common/nxdn/Audio.h"
#include "common/nxdn/NXDNUtils.h"
#include "common/nxdn/Sync.h"
#include "common/nxdn/channel/LICH.h"
#include "common/nxdn/channel/SACCH.h"
#include "common/nxdn/lc/RTCH.h"
#include "common/p25/P25Defines.h"
#include "common/p25/data/LowSpeedData.h"
#include "common/p25/lc/LC.h"
#include "modem/Modem.h"
#undef __PROG_NAME__
#define __PROG_NAME__ "Digital Voice Modem (DVM) FNE Call Simulator"
#undef __EXE_NAME__
#define __EXE_NAME__ "callsim"
using namespace network;
#include <algorithm>
#include <atomic>
#include <cerrno>
#include <chrono>
#include <csignal>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <thread>
#include <vector>
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
static constexpr uint32_t DEFAULT_CALL_COUNT = 1U; //!< Default number of calls to emit.
static constexpr uint32_t DEFAULT_CALL_DURATION_SECONDS = 5U; //!< Default call duration in seconds.
static constexpr uint32_t DEFAULT_CALL_GAP_MS = 500U; //!< Default inter-call delay in milliseconds.
static constexpr uint32_t DEFAULT_CONNECT_TIMEOUT_MS = 10000U; //!< FNE login timeout in milliseconds.
static constexpr uint32_t MILLISECONDS_PER_SECOND = 1000U; //!< Milliseconds in one second.
static constexpr uint32_t P25_LDU_PERIOD_MS = 180U; //!< P25 Phase 1 LDU interval in milliseconds.
static constexpr uint32_t DMR_BURST_PERIOD_MS = 60U; //!< DMR voice burst interval in milliseconds.
static constexpr uint32_t NXDN_FRAME_PERIOD_MS = 80U; //!< NXDN 4800 voice-frame interval in milliseconds.
/** @brief Stationary IMBE frame taken from the project's P25 1011 Hz calibration vector. */
static const uint8_t P25_TONE_IMBE[P25DEF::RAW_IMBE_LENGTH_BYTES] = {
0xCBU, 0x19U, 0x4DU, 0x0DU, 0xCEU, 0x24U, 0xA1U, 0x24U, 0x0DU, 0x43U, 0x3CU
};
/** @brief Fixed AMBE payload taken from the project's DMR 1031 Hz calibration vector. */
static const uint8_t DMR_TONE_BURST[DMRDEF::DMR_FRAME_LENGTH_BYTES] = {
0xCEU, 0xA8U, 0xFEU, 0x83U, 0xACU, 0xC4U, 0x58U, 0x20U, 0x0AU, 0xCEU, 0xA8U,
0xFEU, 0x83U, 0xA0U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x0CU, 0xC4U, 0x58U,
0x20U, 0x0AU, 0xCEU, 0xA8U, 0xFEU, 0x83U, 0xACU, 0xC4U, 0x58U, 0x20U, 0x0AU
};
// ---------------------------------------------------------------------------
// Global Variables
// ---------------------------------------------------------------------------
std::string g_progExe = std::string(__EXE_NAME__);
static std::atomic<bool> g_stop(false); //!< Set by the signal handler to request an orderly shutdown.
// ---------------------------------------------------------------------------
// Enumeration Declaration
// ---------------------------------------------------------------------------
/**
* @brief Identifies the air-interface protocol simulated for each call.
* @ingroup callsim
*/
enum class Mode {
DMR, //!< Digital Mobile Radio Tier II/III voice.
P25, //!< APCO Project 25 Phase 1 FDMA voice.
NXDN //!< NXDN 4800 baud FDMA voice.
};
// ---------------------------------------------------------------------------
// Structure Declaration
// ---------------------------------------------------------------------------
/**
* @brief Holds validated command-line configuration for one simulator run.
* @ingroup callsim
*/
struct Options {
std::string address = "127.0.0.1"; //!< FNE hostname or IP address.
uint16_t port = TRAFFIC_DEFAULT_PORT; //!< FNE UDP traffic port.
uint32_t peerId = 9000998U; //!< Peer ID presented during FNE authentication.
std::string password = "passw0rd"; //!< Password used for FNE authentication.
std::string identity = "CallSim"; //!< Descriptive identity included in peer metadata.
Mode mode = Mode::P25; //!< Protocol used for generated calls.
uint32_t callCount = DEFAULT_CALL_COUNT; //!< Calls, or simultaneous TG-batch repetitions, to emit.
uint32_t durationSeconds = DEFAULT_CALL_DURATION_SECONDS; //!< Duration of each call in seconds.
uint32_t gapMs = DEFAULT_CALL_GAP_MS; //!< Delay between completed calls in milliseconds.
uint32_t srcId = 100001U; //!< Source ID used by the first call; incremented per call.
std::vector<uint32_t> talkgroups; //!< Destination IDs selected round-robin by call index.
std::vector<uint8_t> slots; //!< DMR slots selected round-robin by call index.
bool grantDemand = false; //!< Sends an FNE grant request before each call when true.
bool simultaneous = false; //!< Emits all configured talkgroups concurrently when true.
bool debug = false; //!< Enables verbose network logging when true.
};
// ---------------------------------------------------------------------------
// Global Functions
// ---------------------------------------------------------------------------
/**
* @brief Signal handler for orderly simulator shutdown.
* @param signal The signal number received (ignored).
*/
static void signalHandler(int)
{
g_stop = true;
}
/**
* @brief Prints version information for the program.
*/
static void version()
{
::fprintf(stdout, __PROG_NAME__ " %s (built %s)\r\n", __VER__, __BUILD__);
::fprintf(stdout, "Copyright (c) 2017-2026 Bryan Biedenkapp, N2PLL and DVMProject (https://github.com/dvmproject) Authors.\n");
::fprintf(stdout, "Portions Copyright (c) 2015-2021 by Jonathan Naylor, G4KLX and others\n");
::fprintf(stdout, HIGHLY_UNNECESSARY_DISCLAIMER_FOR_THE_MENTAL "\n\n");
}
/**
* @brief Prints command-line usage, optionally preceded by an argument error.
* @param message An optional error message to display before the usage.
* @param arg An optional argument to format into the error message.
*/
static void usage(const char* message, const char* arg)
{
version();
if (message != nullptr) {
::fprintf(stderr, "%s: ", g_progExe.c_str());
::fprintf(stderr, message, arg);
::fprintf(stderr, "\n\n");
}
::fprintf(stdout,
"usage: %s --mode <p25|dmr|nxdn> --talkgroups <tg[,tg...]> [options]\n"
"\n"
"Connection:\n"
" --address <host> FNE address (default 127.0.0.1)\n"
" --port <port> FNE traffic port (default %u)\n"
" --peer-id <id> Simulated peer ID (default 9000998)\n"
" --password <password> FNE peer password\n"
" --identity <name> Peer identity sent to the FNE\n"
"\n"
"Calls:\n"
" --mode <mode> p25, dmr, or nxdn\n"
" --calls <count> Calls, or simultaneous batch repetitions (default 1)\n"
" --duration <seconds> Call duration in seconds (default 5)\n"
" --gap <ms> Delay between calls (default 500)\n"
" --source <rid> First source radio ID (default 100001)\n"
" --talkgroups <list> Comma-separated destination IDs\n"
" --slots <list> DMR slots, one per TG or cycled (default 1)\n"
" --grant-demand Send an FNE grant request before each call\n"
" --simultaneous Emit all configured TGs concurrently per repetition\n"
" --debug Enable network debug logging\n"
" --help Show this help\n",
g_progExe.c_str(), TRAFFIC_DEFAULT_PORT);
}
/**
* @brief Parses and range-checks an unsigned decimal command-line value.
* @param text The string containing the value to parse.
* @param minimum The minimum allowed value.
* @param maximum The maximum allowed value.
* @param value The variable to store the parsed value.
* @return true if the value was successfully parsed and within range, false otherwise.
*/
static bool parseUnsigned(const char* text, uint32_t minimum, uint32_t maximum, uint32_t& value)
{
if (text == nullptr || text[0U] == '\0')
return false;
errno = 0;
char* end = nullptr;
unsigned long parsed = ::strtoul(text, &end, 10);
if (errno != 0 || end == text || *end != '\0' || parsed < minimum || parsed > maximum)
return false;
value = static_cast<uint32_t>(parsed);
return true;
}
/**
* @brief Parses a comma-separated list of range-checked unsigned decimal values.
* @param text The comma-separated list of values as a string.
* @param minimum The minimum allowed value for each item.
* @param maximum The maximum allowed value for each item.
* @param values The vector to populate with parsed values.
* @return true if all values were successfully parsed and within range, false otherwise.
*/
static bool parseList(const std::string& text, uint32_t minimum, uint32_t maximum, std::vector<uint32_t>& values)
{
// parse a comma-separated list of unsigned decimal values within the specified range
size_t start = 0U;
while (start <= text.length()) {
size_t end = text.find(',', start);
std::string item = text.substr(start, end == std::string::npos ? std::string::npos : end - start);
uint32_t value = 0U;
if (!parseUnsigned(item.c_str(), minimum, maximum, value))
return false;
values.push_back(value);
if (end == std::string::npos)
break;
start = end + 1U;
}
return !values.empty();
}
/**
* @brief Parses and validates all command-line options.
* @param argc The number of command-line arguments.
* @param argv The array of command-line argument strings.
* @param options The structure to populate with parsed options.
* @return true if all options were successfully parsed and validated, false otherwise.
*/
static bool parseOptions(int argc, char** argv, Options& options)
{
for (int i = 1; i < argc; i++) {
const std::string arg(argv[i]);
auto requireValue = [&](const char* name) -> const char* {
if (++i >= argc) {
std::string message = std::string(name) + " requires a value";
usage("error: %s", message.c_str());
return nullptr;
}
return argv[i];
};
if (arg == "--help" || arg == "-h") {
usage(nullptr, nullptr);
return false;
} else if (arg == "--debug") {
options.debug = true;
} else if (arg == "--grant-demand") {
options.grantDemand = true;
} else if (arg == "--simultaneous") {
options.simultaneous = true;
} else if (arg == "--address") {
const char* value = requireValue("--address");
if (value == nullptr)
return false;
options.address = value;
} else if (arg == "--password") {
const char* value = requireValue("--password");
if (value == nullptr)
return false;
options.password = value;
} else if (arg == "--identity") {
const char* value = requireValue("--identity");
if (value == nullptr)
return false;
options.identity = value;
} else if (arg == "--mode") {
const char* value = requireValue("--mode");
if (value == nullptr)
return false;
if (::strcmp(value, "p25") == 0)
options.mode = Mode::P25;
else if (::strcmp(value, "dmr") == 0)
options.mode = Mode::DMR;
else if (::strcmp(value, "nxdn") == 0)
options.mode = Mode::NXDN;
else {
usage("error: %s", "--mode must be p25, dmr, or nxdn");
return false;
}
} else if (arg == "--talkgroups") {
const char* value = requireValue("--talkgroups");
if (value == nullptr || !parseList(value, 1U, 0xFFFFFFU, options.talkgroups)) {
usage("error: %s", "invalid --talkgroups list");
return false;
}
} else if (arg == "--slots") {
const char* value = requireValue("--slots");
std::vector<uint32_t> slots;
if (value == nullptr || !parseList(value, 1U, 2U, slots)) {
usage("error: %s", "invalid --slots list");
return false;
}
for (uint32_t slot : slots)
options.slots.push_back(static_cast<uint8_t>(slot));
} else {
const char* value = nullptr;
uint32_t parsed = 0U;
if (arg == "--port") {
value = requireValue("--port");
if (value == nullptr || !parseUnsigned(value, 1U, 65535U, parsed)) {
usage("error: %s", "invalid --port");
return false;
}
options.port = static_cast<uint16_t>(parsed);
} else if (arg == "--peer-id") {
value = requireValue("--peer-id");
if (value == nullptr || !parseUnsigned(value, 1U, 999999999U, options.peerId)) {
usage("error: %s", "invalid --peer-id");
return false;
}
} else if (arg == "--calls") {
value = requireValue("--calls");
if (value == nullptr || !parseUnsigned(value, 1U, UINT32_MAX, options.callCount)) {
usage("error: %s", "invalid --calls");
return false;
}
} else if (arg == "--duration") {
value = requireValue("--duration");
if (value == nullptr || !parseUnsigned(value, 1U, UINT32_MAX / MILLISECONDS_PER_SECOND, options.durationSeconds)) {
usage("error: %s", "invalid --duration");
return false;
}
} else if (arg == "--gap") {
value = requireValue("--gap");
if (value == nullptr || !parseUnsigned(value, 0U, UINT32_MAX, options.gapMs)) {
usage("error: %s", "invalid --gap");
return false;
}
} else if (arg == "--source") {
value = requireValue("--source");
if (value == nullptr || !parseUnsigned(value, 1U, 0xFFFFFFU, options.srcId)) {
usage("error: %s", "invalid --source");
return false;
}
} else {
usage("error: %s", (std::string("unknown option: ") + arg).c_str());
return false;
}
}
}
if (options.talkgroups.empty()) {
usage("error: %s", "--talkgroups is required");
return false;
}
if (options.mode == Mode::NXDN && (*std::max_element(options.talkgroups.begin(), options.talkgroups.end()) > 65535U || options.srcId > 65535U)) {
usage("error: %s", "NXDN source and destination IDs must not exceed 65535");
return false;
}
if (options.slots.empty())
options.slots.push_back(1U);
return true;
}
/**
* @brief Clocks FNE timers and transport processing while maintaining a protocol delay.
* @param network The network interface to use for clocking.
* @param durationMs The duration to wait and clock in milliseconds.
*/
static void waitAndClock(CallSimNetwork& network, uint32_t durationMs)
{
auto start = std::chrono::steady_clock::now();
auto last = start;
while (!g_stop) {
auto now = std::chrono::steady_clock::now();
uint32_t elapsed = static_cast<uint32_t>(std::chrono::duration_cast<std::chrono::milliseconds>(now - start).count());
uint32_t delta = static_cast<uint32_t>(std::chrono::duration_cast<std::chrono::milliseconds>(now - last).count());
if (elapsed >= durationMs)
break;
if (delta > 0U) {
network.clockConnection(delta);
last = now;
}
Thread::sleep(1U);
}
}
/**
* @brief Waits between protocol frames while the owning scheduler clocks the connection.
* @param durationMs The duration to wait in milliseconds.
*/
static void waitForFrame(uint32_t durationMs)
{
const auto start = std::chrono::steady_clock::now();
while (!g_stop) {
const auto now = std::chrono::steady_clock::now();
const uint32_t elapsed = static_cast<uint32_t>(std::chrono::duration_cast<std::chrono::milliseconds>(now - start).count());
if (elapsed >= durationMs)
break;
Thread::sleep(1U);
}
}
/**
* @brief Sends an optional group-call grant request using the selected protocol and slot.
* @param network The network interface to use for sending the request.
* @param mode The protocol mode (DMR, P25, NXDN).
* @param srcId The source ID of the call.
* @param dstId The destination ID of the call.
* @param slot The slot number for the call (relevant for DMR mode).
* @return true if the grant request was successfully sent, false otherwise.
*/
static bool writeGrantDemand(CallSimNetwork& network, Mode mode, uint32_t srcId, uint32_t dstId, uint8_t slot)
{
uint8_t dvmState = modem::DVM_STATE::STATE_IDLE;
switch (mode) {
case Mode::DMR:
dvmState = modem::DVM_STATE::STATE_DMR;
break;
case Mode::P25:
dvmState = modem::DVM_STATE::STATE_P25;
slot = 0U;
break;
case Mode::NXDN:
dvmState = modem::DVM_STATE::STATE_NXDN;
slot = 0U;
break;
}
// calls emitted by this simulator are group calls with default service
// options; only DMR carries a meaningful TDMA slot in this implementation
return network.writeGroupGrantRequest(dvmState, srcId, dstId, slot);
}
/**
* @brief Fills each vocoder position in a raw P25 LDU with the fixed tone vector.
* @param data Pointer to the raw P25 LDU data buffer to be filled.
*/
static void fillP25Tone(uint8_t* data)
{
static const uint16_t offsets[] = { 10U, 26U, 55U, 80U, 105U, 130U, 155U, 180U, 204U };
::memset(data, 0x00U, P25DEF::P25_LDU_FRAME_LENGTH_BYTES);
for (uint16_t offset : offsets)
::memcpy(data + offset, P25_TONE_IMBE, P25DEF::RAW_IMBE_LENGTH_BYTES);
}
/**
* @brief Emits alternating P25 LDU1/LDU2 frames followed by a TDU terminator.
* @param network The network interface to use for emitting the frames.
* @param srcId The source ID of the call.
* @param dstId The destination ID of the call.
* @param durationMs The duration of the call in milliseconds.
* @param stream The call stream state.
* @return true if the frames were successfully emitted, false otherwise.
*/
static bool emitP25(CallSimNetwork& network, uint32_t srcId, uint32_t dstId, uint32_t durationMs, CallStreamState& stream)
{
p25::lc::LC control;
control.setLCO(P25DEF::LCO::GROUP);
control.setGroup(true);
control.setSrcId(srcId);
control.setDstId(dstId);
control.setPriority(4U);
p25::data::LowSpeedData lsd;
uint8_t ldu1[P25DEF::P25_LDU_FRAME_LENGTH_BYTES];
uint8_t ldu2[P25DEF::P25_LDU_FRAME_LENGTH_BYTES];
fillP25Tone(ldu1);
fillP25Tone(ldu2);
uint32_t elapsed = 0U;
bool useLDU1 = true;
while (!g_stop && elapsed < durationMs) {
bool written = useLDU1 ? network.writeP25LDU1Call(control, lsd, ldu1, stream) : network.writeP25LDU2Call(control, lsd, ldu2, stream);
if (!written)
return false;
useLDU1 = !useLDU1;
uint32_t delay = std::min(P25_LDU_PERIOD_MS, durationMs - elapsed);
waitForFrame(delay);
elapsed += delay;
}
return network.writeP25TDUCall(control, lsd, stream);
}
/**
* @brief Emits a six-burst DMR voice sequence followed by a terminator-with-LC marker.
* @param network The network interface to use for emitting the call.
* @param srcId The source ID of the call.
* @param dstId The destination ID of the call.
* @param slot The slot number for the call (relevant for DMR mode).
* @param durationMs The duration of the call in milliseconds.
* @param stream The call stream state.
* @return true if the call was successfully emitted, false otherwise.
*/
static bool emitDMR(CallSimNetwork& network, uint32_t srcId, uint32_t dstId, uint8_t slot, uint32_t durationMs, CallStreamState& stream)
{
uint32_t elapsed = 0U;
uint8_t n = 0U;
uint8_t seqNo = 0U;
while (!g_stop && elapsed < durationMs) {
dmr::data::NetData frame;
frame.setSlotNo(slot);
frame.setDataType(n == 0U ? DMRDEF::DataType::VOICE_SYNC : DMRDEF::DataType::VOICE);
frame.setSrcId(srcId);
frame.setDstId(dstId);
frame.setFLCO(DMRDEF::FLCO::GROUP);
frame.setN(n);
frame.setSeqNo(seqNo++);
frame.setBER(0U);
frame.setRSSI(0U);
frame.setData(DMR_TONE_BURST);
if (!network.writeDMRCall(frame, stream))
return false;
n = (n + 1U) % 6U;
uint32_t delay = std::min(DMR_BURST_PERIOD_MS, durationMs - elapsed);
waitForFrame(delay);
elapsed += delay;
}
dmr::data::NetData terminator;
uint8_t data[DMRDEF::DMR_FRAME_LENGTH_BYTES] = { 0U };
terminator.setSlotNo(slot);
terminator.setDataType(DMRDEF::DataType::TERMINATOR_WITH_LC);
terminator.setSrcId(srcId);
terminator.setDstId(dstId);
terminator.setFLCO(DMRDEF::FLCO::GROUP);
terminator.setSeqNo(seqNo);
terminator.setData(data);
return network.writeDMRCall(terminator, stream, true);
}
/**
* @brief Emits one NXDN 4800 voice frame with segmented SACCH link control.
* @param network The network interface to use for emitting the frame.
* @param control The RTCH link control structure.
* @param sequence The sequence number of the frame within the superframe.
* @param stream The call stream state.
* @param end Indicates if this is the last frame of the call.
* @return true if the frame was successfully emitted, false otherwise.
*/
static bool emitNXDNFrame(CallSimNetwork& network, nxdn::lc::RTCH& control, uint8_t sequence, CallStreamState& stream, bool end = false)
{
using namespace nxdn;
using namespace nxdn::defines;
uint8_t frame[NXDN_FRAME_LENGTH_BYTES];
::memset(frame, 0x00U, sizeof(frame));
Sync::addNXDNSync(frame);
channel::LICH lich;
lich.setRFCT(RFChannelType::RDCH);
lich.setFCT(FuncChannelType::USC_SACCH_SS);
lich.setOption(ChOption::STEAL_NONE);
lich.setOutbound(true);
lich.encode(frame);
uint8_t lcData[NXDN_RTCH_LC_LENGTH_BYTES] = { 0U };
control.encode(lcData, NXDN_RTCH_LC_LENGTH_BITS);
static const ChStructure::E structures[] = {
ChStructure::SR_1_4, ChStructure::SR_2_4, ChStructure::SR_3_4, ChStructure::SR_4_4
};
uint8_t sacchData[3U] = { 0U };
uint8_t superframeIndex = sequence % 4U;
for (uint32_t bit = 0U; bit < 18U; bit++)
WRITE_BIT(sacchData, bit, READ_BIT(lcData, superframeIndex * 18U + bit));
channel::SACCH sacch;
sacch.setData(sacchData);
sacch.setRAN(0U);
sacch.setStructure(structures[superframeIndex]);
sacch.encode(frame);
// reuse the fixed AMBE test bits as the four vocoder payloads carried by
// this NXDN frame, then apply the NXDN-specific audio FEC/interleaving
Audio audio;
uint8_t packedBits[13U] = { 0U };
const uint8_t* ambe0 = DMR_TONE_BURST;
const uint8_t* ambe1 = DMR_TONE_BURST + 9U;
const uint8_t* ambe2 = DMR_TONE_BURST + 18U;
for (uint32_t bit = 0U; bit < 49U; bit++) {
WRITE_BIT(packedBits, bit, READ_BIT(ambe0, bit));
WRITE_BIT(packedBits, 49U + bit, READ_BIT(ambe1, bit));
}
audio.encode(packedBits, frame + NXDN_FSW_LICH_SACCH_LENGTH_BYTES);
::memset(packedBits, 0x00U, sizeof(packedBits));
for (uint32_t bit = 0U; bit < 49U; bit++) {
WRITE_BIT(packedBits, bit, READ_BIT(ambe2, bit));
WRITE_BIT(packedBits, 49U + bit, READ_BIT(ambe0, bit));
}
audio.encode(packedBits, frame + NXDN_FSW_LICH_SACCH_LENGTH_BYTES + 18U);
NXDNUtils::scrambler(frame);
return network.writeNXDNCall(control, frame, NXDN_FRAME_LENGTH_BYTES, stream, end);
}
/**
* @brief Emits NXDN voice frames followed by an RTCH transmission-release frame.
* @param network The network interface to use for emitting the frames.
* @param srcId The source ID of the call.
* @param dstId The destination ID of the call.
* @param durationMs The duration of the call in milliseconds.
* @param stream The call stream state.
* @return true if the frames were successfully emitted, false otherwise.
*/
static bool emitNXDN(CallSimNetwork& network, uint32_t srcId, uint32_t dstId, uint32_t durationMs, CallStreamState& stream)
{
using namespace nxdn::defines;
nxdn::lc::RTCH control;
control.setMessageType(MessageType::RTCH_VCALL);
control.setCallType(CallType::UNSPECIFIED);
control.setGroup(true);
control.setSrcId(static_cast<uint16_t>(srcId));
control.setDstId(static_cast<uint16_t>(dstId));
control.setTransmissionMode(TransmissionMode::MODE_4800);
uint32_t elapsed = 0U;
uint8_t sequence = 0U;
while (!g_stop && elapsed < durationMs) {
if (!emitNXDNFrame(network, control, sequence++, stream))
return false;
uint32_t delay = std::min(NXDN_FRAME_PERIOD_MS, durationMs - elapsed);
waitForFrame(delay);
elapsed += delay;
}
control.setMessageType(MessageType::RTCH_TX_REL);
return emitNXDNFrame(network, control, sequence, stream, true);
}
/**
* @brief Emits one complete call using the specified options and call parameters.
* @param network The network interface to use for emitting the call.
* @param options The call simulation options.
* @param callNumber The current call number.
* @param totalCalls The total number of calls to simulate.
* @param srcId The source ID of the call.
* @param dstId The destination ID of the call.
* @param slot The slot number for the call (relevant for DMR mode).
* @param durationMs The duration of the call in milliseconds.
* @return true if the call was successfully emitted, false otherwise.
*/
static bool emitCall(CallSimNetwork& network, const Options& options, uint32_t callNumber, uint32_t totalCalls,
uint32_t srcId, uint32_t dstId, uint8_t slot, uint32_t durationMs)
{
LogInfo("Call %u/%u start: srcId = %u, dstId = %u, slot = %u, duration = %u seconds",
callNumber, totalCalls, srcId, dstId, slot, options.durationSeconds);
if (options.grantDemand) {
LogInfo("Call %u/%u grant demand: srcId = %u, dstId = %u, slot = %u",
callNumber, totalCalls, srcId, dstId, options.mode == Mode::DMR ? slot : 0U);
if (!writeGrantDemand(network, options.mode, srcId, dstId, slot)) {
LogError(LOG_NET, "call %u grant demand failed while writing to the FNE", callNumber);
return false;
}
}
CallStreamState stream;
bool success = false;
switch (options.mode) {
case Mode::P25:
success = emitP25(network, srcId, dstId, durationMs, stream);
break;
case Mode::DMR:
success = emitDMR(network, srcId, dstId, slot, durationMs, stream);
break;
case Mode::NXDN:
success = emitNXDN(network, srcId, dstId, durationMs, stream);
break;
}
if (!success) {
LogError(LOG_NET, "call %u failed while writing to the FNE", callNumber);
return false;
}
LogInfo("Call %u/%u end: srcId = %u, dstId = %u", callNumber, totalCalls, srcId, dstId);
return true;
}
// ---------------------------------------------------------------------------
// Program Entry Point
// ---------------------------------------------------------------------------
int main(int argc, char** argv)
{
if (argv[0] != nullptr && *argv[0] != 0)
g_progExe = std::string(argv[0]);
Options options;
if (!parseOptions(argc, argv, options))
return argc > 1 && (std::string(argv[1U]) == "--help" || std::string(argv[1U]) == "-h") ? EXIT_SUCCESS : EXIT_FAILURE;
version();
::signal(SIGINT, signalHandler);
::signal(SIGTERM, signalHandler);
if (!LogInitialise("", "callsim", 0U, options.debug ? 4U : 2U, false, false)) {
::fprintf(stderr, "callsim: failed to initialize logging\n");
return EXIT_FAILURE;
}
CallSimNetwork network(options.address, options.port, 0U, options.peerId, options.password,
true, options.debug, true, true, true, false, true, true, false, false, false, false);
network.setMetadata(options.identity, 0U, 0U, 0.0F, 0.0F, 0U, 0U, 0U, 0.0F, 0.0F, 0, "");
network.enable(true);
if (!network.open()) {
LogError(LOG_NET, "failed to open FNE connection to %s:%u", options.address.c_str(), options.port);
LogFinalise();
return EXIT_FAILURE;
}
// Network::open() starts the login exchange; continue clocking until the
// FNE accepts the peer or the bounded login interval expires.
uint32_t waited = 0U;
while (!g_stop && network.getStatus() != NET_STAT_RUNNING && waited < DEFAULT_CONNECT_TIMEOUT_MS) {
waitAndClock(network, 10U);
waited += 10U;
}
if (network.getStatus() != NET_STAT_RUNNING) {
LogError(LOG_NET, "FNE login did not complete within %u ms", DEFAULT_CONNECT_TIMEOUT_MS);
network.close();
LogFinalise();
return EXIT_FAILURE;
}
// in simultaneous mode each repetition starts one call per talkgroup; the
// calls retain independent RTP stream and sequence state while all socket
// access is serialized through the single authenticated connection
const uint32_t durationMs = options.durationSeconds * MILLISECONDS_PER_SECOND;
const uint32_t callsPerRepetition = options.simultaneous ? static_cast<uint32_t>(options.talkgroups.size()) : 1U;
const uint32_t totalCalls = options.callCount * callsPerRepetition;
bool success = true;
// iterate over the number of call repetitions, spawning and managing worker threads for each call
for (uint32_t repetition = 0U; repetition < options.callCount && !g_stop; repetition++) {
std::atomic<uint32_t> remaining(callsPerRepetition);
std::vector<uint8_t> results(callsPerRepetition, 0U);
std::vector<std::thread> workers;
workers.reserve(callsPerRepetition);
// spawn worker threads for each call in the current repetition
for (uint32_t member = 0U; member < callsPerRepetition; member++) {
const uint32_t scheduleIndex = options.simultaneous ? member : repetition;
const uint32_t callIndex = repetition * callsPerRepetition + member;
const uint32_t dstId = options.talkgroups[scheduleIndex % options.talkgroups.size()];
const uint8_t slot = options.slots[scheduleIndex % options.slots.size()];
const uint32_t srcId = options.srcId + callIndex;
workers.emplace_back([&, member, callIndex, srcId, dstId, slot]() {
results[member] = emitCall(network, options, callIndex + 1U, totalCalls, srcId, dstId, slot, durationMs) ? 1U : 0U;
remaining--;
});
}
auto lastClock = std::chrono::steady_clock::now();
while (remaining.load() > 0U) {
const auto now = std::chrono::steady_clock::now();
const uint32_t elapsed = static_cast<uint32_t>(std::chrono::duration_cast<std::chrono::milliseconds>(now - lastClock).count());
if (elapsed > 0U) {
network.clockConnection(elapsed);
lastClock = now;
}
Thread::sleep(1U);
}
// join all worker threads for the current repetition before proceeding
for (std::thread& worker : workers)
worker.join();
// determine if all calls in the current repetition were successful
success = std::find(results.begin(), results.end(), 0U) == results.end();
if (!success)
break;
if (repetition + 1U < options.callCount)
waitAndClock(network, options.gapMs);
}
network.close();
LogFinalise();
return success ? EXIT_SUCCESS : EXIT_FAILURE;
}

@ -0,0 +1,126 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
* Digital Voice Modem - FNE Call Simulator
* GPLv2 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright (C) 2026 Bryan Biedenkapp, N2PLL
*
*/
#include "CallSimNetwork.h"
using namespace network;
// ---------------------------------------------------------------------------
// Public Class Members
// ---------------------------------------------------------------------------
/* Clocks the shared FNE connection while excluding concurrent writers. */
void CallSimNetwork::clockConnection(uint32_t ms)
{
std::lock_guard<std::mutex> lock(m_writeMutex);
clock(ms);
}
/* Writes a DMR frame using call-specific RTP state. */
bool CallSimNetwork::writeDMRCall(const dmr::data::NetData& data, CallStreamState& stream, bool end)
{
std::lock_guard<std::mutex> lock(m_writeMutex);
if (stream.streamId == 0U)
stream.streamId = createStreamId();
uint32_t messageLength = 0U;
UInt8Array message = createDMR_Message(messageLength, stream.streamId, data);
if (message == nullptr)
return false;
return writeMaster({ NET_FUNC::PROTOCOL, NET_SUBFUNC::PROTOCOL_SUBFUNC_DMR }, message.get(), messageLength,
nextSequence(stream, end), stream.streamId);
}
/* Writes a P25 LDU1 using call-specific RTP state. */
bool CallSimNetwork::writeP25LDU1Call(const p25::lc::LC& control, const p25::data::LowSpeedData& lsd,
const uint8_t* data, CallStreamState& stream)
{
std::lock_guard<std::mutex> lock(m_writeMutex);
uint32_t messageLength = 0U;
UInt8Array message = createP25_LDU1Message(messageLength, control, lsd, data, P25DEF::FrameType::DATA_UNIT, 0U);
if (message == nullptr)
return false;
return writeMaster({ NET_FUNC::PROTOCOL, NET_SUBFUNC::PROTOCOL_SUBFUNC_P25 }, message.get(), messageLength,
nextSequence(stream, false), stream.streamId);
}
/* Writes a P25 LDU2 using call-specific RTP state. */
bool CallSimNetwork::writeP25LDU2Call(const p25::lc::LC& control, const p25::data::LowSpeedData& lsd,
const uint8_t* data, CallStreamState& stream)
{
std::lock_guard<std::mutex> lock(m_writeMutex);
uint32_t messageLength = 0U;
UInt8Array message = createP25_LDU2Message(messageLength, control, lsd, data, 0U);
if (message == nullptr)
return false;
return writeMaster({ NET_FUNC::PROTOCOL, NET_SUBFUNC::PROTOCOL_SUBFUNC_P25 }, message.get(), messageLength,
nextSequence(stream, false), stream.streamId);
}
/* Writes a terminating P25 TDU using call-specific RTP state. */
bool CallSimNetwork::writeP25TDUCall(const p25::lc::LC& control, const p25::data::LowSpeedData& lsd,
CallStreamState& stream)
{
std::lock_guard<std::mutex> lock(m_writeMutex);
uint32_t messageLength = 0U;
UInt8Array message = createP25_TDUMessage(messageLength, control, lsd, 0U);
if (message == nullptr)
return false;
return writeMaster({ NET_FUNC::PROTOCOL, NET_SUBFUNC::PROTOCOL_SUBFUNC_P25 }, message.get(), messageLength,
nextSequence(stream, true), stream.streamId);
}
/* Writes an NXDN frame using call-specific RTP state. */
bool CallSimNetwork::writeNXDNCall(const nxdn::lc::RTCH& control, const uint8_t* data, uint32_t length,
CallStreamState& stream, bool end)
{
std::lock_guard<std::mutex> lock(m_writeMutex);
uint32_t messageLength = 0U;
UInt8Array message = createNXDN_Message(messageLength, control, data, length);
if (message == nullptr)
return false;
return writeMaster({ NET_FUNC::PROTOCOL, NET_SUBFUNC::PROTOCOL_SUBFUNC_NXDN }, message.get(), messageLength,
nextSequence(stream, end), stream.streamId);
}
/* Writes a group-call grant request while excluding concurrent frame writers. */
bool CallSimNetwork::writeGroupGrantRequest(uint8_t mode, uint32_t srcId, uint32_t dstId, uint8_t slot)
{
std::lock_guard<std::mutex> lock(m_writeMutex);
return writeGrantReq(mode, srcId, dstId, slot, false, 0U);
}
// ---------------------------------------------------------------------------
// Private Class Members
// ---------------------------------------------------------------------------
/* Allocates an RTP stream ID on first use and returns the next sequence. */
uint16_t CallSimNetwork::nextSequence(CallStreamState& stream, bool end)
{
if (stream.streamId == 0U)
stream.streamId = createStreamId();
if (end)
return RTP_END_OF_CALL_SEQ;
return stream.sequence++;
}

@ -0,0 +1,129 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
* Digital Voice Modem - FNE Call Simulator
* GPLv2 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright (C) 2026 Bryan Biedenkapp, N2PLL
*
*/
/**
* @file CallSimNetwork.h
* @brief Defines single-connection, multi-stream FNE networking for callsim.
* @ingroup callsim
*/
#if !defined(__CALL_SIM_NETWORK_H__)
#define __CALL_SIM_NETWORK_H__
#include "common/network/Network.h"
#include <cstdint>
#include <mutex>
// ---------------------------------------------------------------------------
// Structure Declaration
// ---------------------------------------------------------------------------
/**
* @brief Tracks the independent RTP state assigned to one simulated call.
* @ingroup callsim
*/
struct CallStreamState {
uint32_t streamId = 0U; //!< RTP stream ID; allocated when the first frame is written.
uint16_t sequence = 0U; //!< Next ordinary RTP packet sequence number.
};
// ---------------------------------------------------------------------------
// Class Declaration
// ---------------------------------------------------------------------------
/**
* @brief Multiplexes independent simulated calls over one authenticated FNE connection.
* @ingroup callsim
*/
class CallSimNetwork : public network::Network {
public:
using network::Network::Network;
/**
* @brief Clocks the shared FNE connection while excluding concurrent writers.
* @param ms Elapsed time in milliseconds.
*/
void clockConnection(uint32_t ms);
/**
* @brief Writes a DMR frame using call-specific RTP state.
* @param[in] data DMR network frame.
* @param[in,out] stream RTP state for the simulated call.
* @param end Flag indicating that this is the final call frame.
* @return True when the frame was accepted for transmission.
*/
bool writeDMRCall(const dmr::data::NetData& data, CallStreamState& stream, bool end = false);
/**
* @brief Writes a P25 LDU1 using call-specific RTP state.
* @param[in] control P25 link control.
* @param[in] lsd P25 low-speed data.
* @param[in] data Raw P25 LDU data.
* @param[in,out] stream RTP state for the simulated call.
* @return True when the frame was accepted for transmission.
*/
bool writeP25LDU1Call(const p25::lc::LC& control, const p25::data::LowSpeedData& lsd,
const uint8_t* data, CallStreamState& stream);
/**
* @brief Writes a P25 LDU2 using call-specific RTP state.
* @param[in] control P25 link control.
* @param[in] lsd P25 low-speed data.
* @param[in] data Raw P25 LDU data.
* @param[in,out] stream RTP state for the simulated call.
* @return True when the frame was accepted for transmission.
*/
bool writeP25LDU2Call(const p25::lc::LC& control, const p25::data::LowSpeedData& lsd,
const uint8_t* data, CallStreamState& stream);
/**
* @brief Writes a terminating P25 TDU using call-specific RTP state.
* @param[in] control P25 link control.
* @param[in] lsd P25 low-speed data.
* @param[in,out] stream RTP state for the simulated call.
* @return True when the frame was accepted for transmission.
*/
bool writeP25TDUCall(const p25::lc::LC& control, const p25::data::LowSpeedData& lsd,
CallStreamState& stream);
/**
* @brief Writes an NXDN frame using call-specific RTP state.
* @param[in] control NXDN link control.
* @param[in] data Raw NXDN frame.
* @param length Raw frame length in bytes.
* @param[in,out] stream RTP state for the simulated call.
* @param end Flag indicating that this is the final call frame.
* @return True when the frame was accepted for transmission.
*/
bool writeNXDNCall(const nxdn::lc::RTCH& control, const uint8_t* data, uint32_t length,
CallStreamState& stream, bool end = false);
/**
* @brief Writes a group-call grant request while excluding concurrent frame writers.
* @param mode DVM protocol mode.
* @param srcId Source subscriber ID.
* @param dstId Destination talkgroup ID.
* @param slot DMR slot, or zero for FDMA protocols.
* @return True when the request was accepted for transmission.
*/
bool writeGroupGrantRequest(uint8_t mode, uint32_t srcId, uint32_t dstId, uint8_t slot);
private:
std::mutex m_writeMutex; //!< Serializes socket writes and connection clock processing.
/**
* @brief Allocates an RTP stream ID on first use and returns the next sequence.
* @param[in,out] stream RTP state for the simulated call.
* @param end Flag indicating an end-of-call sequence marker.
* @return RTP packet sequence value for the next write.
*/
uint16_t nextSequence(CallStreamState& stream, bool end);
};
#endif // __CALL_SIM_NETWORK_H__

@ -0,0 +1,25 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
* Digital Voice Modem - FNE Call Simulator
* GPLv2 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright (C) 2026 Bryan Biedenkapp, N2PLL
*
*/
/**
* @file Defines.h
* @ingroup callsim
*/
#ifndef CALLSIM_DEFINES_H
#define CALLSIM_DEFINES_H
#include "common/Defines.h"
#include "common/GitHash.h"
#undef __PROG_NAME__
#define __PROG_NAME__ "Digital Voice Modem (DVM) FNE Call Simulator"
#undef __EXE_NAME__
#define __EXE_NAME__ "callsim"
#endif // CALLSIM_DEFINES_H

@ -0,0 +1,123 @@
# callsim
`callsim` is a command-line FNE peer that generates deterministic group voice
calls for exercising DMR, P25, and NXDN call handling. It authenticates to an
FNE traffic port as one peer and can emit calls sequentially or multiplex
simultaneous calls to several talkgroups over that single connection.
The generated audio is a fixed calibration-tone test pattern. P25 uses the
project's 1011 Hz IMBE vector, while DMR uses its 1031 Hz AMBE vector. NXDN
builds its voice payload from the fixed AMBE test bits and applies the NXDN
voice encoding and interleaving used by the common library. This is synthetic
protocol traffic; the tool does not read audio from a file or sound device.
## Build
Build the target from a configured repository build tree:
```sh
cmake --build build --target callsim -j6
```
Depending on whether `dvmhost` is built directly or as part of the
superproject, the executable will normally be `build/callsim` or
`build/dvmhost/callsim`.
## Basic usage
The protocol mode and at least one destination talkgroup are required:
```sh
build/callsim \
--address 127.0.0.1 \
--port 62031 \
--peer-id 9000998 \
--password passw0rd \
--mode p25 \
--talkgroups 1001 \
--calls 3 \
--duration 5
```
This example logs in as peer `9000998` and sends three five-second P25 Phase 1
group calls to talkgroup `1001`. Source radio IDs begin at `100001` and increase
by one for every generated call.
## Options
| Option | Description |
| --- | --- |
| `--address <host>` | FNE address. The default is `127.0.0.1`. |
| `--port <port>` | FNE UDP traffic port. The default is `62031`. |
| `--peer-id <id>` | Peer ID used for FNE authentication. The default is `9000998`. |
| `--password <password>` | FNE peer password. The default is `passw0rd`. |
| `--identity <name>` | Peer identity reported to the FNE. The default is `CallSim`. |
| `--mode <mode>` | Required digital mode: `p25`, `dmr`, or `nxdn`. |
| `--talkgroups <list>` | Required comma-separated destination talkgroup IDs. |
| `--calls <count>` | Number of calls, or simultaneous-call batches. The default is `1`. |
| `--duration <seconds>` | Duration of each call in seconds. The default is `5`. |
| `--gap <ms>` | Delay between calls or batches in milliseconds. The default is `500`. |
| `--source <rid>` | Source radio ID for the first call. The default is `100001`. |
| `--slots <list>` | Comma-separated DMR slots (`1` or `2`), cycled as needed. The default is slot `1`. |
| `--grant-demand` | Sends an FNE group-call grant request before every call. |
| `--simultaneous` | Starts one call per configured talkgroup in each batch. |
| `--debug` | Enables verbose network logging. |
| `--help`, `-h` | Displays command-line help. |
P25 and DMR source and destination IDs may be up to 24 bits. NXDN source and
destination IDs must not exceed `65535`. The `--slots` setting is used only for
DMR; P25 and NXDN are FDMA modes.
## Multiple talkgroups
Without `--simultaneous`, calls are sent one at a time and the talkgroup and
DMR slot lists are selected round-robin:
```sh
build/callsim \
--mode dmr \
--talkgroups 1001,1002 \
--slots 1,2 \
--calls 6 \
--duration 10
```
This produces six sequential calls, alternating talkgroups `1001` and `1002`
and slots 1 and 2.
With `--simultaneous`, each repetition starts one call for every talkgroup:
```sh
build/callsim \
--mode dmr \
--talkgroups 1001,1002 \
--slots 1,2 \
--calls 3 \
--duration 10 \
--grant-demand \
--simultaneous
```
This produces three batches of two concurrent calls. Each call has independent
RTP stream and sequence state, but all traffic uses one authenticated FNE peer
connection. The gap is applied between completed batches, not between members
of a batch. If fewer slots than talkgroups are supplied, the slot list cycles;
concurrent calls assigned to the same DMR slot may be rejected or contend under
the connected FNE's policy.
## Grant requests and call termination
By default, voice traffic begins without a preceding grant request. Add
`--grant-demand` when the target system should receive an FNE group-call grant
request before each voice stream. The request uses the call's source ID,
talkgroup, and DMR slot where applicable.
The simulator sends normal protocol-specific call termination signalling after
the requested duration: a TDU for P25, a terminator-with-link-control burst for
DMR, or an RTCH transmission release for NXDN. `SIGINT` and `SIGTERM` request an
orderly shutdown.
The peer ID and credentials must be accepted by the target FNE, and the selected
source IDs, talkgroups, modes, and slots remain subject to its access and grant
policy. Use the simulator only against a test system where generated call
traffic is expected.

@ -0,0 +1,13 @@
# SPDX-License-Identifier: GPL-2.0-only
#/*
# * Digital Voice Modem - CMake Build System
# * GPLv2 Open Source. Use is subject to license terms.
# * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
# *
# * Copyright (C) 2026 Bryan Biedenkapp, N2PLL
# *
# */
file(GLOB tsbkDecode_SRC CONFIGURE_DEPENDS
"${CMAKE_CURRENT_LIST_DIR}/*.h"
"${CMAKE_CURRENT_LIST_DIR}/*.cpp"
)

@ -0,0 +1,457 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
* Digital Voice Modem - TSBK/CSBK Decoder
* GPLv2 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright (C) 2026 Bryan Biedenkapp, N2PLL
*
*/
#include "Defines.h"
#include "Decode.h"
#include "common/edac/CRC.h"
#include "common/edac/BPTC19696.h"
#include "common/p25/P25Utils.h"
#include "common/p25/lc/tsbk/TSBKFactory.h"
#include "common/dmr/lc/csbk/CSBKFactory.h"
#include <algorithm>
#include <array>
#include <iomanip>
#include <iostream>
// ---------------------------------------------------------------------------
// Internal Functions
// ---------------------------------------------------------------------------
/**
* @brief Prints a numeric field in decimal and hexadecimal.
* @tparam T Numeric type.
* @param name Field name.
* @param value Numeric value.
*/
template<typename T>
void field(const char* name, T value)
{
std::cout << " " << name << ": " << +value << " (0x" << std::hex << +value << std::dec << ")\n";
}
/**
* @brief Prints a boolean field.
* @param name Field name.
* @param value Boolean value.
*/
void field(const char* name, bool value)
{
std::cout << " " << name << ": " << (value ? "true" : "false") << '\n';
}
/**
* @brief Dumps the decoded bytes and common block fields.
* @param raw Pointer to the raw block bytes.
*/
void dump(const uint8_t* raw)
{
std::cout << "Decoded bytes:";
for (unsigned i = 0; i < 12; ++i)
std::cout << ' ' << std::hex << std::setw(2) << std::setfill('0') << +raw[i];
std::cout << std::dec << '\n';
field("Opcode", raw[0] & 0x3f);
field("Manufacturer / feature ID", raw[1]);
field("Last block", (raw[0] & 0x80) != 0);
}
// ---------------------------------------------------------------------------
// Global Functions
// ---------------------------------------------------------------------------
/* Decodes a raw block or frame and prints its fields. */
bool decodeBlock(std::vector<uint8_t> bytes, bool csbk, bool frame, bool isp)
{
const size_t expected = frame ? (csbk ? 33U : 45U) : 12U;
if ((!frame && bytes.size() != 10U && bytes.size() != 12U) ||
(frame && bytes.size() != expected)) {
std::cerr << "Expected " << (frame ? (csbk ? "33" : "45") : "10 or 12") << " bytes.\n";
return false;
}
std::array<uint8_t, 12> raw{};
std::array<uint8_t, 33> burst{};
if (frame) {
if (csbk) {
edac::BPTC19696 bptc;
bptc.decode(bytes.data(), raw.data());
}
else {
std::array<uint8_t, 25> fec{};
p25::P25Utils::decode(bytes.data(), fec.data(), 114U, 318U);
edac::Trellis trellis;
if (!trellis.decode12(fec.data(), raw.data())) {
std::cerr << "Trellis decoding failed.\n";
return false;
}
}
}
else {
std::copy(bytes.begin(), bytes.end(), raw.begin());
if (bytes.size() == 10U) {
edac::CRC::addCCITT162(raw.data(), 12U);
if (csbk) {
raw[10] ^= 0xa5;
raw[11] ^= 0xa5;
}
std::cout << "CRC generated for 10-byte input.\n";
}
}
auto checksum = raw;
if (csbk) {
checksum[10] ^= 0xa5;
checksum[11] ^= 0xa5;
}
const bool validCRC = edac::CRC::checkCCITT162(checksum.data(), 12U);
if (!validCRC)
std::cerr << "Warning: CRC check failed; continuing decode.\n";
dump(raw.data());
p25::lc::TSBK::setWarnCRC(true);
p25::lc::tsbk::TSBKFactory::setWarnCRC(true);
dmr::lc::CSBK::setWarnCRC(true);
dmr::lc::csbk::CSBKFactory::setWarnCRC(true);
if (csbk) {
edac::BPTC19696 bptc;
bptc.encode(raw.data(), burst.data());
auto block = dmr::lc::csbk::CSBKFactory::createCSBK(burst.data(), dmr::defines::DataType::CSBK);
if (!block) {
std::cerr << "Unsupported CSBK opcode.\n";
return false;
}
std::cout << block->toString() << '\n';
if (!block->getDecodedRaw()) {
std::cerr << "CSBK decoder is not implemented in the common library.\n";
return false;
}
if (auto typed = dynamic_cast<dmr::lc::csbk::CSBK_ACK_RSP*>(block.get())) {
field("GI", typed->getGI());
field("SrcId", typed->getSrcId());
field("DstId", typed->getDstId());
field("Reason", typed->getReason());
}
if (auto typed = dynamic_cast<dmr::lc::csbk::CSBK_BROADCAST*>(block.get())) {
field("ColorCode", typed->getColorCode());
field("LogicalCh1", typed->getLogicalCh1());
field("LogicalCh2", typed->getLogicalCh2());
field("AnncType", typed->getAnncType());
field("AnnWdCh1", typed->getAnnWdCh1());
field("AnnWdCh2", typed->getAnnWdCh2());
field("RequireReg", typed->getRequireReg());
field("SystemId", typed->getSystemId());
field("BackoffNo", typed->getBackoffNo());
}
if (auto typed = dynamic_cast<dmr::lc::csbk::CSBK_BSDWNACT*>(block.get())) {
field("SrcId", typed->getSrcId());
field("BSId", typed->getBSId());
}
if (auto typed = dynamic_cast<dmr::lc::csbk::CSBK_CALL_ALRT*>(block.get())) {
field("GI", typed->getGI());
field("SrcId", typed->getSrcId());
field("DstId", typed->getDstId());
}
if (auto typed = dynamic_cast<dmr::lc::csbk::CSBK_EXT_FNCT*>(block.get())) {
field("SrcId", typed->getSrcId());
field("DstId", typed->getDstId());
field("DataContent", typed->getDataContent());
field("ExtendedFunction", typed->getExtendedFunction());
}
if (auto typed = dynamic_cast<dmr::lc::csbk::CSBK_MAINT*>(block.get())) {
field("SrcId", typed->getSrcId());
field("DstId", typed->getDstId());
field("MaintKind", typed->getMaintKind());
}
if (auto typed = dynamic_cast<dmr::lc::csbk::CSBK_NACK_RSP*>(block.get())) {
field("GI", typed->getGI());
field("SrcId", typed->getSrcId());
field("DstId", typed->getDstId());
field("Reason", typed->getReason());
field("ServiceKind", typed->getServiceKind());
}
if (auto typed = dynamic_cast<dmr::lc::csbk::CSBK_PRECCSBK*>(block.get())) {
field("GI", typed->getGI());
field("SrcId", typed->getSrcId());
field("DstId", typed->getDstId());
field("DataContent", typed->getDataContent());
field("CBF", typed->getCBF());
}
if (auto typed = dynamic_cast<dmr::lc::csbk::CSBK_RAND*>(block.get())) {
field("SrcId", typed->getSrcId());
field("DstId", typed->getDstId());
field("Proxy", typed->getProxy());
field("ServiceOptions", typed->getServiceOptions());
field("ServiceExtra", typed->getServiceExtra());
field("ServiceKind", typed->getServiceKind());
}
if (auto typed = dynamic_cast<dmr::lc::csbk::CSBK_UU_ANS_RSP*>(block.get())) {
field("SrcId", typed->getSrcId());
field("DstId", typed->getDstId());
}
if (auto typed = dynamic_cast<dmr::lc::csbk::CSBK_UU_V_REQ*>(block.get())) {
field("SrcId", typed->getSrcId());
field("DstId", typed->getDstId());
}
}
else {
// These outbound opcodes overlap inbound authentication messages in the factory.
const unsigned opcode = raw[0] & 0x3f;
if (!isp && (opcode == p25::defines::TSBKO::OSP_NET_STS_BCAST ||
opcode == p25::defines::TSBKO::OSP_RFSS_STS_BCAST)) {
field("LRA", raw[2]);
if (opcode == p25::defines::TSBKO::OSP_NET_STS_BCAST) {
std::cout << "Network Status Broadcast\n";
field("NetId", (unsigned(raw[3]) << 12) | (unsigned(raw[4]) << 4) | (raw[5] >> 4));
field("SysId", ((raw[5] & 0x0f) << 8) | raw[6]);
}
else {
std::cout << "RFSS Status Broadcast\n";
field("SysId", ((raw[3] & 0x0f) << 8) | raw[4]);
field("RFSSId", raw[5]);
field("SiteId", raw[6]);
}
field("ChannelId", raw[7] >> 4);
field("ChannelNo", ((raw[7] & 0x0f) << 8) | raw[8]);
field("ServiceClass", raw[9]);
return true;
}
auto block = p25::lc::tsbk::TSBKFactory::createTSBK(raw.data(), true);
if (!block) {
std::cerr << "Unsupported TSBK opcode.\n";
return false;
}
std::cout << block->toString(isp) << '\n';
if (!block->getDecodedRaw()) {
std::cerr << "TSBK decoder is not implemented in the common library.\n";
return false;
}
if (auto typed = dynamic_cast<p25::lc::tsbk::IOSP_ACK_RSP*>(block.get())) {
field("SrcId", typed->getSrcId());
field("DstId", typed->getDstId());
field("AIV", typed->getAIV());
field("ExtendedAddr", typed->getEX());
if (typed->getAIV() && typed->getEX()) {
field("NetId", typed->getNetId());
field("SysId", typed->getSysId());
}
field("Service", typed->getService());
}
if (auto typed = dynamic_cast<p25::lc::tsbk::IOSP_CALL_ALRT*>(block.get())) {
field("SrcId", typed->getSrcId());
field("DstId", typed->getDstId());
}
if (auto typed = dynamic_cast<p25::lc::tsbk::IOSP_EXT_FNCT*>(block.get())) {
field("SrcId", typed->getSrcId());
field("DstId", typed->getDstId());
field("ExtendedFunction", typed->getExtendedFunction());
}
if (auto typed = dynamic_cast<p25::lc::tsbk::IOSP_GRP_AFF*>(block.get())) {
field("SrcId", typed->getSrcId());
field("DstId", typed->getDstId());
field("SysId", typed->getSysId());
}
if (auto typed = dynamic_cast<p25::lc::tsbk::IOSP_GRP_VCH*>(block.get())) {
field("SrcId", typed->getSrcId());
field("DstId", typed->getDstId());
field("GrpVchId", typed->getGrpVchId());
field("GrpVchNo", typed->getGrpVchNo());
field("Emergency", typed->getEmergency());
field("Encrypted", typed->getEncrypted());
field("Priority", typed->getPriority());
}
if (auto typed = dynamic_cast<p25::lc::tsbk::IOSP_MSG_UPDT*>(block.get())) {
field("SrcId", typed->getSrcId());
field("DstId", typed->getDstId());
field("Message", typed->getMessage());
}
if (auto typed = dynamic_cast<p25::lc::tsbk::IOSP_RAD_MON*>(block.get())) {
field("SrcId", typed->getSrcId());
field("DstId", typed->getDstId());
field("TxMult", typed->getTxMult());
}
if (auto typed = dynamic_cast<p25::lc::tsbk::IOSP_STS_UPDT*>(block.get())) {
field("SrcId", typed->getSrcId());
field("DstId", typed->getDstId());
field("Status", typed->getStatus());
}
if (auto typed = dynamic_cast<p25::lc::tsbk::IOSP_UU_ANS*>(block.get())) {
field("SrcId", typed->getSrcId());
field("DstId", typed->getDstId());
field("Response", typed->getResponse());
field("Emergency", typed->getEmergency());
field("Encrypted", typed->getEncrypted());
field("Priority", typed->getPriority());
}
if (auto typed = dynamic_cast<p25::lc::tsbk::IOSP_UU_VCH*>(block.get())) {
field("SrcId", typed->getSrcId());
field("DstId", typed->getDstId());
field("GrpVchId", typed->getGrpVchId());
field("GrpVchNo", typed->getGrpVchNo());
field("Emergency", typed->getEmergency());
field("Encrypted", typed->getEncrypted());
field("Priority", typed->getPriority());
}
if (auto typed = dynamic_cast<p25::lc::tsbk::IOSP_U_REG*>(block.get())) {
field("SrcId", typed->getSrcId());
field("NetId", typed->getNetId());
field("SysId", typed->getSysId());
}
if (auto typed = dynamic_cast<p25::lc::tsbk::ISP_AUTH_FNE_RST*>(block.get())) {
field("SrcId", typed->getSrcId());
field("AuthSuccess", typed->getAuthSuccess());
field("AuthStandalone", typed->getAuthStandalone());
}
if (auto typed = dynamic_cast<p25::lc::tsbk::ISP_AUTH_RESP*>(block.get())) {
field("SrcId", typed->getSrcId());
field("AuthStandalone", typed->getAuthStandalone());
}
if (auto typed = dynamic_cast<p25::lc::tsbk::ISP_AUTH_SU_DMD*>(block.get())) {
field("SrcId", typed->getSrcId());
}
if (auto typed = dynamic_cast<p25::lc::tsbk::ISP_CAN_SRV_REQ*>(block.get())) {
field("SrcId", typed->getSrcId());
field("DstId", typed->getDstId());
field("AIV", typed->getAIV());
field("Service", typed->getService());
field("Response", typed->getResponse());
}
if (auto typed = dynamic_cast<p25::lc::tsbk::ISP_EMERG_ALRM_REQ*>(block.get())) {
field("SrcId", typed->getSrcId());
field("DstId", typed->getDstId());
field("AIV", typed->getAIV());
field("Service", typed->getService());
field("Response", typed->getResponse());
field("Emergency", typed->getEmergency());
}
if (auto typed = dynamic_cast<p25::lc::tsbk::ISP_GRP_AFF_Q_RSP*>(block.get())) {
field("SrcId", typed->getSrcId());
field("DstId", typed->getDstId());
field("AnnounceGroup", typed->getAnnounceGroup());
}
if (auto typed = dynamic_cast<p25::lc::tsbk::ISP_LOC_REG_REQ*>(block.get())) {
field("SrcId", typed->getSrcId());
field("DstId", typed->getDstId());
field("LRA", typed->getLRA());
}
if (auto typed = dynamic_cast<p25::lc::tsbk::ISP_SNDCP_CH_REQ*>(block.get())) {
field("SrcId", typed->getSrcId());
field("DataServiceOptions", typed->getDataServiceOptions());
field("DataAccessControl", typed->getDataAccessControl());
}
if (auto typed = dynamic_cast<p25::lc::tsbk::ISP_SNDCP_REC_REQ*>(block.get())) {
field("SrcId", typed->getSrcId());
field("DataToSend", typed->getDataToSend());
field("DataServiceOptions", typed->getDataServiceOptions());
field("DataAccessControl", typed->getDataAccessControl());
}
if (auto typed = dynamic_cast<p25::lc::tsbk::ISP_U_DEREG_REQ*>(block.get())) {
field("SrcId", typed->getSrcId());
field("NetId", typed->getNetId());
field("SysId", typed->getSysId());
}
if (auto typed = dynamic_cast<p25::lc::tsbk::OSP_ADJ_STS_BCAST*>(block.get())) {
field("AdjSiteSysId", typed->getAdjSiteSysId());
field("AdjSiteRFSSId", typed->getAdjSiteRFSSId());
field("AdjSiteId", typed->getAdjSiteId());
field("AdjSiteChnId", typed->getAdjSiteChnId());
field("AdjSiteChnNo", typed->getAdjSiteChnNo());
field("AdjSiteSvcClass", typed->getAdjSiteSvcClass());
}
if (auto typed = dynamic_cast<p25::lc::tsbk::OSP_DENY_RSP*>(block.get())) {
field("SrcId", typed->getSrcId());
field("DstId", typed->getDstId());
field("AIV", typed->getAIV());
field("Service", typed->getService());
field("Response", typed->getResponse());
}
if (auto typed = dynamic_cast<p25::lc::tsbk::OSP_DVM_LC_CALL_TERM*>(block.get())) {
field("SrcId", typed->getSrcId());
field("DstId", typed->getDstId());
field("GrpVchId", typed->getGrpVchId());
field("GrpVchNo", typed->getGrpVchNo());
}
if (auto typed = dynamic_cast<p25::lc::tsbk::OSP_GRP_VCH_GRANT_UPD*>(block.get())) {
field("DstId", typed->getDstId());
field("GrpVchId", typed->getGrpVchId());
field("GrpVchNo", typed->getGrpVchNo());
field("GrpVchIdB", typed->getGrpVchIdB());
field("GrpVchNoB", typed->getGrpVchNoB());
field("DstIdB", typed->getDstIdB());
}
if (auto typed = dynamic_cast<p25::lc::tsbk::OSP_QUE_RSP*>(block.get())) {
field("SrcId", typed->getSrcId());
field("DstId", typed->getDstId());
field("AIV", typed->getAIV());
field("Service", typed->getService());
field("Response", typed->getResponse());
}
if (auto typed = dynamic_cast<p25::lc::tsbk::OSP_UU_VCH_GRANT_UPD*>(block.get())) {
field("SrcId", typed->getSrcId());
field("DstId", typed->getDstId());
field("GrpVchId", typed->getGrpVchId());
field("GrpVchNo", typed->getGrpVchNo());
}
if (auto typed = dynamic_cast<p25::lc::tsbk::OSP_U_DEREG_ACK*>(block.get())) {
field("SrcId", typed->getSrcId());
field("NetId", typed->getNetId());
field("SysId", typed->getSysId());
}
}
return true;
}

@ -0,0 +1,37 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
* Digital Voice Modem - TSBK/CSBK Decoder
* GPLv2 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright (C) 2026 Bryan Biedenkapp, N2PLL
*
*/
/**
* @file Decode.h
* @ingroup tsbkdecode
* @file Decode.cpp
* @ingroup tsbkdecode
*/
#if !defined(__TSBKDECODE_DECODE_H__)
#define __TSBKDECODE_DECODE_H__
#include <cstdint>
#include <vector>
// ---------------------------------------------------------------------------
// Global Functions
// ---------------------------------------------------------------------------
/**
* @brief Decodes a raw block or frame and prints its fields.
* @param bytes Raw block or frame bytes.
* @param csbk Flag indicating a DMR CSBK rather than a P25 TSBK.
* @param frame Flag indicating a complete frame rather than a raw block.
* @param isp Flag indicating inbound P25 subscriber signalling.
* @returns bool True if decoded successfully, otherwise false for invalid input,
* unknown opcodes, or unimplemented decoders. CRC failures warn and decoding continues.
*/
bool decodeBlock(std::vector<uint8_t> bytes, bool csbk, bool frame, bool isp);
#endif // __TSBKDECODE_DECODE_H__

@ -0,0 +1,25 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
* Digital Voice Modem - TSBK/CSBK Decoder
* GPLv2 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright (C) 2026 Bryan Biedenkapp, N2PLL
*
*/
/**
* @file Defines.h
* @ingroup tsbkdecode
*/
#ifndef TSBKDECODE_DEFINES_H
#define TSBKDECODE_DEFINES_H
#include "common/Defines.h"
#include "common/GitHash.h"
#undef __PROG_NAME__
#define __PROG_NAME__ "Digital Voice Modem (DVM) TSBK/CSBK Decoder"
#undef __EXE_NAME__
#define __EXE_NAME__ "tsbkdecode"
#endif // TSBKDECODE_DEFINES_H

@ -0,0 +1,39 @@
# tsbkdecode
Standalone test tool using the common P25 TSBK and DMR CSBK decoders, with
local definitions, CLI entry point, decoder implementation, and CMake source
list following `src/testapp`.
Build from the repository root:
```sh
cmake --build build --target tsbkdecode
```
Supply hexadecimal bytes as separate arguments or one quoted/contiguous string:
```sh
build/tsbkdecode tsbk 80 00 04 10 23 12 34 00 00 01
build/tsbkdecode csbk "9f 00 00 00 12 34 56 65 43 21"
build/tsbkdecode tsbk 800004102312340000016ed5
```
Raw input accepts 10 bytes (header and payload; CRC is generated) or 12 bytes
(including CRC; failures warn and decoding continues). CSBK input uses the standard A5A5 CRC mask.
Spaces, colons, commas, and per-byte `0x` prefixes are supported.
Use `--frame` for a 45-byte P25 TSDU or 33-byte DMR burst, without modem tags.
The P25 frame path decodes the first TSBK. Use `--isp` for inbound P25 signalling;
outbound network/RFSS status broadcasts otherwise take precedence over the
inbound authentication opcodes sharing their values.
Output includes decoded bytes, opcode, manufacturer/feature ID, last-block
flag, message description, and fields extracted by the applicable decoder.
Numbers are displayed in decimal and hexadecimal. Interpretation follows the
common library's supported layouts; unknown opcodes and unimplemented decoders
report errors rather than claim a complete interpretation. No channel frequency
lookup is performed.
Exit status is 0 on successful decoding/help/version and 1 for malformed input,
incorrect length or unsupported decoding. CRC failures alone do not cause a nonzero
exit status, including with `--frame`. See `--help` for usage.

@ -0,0 +1,175 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
* Digital Voice Modem - TSBK/CSBK Decoder
* GPLv2 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright (C) 2026 Bryan Biedenkapp, N2PLL
*
*/
#include "Defines.h"
#include "Decode.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <stdexcept>
#include <cctype>
// ---------------------------------------------------------------------------
// Macros
// ---------------------------------------------------------------------------
#define IS(s) (::strcmp(argv[i], s) == 0)
// ---------------------------------------------------------------------------
// Global Variables
// ---------------------------------------------------------------------------
std::string g_progExe = std::string(__EXE_NAME__);
std::string g_protocol;
bool g_frame = false;
bool g_isp = false;
std::vector<uint8_t> g_bytes;
// ---------------------------------------------------------------------------
// Global Functions
// ---------------------------------------------------------------------------
/* Helper to print a fatal error message and exit. */
static void fatal(const char* message)
{
::fprintf(stderr, "%s: %s\n", g_progExe.c_str(), message);
exit(EXIT_FAILURE);
}
/* Helper to print version information. */
static void version()
{
::fprintf(stdout, __PROG_NAME__ " %s (built %s)\r\n", __VER__, __BUILD__);
::fprintf(stdout, "Copyright (c) 2017-2026 Bryan Biedenkapp, N2PLL and DVMProject (https://github.com/dvmproject) Authors.\n");
::fprintf(stdout, "Portions Copyright (c) 2015-2021 by Jonathan Naylor, G4KLX and others\n");
::fprintf(stdout, HIGHLY_UNNECESSARY_DISCLAIMER_FOR_THE_MENTAL "\n\n");
}
/* Helper to print usage and optionally an error. */
static void usage(const char* message, const char* arg)
{
version();
if (message != nullptr) {
::fprintf(stderr, "%s: ", g_progExe.c_str());
::fprintf(stderr, message, arg);
::fprintf(stderr, "\n\n");
}
::fprintf(stdout,
"usage: %s <tsbk|csbk> [--frame] [--isp] <hex bytes...>\n\n"
" -v, --version show version information\n"
" -h, --help show this screen\n"
" --frame 45-byte P25 TSDU or 33-byte DMR CSBK burst, without modem tags\n"
" --isp interpret P25 as inbound subscriber signalling (default: outbound)\n"
" -- stop handling options\n\n"
" Raw: 10 bytes (CRC generated) or 12 bytes (CRC checked).\n"
" CRC failures warn and decoding continues, including with --frame.\n"
" Hex: contiguous, spaced, colon/comma separated, or 0x-prefixed bytes.\n"
" CSBK CRC bytes must include the standard A5A5 mask.\n"
"Example: %s tsbk 80 00 04 10 23 12 34 00 00 01\n",
g_progExe.c_str(), g_progExe.c_str());
exit(message == nullptr ? EXIT_SUCCESS : EXIT_FAILURE);
}
/* Helper to parse hexadecimal bytes from a command line argument. */
static void parseHex(const char* value)
{
const std::string arg(value);
size_t pos = 0;
while (pos < arg.size()) {
if (std::isspace(static_cast<unsigned char>(arg[pos])) || arg[pos] == ':' || arg[pos] == ',') {
++pos;
continue;
}
if (arg.compare(pos, 2, "0x") == 0 || arg.compare(pos, 2, "0X") == 0)
pos += 2;
if (pos + 2 > arg.size() || !std::isxdigit(static_cast<unsigned char>(arg[pos])) ||
!std::isxdigit(static_cast<unsigned char>(arg[pos + 1])))
throw std::invalid_argument("Hex input must contain complete two-digit bytes.");
g_bytes.push_back(static_cast<uint8_t>(std::stoul(arg.substr(pos, 2), nullptr, 16)));
pos += 2;
}
}
/* Helper to validate the command line arguments. */
static void checkArgs(int argc, char* argv[])
{
bool options = true;
// iterate through arguments
for (int i = 1; i < argc; i++) {
if (options && *argv[i] == '-') {
if (IS("--")) {
options = false;
}
else if (IS("--frame")) {
g_frame = true;
}
else if (IS("--isp")) {
g_isp = true;
}
else if (IS("-v") || IS("--version")) {
version();
if (argc == 2)
exit(EXIT_SUCCESS);
}
else if (IS("-h") || IS("--help")) {
usage(nullptr, nullptr);
}
else {
usage("unrecognized option `%s'", argv[i]);
}
}
else if (g_protocol.empty()) {
g_protocol = std::string(argv[i]);
if (g_protocol != "tsbk" && g_protocol != "csbk")
usage("error: %s", "Protocol must be tsbk or csbk.");
}
else {
parseHex(argv[i]);
}
}
if (g_protocol.empty())
usage("error: %s", "must specify the protocol (tsbk or csbk)");
if (g_bytes.empty())
usage("error: %s", "must specify hex bytes to decode");
}
// ---------------------------------------------------------------------------
// Program Entry Point
// ---------------------------------------------------------------------------
int main(int argc, char** argv)
{
if (argv[0] != nullptr && *argv[0] != 0)
g_progExe = std::string(argv[0]);
try {
// check arguments
checkArgs(argc, argv);
return decodeBlock(g_bytes, g_protocol == "csbk", g_frame, g_isp) ? EXIT_SUCCESS : EXIT_FAILURE;
} catch (const std::exception& error) {
fatal(error.what());
}
return EXIT_FAILURE;
}
Loading…
Cancel
Save

Powered by TurnKey Linux.