// Host-side test: cc -std=c11 -I. tests/test_pocsag.c app/pocsag.c -o test_pocsag && ./test_pocsag #include "app/pocsag.h" #include #include #include #include // Known-good page (from the uv-k5-pocsag reference): RIC 260321, "test 1234 miau! :3", // followed by a second page in the next batch. Logical bit order, MSB first. static const uint8_t reference_batch[] = { 0x7C, 0xD2, 0x15, 0xD8, 0x7A, 0x89, 0xC1, 0x97, 0x7A, 0x89, 0xC1, 0x97, 0x0F, 0xE3, 0x9E, 0x99, 0x97, 0xA7, 0x9B, 0x51, 0xCB, 0x82, 0x8F, 0x72, 0xC9, 0xB3, 0x13, 0x0C, 0xE0, 0x56, 0xE2, 0x79, 0xDC, 0x3A, 0xF1, 0xB4, 0x88, 0x12, 0xEB, 0xE1, 0xCC, 0x00, 0x01, 0x97, 0x7A, 0x89, 0xC1, 0x97, 0x7A, 0x89, 0xC1, 0x97, 0x7A, 0x89, 0xC1, 0x97, 0x7A, 0x89, 0xC1, 0x97, 0x7A, 0x89, 0xC1, 0x97, 0x7A, 0x89, 0xC1, 0x97, }; static void invert(uint8_t *buffer, size_t size) { for (size_t i = 0; i < size; i++) buffer[i] = (uint8_t)~buffer[i]; } int main(void) { static const uint32_t capcodes[] = {0, 1, 7, 8, 123456, 260321, 0x1FFFFF}; uint8_t frame[POCSAG_MAX_FRAME_SIZE]; char decoded[POCSAG_MAX_MESSAGE_LENGTH + 1U]; uint32_t sender; size_t frame_size; // Encoder must match the reference transmitter bit for bit. assert(POCSAG_Encode(260321U, "test 1234 miau! :3", frame, sizeof(frame), &frame_size)); assert(frame_size == POCSAG_MAX_FRAME_SIZE); for (size_t i = 0; i < POCSAG_PREAMBLE_BYTES; i++) assert(frame[i] == 0xAA); if (memcmp(frame + POCSAG_PREAMBLE_BYTES, reference_batch, sizeof(reference_batch)) != 0) { fprintf(stderr, "encoder output differs from reference batch\n"); return 1; } // Decoder must read the reference. assert(POCSAG_Decode(reference_batch, sizeof(reference_batch), 260321U, &sender, decoded, sizeof(decoded))); assert(sender == 260321U); assert(strcmp(decoded, "test 1234 miau! :3") == 0); assert(!POCSAG_Decode(reference_batch, sizeof(reference_batch), 260320U, NULL, decoded, sizeof(decoded))); for (size_t i = 0; i < sizeof(capcodes) / sizeof(capcodes[0]); i++) { static const char *messages[] = {"Hello pager", "12345678901234567890", "A"}; for (size_t m = 0; m < sizeof(messages) / sizeof(messages[0]); m++) { assert(POCSAG_Encode(capcodes[i], messages[m], frame, sizeof(frame), &frame_size)); assert(frame_size == POCSAG_MAX_FRAME_SIZE); sender = 0xFFFFFFFFU; if (!POCSAG_Decode(frame, frame_size, capcodes[i], &sender, decoded, sizeof(decoded)) || strcmp(decoded, messages[m]) != 0) { fprintf(stderr, "decode failed for capcode %lu '%s'\n", (unsigned long)capcodes[i], messages[m]); return 1; } assert(sender == capcodes[i]); // What the radio's RX buffer looks like: sync + 128 bytes after the chip sync. const uint8_t *rx = frame + POCSAG_PREAMBLE_BYTES; assert(POCSAG_Decode(rx, 4U + 128U, capcodes[i], NULL, decoded, sizeof(decoded))); assert(strcmp(decoded, messages[m]) == 0); // Single bit error in the address codeword gets corrected. const size_t address_byte = POCSAG_PREAMBLE_BYTES + 4U + (capcodes[i] & 7U) * 8U + 1U; frame[address_byte] ^= 0x10U; assert(POCSAG_Decode(frame, frame_size, capcodes[i], NULL, decoded, sizeof(decoded))); assert(strcmp(decoded, messages[m]) == 0); frame[address_byte] ^= 0x10U; // Inverted polarity is detected via the inverted sync word. invert(frame, frame_size); assert(POCSAG_Decode(frame, frame_size, capcodes[i], NULL, decoded, sizeof(decoded))); assert(strcmp(decoded, messages[m]) == 0); invert(frame, frame_size); assert(!POCSAG_Decode(frame, frame_size, capcodes[i] ^ 0x100U, NULL, decoded, sizeof(decoded))); } } // Radio RX: the chip locks anywhere in the preamble and the capture is cut off // when the transmission ends (decoded on squelch close), in both FIFO byte orders. assert(POCSAG_Encode(1234567U, "12345678901234567890", frame, sizeof(frame), NULL)); for (size_t lock = 0; lock < POCSAG_PREAMBLE_BYTES; lock++) { const size_t size = sizeof(frame) - lock; assert(POCSAG_Decode(frame + lock, size, 1234567U, NULL, decoded, sizeof(decoded))); assert(strcmp(decoded, "12345678901234567890") == 0); } // Sync codeword with 2 bit errors (both batches) still decodes, also inverted. assert(POCSAG_Encode(1234567U, "sync errors", frame, sizeof(frame), NULL)); frame[POCSAG_PREAMBLE_BYTES] ^= 0x81U; frame[POCSAG_PREAMBLE_BYTES + POCSAG_BATCH_BYTES + 2U] ^= 0x18U; assert(POCSAG_Decode(frame, sizeof(frame), 1234567U, NULL, decoded, sizeof(decoded))); assert(strcmp(decoded, "sync errors") == 0); invert(frame, sizeof(frame)); assert(POCSAG_Decode(frame, sizeof(frame), 1234567U, NULL, decoded, sizeof(decoded))); assert(strcmp(decoded, "sync errors") == 0); // Monitor mode: any address decodes and reports the RIC. assert(POCSAG_Encode(1234567U, "Ahoj", frame, sizeof(frame), NULL)); assert(POCSAG_Decode(frame, sizeof(frame), POCSAG_ANY_RIC, &sender, decoded, sizeof(decoded))); assert(sender == 1234567U && strcmp(decoded, "Ahoj") == 0); assert(!POCSAG_Encode(0x200000U, "bad", frame, sizeof(frame), NULL)); assert(!POCSAG_Encode(123U, "", frame, sizeof(frame), NULL)); // longest message, for every frame position, then one character too many { char longest[POCSAG_MAX_MESSAGE_LENGTH + 2U]; for (size_t i = 0; i < POCSAG_MAX_MESSAGE_LENGTH + 1U; i++) longest[i] = (char) ('A' + i % 26U); longest[POCSAG_MAX_MESSAGE_LENGTH] = '\0'; for (uint32_t ric = 0; ric < 8U; ric++) { assert(POCSAG_Encode(1000000U + ric, longest, frame, sizeof(frame), NULL)); assert(POCSAG_Decode(frame, sizeof(frame), 1000000U + ric, NULL, decoded, sizeof(decoded))); assert(strcmp(decoded, longest) == 0); } longest[POCSAG_MAX_MESSAGE_LENGTH] = 'X'; longest[POCSAG_MAX_MESSAGE_LENGTH + 1U] = '\0'; assert(!POCSAG_Encode(123U, longest, frame, sizeof(frame), NULL)); } assert(!POCSAG_Encode(123U, "ok", frame, sizeof(frame) - 1U, NULL)); puts("pocsag tests passed"); return 0; }