diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 3bfef74ed..05820299d 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -52,6 +52,14 @@ All notable changes to GNSS-SDR will be documented in this file. code with the CL slots zeroed, the same replica that tracking already uses, instead of holding each CM chip across the CL slot. The CL half of the signal no longer adds noise to the correlation. +- Fixed the preamble search of the GPS CNAV message decoder used by the GPS L2C + and L5 telemetry decoders. The search did not test the first position of the + decoded bits buffer nor its last complete 8-bit window, so a preamble starting + at 2 out of every 32 positions with respect to the 32-bit blocks delivered by + the Viterbi decoder was missed and the first message after the start of + tracking was lost. In short captures, the satellite could end up without a + complete ephemeris set. The first position is now skipped only in the rescan + that follows a CRC failure. ### Improvements in Efficiency: diff --git a/src/algorithms/telemetry_decoder/libs/libswiftcnav/cnav_msg.c b/src/algorithms/telemetry_decoder/libs/libswiftcnav/cnav_msg.c index d7e4e5e24..6003fdad0 100644 --- a/src/algorithms/telemetry_decoder/libs/libswiftcnav/cnav_msg.c +++ b/src/algorithms/telemetry_decoder/libs/libswiftcnav/cnav_msg.c @@ -101,10 +101,13 @@ static uint32_t cnav_extract_crc_(const cnav_v27_part_t *part) /** - * Helper to rescan for preamble in the received buffer. + * Helper to scan for preamble in the received buffer. + * Every complete preamble-sized window of the buffer is tested, from the head + * of the buffer up to the last \a GPS_CNAV_PREAMBLE_LENGTH bits. * Occasionally there could be a false lock on message contents if it the - * preamble sequence is encountered in the message body. For this case, the - * function performs for a scan for a preamble with a different offset: + * preamble sequence is encountered in the message body. For this case + * (\a skip_first is set), the function performs for a scan for a preamble + * with a different offset, so the head of the buffer is not tested again. * - When found, the preamble octet is moved into the head of the buffer. * - When not found, only 7 bits are left in the buffer. * @@ -116,13 +119,17 @@ static uint32_t cnav_extract_crc_(const cnav_v27_part_t *part) */ static void cnav_rescan_preamble_(cnav_v27_part_t *part) { + /* The head of the buffer is skipped only when it holds the preamble of a + * message that has just failed the CRC check. */ + const size_t first = part->skip_first ? 1 : 0; + part->skip_first = false; part->preamble_seen = false; - if (part->n_decoded > GPS_CNAV_PREAMBLE_LENGTH + 1) + if (part->n_decoded >= GPS_CNAV_PREAMBLE_LENGTH) { size_t i = 0; size_t j = 0; - for (i = 1, j = part->n_decoded - GPS_CNAV_PREAMBLE_LENGTH; i < j; ++i) + for (i = first, j = part->n_decoded - GPS_CNAV_PREAMBLE_LENGTH; i <= j; ++i) { const uint32_t c = getbitu(part->decoded, i, GPS_CNAV_PREAMBLE_LENGTH); if (GPS_CNAV_PREAMBLE1 == c || GPS_CNAV_PREAMBLE2 == c) @@ -221,7 +228,8 @@ static void cnav_add_symbol_(cnav_v27_part_t *part, uint8_t ch) if (!part->preamble_seen) { - /* Rescan for preamble if possible. The first bit is ignored. */ + /* Scan for preamble if possible. The first bit is ignored only + * when rescanning after a CRC failure. */ cnav_rescan_preamble_(part); } if (part->preamble_seen && GPS_CNAV_MSG_LENGTH <= part->n_decoded) @@ -250,6 +258,7 @@ static void cnav_add_symbol_(cnav_v27_part_t *part, uint8_t ch) part->n_crc_fail = 0; part->message_lock = false; part->preamble_seen = false; + part->skip_first = true; /* Try to find a new preamble, reuse data from buffer. */ retry = true; } @@ -268,6 +277,7 @@ static void cnav_add_symbol_(cnav_v27_part_t *part, uint8_t ch) * false positive lock - rescan for preamble. */ part->crc_ok = false; part->preamble_seen = false; + part->skip_first = true; /* CRC mismatch - try to re-scan for preamble */ retry = true; @@ -386,6 +396,8 @@ void cnav_msg_decoder_init(cnav_msg_decoder_t *dec) 0); dec->part1.init = true; dec->part2.init = true; + dec->part1.skip_first = false; + dec->part2.skip_first = false; cnav_add_symbol_(&dec->part2, 0x80); } diff --git a/src/algorithms/telemetry_decoder/libs/libswiftcnav/cnav_msg.h b/src/algorithms/telemetry_decoder/libs/libswiftcnav/cnav_msg.h index 044bb030b..3a6a7487a 100644 --- a/src/algorithms/telemetry_decoder/libs/libswiftcnav/cnav_msg.h +++ b/src/algorithms/telemetry_decoder/libs/libswiftcnav/cnav_msg.h @@ -84,6 +84,9 @@ typedef struct size_t n_crc_fail; /**< Counter for CRC failures */ bool init; /**< Initial state flag. When true, initial bits * do not produce output. */ + bool skip_first; /**< When true, the next preamble scan skips the + * head of the decode buffer (rescan after a CRC + * failure). */ } cnav_v27_part_t; /** diff --git a/tests/unit-tests/signal-processing-blocks/telemetry_decoder/gps_cnav_msg_decoder_test.cc b/tests/unit-tests/signal-processing-blocks/telemetry_decoder/gps_cnav_msg_decoder_test.cc new file mode 100644 index 000000000..a5c940796 --- /dev/null +++ b/tests/unit-tests/signal-processing-blocks/telemetry_decoder/gps_cnav_msg_decoder_test.cc @@ -0,0 +1,237 @@ +/*! + * \file gps_cnav_msg_decoder_test.cc + * \brief Tests for the GPS CNAV message decoder (libswiftcnav): preamble + * search at every alignment of the message in the Viterbi decoding blocks + * \author Ben Gant, 2026. + * + * ----------------------------------------------------------------------------- + * + * GNSS-SDR is a Global Navigation Satellite System software-defined receiver. + * This file is part of GNSS-SDR. + * + * Copyright (C) 2010-2026 (see AUTHORS file for a list of contributors) + * SPDX-License-Identifier: GPL-3.0-or-later + * + * ----------------------------------------------------------------------------- + */ + +#include +#include +#include +#include + +extern "C" +{ +#include "cnav_msg.h" +} + + +namespace gps_cnav_msg_decoder_test +{ +const size_t message_bits = 300; // IS-GPS-200, 30.3.3: message length +const size_t crc_bits = 24; // IS-GPS-200, 30.3.5: CRC-24Q +const uint32_t preamble = 0x8BU; // IS-GPS-200, 30.3.3: 10001011 +const size_t idle_bits = 64; // zero bits ahead of the first message +const size_t flush_bits = 128; // zero bits that push the last message out +const size_t decode_block_bits = 32; // GPS_L2C_V27_DECODE_BITS + + +void append_field(std::vector& bits, uint32_t value, size_t width) +{ + for (size_t i = 0; i < width; i++) + { + bits.push_back(static_cast((value >> (width - i - 1)) & 1U)); + } +} + + +// CRC-24Q, g(X) = 0x1864CFB, computed bit by bit over the message bits +uint32_t crc24q(const std::vector& bits) +{ + uint32_t crc = 0; + for (const auto bit : bits) + { + crc ^= static_cast(bit) << 23U; + crc <<= 1U; + if ((crc & 0x1000000U) != 0U) + { + crc ^= 0x1864CFBU; + } + } + return crc & 0xFFFFFFU; +} + + +// A 300-bit CNAV message: preamble, PRN, message type ID, message TOW count, +// alert flag, 238 bits of pseudo-random payload and the CRC-24Q parity. +std::vector make_message(uint32_t prn, uint32_t msg_id, uint32_t tow, uint32_t seed) +{ + std::vector bits; + bits.reserve(message_bits); + append_field(bits, preamble, 8); + append_field(bits, prn, 6); + append_field(bits, msg_id, 6); + append_field(bits, tow, 17); + append_field(bits, 0, 1); + uint32_t lcg = seed; + while (bits.size() < message_bits - crc_bits) + { + lcg = lcg * 1664525U + 1013904223U; + bits.push_back(static_cast((lcg >> 24U) & 1U)); + } + append_field(bits, crc24q(bits), crc_bits); + return bits; +} + + +uint8_t parity7(uint32_t value) +{ + uint8_t p = 0; + for (uint32_t i = 0; i < 7; i++) + { + p ^= static_cast((value >> i) & 1U); + } + return p; +} + + +// Rate 1/2, constraint length 7 convolutional encoder, G1 = 171o, G2 = 133o +// (IS-GPS-200, 3.3.3.1.1), the G1 symbol first. Hard symbols: 0x00 or 0xFF. +std::vector encode(const std::vector& bits, bool invert) +{ + std::vector symbols; + symbols.reserve(2 * bits.size()); + uint32_t reg = 0; + for (const auto bit : bits) + { + reg = ((reg << 1U) | bit) & 0x7FU; // the newest bit is the LSB + const uint8_t g1 = parity7(reg & 0x4FU); + const uint8_t g2 = parity7(reg & 0x6DU); + symbols.push_back(((g1 != 0) != invert) ? 0xFF : 0x00); + symbols.push_back(((g2 != 0) != invert) ? 0xFF : 0x00); + } + return symbols; +} + + +// Feeds the symbols of \a bits to a new decoder and returns what it decodes. +// \a lead_symbols extra symbols ahead of the stream select which of the two +// Viterbi decoders of cnav_msg_decoder_t is the one aligned to symbol pairs. +std::vector decode(const std::vector& bits, size_t lead_symbols, bool invert) +{ + std::vector messages; + cnav_msg_decoder_t decoder; + cnav_msg_decoder_init(&decoder); + cnav_msg_t msg; + uint32_t delay = 0; + for (size_t i = 0; i < lead_symbols; i++) + { + if (cnav_msg_decoder_add_symbol(&decoder, 0x00, &msg, &delay)) + { + messages.push_back(msg); + } + } + for (const auto symbol : encode(bits, invert)) + { + if (cnav_msg_decoder_add_symbol(&decoder, symbol, &msg, &delay)) + { + messages.push_back(msg); + } + } + return messages; +} + + +bool same_bits(const cnav_msg_t& msg, const std::vector& bits) +{ + for (size_t i = 0; i < bits.size(); i++) + { + if (((msg.raw_msg[i / 8] >> (7U - i % 8)) & 1U) != bits[i]) + { + return false; + } + } + return true; +} + + +void append(std::vector& stream, const std::vector& bits) +{ + stream.insert(stream.end(), bits.begin(), bits.end()); +} +} // namespace gps_cnav_msg_decoder_test + + +// Two consecutive messages; the preamble of the first one is put at each of +// the 32 positions it can take in the 32-bit blocks the Viterbi decoder +// delivers. The first message must be decoded at every position. +TEST(GpsCnavMsgDecoderTest, FirstMessageAtEveryBlockAlignment) +{ + using namespace gps_cnav_msg_decoder_test; + const auto msg_1 = make_message(32, 10, 1001, 1); + const auto msg_2 = make_message(32, 11, 1002, 2); + + for (const bool invert : {false, true}) + { + for (size_t lead_symbols = 0; lead_symbols < 2; lead_symbols++) + { + for (size_t shift = 0; shift < decode_block_bits; shift++) + { + SCOPED_TRACE(testing::Message() << "shift=" << shift << " bits, lead_symbols=" << lead_symbols << ", invert=" << invert); + std::vector stream(idle_bits + shift, 0); + append(stream, msg_1); + append(stream, msg_2); + stream.insert(stream.end(), flush_bits, 0); + + const auto messages = decode(stream, lead_symbols, invert); + + ASSERT_FALSE(messages.empty()); + EXPECT_EQ(messages.size(), 2U); + EXPECT_EQ(messages[0].prn, 32U); + EXPECT_EQ(messages[0].msg_id, 10U); + EXPECT_EQ(messages[0].tow, 1001U); + EXPECT_FALSE(messages[0].alert); + EXPECT_TRUE(same_bits(messages[0], msg_1)); + if (messages.size() == 2) + { + EXPECT_EQ(messages[1].msg_id, 11U); + EXPECT_EQ(messages[1].tow, 1002U); + EXPECT_TRUE(same_bits(messages[1], msg_2)); + } + } + } + } +} + + +// A preamble pattern that is not followed by a valid message makes the decoder +// collect 300 bits and fail the CRC check. The rescan that follows must not +// stay on that pattern and must find the preamble of the first valid message, +// wherever it is with respect to the pattern and to the end of the decoded +// bits. +TEST(GpsCnavMsgDecoderTest, RescanAfterCrcFailure) +{ + using namespace gps_cnav_msg_decoder_test; + const auto msg_1 = make_message(7, 30, 2001, 3); + const auto msg_2 = make_message(7, 33, 2002, 4); + + for (size_t gap = 0; gap < message_bits + 2 * decode_block_bits; gap++) + { + SCOPED_TRACE(testing::Message() << "gap=" << gap << " bits"); + std::vector stream(idle_bits, 0); + append_field(stream, preamble, 8); + stream.insert(stream.end(), gap, 0); + append(stream, msg_1); + append(stream, msg_2); + stream.insert(stream.end(), flush_bits, 0); + + const auto messages = decode(stream, 0, false); + + ASSERT_FALSE(messages.empty()); + EXPECT_EQ(messages.size(), 2U); + EXPECT_EQ(messages[0].prn, 7U); + EXPECT_EQ(messages[0].msg_id, 30U); + EXPECT_EQ(messages[0].tow, 2001U); + EXPECT_TRUE(same_bits(messages[0], msg_1)); + } +} diff --git a/tests/unit_tests_core.cc b/tests/unit_tests_core.cc index 2691c73eb..3a2a2bd6a 100644 --- a/tests/unit_tests_core.cc +++ b/tests/unit_tests_core.cc @@ -51,6 +51,7 @@ #include "unit-tests/signal-processing-blocks/sources/unpack_2bit_samples_test.cc" #include "unit-tests/signal-processing-blocks/telemetry_decoder/deterministic_telemetry_fault_test.cc" #include "unit-tests/signal-processing-blocks/telemetry_decoder/galileo_fnav_inav_decoder_test.cc" +#include "unit-tests/signal-processing-blocks/telemetry_decoder/gps_cnav_msg_decoder_test.cc" #include "unit-tests/signal-processing-blocks/telemetry_decoder/gps_l1c_telemetry_decoder_test.cc" #include "unit-tests/signal-processing-blocks/telemetry_decoder/sbas_l1_telemetry_decoder_test.cc"