Files
brnQuanFW/app/pocsag.c
T
2026-11-14 23:55:43 +01:00

240 lines
7.0 KiB
C

#include "app/pocsag.h"
#include <string.h>
#define POCSAG_BCH_POLYNOMIAL 0x769U // x^10 + x^9 + x^8 + x^6 + x^5 + x^3 + 1
#define POCSAG_FUNCTION_ALPHA 3U
#define POCSAG_MESSAGE_WORDS ((POCSAG_MAX_MESSAGE_LENGTH * 7U + 19U) / 20U)
#define POCSAG_TOTAL_SLOTS (POCSAG_BATCH_COUNT * POCSAG_BATCH_WORDS)
// Worst case: address in the last slot of frame 7, followed by a full message.
// There must be room for at least one idle codeword afterwards to terminate it.
_Static_assert(7U * 2U + 1U + POCSAG_MESSAGE_WORDS < POCSAG_TOTAL_SLOTS,
"POCSAG frame too short for the longest message");
static uint8_t parity32(uint32_t value)
{
value ^= value >> 16;
value ^= value >> 8;
value ^= value >> 4;
value ^= value >> 2;
value ^= value >> 1;
return value & 1U;
}
// information: 21 bits (flag bit 20, then 20 data bits)
static uint32_t make_codeword(const uint32_t information)
{
uint32_t remainder = information << 10;
uint32_t codeword;
for (int bit = 30; bit >= 10; bit--) {
if (remainder & (1UL << bit))
remainder ^= (uint32_t)POCSAG_BCH_POLYNOMIAL << (bit - 10);
}
codeword = (information << 11) | ((remainder & 0x3FFU) << 1);
return codeword | parity32(codeword);
}
static void write_word(uint8_t *output, const uint32_t word)
{
output[0] = (uint8_t)(word >> 24);
output[1] = (uint8_t)(word >> 16);
output[2] = (uint8_t)(word >> 8);
output[3] = (uint8_t)word;
}
static uint32_t read_word(const uint8_t *input, const size_t bit_offset, const bool invert)
{
uint32_t word = 0;
for (unsigned bit = 0; bit < 32; bit++) {
const size_t position = bit_offset + bit;
word = (word << 1) | ((input[position >> 3] >> (7U - (position & 7U))) & 1U);
}
return invert ? ~word : word;
}
static bool is_valid_codeword(const uint32_t word)
{
return make_codeword(word >> 11) == word;
}
// Accepts a codeword with at most one bit error.
static bool correct_codeword(uint32_t *word)
{
if (is_valid_codeword(*word))
return true;
for (unsigned bit = 0; bit < 32; bit++) {
const uint32_t candidate = *word ^ (1UL << bit);
if (is_valid_codeword(candidate)) {
*word = candidate;
return true;
}
}
return false;
}
static void terminate(char *message, const size_t message_capacity, const size_t length)
{
message[length < message_capacity ? length : message_capacity - 1U] = '\0';
}
static bool decode_after_address(const uint8_t *input, const size_t input_bits,
const size_t address_bit, const bool invert, const uint32_t capcode,
const unsigned frame, uint32_t *sender, char *message, const size_t message_capacity)
{
uint32_t address = read_word(input, address_bit, invert);
if (address == POCSAG_IDLE_WORD || !correct_codeword(&address) || (address >> 31) != 0)
return false;
const uint32_t found_capcode = (((address >> 13) & 0x3FFFFUL) << 3) | frame;
if (found_capcode != capcode)
return false;
if (sender != NULL)
*sender = found_capcode;
size_t length = 0;
uint32_t payload = 0;
unsigned payload_bits = 0;
bool found_message = false;
for (size_t position = address_bit + 32U; position + 32U <= input_bits; position += 32U) {
uint32_t codeword = read_word(input, position, invert);
// Messages may continue across a batch boundary.
if (codeword == POCSAG_SYNC_WORD)
continue;
// Idle or a new address codeword ends the message.
if (!correct_codeword(&codeword) || (codeword >> 31) == 0)
break;
found_message = true;
payload = (payload << 20) | ((codeword >> 11) & 0xFFFFFUL);
payload_bits += 20U;
while (payload_bits >= 7U) {
payload_bits -= 7U;
const uint8_t bits = (uint8_t)((payload >> payload_bits) & 0x7FU);
payload &= (1UL << payload_bits) - 1U;
// characters are transmitted LSB first
uint8_t character = 0;
for (unsigned i = 0; i < 7U; i++)
character |= ((bits >> (6U - i)) & 1U) << i;
if (character == '\0' || character == 0x04U) { // NUL padding or EOT
terminate(message, message_capacity, length);
return true;
}
if (length + 1U < message_capacity)
message[length] = (char)character;
length++;
}
}
terminate(message, message_capacity, length);
return found_message;
}
bool POCSAG_Encode(const uint32_t capcode, const char *message, uint8_t *output,
const size_t output_capacity, size_t *output_size)
{
uint32_t slots[POCSAG_TOTAL_SLOTS];
uint32_t message_words[POCSAG_MESSAGE_WORDS];
size_t message_length;
if (output_size != NULL)
*output_size = 0;
if (message == NULL || output == NULL || capcode > POCSAG_MAX_RIC ||
output_capacity < POCSAG_MAX_FRAME_SIZE)
return false;
message_length = strlen(message);
if (message_length == 0 || message_length > POCSAG_MAX_MESSAGE_LENGTH)
return false;
// Pack 7 bit characters LSB first into 20 bit message chunks (zero padded).
memset(message_words, 0, sizeof(message_words));
for (size_t character = 0; character < message_length; character++) {
const uint8_t value = (uint8_t)message[character];
if (value < 0x20U || value > 0x7EU)
return false;
for (unsigned bit = 0; bit < 7U; bit++) {
const size_t bit_index = character * 7U + bit;
if ((value >> bit) & 1U)
message_words[bit_index / 20U] |= 1UL << (19U - (bit_index % 20U));
}
}
const size_t message_word_count = (message_length * 7U + 19U) / 20U;
for (size_t i = 0; i < POCSAG_TOTAL_SLOTS; i++)
slots[i] = POCSAG_IDLE_WORD;
// Address codeword: flag 0, upper 18 bits of the capcode, 2 function bits.
// The lower 3 bits of the capcode select the frame (slot pair) within the batch.
size_t slot = (capcode & 7U) * 2U;
slots[slot++] = make_codeword((((capcode >> 3) & 0x3FFFFUL) << 2) | POCSAG_FUNCTION_ALPHA);
for (size_t i = 0; i < message_word_count; i++)
slots[slot++] = make_codeword((1UL << 20) | message_words[i]);
memset(output, 0xAA, POCSAG_PREAMBLE_BYTES);
uint8_t *out = output + POCSAG_PREAMBLE_BYTES;
for (size_t i = 0; i < POCSAG_TOTAL_SLOTS; i++) {
if (i % POCSAG_BATCH_WORDS == 0) {
write_word(out, POCSAG_SYNC_WORD);
out += 4;
}
write_word(out, slots[i]);
out += 4;
}
if (output_size != NULL)
*output_size = POCSAG_MAX_FRAME_SIZE;
return true;
}
bool POCSAG_Decode(const uint8_t *input, const size_t input_size, const uint32_t capcode,
uint32_t *sender, char *message, const size_t message_capacity)
{
const size_t input_bits = input_size * 8U;
if (input == NULL || message == NULL || message_capacity == 0 || capcode > POCSAG_MAX_RIC)
return false;
message[0] = '\0';
for (size_t bit = 0; bit + 32U <= input_bits; bit++) {
const uint32_t word = read_word(input, bit, false);
bool invert;
if (word == POCSAG_SYNC_WORD)
invert = false;
else if (word == (uint32_t)~POCSAG_SYNC_WORD)
invert = true;
else
continue;
const size_t first_codeword = bit + 32U;
for (size_t slot = 0; slot < POCSAG_BATCH_WORDS; slot++) {
const size_t address_bit = first_codeword + slot * 32U;
if (address_bit + 32U > input_bits)
break;
if (decode_after_address(input, input_bits, address_bit, invert, capcode,
(unsigned)(slot / 2U), sender, message, message_capacity))
return true;
}
bit += 32U * (POCSAG_BATCH_WORDS + 1U) - 1U; // continue at the next batch
}
return false;
}